adding sync_impl files keysync
authorVolker Birk <vb@pep.foundation>
Tue, 23 Aug 2016 11:42:05 +0200
branchkeysync
changeset 1059b5e275717e44
parent 1058 1d34706c62d1
child 1060 fcfc07caa0f6
adding sync_impl files
src/sync_impl.c
src/sync_impl.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/sync_impl.c	Tue Aug 23 11:42:05 2016 +0200
     1.3 @@ -0,0 +1,235 @@
     1.4 +#include "../asn.1/DeviceGroup-Protocol.h"
     1.5 +#include "sync_impl.h"
     1.6 +#include "pEp_internal.h"
     1.7 +#include "keymanagement.h"
     1.8 +#include "map_asn1.h"
     1.9 +#include "baseprotocol.h"
    1.10 +
    1.11 +PEP_STATUS receive_sync_msg(
    1.12 +        PEP_SESSION session,
    1.13 +        DeviceGroup_Protocol_t *msg
    1.14 +    )
    1.15 +{
    1.16 +    assert(session && msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING);
    1.17 +    if (!(session && msg && msg->payload.present != DeviceGroup_Protocol__payload_PR_NOTHING))
    1.18 +        return PEP_ILLEGAL_VALUE;
    1.19 +
    1.20 +    void *extra = NULL;
    1.21 +    Identity partner = NULL;
    1.22 +    DeviceState_event event = DeviceState_event_NONE;
    1.23 +
    1.24 +    switch (msg->payload.present) {
    1.25 +        case DeviceGroup_Protocol__payload_PR_beacon:
    1.26 +            partner = Identity_to_Struct(&msg->header.me, NULL);
    1.27 +            if (!partner)
    1.28 +                return PEP_OUT_OF_MEMORY;
    1.29 +            event = Beacon;
    1.30 +            break;
    1.31 +
    1.32 +        case DeviceGroup_Protocol__payload_PR_handshakeRequest:
    1.33 +            partner = Identity_to_Struct(
    1.34 +                    &msg->header.me, NULL);
    1.35 +            if (!partner)
    1.36 +                return PEP_OUT_OF_MEMORY;
    1.37 +            event = HandshakeRequest;
    1.38 +            break;
    1.39 +
    1.40 +        case DeviceGroup_Protocol__payload_PR_groupKeys:
    1.41 +            partner = Identity_to_Struct(&msg->header.me,
    1.42 +                    NULL);
    1.43 +            if (!partner)
    1.44 +                return PEP_OUT_OF_MEMORY;
    1.45 +            identity_list *group_keys = IdentityList_to_identity_list(
    1.46 +                    &msg->payload.choice.groupKeys.ownIdentities, NULL);
    1.47 +            if (!group_keys) {
    1.48 +                free_identity(partner);
    1.49 +                return PEP_OUT_OF_MEMORY;
    1.50 +            }
    1.51 +            extra = (void *) group_keys;
    1.52 +            event = GroupKeys;
    1.53 +            break;
    1.54 +
    1.55 +        default:
    1.56 +            return PEP_SYNC_ILLEGAL_MESSAGE;
    1.57 +    }
    1.58 +
    1.59 +    return fsm_DeviceState_inject(session, event, partner, extra);
    1.60 +}
    1.61 +
    1.62 +PEP_STATUS receive_DeviceState_msg(PEP_SESSION session, message *src)
    1.63 +{
    1.64 +    assert(session && src);
    1.65 +    if (!(session && src))
    1.66 +        return PEP_ILLEGAL_VALUE;
    1.67 +
    1.68 +    bool found = false;
    1.69 +
    1.70 +    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
    1.71 +        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp") == 0
    1.72 +                && bl->size) {
    1.73 +            DeviceGroup_Protocol_t *msg;
    1.74 +            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol,
    1.75 +                    (void **) &msg, bl->value, bl->size);
    1.76 +            if (msg) {
    1.77 +                found = true;
    1.78 +                PEP_STATUS status = session->inject_sync_msg(msg, session->sync_obj);
    1.79 +                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    1.80 +                if (status != PEP_STATUS_OK)
    1.81 +                    return status;
    1.82 +            }
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    if (found) {
    1.87 +        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
    1.88 +                spl = spl->next) {
    1.89 +            if (spl->value->key &&
    1.90 +                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
    1.91 +                if (spl->value->value &&
    1.92 +                        strcasecmp(spl->value->value, "yes") == 0)
    1.93 +                    return PEP_MESSAGE_CONSUMED;
    1.94 +            }
    1.95 +        }
    1.96 +    }
    1.97 +
    1.98 +    return PEP_STATUS_OK;
    1.99 +}
   1.100 +
   1.101 +DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type)
   1.102 +{
   1.103 +    DeviceGroup_Protocol_t *msg = (DeviceGroup_Protocol_t *)
   1.104 +            calloc(1, sizeof(HandshakeRequest_t));
   1.105 +    assert(msg);
   1.106 +    if (!msg)
   1.107 +        return NULL;
   1.108 +    msg->payload.present = type;
   1.109 +    return msg;
   1.110 +}
   1.111 +
   1.112 +void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg)
   1.113 +{
   1.114 +    ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
   1.115 +}
   1.116 +
   1.117 +PEP_STATUS unicast_msg(
   1.118 +        PEP_SESSION session,
   1.119 +        Identity partner,
   1.120 +        DeviceState_state state,
   1.121 +        DeviceGroup_Protocol_t *msg
   1.122 +    )
   1.123 +{
   1.124 +    PEP_STATUS status = PEP_STATUS_OK;
   1.125 +    char *payload = NULL;
   1.126 +    message *_message = NULL;
   1.127 +    pEp_identity *me = NULL;
   1.128 +
   1.129 +    assert(session && partner && state && msg);
   1.130 +    if (!(session && partner && state && msg))
   1.131 +        return PEP_ILLEGAL_VALUE;
   1.132 +
   1.133 +    assert(session->messageToSend);
   1.134 +    if (!session->messageToSend) {
   1.135 +        status = PEP_SEND_FUNCTION_NOT_REGISTERED;
   1.136 +        goto error;
   1.137 +    }
   1.138 +
   1.139 +    int32_t seq;
   1.140 +    status = sequence_value(session, "DeviceGroup", &seq);
   1.141 +    if (status != PEP_STATUS_OK)
   1.142 +        goto error;
   1.143 +    msg->header.sequence = (long) seq;
   1.144 +
   1.145 +    bool devicegroup = storedGroupKeys(session);
   1.146 +    if (devicegroup) { // default is FALSE
   1.147 +        BOOLEAN_t *dg = malloc(sizeof(BOOLEAN_t));
   1.148 +        assert(dg);
   1.149 +        if (!dg)
   1.150 +            goto enomem;
   1.151 +
   1.152 +        *dg = 1;
   1.153 +        msg->header.devicegroup = dg;
   1.154 +    }
   1.155 +
   1.156 +    msg->header.state = (long) state;
   1.157 +
   1.158 +    me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
   1.159 +    if (!me)
   1.160 +        goto enomem;
   1.161 +    status = myself(session, me);
   1.162 +    if (status != PEP_STATUS_OK)
   1.163 +        goto error;
   1.164 +    if (Identity_from_Struct(me, &msg->header.me) == NULL)
   1.165 +        goto enomem;
   1.166 +
   1.167 +    if (asn_check_constraints(&asn_DEF_DeviceGroup_Protocol, msg, NULL, NULL)) {
   1.168 +        status = PEP_CONTRAINTS_VIOLATED;
   1.169 +        goto error;
   1.170 +    }
   1.171 +
   1.172 +    ssize_t size = uper_encode_to_new_buffer(&asn_DEF_DeviceGroup_Protocol,
   1.173 +            NULL, msg, (void **) &payload);
   1.174 +    if (size == -1) {
   1.175 +        status = PEP_CANNOT_ENCODE;
   1.176 +        goto error;
   1.177 +    }
   1.178 +
   1.179 +    status = prepare_message(me, partner, payload, size, &_message);
   1.180 +    if (status != PEP_STATUS_OK)
   1.181 +        goto error;
   1.182 +    payload = NULL;
   1.183 +
   1.184 +    free_identity(me);
   1.185 +    me = NULL;
   1.186 +
   1.187 +    status = session->messageToSend(session->sync_obj, _message);
   1.188 +
   1.189 +    free_DeviceGroup_Protocol_msg(msg);
   1.190 +    free_identity(partner);
   1.191 +    return status;
   1.192 +
   1.193 +enomem:
   1.194 +    status = PEP_OUT_OF_MEMORY;
   1.195 +error:
   1.196 +    free_DeviceGroup_Protocol_msg(msg);
   1.197 +    free(payload);
   1.198 +    free_message(_message);
   1.199 +    free_identity(me);
   1.200 +    free_identity(partner);
   1.201 +    return status;
   1.202 +}
   1.203 +
   1.204 +PEP_STATUS multicast_self_msg(
   1.205 +        PEP_SESSION session,
   1.206 +        DeviceState_state state,
   1.207 +        DeviceGroup_Protocol_t *msg
   1.208 +    )
   1.209 +{
   1.210 +    PEP_STATUS status = PEP_STATUS_OK;
   1.211 +
   1.212 +    assert(session && state && msg);
   1.213 +    if (!(session && state && msg))
   1.214 +        return PEP_ILLEGAL_VALUE;
   1.215 +
   1.216 +    identity_list *own_identities = NULL;
   1.217 +    status = own_identities_retrieve(session, &own_identities);
   1.218 +    if (status != PEP_STATUS_OK)
   1.219 +        return status;
   1.220 +
   1.221 +    for (identity_list *_i = own_identities; _i && _i->ident; _i = _i->next) {
   1.222 +        pEp_identity *me = _i->ident;
   1.223 +
   1.224 +        status = myself(session, me);
   1.225 +        if (status == PEP_OUT_OF_MEMORY)
   1.226 +            goto the_end;
   1.227 +        if (status != PEP_STATUS_OK)
   1.228 +            continue;
   1.229 +     
   1.230 +        status = unicast_msg(session, me, state, msg);
   1.231 +    }
   1.232 +
   1.233 +the_end:
   1.234 +    free_identity_list(own_identities);
   1.235 +    free_DeviceGroup_Protocol_msg(msg);
   1.236 +    return PEP_STATUS_OK;
   1.237 +}
   1.238 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/sync_impl.h	Tue Aug 23 11:42:05 2016 +0200
     2.3 @@ -0,0 +1,36 @@
     2.4 +#pragma once
     2.5 +
     2.6 +#include "message.h"
     2.7 +#include "sync_fsm.h"
     2.8 +
     2.9 +#ifdef __cplusplus
    2.10 +extern "C" {
    2.11 +#endif
    2.12 +
    2.13 +PEP_STATUS receive_sync_msg(
    2.14 +        PEP_SESSION session,
    2.15 +        DeviceGroup_Protocol_t *msg
    2.16 +    );
    2.17 +
    2.18 +PEP_STATUS receive_DeviceState_msg(PEP_SESSION session, message *src);
    2.19 +
    2.20 +DeviceGroup_Protocol_t *new_DeviceGroup_Protocol_msg(DeviceGroup_Protocol__payload_PR type);
    2.21 +void free_DeviceGroup_Protocol_msg(DeviceGroup_Protocol_t *msg);
    2.22 +
    2.23 +PEP_STATUS unicast_msg(
    2.24 +        PEP_SESSION session,
    2.25 +        Identity partner,
    2.26 +        DeviceState_state state,
    2.27 +        DeviceGroup_Protocol_t *msg
    2.28 +    );
    2.29 +
    2.30 +PEP_STATUS multicast_self_msg(
    2.31 +        PEP_SESSION session,
    2.32 +        DeviceState_state state,
    2.33 +        DeviceGroup_Protocol_t *msg
    2.34 +    );
    2.35 +
    2.36 +#ifdef __cplusplus
    2.37 +}
    2.38 +#endif
    2.39 +