src/sync.c
author Volker Birk <vb@pep.foundation>
Sat, 11 Aug 2018 21:45:50 +0200
branchsync
changeset 2839 d9f7cb22f1d1
parent 2838 7eef15a2c61e
child 2840 b881c48c9e9d
permissions -rw-r--r--
...
krista@2271
     1
// This file is under GNU General Public License 3.0
krista@2271
     2
// see LICENSE.txt
krista@2271
     3
krista@2271
     4
#include "pEp_internal.h"
krista@2271
     5
krista@2271
     6
#include <memory.h>
krista@2271
     7
#include <assert.h>
krista@2271
     8
vb@2830
     9
#include "KeySync_fsm.h"
krista@2271
    10
krista@2271
    11
DYNAMIC_API PEP_STATUS register_sync_callbacks(
krista@2271
    12
        PEP_SESSION session,
krista@2271
    13
        void *management,
krista@2271
    14
        notifyHandshake_t notifyHandshake,
vb@2839
    15
        inject_sync_event_t inject_sync_event,
vb@2839
    16
        retrieve_next_sync_event_t retrieve_next_sync_event
krista@2271
    17
    )
krista@2271
    18
{
vb@2839
    19
    assert(session && management && notifyHandshake && inject_sync_event && retrieve_next_sync_event);
vb@2839
    20
    if (!(session && management && notifyHandshake && inject_sync_event && retrieve_next_sync_event))
krista@2271
    21
        return PEP_ILLEGAL_VALUE;
krista@2271
    22
krista@2271
    23
    session->sync_management = management;
krista@2271
    24
    session->notifyHandshake = notifyHandshake;
vb@2839
    25
    session->inject_sync_event = inject_sync_event;
vb@2839
    26
    session->retrieve_next_sync_event = retrieve_next_sync_event;
krista@2271
    27
vb@2839
    28
    return PEP_STATUS_OK;
krista@2271
    29
}
krista@2271
    30
krista@2271
    31
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
krista@2271
    32
    // stop state machine
vb@2837
    33
    free_Sync_state(session);
krista@2271
    34
krista@2271
    35
    // unregister
krista@2271
    36
    session->sync_management = NULL;
krista@2271
    37
    session->notifyHandshake = NULL;
vb@2839
    38
    session->inject_sync_event = NULL;
vb@2839
    39
    session->retrieve_next_sync_event = NULL;
krista@2271
    40
}
krista@2271
    41
krista@2271
    42
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
krista@2271
    43
        PEP_SESSION session,
vb@2830
    44
        pEp_identity *partner,
krista@2271
    45
        sync_handshake_result result
krista@2271
    46
    )
krista@2271
    47
{
krista@2271
    48
    assert(session);
krista@2271
    49
    if (!session)
krista@2271
    50
        return PEP_ILLEGAL_VALUE;
krista@2271
    51
krista@2271
    52
    PEP_STATUS status = PEP_STATUS_OK;
krista@2271
    53
vb@2830
    54
    int event;
krista@2271
    55
    bool need_partner = false;
krista@2271
    56
krista@2271
    57
    switch (result) {
krista@2271
    58
        case SYNC_HANDSHAKE_CANCEL:
krista@2271
    59
            event = Cancel;
krista@2271
    60
            break;
krista@2271
    61
        case SYNC_HANDSHAKE_ACCEPTED:
krista@2271
    62
        {
vb@2830
    63
            event = Accept;
krista@2271
    64
            break;
krista@2271
    65
        }
krista@2271
    66
        case SYNC_HANDSHAKE_REJECTED:
krista@2271
    67
        {
vb@2830
    68
            event = Reject;
krista@2271
    69
            break;
krista@2271
    70
        }
krista@2271
    71
        default:
krista@2271
    72
            return PEP_ILLEGAL_VALUE;
krista@2271
    73
    }
krista@2271
    74
krista@2271
    75
    pEp_identity *_partner = NULL;
krista@2271
    76
    if(need_partner){
krista@2271
    77
        _partner = identity_dup(partner);
krista@2271
    78
        if (_partner == NULL)
krista@2271
    79
            return PEP_OUT_OF_MEMORY;
krista@2271
    80
    }
vb@2834
    81
    status = send_Sync_message(session, Sync_PR_keysync, event);
krista@2271
    82
krista@2271
    83
    return status;
krista@2271
    84
}
krista@2271
    85
krista@2271
    86
DYNAMIC_API PEP_STATUS do_sync_protocol(
krista@2271
    87
        PEP_SESSION session,
krista@2271
    88
        void *obj
krista@2271
    89
    )
krista@2271
    90
{
vb@2836
    91
    Sync_t *msg = NULL;
krista@2271
    92
    PEP_STATUS status = PEP_STATUS_OK;
krista@2271
    93
vb@2839
    94
    assert(session && session->retrieve_next_sync_event);
vb@2839
    95
    if (!(session && session->retrieve_next_sync_event))
krista@2271
    96
        return PEP_ILLEGAL_VALUE;
krista@2271
    97
krista@2271
    98
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
krista@2271
    99
krista@2271
   100
    session->sync_obj = obj;
krista@2271
   101
    while (true) 
krista@2271
   102
    {
vb@2839
   103
        event = session->retrieve_next_sync_event(session->sync_management);
vb@2834
   104
        if (msg == NULL)
krista@2271
   105
            break;
vb@2834
   106
vb@2836
   107
        status = recv_Sync_event(session, msg);
vb@2834
   108
        if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
krista@2271
   109
            char buffer[MAX_LINELENGTH];
krista@2271
   110
            memset(buffer, 0, MAX_LINELENGTH);
vb@2834
   111
            snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
krista@2271
   112
            log_event(session, buffer, "pEp sync protocol", NULL, NULL);
krista@2271
   113
        }
krista@2271
   114
    }
vb@2834
   115
    session->sync_obj = NULL;
krista@2271
   116
krista@2271
   117
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
krista@2271
   118
krista@2271
   119
    return PEP_STATUS_OK;
krista@2271
   120
}
krista@2271
   121