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