src/sync.c
author Edouard Tisserant <edouard@pep-project.org>
Tue, 13 Sep 2016 16:35:17 +0200
branchkeysync
changeset 1160 a176739cd3c3
parent 1129 d928a7221b96
child 1167 781218f0c0d0
permissions -rw-r--r--
Added partner parameter to deliverHandshakeResult
vb@572
     1
#include "pEp_internal.h"
vb@572
     2
vb@572
     3
#include <memory.h>
vb@572
     4
#include <assert.h>
vb@572
     5
vb@1116
     6
#include "asn1_helper.h"
vb@951
     7
#include "../asn.1/DeviceGroup-Protocol.h"
vb@951
     8
vb@1043
     9
// receive_sync_msg is defined in the sync_actions
vb@1043
    10
vb@1043
    11
PEP_STATUS receive_sync_msg(
vb@1043
    12
        PEP_SESSION session,
vb@1043
    13
        DeviceGroup_Protocol_t *msg
vb@1043
    14
    );
vb@572
    15
vb@572
    16
DYNAMIC_API PEP_STATUS register_sync_callbacks(
vb@599
    17
        PEP_SESSION session,
vb@599
    18
        void *obj,
vb@597
    19
        messageToSend_t messageToSend,
vb@1043
    20
        showHandshake_t showHandshake,
vb@1043
    21
        inject_sync_msg_t inject_sync_msg,
vb@1043
    22
        retrieve_next_sync_msg_t retrieve_next_sync_msg
vb@572
    23
    )
vb@572
    24
{
vb@1091
    25
    unsigned char uuid[16];
vb@1091
    26
    uuid_generate_random(uuid);
vb@1091
    27
    uuid_unparse_upper(uuid, sync_uuid);
vb@1091
    28
vb@599
    29
    session->sync_obj = obj;
vb@604
    30
    session->messageToSend = messageToSend;
vb@604
    31
    session->showHandshake = showHandshake;
vb@1043
    32
    session->inject_sync_msg = inject_sync_msg;
vb@1043
    33
    session->retrieve_next_sync_msg = retrieve_next_sync_msg;
vb@572
    34
vb@976
    35
    // start state machine
vb@976
    36
    session->sync_state = InitState;
vb@976
    37
    PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
vb@987
    38
    if (status != PEP_STATUS_OK)
vb@987
    39
        unregister_sync_callbacks(session);
vb@976
    40
vb@976
    41
    return status;
vb@572
    42
}
vb@572
    43
vb@572
    44
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
vb@986
    45
    // stop state machine
vb@986
    46
    session->sync_state = DeviceState_state_NONE;
vb@986
    47
vb@986
    48
    // unregister
vb@602
    49
    session->sync_obj = NULL;
vb@604
    50
    session->messageToSend = NULL;
vb@604
    51
    session->showHandshake = NULL;
vb@1043
    52
    session->retrieve_next_sync_msg = NULL;
vb@572
    53
}
vb@572
    54
vb@679
    55
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
vb@609
    56
        PEP_SESSION session,
edouard@1160
    57
        Identity partner,
vb@609
    58
        sync_handshake_result result
vb@609
    59
    )
vb@609
    60
{
vb@609
    61
    assert(session);
vb@609
    62
    if (!session)
vb@609
    63
        return PEP_ILLEGAL_VALUE;
vb@609
    64
vb@1043
    65
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    66
vb@609
    67
    switch (result) {
vb@609
    68
        case SYNC_HANDSHAKE_CANCEL:
vb@1043
    69
            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
vb@609
    70
            break;
vb@609
    71
        case SYNC_HANDSHAKE_ACCEPTED:
edouard@1160
    72
            status = fsm_DeviceState_inject(session, HandshakeAccepted, partner, 0);
vb@609
    73
            break;
vb@609
    74
        case SYNC_HANDSHAKE_REJECTED:
edouard@1160
    75
            status = fsm_DeviceState_inject(session, HandshakeRejected, partner, 0);
vb@609
    76
            break;
vb@609
    77
        default:
vb@609
    78
            return PEP_ILLEGAL_VALUE;
vb@609
    79
    }
vb@609
    80
vb@1043
    81
    return status;
vb@1043
    82
}
vb@1043
    83
vb@1043
    84
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
    85
        PEP_SESSION session,
vb@1043
    86
        void *management
vb@1043
    87
    )
vb@1043
    88
{
vb@1078
    89
    DeviceGroup_Protocol_t *msg = NULL;
vb@1043
    90
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    91
vb@1043
    92
    assert(session && session->retrieve_next_sync_msg);
vb@1043
    93
    assert(management);
vb@1043
    94
vb@1043
    95
    if (!(session && session->retrieve_next_sync_msg) || !management)
vb@1043
    96
        return PEP_ILLEGAL_VALUE;
vb@1043
    97
vb@1043
    98
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
vb@1043
    99
vb@1043
   100
    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
vb@1043
   101
    {
vb@1043
   102
        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
vb@1043
   103
            char buffer[MAX_LINELENGTH];
vb@1043
   104
            memset(buffer, 0, MAX_LINELENGTH);
vb@1043
   105
            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
vb@1043
   106
            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
vb@1043
   107
        }
vb@1043
   108
    }
vb@1043
   109
vb@1043
   110
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
vb@1043
   111
vb@609
   112
    return PEP_STATUS_OK;
vb@609
   113
}
vb@609
   114
vb@1116
   115
DYNAMIC_API PEP_STATUS decode_sync_msg(
vb@1116
   116
        const char *data,
vb@1116
   117
        size_t size,
vb@1116
   118
        char **text
vb@1116
   119
    )
vb@1116
   120
{
vb@1116
   121
    PEP_STATUS status = PEP_STATUS_OK;
vb@1116
   122
vb@1116
   123
    assert(data && text);
vb@1116
   124
    if (!(data && text))
vb@1116
   125
        return PEP_ILLEGAL_VALUE;
vb@1116
   126
vb@1116
   127
    *text = NULL;
vb@1116
   128
vb@1116
   129
    DeviceGroup_Protocol_t *msg = NULL;
vb@1116
   130
    uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **) &msg,
vb@1116
   131
            data, size);
vb@1116
   132
    if (!msg)
vb@1116
   133
        return PEP_SYNC_ILLEGAL_MESSAGE;
vb@1116
   134
vb@1116
   135
    growing_buf_t *dst = new_growing_buf();
vb@1116
   136
    if (!dst) {
vb@1116
   137
        status = PEP_OUT_OF_MEMORY;
vb@1116
   138
        goto the_end;
vb@1116
   139
    }
vb@1116
   140
vb@1116
   141
    asn_enc_rval_t er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg,
vb@1117
   142
            XER_F_BASIC, (asn_app_consume_bytes_f *) consume_bytes, (void *) dst);
vb@1116
   143
    if (er.encoded == -1) {
vb@1128
   144
        status = PEP_CANNOT_ENCODE;
vb@1116
   145
        goto the_end;
vb@1116
   146
    }
vb@1116
   147
vb@1116
   148
    *text = dst->data;
vb@1116
   149
    dst->data = NULL;
vb@1116
   150
vb@1116
   151
the_end:
vb@1116
   152
    free_growing_buf(dst);
vb@1116
   153
    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1116
   154
    return status;
vb@1116
   155
}
vb@1116
   156
vb@1128
   157
DYNAMIC_API PEP_STATUS encode_sync_msg(
vb@1128
   158
        const char *text,
vb@1128
   159
        char **data,
vb@1128
   160
        size_t *size
vb@1128
   161
    )
vb@1128
   162
{
vb@1128
   163
    PEP_STATUS status = PEP_STATUS_OK;
vb@1128
   164
vb@1128
   165
    assert(text && data && size);
vb@1128
   166
    if (!(text && data && size))
vb@1128
   167
        return PEP_ILLEGAL_VALUE;
vb@1128
   168
vb@1129
   169
    *data = NULL;
vb@1129
   170
    *size = 0;
vb@1129
   171
vb@1128
   172
    DeviceGroup_Protocol_t *msg = NULL;
vb@1128
   173
    asn_dec_rval_t dr = xer_decode(NULL, &asn_DEF_DeviceGroup_Protocol,
vb@1128
   174
            (void **) &msg, (const void *) text, strlen(text));
vb@1128
   175
    if (dr.code != RC_OK) {
vb@1128
   176
        status = PEP_SYNC_ILLEGAL_MESSAGE;
vb@1128
   177
        goto the_end;
vb@1128
   178
    }
vb@1128
   179
vb@1128
   180
    char *payload = NULL;
vb@1128
   181
    ssize_t _size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
vb@1128
   182
            NULL, msg, (void **) &payload);
vb@1128
   183
    if (_size == -1) {
vb@1128
   184
        status = PEP_CANNOT_ENCODE;
vb@1128
   185
        goto the_end;
vb@1128
   186
    }
vb@1128
   187
vb@1128
   188
    *data = payload;
vb@1128
   189
    *size = (size_t) _size;
vb@1128
   190
vb@1128
   191
the_end:
vb@1128
   192
    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1128
   193
    return status;
vb@1128
   194
}
vb@1128
   195