restructuring things (will not build) keysync
authorVolker Birk <vb@pep.foundation>
Mon, 22 Aug 2016 17:26:40 +0200
branchkeysync
changeset 10472a4a2ec1340b
parent 1046 73be5d9af815
child 1048 a9b9b8096891
restructuring things (will not build)
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
src/sync_actions.c
src/sync_fsm.h
src/sync_send_actions.c
sync/devicegroup.fsm
sync/gen_actions_skeleton.ysl2
sync/gen_statemachine.ysl2
test/get_own_addresses_test.cc
     1.1 --- a/src/pEpEngine.c	Sat Aug 20 18:04:13 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Aug 22 17:26:40 2016 +0200
     1.3 @@ -38,7 +38,6 @@
     1.4      static const char *sql_crashdump;
     1.5      static const char *sql_languagelist;
     1.6      static const char *sql_i18n_token;
     1.7 -    static const char *sql_get_own_addresses;
     1.8  
     1.9      // blacklist
    1.10      static const char *sql_blacklist_add;
    1.11 @@ -337,8 +336,6 @@
    1.12  
    1.13          sql_i18n_token = "select phrase from i18n_token where lang = lower(?1) and id = ?2 ;";
    1.14  
    1.15 -        sql_get_own_addresses = "select address from identity where user_id = '" PEP_OWN_USERID "';";
    1.16 -
    1.17          // blacklist
    1.18  
    1.19          sql_blacklist_add = "insert or replace into blacklist_keys (fpr) values (upper(replace(?1,' ',''))) ;"
    1.20 @@ -447,11 +444,6 @@
    1.21              (int)strlen(sql_i18n_token), &_session->i18n_token, NULL);
    1.22      assert(int_result == SQLITE_OK);
    1.23  
    1.24 -    int_result = sqlite3_prepare_v2(_session->db, sql_get_own_addresses,
    1.25 -            (int)strlen(sql_get_own_addresses), &_session->get_own_addresses,
    1.26 -            NULL);
    1.27 -    assert(int_result == SQLITE_OK);
    1.28 -
    1.29      // blacklist
    1.30  
    1.31      int_result = sqlite3_prepare_v2(_session->db, sql_blacklist_add,
    1.32 @@ -1731,52 +1723,6 @@
    1.33      return status;
    1.34  }
    1.35  
    1.36 -DYNAMIC_API PEP_STATUS get_own_addresses(
    1.37 -        PEP_SESSION session,
    1.38 -        stringlist_t **addresses
    1.39 -    )
    1.40 -{
    1.41 -    PEP_STATUS status = PEP_STATUS_OK;
    1.42 -
    1.43 -    assert(session && addresses);
    1.44 -    if (!(session && addresses))
    1.45 -        return PEP_ILLEGAL_VALUE;
    1.46 -
    1.47 -    stringlist_t *_addresses = new_stringlist(NULL);
    1.48 -    if (!_addresses)
    1.49 -        return PEP_OUT_OF_MEMORY;
    1.50 -
    1.51 -    sqlite3_reset(session->get_own_addresses);
    1.52 -    int result;
    1.53 -    stringlist_t *_a = _addresses;
    1.54 -    do {
    1.55 -        result = sqlite3_step(session->get_own_addresses);
    1.56 -        const char *address;
    1.57 -        switch (result) {
    1.58 -            case SQLITE_ROW:
    1.59 -                address = (const char *)
    1.60 -                    sqlite3_column_text(session->get_own_addresses, 0);
    1.61 -                _a = stringlist_add(_a, address);
    1.62 -                if (!_a) {
    1.63 -                    free_stringlist(_addresses);
    1.64 -                    return PEP_OUT_OF_MEMORY;
    1.65 -                }
    1.66 -                break;
    1.67 -
    1.68 -            case SQLITE_DONE:
    1.69 -                break;
    1.70 -
    1.71 -            default:
    1.72 -                status = PEP_UNKNOWN_ERROR;
    1.73 -        }
    1.74 -    } while (result == SQLITE_ROW);
    1.75 -
    1.76 -    sqlite3_reset(session->get_own_addresses);
    1.77 -    if (status == PEP_STATUS_OK)
    1.78 -        *addresses = _addresses;
    1.79 -    return status;
    1.80 -}
    1.81 -
    1.82  DYNAMIC_API PEP_STATUS set_revoked(
    1.83         PEP_SESSION session,
    1.84         const char *revoked_fpr,
     2.1 --- a/src/pEpEngine.h	Sat Aug 20 18:04:13 2016 +0200
     2.2 +++ b/src/pEpEngine.h	Mon Aug 22 17:26:40 2016 +0200
     2.3 @@ -869,17 +869,6 @@
     2.4      );
     2.5  
     2.6  
     2.7 -// get_own_addresses() - get a list of own addresses
     2.8 -//
     2.9 -//  parameters:
    2.10 -//      session (in)            session handle
    2.11 -//      addresses (out)         stringlist with addresses
    2.12 -
    2.13 -DYNAMIC_API PEP_STATUS get_own_addresses(
    2.14 -        PEP_SESSION session,
    2.15 -        stringlist_t **addresses
    2.16 -    );
    2.17 -
    2.18  // set_revoked() - records relation between a revoked key and its replacement
    2.19  //
    2.20  //  parameters:
     3.1 --- a/src/pEp_internal.h	Sat Aug 20 18:04:13 2016 +0200
     3.2 +++ b/src/pEp_internal.h	Mon Aug 22 17:26:40 2016 +0200
     3.3 @@ -106,7 +106,6 @@
     3.4      sqlite3_stmt *crashdump;
     3.5      sqlite3_stmt *languagelist;
     3.6      sqlite3_stmt *i18n_token;
     3.7 -    sqlite3_stmt *get_own_addresses;
     3.8  
     3.9      // blacklist
    3.10      sqlite3_stmt *blacklist_add;
     4.1 --- a/src/sync_actions.c	Sat Aug 20 18:04:13 2016 +0200
     4.2 +++ b/src/sync_actions.c	Mon Aug 22 17:26:40 2016 +0200
     4.3 @@ -6,7 +6,6 @@
     4.4  #include "sync_fsm.h"
     4.5  #include "map_asn1.h"
     4.6  #include "baseprotocol.h"
     4.7 -#include "../asn.1/DeviceGroup-Protocol.h"
     4.8  
     4.9  // conditions
    4.10  
    4.11 @@ -229,146 +228,3 @@
    4.12      return status;
    4.13  }
    4.14  
    4.15 -PEP_STATUS receive_sync_msg(
    4.16 -        PEP_SESSION session,
    4.17 -        DeviceGroup_Protocol_t *msg
    4.18 -    )
    4.19 -{
    4.20 -    assert(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING);
    4.21 -    if (!(session && msg && msg->present != DeviceGroup_Protocol_PR_NOTHING))
    4.22 -        return PEP_ILLEGAL_VALUE;
    4.23 -
    4.24 -    void *extra = NULL;
    4.25 -    Identity partner = NULL;
    4.26 -    DeviceState_event event = DeviceState_event_NONE;
    4.27 -
    4.28 -    switch (msg->present) {
    4.29 -        case DeviceGroup_Protocol_PR_beacon:
    4.30 -            partner = Identity_to_Struct(&msg->choice.beacon.header.me, NULL);
    4.31 -            if (!partner)
    4.32 -                return PEP_OUT_OF_MEMORY;
    4.33 -            event = Beacon;
    4.34 -            break;
    4.35 -
    4.36 -        case DeviceGroup_Protocol_PR_handshakeRequest:
    4.37 -            partner = Identity_to_Struct(
    4.38 -                    &msg->choice.handshakeRequest.header.me, NULL);
    4.39 -            if (!partner)
    4.40 -                return PEP_OUT_OF_MEMORY;
    4.41 -            event = HandshakeRequest;
    4.42 -            break;
    4.43 -
    4.44 -        case DeviceGroup_Protocol_PR_groupKeys:
    4.45 -            partner = Identity_to_Struct(&msg->choice.groupKeys.header.me,
    4.46 -                    NULL);
    4.47 -            if (!partner)
    4.48 -                return PEP_OUT_OF_MEMORY;
    4.49 -            identity_list *group_keys = IdentityList_to_identity_list(
    4.50 -                    &msg->choice.groupKeys.ownIdentities, NULL);
    4.51 -            if (!group_keys) {
    4.52 -                free_identity(partner);
    4.53 -                return PEP_OUT_OF_MEMORY;
    4.54 -            }
    4.55 -            extra = (void *) group_keys;
    4.56 -            event = GroupKeys;
    4.57 -            break;
    4.58 -
    4.59 -        default:
    4.60 -            return PEP_SYNC_ILLEGAL_MESSAGE;
    4.61 -    }
    4.62 -
    4.63 -    return fsm_DeviceState_inject(session, event, partner, extra);
    4.64 -}
    4.65 -
    4.66 -PEP_STATUS receive_DeviceState_msg(PEP_SESSION session, message *src)
    4.67 -{
    4.68 -    assert(session && src);
    4.69 -    if (!(session && src))
    4.70 -        return PEP_ILLEGAL_VALUE;
    4.71 -
    4.72 -    bool found = false;
    4.73 -
    4.74 -    for (bloblist_t *bl = src->attachments; bl && bl->value; bl = bl->next) {
    4.75 -        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp") == 0
    4.76 -                && bl->size) {
    4.77 -            DeviceGroup_Protocol_t *msg;
    4.78 -            uper_decode_complete(NULL, &asn_DEF_DeviceGroup_Protocol,
    4.79 -                    (void **) &msg, bl->value, bl->size);
    4.80 -            if (msg) {
    4.81 -                found = true;
    4.82 -                PEP_STATUS status = session->inject_sync_msg(msg, session->sync_obj);
    4.83 -                ASN_STRUCT_FREE(asn_DEF_DeviceGroup_Protocol, msg);
    4.84 -                if (status != PEP_STATUS_OK)
    4.85 -                    return status;
    4.86 -            }
    4.87 -        }
    4.88 -    }
    4.89 -
    4.90 -    if (found) {
    4.91 -        for (stringpair_list_t *spl = src->opt_fields ; spl && spl->value ;
    4.92 -                spl = spl->next) {
    4.93 -            if (spl->value->key &&
    4.94 -                    strcasecmp(spl->value->key, "pEp-auto-consume") == 0) {
    4.95 -                if (spl->value->value &&
    4.96 -                        strcasecmp(spl->value->value, "yes") == 0)
    4.97 -                    return PEP_MESSAGE_CONSUMED;
    4.98 -            }
    4.99 -        }
   4.100 -    }
   4.101 -
   4.102 -    return PEP_STATUS_OK;
   4.103 -}
   4.104 -
   4.105 -PEP_STATUS broadcast_msg(PEP_SESSION session, Identity partner, char *payload,
   4.106 -        size_t size)
   4.107 -{
   4.108 -    PEP_STATUS status = PEP_STATUS_OK;
   4.109 -
   4.110 -    assert(session && partner && payload);
   4.111 -    if (!(session && partner && payload))
   4.112 -        return PEP_ILLEGAL_VALUE;
   4.113 -
   4.114 -    if (size == 0)
   4.115 -        return PEP_STATUS_OK;
   4.116 -
   4.117 -    stringlist_t *own_addresses = NULL;
   4.118 -    status = get_own_addresses(session, &own_addresses);
   4.119 -    if (status != PEP_STATUS_OK)
   4.120 -        return status;
   4.121 -
   4.122 -    Identity me = NULL;
   4.123 -    message *_message = NULL;
   4.124 -
   4.125 -    for (stringlist_t *_a = own_addresses; _a && _a->value; _a = _a->next) {
   4.126 -        me = new_identity(_a->value, NULL, PEP_OWN_USERID, NULL);
   4.127 -        if (!me) {
   4.128 -            status = PEP_OUT_OF_MEMORY;
   4.129 -            goto the_end;
   4.130 -        }
   4.131 -
   4.132 -        status = myself(session, me);
   4.133 -        if (status == PEP_OUT_OF_MEMORY)
   4.134 -            goto the_end;
   4.135 -        if (status != PEP_STATUS_OK)
   4.136 -            continue;
   4.137 -
   4.138 -        status = prepare_message(me, partner, payload, size, &_message);
   4.139 -        if (status != PEP_STATUS_OK)
   4.140 -            goto the_end;
   4.141 -        
   4.142 -        free_identity(me);
   4.143 -        me = NULL;
   4.144 -
   4.145 -        status = session->messageToSend(session->sync_obj, _message);
   4.146 -        if (status == PEP_OUT_OF_MEMORY)
   4.147 -            goto the_end;
   4.148 -        assert(status == PEP_STATUS_OK);
   4.149 -    }
   4.150 -
   4.151 -the_end:
   4.152 -    free_stringlist(own_addresses);
   4.153 -    free_identity(me);
   4.154 -    free_message(_message);
   4.155 -    return status;
   4.156 -}
   4.157 -
     5.1 --- a/src/sync_fsm.h	Sat Aug 20 18:04:13 2016 +0200
     5.2 +++ b/src/sync_fsm.h	Mon Aug 22 17:26:40 2016 +0200
     5.3 @@ -101,11 +101,6 @@
     5.4          void *extra
     5.5      );
     5.6  
     5.7 -// misc
     5.8 -
     5.9 -PEP_STATUS broadcast_msg(PEP_SESSION session, Identity partner, char
    5.10 -        *payload, size_t size);
    5.11 -
    5.12  #ifdef __cplusplus
    5.13  }
    5.14  #endif
     6.1 --- a/src/sync_send_actions.c	Sat Aug 20 18:04:13 2016 +0200
     6.2 +++ b/src/sync_send_actions.c	Mon Aug 22 17:26:40 2016 +0200
     6.3 @@ -8,6 +8,7 @@
     6.4  #include "baseprotocol.h"
     6.5  #include "map_asn1.h"
     6.6  #include "../asn.1/DeviceGroup-Protocol.h"
     6.7 +#include "sync_impl.h"
     6.8  #include "../asn.1/Beacon.h"
     6.9  #include "../asn.1/HandshakeRequest.h"
    6.10  #include "../asn.1/GroupKeys.h"
    6.11 @@ -34,7 +35,6 @@
    6.12      Beacon_t *msg = NULL;
    6.13      char *payload = NULL;
    6.14      message *_message = NULL;
    6.15 -    pEp_identity *me = NULL;
    6.16  
    6.17      assert(session);
    6.18      assert(!partner);
    6.19 @@ -71,14 +71,6 @@
    6.20  
    6.21      msg->header.state = (long) state;
    6.22  
    6.23 -    me = new_identity(NULL, NULL, NULL, NULL);
    6.24 -    if (!me)
    6.25 -        goto enomem;
    6.26 -    status = myself(session, me);
    6.27 -    if (status != PEP_STATUS_OK)
    6.28 -        goto error;
    6.29 -    if (Identity_from_Struct(me, &msg->header.me) == NULL)
    6.30 -        goto enomem;
    6.31  
    6.32      if (asn_check_constraints(&asn_DEF_Beacon, msg, NULL, NULL)) {
    6.33          status = PEP_CONTRAINTS_VIOLATED;
    6.34 @@ -92,15 +84,7 @@
    6.35          goto error;
    6.36      }
    6.37  
    6.38 -    status = prepare_message(me, partner, payload, size, &_message);
    6.39 -    if (status != PEP_STATUS_OK)
    6.40 -        goto error;
    6.41 -    payload = NULL;
    6.42 -
    6.43 -    free_identity(me);
    6.44 -    me = NULL;
    6.45 -
    6.46 -    status = session->messageToSend(session->sync_obj, _message);
    6.47 +    status = multicast_self_msg(session, msg);
    6.48  
    6.49      ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.50      free_identity(partner);
    6.51 @@ -112,7 +96,6 @@
    6.52      ASN_STRUCT_FREE(asn_DEF_Beacon, msg);
    6.53      free(payload);
    6.54      free_message(_message);
    6.55 -    free_identity(me);
    6.56      free_identity(partner);
    6.57      return status;
    6.58  }
    6.59 @@ -176,7 +159,7 @@
    6.60  
    6.61      msg->header.state = (long) state;
    6.62  
    6.63 -    me = new_identity(NULL, NULL, NULL, NULL);
    6.64 +    me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
    6.65      if (!me)
    6.66          goto enomem;
    6.67      status = myself(session, me);
    6.68 @@ -282,7 +265,7 @@
    6.69  
    6.70      msg->header.state = (long) state;
    6.71  
    6.72 -    me = new_identity(NULL, NULL, NULL, NULL);
    6.73 +    me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
    6.74      if (!me)
    6.75          goto enomem;
    6.76      status = myself(session, me);
     7.1 --- a/sync/devicegroup.fsm	Sat Aug 20 18:04:13 2016 +0200
     7.2 +++ b/sync/devicegroup.fsm	Mon Aug 22 17:26:40 2016 +0200
     7.3 @@ -9,6 +9,8 @@
     7.4  protocol DeviceGroup {
     7.5      // all messages have a timestamp, time out and are removed after timeout
     7.6  
     7.7 +    broadcast sendBeacon;
     7.8 +
     7.9      fsm DeviceState filename=sync {
    7.10          condition storedGroupKeys();
    7.11          condition keyElectionWon(Identity partner);
    7.12 @@ -23,7 +25,7 @@
    7.13  
    7.14          state Sole {
    7.15              on KeyGen // injected by generate_keypair()
    7.16 -                do sendBeacon;
    7.17 +                do sendBeacon broadcast=1;
    7.18              on CannotDecrypt
    7.19                  do sendBeacon;  // cry, baby
    7.20              on Beacon(Identity partner) // this event will not happen for already
     8.1 --- a/sync/gen_actions_skeleton.ysl2	Sat Aug 20 18:04:13 2016 +0200
     8.2 +++ b/sync/gen_actions_skeleton.ysl2	Mon Aug 22 17:26:40 2016 +0200
     8.3 @@ -28,6 +28,7 @@
     8.4          #include "baseprotocol.h"
     8.5          #include "map_asn1.h"
     8.6          #include "../asn.1/DeviceGroup-Protocol.h"
     8.7 +        #include "sync_impl.h"
     8.8          ||
     8.9          for "func:distinctName(//action)"
    8.10              if "substring(@name, 1, 4) = 'send'"
    8.11 @@ -158,7 +159,7 @@
    8.12              «$name»_t *msg = NULL;
    8.13              char *payload = NULL;
    8.14              message *_message = NULL;
    8.15 -            pEp_identity *me = NULL;
    8.16 +            `` if "count(/protocol/broadcast/*[name()=$action/@name]) = 0" |> pEp_identity *me = NULL;
    8.17              `` if "$name='GroupKeys'" |> identity_list *kl = NULL;
    8.18  
    8.19              `` call "paramcheck" with "partner", "parm/partner";
    8.20 @@ -193,7 +194,10 @@
    8.21  
    8.22              msg->header.state = (long) state;
    8.23  
    8.24 -            me = new_identity(NULL, NULL, NULL, NULL);
    8.25 +        ||
    8.26 +        if "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
    8.27 +        ||
    8.28 +            me = new_identity(partner->address, NULL, PEP_OWN_USERID, NULL);
    8.29              if (!me)
    8.30                  goto enomem;
    8.31              status = myself(session, me);
    8.32 @@ -224,6 +228,12 @@
    8.33                  goto error;
    8.34              }
    8.35  
    8.36 +        ||
    8.37 +        choose {
    8.38 +            when "count(/protocol/broadcast/*[name()=$action/@name]) > 0"
    8.39 +                |> status = multicast_self_msg(session, msg);
    8.40 +            otherwise
    8.41 +        ||
    8.42              status = prepare_message(me, partner, payload, size, &_message);
    8.43              if (status != PEP_STATUS_OK)
    8.44                  goto error;
    8.45 @@ -233,6 +243,9 @@
    8.46              me = NULL;
    8.47  
    8.48              status = session->messageToSend(session->«$filename»_obj, _message);
    8.49 +        ||
    8.50 +        }
    8.51 +        ||
    8.52  
    8.53              ASN_STRUCT_FREE(asn_DEF_«$name», msg);
    8.54              free_identity(partner);
    8.55 @@ -245,7 +258,10 @@
    8.56              ASN_STRUCT_FREE(asn_DEF_«$name», msg);
    8.57              free(payload);
    8.58              free_message(_message);
    8.59 -            free_identity(me);
    8.60 +        ||
    8.61 +        if "count(/protocol/broadcast/*[name()=$action/@name]) = 0"
    8.62 +            |> free_identity(me);
    8.63 +        ||
    8.64              free_identity(partner);
    8.65              `` if "$name='GroupKeys'" |> free_identity_list(kl);
    8.66              return status;
     9.1 --- a/sync/gen_statemachine.ysl2	Sat Aug 20 18:04:13 2016 +0200
     9.2 +++ b/sync/gen_statemachine.ysl2	Mon Aug 22 17:26:40 2016 +0200
     9.3 @@ -115,11 +115,6 @@
     9.4                  void *extra
     9.5              );
     9.6  
     9.7 -        // misc
     9.8 -
     9.9 -        PEP_STATUS broadcast_msg(PEP_SESSION session, Identity partner, char
    9.10 -                *payload, size_t size);
    9.11 -
    9.12          #ifdef __cplusplus
    9.13          }
    9.14          #endif
    10.1 --- a/test/get_own_addresses_test.cc	Sat Aug 20 18:04:13 2016 +0200
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,38 +0,0 @@
    10.4 -#include <iostream>
    10.5 -#include <string>
    10.6 -#include <assert.h>
    10.7 -#include "pEpEngine_test.h"
    10.8 -#include "pEpEngine.h"
    10.9 -
   10.10 -using namespace std;
   10.11 -
   10.12 -int main() {
   10.13 -    cout << "\n*** get_own_addresses_test ***\n\n";
   10.14 -
   10.15 -    PEP_SESSION session;
   10.16 -    
   10.17 -    cout << "calling init()\n";
   10.18 -    PEP_STATUS status1 = init(&session);   
   10.19 -    ASSERT_STATUS(status1);
   10.20 -    assert(session);
   10.21 -    cout << "init() completed.\n";
   10.22 -
   10.23 -    // get_own_addresses test code
   10.24 -
   10.25 -    cout << "calling get_own_addresses()\n";
   10.26 -    stringlist_t *addresses = NULL;
   10.27 -    PEP_STATUS status2 = get_own_addresses(session, &addresses);
   10.28 -    ASSERT_STATUS(status2);
   10.29 -    assert(addresses);
   10.30 -    cout << "success.\n";
   10.31 -
   10.32 -    cout << "addresses received:\n";
   10.33 -    for (stringlist_t *_a = addresses; _a && _a->value; _a = _a->next)
   10.34 -        cout << _a->value << "\n";
   10.35 -    cout << ".\n";
   10.36 -
   10.37 -    cout << "calling release()\n";
   10.38 -    release(session);
   10.39 -    return 0;
   10.40 -}
   10.41 -