src/sync_send_actions.c
author Volker Birk <vb@pep.foundation>
Tue, 23 Aug 2016 15:01:06 +0200
branchkeysync
changeset 1066 e6db6bb9d542
parent 1060 fcfc07caa0f6
child 1075 6ac3fd936e93
permissions -rw-r--r--
new_ and free_ together
     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/DeviceGroup-Protocol.h"
    11 #include "sync_impl.h"
    12 #include "../asn.1/Beacon.h"
    13 #include "../asn.1/HandshakeRequest.h"
    14 #include "../asn.1/GroupKeys.h"
    15 
    16 
    17 // sendBeacon() - send Beacon message
    18 //
    19 //  params:
    20 //      session (in)        session handle
    21 //      state (in)          state the state machine is in
    22 //      partner (in)        (must be NULL)
    23 //
    24 //  returns:
    25 //      PEP_STATUS_OK or any other value on error
    26 
    27 PEP_STATUS sendBeacon(
    28         PEP_SESSION session,
    29         DeviceState_state state,
    30         Identity partner,
    31         void *extra
    32     )
    33 {
    34     assert(session && state);
    35     if (!(session && state))
    36         return PEP_ILLEGAL_VALUE;
    37 
    38     PEP_STATUS status = PEP_STATUS_OK;
    39 
    40     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_beacon);
    41     if (!msg)
    42         goto enomem;
    43 
    44     status = multicast_self_msg(session, state, msg);
    45     if (status != PEP_STATUS_OK)
    46         goto error;
    47 
    48     free_DeviceGroup_Protocol_msg(msg);
    49     return PEP_STATUS_OK;
    50 
    51 enomem:
    52     status = PEP_OUT_OF_MEMORY;
    53 error:
    54     free_DeviceGroup_Protocol_msg(msg);
    55     return status;
    56 }
    57 
    58 
    59 // sendHandshakeRequest() - send HandshakeRequest message
    60 //
    61 //  params:
    62 //      session (in)        session handle
    63 //      state (in)          state the state machine is in
    64 //      partner (in)        partner to communicate with
    65 //
    66 //  returns:
    67 //      PEP_STATUS_OK or any other value on error
    68 
    69 PEP_STATUS sendHandshakeRequest(
    70         PEP_SESSION session,
    71         DeviceState_state state,
    72         Identity partner,
    73         void *extra
    74     )
    75 {
    76     assert(session && state);
    77     if (!(session && state))
    78         return PEP_ILLEGAL_VALUE;
    79 
    80     PEP_STATUS status = PEP_STATUS_OK;
    81 
    82     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
    83     if (!msg)
    84         goto enomem;
    85 
    86     status = unicast_msg(session, partner, state, msg);
    87     if (status != PEP_STATUS_OK)
    88         goto error;
    89 
    90     free_DeviceGroup_Protocol_msg(msg);
    91     return PEP_STATUS_OK;
    92 
    93 enomem:
    94     status = PEP_OUT_OF_MEMORY;
    95 error:
    96     free_DeviceGroup_Protocol_msg(msg);
    97     return status;
    98 }
    99 
   100 
   101 // sendGroupKeys() - send GroupKeys message
   102 //
   103 //  params:
   104 //      session (in)        session handle
   105 //      state (in)          state the state machine is in
   106 //      partner (in)        (must be NULL)
   107 //
   108 //  returns:
   109 //      PEP_STATUS_OK or any other value on error
   110 
   111 PEP_STATUS sendGroupKeys(
   112         PEP_SESSION session,
   113         DeviceState_state state,
   114         Identity partner,
   115         void *extra
   116     )
   117 {
   118     assert(session && state);
   119     if (!(session && state))
   120         return PEP_ILLEGAL_VALUE;
   121 
   122     PEP_STATUS status = PEP_STATUS_OK;
   123     identity_list *kl = new_identity_list(NULL);
   124 
   125     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
   126     if (!msg)
   127         goto enomem;
   128 
   129     status = own_identities_retrieve(session, &kl);
   130     if (status != PEP_STATUS_OK)
   131         goto error;
   132     if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
   133         goto enomem;
   134 
   135     status = unicast_msg(session, partner, state, msg);
   136     if (status != PEP_STATUS_OK)
   137         goto error;
   138 
   139     free_identity_list(kl);
   140     free_DeviceGroup_Protocol_msg(msg);
   141     return PEP_STATUS_OK;
   142 
   143 enomem:
   144     status = PEP_OUT_OF_MEMORY;
   145 error:
   146     free_DeviceGroup_Protocol_msg(msg);
   147     free_identity_list(kl);
   148     return status;
   149 }
   150