sync/generated/sync_send_actions.c
author Krista Bennett <krista@pep-project.org>
Mon, 20 Nov 2017 16:04:50 +0100
branchENGINE-289
changeset 2287 026ab4dae779
parent 1627 bf605dd7b705
parent 2271 25bfd109320c
permissions -rw-r--r--
merged in default
     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 #include "../asn.1/GroupUpdate.h"
    16 #include "../asn.1/UpdateRequest.h"
    17 
    18 
    19 // sendBeacon() - send Beacon message
    20 //
    21 //  params:
    22 //      session (in)        session handle
    23 //      state (in)          state the state machine is in
    24 //      partner (in)        (must be NULL)
    25 //
    26 //  returns:
    27 //      PEP_STATUS_OK or any other value on error
    28 
    29 PEP_STATUS sendBeacon(
    30         PEP_SESSION session,
    31         DeviceState_state state,
    32         Identity partner,
    33         void *extra
    34     )
    35 {
    36     assert(session && state);
    37     if (!(session && state))
    38         return PEP_ILLEGAL_VALUE;
    39 
    40     PEP_STATUS status = PEP_STATUS_OK;
    41 
    42     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_beacon);
    43     if (!msg)
    44         goto enomem;
    45 
    46     bool encrypted = false;
    47     status = multicast_self_msg(session, state, msg, encrypted);
    48     if (status != PEP_STATUS_OK)
    49         goto error;
    50 
    51     free_DeviceGroup_Protocol_msg(msg);
    52     return PEP_STATUS_OK;
    53 
    54 enomem:
    55     status = PEP_OUT_OF_MEMORY;
    56 error:
    57     free_DeviceGroup_Protocol_msg(msg);
    58     return status;
    59 }
    60 
    61 
    62 // sendHandshakeRequest() - send HandshakeRequest message
    63 //
    64 //  params:
    65 //      session (in)        session handle
    66 //      state (in)          state the state machine is in
    67 //      partner (in)        partner to communicate with
    68 //
    69 //  returns:
    70 //      PEP_STATUS_OK or any other value on error
    71 
    72 PEP_STATUS sendHandshakeRequest(
    73         PEP_SESSION session,
    74         DeviceState_state state,
    75         Identity partner,
    76         void *extra
    77     )
    78 {
    79     assert(session && state);
    80     if (!(session && state))
    81         return PEP_ILLEGAL_VALUE;
    82 
    83     PEP_STATUS status = PEP_STATUS_OK;
    84 
    85     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
    86     if (!msg)
    87         goto enomem;
    88 
    89     msg->payload.choice.handshakeRequest.partner_id = 
    90         OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
    91                                  partner->user_id, -1);
    92     if (partner->user_id && !msg->payload.choice.handshakeRequest.partner_id)
    93        goto enomem;
    94 
    95     char *devgrp = NULL;
    96     status = get_device_group(session, &devgrp);
    97     if (status == PEP_STATUS_OK && devgrp && devgrp[0])
    98     msg->payload.choice.handshakeRequest.group_id = 
    99         OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
   100                                  devgrp, -1);
   101     free(devgrp);
   102     if (devgrp && !msg->payload.choice.handshakeRequest.partner_id)
   103        goto enomem;
   104 
   105     bool encrypted = true;
   106     status = unicast_msg(session, partner, state, msg, encrypted);
   107     if (status != PEP_STATUS_OK)
   108         goto error;
   109 
   110     free_DeviceGroup_Protocol_msg(msg);
   111     return PEP_STATUS_OK;
   112 
   113 enomem:
   114     status = PEP_OUT_OF_MEMORY;
   115 error:
   116     free_DeviceGroup_Protocol_msg(msg);
   117     return status;
   118 }
   119 
   120 
   121 // sendGroupKeys() - send GroupKeys message
   122 //
   123 //  params:
   124 //      session (in)        session handle
   125 //      state (in)          state the state machine is in
   126 //      partner (in)        partner to communicate with
   127 //
   128 //  returns:
   129 //      PEP_STATUS_OK or any other value on error
   130 
   131 PEP_STATUS sendGroupKeys(
   132         PEP_SESSION session,
   133         DeviceState_state state,
   134         Identity partner,
   135         void *extra
   136     )
   137 {
   138     assert(session && state);
   139     if (!(session && state))
   140         return PEP_ILLEGAL_VALUE;
   141 
   142     PEP_STATUS status = PEP_STATUS_OK;
   143     identity_list *kl = new_identity_list(NULL);
   144 
   145     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
   146     if (!msg)
   147         goto enomem;
   148 
   149     status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
   150     if (status != PEP_STATUS_OK)
   151         goto error;
   152     if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
   153         goto enomem;
   154 
   155     msg->payload.choice.groupKeys.partner_id = 
   156         OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
   157                                  partner->user_id, -1);
   158     if (partner->user_id && !msg->payload.choice.groupKeys.partner_id)
   159        goto enomem;
   160 
   161     char *devgrp = NULL;
   162     status = get_device_group(session, &devgrp);
   163     if (status == PEP_STATUS_OK && devgrp && devgrp[0])
   164     msg->payload.choice.groupKeys.group_id = 
   165         OCTET_STRING_new_fromBuf(&asn_DEF_UTF8String,
   166                                  devgrp, -1);
   167     free(devgrp);
   168     if (devgrp && !msg->payload.choice.groupKeys.partner_id)
   169        goto enomem;
   170 
   171     bool encrypted = true;
   172     status = unicast_msg(session, partner, state, msg, encrypted);
   173     if (status != PEP_STATUS_OK)
   174         goto error;
   175 
   176     free_identity_list(kl);
   177     free_DeviceGroup_Protocol_msg(msg);
   178     return PEP_STATUS_OK;
   179 
   180 enomem:
   181     status = PEP_OUT_OF_MEMORY;
   182 error:
   183     free_DeviceGroup_Protocol_msg(msg);
   184     free_identity_list(kl);
   185     return status;
   186 }
   187 
   188 
   189 // sendGroupUpdate() - send GroupUpdate message
   190 //
   191 //  params:
   192 //      session (in)        session handle
   193 //      state (in)          state the state machine is in
   194 //      partner (in)        (must be NULL)
   195 //
   196 //  returns:
   197 //      PEP_STATUS_OK or any other value on error
   198 
   199 PEP_STATUS sendGroupUpdate(
   200         PEP_SESSION session,
   201         DeviceState_state state,
   202         Identity partner,
   203         void *extra
   204     )
   205 {
   206     assert(session && state);
   207     if (!(session && state))
   208         return PEP_ILLEGAL_VALUE;
   209 
   210     PEP_STATUS status = PEP_STATUS_OK;
   211     identity_list *kl = new_identity_list(NULL);
   212 
   213     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupUpdate);
   214     if (!msg)
   215         goto enomem;
   216 
   217     status = _own_identities_retrieve(session, &kl, PEP_idf_not_for_sync);
   218     if (status != PEP_STATUS_OK)
   219         goto error;
   220     if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupUpdate.ownIdentities) == NULL)
   221         goto enomem;
   222 
   223     bool encrypted = true;
   224     status = multicast_self_msg(session, state, msg, encrypted);
   225     if (status != PEP_STATUS_OK)
   226         goto error;
   227 
   228     free_identity_list(kl);
   229     free_DeviceGroup_Protocol_msg(msg);
   230     return PEP_STATUS_OK;
   231 
   232 enomem:
   233     status = PEP_OUT_OF_MEMORY;
   234 error:
   235     free_DeviceGroup_Protocol_msg(msg);
   236     return status;
   237 }
   238 
   239 
   240 // sendUpdateRequest() - send UpdateRequest message
   241 //
   242 //  params:
   243 //      session (in)        session handle
   244 //      state (in)          state the state machine is in
   245 //      partner (in)        (must be NULL)
   246 //
   247 //  returns:
   248 //      PEP_STATUS_OK or any other value on error
   249 
   250 PEP_STATUS sendUpdateRequest(
   251         PEP_SESSION session,
   252         DeviceState_state state,
   253         Identity partner,
   254         void *extra
   255     )
   256 {
   257     assert(session && state);
   258     if (!(session && state))
   259         return PEP_ILLEGAL_VALUE;
   260 
   261     PEP_STATUS status = PEP_STATUS_OK;
   262 
   263     DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_updateRequest);
   264     if (!msg)
   265         goto enomem;
   266 
   267     bool encrypted = true;
   268     status = multicast_self_msg(session, state, msg, encrypted);
   269     if (status != PEP_STATUS_OK)
   270         goto error;
   271 
   272     free_DeviceGroup_Protocol_msg(msg);
   273     return PEP_STATUS_OK;
   274 
   275 enomem:
   276     status = PEP_OUT_OF_MEMORY;
   277 error:
   278     free_DeviceGroup_Protocol_msg(msg);
   279     return status;
   280 }
   281 
   282 
   283 PEP_STATUS _notifyHandshake(
   284         PEP_SESSION session,
   285         Identity partner,
   286         sync_handshake_signal signal
   287     );
   288 
   289 // notifyInitFormGroup() - notify InitFormGroup to app
   290 //
   291 //  params:
   292 //      session (in)        session handle
   293 //      state (in)          state the state machine is in
   294 //      partner (in)        partner to communicate with
   295 //
   296 //  returns:
   297 //      PEP_STATUS_OK or any other value on error
   298 
   299 PEP_STATUS notifyInitFormGroup(
   300         PEP_SESSION session,
   301         DeviceState_state state,
   302         Identity partner,
   303         void *extra
   304     )
   305 {
   306     assert(session && state);
   307     assert(extra == NULL);
   308     if (!(session && state && extra == NULL))
   309         return PEP_ILLEGAL_VALUE;
   310 
   311     return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_FORM_GROUP);
   312 }
   313 
   314 
   315 // notifyInitAddOurDevice() - notify InitAddOurDevice to app
   316 //
   317 //  params:
   318 //      session (in)        session handle
   319 //      state (in)          state the state machine is in
   320 //      partner (in)        partner to communicate with
   321 //
   322 //  returns:
   323 //      PEP_STATUS_OK or any other value on error
   324 
   325 PEP_STATUS notifyInitAddOurDevice(
   326         PEP_SESSION session,
   327         DeviceState_state state,
   328         Identity partner,
   329         void *extra
   330     )
   331 {
   332     assert(session && state);
   333     assert(extra == NULL);
   334     if (!(session && state && extra == NULL))
   335         return PEP_ILLEGAL_VALUE;
   336 
   337     return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_ADD_OUR_DEVICE);
   338 }
   339 
   340 
   341 // notifyAcceptedGroupCreated() - notify AcceptedGroupCreated to app
   342 //
   343 //  params:
   344 //      session (in)        session handle
   345 //      state (in)          state the state machine is in
   346 //      partner (in)        partner to communicate with
   347 //
   348 //  returns:
   349 //      PEP_STATUS_OK or any other value on error
   350 
   351 PEP_STATUS notifyAcceptedGroupCreated(
   352         PEP_SESSION session,
   353         DeviceState_state state,
   354         Identity partner,
   355         void *extra
   356     )
   357 {
   358     assert(session && state);
   359     assert(extra == NULL);
   360     if (!(session && state && extra == NULL))
   361         return PEP_ILLEGAL_VALUE;
   362 
   363     return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_GROUP_CREATED);
   364 }
   365 
   366 
   367 // notifyTimeout() - notify Timeout to app
   368 //
   369 //  params:
   370 //      session (in)        session handle
   371 //      state (in)          state the state machine is in
   372 //      partner (in)        partner to communicate with
   373 //
   374 //  returns:
   375 //      PEP_STATUS_OK or any other value on error
   376 
   377 PEP_STATUS notifyTimeout(
   378         PEP_SESSION session,
   379         DeviceState_state state,
   380         Identity partner,
   381         void *extra
   382     )
   383 {
   384     assert(session && state);
   385     assert(extra == NULL);
   386     if (!(session && state && extra == NULL))
   387         return PEP_ILLEGAL_VALUE;
   388 
   389     return _notifyHandshake(session, partner, SYNC_NOTIFY_TIMEOUT);
   390 }
   391 
   392 
   393 // notifyAcceptedDeviceAdded() - notify AcceptedDeviceAdded to app
   394 //
   395 //  params:
   396 //      session (in)        session handle
   397 //      state (in)          state the state machine is in
   398 //      partner (in)        partner to communicate with
   399 //
   400 //  returns:
   401 //      PEP_STATUS_OK or any other value on error
   402 
   403 PEP_STATUS notifyAcceptedDeviceAdded(
   404         PEP_SESSION session,
   405         DeviceState_state state,
   406         Identity partner,
   407         void *extra
   408     )
   409 {
   410     assert(session && state);
   411     assert(extra == NULL);
   412     if (!(session && state && extra == NULL))
   413         return PEP_ILLEGAL_VALUE;
   414 
   415     return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED);
   416 }
   417 
   418 
   419 // notifyInitAddOtherDevice() - notify InitAddOtherDevice to app
   420 //
   421 //  params:
   422 //      session (in)        session handle
   423 //      state (in)          state the state machine is in
   424 //      partner (in)        partner to communicate with
   425 //
   426 //  returns:
   427 //      PEP_STATUS_OK or any other value on error
   428 
   429 PEP_STATUS notifyInitAddOtherDevice(
   430         PEP_SESSION session,
   431         DeviceState_state state,
   432         Identity partner,
   433         void *extra
   434     )
   435 {
   436     assert(session && state);
   437     assert(extra == NULL);
   438     if (!(session && state && extra == NULL))
   439         return PEP_ILLEGAL_VALUE;
   440 
   441     return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE);
   442 }
   443 
   444 
   445 // notifyInitMoveOurDevice() - notify InitMoveOurDevice to app
   446 //
   447 //  params:
   448 //      session (in)        session handle
   449 //      state (in)          state the state machine is in
   450 //      partner (in)        partner to communicate with
   451 //
   452 //  returns:
   453 //      PEP_STATUS_OK or any other value on error
   454 
   455 PEP_STATUS notifyInitMoveOurDevice(
   456         PEP_SESSION session,
   457         DeviceState_state state,
   458         Identity partner,
   459         void *extra
   460     )
   461 {
   462     assert(session && state);
   463     assert(extra == NULL);
   464     if (!(session && state && extra == NULL))
   465         return PEP_ILLEGAL_VALUE;
   466 
   467     return _notifyHandshake(session, partner, SYNC_NOTIFY_INIT_MOVE_OUR_DEVICE);
   468 }
   469 
   470 
   471 // notifyOvertaken() - notify Overtaken to app
   472 //
   473 //  params:
   474 //      session (in)        session handle
   475 //      state (in)          state the state machine is in
   476 //      partner (in)        partner to communicate with
   477 //
   478 //  returns:
   479 //      PEP_STATUS_OK or any other value on error
   480 
   481 PEP_STATUS notifyOvertaken(
   482         PEP_SESSION session,
   483         DeviceState_state state,
   484         Identity partner,
   485         void *extra
   486     )
   487 {
   488     assert(session && state);
   489     assert(extra == NULL);
   490     if (!(session && state && extra == NULL))
   491         return PEP_ILLEGAL_VALUE;
   492 
   493     return _notifyHandshake(session, partner, SYNC_NOTIFY_OVERTAKEN);
   494 }
   495 
   496 
   497 // notifyAcceptedDeviceMoved() - notify AcceptedDeviceMoved to app
   498 //
   499 //  params:
   500 //      session (in)        session handle
   501 //      state (in)          state the state machine is in
   502 //      partner (in)        partner to communicate with
   503 //
   504 //  returns:
   505 //      PEP_STATUS_OK or any other value on error
   506 
   507 PEP_STATUS notifyAcceptedDeviceMoved(
   508         PEP_SESSION session,
   509         DeviceState_state state,
   510         Identity partner,
   511         void *extra
   512     )
   513 {
   514     assert(session && state);
   515     assert(extra == NULL);
   516     if (!(session && state && extra == NULL))
   517         return PEP_ILLEGAL_VALUE;
   518 
   519     return _notifyHandshake(session, partner, SYNC_NOTIFY_ACCEPTED_DEVICE_MOVED);
   520 }
   521