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;
};
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 */
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 */
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,
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) {
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);
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 */