src/sync_impl.c
changeset 1316 942c9943ae9d
parent 1297 78f5f9894fbd
child 1338 515f4edb52d0
     1.1 --- a/src/sync_impl.c	Tue Oct 18 20:57:54 2016 +0200
     1.2 +++ b/src/sync_impl.c	Tue Oct 18 21:47:38 2016 +0200
     1.3 @@ -14,6 +14,9 @@
     1.4  #define SYNC_VERSION_MAJOR 1
     1.5  #define SYNC_VERSION_MINOR 0
     1.6  
     1.7 +#define SYNC_INHIBIT_TIME (60*10)
     1.8 +#define SYNC_MSG_EXPIRE_TIME (60 * 10)
     1.9 +
    1.10  struct _sync_msg_t {
    1.11      bool is_a_message;
    1.12      union {
    1.13 @@ -49,34 +52,20 @@
    1.14  
    1.15          switch (msg->payload.present) {
    1.16              case DeviceGroup_Protocol__payload_PR_beacon:
    1.17 -                partner = Identity_to_Struct(&msg->header.me, NULL);
    1.18 -                if (!partner){
    1.19 -                    status = PEP_OUT_OF_MEMORY;
    1.20 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.21 -                    goto error;
    1.22 -                }
    1.23                  event = Beacon;
    1.24                  break;
    1.25  
    1.26              case DeviceGroup_Protocol__payload_PR_handshakeRequest:
    1.27 -                partner = Identity_to_Struct(&msg->header.me, NULL);
    1.28 -                if (!partner){
    1.29 -                    status = PEP_OUT_OF_MEMORY;
    1.30 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.31 -                    goto error;
    1.32 -                }
    1.33 +                event = HandshakeRequest;
    1.34 +                break;
    1.35  
    1.36 -                event = HandshakeRequest;
    1.37 +            case DeviceGroup_Protocol__payload_PR_updateRequest:
    1.38 +                event = UpdateRequest;
    1.39                  break;
    1.40  
    1.41              case DeviceGroup_Protocol__payload_PR_groupKeys:
    1.42              case DeviceGroup_Protocol__payload_PR_groupUpdate:
    1.43 -                partner = Identity_to_Struct(&msg->header.me, NULL);
    1.44 -                if (!partner){
    1.45 -                    status = PEP_OUT_OF_MEMORY;
    1.46 -                    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.47 -                    goto error;
    1.48 -                }
    1.49 +            {
    1.50                  identity_list *group_keys = IdentityList_to_identity_list(
    1.51                          msg->payload.present == 
    1.52                            DeviceGroup_Protocol__payload_PR_groupKeys ?
    1.53 @@ -84,7 +73,6 @@
    1.54                              &msg->payload.choice.groupUpdate.ownIdentities,
    1.55                          NULL);
    1.56                  if (!group_keys) {
    1.57 -                    free_identity(partner);
    1.58                      status = PEP_OUT_OF_MEMORY;
    1.59                      ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.60                      goto error;
    1.61 @@ -94,12 +82,20 @@
    1.62                            DeviceGroup_Protocol__payload_PR_groupKeys ?
    1.63                              GroupKeys : GroupUpdate;
    1.64                  break;
    1.65 +            }
    1.66  
    1.67              default:
    1.68                  status = PEP_SYNC_ILLEGAL_MESSAGE;
    1.69                  ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.70                  goto error;
    1.71          }
    1.72 +
    1.73 +        partner = Identity_to_Struct(&msg->header.me, NULL);
    1.74 +        if (!partner){
    1.75 +            status = PEP_OUT_OF_MEMORY;
    1.76 +            ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.77 +            goto error;
    1.78 +        }
    1.79      }
    1.80      else{
    1.81          partner = sync_msg->u.event.partner;
    1.82 @@ -107,6 +103,30 @@
    1.83          event = sync_msg->u.event.event;
    1.84      }
    1.85  
    1.86 +    // Event inhibition, to limit mailbox and prevent cycles
    1.87 +    time_t *last = NULL;
    1.88 +    switch(event){
    1.89 +        case CannotDecrypt:
    1.90 +            last = &session->LastCannotDecrypt;
    1.91 +            break;
    1.92 +
    1.93 +        case UpdateRequest:
    1.94 +            last = &session->LastUpdateRequest;
    1.95 +            break;
    1.96 +
    1.97 +        default:
    1.98 +            break;
    1.99 +    }
   1.100 +    time_t now = time(NULL);
   1.101 +    if(last != NULL){
   1.102 +        if(*last != 0 && (*last + SYNC_INHIBIT_TIME) > now ){
   1.103 +            free_identity(partner);
   1.104 +            status = PEP_STATEMACHINE_INHIBITED_EVENT;
   1.105 +            goto error;
   1.106 +        }
   1.107 +        *last = now;
   1.108 +    }
   1.109 +
   1.110      // partner identity must be explicitely added DB to later
   1.111      // be able to communicate securely with it.
   1.112      if(partner){
   1.113 @@ -227,9 +247,6 @@
   1.114      return status;
   1.115  }
   1.116  
   1.117 -// Ten minutes
   1.118 -#define SYNC_MSG_EXPIRE_DELTA (60 * 10)
   1.119 -
   1.120  PEP_STATUS receive_DeviceState_msg(
   1.121      PEP_SESSION session, 
   1.122      message *src, 
   1.123 @@ -266,7 +283,7 @@
   1.124  
   1.125                  // check message expiry 
   1.126                  if(src->recv) {
   1.127 -                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_DELTA;
   1.128 +                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_TIME;
   1.129                      time_t now = time(NULL);
   1.130                      if(expiry != 0 && now != 0 && expiry < now){
   1.131                          expired = true;
   1.132 @@ -323,14 +340,12 @@
   1.133                              break;
   1.134                          }
   1.135                          case DeviceGroup_Protocol__payload_PR_groupUpdate:
   1.136 +                        case DeviceGroup_Protocol__payload_PR_updateRequest:
   1.137 +                        {
   1.138                              // inject message but don't consume it, so 
   1.139                              // that other group members can also be updated
   1.140                              force_keep_msg = true;
   1.141                              
   1.142 -                            // no break
   1.143 -
   1.144 -                        case DeviceGroup_Protocol__payload_PR_updateRequest:
   1.145 -                        {
   1.146                              if (!keylist || rating < PEP_rating_reliable){
   1.147                                  discarded = true;
   1.148                                  goto free_all;