src/sync_api.c
author Volker Birk <vb@pep-project.org>
Wed, 27 Mar 2019 20:17:29 +0100
branchsync
changeset 3407 dabc9c3b13e6
parent 3406 c7540a618a4c
child 3408 78506867a9c0
permissions -rw-r--r--
adding param to deliverHandshakeResult
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@2992
    18
    assert(session && notifyHandshake && retrieve_next_sync_event);
vb@2992
    19
    if (!(session && 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@3407
    42
        sync_handshake_result result,
vb@3407
    43
        const identity_list *identities_sharing
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
vb@3407
    50
    for (const identity_list *_il = identities_sharing; _il && _il->ident;
vb@3407
    51
            _il = _il->next) {
vb@3407
    52
        if (!_il->ident->me || !_il->ident->user_id || !_il->ident->user_id[0]
vb@3407
    53
                || !_il->ident->address || !_il->ident->address[0])
vb@3407
    54
            return PEP_ILLEGAL_VALUE;
vb@3407
    55
    }
vb@3407
    56
krista@2271
    57
    PEP_STATUS status = PEP_STATUS_OK;
vb@2830
    58
    int event;
krista@2271
    59
krista@2271
    60
    switch (result) {
krista@2271
    61
        case SYNC_HANDSHAKE_CANCEL:
krista@2271
    62
            event = Cancel;
krista@2271
    63
            break;
krista@2271
    64
        case SYNC_HANDSHAKE_ACCEPTED:
krista@2271
    65
        {
vb@2830
    66
            event = Accept;
krista@2271
    67
            break;
krista@2271
    68
        }
krista@2271
    69
        case SYNC_HANDSHAKE_REJECTED:
krista@2271
    70
        {
vb@2830
    71
            event = Reject;
krista@2271
    72
            break;
krista@2271
    73
        }
krista@2271
    74
        default:
krista@2271
    75
            return PEP_ILLEGAL_VALUE;
krista@2271
    76
    }
krista@2271
    77
vb@3407
    78
    free_identity_list(session->sync_state.common.own_identities);
vb@3407
    79
    if (identities_sharing) {
vb@3407
    80
        session->sync_state.common.own_identities = identity_list_dup(identities_sharing);
vb@3407
    81
        if (session->sync_state.common.own_identities)
vb@3407
    82
            status = PEP_OUT_OF_MEMORY;
vb@3407
    83
    }
vb@3407
    84
    else {
vb@3407
    85
        status = own_identities_retrieve(session, &session->sync_state.common.own_identities);
vb@3407
    86
    }
vb@3407
    87
vb@3407
    88
    if (!status)
vb@3407
    89
        status = signal_Sync_event(session, Sync_PR_keysync, event);
krista@2271
    90
    return status;
krista@2271
    91
}
krista@2271
    92
krista@2271
    93
DYNAMIC_API PEP_STATUS do_sync_protocol(
krista@2271
    94
        PEP_SESSION session,
krista@2271
    95
        void *obj
krista@2271
    96
    )
krista@2271
    97
{
vb@2840
    98
    Sync_event_t *event= NULL;
krista@2271
    99
vb@2839
   100
    assert(session && session->retrieve_next_sync_event);
vb@2839
   101
    if (!(session && session->retrieve_next_sync_event))
krista@2271
   102
        return PEP_ILLEGAL_VALUE;
krista@2271
   103
vb@2909
   104
    log_event(session, "sync_protocol thread started", "pEp sync protocol",
vb@2909
   105
            NULL, NULL);
krista@2271
   106
krista@2271
   107
    while (true) 
krista@2271
   108
    {
vb@2909
   109
        event = session->retrieve_next_sync_event(session->sync_management,
vb@2909
   110
                SYNC_THRESHOLD);
vb@2840
   111
        if (!event)
krista@2271
   112
            break;
vb@2834
   113
vb@2899
   114
        do_sync_protocol_step(session, obj, event);
krista@2271
   115
    }
vb@2834
   116
    session->sync_obj = NULL;
krista@2271
   117
vb@2909
   118
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol",
vb@2909
   119
            NULL, NULL);
krista@2271
   120
krista@2271
   121
    return PEP_STATUS_OK;
krista@2271
   122
}
krista@2271
   123
vb@2899
   124
DYNAMIC_API PEP_STATUS do_sync_protocol_step(
vb@2899
   125
        PEP_SESSION session,
vb@2899
   126
        void *obj,
vb@2899
   127
        SYNC_EVENT event
vb@2899
   128
    )
vb@2899
   129
{
vb@2899
   130
    assert(session);
vb@2899
   131
    if (!session)
vb@2899
   132
        return PEP_ILLEGAL_VALUE;
vb@2899
   133
vb@2899
   134
    if (!event)
vb@2899
   135
        return PEP_STATUS_OK;
vb@2899
   136
vb@2899
   137
    session->sync_obj = obj;
vb@2899
   138
vb@2899
   139
    PEP_STATUS status = recv_Sync_event(session, event);
vb@2899
   140
    return status == PEP_MESSAGE_IGNORE ? PEP_STATUS_OK : status;
vb@2899
   141
}
vb@2899
   142
vb@2899
   143
DYNAMIC_API bool is_sync_thread(PEP_SESSION session)
vb@2899
   144
{
vb@2899
   145
    assert(session);
vb@2899
   146
    if (!session)
vb@2899
   147
        return false;
vb@2899
   148
    return session->retrieve_next_sync_event != NULL;
vb@2899
   149
}
vb@2899
   150
vb@2911
   151
DYNAMIC_API SYNC_EVENT new_sync_timeout_event()
vb@2911
   152
{
vb@2911
   153
    return SYNC_TIMEOUT_EVENT;
vb@2911
   154
}
vb@2911
   155
vb@3406
   156
DYNAMIC_API PEP_STATUS enter_device_group(
vb@3406
   157
        PEP_SESSION session,
vb@3406
   158
        const identity_list *identities_sharing
vb@3406
   159
    )
vb@3406
   160
{
vb@3406
   161
    assert(session);
vb@3406
   162
    if (!session)
vb@3406
   163
        return PEP_ILLEGAL_VALUE;
vb@3406
   164
vb@3406
   165
    for (const identity_list *_il = identities_sharing; _il && _il->ident;
vb@3406
   166
            _il = _il->next) {
vb@3406
   167
        if (!_il->ident->me || !_il->ident->user_id || !_il->ident->user_id[0]
vb@3406
   168
                || !_il->ident->address || !_il->ident->address[0])
vb@3406
   169
            return PEP_ILLEGAL_VALUE;
vb@3406
   170
    }
vb@3406
   171
vb@3406
   172
    identity_list *own_identities = NULL;
vb@3406
   173
    PEP_STATUS status = own_identities_retrieve(session, &own_identities);
vb@3406
   174
    if (status)
vb@3406
   175
        goto the_end;
vb@3406
   176
vb@3406
   177
    if (identities_sharing) {
vb@3406
   178
        for (identity_list *_il = own_identities; _il && _il->ident;
vb@3406
   179
                _il = _il->next) {
vb@3406
   180
            bool found = false;
vb@3406
   181
vb@3406
   182
            for (const identity_list *_is = identities_sharing;
vb@3406
   183
                    _is && _is->ident; _is = _is->next) {
vb@3406
   184
                // FIXME: "john@doe.com" and "mailto:john@doe.com" should be equal
vb@3406
   185
                if (strcmp(_il->ident->address, _is->ident->address) == 0
vb@3406
   186
                        && strcmp(_il->ident->user_id, _is->ident->user_id) == 0) {
vb@3406
   187
                    found = true;
vb@3406
   188
vb@3406
   189
                    status = set_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   190
                    if (status)
vb@3406
   191
                        goto the_end;
vb@3406
   192
vb@3406
   193
                    break;
vb@3406
   194
                }
vb@3406
   195
            }
vb@3406
   196
            if (!found) {
vb@3406
   197
                status = unset_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   198
                if (status)
vb@3406
   199
                    goto the_end;
vb@3406
   200
            }
vb@3406
   201
        }
vb@3406
   202
    }
vb@3406
   203
    else {
vb@3406
   204
        for (identity_list *_il = own_identities; _il && _il->ident;
vb@3406
   205
                _il = _il->next) {
vb@3406
   206
            status = set_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   207
            if (status)
vb@3406
   208
                goto the_end;
vb@3406
   209
        }
vb@3406
   210
    }
vb@3406
   211
vb@3406
   212
the_end:
vb@3406
   213
    free_identity_list(own_identities);
vb@3406
   214
    return status;
vb@3406
   215
}
vb@3406
   216
vb@3406
   217
DYNAMIC_API PEP_STATUS leave_device_group(PEP_SESSION session)
vb@3406
   218
{
vb@3406
   219
    assert(session);
vb@3406
   220
    if (!session)
vb@3406
   221
        return PEP_ILLEGAL_VALUE;
vb@3406
   222
vb@3406
   223
    identity_list *il = NULL;
vb@3406
   224
    PEP_STATUS status = own_identities_retrieve(session, &il);
vb@3406
   225
    if (status)
vb@3406
   226
        goto the_end;
vb@3406
   227
vb@3406
   228
    for (identity_list *_il = il; _il && _il->ident ; _il = _il->next) {
vb@3406
   229
        status = unset_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   230
        if (status)
vb@3406
   231
            goto the_end;
vb@3406
   232
    }
vb@3406
   233
vb@3406
   234
the_end:
vb@3406
   235
    free_identity_list(il);
vb@3406
   236
    return status;
vb@3406
   237
}
vb@3406
   238