src/sync_api.c
author us@chu.huenfield.org
Tue, 25 Dec 2018 14:46:45 +0100
branchsync
changeset 3209 c15b4ca2b52a
parent 2992 15e45b39c38c
child 3375 4f64652fb80d
permissions -rw-r--r--
Replace use of Sequoia's backend with a custom key store.

- Sequoia's key store doesn't meet pep's needs (in particular, the
ability to search on a key's user id) and trying to shoehorn pep's
needs onto Sequoia's key store abstractions is just introducing
overhead with no appreciable gain in functionality.

- This patch changes the Sequoia backend to use a local sqlite
database to store the public keys.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 
     6 #include <memory.h>
     7 #include <assert.h>
     8 
     9 #include "KeySync_fsm.h"
    10 
    11 DYNAMIC_API PEP_STATUS register_sync_callbacks(
    12         PEP_SESSION session,
    13         void *management,
    14         notifyHandshake_t notifyHandshake,
    15         retrieve_next_sync_event_t retrieve_next_sync_event
    16     )
    17 {
    18     assert(session && notifyHandshake && retrieve_next_sync_event);
    19     if (!(session && notifyHandshake && retrieve_next_sync_event))
    20         return PEP_ILLEGAL_VALUE;
    21 
    22     session->sync_management = management;
    23     session->notifyHandshake = notifyHandshake;
    24     session->retrieve_next_sync_event = retrieve_next_sync_event;
    25 
    26     // start state machine
    27     return Sync_driver(session, Sync_PR_keysync, Init);
    28 }
    29 
    30 DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
    31     // stop state machine
    32     free_Sync_state(session);
    33 
    34     // unregister
    35     session->sync_management = NULL;
    36     session->notifyHandshake = NULL;
    37     session->retrieve_next_sync_event = NULL;
    38 }
    39 
    40 DYNAMIC_API PEP_STATUS deliverHandshakeResult(
    41         PEP_SESSION session,
    42         pEp_identity *partner,
    43         sync_handshake_result result
    44     )
    45 {
    46     assert(session);
    47     if (!session)
    48         return PEP_ILLEGAL_VALUE;
    49 
    50     PEP_STATUS status = PEP_STATUS_OK;
    51 
    52     int event;
    53     bool need_partner = false;
    54 
    55     switch (result) {
    56         case SYNC_HANDSHAKE_CANCEL:
    57             event = Cancel;
    58             break;
    59         case SYNC_HANDSHAKE_ACCEPTED:
    60         {
    61             event = Accept;
    62             break;
    63         }
    64         case SYNC_HANDSHAKE_REJECTED:
    65         {
    66             event = Reject;
    67             break;
    68         }
    69         default:
    70             return PEP_ILLEGAL_VALUE;
    71     }
    72 
    73     pEp_identity *_partner = NULL;
    74     if(need_partner){
    75         _partner = identity_dup(partner);
    76         if (_partner == NULL)
    77             return PEP_OUT_OF_MEMORY;
    78     }
    79     status = send_Sync_message(session, Sync_PR_keysync, event);
    80 
    81     return status;
    82 }
    83 
    84 DYNAMIC_API PEP_STATUS do_sync_protocol(
    85         PEP_SESSION session,
    86         void *obj
    87     )
    88 {
    89     Sync_event_t *event= NULL;
    90 
    91     assert(session && session->retrieve_next_sync_event);
    92     if (!(session && session->retrieve_next_sync_event))
    93         return PEP_ILLEGAL_VALUE;
    94 
    95     log_event(session, "sync_protocol thread started", "pEp sync protocol",
    96             NULL, NULL);
    97 
    98     while (true) 
    99     {
   100         event = session->retrieve_next_sync_event(session->sync_management,
   101                 SYNC_THRESHOLD);
   102         if (!event)
   103             break;
   104 
   105         do_sync_protocol_step(session, obj, event);
   106     }
   107     session->sync_obj = NULL;
   108 
   109     log_event(session, "sync_protocol thread shutdown", "pEp sync protocol",
   110             NULL, NULL);
   111 
   112     return PEP_STATUS_OK;
   113 }
   114 
   115 DYNAMIC_API PEP_STATUS do_sync_protocol_step(
   116         PEP_SESSION session,
   117         void *obj,
   118         SYNC_EVENT event
   119     )
   120 {
   121     assert(session);
   122     if (!session)
   123         return PEP_ILLEGAL_VALUE;
   124 
   125     if (!event)
   126         return PEP_STATUS_OK;
   127 
   128     session->sync_obj = obj;
   129 
   130     PEP_STATUS status = recv_Sync_event(session, event);
   131     if (status != PEP_STATUS_OK && status != PEP_MESSAGE_IGNORE) {
   132         char buffer[MAX_LINELENGTH];
   133         memset(buffer, 0, MAX_LINELENGTH);
   134         snprintf(buffer, MAX_LINELENGTH, "problem with msg received: %d\n",
   135                 (int) status);
   136         log_event(session, buffer, "pEp sync protocol", NULL, NULL);
   137     }
   138 
   139     return status == PEP_MESSAGE_IGNORE ? PEP_STATUS_OK : status;
   140 }
   141 
   142 DYNAMIC_API bool is_sync_thread(PEP_SESSION session)
   143 {
   144     assert(session);
   145     if (!session)
   146         return false;
   147     return session->retrieve_next_sync_event != NULL;
   148 }
   149 
   150 DYNAMIC_API SYNC_EVENT new_sync_timeout_event()
   151 {
   152     return SYNC_TIMEOUT_EVENT;
   153 }
   154