sync : attach_sync_session keysync
authorEdouard Tisserant <edouard@pep-project.org>
Mon, 03 Oct 2016 19:08:15 +0200
branchkeysync
changeset 1236991afc1aa2a1
parent 1235 cfc9214a60a5
child 1237 18438a7595d7
sync : attach_sync_session
src/pEpEngine.c
src/pEp_internal.h
src/sync.c
src/sync.h
src/sync_impl.c
sync/devicegroup.fsm
     1.1 --- a/src/pEpEngine.c	Fri Sep 30 17:25:15 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Oct 03 19:08:15 2016 +0200
     1.3 @@ -6,7 +6,6 @@
     1.4  #include "sync_fsm.h"
     1.5  
     1.6  static int init_count = -1;
     1.7 -char sync_uuid[37];
     1.8  
     1.9  static int user_version(void *_version, int count, char **text, char **name)
    1.10  {
    1.11 @@ -135,7 +134,6 @@
    1.12  #define _DDL_USER_VERSION "3"
    1.13  
    1.14      if (in_first) {
    1.15 -        memset(sync_uuid, 0, 37);
    1.16  
    1.17          int_result = sqlite3_exec(
    1.18              _session->db,
    1.19 @@ -1897,7 +1895,7 @@
    1.20          own = 1;
    1.21      }
    1.22      else {
    1.23 -        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
    1.24 +        if (name == session->sync_uuid || strcmp(name, session->sync_uuid) == 0)
    1.25              own = 1;
    1.26      }
    1.27  
     2.1 --- a/src/pEp_internal.h	Fri Sep 30 17:25:15 2016 +0200
     2.2 +++ b/src/pEp_internal.h	Mon Oct 03 19:08:15 2016 +0200
     2.3 @@ -77,8 +77,6 @@
     2.4  
     2.5  #define NOT_IMPLEMENTED assert(0); return PEP_UNKNOWN_ERROR;
     2.6  
     2.7 -extern char sync_uuid[37];
     2.8 -
     2.9  typedef struct _pEpSession {
    2.10      const char *version;
    2.11  #ifdef USE_GPG
    2.12 @@ -126,7 +124,7 @@
    2.13      sqlite3_stmt *sequence_value2;
    2.14      sqlite3_stmt *sequence_value3;
    2.15  
    2.16 -    // sequence value
    2.17 +    // revoked keys
    2.18      sqlite3_stmt *set_revoked;
    2.19      sqlite3_stmt *get_revoked;
    2.20  
    2.21 @@ -139,8 +137,9 @@
    2.22      inject_sync_msg_t inject_sync_msg;
    2.23      retrieve_next_sync_msg_t retrieve_next_sync_msg;
    2.24  
    2.25 -    // state machines
    2.26 +    // key sync
    2.27      DeviceState_state sync_state;
    2.28 +    char sync_uuid[37];
    2.29  
    2.30      // runtime config
    2.31  
     3.1 --- a/src/sync.c	Fri Sep 30 17:25:15 2016 +0200
     3.2 +++ b/src/sync.c	Mon Oct 03 19:08:15 2016 +0200
     3.3 @@ -6,9 +6,6 @@
     3.4  #include "asn1_helper.h"
     3.5  #include "../asn.1/DeviceGroup-Protocol.h"
     3.6  
     3.7 -static void *static_sync_obj = NULL;
     3.8 -static inject_sync_msg_t static_inject_sync_msg = NULL;
     3.9 -
    3.10  // receive_sync_msg is defined in the sync_actions
    3.11  
    3.12  PEP_STATUS receive_sync_msg(
    3.13 @@ -25,9 +22,13 @@
    3.14          retrieve_next_sync_msg_t retrieve_next_sync_msg
    3.15      )
    3.16  {
    3.17 +    assert(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg);
    3.18 +    if (!(session && obj && messageToSend && showHandshake && inject_sync_msg && retrieve_next_sync_msg))
    3.19 +        return PEP_ILLEGAL_VALUE;
    3.20 +
    3.21      unsigned char uuid[16];
    3.22      uuid_generate_random(uuid);
    3.23 -    uuid_unparse_upper(uuid, sync_uuid);
    3.24 +    uuid_unparse_upper(uuid, session->sync_uuid);
    3.25  
    3.26      session->sync_obj = obj;
    3.27      session->messageToSend = messageToSend;
    3.28 @@ -35,9 +36,6 @@
    3.29      session->inject_sync_msg = inject_sync_msg;
    3.30      session->retrieve_next_sync_msg = retrieve_next_sync_msg;
    3.31  
    3.32 -    static_sync_obj = obj;
    3.33 -    static_inject_sync_msg = inject_sync_msg;
    3.34 -
    3.35      // start state machine
    3.36      session->sync_state = InitState;
    3.37      PEP_STATUS status = fsm_DeviceState_inject(session, Init, NULL, NULL);
    3.38 @@ -47,24 +45,49 @@
    3.39      return status;
    3.40  }
    3.41  
    3.42 +DYNAMIC_API PEP_STATUS attach_sync_session(
    3.43 +        PEP_SESSION session,
    3.44 +        PEP_SESSION sync_session
    3.45 +    )
    3.46 +{
    3.47 +    assert(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg );
    3.48 +    if (!(session && sync_session && sync_session->sync_obj && sync_session->inject_sync_msg ))
    3.49 +        return PEP_ILLEGAL_VALUE;
    3.50 +
    3.51 +    memcpy(session->sync_uuid, sync_session->sync_uuid, 37);
    3.52 +
    3.53 +    session->sync_obj = sync_session->sync_obj;
    3.54 +    session->inject_sync_msg = sync_session->inject_sync_msg;
    3.55 +
    3.56 +    return PEP_STATUS_OK;
    3.57 +}
    3.58 +
    3.59 +DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session)
    3.60 +{
    3.61 +    assert(session && session->sync_obj && session->inject_sync_msg );
    3.62 +    if (!(session && session->sync_obj && session->inject_sync_msg ))
    3.63 +        return PEP_ILLEGAL_VALUE;
    3.64 +
    3.65 +    memset(session->sync_uuid, 0, 37);
    3.66 +
    3.67 +    session->sync_obj = NULL;
    3.68 +    session->inject_sync_msg = NULL;
    3.69 +
    3.70 +    return PEP_STATUS_OK;
    3.71 +}
    3.72 +
    3.73  int call_inject_sync_msg(PEP_SESSION session, void *msg)
    3.74  {
    3.75      if(session->inject_sync_msg && session->sync_obj)
    3.76          return session->inject_sync_msg(msg, session->sync_obj);
    3.77 -    else if(static_inject_sync_msg && static_sync_obj)
    3.78 -        return static_inject_sync_msg(msg, static_sync_obj);
    3.79      else
    3.80         return PEP_SYNC_NO_INJECT_CALLBACK;
    3.81  }
    3.82  
    3.83 -
    3.84  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session) {
    3.85      // stop state machine
    3.86      session->sync_state = DeviceState_state_NONE;
    3.87  
    3.88 -    static_sync_obj = NULL;
    3.89 -    static_inject_sync_msg = NULL;
    3.90 -
    3.91      // unregister
    3.92      session->sync_obj = NULL;
    3.93      session->messageToSend = NULL;
     4.1 --- a/src/sync.h	Fri Sep 30 17:25:15 2016 +0200
     4.2 +++ b/src/sync.h	Mon Oct 03 19:08:15 2016 +0200
     4.3 @@ -115,15 +115,38 @@
     4.4          retrieve_next_sync_msg_t retrieve_next_sync_msg
     4.5      );
     4.6  
     4.7 +// attach_sync_session() - attach session to a session running keysync state machine 
     4.8 +//
     4.9 +//  parameters:
    4.10 +//      session (in)                session to attach
    4.11 +//      sync_session (in)           session running keysync
    4.12 +//
    4.13 +//  return value:
    4.14 +//      PEP_STATUS_OK or any other value on errror
    4.15 +//
    4.16 +//  caveat:
    4.17 +//      register_sync_callbacks must have been called on sync_session
    4.18 +//      call that BEFORE you're using that session in any other part of the engine
    4.19 +
    4.20 +DYNAMIC_API PEP_STATUS attach_sync_session(
    4.21 +        PEP_SESSION session,
    4.22 +        PEP_SESSION sync_session
    4.23 +    );
    4.24 +
    4.25 +// detach_sync_session() - detach previously attached sync session
    4.26 +//
    4.27 +//  parameters:
    4.28 +//      session (in)                session to detach 
    4.29 +
    4.30 +DYNAMIC_API PEP_STATUS detach_sync_session(PEP_SESSION session);
    4.31  
    4.32  // unregister_sync_callbacks() - unregister adapter's callbacks
    4.33  //
    4.34  //  parameters:
    4.35 -//      session (in)                session where to store obj handle
    4.36 +//      session (in)                session to unregister
    4.37  
    4.38  DYNAMIC_API void unregister_sync_callbacks(PEP_SESSION session);
    4.39  
    4.40 -
    4.41  // do_sync_protocol() - function to be run on an extra thread
    4.42  //
    4.43  //  parameters:
     5.1 --- a/src/sync_impl.c	Fri Sep 30 17:25:15 2016 +0200
     5.2 +++ b/src/sync_impl.c	Mon Oct 03 19:08:15 2016 +0200
     5.3 @@ -196,7 +196,7 @@
     5.4                      // HandshakeRequest needs encryption
     5.5                      case DeviceGroup_Protocol__payload_PR_handshakeRequest:
     5.6                          if (rating < PEP_rating_reliable ||
     5.7 -                            strncmp(sync_uuid,
     5.8 +                            strncmp(session->sync_uuid,
     5.9                                      (const char *)msg->payload.choice.handshakeRequest.partner.user_id->buf,
    5.10                                      msg->payload.choice.handshakeRequest.partner.user_id->size) != 0){
    5.11                              ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    5.12 @@ -207,7 +207,7 @@
    5.13                      // accepting GroupKeys needs encryption and trust
    5.14                      case DeviceGroup_Protocol__payload_PR_groupKeys:
    5.15                          if (!keylist || rating < PEP_rating_reliable ||
    5.16 -                            strncmp(sync_uuid,
    5.17 +                            strncmp(session->sync_uuid,
    5.18                                      (const char *)msg->payload.choice.groupKeys.partner.user_id->buf,
    5.19                                      msg->payload.choice.groupKeys.partner.user_id->size) != 0){
    5.20                              ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    5.21 @@ -350,7 +350,7 @@
    5.22      
    5.23      int32_t seq = 0;
    5.24  
    5.25 -    status = sequence_value(session, sync_uuid, &seq);
    5.26 +    status = sequence_value(session, session->sync_uuid, &seq);
    5.27      if (status != PEP_OWN_SEQUENCE && status != PEP_STATUS_OK)
    5.28          goto error;
    5.29  
    5.30 @@ -361,7 +361,7 @@
    5.31          goto enomem;
    5.32  
    5.33      free(_me->user_id);
    5.34 -    _me->user_id = strndup(sync_uuid, 36);
    5.35 +    _me->user_id = strndup(session->sync_uuid, 36);
    5.36      assert(_me->user_id);
    5.37      if (!_me->user_id)
    5.38          goto enomem;
    5.39 @@ -478,6 +478,7 @@
    5.40      if (status != PEP_STATUS_OK)
    5.41          return status;
    5.42  
    5.43 +    // FIXME: exclude previously rejected identities
    5.44      for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
    5.45          pEp_identity *me = _i->ident;
    5.46  
     6.1 --- a/sync/devicegroup.fsm	Fri Sep 30 17:25:15 2016 +0200
     6.2 +++ b/sync/devicegroup.fsm	Mon Oct 03 19:08:15 2016 +0200
     6.3 @@ -78,6 +78,11 @@
     6.4                  do sendHandshakeRequest(partner);
     6.5                  go HandshakingGrouped(partner);
     6.6              }
     6.7 +            on GroupKeys(Identity partner, Stringlist keys) {
     6.8 +                do storeGroupKeys(partner, keys);
     6.9 +                // TODO : add a callback to signal finished waiting for group keys
    6.10 +                go Grouped;
    6.11 +            }
    6.12          }
    6.13  
    6.14          state HandshakingGrouped(Identity partner) {