From 0842616add7b0d16b32a9ceb3ed8013733b9c2bb Mon Sep 17 00:00:00 2001 From: colm Date: Mon, 12 Dec 2016 11:20:25 -0500 Subject: [PATCH] FS-9775: Update touch time on existing node in create_node. Rename enums --- libs/libks/src/dht/ks_dht.h | 12 ++++++------ libs/libks/src/dht/ks_dht_bucket.c | 24 +++++++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/libs/libks/src/dht/ks_dht.h b/libs/libks/src/dht/ks_dht.h index c77750d146..508a75277c 100644 --- a/libs/libks/src/dht/ks_dht.h +++ b/libs/libks/src/dht/ks_dht.h @@ -52,15 +52,15 @@ struct ks_dht_nodeid_s { uint8_t id[KS_DHT_NODEID_SIZE]; }; -enum ipfamily { ifv4=AF_INET, ifv6=AF_INET6, ifboth=AF_INET+AF_INET6}; -enum ks_dht_nodetype_t { ks_dht_remote_t=0x01, - ks_dht_local_t=0x02, - ks_dht_both_t=ks_dht_remote_t+ks_dht_local_t }; +enum ks_afflags_t { ifv4=AF_INET, ifv6=AF_INET6, ifboth=AF_INET+AF_INET6}; +enum ks_dht_nodetype_t { KS_DHT_REMOTE=0x01, + KS_DHT_LOCAL=0x02, + KS_DHT_BOTH=KS_DHT_REMOTE+KS_DHT_LOCAL }; struct ks_dht_node_s { ks_dht_nodeid_t nodeid; ks_sockaddr_t addr; - enum ipfamily family; /* AF_INET or AF_INET6 */ + enum ks_afflags_t family; /* AF_INET or AF_INET6 */ enum ks_dht_nodetype_t type; /* local or remote */ ks_dhtrt_routetable_t* table; }; @@ -73,7 +73,7 @@ struct ks_dhtrt_routetable_s { struct ks_dhtrt_querynodes_s { ks_dht_nodeid_t nodeid; /* in: id to query */ - enum ipfamily family; /* in: AF_INET or AF_INET6 or both */ + enum ks_afflags_t family; /* in: AF_INET or AF_INET6 or both */ enum ks_dht_nodetype_t type; /* remote, local, or both */ uint8_t max; /* in: maximum to return */ uint8_t count; /* out: number returned */ diff --git a/libs/libks/src/dht/ks_dht_bucket.c b/libs/libks/src/dht/ks_dht_bucket.c index a279ba53b7..85fb0e84d6 100644 --- a/libs/libks/src/dht/ks_dht_bucket.c +++ b/libs/libks/src/dht/ks_dht_bucket.c @@ -55,7 +55,7 @@ typedef struct ks_dhtrt_bucket_entry_s { uint8_t id[KS_DHT_NODEID_SIZE]; ks_dht_node_t *gptr; /* ptr to peer */ enum ks_dht_nodetype_t type; - enum ipfamily family; + enum ks_afflags_t family; uint8_t inuse; uint8_t outstanding_pings; uint8_t flags; /* active, suspect, expired */ @@ -151,7 +151,7 @@ uint8_t ks_dhtrt_load_query(ks_dhtrt_querynodes_t *query, ks_dhtrt_sortedxors_t static uint8_t ks_dhtrt_findclosest_bucketnodes(unsigned char *nodeid, enum ks_dht_nodetype_t type, - enum ipfamily family, + enum ks_afflags_t family, ks_dhtrt_bucket_header_t *header, ks_dhtrt_sortedxors_t *xors, unsigned char *hixor, @@ -210,11 +210,18 @@ KS_DECLARE(ks_status_t) ks_dhtrt_create_node( ks_dhtrt_routetable_t *table, unsigned short port, ks_dht_node_t **node) { - ks_dht_node_t *tnode = ks_dhtrt_find_node(table, nodeid); + ks_dhtrt_bucket_header_t *header = ks_dhtrt_find_bucketheader(table, nodeid.id); + assert(header != NULL); /* should always find a header */ + + ks_dhtrt_bucket_entry_t *bentry = ks_dhtrt_find_bucketentry(header, nodeid.id); + if (bentry != 0) { + bentry->type = ks_time_now_sec(); + (*node) = bentry->gptr; + return KS_STATUS_SUCCESS; + } - if (tnode != 0) return KS_STATUS_FAIL; /* protect against duplicates */ /* @todo - replace with reusable memory pool */ - tnode = ks_pool_alloc(table->pool, sizeof(ks_dht_node_t)); + ks_dht_node_t *tnode = ks_pool_alloc(table->pool, sizeof(ks_dht_node_t)); tnode->table = table; for (int i = 0; i < 5; ++i) { @@ -262,10 +269,13 @@ ks_status_t ks_dhtrt_insert_node(ks_dhtrt_routetable_t *table, ks_dht_node_t *no int insanity = 0; ks_dhtrt_bucket_header_t *header = ks_dhtrt_find_bucketheader(table, node->nodeid.id); + assert(header != NULL); /* should always find a header */ bucket = header->bucket; - assert(bucket != 0); /* we were not able to find a bucket*/ + if (bucket == 0) { + return KS_STATUS_FAIL; /* we were not able to find a bucket*/ + } while (bucket->count == KS_DHT_BUCKETSIZE) { if (insanity > 3200) assert(insanity < 3200); @@ -932,7 +942,7 @@ void ks_dhtrt_delete_id(ks_dhtrt_bucket_t *bucket, ks_dhtrt_nodeid_t id) static uint8_t ks_dhtrt_findclosest_bucketnodes(ks_dhtrt_nodeid_t id, enum ks_dht_nodetype_t type, - enum ipfamily family, + enum ks_afflags_t family, ks_dhtrt_bucket_header_t *header, ks_dhtrt_sortedxors_t *xors, unsigned char *hixor, /*todo: remove */