Sync : re-impl of sync message expiry based on message reception time stamp
authorEdouard Tisserant <edouard@pep-project.org>
Fri, 07 Oct 2016 17:39:48 +0200
changeset 12570c12d65fe859
parent 1256 8bc2e3636021
child 1258 9c376b153898
Sync : re-impl of sync message expiry based on message reception time stamp
asn.1/devicegroup.asn1
src/map_asn1.c
src/map_asn1.h
src/sync_impl.c
     1.1 --- a/asn.1/devicegroup.asn1	Fri Oct 07 13:55:23 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Fri Oct 07 17:39:48 2016 +0200
     1.3 @@ -27,9 +27,8 @@
     1.4          sequence    INTEGER,  /* always increases */
     1.5          me          Identity, /* identity of the sender */
     1.6          state       INTEGER,  /* state the sender is in */
     1.7 -        devicegroup BOOLEAN,
     1.8 +        devicegroup BOOLEAN
     1.9              /* signals if this message is coming from a device group member */
    1.10 -        expiry      GeneralizedTime
    1.11      },
    1.12  
    1.13      payload CHOICE {
     2.1 --- a/src/map_asn1.c	Fri Oct 07 13:55:23 2016 +0200
     2.2 +++ b/src/map_asn1.c	Fri Oct 07 17:39:48 2016 +0200
     2.3 @@ -178,35 +178,3 @@
     2.4      return NULL;
     2.5  }
     2.6  
     2.7 -timestamp *GeneralizedTime_to_timestamp(GeneralizedTime_t * asntime, timestamp *result)
     2.8 -{
     2.9 -    assert(asntime);
    2.10 -    if (!asntime)
    2.11 -        return NULL;
    2.12 -
    2.13 -    time_t smpltime = asn_GT2time(asntime, NULL, 0);
    2.14 -
    2.15 -    if (!result){
    2.16 -        result = new_timestamp(smpltime);
    2.17 -    }
    2.18 -    else
    2.19 -        gmtime_r(&smpltime, result);
    2.20 -
    2.21 -
    2.22 -    return result;
    2.23 -}
    2.24 -
    2.25 -time_t GeneralizedTime_to_time_t(GeneralizedTime_t * asntime)
    2.26 -{
    2.27 -    return asn_GT2time(asntime, NULL, 0);
    2.28 -}
    2.29 -
    2.30 -GeneralizedTime_t *timestamp_to_GeneralizedTime(timestamp * ts, GeneralizedTime_t *result)
    2.31 -{
    2.32 -    assert(ts);
    2.33 -    if (!ts)
    2.34 -        return NULL;
    2.35 -
    2.36 -    GeneralizedTime_t *asntime = asn_time2GT(result, ts, 0);
    2.37 -    return asntime;
    2.38 -}
     3.1 --- a/src/map_asn1.h	Fri Oct 07 13:55:23 2016 +0200
     3.2 +++ b/src/map_asn1.h	Fri Oct 07 17:39:48 2016 +0200
     3.3 @@ -4,7 +4,6 @@
     3.4  #include "identity_list.h"
     3.5  #include "../asn.1/Identity.h"
     3.6  #include "../asn.1/IdentityList.h"
     3.7 -#include "../asn.1/GeneralizedTime.h"
     3.8  
     3.9  #ifdef __cplusplus
    3.10  extern "C" {
    3.11 @@ -75,45 +74,6 @@
    3.12  
    3.13  identity_list *IdentityList_to_identity_list(IdentityList_t *list, identity_list *result);
    3.14  
    3.15 -// GeneralizedTime_to_timestamp() - convert ASN.1 GeneralizedTime to timestamp
    3.16 -//
    3.17 -//  params:
    3.18 -//      asntime (in)        ASN.1 GeneralizedTime to convert
    3.19 -//      result (inout)      timestamp to update or NULL to alloc a new one
    3.20 -//
    3.21 -//  return value:
    3.22 -//      pointer to allocated timestamp
    3.23 -//
    3.24 -//  caveat:
    3.25 -//      if a new timestamp is allocated, the ownership goes to the caller
    3.26 -
    3.27 -timestamp *GeneralizedTime_to_timestamp(GeneralizedTime_t * asntime, timestamp *result);
    3.28 -
    3.29 -// GeneralizedTime_to_time_t() - convert ASN.1 GeneralizedTime to time_t
    3.30 -//
    3.31 -//  params:
    3.32 -//      asntime (in)        ASN.1 GeneralizedTime to convert
    3.33 -//
    3.34 -//  return value:
    3.35 -//      resulting time_t
    3.36 -//
    3.37 -
    3.38 -time_t GeneralizedTime_to_time_t(GeneralizedTime_t * asntime);
    3.39 -
    3.40 -// timestamp_GeneralizedTime_to() - convert ASN.1 timestamp to GeneralizedTime
    3.41 -//
    3.42 -//  params:
    3.43 -//      ts (in)             timestam to convert
    3.44 -//      result (inout)      GeneralizedTime_t to update or NULL to alloc a new one
    3.45 -//
    3.46 -//  return value:
    3.47 -//      pointer to allocated ASN.1 GeneralizedTime
    3.48 -//
    3.49 -//  caveat:
    3.50 -//      if a new GeneralizedTime is allocated, the ownership goes to the caller
    3.51 -
    3.52 -GeneralizedTime_t *timestamp_to_GeneralizedTime(timestamp * ts, GeneralizedTime_t *result);
    3.53 -
    3.54  #ifdef __cplusplus
    3.55  }
    3.56  #endif
     4.1 --- a/src/sync_impl.c	Fri Oct 07 13:55:23 2016 +0200
     4.2 +++ b/src/sync_impl.c	Fri Oct 07 17:39:48 2016 +0200
     4.3 @@ -204,6 +204,9 @@
     4.4      return status;
     4.5  }
     4.6  
     4.7 +// Ten minutes
     4.8 +#define SYNC_MSG_EXPIRE_DELTA (60 * 10)
     4.9 +
    4.10  PEP_STATUS receive_DeviceState_msg(
    4.11      PEP_SESSION session, 
    4.12      message *src, 
    4.13 @@ -228,6 +231,7 @@
    4.14                      &msg, bl->value, bl->size);
    4.15  
    4.16              if (msg) {
    4.17 +                PEP_STATUS status = PEP_STATUS_OK;
    4.18  
    4.19                  char *user_id = strndup((char *) msg->header.me.user_id->buf,
    4.20                          msg->header.me.user_id->size);
    4.21 @@ -238,15 +242,17 @@
    4.22                  }
    4.23  
    4.24                  // check message expiry 
    4.25 -                time_t expiry = GeneralizedTime_to_time_t(&msg->header.expiry);
    4.26 -                time_t now = time(NULL);
    4.27 -                if(expiry != 0 && now != 0 && expiry < now){
    4.28 -                    expired = true;
    4.29 -                    goto free_all;
    4.30 +                if(src->recv) {
    4.31 +                    time_t expiry = timegm(src->recv) + SYNC_MSG_EXPIRE_DELTA;
    4.32 +                    time_t now = time(NULL);
    4.33 +                    if(expiry != 0 && now != 0 && expiry < now){
    4.34 +                        expired = true;
    4.35 +                        goto free_all;
    4.36 +                    }
    4.37                  }
    4.38  
    4.39                  int32_t value = (int32_t) msg->header.sequence;
    4.40 -                PEP_STATUS status = sequence_value(session, (char *) user_id,
    4.41 +                status = sequence_value(session, (char *) user_id,
    4.42                          &value);
    4.43  
    4.44                  if (status == PEP_STATUS_OK) {
    4.45 @@ -399,9 +405,6 @@
    4.46      ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    4.47  }
    4.48  
    4.49 -// Ten minutes
    4.50 -#define SYNC_MSG_EXPIRE_DELTA (60 * 10)
    4.51 -
    4.52  PEP_STATUS unicast_msg(
    4.53          PEP_SESSION session,
    4.54          const Identity partner,
    4.55 @@ -465,13 +468,6 @@
    4.56      else
    4.57          msg->header.devicegroup = 0;
    4.58  
    4.59 -    timestamp *expiry = new_timestamp(time(NULL) + SYNC_MSG_EXPIRE_DELTA);
    4.60 -    if(timestamp_to_GeneralizedTime(expiry, &msg->header.expiry) == NULL){
    4.61 -        free_timestamp(expiry);
    4.62 -        goto enomem;
    4.63 -    }
    4.64 -    free_timestamp(expiry);
    4.65 -
    4.66      if (asn_check_constraints(&asn_DEF_DeviceGroup_Protocol, msg, NULL, NULL)) {
    4.67          status = PEP_CONTRAINTS_VIOLATED;
    4.68          goto error;