From 72520c7339f04849633114d9dca644e32f85e9fb Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Thu, 3 Aug 2017 05:40:23 +0200 Subject: implemented mtype return mask; fixed nasty ctype bug in conn create --- code/core/core.c | 47 ++++++++++++++++++++++++++------------------ code/core/core.h | 10 ++++++++++ code/core/htable/hashtable.c | 31 +++++++++++++++++++---------- code/core/htable/hashtable.h | 7 +++---- code/core/htable/htable.c | 19 ++---------------- code/core/timer.c | 4 ++-- 6 files changed, 66 insertions(+), 52 deletions(-) (limited to 'code/core') diff --git a/code/core/core.c b/code/core/core.c index ae654c9..544f896 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -485,7 +485,7 @@ static ssize_t _conn_send_kget(ECPConnection *conn, ECPTimerItem *ti) { unsigned char payload[ECP_SIZE_PLD(0)]; ecp_seq_t seq; - ecp_pld_set_type(payload, ECP_MTYPE_KGET); + ecp_pld_set_type(payload, ECP_MTYPE_KGET_REQ); return ecp_pld_send_wkey(conn, &seq, ECP_ECDH_IDX_PERMA, ECP_ECDH_IDX_INV, payload, sizeof(payload)); } @@ -512,7 +512,7 @@ int ecp_conn_open(ECPConnection *conn, ECPNode *node) { int rv = ecp_conn_init(conn, node); if (rv) return rv; - ssize_t _rv = ecp_timer_send(conn, _conn_send_kget, ECP_MTYPE_KGET, 3, 500); + ssize_t _rv = ecp_timer_send(conn, _conn_send_kget, ECP_MTYPE_KGET_REP, 3, 500); if (_rv < 0) { ecp_conn_unregister(conn); return _rv; @@ -578,14 +578,14 @@ static ssize_t _conn_send_open(ECPConnection *conn, ECPTimerItem *ti) { unsigned char payload[ECP_SIZE_PLD(1)]; unsigned char *buf = ecp_pld_get_buf(payload); - ecp_pld_set_type(payload, ECP_MTYPE_OPEN); + ecp_pld_set_type(payload, ECP_MTYPE_OPEN_REQ); buf[0] = conn->type; return ecp_pld_send(conn, payload, sizeof(payload)); } ssize_t ecp_conn_send_open(ECPConnection *conn) { - return ecp_timer_send(conn, _conn_send_open, ECP_MTYPE_OPEN, 3, 500); + return ecp_timer_send(conn, _conn_send_open, ECP_MTYPE_OPEN_REP, 3, 500); } int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *proxy, unsigned char s_idx, unsigned char c_idx, unsigned char *c_public, ecp_aead_key_t *shsec, unsigned char *payload, size_t payload_size) { @@ -597,10 +597,11 @@ int ecp_conn_handle_new(ECPSocket *sock, ECPConnection **_conn, ECPConnection *p if (payload_size < 1) return ECP_ERR; + ctype = payload[0]; + conn = sock->ctx->conn_alloc ? sock->ctx->conn_alloc(ctype) : NULL; if (conn == NULL) return ECP_ERR_ALLOC; - ctype = payload[0]; rv = ecp_conn_create(conn, sock, ctype); if (rv) { if (sock->ctx->conn_free) sock->ctx->conn_free(conn); @@ -664,17 +665,19 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m pthread_mutex_unlock(&conn->mutex); #endif - if (conn->out) { + if (mtype & ECP_MTYPE_FLAG_REP) { + if (!conn->out) return ECP_ERR; return 0; } else { unsigned char payload[ECP_SIZE_PLD(0)]; unsigned char ctype = 0; + if (conn->out) return ECP_ERR; if (size < 1) return ECP_ERR; ctype = msg[0]; - ecp_pld_set_type(payload, ECP_MTYPE_OPEN); + ecp_pld_set_type(payload, ECP_MTYPE_OPEN_REP); ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); return 1; @@ -683,8 +686,11 @@ ssize_t ecp_conn_handle_open(ECPConnection *conn, ecp_seq_t seq, unsigned char m } ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { - if (conn->out) { + if (mtype & ECP_MTYPE_FLAG_REP) { ECPContext *ctx = conn->sock->ctx; + + if (!conn->out) return ECP_ERR; + #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); #endif @@ -713,8 +719,9 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m } else { unsigned char payload[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1)]; unsigned char *buf = ecp_pld_get_buf(payload); - ecp_pld_set_type(payload, ECP_MTYPE_KGET); + ecp_pld_set_type(payload, ECP_MTYPE_KGET_REP); + if (conn->out) return ECP_ERR; if (size < 0) return size; int rv = ecp_conn_dhkey_get_curr(conn, buf, buf+1); @@ -730,17 +737,19 @@ ssize_t ecp_conn_handle_kget(ECPConnection *conn, ecp_seq_t seq, unsigned char m ssize_t ecp_conn_handle_kput(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype, unsigned char *msg, ssize_t size) { if (size < 0) return size; - if (conn->out) { + if (mtype & ECP_MTYPE_FLAG_REP) { + if (!conn->out) return ECP_ERR; return 0; } else { unsigned char payload[ECP_SIZE_PLD(0)]; + if (conn->out) return ECP_ERR; if (size < ECP_ECDH_SIZE_KEY+1) return ECP_ERR; int rv = ecp_conn_dhkey_new_pub(conn, msg[0], msg+1); if (rv) return rv; - ecp_pld_set_type(payload, ECP_MTYPE_KPUT); + ecp_pld_set_type(payload, ECP_MTYPE_KPUT_REP); ssize_t _rv = ecp_pld_send(conn, payload, sizeof(payload)); return ECP_ECDH_SIZE_KEY+1; @@ -758,7 +767,7 @@ static ssize_t _conn_send_kput(ECPConnection *conn, ECPTimerItem *ti) { unsigned char *buf = ecp_pld_get_buf(payload); int rv = ECP_OK; - ecp_pld_set_type(payload, ECP_MTYPE_KPUT); + ecp_pld_set_type(payload, ECP_MTYPE_KPUT_REQ); rv = ecp_conn_dhkey_get_curr(conn, buf, buf+1); if (rv) return rv; @@ -786,7 +795,7 @@ int ecp_conn_dhkey_new(ECPConnection *conn) { if (rv) return rv; - _rv = ecp_timer_send(conn, _conn_send_kput, ECP_MTYPE_KPUT, 3, 500); + _rv = ecp_timer_send(conn, _conn_send_kput, ECP_MTYPE_KPUT_REP, 3, 500); if (_rv < 0) return _rv; return ECP_OK; @@ -1027,16 +1036,16 @@ ssize_t ecp_pkt_handle(ECPSocket *sock, ECPNetAddr *addr, ECPConnection *proxy, memcpy(nonce, packet+ECP_SIZE_PKT_HDR, sizeof(nonce)); if (conn == NULL) { - if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_OPEN) { + if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_OPEN_REQ) { rv = sock->conn_new(sock, &conn, proxy, s_idx, c_idx, packet+ECP_SIZE_PROTO+1, &shsec, payload+ECP_SIZE_MSG_HDR, pld_size-ECP_SIZE_MSG_HDR); if (rv) return rv; seq_bitmap = 0; n_seq = p_seq; - } else if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_KGET) { + } else if (payload[ECP_SIZE_PLD_HDR] == ECP_MTYPE_KGET_REQ) { unsigned char payload_[ECP_SIZE_PLD(ECP_ECDH_SIZE_KEY+1)]; unsigned char *buf = ecp_pld_get_buf(payload_); - ecp_pld_set_type(payload_, ECP_MTYPE_KGET); + ecp_pld_set_type(payload_, ECP_MTYPE_KGET_REP); rv = ecp_sock_dhkey_get_curr(sock, buf, buf+1); if (!rv) { @@ -1165,11 +1174,11 @@ ssize_t ecp_msg_handle(ECPConnection *conn, ecp_seq_t seq, unsigned char *msg, s msg++; rem_size--; - if (mtype >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; + if ((mtype & ECP_MTYPE_MASK) >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; if (rem_size < ECP_MIN_MSG) return ECP_ERR_MIN_MSG; - if (conn->out) ecp_timer_pop(conn, mtype); - handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype] : NULL; + ecp_timer_pop(conn, mtype); + handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype & ECP_MTYPE_MASK] : NULL; if (handler) { rv = handler(conn, seq, mtype, msg, rem_size); if (rv < 0) return rv; diff --git a/code/core/core.h b/code/core/core.h index 46f0d70..9cf3cf2 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -53,11 +53,21 @@ #define ECP_ECDH_IDX_INV 0xFF #define ECP_ECDH_IDX_PERMA 0x0F +#define ECP_MTYPE_MASK 0x7f +#define ECP_MTYPE_FLAG_REP 0x80 + #define ECP_MTYPE_OPEN 0x00 #define ECP_MTYPE_KGET 0x01 #define ECP_MTYPE_KPUT 0x02 #define ECP_MTYPE_EXEC 0x03 +#define ECP_MTYPE_OPEN_REQ (ECP_MTYPE_OPEN) +#define ECP_MTYPE_OPEN_REP (ECP_MTYPE_OPEN | ECP_MTYPE_FLAG_REP) +#define ECP_MTYPE_KGET_REQ (ECP_MTYPE_KGET) +#define ECP_MTYPE_KGET_REP (ECP_MTYPE_KGET | ECP_MTYPE_FLAG_REP) +#define ECP_MTYPE_KPUT_REQ (ECP_MTYPE_KPUT) +#define ECP_MTYPE_KPUT_REP (ECP_MTYPE_KPUT | ECP_MTYPE_FLAG_REP) + #define ECP_SIZE_PLD(X) ((X) < ECP_MIN_MSG ? ECP_MIN_MSG + ECP_SIZE_MSG_HDR : (X) + ECP_SIZE_MSG_HDR) #define ECP_SIZE_PKT(X) ((X) < ECP_MIN_MSG ? ECP_MIN_MSG + ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_HDR+ECP_AEAD_SIZE_TAG : (X) + ECP_SIZE_PKT_HDR+ECP_SIZE_MSG_HDR+ECP_AEAD_SIZE_TAG) diff --git a/code/core/htable/hashtable.c b/code/core/htable/hashtable.c index 8174a5d..de15827 100755 --- a/code/core/htable/hashtable.c +++ b/code/core/htable/hashtable.c @@ -24,34 +24,44 @@ const unsigned int prime_table_length = sizeof(primes)/sizeof(primes[0]); const float max_load_factor = 0.65; /*****************************************************************************/ -int -create_hashtable(struct hashtable *h, unsigned int minsize, +struct hashtable * +create_hashtable(unsigned int minsize, unsigned int (*hash_fn) (void*), int (*eq_fn) (void*,void*), void *(*malloc_fn) (size_t), void *(*realloc_fn) (void*,size_t), void (*free_fn) (void*)) { + struct hashtable *h = NULL; unsigned int pindex, size = primes[0]; + /* Check requested hashtable isn't too large */ - if (minsize > (1u << 30)) return 0; + if (minsize > (1u << 30)) return NULL; + + malloc_fn = malloc_fn ? malloc_fn : malloc; + realloc_fn = realloc_fn ? realloc_fn : realloc; + free_fn = free_fn ? free_fn : free; + h = malloc_fn(sizeof(struct hashtable)); + if (NULL == h) return NULL; + memset(h, 0, sizeof(struct hashtable)); + /* Enforce size as prime */ for (pindex=0; pindex < prime_table_length; pindex++) { if (primes[pindex] > minsize) { size = primes[pindex]; break; } } - h->fn_malloc = malloc_fn ? malloc_fn : malloc; - h->fn_realloc = realloc_fn ? realloc_fn : realloc; - h->fn_free = free_fn ? free_fn : free; + h->fn_malloc = malloc_fn; + h->fn_realloc = realloc_fn; + h->fn_free = free_fn; h->tablelength = size; h->primeindex = pindex; h->entrycount = 0; h->fn_hash = hash_fn; h->fn_eq = eq_fn; h->loadlimit = (unsigned int) ceil(size * max_load_factor); - h->table = h->fn_malloc(sizeof(struct entry *) * size); - if (NULL == h->table) return 0; /*oom*/ + h->table = malloc_fn(sizeof(struct entry *) * size); + if (NULL == h->table) { free_fn(h); return NULL; } /*oom*/ memset(h->table, 0, size * sizeof(struct entry *)); - return -1; + return h; } /*****************************************************************************/ @@ -190,7 +200,7 @@ hashtable_remove(struct hashtable *h, void *k) unsigned int hashvalue, index; hashvalue = hashtable_hash(h,k); - index = indexFor(h->tablelength,hashtable_hash(h,k)); + index = indexFor(h->tablelength,hashvalue); pE = &(h->table[index]); e = *pE; while (NULL != e) @@ -227,6 +237,7 @@ hashtable_destroy(struct hashtable *h) { f = e; e = e->next; if (h->fn_free_k) h->fn_free_k(f->k); if (h->fn_free_v) h->fn_free_v(f->v); h->fn_free(f); } } h->fn_free(h->table); + h->fn_free(h); } /* diff --git a/code/core/htable/hashtable.h b/code/core/htable/hashtable.h index d99f1c4..046f740 100755 --- a/code/core/htable/hashtable.h +++ b/code/core/htable/hashtable.h @@ -67,18 +67,17 @@ struct hashtable; * create_hashtable * @name create_hashtable - * @name h hashtable to create * @param minsize minimum initial size of hashtable * @param hash_fn function for hashing keys * @param eq_fn function for determining key equality * @param malloc_fn function malloc * @param realloc_fn function realloc * @param free_fn function free - * @return non-zero on success + * @return hashtable created */ -int -create_hashtable(struct hashtable *h, unsigned int minsize, +struct hashtable * +create_hashtable(unsigned int minsize, unsigned int (*hash_fn) (void*), int (*eq_fn) (void*,void*), void *(*malloc_fn) (size_t), diff --git a/code/core/htable/htable.c b/code/core/htable/htable.c index 4a9ee11..552a03b 100644 --- a/code/core/htable/htable.c +++ b/code/core/htable/htable.c @@ -3,33 +3,17 @@ #include #include "hashtable.h" -#include "hashtable_private.h" - -static unsigned int hash_fn(void *k) { - return *((unsigned int *)k); -} - -static int eq_fn(void *k1, void *k2) { - return !memcmp(k1, k2, ECP_ECDH_SIZE_KEY); -} static void *h_create(ECPContext *ctx) { int rv; - struct hashtable *h = malloc(sizeof(struct hashtable)); + struct hashtable *h = create_hashtable(1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL); if (h == NULL) return NULL; - rv = create_hashtable(h, 1000, (unsigned int (*)(void *))ctx->cr.dh_pub_hash_fn, (int (*)(void *, void *))ctx->cr.dh_pub_hash_eq, NULL, NULL, NULL); - if (!rv) { - free(h); - return NULL; - } - return h; } static void h_destroy(void *h) { hashtable_destroy(h); - free(h); } static int h_insert(void *h, unsigned char *k, ECPConnection *v) { @@ -39,6 +23,7 @@ static int h_insert(void *h, unsigned char *k, ECPConnection *v) { } static ECPConnection *h_remove(void *h, unsigned char *k) { + printf("REMOVE!!!\n"); return hashtable_remove(h, k); } diff --git a/code/core/timer.c b/code/core/timer.c index 0d06ba4..7767e67 100644 --- a/code/core/timer.c +++ b/code/core/timer.c @@ -22,7 +22,7 @@ void ecp_timer_destroy(ECPTimer *timer) { int ecp_timer_item_init(ECPTimerItem *ti, ECPConnection *conn, unsigned char mtype, unsigned short cnt, unsigned int timeout) { unsigned int abstime = conn->sock->ctx->tm.abstime_ms(timeout); - if (mtype >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; + if ((mtype & ECP_MTYPE_MASK) >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; ti->conn = conn; ti->mtype = mtype; @@ -181,7 +181,7 @@ unsigned int ecp_timer_exe(ECPSocket *sock) { unsigned char *pld = to_exec[i].pld; unsigned char pld_size = to_exec[i].pld_size; ecp_timer_retry_t *retry = to_exec[i].retry; - ecp_conn_handler_msg_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype] : NULL; + ecp_conn_handler_msg_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype & ECP_MTYPE_MASK] : NULL; if (to_exec[i].cnt) { ssize_t _rv = 0; -- cgit v1.2.3