src/sync_send_actions.c
changeset 883 096ff09932ad
child 939 ed7dfdae3031
child 1122 bbd0d058f0e3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sync_send_actions.c	Thu Jul 14 13:33:24 2016 +0200
     1.3 @@ -0,0 +1,289 @@
     1.4 +// Send 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 +#include "../asn.1/GroupKeys.h"
    1.16 +
    1.17 +
    1.18 +// sendBeacon() - send Beacon message
    1.19 +//
    1.20 +//  params:
    1.21 +//      session (in)        session handle
    1.22 +//      state (in)          state the state machine is in
    1.23 +//      partner (in)        (must be NULL)
    1.24 +//
    1.25 +//  returns:
    1.26 +//      PEP_STATUS_OK or any other value on error
    1.27 +
    1.28 +PEP_STATUS sendBeacon(
    1.29 +        PEP_SESSION session,
    1.30 +        DeviceState_state state,
    1.31 +        const Identity partner
    1.32 +    )
    1.33 +{
    1.34 +    PEP_STATUS status = PEP_STATUS_OK;
    1.35 +    Beacon_t *msg = NULL;
    1.36 +    char *payload = NULL;
    1.37 +    message *_message = NULL;
    1.38 +    pEp_identity *me = NULL;
    1.39 +
    1.40 +    assert(session);
    1.41 +    assert(!partner);
    1.42 +    if (!(session && !partner))
    1.43 +        return PEP_ILLEGAL_VALUE;
    1.44 +
    1.45 +    assert(session->messageToSend);
    1.46 +    if (!session->messageToSend)
    1.47 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
    1.48 +
    1.49 +    msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    1.50 +    assert(msg);
    1.51 +    if (!msg)
    1.52 +        goto enomem;
    1.53 +
    1.54 +    int32_t seq;
    1.55 +    status = sequence_value(session, "DeviceGroup", &seq);
    1.56 +    if (status != PEP_STATUS_OK)
    1.57 +        goto error;
    1.58 +    msg->header.sequence = (long) seq;
    1.59 +
    1.60 +    msg->state = (long) state;
    1.61 +
    1.62 +    me = new_identity(NULL, NULL, NULL, NULL);
    1.63 +    if (!me)
    1.64 +        goto enomem;
    1.65 +    status = myself(session, me);
    1.66 +    if (status != PEP_STATUS_OK)
    1.67 +        goto error;
    1.68 +    if (Identity_from_Struct(me, &msg->me) == NULL)
    1.69 +        goto enomem;
    1.70 +
    1.71 +    if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    1.72 +        status = PEP_CONTRAINTS_VIOLATED;
    1.73 +        goto error;
    1.74 +    }
    1.75 +
    1.76 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
    1.77 +            NULL, msg, (void **) &payload);
    1.78 +    if (size == -1) {
    1.79 +        status = PEP_CANNOT_ENCODE;
    1.80 +        goto error;
    1.81 +    }
    1.82 +
    1.83 +    status = prepare_message(me, partner, payload, size, &_message);
    1.84 +    if (status != PEP_STATUS_OK)
    1.85 +        goto error;
    1.86 +    payload = NULL;
    1.87 +
    1.88 +    free_identity(me);
    1.89 +    me = NULL;
    1.90 +
    1.91 +    status = session->messageToSend(session->sync_obj, _message);
    1.92 +
    1.93 +    free_message(_message);
    1.94 +    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    1.95 +
    1.96 +    return status;
    1.97 +
    1.98 +enomem:
    1.99 +    status = PEP_OUT_OF_MEMORY;
   1.100 +error:
   1.101 +    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
   1.102 +    free(payload);
   1.103 +    free_message(_message);
   1.104 +    free_identity(me);
   1.105 +    return status;
   1.106 +}
   1.107 +
   1.108 +
   1.109 +// sendHandshakeRequest() - send HandshakeRequest message
   1.110 +//
   1.111 +//  params:
   1.112 +//      session (in)        session handle
   1.113 +//      state (in)          state the state machine is in
   1.114 +//      partner (in)        partner to communicate with
   1.115 +//
   1.116 +//  returns:
   1.117 +//      PEP_STATUS_OK or any other value on error
   1.118 +
   1.119 +PEP_STATUS sendHandshakeRequest(
   1.120 +        PEP_SESSION session,
   1.121 +        DeviceState_state state,
   1.122 +        const Identity partner
   1.123 +    )
   1.124 +{
   1.125 +    PEP_STATUS status = PEP_STATUS_OK;
   1.126 +    HandshakeRequest_t *msg = NULL;
   1.127 +    char *payload = NULL;
   1.128 +    message *_message = NULL;
   1.129 +    pEp_identity *me = NULL;
   1.130 +
   1.131 +    assert(session);
   1.132 +    assert(partner);
   1.133 +    if (!(session && partner))
   1.134 +        return PEP_ILLEGAL_VALUE;
   1.135 +
   1.136 +    assert(session->messageToSend);
   1.137 +    if (!session->messageToSend)
   1.138 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   1.139 +
   1.140 +    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   1.141 +    assert(msg);
   1.142 +    if (!msg)
   1.143 +        goto enomem;
   1.144 +
   1.145 +    int32_t seq;
   1.146 +    status = sequence_value(session, "DeviceGroup", &seq);
   1.147 +    if (status != PEP_STATUS_OK)
   1.148 +        goto error;
   1.149 +    msg->header.sequence = (long) seq;
   1.150 +
   1.151 +    msg->state = (long) state;
   1.152 +
   1.153 +    me = new_identity(NULL, NULL, NULL, NULL);
   1.154 +    if (!me)
   1.155 +        goto enomem;
   1.156 +    status = myself(session, me);
   1.157 +    if (status != PEP_STATUS_OK)
   1.158 +        goto error;
   1.159 +    if (Identity_from_Struct(me, &msg->me) == NULL)
   1.160 +        goto enomem;
   1.161 +
   1.162 +    if (Identity_from_Struct(partner, &msg->partner) == NULL)
   1.163 +        goto enomem;
   1.164 +
   1.165 +    if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
   1.166 +        status = PEP_CONTRAINTS_VIOLATED;
   1.167 +        goto error;
   1.168 +    }
   1.169 +
   1.170 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   1.171 +            NULL, msg, (void **) &payload);
   1.172 +    if (size == -1) {
   1.173 +        status = PEP_CANNOT_ENCODE;
   1.174 +        goto error;
   1.175 +    }
   1.176 +
   1.177 +    status = prepare_message(me, partner, payload, size, &_message);
   1.178 +    if (status != PEP_STATUS_OK)
   1.179 +        goto error;
   1.180 +    payload = NULL;
   1.181 +
   1.182 +    free_identity(me);
   1.183 +    me = NULL;
   1.184 +
   1.185 +    status = session->messageToSend(session->sync_obj, _message);
   1.186 +
   1.187 +    free_message(_message);
   1.188 +    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   1.189 +
   1.190 +    return status;
   1.191 +
   1.192 +enomem:
   1.193 +    status = PEP_OUT_OF_MEMORY;
   1.194 +error:
   1.195 +    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   1.196 +    free(payload);
   1.197 +    free_message(_message);
   1.198 +    free_identity(me);
   1.199 +    return status;
   1.200 +}
   1.201 +
   1.202 +
   1.203 +// sendGroupKeys() - send GroupKeys message
   1.204 +//
   1.205 +//  params:
   1.206 +//      session (in)        session handle
   1.207 +//      state (in)          state the state machine is in
   1.208 +//      partner (in)        (must be NULL)
   1.209 +//
   1.210 +//  returns:
   1.211 +//      PEP_STATUS_OK or any other value on error
   1.212 +
   1.213 +PEP_STATUS sendGroupKeys(
   1.214 +        PEP_SESSION session,
   1.215 +        DeviceState_state state,
   1.216 +        const Identity partner
   1.217 +    )
   1.218 +{
   1.219 +    PEP_STATUS status = PEP_STATUS_OK;
   1.220 +    GroupKeys_t *msg = NULL;
   1.221 +    char *payload = NULL;
   1.222 +    message *_message = NULL;
   1.223 +    pEp_identity *me = NULL;
   1.224 +
   1.225 +    assert(session);
   1.226 +    assert(!partner);
   1.227 +    if (!(session && !partner))
   1.228 +        return PEP_ILLEGAL_VALUE;
   1.229 +
   1.230 +    assert(session->messageToSend);
   1.231 +    if (!session->messageToSend)
   1.232 +        return PEP_SEND_FUNCTION_NOT_REGISTERED;
   1.233 +
   1.234 +    msg = (GroupKeys_t *) calloc(1, sizeof(GroupKeys_t));
   1.235 +    assert(msg);
   1.236 +    if (!msg)
   1.237 +        goto enomem;
   1.238 +
   1.239 +    int32_t seq;
   1.240 +    status = sequence_value(session, "DeviceGroup", &seq);
   1.241 +    if (status != PEP_STATUS_OK)
   1.242 +        goto error;
   1.243 +    msg->header.sequence = (long) seq;
   1.244 +
   1.245 +    msg->state = (long) state;
   1.246 +
   1.247 +    me = new_identity(NULL, NULL, NULL, NULL);
   1.248 +    if (!me)
   1.249 +        goto enomem;
   1.250 +    status = myself(session, me);
   1.251 +    if (status != PEP_STATUS_OK)
   1.252 +        goto error;
   1.253 +    if (Identity_from_Struct(me, &msg->me) == NULL)
   1.254 +        goto enomem;
   1.255 +
   1.256 +    if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
   1.257 +        status = PEP_CONTRAINTS_VIOLATED;
   1.258 +        goto error;
   1.259 +    }
   1.260 +
   1.261 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_GroupKeys,
   1.262 +            NULL, msg, (void **) &payload);
   1.263 +    if (size == -1) {
   1.264 +        status = PEP_CANNOT_ENCODE;
   1.265 +        goto error;
   1.266 +    }
   1.267 +
   1.268 +    status = prepare_message(me, partner, payload, size, &_message);
   1.269 +    if (status != PEP_STATUS_OK)
   1.270 +        goto error;
   1.271 +    payload = NULL;
   1.272 +
   1.273 +    free_identity(me);
   1.274 +    me = NULL;
   1.275 +
   1.276 +    status = session->messageToSend(session->sync_obj, _message);
   1.277 +
   1.278 +    free_message(_message);
   1.279 +    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   1.280 +
   1.281 +    return status;
   1.282 +
   1.283 +enomem:
   1.284 +    status = PEP_OUT_OF_MEMORY;
   1.285 +error:
   1.286 +    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   1.287 +    free(payload);
   1.288 +    free_message(_message);
   1.289 +    free_identity(me);
   1.290 +    return status;
   1.291 +}
   1.292 +