src/sync_api.c
author Volker Birk <vb@pep-project.org>
Fri, 17 May 2019 17:59:06 +0200
branchsync
changeset 3720 9ed76a79d784
parent 3540 8b1730bcb3e0
child 3745 1ee3caae7f33
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
        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@3540
    78
    identity_list *own_identities = NULL;
vb@3410
    79
vb@3411
    80
    if (identities_sharing && identities_sharing->ident) {
vb@3540
    81
        own_identities = identity_list_dup(identities_sharing);
vb@3540
    82
        if (!own_identities)
vb@3540
    83
            return PEP_OUT_OF_MEMORY;
vb@3407
    84
    }
vb@3407
    85
    else {
vb@3540
    86
        status = own_identities_retrieve(session, &own_identities);
vb@3407
    87
    }
vb@3407
    88
vb@3407
    89
    if (!status)
vb@3540
    90
        status = signal_Sync_event(session, Sync_PR_keysync, event, own_identities);
krista@2271
    91
    return status;
krista@2271
    92
}
krista@2271
    93
krista@2271
    94
DYNAMIC_API PEP_STATUS do_sync_protocol(
krista@2271
    95
        PEP_SESSION session,
krista@2271
    96
        void *obj
krista@2271
    97
    )
krista@2271
    98
{
vb@2840
    99
    Sync_event_t *event= NULL;
krista@2271
   100
vb@2839
   101
    assert(session && session->retrieve_next_sync_event);
vb@2839
   102
    if (!(session && session->retrieve_next_sync_event))
krista@2271
   103
        return PEP_ILLEGAL_VALUE;
krista@2271
   104
vb@2909
   105
    log_event(session, "sync_protocol thread started", "pEp sync protocol",
vb@2909
   106
            NULL, NULL);
krista@2271
   107
krista@2271
   108
    while (true) 
krista@2271
   109
    {
vb@2909
   110
        event = session->retrieve_next_sync_event(session->sync_management,
vb@2909
   111
                SYNC_THRESHOLD);
vb@2840
   112
        if (!event)
krista@2271
   113
            break;
vb@2834
   114
vb@2899
   115
        do_sync_protocol_step(session, obj, event);
krista@2271
   116
    }
vb@2834
   117
    session->sync_obj = NULL;
krista@2271
   118
vb@2909
   119
    log_event(session, "sync_protocol thread shutdown", "pEp sync protocol",
vb@2909
   120
            NULL, NULL);
krista@2271
   121
krista@2271
   122
    return PEP_STATUS_OK;
krista@2271
   123
}
krista@2271
   124
vb@2899
   125
DYNAMIC_API PEP_STATUS do_sync_protocol_step(
vb@2899
   126
        PEP_SESSION session,
vb@2899
   127
        void *obj,
vb@2899
   128
        SYNC_EVENT event
vb@2899
   129
    )
vb@2899
   130
{
vb@2899
   131
    assert(session);
vb@2899
   132
    if (!session)
vb@2899
   133
        return PEP_ILLEGAL_VALUE;
vb@2899
   134
vb@2899
   135
    if (!event)
vb@2899
   136
        return PEP_STATUS_OK;
vb@2899
   137
vb@2899
   138
    session->sync_obj = obj;
vb@2899
   139
vb@2899
   140
    PEP_STATUS status = recv_Sync_event(session, event);
vb@2899
   141
    return status == PEP_MESSAGE_IGNORE ? PEP_STATUS_OK : status;
vb@2899
   142
}
vb@2899
   143
vb@2899
   144
DYNAMIC_API bool is_sync_thread(PEP_SESSION session)
vb@2899
   145
{
vb@2899
   146
    assert(session);
vb@2899
   147
    if (!session)
vb@2899
   148
        return false;
vb@2899
   149
    return session->retrieve_next_sync_event != NULL;
vb@2899
   150
}
vb@2899
   151
vb@2911
   152
DYNAMIC_API SYNC_EVENT new_sync_timeout_event()
vb@2911
   153
{
vb@2911
   154
    return SYNC_TIMEOUT_EVENT;
vb@2911
   155
}
vb@2911
   156
vb@3406
   157
DYNAMIC_API PEP_STATUS enter_device_group(
vb@3406
   158
        PEP_SESSION session,
vb@3406
   159
        const identity_list *identities_sharing
vb@3406
   160
    )
vb@3406
   161
{
vb@3406
   162
    assert(session);
vb@3406
   163
    if (!session)
vb@3406
   164
        return PEP_ILLEGAL_VALUE;
vb@3406
   165
vb@3406
   166
    for (const identity_list *_il = identities_sharing; _il && _il->ident;
vb@3406
   167
            _il = _il->next) {
vb@3406
   168
        if (!_il->ident->me || !_il->ident->user_id || !_il->ident->user_id[0]
vb@3406
   169
                || !_il->ident->address || !_il->ident->address[0])
vb@3406
   170
            return PEP_ILLEGAL_VALUE;
vb@3406
   171
    }
vb@3406
   172
vb@3406
   173
    identity_list *own_identities = NULL;
vb@3406
   174
    PEP_STATUS status = own_identities_retrieve(session, &own_identities);
vb@3406
   175
    if (status)
vb@3406
   176
        goto the_end;
vb@3406
   177
vb@3409
   178
    if (identities_sharing && identities_sharing->ident) {
vb@3406
   179
        for (identity_list *_il = own_identities; _il && _il->ident;
vb@3406
   180
                _il = _il->next) {
vb@3406
   181
            bool found = false;
vb@3406
   182
vb@3406
   183
            for (const identity_list *_is = identities_sharing;
vb@3406
   184
                    _is && _is->ident; _is = _is->next) {
vb@3406
   185
                // FIXME: "john@doe.com" and "mailto:john@doe.com" should be equal
vb@3406
   186
                if (strcmp(_il->ident->address, _is->ident->address) == 0
vb@3406
   187
                        && strcmp(_il->ident->user_id, _is->ident->user_id) == 0) {
vb@3406
   188
                    found = true;
vb@3406
   189
vb@3406
   190
                    status = set_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   191
                    if (status)
vb@3406
   192
                        goto the_end;
vb@3406
   193
vb@3406
   194
                    break;
vb@3406
   195
                }
vb@3406
   196
            }
vb@3406
   197
            if (!found) {
vb@3406
   198
                status = unset_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   199
                if (status)
vb@3406
   200
                    goto the_end;
vb@3406
   201
            }
vb@3406
   202
        }
vb@3406
   203
    }
vb@3406
   204
    else {
vb@3406
   205
        for (identity_list *_il = own_identities; _il && _il->ident;
vb@3406
   206
                _il = _il->next) {
vb@3406
   207
            status = set_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   208
            if (status)
vb@3406
   209
                goto the_end;
vb@3406
   210
        }
vb@3406
   211
    }
vb@3406
   212
vb@3406
   213
the_end:
vb@3406
   214
    free_identity_list(own_identities);
vb@3406
   215
    return status;
vb@3406
   216
}
vb@3406
   217
vb@3406
   218
DYNAMIC_API PEP_STATUS leave_device_group(PEP_SESSION session)
vb@3406
   219
{
vb@3406
   220
    assert(session);
vb@3406
   221
    if (!session)
vb@3406
   222
        return PEP_ILLEGAL_VALUE;
vb@3406
   223
vb@3406
   224
    identity_list *il = NULL;
vb@3406
   225
    PEP_STATUS status = own_identities_retrieve(session, &il);
vb@3406
   226
    if (status)
vb@3406
   227
        goto the_end;
vb@3406
   228
vb@3406
   229
    for (identity_list *_il = il; _il && _il->ident ; _il = _il->next) {
vb@3406
   230
        status = unset_identity_flags(session, _il->ident, PEP_idf_devicegroup);
vb@3406
   231
        if (status)
vb@3406
   232
            goto the_end;
vb@3406
   233
    }
vb@3406
   234
vb@3406
   235
the_end:
vb@3406
   236
    free_identity_list(il);
vb@3406
   237
    return status;
vb@3406
   238
}
vb@3406
   239