src/sync.c
author Volker Birk <vb@pep.foundation>
Thu, 01 Sep 2016 22:47:08 +0200
branchkeysync
changeset 1116 c9a03f2423c1
parent 1091 0571ce4b97a2
child 1117 afcee607d04d
permissions -rw-r--r--
decode_sync_msg() for debugging
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,
vb@609
    57
        sync_handshake_result result
vb@609
    58
    )
vb@609
    59
{
vb@609
    60
    assert(session);
vb@609
    61
    if (!session)
vb@609
    62
        return PEP_ILLEGAL_VALUE;
vb@609
    63
vb@1043
    64
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    65
vb@609
    66
    switch (result) {
vb@609
    67
        case SYNC_HANDSHAKE_CANCEL:
vb@1043
    68
            status = fsm_DeviceState_inject(session, Cancel, NULL, 0);
vb@609
    69
            break;
vb@609
    70
        case SYNC_HANDSHAKE_ACCEPTED:
vb@1043
    71
            status = fsm_DeviceState_inject(session, HandshakeAccepted, NULL, 0);
vb@609
    72
            break;
vb@609
    73
        case SYNC_HANDSHAKE_REJECTED:
vb@1043
    74
            status = fsm_DeviceState_inject(session, HandshakeRejected, NULL, 0);
vb@609
    75
            break;
vb@609
    76
        default:
vb@609
    77
            return PEP_ILLEGAL_VALUE;
vb@609
    78
    }
vb@609
    79
vb@1043
    80
    return status;
vb@1043
    81
}
vb@1043
    82
vb@1043
    83
DYNAMIC_API PEP_STATUS do_sync_protocol(
vb@1043
    84
        PEP_SESSION session,
vb@1043
    85
        void *management
vb@1043
    86
    )
vb@1043
    87
{
vb@1078
    88
    DeviceGroup_Protocol_t *msg = NULL;
vb@1043
    89
    PEP_STATUS status = PEP_STATUS_OK;
vb@1043
    90
vb@1043
    91
    assert(session && session->retrieve_next_sync_msg);
vb@1043
    92
    assert(management);
vb@1043
    93
vb@1043
    94
    if (!(session && session->retrieve_next_sync_msg) || !management)
vb@1043
    95
        return PEP_ILLEGAL_VALUE;
vb@1043
    96
vb@1043
    97
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
vb@1043
    98
vb@1043
    99
    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
vb@1043
   100
    {
vb@1043
   101
        if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
vb@1043
   102
            char buffer[MAX_LINELENGTH];
vb@1043
   103
            memset(buffer, 0, MAX_LINELENGTH);
vb@1043
   104
            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
vb@1043
   105
            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
vb@1043
   106
        }
vb@1043
   107
    }
vb@1043
   108
vb@1043
   109
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
vb@1043
   110
vb@609
   111
    return PEP_STATUS_OK;
vb@609
   112
}
vb@609
   113
vb@1116
   114
DYNAMIC_API PEP_STATUS decode_sync_msg(
vb@1116
   115
        const char *data,
vb@1116
   116
        size_t size,
vb@1116
   117
        char **text
vb@1116
   118
    )
vb@1116
   119
{
vb@1116
   120
    PEP_STATUS status = PEP_STATUS_OK;
vb@1116
   121
vb@1116
   122
    assert(data && text);
vb@1116
   123
    if (!(data && text))
vb@1116
   124
        return PEP_ILLEGAL_VALUE;
vb@1116
   125
vb@1116
   126
    *text = NULL;
vb@1116
   127
vb@1116
   128
    DeviceGroup_Protocol_t *msg = NULL;
vb@1116
   129
    uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol, (void **) &msg,
vb@1116
   130
            data, size);
vb@1116
   131
    if (!msg)
vb@1116
   132
        return PEP_SYNC_ILLEGAL_MESSAGE;
vb@1116
   133
vb@1116
   134
    growing_buf_t *dst = new_growing_buf();
vb@1116
   135
    if (!dst) {
vb@1116
   136
        status = PEP_OUT_OF_MEMORY;
vb@1116
   137
        goto the_end;
vb@1116
   138
    }
vb@1116
   139
vb@1116
   140
    asn_enc_rval_t er = xer_encode(&asn_DEF_DeviceGroup_Protocol, msg,
vb@1116
   141
            XER_F_BASIC, (asn_app_consume_bytes_f *) consume_bytes, (void *) &dst);
vb@1116
   142
    if (er.encoded == -1) {
vb@1116
   143
        status = PEP_SYNC_ILLEGAL_MESSAGE;
vb@1116
   144
        goto the_end;
vb@1116
   145
    }
vb@1116
   146
vb@1116
   147
    *text = dst->data;
vb@1116
   148
    dst->data = NULL;
vb@1116
   149
vb@1116
   150
the_end:
vb@1116
   151
    free_growing_buf(dst);
vb@1116
   152
    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
vb@1116
   153
    return status;
vb@1116
   154
}
vb@1116
   155