From e9ced8e60689c6f46ac4fc31013b88f4c3f4fa80 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Fri, 4 Aug 2017 16:33:04 +0200 Subject: hash table condition compile --- code/core/core.c | 61 +++++++++++++++++++++++++++++++++++++++++++------------- code/core/core.h | 28 ++++++++++++++++---------- 2 files changed, 64 insertions(+), 25 deletions(-) (limited to 'code/core') diff --git a/code/core/core.c b/code/core/core.c index 78387b3..a725952 100644 --- a/code/core/core.c +++ b/code/core/core.c @@ -38,14 +38,14 @@ int ecp_ctx_create(ECPContext *ctx) { rv = ecp_crypto_init(&ctx->cr); if (rv) return rv; -#ifdef ECP_WITH_HTABLE - rv = ecp_htable_init(&ctx->ht); - if (rv) return rv; -#endif rv = ecp_transport_init(&ctx->tr); if (rv) return rv; rv = ecp_time_init(&ctx->tm); if (rv) return rv; +#ifdef ECP_WITH_HTABLE + rv = ecp_htable_init(&ctx->ht); + if (rv) return rv; +#endif return ECP_OK; } @@ -58,15 +58,19 @@ static int ctable_create(ECPSockCTable *conn, ECPContext *ctx) { int rv = ECP_OK; memset(conn, 0, sizeof(ECPSockCTable)); +#ifdef ECP_WITH_HTABLE if (ctx->ht.init) { conn->htable = ctx->ht.create(ctx); if (conn->htable == NULL) return ECP_ERR_ALLOC; } - +#endif #ifdef ECP_WITH_PTHREAD rv = pthread_mutex_init(&conn->mutex, NULL); #endif +#ifdef ECP_WITH_HTABLE if (rv && ctx->ht.init) ctx->ht.destroy(conn->htable); +#endif + return rv; } @@ -74,14 +78,22 @@ static void ctable_destroy(ECPSockCTable *conn, ECPContext *ctx) { #ifdef ECP_WITH_PTHREAD pthread_mutex_destroy(&conn->mutex); #endif +#ifdef ECP_WITH_HTABLE if (ctx->ht.init) ctx->ht.destroy(conn->htable); +#endif } static int ctable_insert(ECPConnection *conn) { + int with_htable = 0; ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; - if (ctx->ht.init) { +#ifdef ECP_WITH_HTABLE + if (ctx->ht.init) with_htable = 1; +#endif + + if (with_htable) { +#ifdef ECP_WITH_HTABLE int i, rv = ECP_OK; if (conn->out) { for (i=0; iconn.size == ECP_MAX_SOCK_CONN) return ECP_ERR_MAX_SOCK_CONN; sock->conn.array[sock->conn.size] = conn; @@ -114,18 +127,23 @@ static int ctable_insert(ECPConnection *conn) { } static void ctable_remove(ECPConnection *conn) { - int i; + int i, with_htable = 0; ECPSocket *sock = conn->sock; ECPContext *ctx = sock->ctx; - if (ctx->ht.init) { - int i; +#ifdef ECP_WITH_HTABLE + if (ctx->ht.init) with_htable = 1; +#endif + + if (with_htable) { +#ifdef ECP_WITH_HTABLE if (conn->out) { for (i=0; ikey[i].valid) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[i].public)); } else { ECPDHRKeyBucket *remote = &conn->remote; for (i=0; ikey[i].idx != ECP_ECDH_IDX_INV) ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[i].public)); } +#endif } else { for (i=0; iconn.size; i++) { if (conn == sock->conn.array[i]) { @@ -139,11 +157,17 @@ static void ctable_remove(ECPConnection *conn) { } static ECPConnection *ctable_search(ECPSocket *sock, unsigned char c_idx, unsigned char *c_public, ECPNetAddr *addr) { + int i, with_htable = 0; ECPConnection *conn = NULL; - int i; - if (sock->ctx->ht.init) { +#ifdef ECP_WITH_HTABLE + if (sock->ctx->ht.init) with_htable = 1; +#endif + + if (with_htable) { +#ifdef ECP_WITH_HTABLE return sock->ctx->ht.search(sock->conn.htable, c_public); +#endif } else { if (c_public) { for (i=0; iconn.size; i++) { @@ -273,17 +297,22 @@ static int conn_dhkey_new_pair(ECPConnection *conn, ECPDHKey *key) { ECPContext *ctx = sock->ctx; conn->key_curr = conn->key_curr == ECP_ECDH_IDX_INV ? 0 : (conn->key_curr+1) % ECP_MAX_CONN_KEY; +#ifdef ECP_WITH_HTABLE if (ctx->ht.init && conn->out && ecp_conn_is_reg(conn) && conn->key[conn->key_curr].valid) { ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[conn->key_curr].public)); } +#endif + conn->key[conn->key_curr] = *key; conn->key_idx_map[conn->key_curr] = ECP_ECDH_IDX_INV; +#ifdef ECP_WITH_HTABLE if (ctx->ht.init && conn->out && ecp_conn_is_reg(conn)) { int rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&conn->key[conn->key_curr].public), conn); if (rv) return rv; } - +#endif + return ECP_OK; } @@ -317,21 +346,25 @@ static int conn_dhkey_new_pub_remote(ECPConnection *conn, unsigned char idx, uns if (idx >= ECP_MAX_SOCK_KEY) return ECP_ERR_ECDH_IDX; if ((remote->key_idx_map[idx] != ECP_ECDH_IDX_INV) && ctx->cr.dh_pub_eq(public, &remote->key[remote->key_idx_map[idx]].public)) return ECP_ERR_ECDH_KEY_DUP; +#ifdef ECP_WITH_HTABLE if (ctx->ht.init && !conn->out && ecp_conn_is_reg(conn) && (remote->key[new].idx != ECP_ECDH_IDX_INV)) { ctx->ht.remove(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[new].public)); } - +#endif + if (remote->key[new].idx != ECP_ECDH_IDX_INV) remote->key_idx_map[remote->key[new].idx] = ECP_ECDH_IDX_INV; remote->key_idx_map[idx] = new; ctx->cr.dh_pub_from_buf(&remote->key[new].public, public); remote->key[new].idx = idx; remote->key_curr = new; +#ifdef ECP_WITH_HTABLE if (ctx->ht.init && !conn->out && ecp_conn_is_reg(conn)) { int rv = ctx->ht.insert(sock->conn.htable, ctx->cr.dh_pub_get_buf(&remote->key[new].public), conn); if (rv) return rv; } - +#endif + for (i=0; ishared[i][new].valid = 0; } diff --git a/code/core/core.h b/code/core/core.h index cf4009f..81ec69c 100644 --- a/code/core/core.h +++ b/code/core/core.h @@ -137,15 +137,6 @@ typedef struct ECPCryptoIface { int (*dsa_verify) (unsigned char *m, size_t ml, unsigned char *sig, ecp_dsa_public_t *p); } ECPCryptoIface; -typedef struct ECPHTableIface { - int init; - void *(*create) (struct ECPContext *c); - void (*destroy) (void *t); - int (*insert) (void *t, unsigned char *k, struct ECPConnection *v); - struct ECPConnection *(*remove) (void *t, unsigned char *k); - struct ECPConnection *(*search) (void *t, unsigned char *k); -} ECPHTableIface; - typedef struct ECPTransportIface { int init; int (*open) (ECPNetSock *, void *addr); @@ -163,6 +154,17 @@ typedef struct ECPTimeIface { void (*sleep_ms) (unsigned int); } ECPTimeIface; +#ifdef ECP_WITH_HTABLE +typedef struct ECPHTableIface { + int init; + void *(*create) (struct ECPContext *c); + void (*destroy) (void *t); + int (*insert) (void *t, unsigned char *k, struct ECPConnection *v); + struct ECPConnection *(*remove) (void *t, unsigned char *k); + struct ECPConnection *(*search) (void *t, unsigned char *k); +} ECPHTableIface; +#endif + typedef struct ECPDHKey { ecp_dh_public_t public; ecp_dh_private_t private; @@ -211,9 +213,11 @@ typedef struct ECPContext { ecp_conn_alloc_t *conn_alloc; ecp_conn_free_t *conn_free; ECPCryptoIface cr; - ECPHTableIface ht; ECPTransportIface tr; ECPTimeIface tm; +#ifdef ECP_WITH_HTABLE + ECPHTableIface ht; +#endif ssize_t (*pack) (struct ECPConnection *conn, ECPNetAddr *addr, ecp_seq_t *seq, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, unsigned char *payload, size_t payload_size); ssize_t (*pack_raw) (struct ECPSocket *sock, struct ECPConnection *proxy, ECPNetAddr *addr, unsigned char *packet, size_t pkt_size, unsigned char s_idx, unsigned char c_idx, ecp_dh_public_t *public, ecp_aead_key_t *shsec, unsigned char *nonce, ecp_seq_t seq, unsigned char *payload, size_t payload_size); ECPConnHandler *handler[ECP_MAX_CTYPE]; @@ -266,9 +270,11 @@ typedef struct ECPConnection { int ecp_init(ECPContext *ctx); int ecp_crypto_init(ECPCryptoIface *t); -int ecp_htable_init(ECPHTableIface *h); int ecp_transport_init(ECPTransportIface *t); int ecp_time_init(ECPTimeIface *t); +#ifdef ECP_WITH_HTABLE +int ecp_htable_init(ECPHTableIface *h); +#endif int ecp_dhkey_generate(ECPContext *ctx, ECPDHKey *key); int ecp_node_init(ECPContext *ctx, ECPNode *node, ecp_dh_public_t *public, void *addr); -- cgit v1.2.3