src/sync_actions.c
changeset 690 3ae86b68fdfe
parent 665 51b2e250297a
child 691 67c247f1da64
     1.1 --- a/src/sync_actions.c	Fri May 27 11:46:24 2016 +0200
     1.2 +++ b/src/sync_actions.c	Sun May 29 12:21:50 2016 +0200
     1.3 @@ -1,8 +1,11 @@
     1.4  // Actions for DeviceState state machine
     1.5  
     1.6  #include <assert.h>
     1.7 +#include "pEp_internal.h"
     1.8  #include "keymanagement.h"
     1.9 +#include "message.h"
    1.10  #include "sync_fsm.h"
    1.11 +#include "baseprotocol.h"
    1.12  #include "map_asn1.h"
    1.13  #include "../asn.1/Beacon.h"
    1.14  #include "../asn.1/HandshakeRequest.h"
    1.15 @@ -13,21 +16,33 @@
    1.16  //
    1.17  //  params:
    1.18  //      session (in)        session handle
    1.19 +//      state (in)          state the state machine is in
    1.20  //      partner (in)        (must be NULL)
    1.21  //
    1.22  //  returns:
    1.23  //      PEP_STATUS_OK or any other value on error
    1.24  
    1.25 -PEP_STATUS sendBeacon(PEP_SESSION session, const Identity partner)
    1.26 +PEP_STATUS sendBeacon(
    1.27 +        PEP_SESSION session,
    1.28 +        DeviceState_state state,
    1.29 +        const Identity partner
    1.30 +    )
    1.31  {
    1.32      PEP_STATUS status = PEP_STATUS_OK;
    1.33 +    Beacon_t *msg = NULL;
    1.34 +    char *payload = NULL;
    1.35 +    message *_message = NULL;
    1.36  
    1.37      assert(session);
    1.38      assert(!partner);
    1.39      if (!(session && !partner))
    1.40          return PEP_ILLEGAL_VALUE;
    1.41  
    1.42 -    Beacon_t *msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    1.43 +    assert(session->messageToSend);
    1.44 +    if (!session->messageToSend)
    1.45 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
    1.46 +
    1.47 +    msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    1.48      assert(msg);
    1.49      if (!msg)
    1.50          goto enomem;
    1.51 @@ -38,6 +53,8 @@
    1.52          goto error;
    1.53      msg->header.sequence = (long) seq;
    1.54  
    1.55 +    msg->state = (long) state;
    1.56 +
    1.57      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
    1.58      if (!me)
    1.59          goto enomem;
    1.60 @@ -47,12 +64,31 @@
    1.61      if (Identity_from_Struct(me, &msg->me) == NULL)
    1.62          goto enomem;
    1.63  
    1.64 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
    1.65 +            NULL, msg, (void **) &payload);
    1.66 +    if (size == -1) {
    1.67 +        status = PEP_CANNOT_ENCODE;
    1.68 +        goto error;
    1.69 +    }
    1.70 +
    1.71 +    status = prepare_message(me, partner, payload, size, &_message);
    1.72 +    if (status != PEP_STATUS_OK)
    1.73 +        goto error;
    1.74 +    payload = NULL;
    1.75 +
    1.76 +    status = session->messageToSend(session->sync_obj, _message);
    1.77 +
    1.78 +    free_message(_message);
    1.79 +    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    1.80 +
    1.81      return status;
    1.82  
    1.83  enomem:
    1.84      status = PEP_OUT_OF_MEMORY;
    1.85  error:
    1.86      ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    1.87 +    free(payload);
    1.88 +    free_message(_message);
    1.89      return status;
    1.90  }
    1.91  
    1.92 @@ -61,21 +97,33 @@
    1.93  //
    1.94  //  params:
    1.95  //      session (in)        session handle
    1.96 +//      state (in)          state the state machine is in
    1.97  //      partner (in)        partner in sync
    1.98  //
    1.99  //  returns:
   1.100  //      PEP_STATUS_OK or any other value on error
   1.101  
   1.102 -PEP_STATUS sendHandshakeRequest(PEP_SESSION session, const Identity partner)
   1.103 +PEP_STATUS sendHandshakeRequest(
   1.104 +        PEP_SESSION session,
   1.105 +        DeviceState_state state,
   1.106 +        const Identity partner
   1.107 +    )
   1.108  {
   1.109      PEP_STATUS status = PEP_STATUS_OK;
   1.110 +    HandshakeRequest_t *msg = NULL;
   1.111 +    char *payload = NULL;
   1.112 +    message *_message = NULL;
   1.113  
   1.114      assert(session);
   1.115      assert(partner);
   1.116      if (!(session && partner))
   1.117          return PEP_ILLEGAL_VALUE;
   1.118  
   1.119 -    HandshakeRequest_t *msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   1.120 +    assert(session->messageToSend);
   1.121 +    if (!session->messageToSend)
   1.122 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   1.123 +
   1.124 +    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   1.125      assert(msg);
   1.126      if (!msg)
   1.127          goto enomem;
   1.128 @@ -86,6 +134,8 @@
   1.129          goto error;
   1.130      msg->header.sequence = (long) seq;
   1.131  
   1.132 +    msg->state = (long) state;
   1.133 +
   1.134      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   1.135      if (!me)
   1.136          goto enomem;
   1.137 @@ -98,12 +148,31 @@
   1.138      if (Identity_from_Struct(partner, &msg->partner) == NULL)
   1.139          goto enomem;
   1.140  
   1.141 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   1.142 +            NULL, msg, (void **) &payload);
   1.143 +    if (size == -1) {
   1.144 +        status = PEP_CANNOT_ENCODE;
   1.145 +        goto error;
   1.146 +    }
   1.147 +
   1.148 +    status = prepare_message(me, partner, payload, size, &_message);
   1.149 +    if (status != PEP_STATUS_OK)
   1.150 +        goto error;
   1.151 +    payload = NULL;
   1.152 +
   1.153 +    status = session->messageToSend(session->sync_obj, _message);
   1.154 +
   1.155 +    free_message(_message);
   1.156 +    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   1.157 +
   1.158      return status;
   1.159  
   1.160  enomem:
   1.161      status = PEP_OUT_OF_MEMORY;
   1.162  error:
   1.163      ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   1.164 +    free(payload);
   1.165 +    free_message(_message);
   1.166      return status;
   1.167  }
   1.168  
   1.169 @@ -112,12 +181,17 @@
   1.170  //
   1.171  //  params:
   1.172  //      session (in)        session handle
   1.173 +//      state (in)          state the state machine is in
   1.174  //      partner (in)        partner in sync
   1.175  //
   1.176  //  returns:
   1.177  //      PEP_STATUS_OK or any other value on error
   1.178  
   1.179 -PEP_STATUS showHandshake(PEP_SESSION session, const Identity partner)
   1.180 +PEP_STATUS showHandshake(
   1.181 +        PEP_SESSION session,
   1.182 +        DeviceState_state state,
   1.183 +        const Identity partner
   1.184 +    )
   1.185  {
   1.186      PEP_STATUS status = PEP_STATUS_OK;
   1.187  
   1.188 @@ -143,12 +217,17 @@
   1.189  //
   1.190  //  params:
   1.191  //      session (in)        session handle
   1.192 +//      state (in)          state the state machine is in
   1.193  //      partner (in)        partner in sync
   1.194  //
   1.195  //  returns:
   1.196  //      PEP_STATUS_OK or any other value on error
   1.197  
   1.198 -PEP_STATUS reject(PEP_SESSION session, const Identity partner)
   1.199 +PEP_STATUS reject(
   1.200 +        PEP_SESSION session,
   1.201 +        DeviceState_state state,
   1.202 +        const Identity partner
   1.203 +    )
   1.204  {
   1.205      PEP_STATUS status = PEP_STATUS_OK;
   1.206  
   1.207 @@ -174,12 +253,17 @@
   1.208  //
   1.209  //  params:
   1.210  //      session (in)        session handle
   1.211 +//      state (in)          state the state machine is in
   1.212  //      partner (in)        partner in sync
   1.213  //
   1.214  //  returns:
   1.215  //      PEP_STATUS_OK or any other value on error
   1.216  
   1.217 -PEP_STATUS storeGroupKeys(PEP_SESSION session, const Identity partner)
   1.218 +PEP_STATUS storeGroupKeys(
   1.219 +        PEP_SESSION session,
   1.220 +        DeviceState_state state,
   1.221 +        const Identity partner
   1.222 +    )
   1.223  {
   1.224      PEP_STATUS status = PEP_STATUS_OK;
   1.225  
   1.226 @@ -205,21 +289,33 @@
   1.227  //
   1.228  //  params:
   1.229  //      session (in)        session handle
   1.230 +//      state (in)          state the state machine is in
   1.231  //      partner (in)        (must be NULL)
   1.232  //
   1.233  //  returns:
   1.234  //      PEP_STATUS_OK or any other value on error
   1.235  
   1.236 -PEP_STATUS sendOwnKeys(PEP_SESSION session, const Identity partner)
   1.237 +PEP_STATUS sendOwnKeys(
   1.238 +        PEP_SESSION session,
   1.239 +        DeviceState_state state,
   1.240 +        const Identity partner
   1.241 +    )
   1.242  {
   1.243      PEP_STATUS status = PEP_STATUS_OK;
   1.244 +    OwnKeys_t *msg = NULL;
   1.245 +    char *payload = NULL;
   1.246 +    message *_message = NULL;
   1.247  
   1.248      assert(session);
   1.249      assert(!partner);
   1.250      if (!(session && !partner))
   1.251          return PEP_ILLEGAL_VALUE;
   1.252  
   1.253 -    OwnKeys_t *msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   1.254 +    assert(session->messageToSend);
   1.255 +    if (!session->messageToSend)
   1.256 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   1.257 +
   1.258 +    msg = (OwnKeys_t *) calloc(1, sizeof(OwnKeys_t));
   1.259      assert(msg);
   1.260      if (!msg)
   1.261          goto enomem;
   1.262 @@ -230,6 +326,8 @@
   1.263          goto error;
   1.264      msg->header.sequence = (long) seq;
   1.265  
   1.266 +    msg->state = (long) state;
   1.267 +
   1.268      pEp_identity *me = new_identity(NULL, NULL, NULL, NULL);
   1.269      if (!me)
   1.270          goto enomem;
   1.271 @@ -246,12 +344,31 @@
   1.272      if (KeyList_from_stringlist(sl, &msg->keylist) == NULL)
   1.273          goto enomem;
   1.274  
   1.275 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   1.276 +            NULL, msg, (void **) &payload);
   1.277 +    if (size == -1) {
   1.278 +        status = PEP_CANNOT_ENCODE;
   1.279 +        goto error;
   1.280 +    }
   1.281 +
   1.282 +    status = prepare_message(me, partner, payload, size, &_message);
   1.283 +    if (status != PEP_STATUS_OK)
   1.284 +        goto error;
   1.285 +    payload = NULL;
   1.286 +
   1.287 +    status = session->messageToSend(session->sync_obj, _message);
   1.288 +
   1.289 +    free_message(_message);
   1.290 +    ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   1.291 +
   1.292      return status;
   1.293  
   1.294  enomem:
   1.295      status = PEP_OUT_OF_MEMORY;
   1.296  error:
   1.297      ASN_STRUCT_FREE(asn_DEF_OwnKeys, msg);
   1.298 +    free(payload);
   1.299 +    free_message(_message);
   1.300      return status;
   1.301  }
   1.302  
   1.303 @@ -260,12 +377,17 @@
   1.304  //
   1.305  //  params:
   1.306  //      session (in)        session handle
   1.307 +//      state (in)          state the state machine is in
   1.308  //      partner (in)        partner in sync
   1.309  //
   1.310  //  returns:
   1.311  //      PEP_STATUS_OK or any other value on error
   1.312  
   1.313 -PEP_STATUS transmitGroupKeys(PEP_SESSION session, const Identity partner)
   1.314 +PEP_STATUS transmitGroupKeys(
   1.315 +        PEP_SESSION session,
   1.316 +        DeviceState_state state,
   1.317 +        const Identity partner
   1.318 +    )
   1.319  {
   1.320      PEP_STATUS status = PEP_STATUS_OK;
   1.321