src/sync_api.c
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 2899 63b619aef131
parent 2879 1b90ffed2d89
child 2909 13da9af3bd4f
permissions -rw-r--r--
somewhat bigger
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
        retrieve_next_sync_event_t retrieve_next_sync_event
krista@2271
    16
    )
krista@2271
    17
{
vb@2879
    18
    assert(session && management && notifyHandshake && retrieve_next_sync_event);
vb@2879
    19
    if (!(session && management && notifyHandshake && retrieve_next_sync_event))
krista@2271
    20
        return PEP_ILLEGAL_VALUE;
krista@2271
    21
krista@2271
    22
    session->sync_management = management;
krista@2271
    23
    session->notifyHandshake = notifyHandshake;
vb@2839
    24
    session->retrieve_next_sync_event = retrieve_next_sync_event;
krista@2271
    25
vb@2850
    26
    // start state machine
vb@2850
    27
    return Sync_driver(session, Sync_PR_keysync, Init);
krista@2271
    28
}
krista@2271
    29
krista@2271
    30
DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
krista@2271
    31
    // stop state machine
vb@2837
    32
    free_Sync_state(session);
krista@2271
    33
krista@2271
    34
    // unregister
krista@2271
    35
    session->sync_management = NULL;
krista@2271
    36
    session->notifyHandshake = NULL;
vb@2839
    37
    session->retrieve_next_sync_event = NULL;
krista@2271
    38
}
krista@2271
    39
krista@2271
    40
DYNAMIC_API PEP_STATUS deliverHandshakeResult(
krista@2271
    41
        PEP_SESSION session,
vb@2830
    42
        pEp_identity *partner,
krista@2271
    43
        sync_handshake_result result
krista@2271
    44
    )
krista@2271
    45
{
krista@2271
    46
    assert(session);
krista@2271
    47
    if (!session)
krista@2271
    48
        return PEP_ILLEGAL_VALUE;
krista@2271
    49
krista@2271
    50
    PEP_STATUS status = PEP_STATUS_OK;
krista@2271
    51
vb@2830
    52
    int event;
krista@2271
    53
    bool need_partner = false;
krista@2271
    54
krista@2271
    55
    switch (result) {
krista@2271
    56
        case SYNC_HANDSHAKE_CANCEL:
krista@2271
    57
            event = Cancel;
krista@2271
    58
            break;
krista@2271
    59
        case SYNC_HANDSHAKE_ACCEPTED:
krista@2271
    60
        {
vb@2830
    61
            event = Accept;
krista@2271
    62
            break;
krista@2271
    63
        }
krista@2271
    64
        case SYNC_HANDSHAKE_REJECTED:
krista@2271
    65
        {
vb@2830
    66
            event = Reject;
krista@2271
    67
            break;
krista@2271
    68
        }
krista@2271
    69
        default:
krista@2271
    70
            return PEP_ILLEGAL_VALUE;
krista@2271
    71
    }
krista@2271
    72
krista@2271
    73
    pEp_identity *_partner = NULL;
krista@2271
    74
    if(need_partner){
krista@2271
    75
        _partner = identity_dup(partner);
krista@2271
    76
        if (_partner == NULL)
krista@2271
    77
            return PEP_OUT_OF_MEMORY;
krista@2271
    78
    }
vb@2834
    79
    status = send_Sync_message(session, Sync_PR_keysync, event);
krista@2271
    80
krista@2271
    81
    return status;
krista@2271
    82
}
krista@2271
    83
krista@2271
    84
DYNAMIC_API PEP_STATUS do_sync_protocol(
krista@2271
    85
        PEP_SESSION session,
krista@2271
    86
        void *obj
krista@2271
    87
    )
krista@2271
    88
{
vb@2840
    89
    Sync_event_t *event= NULL;
krista@2271
    90
vb@2839
    91
    assert(session && session->retrieve_next_sync_event);
vb@2839
    92
    if (!(session && session->retrieve_next_sync_event))
krista@2271
    93
        return PEP_ILLEGAL_VALUE;
krista@2271
    94
krista@2271
    95
    log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
krista@2271
    96
krista@2271
    97
    while (true) 
krista@2271
    98
    {
vb@2839
    99
        event = session->retrieve_next_sync_event(session->sync_management);
vb@2840
   100
        if (!event)
krista@2271
   101
            break;
vb@2834
   102
vb@2899
   103
        do_sync_protocol_step(session, obj, event);
krista@2271
   104
    }
vb@2834
   105
    session->sync_obj = NULL;
krista@2271
   106
krista@2271
   107
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol", NULL, NULL);
krista@2271
   108
krista@2271
   109
    return PEP_STATUS_OK;
krista@2271
   110
}
krista@2271
   111
vb@2899
   112
DYNAMIC_API PEP_STATUS do_sync_protocol_step(
vb@2899
   113
        PEP_SESSION session,
vb@2899
   114
        void *obj,
vb@2899
   115
        SYNC_EVENT event
vb@2899
   116
    )
vb@2899
   117
{
vb@2899
   118
    assert(session);
vb@2899
   119
    if (!session)
vb@2899
   120
        return PEP_ILLEGAL_VALUE;
vb@2899
   121
vb@2899
   122
    if (!event)
vb@2899
   123
        return PEP_STATUS_OK;
vb@2899
   124
vb@2899
   125
    session->sync_obj = obj;
vb@2899
   126
vb@2899
   127
    PEP_STATUS status = recv_Sync_event(session, event);
vb@2899
   128
    if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
vb@2899
   129
        char buffer[MAX_LINELENGTH];
vb@2899
   130
        memset(buffer, 0, MAX_LINELENGTH);
vb@2899
   131
        snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n", (int) status);
vb@2899
   132
        log_event(session, buffer, "pEp sync protocol", NULL, NULL);
vb@2899
   133
    }
vb@2899
   134
vb@2899
   135
    return status == PEP_MESSAGE_IGNORE ? PEP_STATUS_OK : status;
vb@2899
   136
}
vb@2899
   137
vb@2899
   138
DYNAMIC_API bool is_sync_thread(PEP_SESSION session)
vb@2899
   139
{
vb@2899
   140
    assert(session);
vb@2899
   141
    if (!session)
vb@2899
   142
        return false;
vb@2899
   143
    return session->retrieve_next_sync_event != NULL;
vb@2899
   144
}
vb@2899
   145