Sync : changed sync queue payload type so that sync protocol can receive event such as keygen or decrypt failure asynchronously from other sessions. Fixed problem with ownership of payload leading to memory leak in some case. keysync
authorEdouard Tisserant <edouard@pep-project.org>
Sat, 24 Sep 2016 09:27:25 +0200
branchkeysync
changeset 117215a75c0cc78f
parent 1171 da88e97231f9
child 1173 5a8002f941e0
Sync : changed sync queue payload type so that sync protocol can receive event such as keygen or decrypt failure asynchronously from other sessions. Fixed problem with ownership of payload leading to memory leak in some case.
src/sync.c
src/sync.h
src/sync_impl.c
src/sync_impl.h
     1.1 --- a/src/sync.c	Wed Sep 21 12:44:17 2016 +0200
     1.2 +++ b/src/sync.c	Sat Sep 24 09:27:25 2016 +0200
     1.3 @@ -107,7 +107,7 @@
     1.4          void *management
     1.5      )
     1.6  {
     1.7 -    DeviceGroup_Protocol_t *msg = NULL;
     1.8 +    sync_msg_t *msg = NULL;
     1.9      PEP_STATUS status = PEP_STATUS_OK;
    1.10  
    1.11      assert(session && session->retrieve_next_sync_msg);
    1.12 @@ -118,7 +118,7 @@
    1.13  
    1.14      log_event(session, "sync_protocol thread started", "pEp sync protocol", NULL, NULL);
    1.15  
    1.16 -    while ((msg = (DeviceGroup_Protocol_t *) session->retrieve_next_sync_msg(management))) 
    1.17 +    while ((msg = (sync_msg_t *) session->retrieve_next_sync_msg(management))) 
    1.18      {
    1.19          if ((status = receive_sync_msg(session, msg) != PEP_STATUS_OK)) {
    1.20              char buffer[MAX_LINELENGTH];
     2.1 --- a/src/sync.h	Wed Sep 21 12:44:17 2016 +0200
     2.2 +++ b/src/sync.h	Sat Sep 24 09:27:25 2016 +0200
     2.3 @@ -65,6 +65,8 @@
     2.4          sync_handshake_result result
     2.5      );
     2.6  
     2.7 +// sync_msg_t - items queued for serialized handling by protocol engine
     2.8 +typedef struct _sync_msg_t sync_msg_t;
     2.9  
    2.10  // inject_sync_msg - inject sync protocol message
    2.11  //
     3.1 --- a/src/sync_impl.c	Wed Sep 21 12:44:17 2016 +0200
     3.2 +++ b/src/sync_impl.c	Sat Sep 24 09:27:25 2016 +0200
     3.3 @@ -9,62 +9,133 @@
     3.4  #define SYNC_VERSION_MAJOR 1
     3.5  #define SYNC_VERSION_MINOR 0
     3.6  
     3.7 +struct _sync_msg_t {
     3.8 +    bool is_a_message;
     3.9 +    union {
    3.10 +        DeviceGroup_Protocol_t *message;
    3.11 +        struct {
    3.12 +            DeviceState_event event;
    3.13 +            Identity partner;
    3.14 +            void *extra;
    3.15 +        } event;
    3.16 +    } u;
    3.17 +};
    3.18 +
    3.19  PEP_STATUS receive_sync_msg(
    3.20          PEP_SESSION session,
    3.21 -        DeviceGroup_Protocol_t *msg
    3.22 +        sync_msg_t *sync_msg
    3.23      )
    3.24  {
    3.25 -    assert(session && msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
    3.26 -    if (!(session && msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING))
    3.27 -        return PEP_ILLEGAL_VALUE;
    3.28 -
    3.29 +    PEP_STATUS status;
    3.30      void *extra = NULL;
    3.31      Identity partner = NULL;
    3.32      DeviceState_event event = DeviceState_event_NONE;
    3.33 +    assert(session && sync_msg);
    3.34 +    if (!(session && sync_msg))
    3.35 +        return PEP_ILLEGAL_VALUE;
    3.36  
    3.37 -    switch (msg->payload.present) {
    3.38 -        case DeviceGroup_Protocol__payload_PR_beacon:
    3.39 -            partner = Identity_to_Struct(&msg->header.me, NULL);
    3.40 -            if (!partner)
    3.41 -                return PEP_OUT_OF_MEMORY;
    3.42 -            event = Beacon;
    3.43 -            break;
    3.44 +    if(sync_msg->is_a_message){
    3.45 +        DeviceGroup_Protocol_t *msg = sync_msg->u.message;
    3.46 +        assert(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
    3.47 +        if (!(msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING)){
    3.48 +            status = PEP_OUT_OF_MEMORY;
    3.49 +            goto error;
    3.50 +        }
    3.51  
    3.52 -        case DeviceGroup_Protocol__payload_PR_handshakeRequest:
    3.53 -            partner = Identity_to_Struct(&msg->header.me, NULL);
    3.54 -            if (!partner)
    3.55 -                return PEP_OUT_OF_MEMORY;
    3.56 -            event = HandshakeRequest;
    3.57 -            break;
    3.58 +        switch (msg->payload.present) {
    3.59 +            case DeviceGroup_Protocol__payload_PR_beacon:
    3.60 +                partner = Identity_to_Struct(&msg->header.me, NULL);
    3.61 +                if (!partner){
    3.62 +                    status = PEP_OUT_OF_MEMORY;
    3.63 +                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    3.64 +                    goto error;
    3.65 +                }
    3.66 +                event = Beacon;
    3.67 +                break;
    3.68  
    3.69 -        case DeviceGroup_Protocol__payload_PR_groupKeys:
    3.70 -            partner = Identity_to_Struct(&msg->header.me, NULL);
    3.71 -            if (!partner)
    3.72 -                return PEP_OUT_OF_MEMORY;
    3.73 -            identity_list *group_keys = IdentityList_to_identity_list(
    3.74 -                    &msg->payload.choice.groupKeys.ownIdentities, NULL);
    3.75 -            if (!group_keys) {
    3.76 -                free_identity(partner);
    3.77 -                return PEP_OUT_OF_MEMORY;
    3.78 -            }
    3.79 -            extra = (void *) group_keys;
    3.80 -            event = GroupKeys;
    3.81 -            break;
    3.82 +            case DeviceGroup_Protocol__payload_PR_handshakeRequest:
    3.83 +                partner = Identity_to_Struct(&msg->header.me, NULL);
    3.84 +                if (!partner){
    3.85 +                    status = PEP_OUT_OF_MEMORY;
    3.86 +                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    3.87 +                    goto error;
    3.88 +                }
    3.89 +                event = HandshakeRequest;
    3.90 +                break;
    3.91  
    3.92 -        default:
    3.93 -            return PEP_SYNC_ILLEGAL_MESSAGE;
    3.94 +            case DeviceGroup_Protocol__payload_PR_groupKeys:
    3.95 +                partner = Identity_to_Struct(&msg->header.me, NULL);
    3.96 +                if (!partner){
    3.97 +                    status = PEP_OUT_OF_MEMORY;
    3.98 +                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    3.99 +                    goto error;
   3.100 +                }
   3.101 +                identity_list *group_keys = IdentityList_to_identity_list(
   3.102 +                        &msg->payload.choice.groupKeys.ownIdentities, NULL);
   3.103 +                if (!group_keys) {
   3.104 +                    free_identity(partner);
   3.105 +                    status = PEP_OUT_OF_MEMORY;
   3.106 +                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   3.107 +                    goto error;
   3.108 +                }
   3.109 +                extra = (void *) group_keys;
   3.110 +                event = GroupKeys;
   3.111 +                break;
   3.112 +
   3.113 +            default:
   3.114 +                status = PEP_SYNC_ILLEGAL_MESSAGE;
   3.115 +                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   3.116 +                goto error;
   3.117 +        }
   3.118 +    }
   3.119 +    else{
   3.120 +        partner = sync_msg->u.event.partner;
   3.121 +        extra = sync_msg->u.event.extra;
   3.122 +        event = sync_msg->u.event.event;
   3.123      }
   3.124  
   3.125 -    PEP_STATUS status = fsm_DeviceState_inject(session, event, partner, extra);
   3.126 +    status = fsm_DeviceState_inject(session, event, partner, extra);
   3.127  
   3.128      free_identity(partner);
   3.129  
   3.130 +error:
   3.131 +    free(sync_msg);
   3.132 +
   3.133      return status;
   3.134  }
   3.135  
   3.136  // from sync.c
   3.137  int call_inject_sync_msg(PEP_SESSION session, void *msg);
   3.138  
   3.139 +PEP_STATUS inject_DeviceState_event(
   3.140 +    PEP_SESSION session, 
   3.141 +    DeviceState_event event,
   3.142 +    Identity partner,
   3.143 +    void *extra)
   3.144 +{
   3.145 +    PEP_STATUS status;
   3.146 +
   3.147 +    assert(session);
   3.148 +    if (!(session))
   3.149 +        return PEP_ILLEGAL_VALUE;
   3.150 +
   3.151 +    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
   3.152 +    if(sync_msg == NULL)
   3.153 +        return PEP_OUT_OF_MEMORY;
   3.154 +
   3.155 +    sync_msg->is_a_message = false;
   3.156 +    sync_msg->u.event.partner = partner;
   3.157 +    sync_msg->u.event.extra = extra;
   3.158 +    sync_msg->u.event.event = event;
   3.159 +
   3.160 +    status = call_inject_sync_msg(session, sync_msg);
   3.161 +    if (status == PEP_SYNC_NO_INJECT_CALLBACK){
   3.162 +        free(sync_msg);
   3.163 +    }
   3.164 +
   3.165 +    return status;
   3.166 +}
   3.167 +
   3.168  PEP_STATUS receive_DeviceState_msg(
   3.169      PEP_SESSION session, 
   3.170      message *src, 
   3.171 @@ -142,9 +213,19 @@
   3.172  
   3.173                  if (status == PEP_STATUS_OK) {
   3.174                      found = true;
   3.175 -                    status = call_inject_sync_msg(session, msg);
   3.176 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   3.177 +                    sync_msg_t *sync_msg = malloc(sizeof(sync_msg_t));
   3.178 +                    if(sync_msg == NULL){
   3.179 +                        ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   3.180 +                        return PEP_OUT_OF_MEMORY;
   3.181 +                    }
   3.182 +                    sync_msg->is_a_message = true;
   3.183 +                    sync_msg->u.message = msg;
   3.184 +                    status = call_inject_sync_msg(session, sync_msg);
   3.185                      if (status != PEP_STATUS_OK){
   3.186 +                        ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   3.187 +                        if (status == PEP_SYNC_NO_INJECT_CALLBACK){
   3.188 +                            free(sync_msg);
   3.189 +                        }
   3.190                          return status;
   3.191                      }
   3.192                  }
     4.1 --- a/src/sync_impl.h	Wed Sep 21 12:44:17 2016 +0200
     4.2 +++ b/src/sync_impl.h	Sat Sep 24 09:27:25 2016 +0200
     4.3 @@ -1,6 +1,7 @@
     4.4  #pragma once
     4.5  
     4.6  #include "message.h"
     4.7 +#include "sync.h"
     4.8  #include "sync_fsm.h"
     4.9  
    4.10  #ifdef __cplusplus
    4.11 @@ -9,7 +10,7 @@
    4.12  
    4.13  PEP_STATUS receive_sync_msg(
    4.14          PEP_SESSION session,
    4.15 -        DeviceGroup_Protocol_t *msg
    4.16 +        sync_msg_t *sync_msg
    4.17      );
    4.18  
    4.19  PEP_STATUS receive_DeviceState_msg(