From 109f39e09630409a30a9f4e8183f539c499f07ba Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Mon, 3 Jun 2024 04:06:21 +0200 Subject: improved (v)conn init / open / init vlink / open vlink API; implemented randezvous hashing --- ecp/server/dir.c | 39 +++++++------ ecp/server/dir.h | 2 +- ecp/server/server.c | 25 ++++----- ecp/server/vlink.c | 2 +- ecp/src/ecp/core.c | 105 +++++++++++++++++++---------------- ecp/src/ecp/core.h | 21 ++++--- ecp/src/ecp/cr.h | 1 + ecp/src/ecp/crypto/crypto.c | 20 +++++-- ecp/src/ecp/crypto/crypto.h | 1 + ecp/src/ecp/dir/dir.c | 4 +- ecp/src/ecp/dir/dir.h | 2 +- ecp/src/ecp/dir/dir_client.c | 91 ++++++++++++++++++++++++++---- ecp/src/ecp/dir/dir_client.h | 8 ++- ecp/src/ecp/vconn/vconn.c | 67 ++++++++++++++++------ ecp/src/ecp/vconn/vconn.h | 6 +- ecp/test/Makefile | 12 ++-- ecp/test/basic.c | 8 +-- ecp/test/client.c | 15 +++-- ecp/test/dir.c | 34 ++++++++---- ecp/test/init.c | 2 +- ecp/test/init.h | 2 +- ecp/test/init_vconn.c | 2 +- ecp/test/init_vconn.h | 2 +- ecp/test/vc_client.c | 27 ++++----- ecp/test/vc_common.c | 128 ------------------------------------------- ecp/test/vc_common.h | 2 - ecp/test/vc_inb.c | 124 ----------------------------------------- ecp/test/vc_outb.c | 128 ------------------------------------------- ecp/test/vc_server.c | 38 ++++++------- ecp/test/vconn_inb.c | 110 +++++++++++++++++++++++++++++++++++++ ecp/test/vconn_outb.c | 117 +++++++++++++++++++++++++++++++++++++++ ecp/test/vcs.c | 21 +++---- 32 files changed, 567 insertions(+), 599 deletions(-) delete mode 100644 ecp/test/vc_common.c delete mode 100644 ecp/test/vc_common.h delete mode 100644 ecp/test/vc_inb.c delete mode 100644 ecp/test/vc_outb.c create mode 100644 ecp/test/vconn_inb.c create mode 100644 ecp/test/vconn_outb.c diff --git a/ecp/server/dir.c b/ecp/server/dir.c index b941050..8064d0c 100644 --- a/ecp/server/dir.c +++ b/ecp/server/dir.c @@ -29,7 +29,9 @@ static int dir_process_enable; static SRVConfig *srv_config; -#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) +/* other directory servers */ +#define _dir_item_is_dir(dir_item) (((dir_item)->roles & ECP_ROLE_DIR) && (memcmp(&(dir_item)->node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) != 0)) +#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) /* messages from clients */ ssize_t dir_send_online(ECPConnection *conn, uint8_t region) { @@ -414,14 +416,19 @@ void dir_process_item(ECPDirItem *dir_item, ECPSocket *sock, ecp_ecdh_public_t * if (rv) pthread_rwlock_unlock(&dir_shadow_rwlock); } if (rv) { - if (node) { - dir_destroy_node(node); - node = NULL; - } - LOG(LOG_ERR, "dir_process_item: err:%d\n", rv); + if (node) dir_destroy_node(node); + /* ignore duplicate hashtable inserts */ + if (rv != ECP_ERR_DUP) LOG(LOG_ERR, "dir_process_item: create node err:%d\n", rv); + return; } + /* try to open connection to other directory servers */ + if (_dir_item_is_dir(dir_item)) { + rv = dir_open_conn(node, sock); + if (rv) LOG(LOG_ERR, "dir_process_item: conn open err:%d\n", rv); + } + LOG(LOG_DEBUG, "dir_process_item: new node\n"); } @@ -496,7 +503,7 @@ int dir_open_conn(DIRNode *node, ECPSocket *sock) { ecp_conn_init(node->conn, sock, CTYPE_DIR); ecp_conn_set_flags(node->conn, ECP_CONN_FLAG_VBOX); - rv = ecp_conn_open(node->conn, &node->dir_item.node); + rv = ecp_conn_open(node->conn, &node->dir_item.node.key_perma.public, &node->dir_item.node.addr); if (rv) { free(node->conn); node->conn = NULL; @@ -522,16 +529,7 @@ int dir_create_node(ECPDirItem *dir_item, ECPSocket *sock, DIRNode **node) { _node->dir_item = *dir_item; _node->is_new = 1; - - /* open connection to other directory servers */ - if ((dir_item->roles & ECP_ROLE_DIR) && (memcmp(&dir_item->node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) != 0)) { - rv = dir_open_conn(_node, sock); - if (rv) { - pthread_mutex_destroy(&_node->mutex); - free(_node); - return rv; - } - } + _node->conn = NULL; *node = _node; return ECP_OK; @@ -802,7 +800,8 @@ void dir_announce(ECPSocket *sock, int ann_period) { do { node = ecp_ht_itr_value(&itr); - if ((node->dir_item.roles & ECP_ROLE_DIR) && (memcmp(&node->dir_item.node.key_perma.public, &srv_config->key_perma.public, sizeof(srv_config->key_perma.public)) != 0)) { + /* announce to other directory servers */ + if (_dir_item_is_dir(&node->dir_item)) { if (announce_cnt < MAX_NODE_ANNOUNCE) { announce_node[announce_cnt] = node; announce_cnt++; @@ -922,7 +921,7 @@ void dir_init_switch(ECPSocket *sock, int init_ann) { sleep(init_ann); } -int dir_init_ann(ECPSocket *sock, ECPNode *node) { +int dir_init_ann(ECPSocket *sock, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { ECPConnection *conn; int i, is_open; int rv; @@ -933,7 +932,7 @@ int dir_init_ann(ECPSocket *sock, ECPNode *node) { ecp_conn_init(conn, sock, CTYPE_DIR); ecp_conn_set_flags(conn, ECP_CONN_FLAG_VBOX); - rv = ecp_conn_open(conn, node); + rv = ecp_conn_open(conn, public, addr); if (rv) { free(conn); LOG(LOG_ERR, "dir_init_ann: conn open err:%d\n", rv); diff --git a/ecp/server/dir.h b/ecp/server/dir.h index dbcbc2e..e5eba75 100644 --- a/ecp/server/dir.h +++ b/ecp/server/dir.h @@ -73,5 +73,5 @@ int dir_init_dir_cnt(unsigned int dir_cnt, uint16_t serial); void dir_announce(ECPSocket *sock, int ann_period); int dir_start_announce(ECPSocket *sock); void dir_init_switch(ECPSocket *sock, int init_ann); -int dir_init_ann(ECPSocket *sock, ECPNode *node); +int dir_init_ann(ECPSocket *sock, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr); int dir_init(ECPSocket *sock); diff --git a/ecp/server/server.c b/ecp/server/server.c index a7140a1..fd8b317 100644 --- a/ecp/server/server.c +++ b/ecp/server/server.c @@ -42,7 +42,7 @@ SRVConfig *srv_get_config(void) { } static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { - LOG(LOG_ERR, "handle_err: ctype:%d mtype:%d err:%d\n", conn->type, mtype, err); + LOG(LOG_ERR, "handle_err: ctype:0x%.2x mtype:0x%.2x err:%d\n", conn->type, mtype, err); } static ssize_t conn_auth(ECPSocket *sock, ECPConnection *parent, unsigned char ctype, ecp_ecdh_public_t *public, unsigned char *msg, size_t msg_size) { @@ -307,20 +307,19 @@ static void dir_result(ECPSocket *sock, ECPDirList *list, int rv) { int main(int argc, char *argv[]) { char *endptr; - unsigned int rcvr_num; char *init_switch; - ECPNode node; + ecp_ecdh_public_t remote_pub; + ecp_tr_addr_t remote_addr; ECPContext ctx; ECPSocket sock; ECPConnection dir_conn; ECPConnHandler vconn_handler; ECPConnHandler vlink_handler; ECPConnHandler dir_handler; - int _argi, _argc, fd; + int _argi, _argc, remote, fd; int rv; memset(&srv_config, 0, sizeof(srv_config)); - memset(&node, 0, sizeof(node)); if (argc < 1) fail("Bad argument count\n"); @@ -443,21 +442,19 @@ int main(int argc, char *argv[]) { if (rv) fail("ACL load failed\n"); } + remote = 0; if (_argc == 2) { - ecp_ecdh_public_t node_pub; - + remote = 1; fd = open(argv[_argi], O_RDONLY); if (fd < 0) fail("Unable to open public key file: %s\n", argv[_argi]); - rv = ecp_util_read_key(fd, &node_pub, NULL, NULL); + rv = ecp_util_read_key(fd, &remote_pub, NULL, NULL); close(fd); if (rv) fail("Bad public key file: %s\n", argv[_argi]); _argi++; _argc--; - ecp_node_init(&node, &node_pub, NULL); - - rv = ecp_node_set_addr(&node, argv[_argi]); + rv = ecp_addr_init(&remote_addr, argv[_argi]); if (rv) fail("Bad remote address: %s\n", argv[_argi]); _argi++; _argc--; @@ -512,12 +509,12 @@ int main(int argc, char *argv[]) { if (rv) fail("start_receivers err:%d\n", rv); init_switch = getenv("ECP_INITSW"); - if (node.key_perma.valid) { + if (remote) { if (init_switch == NULL) { rv = ecp_dir_set_handler(&ctx, &dir_handler, dir_result); if (rv) fail("ecp_dir_set_handler err:%d\n", rv); - rv = ecp_dir_get(&dir_conn, &sock, &node, 0); + rv = ecp_dir_get(&dir_conn, &sock, &remote_pub, &remote_addr, 0); if (rv) fail("ecp_dir_get err:%d\n", rv); pthread_mutex_lock(&dir_sync_mutex); @@ -527,7 +524,7 @@ int main(int argc, char *argv[]) { pthread_mutex_unlock(&dir_sync_mutex); LOG(LOG_DEBUG, "ecp_dir_get: done\n"); } else { - rv = dir_init_ann(&sock, &node); + rv = dir_init_ann(&sock, &remote_pub, &remote_addr); if (rv) fail("dir_init_ann err:%d\n", rv); } } diff --git a/ecp/server/vlink.c b/ecp/server/vlink.c index 445734c..eb2484e 100644 --- a/ecp/server/vlink.c +++ b/ecp/server/vlink.c @@ -32,7 +32,7 @@ int vlink_open_conn(ECPSocket *sock, ECPNode *node, ECPConnection **_conn) { if (conn == NULL) return ECP_ERR_ALLOC; ecp_vlink_init(conn, sock); - rv = ecp_conn_open(conn, node); + rv = ecp_conn_open(conn, &node->key_perma.public, &node->addr); if (rv) { free(conn); return rv; diff --git a/ecp/src/ecp/core.c b/ecp/src/ecp/core.c index b962278..60a346b 100644 --- a/ecp/src/ecp/core.c +++ b/ecp/src/ecp/core.c @@ -25,6 +25,51 @@ int ecp_dhkey_gen(ECPDHKey *key) { return ECP_OK; } +void ecp_dhkey_init(ECPDHKey *key, ecp_ecdh_public_t *public, ecp_ecdh_private_t *private) { + memcpy(&key->public, public, sizeof(key->public)); + memcpy(&key->private, private, sizeof(key->private)); + key->valid = 1; +} + +void ecp_dhpub_init(ECPDHPub *key, ecp_ecdh_public_t *public) { + memcpy(&key->public, public, sizeof(key->public)); + key->valid = 1; +} + +int ecp_addr_init(ecp_tr_addr_t *addr, void *addr_s) { + int rv; + + memset(addr, 0, sizeof(ecp_tr_addr_t)); + rv = ecp_tr_addr_set(addr, addr_s); + return rv; +} + +void ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { + memset(node, 0, sizeof(ECPNode)); + + if (public) { + ECPDHPub *key = &node->key_perma; + + memcpy(&key->public, public, sizeof(key->public)); + key->valid = 1; + } + + if (addr) { + node->addr = *addr; + } +} + +void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public) { + ECPDHPub *key = &node->key_perma; + + memcpy(&key->public, public, sizeof(key->public)); + key->valid = 1; +} + +void ecp_node_set_addr(ECPNode *node, ecp_tr_addr_t *addr) { + node->addr = *addr; +} + int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err, ecp_logger_t logger) { int rv; @@ -70,43 +115,6 @@ ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype) { return NULL; } -int ecp_addr_init(ecp_tr_addr_t *addr, void *addr_s) { - int rv; - - memset(addr, 0, sizeof(ecp_tr_addr_t)); - rv = ecp_tr_addr_set(addr, addr_s); - return rv; -} - -void ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { - memset(node, 0, sizeof(ECPNode)); - - if (public) { - ECPDHPub *key = &node->key_perma; - - memcpy(&key->public, public, sizeof(key->public)); - key->valid = 1; - } - - if (addr) { - node->addr = *addr; - } -} - -void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public) { - ECPDHPub *key = &node->key_perma; - - memcpy(&key->public, public, sizeof(key->public)); - key->valid = 1; -} - -int ecp_node_set_addr(ECPNode *node, void *addr) { - int rv; - - rv = ecp_tr_addr_set(&node->addr, addr); - return rv; -} - static int conn_table_create(ECPConnTable *conn_table) { int rv; @@ -1189,8 +1197,8 @@ void *ecp_conn_get_param(ECPConnection *conn) { return r; } -void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key) { - conn->remote.key_perma = *key; +void ecp_conn_set_remote_key(ECPConnection *conn, ecp_ecdh_public_t *public) { + ecp_dhpub_init(&conn->remote.key_perma, public); } void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr) { @@ -1256,14 +1264,13 @@ int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned cha return rv; } -int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node) { +int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent) { ECPDHKey key; int rv; ecp_conn_set_outb(conn); conn->refcount = 1; - if (node) conn->remote = *node; rv = ecp_dhkey_gen(&key); if (rv) return rv; @@ -1456,11 +1463,11 @@ void ecp_conn_remove_gc(ECPConnection *conn) { } } -int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry) { +int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, int retry) { int rv; ssize_t _rv; - rv = ecp_conn_create_outb(conn, parent, node); + rv = ecp_conn_create_outb(conn, parent); if (rv) return rv; rv = ecp_conn_insert(conn, 0); @@ -1492,17 +1499,21 @@ int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, in return ECP_OK; } -int ecp_conn_open(ECPConnection *conn, ECPNode *node) { +int ecp_conn_open(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { int rv; - rv = _ecp_conn_open(conn, NULL, node, 1); + if (public) ecp_conn_set_remote_key(conn, public); + if (addr) ecp_conn_set_remote_addr(conn, addr); + rv = _ecp_conn_open(conn, NULL, 1); return rv; } -int ecp_conn_try_open(ECPConnection *conn, ECPNode *node) { +int ecp_conn_try_open(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr) { int rv; - rv = _ecp_conn_open(conn, NULL, node, 0); + if (public) ecp_conn_set_remote_key(conn, public); + if (addr) ecp_conn_set_remote_addr(conn, addr); + rv = _ecp_conn_open(conn, NULL, 0); return rv; } diff --git a/ecp/src/ecp/core.h b/ecp/src/ecp/core.h index e79eefc..5fa00a7 100644 --- a/ecp/src/ecp/core.h +++ b/ecp/src/ecp/core.h @@ -451,16 +451,15 @@ typedef struct ECPConnection { } ECPConnection; int ecp_dhkey_gen(ECPDHKey *key); - -int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err, ecp_logger_t logger); -int ecp_ctx_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler); -ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype); - int ecp_addr_init(ecp_tr_addr_t *addr, void *addr_s); void ecp_node_init(ECPNode *node, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr); void ecp_node_set_pub(ECPNode *node, ecp_ecdh_public_t *public); -int ecp_node_set_addr(ECPNode *node, void *addr); +void ecp_node_set_addr(ECPNode *node, ecp_tr_addr_t *addr); + +int ecp_ctx_init(ECPContext *ctx, ecp_conn_auth_t conn_auth, ecp_conn_new_t conn_new, ecp_conn_free_t conn_free, ecp_err_handler_t handle_err, ecp_logger_t logger); +int ecp_ctx_set_handler(ECPContext *ctx, unsigned char ctype, ECPConnHandler *handler); +ECPConnHandler *ecp_ctx_get_handler(ECPContext *ctx, unsigned char ctype); int ecp_sock_init(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); int ecp_sock_create(ECPSocket *sock, ECPContext *ctx, ECPDHKey *key); @@ -495,11 +494,11 @@ int ecp_conn_set_closed(ECPConnection *conn); void ecp_conn_set_rdy(ECPConnection *conn); void *ecp_conn_set_param(ECPConnection *conn, void *param); void *ecp_conn_get_param(ECPConnection *conn); -void ecp_conn_set_remote_key(ECPConnection *conn, ECPDHPub *key); +void ecp_conn_set_remote_key(ECPConnection *conn, ecp_ecdh_public_t *public); void ecp_conn_set_remote_addr(ECPConnection *conn, ecp_tr_addr_t *addr); int ecp_conn_create(ECPConnection *conn, ECPConnection *parent); int ecp_conn_create_inb(ECPConnection *conn, ECPConnection *parent, unsigned char s_idx, unsigned char c_idx, ecp_ecdh_public_t *public, ECPDHPub *rkey_perma, ecp_aead_key_t *shkey); -int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent, ECPNode *node); +int ecp_conn_create_outb(ECPConnection *conn, ECPConnection *parent); void ecp_conn_destroy(ECPConnection *conn); void ecp_conn_free(ECPConnection *conn); @@ -510,9 +509,9 @@ void ecp_conn_remove(ECPConnection *conn); void ecp_conn_remove_addr(ECPConnection *conn); void ecp_conn_remove_gc(ECPConnection *conn); -int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, ECPNode *node, int retry); -int ecp_conn_open(ECPConnection *conn, ECPNode *node); -int ecp_conn_try_open(ECPConnection *conn, ECPNode *node); +int _ecp_conn_open(ECPConnection *conn, ECPConnection *parent, int retry); +int ecp_conn_open(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr); +int ecp_conn_try_open(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr); void _ecp_conn_close(ECPConnection *conn); void ecp_conn_close(ECPConnection *conn); diff --git a/ecp/src/ecp/cr.h b/ecp/src/ecp/cr.h index 84f9bf1..0ac09bf 100644 --- a/ecp/src/ecp/cr.h +++ b/ecp/src/ecp/cr.h @@ -16,6 +16,7 @@ int ecp_ecdsa_sign(ecp_ecdsa_signature_t *sig, unsigned char *m, size_t ml, ecp_ int ecp_ecdsa_verify(unsigned char *m, size_t ml, ecp_ecdsa_signature_t *sig, ecp_ecdsa_public_t *p); void ecp_hmac(unsigned char *hd, ecp_hmac_key_t *k, unsigned char *m, size_t ml); +void ecp_hash(unsigned char *hd, unsigned char *m, size_t ml); int ecp_str2key(uint8_t *key, char *str); void ecp_key2str(char *str, uint8_t *key); diff --git a/ecp/src/ecp/crypto/crypto.c b/ecp/src/ecp/crypto/crypto.c index 1b801ab..95598da 100644 --- a/ecp/src/ecp/crypto/crypto.c +++ b/ecp/src/ecp/crypto/crypto.c @@ -133,8 +133,16 @@ void ecp_hmac(unsigned char *hd, ecp_hmac_key_t *k, unsigned char *m, size_t ml) SHA1_Final(hd, &ctx); } +void ecp_hash(unsigned char *hd, unsigned char *m, size_t ml) { + SHA_CTX ctx; + + SHA1_Init(&ctx); + SHA1_Update(&ctx, m, ml); + SHA1_Final(hd, &ctx); +} + int ecp_str2key(uint8_t *key, char *str) { - unsigned int u[8]; + uint32_t u[8]; int i, rv; if (str[ECP_SIZE_ECDH_KEY_BUF - 1] != '\0') return ECP_ERR; @@ -154,14 +162,14 @@ int ecp_str2key(uint8_t *key, char *str) { } void ecp_key2str(char *str, uint8_t *key) { - unsigned int u[8]; + uint32_t u[8]; int i; for (i=0; i<8; i++) { - u[i] = (unsigned int)key[0] << 24; - u[i] |= (unsigned int)key[1] << 16; - u[i] |= (unsigned int)key[2] << 8; - u[i] |= (unsigned int)key[3]; + u[i] = (uint32_t)key[0] << 24; + u[i] |= (uint32_t)key[1] << 16; + u[i] |= (uint32_t)key[2] << 8; + u[i] |= (uint32_t)key[3]; key += 4; } diff --git a/ecp/src/ecp/crypto/crypto.h b/ecp/src/ecp/crypto/crypto.h index 1fd63a2..1eed243 100644 --- a/ecp/src/ecp/crypto/crypto.h +++ b/ecp/src/ecp/crypto/crypto.h @@ -17,6 +17,7 @@ #define ECP_SIZE_HMAC_KEY 32 #define ECP_SIZE_HMAC_DIGEST SHA_DIGEST_LENGTH +#define ECP_SIZE_HASH_DIGEST SHA_DIGEST_LENGTH #define ECP_SIZE_ECDH_KEY_BUF 72 diff --git a/ecp/src/ecp/dir/dir.c b/ecp/src/ecp/dir/dir.c index 59764bb..b997f5c 100644 --- a/ecp/src/ecp/dir/dir.c +++ b/ecp/src/ecp/dir/dir.c @@ -134,10 +134,10 @@ void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock) { ecp_conn_init(conn, sock, ECP_CTYPE_DIR); } -int ecp_dir_request(ECPConnection *conn, ECPNode *node, unsigned char region) { +int ecp_dir_request(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr, unsigned char region) { int rv; ecp_conn_set_param(conn, (void *)region); - rv = ecp_conn_open(conn, node); + rv = ecp_conn_open(conn, public, addr); return rv; } diff --git a/ecp/src/ecp/dir/dir.h b/ecp/src/ecp/dir/dir.h index 5db33f1..e343ecc 100644 --- a/ecp/src/ecp/dir/dir.h +++ b/ecp/src/ecp/dir/dir.h @@ -24,4 +24,4 @@ int ecp_dir_handle_open(ECPConnection *conn, ECP2Buffer *b); ssize_t ecp_dir_send_req(ECPConnection *conn, unsigned char region); void ecp_dir_conn_init(ECPConnection *conn, ECPSocket *sock); -int ecp_dir_request(ECPConnection *conn, ECPNode *node, unsigned char region); +int ecp_dir_request(ECPConnection *conn, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr, unsigned char region); diff --git a/ecp/src/ecp/dir/dir_client.c b/ecp/src/ecp/dir/dir_client.c index defc202..7c34189 100644 --- a/ecp/src/ecp/dir/dir_client.c +++ b/ecp/src/ecp/dir/dir_client.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "dir_client.h" @@ -16,10 +17,19 @@ static int dir_list_create_items(ECPDirList *dir_list, uint16_t list_size) { return ECP_OK; } -static void dir_list_reset_items(ECPDirList *dir_list, uint16_t serial) { +static int dir_list_reset_items(ECPDirList *dir_list, uint16_t list_size, uint16_t serial) { + int rv; + dir_list->count = 0; dir_list->serial = serial; + if (dir_list->size != list_size) { + if (dir_list->items) free(dir_list->items); + rv = dir_list_create_items(dir_list, list_size); + if (rv) return rv; + } memset(dir_list->items, 0, dir_list->size * sizeof(ECPDirItem)); + + return ECP_OK; } static int dir_handle_open(ECPConnection *conn, ECP2Buffer *b) { @@ -28,7 +38,7 @@ static int dir_handle_open(ECPConnection *conn, ECP2Buffer *b) { rv = ecp_dir_handle_open(conn, b); if (rv) return rv; - conn->param = ecp_dir_list_create(); + conn->param = ecp_dir_list_create(0); if (conn->param == NULL) return ECP_ERR_ALLOC; return ECP_OK; @@ -65,11 +75,8 @@ static ssize_t dir_handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char if (rv) goto handle_dir_msg_fin; if (is_first) { - if (dir_list->items == NULL) { - rv = dir_list_create_items(dir_list, frag_tot * ECP_MAX_DIR_ITEM_IN_MSG); - if (rv) goto handle_dir_msg_fin; - } - dir_list_reset_items(dir_list, serial); + rv = dir_list_reset_items(dir_list, frag_tot * ECP_MAX_DIR_ITEM_IN_MSG, serial); + if (rv) goto handle_dir_msg_fin; } if ((dir_list->items == NULL) || @@ -112,13 +119,22 @@ static void dir_handle_err(ECPConnection *conn, unsigned char mtype, int err) { ecp_conn_close(conn); } -ECPDirList *ecp_dir_list_create(void) { +ECPDirList *ecp_dir_list_create(uint16_t list_size) { ECPDirList *dir_list; dir_list = malloc(sizeof(ECPDirList)); if (dir_list == NULL) return NULL; memset(dir_list, 0, sizeof(ECPDirList)); + if (list_size) { + int rv; + + rv = dir_list_create_items(dir_list, list_size); + if (rv) { + free(dir_list); + return NULL; + } + } return dir_list; } @@ -147,10 +163,65 @@ int ecp_dir_set_handler(ECPContext *ctx, ECPConnHandler *handler, ecp_dir_result return rv; } -int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ECPNode *node, unsigned char region) { +int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr, unsigned char region) { int rv; ecp_dir_conn_init(conn, sock); - rv = ecp_dir_request(conn, node, region); + rv = ecp_dir_request(conn, public, addr, region); return rv; } + +#ifdef ECP_WITH_VCONN + +ssize_t ecp_dir_hrw_select(ECPDirList *dir_list, ecp_ecdh_public_t *srv_pub, ecp_ecdh_public_t vconn_keys[], size_t vconn_size, struct ECPNetAddr *addr) { + unsigned char tmp_hash[ECP_SIZE_HASH_DIGEST]; + unsigned char hrw_hash[ECP_SIZE_HASH_DIGEST]; + ecp_ecdh_public_t public[2]; + uint16_t sel[ECP_MAX_PARENT]; + uint16_t hrw_i, sel_cnt; + uint32_t rnd_i; + int i, j, k; + + if ((dir_list->count == 0) || (vconn_size == 0)) return ECP_ERR_EMPTY; + if (vconn_size > ECP_MAX_PARENT) vconn_size = ECP_MAX_PARENT; + + memset(hrw_hash, 0, sizeof(hrw_hash)); + memcpy(&public[0], srv_pub, sizeof(public[0])); + + for (i=0; icount; i++) { + memcpy(&public[1], &dir_list->items[i].node.key_perma.public, sizeof(public[1])); + ecp_hash(tmp_hash, (unsigned char *)public, sizeof(public)); + if (memcmp(hrw_hash, tmp_hash, sizeof(hrw_hash)) < 0) { + hrw_i = i; + memcpy(hrw_hash, tmp_hash, sizeof(hrw_hash)); + } + } + sel[0] = hrw_i; + sel_cnt = 1; + + for (i=0; icount - sel_cnt) == 0) goto dir_hrw_select_fin; + rnd_i = arc4random_uniform(dir_list->count - sel_cnt); + for (j=0; j= sel[j]) { + rnd_i++; + } else { + for (k=sel_cnt; k>j; k--) { + sel[k] = sel[k-1]; + } + break; + } + } + sel[j] = rnd_i; + sel_cnt++; + memcpy(&vconn_keys[i], &dir_list->items[rnd_i].node.key_perma.public, sizeof(vconn_keys[i])); + if (i == 0) *addr = dir_list->items[rnd_i].node.addr; + } + +dir_hrw_select_fin: + memcpy(&vconn_keys[i], &dir_list->items[hrw_i].node.key_perma.public, sizeof(vconn_keys[i])); + if (i == 0) *addr = dir_list->items[hrw_i].node.addr; + return sel_cnt; +} + +#endif diff --git a/ecp/src/ecp/dir/dir_client.h b/ecp/src/ecp/dir/dir_client.h index d55b8f6..a1be56d 100644 --- a/ecp/src/ecp/dir/dir_client.h +++ b/ecp/src/ecp/dir/dir_client.h @@ -7,9 +7,13 @@ typedef struct ECPDirList { typedef void (*ecp_dir_result_t) (ECPSocket *sock, ECPDirList *dir_list, int err); -ECPDirList *ecp_dir_list_create(void); +ECPDirList *ecp_dir_list_create(uint16_t list_size); void ecp_dir_list_destroy(ECPDirList *dir_list); ECPDirList *ecp_dir_list_copy(ECPDirList *dir_list); int ecp_dir_set_handler(ECPContext *ctx, ECPConnHandler *handler, ecp_dir_result_t dir_result); -int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ECPNode *node, unsigned char region); \ No newline at end of file +int ecp_dir_get(ECPConnection *conn, ECPSocket *sock, ecp_ecdh_public_t *public, ecp_tr_addr_t *addr, unsigned char region); + +#ifdef ECP_WITH_VCONN +ssize_t ecp_dir_hrw_select(ECPDirList *dir_list, ecp_ecdh_public_t *srv_pub, ecp_ecdh_public_t vconn_keys[], size_t vconn_size, struct ECPNetAddr *addr); +#endif diff --git a/ecp/src/ecp/vconn/vconn.c b/ecp/src/ecp/vconn/vconn.c index e8d9b35..615474c 100644 --- a/ecp/src/ecp/vconn/vconn.c +++ b/ecp/src/ecp/vconn/vconn.c @@ -249,17 +249,31 @@ ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer return rv; } -void ecp_vconn_init(ECPVConnOutb vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock) { - ECPDHPub key; +void ecp_vconn_init(ECPVConnOutb vconn[], size_t vconn_size, ECPConnection *conn, ECPSocket *sock) { int i; - key.valid = 1; for (i=0; inext) { - _vconn = (ECPVConnOutb *)_vconn->next; + i = 0; + conn = &vconn->b; + while (conn->type == ECP_CTYPE_VCONN) { + ecp_conn_set_remote_key(conn, &vconn_pub[i]); + conn = ((ECPVConnOutb *)conn)->next; + i++; } + if (public) ecp_conn_set_remote_key(conn, public); + if (addr) ecp_conn_set_remote_addr(&vconn->b, addr); + + rv = _ecp_conn_open(&vconn->b, NULL, 1); + return rv; +} + +int ecp_vconn_open_vlink(ECPVConnOutb *vconn, ecp_ecdh_public_t vconn_pub[], ecp_tr_addr_t *addr) { + ECPConnection *conn; + int i, rv; - _vconn->next = conn; - if (node) { - ecp_conn_set_remote_key(conn, &node->key_perma); - ecp_conn_set_remote_addr(&vconn->b, &node->addr); + i = 0; + conn = &vconn->b; + while (conn->type == ECP_CTYPE_VCONN) { + ecp_conn_set_remote_key(conn, &vconn_pub[i]); + conn = ((ECPVConnOutb *)conn)->next; + i++; } + ecp_conn_set_remote_key(conn, &vconn_pub[i]); + if (addr) ecp_conn_set_remote_addr(&vconn->b, addr); - rv = _ecp_conn_open(&vconn->b, NULL, NULL, 1); + rv = _ecp_conn_open(&vconn->b, NULL, 1); return rv; } @@ -332,7 +363,7 @@ int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs) { /* we should release incoming packet before sending next open packet */ ecp_tr_release(bufs->packet, 1); - rv = _ecp_conn_open(vconn->next, conn, NULL, 1); + rv = _ecp_conn_open(vconn->next, conn, 1); /* err will be handled by ecp_vconn_handle_err */ return rv; diff --git a/ecp/src/ecp/vconn/vconn.h b/ecp/src/ecp/vconn/vconn.h index a0e076f..ab1d2d1 100644 --- a/ecp/src/ecp/vconn/vconn.h +++ b/ecp/src/ecp/vconn/vconn.h @@ -25,11 +25,13 @@ typedef struct ECPVConnOutb { ssize_t ecp_vconn_pack_parent(ECPConnection *conn, ECPBuffer *payload, ECPBuffer *packet, size_t pkt_size, ecp_tr_addr_t *addr); -void ecp_vconn_init(ECPVConnOutb vconn[], ecp_ecdh_public_t keys[], size_t vconn_size, ECPSocket *sock); +void ecp_vconn_init(ECPVConnOutb vconn[], size_t vconn_size, ECPConnection *conn, ECPSocket *sock); +void ecp_vconn_init_vlink(ECPVConnOutb vconn[], size_t vconn_size, ECPSocket *sock); #ifdef ECP_WITH_HTABLE void ecp_vconn_init_inb(ECPVConnInb *vconn, ECPSocket *sock); #endif -int ecp_vconn_open(ECPVConnOutb *vconn, ECPConnection *conn, ECPNode *node); +int ecp_vconn_open(ECPVConnOutb *vconn, ecp_ecdh_public_t vconn_pub[], ecp_tr_addr_t *addr, ecp_ecdh_public_t *public); +int ecp_vconn_open_vlink(ECPVConnOutb *vconn, ecp_ecdh_public_t vconn_pub[], ecp_tr_addr_t *addr); void ecp_vconn_close(ECPConnection *conn); int ecp_vconn_handle_open(ECPConnection *conn, ECP2Buffer *bufs); #ifdef ECP_WITH_HTABLE diff --git a/ecp/test/Makefile b/ecp/test/Makefile index 3fc85d1..44ac16a 100644 --- a/ecp/test/Makefile +++ b/ecp/test/Makefile @@ -4,8 +4,10 @@ CFLAGS += -I../util dep = ../build-posix/*.a ../util/libecputil.a target = basic client server dir +ifeq ($(with_vconn),yes) ifeq ($(with_htable),yes) -target += vcs vc_server vc_client vc_inb vc_outb +target += vcs vc_server vc_client vconn_inb vconn_outb +endif endif @@ -26,11 +28,11 @@ server: server.o init.o $(dep) dir: dir.o $(dep) $(CC) -o $@ $< init.o $(dep) $(LDFLAGS) -vc_inb: vc_inb.o vc_common.o init_vconn.o $(dep) - $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) +vconn_inb: vconn_inb.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) -vc_outb: vc_outb.o vc_common.o init_vconn.o $(dep) - $(CC) -o $@ $< vc_common.o init_vconn.o $(dep) $(LDFLAGS) +vconn_outb: vconn_outb.o init_vconn.o $(dep) + $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) vcs: vcs.o init_vconn.o $(dep) $(CC) -o $@ $< init_vconn.o $(dep) $(LDFLAGS) diff --git a/ecp/test/basic.c b/ecp/test/basic.c index 68cc604..06d9fb3 100644 --- a/ecp/test/basic.c +++ b/ecp/test/basic.c @@ -49,7 +49,6 @@ int main(int argc, char *argv[]) { ecp_tr_addr_t addr; ECPDHKey key_perma_c; ECPDHKey key_perma_s; - ECPNode node; int rv; /* server */ @@ -93,12 +92,11 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock_c); LOG("ecp_start_receiver", rv); - ecp_node_init(&node, &key_perma_s.public, NULL); - rv = ecp_node_set_addr(&node, "127.0.0.1:3000"); - LOG("ecp_node_set_addr", rv); + rv = ecp_addr_init(&addr, "127.0.0.1:3000"); + LOG("ecp_addr_init", rv); ecp_conn_init(&conn, &sock_c, CTYPE_TEST); - rv = ecp_conn_open(&conn, &node); + rv = ecp_conn_open(&conn, &key_perma_s.public, &addr); LOG("ecp_conn_open", rv); while (1) sleep(1); diff --git a/ecp/test/client.c b/ecp/test/client.c index eb39c92..de4fe9c 100644 --- a/ecp/test/client.c +++ b/ecp/test/client.c @@ -39,8 +39,8 @@ static void usage(char *arg) { int main(int argc, char *argv[]) { ECPDHKey key_perma; - ECPNode node; - ecp_ecdh_public_t node_pub; + ecp_tr_addr_t addr; + ecp_ecdh_public_t public; int rv; /* client */ @@ -64,15 +64,14 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); LOG("ecp_start_receiver", rv); - rv = ecp_util_load_key(argv[2], &node_pub, NULL); - LOG("ecp_util_load_key", rv); + rv = ecp_addr_init(&addr, argv[1]); + LOG("ecp_addr_init", rv); - ecp_node_init(&node, &node_pub, NULL); - rv = ecp_node_set_addr(&node, argv[1]); - LOG("ecp_node_set_addr", rv); + rv = ecp_util_load_key(argv[2], &public, NULL); + LOG("ecp_util_load_key", rv); ecp_conn_init(&conn, &sock, CTYPE_TEST); - rv = ecp_conn_open(&conn, &node); + rv = ecp_conn_open(&conn, &public, &addr); LOG("ecp_conn_open", rv); while (1) sleep(1); diff --git a/ecp/test/dir.c b/ecp/test/dir.c index 9c04432..f9a4543 100644 --- a/ecp/test/dir.c +++ b/ecp/test/dir.c @@ -14,24 +14,35 @@ ECPSocket sock; ECPConnection conn; ECPConnHandler dir_handler; -#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv) exit(1); } +#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv<0) exit(1); } static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { printf("ERROR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); } -static void print_list(ECPDirList *dir_list, int err) { +static void print_list(ECPSocket *sock, ECPDirList *dir_list, int err) { + int i; + unsigned char *host; + uint16_t port; + + printf("DIR LIST COUNT:%d\n", dir_list->count); + for (i=0; icount; i++) { + host = dir_list->items[i].node.addr.host; + port = dir_list->items[i].node.addr.port; + + printf("ADDR: %d.%d.%d.%d:%d\n", host[0], host[1], host[2], host[3], ntohs(port)); + } ecp_dir_list_destroy(dir_list); } static void usage(char *arg) { - fprintf(stderr, "Usage: %s
\n", arg); + fprintf(stderr, "Usage: %s
\n", arg); exit(1); } int main(int argc, char *argv[]) { - ECPNode node; - ecp_ecdh_public_t node_pub; + ecp_tr_addr_t addr; + ecp_ecdh_public_t public; int rv; if (argc != 3) usage(argv[0]); @@ -40,7 +51,7 @@ int main(int argc, char *argv[]) { LOG("ecp_ctx_init", rv); rv = ecp_dir_set_handler(&ctx, &dir_handler, print_list); - LOG("ecp_dir_ctx_init", rv); + LOG("ecp_dir_set_handler", rv); rv = ecp_sock_create(&sock, &ctx, NULL); LOG("ecp_sock_create", rv); @@ -51,14 +62,13 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); LOG("ecp_start_receiver", rv); - rv = ecp_util_load_key(argv[2], &node_pub, NULL); - LOG("ecp_util_load_key", rv); + rv = ecp_addr_init(&addr, argv[1]); + LOG("ecp_addr_init", rv); - ecp_node_init(&node, &node_pub, NULL); - rv = ecp_node_set_addr(&node, argv[1]); - LOG("ecp_node_set_addr", rv); + rv = ecp_util_load_key(argv[2], &public, NULL); + LOG("ecp_util_load_key", rv); - rv = ecp_dir_get(&conn, &sock, &node, 0); + rv = ecp_dir_get(&conn, &sock, &public, &addr, 0); LOG("ecp_dir_get", rv); while(1) pause(); diff --git a/ecp/test/init.c b/ecp/test/init.c index 7004794..c56c28d 100644 --- a/ecp/test/init.c +++ b/ecp/test/init.c @@ -4,7 +4,7 @@ #include static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { - printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); + printf("ERROR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); } static ECPConnection *conn_new(ECPSocket *sock, ECPConnection *parent, unsigned char type) { diff --git a/ecp/test/init.h b/ecp/test/init.h index 7edba6e..efa8bcb 100644 --- a/ecp/test/init.h +++ b/ecp/test/init.h @@ -1,3 +1,3 @@ -#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv) exit(1); } +#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv<0) exit(1); } int ecp_init(ECPContext *ctx); \ No newline at end of file diff --git a/ecp/test/init_vconn.c b/ecp/test/init_vconn.c index 273ae5d..8421365 100644 --- a/ecp/test/init_vconn.c +++ b/ecp/test/init_vconn.c @@ -6,7 +6,7 @@ #include static void handle_err(ECPConnection *conn, unsigned char mtype, int err) { - printf("ERR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); + printf("ERROR: CTYPE:0x%x MTYPE:0x%x ERR:%d\n", conn->type, mtype, err); } static ECPConnection *conn_new(ECPSocket *sock, ECPConnection *parent, unsigned char type) { diff --git a/ecp/test/init_vconn.h b/ecp/test/init_vconn.h index 06f10e2..a2228dd 100644 --- a/ecp/test/init_vconn.h +++ b/ecp/test/init_vconn.h @@ -1,3 +1,3 @@ -#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv) exit(1); } +#define LOG(fmt, rv) { printf(fmt " RV:%d\n", rv); if (rv<0) exit(1); } int ecp_init(ECPContext *ctx, ECPConnHandler *vconn_handler, ECPConnHandler *vlink_handler); \ No newline at end of file diff --git a/ecp/test/vc_client.c b/ecp/test/vc_client.c index f46824b..a32b22a 100644 --- a/ecp/test/vc_client.c +++ b/ecp/test/vc_client.c @@ -16,7 +16,7 @@ ECPConnHandler handler; ECPConnHandler vconn_handler; ECPConnHandler vlink_handler; ECPConnection conn; -ECPVConnOutb vconn[3]; +ECPVConnOutb vconn[ECP_MAX_PARENT]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -25,7 +25,9 @@ static int handle_open(ECPConnection *conn, ECP2Buffer *b) { char *_msg = "PERA JE CAR!"; ssize_t rv; - printf("OPEN\n"); + printf("Press ENTER key to start\n"); + while(getchar()!='\n'); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); return ECP_OK; @@ -44,12 +46,12 @@ static void usage(char *arg) { int main(int argc, char *argv[]) { ECPDHKey key_perma; - ECPNode node; - ecp_ecdh_public_t node_pub; - ecp_ecdh_public_t vconn_pub[3]; + ecp_tr_addr_t addr; + ecp_ecdh_public_t public; + ecp_ecdh_public_t vconn_pub[ECP_MAX_PARENT]; int rv, i; - if ((argc < 4) || (argc > 6)) usage(argv[0]); + if ((argc < 4) || (argc > 3 + ECP_MAX_PARENT)) usage(argv[0]); rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); LOG("ecp_init", rv); @@ -64,7 +66,7 @@ int main(int argc, char *argv[]) { LOG("ecp_sock_create", rv); rv = ecp_vconn_sock_create(&sock); - LOG("ecp_vconn_htable_init", rv); + LOG("ecp_vconn_sock_create", rv); rv = ecp_sock_open(&sock, NULL); LOG("ecp_sock_open", rv); @@ -72,12 +74,11 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); LOG("ecp_start_receiver", rv); - rv = ecp_util_load_key(argv[1], &node_pub, NULL); + rv = ecp_util_load_key(argv[1], &public, NULL); LOG("ecp_util_load_key", rv); - ecp_node_init(&node, &node_pub, NULL); - rv = ecp_node_set_addr(&node, argv[2]); - LOG("ecp_node_set_addr", rv); + rv = ecp_addr_init(&addr, argv[2]); + LOG("ecp_addr_init", rv); for (i=3; i -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "vc_common.h" - -#define VCONN_NODES 3 - -#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) -#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) - -static void msg_remove_item(unsigned char *msg, uint16_t i, uint16_t c) { - if (c - i - 1) memmove(msg + i * ECP_SIZE_DIR_ITEM, msg + (i + 1) * ECP_SIZE_DIR_ITEM, (c - i - 1) * ECP_SIZE_DIR_ITEM); -} - -static void hrw_select(unsigned char *msg, uint16_t count, ecp_ecdh_public_t *pub, ecp_ecdh_public_t *hrw_pub, ecp_ecdh_public_t vconn_pub[], size_t *_vconn_size, ecp_tr_addr_t *addr) { - unsigned char *_msg = msg; - unsigned char hash[SHA_DIGEST_LENGTH]; - unsigned char hrw_hash[SHA_DIGEST_LENGTH]; - ecp_ecdh_public_t hash_pub[2]; - size_t vconn_size = *_vconn_size; - ECPDirItem dir_item; - SHA_CTX ctx; - int i, hrw_i; - - if (count == 0) return; - - memset(hrw_hash, 0, sizeof(hrw_hash)); - memcpy(&hash_pub[0], pub, sizeof(ecp_ecdh_public_t)); - - for (i=0; isock); - ecp_node_init(&node, &hrw_pub, &addr); - rv = ecp_vconn_open(vconn, vlink, &node); - printf("ecp_vconn_open RV:%d\n", rv); - - return ECP_OK; -} - -int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub) { - ecp_ecdh_public_t vconn_pub[VCONN_NODES]; - ecp_ecdh_public_t hrw_pub; - ecp_tr_addr_t addr; - ECPNode node; - ECPVConnOutb *vconn; - size_t vconn_size; - int rv; - - vconn_size = VCONN_NODES - 1; - hrw_select(msg, count, remote_pub, &hrw_pub, vconn_pub, &vconn_size, &addr); - memcpy(&vconn_pub[vconn_size], &hrw_pub, sizeof(ecp_ecdh_public_t)); - vconn_size++; - - vconn = malloc(sizeof(ECPVConnOutb) * vconn_size); - if (vconn == NULL) return ECP_ERR_ALLOC; - - ecp_vconn_init(vconn, vconn_pub, vconn_size, conn->sock); - ecp_node_init(&node, remote_pub, &addr); - rv = ecp_vconn_open(vconn, conn, &node); - printf("ecp_vconn_open RV:%d\n", rv); - - return ECP_OK; -} diff --git a/ecp/test/vc_common.h b/ecp/test/vc_common.h deleted file mode 100644 index a684566..0000000 --- a/ecp/test/vc_common.h +++ /dev/null @@ -1,2 +0,0 @@ -int vc_open_inb(ECPConnection *vlink, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *local_pub); -int vc_open_outb(ECPConnection *conn, unsigned char *msg, uint16_t count, ecp_ecdh_public_t *remote_pub); diff --git a/ecp/test/vc_inb.c b/ecp/test/vc_inb.c deleted file mode 100644 index 0c75c5d..0000000 --- a/ecp/test/vc_inb.c +++ /dev/null @@ -1,124 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "init_vconn.h" -#include "vc_common.h" - -ECPContext ctx; -ECPSocket sock; -ECPConnHandler handler; -ECPConnHandler dir_handler; -ECPConnHandler vconn_handler; -ECPConnHandler vlink_handler; - -#define CTYPE_TEST 0 -#define MTYPE_MSG 0 - -static int handle_open(ECPConnection *conn, ECP2Buffer *b) { - printf("OPEN\n"); - - return ECP_OK; -} - -static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - char *_msg = "VAISTINU JE CAR!"; - ssize_t rv; - - printf("MSG:%s size:%ld\n", msg, msg_size); - rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); - - return msg_size; -} - -static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - size_t rsize; - uint16_t count; - ECPConnection *vlink; - int rv; - - if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; - - count = \ - ((uint16_t)msg[0] << 8) | \ - ((uint16_t)msg[1]); - - printf("DIR MSG:%d\n", count); - - rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; - if (msg_size < rsize) return ECP_ERR_SIZE; - - msg += sizeof(uint16_t); - - vlink = malloc(sizeof(ECPConnection)); - if (vlink == NULL) return ECP_ERR_ALLOC; - - ecp_vlink_init(vlink, conn->sock); - rv = vc_open_inb(vlink, msg, count, &conn->sock->key_perma.public); - if (rv) { - free(vlink); - return rv; - } - - return rsize; -} - -static void usage(char *arg) { - fprintf(stderr, "Usage: %s \n", arg); - exit(1); -} - -int main(int argc, char *argv[]) { - ECPConnection *conn_dir; - ECPNode node_dir; - ecp_ecdh_public_t dir_pub; - ECPDHKey key_perma; - int rv; - - if (argc != 4) usage(argv[0]); - - rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); - printf("ecp_init RV:%d\n", rv); - - ecp_conn_handler_init(&dir_handler, ecp_dir_handle_open, NULL, handle_dir_msg, NULL); - ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); - - ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); - ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); - - rv = ecp_util_load_key(argv[1], &key_perma.public, &key_perma.private); - printf("ecp_util_load_key RV:%d\n", rv); - key_perma.valid = 1; - - rv = ecp_sock_create(&sock, &ctx, &key_perma); - printf("ecp_sock_create RV:%d\n", rv); - - rv = ecp_sock_open(&sock, NULL); - printf("ecp_sock_open RV:%d\n", rv); - - rv = ecp_start_receiver(&sock); - printf("ecp_start_receiver RV:%d\n", rv); - - rv = ecp_util_load_key(argv[2], &dir_pub, NULL); - printf("ecp_util_load_key RV:%d\n", rv); - - ecp_node_init(&node_dir, &dir_pub, NULL); - rv = ecp_node_set_addr(&node_dir, argv[3]); - printf("ecp_node_set_addr RV:%d\n", rv); - - conn_dir = malloc(sizeof(ECPConnection)); - if (conn_dir == NULL) printf("out of memory\n"); - - ecp_dir_conn_init(conn_dir, &sock); - rv = ecp_conn_open(conn_dir, &node_dir); - printf("ecp_conn_open RV:%d\n", rv); - - while (1) sleep(1); -} diff --git a/ecp/test/vc_outb.c b/ecp/test/vc_outb.c deleted file mode 100644 index 4b14244..0000000 --- a/ecp/test/vc_outb.c +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include - -#include - -#include "init_vconn.h" -#include "vc_common.h" - -ECPContext ctx; -ECPSocket sock; -ECPConnHandler handler; -ECPConnHandler dir_handler; -ECPConnHandler vconn_handler; -ECPConnHandler vlink_handler; -ecp_ecdh_public_t remote_pub; - -#define CTYPE_TEST 0 -#define MTYPE_MSG 0 - -static int handle_open(ECPConnection *conn, ECP2Buffer *b) { - char *_msg = "PERA JE CAR!"; - ssize_t rv; - - printf("OPEN\n"); - rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); - - return ECP_OK; -} - -static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - printf("MSG:%s size:%ld\n", msg, msg_size); - - return msg_size; -} - -static ssize_t handle_dir_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { - size_t rsize; - uint16_t count; - ECPConnection *_conn; - int rv; - - if (msg_size < sizeof(uint16_t)) return ECP_ERR_SIZE; - - count = \ - ((uint16_t)msg[0] << 8) | \ - ((uint16_t)msg[1]); - - printf("DIR MSG:%d\n", count); - - rsize = sizeof(uint16_t) + count * ECP_SIZE_DIR_ITEM; - if (msg_size < rsize) return ECP_ERR_SIZE; - - msg += sizeof(uint16_t); - - _conn = malloc(sizeof(ECPConnection)); - if (_conn == NULL) return ECP_ERR_ALLOC; - - ecp_conn_init(_conn, conn->sock, CTYPE_TEST); - rv = vc_open_outb(_conn, msg, count, &remote_pub); - if (rv) { - printf("vc_open_outb RV:%d\n", rv); - free(_conn); - return rv; - } - - return rsize; -} - -static void usage(char *arg) { - fprintf(stderr, "Usage: %s \n", arg); - exit(1); -} - -int main(int argc, char *argv[]) { - ECPConnection *conn_dir; - ECPNode node_dir; - ecp_ecdh_public_t dir_pub; - ECPDHKey key_perma; - int rv; - - if (argc != 4) usage(argv[0]); - - rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); - printf("ecp_init RV:%d\n", rv); - - ecp_conn_handler_init(&dir_handler, ecp_dir_handle_open, NULL, handle_dir_msg, NULL); - ecp_ctx_set_handler(&ctx, ECP_CTYPE_DIR, &dir_handler); - - ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); - ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); - - rv = ecp_dhkey_gen(&key_perma); - printf("ecp_dhkey_gen RV:%d\n", rv); - - rv = ecp_sock_create(&sock, &ctx, &key_perma); - printf("ecp_sock_create RV:%d\n", rv); - - rv = ecp_sock_open(&sock, NULL); - printf("ecp_sock_open RV:%d\n", rv); - - rv = ecp_start_receiver(&sock); - printf("ecp_start_receiver RV:%d\n", rv); - - rv = ecp_util_load_key(argv[1], &remote_pub, NULL); - printf("ecp_util_load_key RV:%d\n", rv); - - rv = ecp_util_load_key(argv[2], &dir_pub, NULL); - printf("ecp_util_load_key RV:%d\n", rv); - - ecp_node_init(&node_dir, &dir_pub, NULL); - rv = ecp_node_set_addr(&node_dir, argv[3]); - printf("ecp_node_set_addr RV:%d\n", rv); - - conn_dir = malloc(sizeof(ECPConnection)); - if (conn_dir == NULL) printf("out of memory\n"); - - ecp_dir_conn_init(conn_dir, &sock); - rv = ecp_conn_open(conn_dir, &node_dir); - printf("ecp_conn_open RV:%d\n", rv); - - while (1) sleep(1); -} diff --git a/ecp/test/vc_server.c b/ecp/test/vc_server.c index a1a1c51..5157c84 100644 --- a/ecp/test/vc_server.c +++ b/ecp/test/vc_server.c @@ -16,7 +16,7 @@ ECPConnHandler handler; ECPConnHandler vconn_handler; ECPConnHandler vlink_handler; ECPConnection conn; -ECPVConnOutb vconn[3]; +ECPVConnOutb vconn[ECP_MAX_PARENT]; #define CTYPE_TEST 0 #define MTYPE_MSG 0 @@ -44,11 +44,11 @@ static void usage(char *arg) { int main(int argc, char *argv[]) { ECPDHKey key_perma; - ECPNode node; - ecp_ecdh_public_t node_pub; + ecp_tr_addr_t addr; + ecp_ecdh_public_t vconn_pub[ECP_MAX_PARENT]; int rv, i; - if ((argc < 4) || (argc > 7)) usage(argv[0]); + if ((argc < 4) || (argc > 3 + ECP_MAX_PARENT)) usage(argv[0]); rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); LOG("ecp_init", rv); @@ -64,7 +64,7 @@ int main(int argc, char *argv[]) { LOG("ecp_sock_create", rv); rv = ecp_vconn_sock_create(&sock); - LOG("ecp_vconn_htable_init", rv); + LOG("ecp_vconn_sock_create", rv); rv = ecp_sock_open(&sock, NULL); LOG("ecp_sock_open", rv); @@ -72,27 +72,21 @@ int main(int argc, char *argv[]) { rv = ecp_start_receiver(&sock); LOG("ecp_start_receiver", rv); - rv = ecp_util_load_key(argv[argc-1], &node_pub, NULL); - LOG("ecp_util_load_key", rv); + rv = ecp_addr_init(&addr, argv[2]); + LOG("ecp_addr_init", rv); - ecp_node_init(&node, &node_pub, NULL); - rv = ecp_node_set_addr(&node, argv[2]); - LOG("ecp_node_set_addr", rv); + for (i=3; i 4) { - ecp_ecdh_public_t vconn_pub[3]; - - for (i=3; i +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "init_vconn.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; +ECPConnHandler dir_handler; +ECPConnection dir_conn; +ECPVConnOutb vconn[ECP_MAX_PARENT]; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + printf("OPEN\n"); + + return ECP_OK; +} + +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + char *_msg = "VAISTINU JE CAR!"; + ssize_t rv; + + printf("MSG:%s size:%ld\n", msg, msg_size); + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + return msg_size; +} + +static void handle_dir_list(ECPSocket *sock, ECPDirList *dir_list, int err) { + ecp_tr_addr_t addr; + ecp_ecdh_public_t vconn_pub[ECP_MAX_PARENT]; + ssize_t _rv, vconn_size; + int rv; + + LOG("handle_dir_list", err); + + _rv = ecp_dir_hrw_select(dir_list, &sock->key_perma.public, vconn_pub, ECP_MAX_PARENT, &addr); + LOG("ecp_dir_hrw_select", (int)_rv); + + vconn_size = _rv; + ecp_vconn_init_vlink(vconn, vconn_size, sock); + rv = ecp_vconn_open_vlink(vconn, vconn_pub, &addr); + LOG("ecp_vconn_open_vlink", rv); + + ecp_dir_list_destroy(dir_list); +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s
\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPDHKey key_perma; + ecp_tr_addr_t addr; + ecp_ecdh_public_t public; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + LOG("ecp_init", rv); + + rv = ecp_dir_set_handler(&ctx, &dir_handler, handle_dir_list); + LOG("ecp_dir_set_handler", rv); + + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_util_load_key(argv[1], &key_perma.public, &key_perma.private); + LOG("ecp_util_load_key", rv); + key_perma.valid = 1; + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + LOG("ecp_sock_create", rv); + + rv = ecp_vconn_sock_create(&sock); + LOG("ecp_vconn_sock_create", rv); + + rv = ecp_sock_open(&sock, NULL); + LOG("ecp_sock_open", rv); + + rv = ecp_start_receiver(&sock); + LOG("ecp_start_receiver", rv); + + rv = ecp_addr_init(&addr, argv[2]); + LOG("ecp_addr_init", rv); + + rv = ecp_util_load_key(argv[3], &public, NULL); + LOG("ecp_util_load_key", rv); + + rv = ecp_dir_get(&dir_conn, &sock, &public, &addr, 0); + LOG("ecp_dir_get", rv); + + while(1) pause(); +} diff --git a/ecp/test/vconn_outb.c b/ecp/test/vconn_outb.c new file mode 100644 index 0000000..8b7782b --- /dev/null +++ b/ecp/test/vconn_outb.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include + +#include +#include +#include +#include + +#include + +#include "init_vconn.h" + +ECPContext ctx; +ECPSocket sock; +ECPConnHandler handler; +ECPConnHandler vconn_handler; +ECPConnHandler vlink_handler; +ECPConnHandler dir_handler; +ECPConnection dir_conn; +ECPConnection conn; +ECPVConnOutb vconn[ECP_MAX_PARENT]; +ecp_ecdh_public_t srv_public; + +#define CTYPE_TEST 0 +#define MTYPE_MSG 0 + +static int handle_open(ECPConnection *conn, ECP2Buffer *b) { + char *_msg = "PERA JE CAR!"; + ssize_t rv; + + printf("Press ENTER key to start\n"); + while(getchar()!='\n'); + + rv = ecp_msg_send(conn, MTYPE_MSG, (unsigned char *)_msg, strlen(_msg)+1); + + return ECP_OK; +} + +static ssize_t handle_msg(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, size_t msg_size, ECP2Buffer *b) { + printf("MSG:%s size:%ld\n", msg, msg_size); + + return msg_size; +} + +static void handle_dir_list(ECPSocket *sock, ECPDirList *dir_list, int err) { + ecp_tr_addr_t addr; + ecp_ecdh_public_t vconn_pub[ECP_MAX_PARENT]; + ssize_t _rv, vconn_size; + int rv; + + LOG("handle_dir_list", err); + + _rv = ecp_dir_hrw_select(dir_list, &srv_public, vconn_pub, ECP_MAX_PARENT, &addr); + LOG("ecp_dir_hrw_select", (int)_rv); + + vconn_size = _rv; + ecp_conn_init(&conn, sock, CTYPE_TEST); + ecp_vconn_init(vconn, vconn_size, &conn, sock); + rv = ecp_vconn_open(vconn, vconn_pub, &addr, &srv_public); + LOG("ecp_vconn_open", rv); + + ecp_dir_list_destroy(dir_list); +} + +static void usage(char *arg) { + fprintf(stderr, "Usage: %s
\n", arg); + exit(1); +} + +int main(int argc, char *argv[]) { + ECPDHKey key_perma; + ecp_tr_addr_t addr; + ecp_ecdh_public_t public; + int rv; + + if (argc != 4) usage(argv[0]); + + rv = ecp_init(&ctx, &vconn_handler, &vlink_handler); + LOG("ecp_init", rv); + + rv = ecp_dir_set_handler(&ctx, &dir_handler, handle_dir_list); + LOG("ecp_dir_set_handler", rv); + + ecp_conn_handler_init(&handler, handle_open, NULL, handle_msg, NULL); + ecp_ctx_set_handler(&ctx, CTYPE_TEST, &handler); + + rv = ecp_dhkey_gen(&key_perma); + LOG("ecp_dhkey_gen", rv); + + rv = ecp_sock_create(&sock, &ctx, &key_perma); + LOG("ecp_sock_create", rv); + + rv = ecp_vconn_sock_create(&sock); + LOG("ecp_vconn_sock_create", rv); + + rv = ecp_sock_open(&sock, NULL); + LOG("ecp_sock_open", rv); + + rv = ecp_start_receiver(&sock); + LOG("ecp_start_receiver", rv); + + rv = ecp_util_load_key(argv[1], &srv_public, NULL); + LOG("ecp_util_load_key", rv); + + rv = ecp_addr_init(&addr, argv[2]); + LOG("ecp_addr_init", rv); + + rv = ecp_util_load_key(argv[3], &public, NULL); + LOG("ecp_util_load_key", rv); + + rv = ecp_dir_get(&dir_conn, &sock, &public, &addr, 0); + LOG("ecp_dir_get", rv); + + while (1) sleep(1); +} diff --git a/ecp/test/vcs.c b/ecp/test/vcs.c index 1260f2b..48bab41 100644 --- a/ecp/test/vcs.c +++ b/ecp/test/vcs.c @@ -26,16 +26,13 @@ static int vlink_handle_open(ECPConnection *_conn, ECP2Buffer *bufs) { /* open return connection */ if ((_conn->parent == NULL) && ecp_conn_is_inb(_conn) && (memcmp(&_conn->remote.key_perma.public, &conn.remote.key_perma.public, sizeof(conn.remote.key_perma.public)) != 0)) { - ECPNode node; ECPConnection *conn_r; - ecp_node_init(&node, &_conn->remote.key_perma.public, &_conn->remote.addr); - conn_r = malloc(sizeof(ECPConnection)); if (conn_r == NULL) return ECP_ERR_ALLOC; ecp_vlink_init(conn_r, _conn->sock); - rv = ecp_conn_open(conn_r, &node); + rv = ecp_conn_open(conn_r, &_conn->remote.key_perma.public, &_conn->remote.addr); printf("open return connection: ecp_conn_open RV:%d\n", rv); if (rv) return rv; } @@ -63,7 +60,7 @@ int main(int argc, char *argv[]) { LOG("ecp_sock_create", rv); rv = ecp_vconn_sock_create(&sock); - LOG("ecp_vconn_htable_init", rv); + LOG("ecp_vconn_sock_create", rv); rv = ecp_addr_init(&addr, argv[1]); LOG("ecp_addr_init", rv); @@ -75,18 +72,16 @@ int main(int argc, char *argv[]) { LOG("ecp_start_receiver", rv); if (argc == 5) { - ECPNode node; - ecp_ecdh_public_t node_pub; + ecp_ecdh_public_t public; - rv = ecp_util_load_key(argv[4], &node_pub, NULL); - LOG("ecp_util_load_key", rv); + rv = ecp_addr_init(&addr, argv[3]); + LOG("ecp_addr_init", rv); - ecp_node_init(&node, &node_pub, NULL); - rv = ecp_node_set_addr(&node, argv[3]); - LOG("ecp_node_set_addr", rv); + rv = ecp_util_load_key(argv[4], &public, NULL); + LOG("ecp_util_load_key", rv); ecp_vlink_init(&conn, &sock); - rv = ecp_conn_open(&conn, &node); + rv = ecp_conn_open(&conn, &public, &addr); LOG("ecp_conn_open", rv); } else { memset(&conn, 0, sizeof(conn)); -- cgit v1.2.3