some redesign for unicast_msg() keysync
authorVolker Birk <vb@pep.foundation>
Tue, 23 Aug 2016 11:37:37 +0200
branchkeysync
changeset 10581d34706c62d1
parent 1057 abff13590a4c
child 1059 b5e275717e44
some redesign for unicast_msg()
asn.1/devicegroup.asn1
src/sync_send_actions.c
sync/gen_actions_skeleton.ysl2
     1.1 --- a/asn.1/devicegroup.asn1	Tue Aug 23 07:45:23 2016 +0200
     1.2 +++ b/asn.1/devicegroup.asn1	Tue Aug 23 11:37:37 2016 +0200
     1.3 @@ -9,24 +9,26 @@
     1.4  IMPORTS Identity, IdentityList, Header FROM PEP;
     1.5  
     1.6  Beacon ::= SEQUENCE {
     1.7 -    header Header
     1.8 +
     1.9  }
    1.10  
    1.11  HandshakeRequest ::= SEQUENCE {
    1.12 -    header Header
    1.13 +
    1.14  }
    1.15  
    1.16  GroupKeys ::= SEQUENCE {
    1.17 -    header Header,
    1.18      ownIdentities IdentityList
    1.19  }
    1.20  
    1.21  /* for the tags see end of sync.fsm */
    1.22  
    1.23 -DeviceGroup-Protocol ::= CHOICE {
    1.24 -    beacon [APPLICATION 2] Beacon,
    1.25 -    handshakeRequest [APPLICATION 3] HandshakeRequest,
    1.26 -    groupKeys [APPLICATION 4] GroupKeys
    1.27 +DeviceGroup-Protocol ::= SEQUENCE {
    1.28 +    header Header,
    1.29 +    payload CHOICE {
    1.30 +        beacon [APPLICATION 2] Beacon,
    1.31 +        handshakeRequest [APPLICATION 3] HandshakeRequest,
    1.32 +        groupKeys [APPLICATION 4] GroupKeys
    1.33 +    }
    1.34  }
    1.35  
    1.36  END
     2.1 --- a/src/sync_send_actions.c	Tue Aug 23 07:45:23 2016 +0200
     2.2 +++ b/src/sync_send_actions.c	Tue Aug 23 11:37:37 2016 +0200
     2.3 @@ -32,71 +32,21 @@
     2.4      )
     2.5  {
     2.6      PEP_STATUS status = PEP_STATUS_OK;
     2.7 -    Beacon_t *msg = NULL;
     2.8 -    char *payload = NULL;
     2.9 -    message *_message = NULL;
    2.10  
    2.11 -    assert(session);
    2.12 -    assert(!partner);
    2.13 -    if (!(session && !partner))
    2.14 -        return PEP_ILLEGAL_VALUE;
    2.15 -
    2.16 -    assert(session->messageToSend);
    2.17 -    if (!session->messageToSend) {
    2.18 -        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
    2.19 -        goto error;
    2.20 -    }
    2.21 -
    2.22 -    msg = (Beacon_t *) calloc(1, sizeof(Beacon_t));
    2.23 -    assert(msg);
    2.24 +    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_beacon);
    2.25      if (!msg)
    2.26          goto enomem;
    2.27  
    2.28 -    int32_t seq;
    2.29 -    status = sequence_value(session, "DeviceGroup", &seq);
    2.30 +    status = unicast_msg(session, partner, state, msg);
    2.31      if (status != PEP_STATUS_OK)
    2.32          goto error;
    2.33 -    msg->header.sequence = (long) seq;
    2.34  
    2.35 -    bool devicegroup = storedGroupKeys(session);
    2.36 -    if (devicegroup) { // default is FALSE
    2.37 -        BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
    2.38 -        assert(dg);
    2.39 -        if (!dg)
    2.40 -            goto enomem;
    2.41 -
    2.42 -        *dg = 1;
    2.43 -        msg->header.devicegroup = dg;
    2.44 -    }
    2.45 -
    2.46 -    msg->header.state = (long) state;
    2.47 -
    2.48 -
    2.49 -    if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    2.50 -        status = PEP_CONTRAINTS_VIOLATED;
    2.51 -        goto error;
    2.52 -    }
    2.53 -
    2.54 -    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_Beacon,
    2.55 -            NULL, msg, (void **) &payload);
    2.56 -    if (size == -1) {
    2.57 -        status = PEP_CANNOT_ENCODE;
    2.58 -        goto error;
    2.59 -    }
    2.60 -
    2.61 -    status = multicast_self_msg(session, msg);
    2.62 -
    2.63 -    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    2.64 -    free_identity(partner);
    2.65 -    return status;
    2.66 +    return PEP_STATUS_OK;
    2.67  
    2.68  enomem:
    2.69      status = PEP_OUT_OF_MEMORY;
    2.70  error:
    2.71 -    ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    2.72 -    free(payload);
    2.73 -    free_message(_message);
    2.74 -    free_identity(partner);
    2.75 +    free_DeviceGroup_Protocol_msg(msg);
    2.76      return status;
    2.77  }
    2.78  
    2.79 @@ -119,89 +69,21 @@
    2.80      )
    2.81  {
    2.82      PEP_STATUS status = PEP_STATUS_OK;
    2.83 -    HandshakeRequest_t *msg = NULL;
    2.84 -    char *payload = NULL;
    2.85 -    message *_message = NULL;
    2.86 -    pEp_identity *me = NULL;
    2.87  
    2.88 -    assert(session);
    2.89 -    assert(partner);
    2.90 -    if (!(session && partner))
    2.91 -        return PEP_ILLEGAL_VALUE;
    2.92 -
    2.93 -    assert(session->messageToSend);
    2.94 -    if (!session->messageToSend) {
    2.95 -        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
    2.96 -        goto error;
    2.97 -    }
    2.98 -
    2.99 -    msg = (HandshakeRequest_t *) calloc(1, sizeof(HandshakeRequest_t));
   2.100 -    assert(msg);
   2.101 +    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_handshakeRequest);
   2.102      if (!msg)
   2.103          goto enomem;
   2.104  
   2.105 -    int32_t seq;
   2.106 -    status = sequence_value(session, "DeviceGroup", &seq);
   2.107 +    status = unicast_msg(session, partner, state, msg);
   2.108      if (status != PEP_STATUS_OK)
   2.109          goto error;
   2.110 -    msg->header.sequence = (long) seq;
   2.111  
   2.112 -    bool devicegroup = storedGroupKeys(session);
   2.113 -    if (devicegroup) { // default is FALSE
   2.114 -        BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
   2.115 -        assert(dg);
   2.116 -        if (!dg)
   2.117 -            goto enomem;
   2.118 -
   2.119 -        *dg = 1;
   2.120 -        msg->header.devicegroup = dg;
   2.121 -    }
   2.122 -
   2.123 -    msg->header.state = (long) state;
   2.124 -
   2.125 -    me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
   2.126 -    if (!me)
   2.127 -        goto enomem;
   2.128 -    status = myself(session, me);
   2.129 -    if (status != PEP_STATUS_OK)
   2.130 -        goto error;
   2.131 -    if (Identity_from_Struct(me, &msg->header.me) == NULL)
   2.132 -        goto enomem;
   2.133 -
   2.134 -    if (asn_check_constraints(&asn_DEF_HandshakeRequest, msg, NULL, NULL)) {
   2.135 -        status = PEP_CONTRAINTS_VIOLATED;
   2.136 -        goto error;
   2.137 -    }
   2.138 -
   2.139 -    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_HandshakeRequest,
   2.140 -            NULL, msg, (void **) &payload);
   2.141 -    if (size == -1) {
   2.142 -        status = PEP_CANNOT_ENCODE;
   2.143 -        goto error;
   2.144 -    }
   2.145 -
   2.146 -    status = prepare_message(me, partner, payload, size, &_message);
   2.147 -    if (status != PEP_STATUS_OK)
   2.148 -        goto error;
   2.149 -    payload = NULL;
   2.150 -
   2.151 -    free_identity(me);
   2.152 -    me = NULL;
   2.153 -
   2.154 -    status = session->messageToSend(session->sync_obj, _message);
   2.155 -
   2.156 -    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   2.157 -    free_identity(partner);
   2.158 -    return status;
   2.159 +    return PEP_STATUS_OK;
   2.160  
   2.161  enomem:
   2.162      status = PEP_OUT_OF_MEMORY;
   2.163  error:
   2.164 -    ASN_STRUCT_FREE(asn_DEF_HandshakeRequest, msg);
   2.165 -    free(payload);
   2.166 -    free_message(_message);
   2.167 -    free_identity(me);
   2.168 -    free_identity(partner);
   2.169 +    free_DeviceGroup_Protocol_msg(msg);
   2.170      return status;
   2.171  }
   2.172  
   2.173 @@ -224,97 +106,29 @@
   2.174      )
   2.175  {
   2.176      PEP_STATUS status = PEP_STATUS_OK;
   2.177 -    GroupKeys_t *msg = NULL;
   2.178 -    char *payload = NULL;
   2.179 -    message *_message = NULL;
   2.180 -    pEp_identity *me = NULL;
   2.181 -    identity_list *kl = NULL;
   2.182 +    identity_list *kl = new_identity_list(NULL);
   2.183  
   2.184 -    assert(session);
   2.185 -    assert(!partner);
   2.186 -    if (!(session && !partner))
   2.187 -        return PEP_ILLEGAL_VALUE;
   2.188 -
   2.189 -    assert(session->messageToSend);
   2.190 -    if (!session->messageToSend) {
   2.191 -        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
   2.192 -        goto error;
   2.193 -    }
   2.194 -
   2.195 -    msg = (GroupKeys_t *) calloc(1, sizeof(GroupKeys_t));
   2.196 -    assert(msg);
   2.197 +    DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_groupKeys);
   2.198      if (!msg)
   2.199          goto enomem;
   2.200  
   2.201 -    int32_t seq;
   2.202 -    status = sequence_value(session, "DeviceGroup", &seq);
   2.203 -    if (status != PEP_STATUS_OK)
   2.204 -        goto error;
   2.205 -    msg->header.sequence = (long) seq;
   2.206 -
   2.207 -    bool devicegroup = storedGroupKeys(session);
   2.208 -    if (devicegroup) { // default is FALSE
   2.209 -        BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
   2.210 -        assert(dg);
   2.211 -        if (!dg)
   2.212 -            goto enomem;
   2.213 -
   2.214 -        *dg = 1;
   2.215 -        msg->header.devicegroup = dg;
   2.216 -    }
   2.217 -
   2.218 -    msg->header.state = (long) state;
   2.219 -
   2.220 -    me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
   2.221 -    if (!me)
   2.222 -        goto enomem;
   2.223 -    status = myself(session, me);
   2.224 -    if (status != PEP_STATUS_OK)
   2.225 -        goto error;
   2.226 -    if (Identity_from_Struct(me, &msg->header.me) == NULL)
   2.227 -        goto enomem;
   2.228 -
   2.229      status = own_identities_retrieve(session, &kl);
   2.230      if (status != PEP_STATUS_OK)
   2.231          goto error;
   2.232 -    if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
   2.233 +    if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
   2.234          goto enomem;
   2.235  
   2.236 -    if (asn_check_constraints(&asn_DEF_GroupKeys, msg, NULL, NULL)) {
   2.237 -        status = PEP_CONTRAINTS_VIOLATED;
   2.238 -        goto error;
   2.239 -    }
   2.240 -
   2.241 -    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_GroupKeys,
   2.242 -            NULL, msg, (void **) &payload);
   2.243 -    if (size == -1) {
   2.244 -        status = PEP_CANNOT_ENCODE;
   2.245 -        goto error;
   2.246 -    }
   2.247 -
   2.248 -    status = prepare_message(me, partner, payload, size, &_message);
   2.249 +    status = unicast_msg(session, partner, state, msg);
   2.250      if (status != PEP_STATUS_OK)
   2.251          goto error;
   2.252 -    payload = NULL;
   2.253  
   2.254 -    free_identity(me);
   2.255 -    me = NULL;
   2.256 -
   2.257 -    status = session->messageToSend(session->sync_obj, _message);
   2.258 -
   2.259 -    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   2.260 -    free_identity(partner);
   2.261      free_identity_list(kl);
   2.262 -    return status;
   2.263 +    return PEP_STATUS_OK;
   2.264  
   2.265  enomem:
   2.266      status = PEP_OUT_OF_MEMORY;
   2.267  error:
   2.268 -    ASN_STRUCT_FREE(asn_DEF_GroupKeys, msg);
   2.269 -    free(payload);
   2.270 -    free_message(_message);
   2.271 -    free_identity(me);
   2.272 -    free_identity(partner);
   2.273 +    free_DeviceGroup_Protocol_msg(msg);
   2.274      free_identity_list(kl);
   2.275      return status;
   2.276  }
     3.1 --- a/sync/gen_actions_skeleton.ysl2	Tue Aug 23 07:45:23 2016 +0200
     3.2 +++ b/sync/gen_actions_skeleton.ysl2	Tue Aug 23 11:37:37 2016 +0200
     3.3 @@ -7,6 +7,7 @@
     3.4  include yslt.yml2
     3.5  
     3.6  tstylesheet {
     3.7 +    include standardlib.ysl2
     3.8      include ./functions.ysl2
     3.9  
    3.10      template "/protocol" {
    3.11 @@ -134,6 +135,7 @@
    3.12          param "fsm";
    3.13          param "filename", "'###'";
    3.14          const "name", "substring($action/@name, 5, 255)";
    3.15 +        const "lname", "concat(yml:lcase(substring($name, 1, 1)), substring($name, 2))";
    3.16  
    3.17          ||
    3.18  
    3.19 @@ -156,113 +158,35 @@
    3.20              )
    3.21          {
    3.22              PEP_STATUS status = PEP_STATUS_OK;
    3.23 -            «$name»_t *msg = NULL;
    3.24 -            char *payload = NULL;
    3.25 -            message *_message = NULL;
    3.26 -            `` if "count(/protocol/broadcast/*[name()=$action/@name]) = 0" |> pEp_identity *me = NULL;
    3.27 -            `` if "$name='GroupKeys'" |> identity_list *kl = NULL;
    3.28 +            `` if "$name='GroupKeys'" |> identity_list *kl = new_identity_list(NULL);
    3.29  
    3.30 -            `` call "paramcheck" with "partner", "parm/partner";
    3.31 -
    3.32 -            assert(session->messageToSend);
    3.33 -            if (!session->messageToSend) {
    3.34 -                status = PEP_SEND_FUNCTION_NOT_REGISTERED;
    3.35 -                goto error;
    3.36 -            }
    3.37 -
    3.38 -            msg = («$name»_t *) calloc(1, sizeof(«$name»_t));
    3.39 -            assert(msg);
    3.40 +            DeviceGroup_Protocol_t *msg = new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR_«$lname»);
    3.41              if (!msg)
    3.42                  goto enomem;
    3.43 -
    3.44 -            int32_t seq;
    3.45 -            status = sequence_value(session, "DeviceGroup", &seq);
    3.46 -            if (status != PEP_STATUS_OK)
    3.47 -                goto error;
    3.48 -            msg->header.sequence = (long) seq;
    3.49 -
    3.50 -            bool devicegroup = storedGroupKeys(session);
    3.51 -            if (devicegroup) { // default is FALSE
    3.52 -                BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
    3.53 -                assert(dg);
    3.54 -                if (!dg)
    3.55 -                    goto enomem;
    3.56 -
    3.57 -                *dg = 1;
    3.58 -                msg->header.devicegroup = dg;
    3.59 +        ||
    3.60 +        choose {
    3.61 +            when "$name='GroupKeys'" {
    3.62 +                |
    3.63 +                |> status = own_identities_retrieve(session, &kl);
    3.64 +                |> if (status != PEP_STATUS_OK)
    3.65 +                |>> goto error;
    3.66 +                |> if (IdentityList_from_identity_list(kl, &msg->payload.choice.groupKeys.ownIdentities) == NULL)
    3.67 +                |>> goto enomem;
    3.68              }
    3.69 -
    3.70 -            msg->header.state = (long) state;
    3.71 -
    3.72 -        ||
    3.73 -        if "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
    3.74 -        ||
    3.75 -            me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
    3.76 -            if (!me)
    3.77 -                goto enomem;
    3.78 -            status = myself(session, me);
    3.79 -            if (status != PEP_STATUS_OK)
    3.80 -                goto error;
    3.81 -            if (Identity_from_Struct(me, &msg->header.me) == NULL)
    3.82 -                goto enomem;
    3.83 -        ||
    3.84 -        if "$name='GroupKeys'" {
    3.85 -            |
    3.86 -            |> status = own_identities_retrieve(session, &kl);
    3.87 -            |> if (status != PEP_STATUS_OK)
    3.88 -            |>> goto error;
    3.89 -            |> if (IdentityList_from_identity_list(kl, &msg->ownIdentities) == NULL)
    3.90 -            |>> goto enomem;
    3.91          }
    3.92          ||
    3.93  
    3.94 -            if (asn_check_constraints(&asn_DEF_«$name», msg, NULL, NULL)) {
    3.95 -                status = PEP_CONTRAINTS_VIOLATED;
    3.96 -                goto error;
    3.97 -            }
    3.98 -
    3.99 -            ssize_t size = uper_encode_to_new_buffer(&asn_DEF_«$name»,
   3.100 -                    NULL, msg, (void **) &payload);
   3.101 -            if (size == -1) {
   3.102 -                status = PEP_CANNOT_ENCODE;
   3.103 -                goto error;
   3.104 -            }
   3.105 -
   3.106 -        ||
   3.107 -        choose {
   3.108 -            when "count(/protocol/broadcast/*[name()=$action/@name]) > 0"
   3.109 -                |> status = multicast_self_msg(session, msg);
   3.110 -            otherwise
   3.111 -        ||
   3.112 -            status = prepare_message(me, partner, payload, size, &_message);
   3.113 +            status = unicast_msg(session, partner, state, msg);
   3.114              if (status != PEP_STATUS_OK)
   3.115                  goto error;
   3.116 -            payload = NULL;
   3.117  
   3.118 -            free_identity(me);
   3.119 -            me = NULL;
   3.120 -
   3.121 -            status = session->messageToSend(session->«$filename»_obj, _message);
   3.122 -        ||
   3.123 -        }
   3.124 -        ||
   3.125 -
   3.126 -            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   3.127 -            free_identity(partner);
   3.128              `` if "$name='GroupKeys'" |> free_identity_list(kl);
   3.129 -            return status;
   3.130 +            return PEP_STATUS_OK;
   3.131  
   3.132          enomem:
   3.133              status = PEP_OUT_OF_MEMORY;
   3.134          error:
   3.135 -            ASN_STRUCT_FREE(asn_DEF_«$name», msg);
   3.136 -            free(payload);
   3.137 -            free_message(_message);
   3.138 -        ||
   3.139 -        if "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
   3.140 -            |> free_identity(me);
   3.141 -        ||
   3.142 -            free_identity(partner);
   3.143 +            free_DeviceGroup_Protocol_msg(msg);
   3.144              `` if "$name='GroupKeys'" |> free_identity_list(kl);
   3.145              return status;
   3.146          }