From c22b252ca064a919963bf9c3b9d827196a492887 Mon Sep 17 00:00:00 2001 From: Shane Bryldt Date: Thu, 8 Dec 2016 05:57:59 +0000 Subject: [PATCH] FS-9775: Adjusted ks_dht_nodeid_t back to a structure as you cannot cast raw data to a fixed array --- libs/libks/src/dht/ks_dht.c | 36 ++++++++++++++++++++++++---- libs/libks/src/dht/ks_dht.h | 17 +++++++++---- libs/libks/src/dht/ks_dht_endpoint.c | 4 ++-- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/libs/libks/src/dht/ks_dht.c b/libs/libks/src/dht/ks_dht.c index e8c9046135..5cf4413cc2 100644 --- a/libs/libks/src/dht/ks_dht.c +++ b/libs/libks/src/dht/ks_dht.c @@ -34,6 +34,8 @@ KS_DECLARE(ks_status_t) ks_dht_alloc(ks_dht_t **dht, ks_pool_t *pool) d->recv_buffer_length = 0; d->transactionid_next = 0; d->transactions_hash = NULL; + d->rt_ipv4 = NULL; + d->rt_ipv6 = NULL; return KS_STATUS_SUCCESS; } @@ -65,6 +67,8 @@ KS_DECLARE(ks_status_t) ks_dht_prealloc(ks_dht_t *dht, ks_pool_t *pool) dht->recv_buffer_length = 0; dht->transactionid_next = 0; dht->transactions_hash = NULL; + dht->rt_ipv4 = NULL; + dht->rt_ipv6 = NULL; return KS_STATUS_SUCCESS; } @@ -124,6 +128,9 @@ KS_DECLARE(ks_status_t) ks_dht_init(ks_dht_t *dht) dht->transactionid_next = 1; //rand(); ks_hash_create(&dht->transactions_hash, KS_HASH_MODE_INT, KS_HASH_FLAG_RWLOCK, dht->pool); + + dht->rt_ipv4 = NULL; + dht->rt_ipv6 = NULL; return KS_STATUS_SUCCESS; } @@ -135,6 +142,14 @@ KS_DECLARE(ks_status_t) ks_dht_deinit(ks_dht_t *dht) { ks_assert(dht); + if (dht->rt_ipv4) { + ks_dhtrt_deinitroute(dht->rt_ipv4); + dht->rt_ipv4 = NULL; + } + if (dht->rt_ipv6) { + ks_dhtrt_deinitroute(dht->rt_ipv6); + dht->rt_ipv6 = NULL; + } dht->transactionid_next = 0; if (dht->transactions_hash) { ks_hash_destroy(&dht->transactions_hash); @@ -311,6 +326,17 @@ KS_DECLARE(ks_status_t) ks_dht_bind(ks_dht_t *dht, const ks_dht_nodeid_t *nodeid dht->endpoints_poll[epindex].fd = ep->sock; dht->endpoints_poll[epindex].events = POLLIN | POLLERR; + // @todo initialize or add local nodeid to appropriate route table + if (ep->addr.family == AF_INET) { + if (!dht->rt_ipv4) { + //ks_dhtrt_initroute(&dht->rt_ipv4, dht->pool, (ks_dhtrt_nodeid_t)); + } + } else { + if (!dht->rt_ipv6) { + //ks_dhtrt_initroute(&dht->rt_ipv6, dht->pool); + } + } + if (endpoint) { *endpoint = ep; } @@ -727,7 +753,7 @@ KS_DECLARE(ks_status_t) ks_dht_send_ping(ks_dht_t *dht, ks_dht_endpoint_t *ep, k return KS_STATUS_FAIL; } - ben_dict_set(a, ben_blob("id", 2), ben_blob(ep->nodeid, KS_DHT_NODEID_SIZE)); + ben_dict_set(a, ben_blob("id", 2), ben_blob(ep->nodeid.id, KS_DHT_NODEID_SIZE)); ks_log(KS_LOG_DEBUG, "Sending message query ping\n"); ks_q_push(dht->send_q, (void *)message); @@ -751,8 +777,8 @@ KS_DECLARE(ks_status_t) ks_dht_send_findnode(ks_dht_t *dht, ks_dht_endpoint_t *e return KS_STATUS_FAIL; } - ben_dict_set(a, ben_blob("id", 2), ben_blob(ep->nodeid, KS_DHT_NODEID_SIZE)); - ben_dict_set(a, ben_blob("target", 6), ben_blob(targetid, KS_DHT_NODEID_SIZE)); + ben_dict_set(a, ben_blob("id", 2), ben_blob(ep->nodeid.id, KS_DHT_NODEID_SIZE)); + ben_dict_set(a, ben_blob("target", 6), ben_blob(targetid->id, KS_DHT_NODEID_SIZE)); ks_log(KS_LOG_DEBUG, "Sending message query find_node\n"); ks_q_push(dht->send_q, (void *)message); @@ -1032,7 +1058,7 @@ KS_DECLARE(ks_status_t) ks_dht_process_query_ping(ks_dht_t *dht, ks_dht_message_ // goto done; //} - ben_dict_set(r, ben_blob("id", 2), ben_blob(message->endpoint->nodeid, KS_DHT_NODEID_SIZE)); + ben_dict_set(r, ben_blob("id", 2), ben_blob(message->endpoint->nodeid.id, KS_DHT_NODEID_SIZE)); ks_log(KS_LOG_DEBUG, "Sending message response ping\n"); ks_q_push(dht->send_q, (void *)response); @@ -1144,7 +1170,7 @@ KS_DECLARE(ks_status_t) ks_dht_process_query_findnode(ks_dht_t *dht, ks_dht_mess // goto done; //} - ben_dict_set(r, ben_blob("id", 2), ben_blob(message->endpoint->nodeid, KS_DHT_NODEID_SIZE)); + ben_dict_set(r, ben_blob("id", 2), ben_blob(message->endpoint->nodeid.id, KS_DHT_NODEID_SIZE)); // @todo populate nodes/nodes6 ben_dict_set(r, ben_blob("nodes", 5), ben_blob(buffer, buffer_length)); diff --git a/libs/libks/src/dht/ks_dht.h b/libs/libks/src/dht/ks_dht.h index 36cc4d2af6..a86881d31a 100644 --- a/libs/libks/src/dht/ks_dht.h +++ b/libs/libks/src/dht/ks_dht.h @@ -3,7 +3,7 @@ #include "ks.h" #include "ks_bencode.h" - +#include "ks_dht_bucket.h" KS_BEGIN_EXTERN_C @@ -21,7 +21,7 @@ KS_BEGIN_EXTERN_C #define KS_DHT_TRANSACTION_EXPIRATION_DELAY 30 typedef struct ks_dht_s ks_dht_t; -typedef uint8_t ks_dht_nodeid_t[KS_DHT_NODEID_SIZE]; +typedef struct ks_dht_nodeid_s ks_dht_nodeid_t; typedef struct ks_dht_message_s ks_dht_message_t; typedef struct ks_dht_endpoint_s ks_dht_endpoint_t; typedef struct ks_dht_transaction_s ks_dht_transaction_t; @@ -29,6 +29,12 @@ typedef struct ks_dht_transaction_s ks_dht_transaction_t; typedef ks_status_t (*ks_dht_message_callback_t)(ks_dht_t *dht, ks_dht_message_t *message); +/** + * Note: This must remain a structure for casting from raw data + */ +struct ks_dht_nodeid_s { + uint8_t id[KS_DHT_NODEID_SIZE]; +}; struct ks_dht_message_s { ks_pool_t *pool; @@ -82,8 +88,11 @@ struct ks_dht_s { uint8_t recv_buffer[KS_DHT_RECV_BUFFER_SIZE]; ks_size_t recv_buffer_length; - uint32_t transactionid_next; + volatile uint32_t transactionid_next; ks_hash_t *transactions_hash; + + ks_dhtrt_routetable *rt_ipv4; + ks_dhtrt_routetable *rt_ipv6; }; /** @@ -139,7 +148,7 @@ KS_DECLARE(ks_status_t) ks_dht_message_error(ks_dht_message_t *message, * */ KS_DECLARE(ks_status_t) ks_dht_transaction_alloc(ks_dht_transaction_t **transaction, ks_pool_t *pool); -KS_DECLARE(ks_status_t) ks_dht_transaction_prealloc(ks_dht_transaction_t *trasnaction, ks_pool_t *pool); +KS_DECLARE(ks_status_t) ks_dht_transaction_prealloc(ks_dht_transaction_t *transaction, ks_pool_t *pool); KS_DECLARE(ks_status_t) ks_dht_transaction_free(ks_dht_transaction_t *transaction); KS_DECLARE(ks_status_t) ks_dht_transaction_init(ks_dht_transaction_t *transaction, diff --git a/libs/libks/src/dht/ks_dht_endpoint.c b/libs/libks/src/dht/ks_dht_endpoint.c index 95b48dcc6f..eb84cb7ead 100644 --- a/libs/libks/src/dht/ks_dht_endpoint.c +++ b/libs/libks/src/dht/ks_dht_endpoint.c @@ -58,9 +58,9 @@ KS_DECLARE(ks_status_t) ks_dht_endpoint_init(ks_dht_endpoint_t *endpoint, const ks_assert(addr->family == AF_INET || addr->family == AF_INET6); if (!nodeid) { - randombytes_buf(endpoint->nodeid, KS_DHT_NODEID_SIZE); + randombytes_buf(endpoint->nodeid.id, KS_DHT_NODEID_SIZE); } else { - memcpy(endpoint->nodeid, nodeid, KS_DHT_NODEID_SIZE); + memcpy(endpoint->nodeid.id, nodeid->id, KS_DHT_NODEID_SIZE); } endpoint->addr = *addr; -- 2.47.2