src/sync_send_actions.c
author Volker Birk <vb@pep-project.org>
Thu, 14 Jul 2016 13:33:24 +0200
changeset 883 096ff09932ad
child 939 ed7dfdae3031
child 1122 bbd0d058f0e3
permissions -rw-r--r--
separating send actions from other actions
     1 // Send Actions for DeviceState state machine
     2 
     3 #include <assert.h>
     4 #include "pEp_internal.h"
     5 #include "keymanagement.h"
     6 #include "message.h"
     7 #include "sync_fsm.h"
     8 #include "baseprotocol.h"
     9 #include "map_asn1.h"
    10 #include "../asn.1/Beacon.h"
    11 #include "../asn.1/HandshakeRequest.h"
    12 #include "../asn.1/GroupKeys.h"
    13 
    14 
    15 // sendBeacon() - send Beacon message
    16 //
    17 //  params:
    18 //      session (in)        session handle
    19 //      state (in)          state the state machine is in
    20 //      partner (in)        (must be NULL)
    21 //
    22 //  returns:
    23 //      PEP_STATUS_OK or any other value on error
    24 
    25 PEP_STATUS sendBeacon(
    26         PEP_SESSION session,
    27         DeviceState_state state,
    28         const Identity partner
    29     )
    30 {
    31     PEP_STATUS status = PEP_STATUS_OK;
    32     Beacon_t *msg = NULL;
    33     char *payload = NULL;
    34     message *_message = NULL;
    35     pEp_identity *me = NULL;
    36 
    37     assert(session);
    38     assert(!partner);
    39     if (!(session && !partner))
    40         return PEP_ILLEGAL_VALUE;
    41 
    42     assert(session->messageToSend);
    43     if (!session->messageToSend)
    44         return PEP_SEND_FUNCTION_NOT_REGISTERED;
    45 
    46     msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    47     assert(msg);
    48     if (!msg)
    49         goto enomem;
    50 
    51     int32_t seq;
    52     status = sequence_value(session, "DeviceGroup", &seq);
    53     if (status != PEP_STATUS_OK)
    54         goto error;
    55     msg->header.sequence = (long) seq;
    56 
    57     msg->state = (long) state;
    58 
    59     me = new_identity(NULL, NULL, NULL, NULL);
    60     if (!me)
    61         goto enomem;
    62     status = myself(session, me);
    63     if (status != PEP_STATUS_OK)
    64         goto error;
    65     if (Identity_from_Struct(me, &msg->me) == NULL)
    66         goto enomem;
    67 
    68     if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    69         status = PEP_CONTRAINTS_VIOLATED;
    70         goto error;
    71     }
    72 
    73     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
    74             NULL, msg, (void **) &payload);
    75     if (size == -1) {
    76         status = PEP_CANNOT_ENCODE;
    77         goto error;
    78     }
    79 
    80     status = prepare_message(me, partner, payload, size, &_message);
    81     if (status != PEP_STATUS_OK)
    82         goto error;
    83     payload = NULL;
    84 
    85     free_identity(me);
    86     me = NULL;
    87 
    88     status = session->messageToSend(session->sync_obj, _message);
    89 
    90     free_message(_message);
    91     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    92 
    93     return status;
    94 
    95 enomem:
    96     status = PEP_OUT_OF_MEMORY;
    97 error:
    98     ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    99     free(payload);
   100     free_message(_message);
   101     free_identity(me);
   102     return status;
   103 }
   104 
   105 
   106 // sendHandshakeRequest() - send HandshakeRequest message
   107 //
   108 //  params:
   109 //      session (in)        session handle
   110 //      state (in)          state the state machine is in
   111 //      partner (in)        partner to communicate with
   112 //
   113 //  returns:
   114 //      PEP_STATUS_OK or any other value on error
   115 
   116 PEP_STATUS sendHandshakeRequest(
   117         PEP_SESSION session,
   118         DeviceState_state state,
   119         const Identity partner
   120     )
   121 {
   122     PEP_STATUS status = PEP_STATUS_OK;
   123     HandshakeRequest_t *msg = NULL;
   124     char *payload = NULL;
   125     message *_message = NULL;
   126     pEp_identity *me = NULL;
   127 
   128     assert(session);
   129     assert(partner);
   130     if (!(session && partner))
   131         return PEP_ILLEGAL_VALUE;
   132 
   133     assert(session->messageToSend);
   134     if (!session->messageToSend)
   135         return PEP_SEND_FUNCTION_NOT_REGISTERED;
   136 
   137     msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   138     assert(msg);
   139     if (!msg)
   140         goto enomem;
   141 
   142     int32_t seq;
   143     status = sequence_value(session, "DeviceGroup", &seq);
   144     if (status != PEP_STATUS_OK)
   145         goto error;
   146     msg->header.sequence = (long) seq;
   147 
   148     msg->state = (long) state;
   149 
   150     me = new_identity(NULL, NULL, NULL, NULL);
   151     if (!me)
   152         goto enomem;
   153     status = myself(session, me);
   154     if (status != PEP_STATUS_OK)
   155         goto error;
   156     if (Identity_from_Struct(me, &msg->me) == NULL)
   157         goto enomem;
   158 
   159     if (Identity_from_Struct(partner, &msg->partner) == NULL)
   160         goto enomem;
   161 
   162     if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
   163         status = PEP_CONTRAINTS_VIOLATED;
   164         goto error;
   165     }
   166 
   167     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   168             NULL, msg, (void **) &payload);
   169     if (size == -1) {
   170         status = PEP_CANNOT_ENCODE;
   171         goto error;
   172     }
   173 
   174     status = prepare_message(me, partner, payload, size, &_message);
   175     if (status != PEP_STATUS_OK)
   176         goto error;
   177     payload = NULL;
   178 
   179     free_identity(me);
   180     me = NULL;
   181 
   182     status = session->messageToSend(session->sync_obj, _message);
   183 
   184     free_message(_message);
   185     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   186 
   187     return status;
   188 
   189 enomem:
   190     status = PEP_OUT_OF_MEMORY;
   191 error:
   192     ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   193     free(payload);
   194     free_message(_message);
   195     free_identity(me);
   196     return status;
   197 }
   198 
   199 
   200 // sendGroupKeys() - send GroupKeys message
   201 //
   202 //  params:
   203 //      session (in)        session handle
   204 //      state (in)          state the state machine is in
   205 //      partner (in)        (must be NULL)
   206 //
   207 //  returns:
   208 //      PEP_STATUS_OK or any other value on error
   209 
   210 PEP_STATUS sendGroupKeys(
   211         PEP_SESSION session,
   212         DeviceState_state state,
   213         const Identity partner
   214     )
   215 {
   216     PEP_STATUS status = PEP_STATUS_OK;
   217     GroupKeys_t *msg = NULL;
   218     char *payload = NULL;
   219     message *_message = NULL;
   220     pEp_identity *me = NULL;
   221 
   222     assert(session);
   223     assert(!partner);
   224     if (!(session && !partner))
   225         return PEP_ILLEGAL_VALUE;
   226 
   227     assert(session->messageToSend);
   228     if (!session->messageToSend)
   229         return PEP_SEND_FUNCTION_NOT_REGISTERED;
   230 
   231     msg = (GroupKeys_t *) calloc(1, sizeof(GroupKeys_t));
   232     assert(msg);
   233     if (!msg)
   234         goto enomem;
   235 
   236     int32_t seq;
   237     status = sequence_value(session, "DeviceGroup", &seq);
   238     if (status != PEP_STATUS_OK)
   239         goto error;
   240     msg->header.sequence = (long) seq;
   241 
   242     msg->state = (long) state;
   243 
   244     me = new_identity(NULL, NULL, NULL, NULL);
   245     if (!me)
   246         goto enomem;
   247     status = myself(session, me);
   248     if (status != PEP_STATUS_OK)
   249         goto error;
   250     if (Identity_from_Struct(me, &msg->me) == NULL)
   251         goto enomem;
   252 
   253     if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
   254         status = PEP_CONTRAINTS_VIOLATED;
   255         goto error;
   256     }
   257 
   258     ssize_t size = uper_encode_to_new_buffer(&asn_DEF_GroupKeys,
   259             NULL, msg, (void **) &payload);
   260     if (size == -1) {
   261         status = PEP_CANNOT_ENCODE;
   262         goto error;
   263     }
   264 
   265     status = prepare_message(me, partner, payload, size, &_message);
   266     if (status != PEP_STATUS_OK)
   267         goto error;
   268     payload = NULL;
   269 
   270     free_identity(me);
   271     me = NULL;
   272 
   273     status = session->messageToSend(session->sync_obj, _message);
   274 
   275     free_message(_message);
   276     ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   277 
   278     return status;
   279 
   280 enomem:
   281     status = PEP_OUT_OF_MEMORY;
   282 error:
   283     ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   284     free(payload);
   285     free_message(_message);
   286     free_identity(me);
   287     return status;
   288 }
   289