dht.h - libdht - A simple helper library for distributed hash tables.
 (HTM) git clone git://r-36.net/libdht
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       dht.h (3060B)
       ---
            1 /*
            2  * Copy me if you can.
            3  * by 20h
            4  */
            5 
            6 #ifndef __DHT_H__
            7 #define __DHT_H__
            8 
            9 /*
           10  * List functions. Most of them aren't needed, but provided.
           11  */
           12 
           13 #define fordhtlist(list, elem) for (dhtlistelem_t *elem = (list)->first; elem;\
           14                 elem = elem->next)
           15 
           16 #define forodhtlist(list, elem) for (elem = (list)->first; elem;\
           17                 elem = elem->next)
           18 
           19 typedef struct dhtnode_t dhtnode_t;
           20 
           21 typedef struct dhtlistelem_t dhtlistelem_t;
           22 struct dhtlistelem_t {
           23         dhtlistelem_t *next;
           24         dhtlistelem_t *prev;
           25 
           26         dhtnode_t *node;
           27 };
           28 
           29 typedef struct dhtlist_t dhtlist_t;
           30 struct dhtlist_t {
           31         dhtlistelem_t *first;
           32         dhtlistelem_t *last;
           33         int len;
           34 };
           35 
           36 dhtlistelem_t *dhtlistelem_set(dhtlistelem_t *elem, dhtnode_t *node);
           37 dhtlistelem_t *dhtlistelem_new(dhtnode_t *node);
           38 void dhtlistelem_free(dhtlistelem_t *elem);
           39 
           40 dhtlist_t *dhtlist_new(void);
           41 void dhtlist_free(dhtlist_t *dhtlist);
           42 dhtlistelem_t *dhtlist_addelem(dhtlist_t *dhtlist, dhtlistelem_t *elem);
           43 dhtlistelem_t *dhtlist_add(dhtlist_t *dhtlist, dhtnode_t *node);
           44 dhtlistelem_t *dhtlist_push(dhtlist_t *dhtlist, dhtnode_t *node);
           45 void dhtlist_delelemlinks(dhtlist_t *dhtlist, dhtlistelem_t *elem);
           46 void dhtlist_delelem(dhtlist_t *dhtlist, dhtlistelem_t *elem);
           47 dhtlistelem_t *dhtlist_insert(dhtlist_t *dhtlist, dhtlistelem_t *elem, int idx);
           48 dhtlistelem_t *dhtlist_move(dhtlist_t *dhtlist, dhtlistelem_t *elem, int idx);
           49 void dhtlist_print(dhtlist_t *dhtlist);
           50 
           51 /*
           52  * DHT functions.
           53  */
           54 #define IDLENGTH 20
           55 
           56 struct dhtnode_t {
           57         char id[IDLENGTH];
           58         char *addr;
           59 };
           60 
           61 typedef struct dhtrouting_t dhtrouting_t;
           62 struct dhtrouting_t {
           63         dhtnode_t *node;
           64         dhtlist_t *buckets[IDLENGTH * 8];
           65 };
           66 
           67 typedef struct dht_t dht_t;
           68 struct dht_t {
           69         dhtrouting_t *routing;
           70 };
           71 
           72 /*
           73  * These functions are used for handling the dhtnodes, which is simply
           74  * the ID of the specific host.
           75  */
           76 dhtnode_t *dhtnode_mkid(dhtnode_t *node);
           77 dhtnode_t *dhtnode_setid(dhtnode_t *node, char id[IDLENGTH]);
           78 dhtnode_t *dhtnode_setaddr(dhtnode_t *node, char *addr);
           79 dhtnode_t *dhtnode_new(void);
           80 void dhtnode_free(dhtnode_t *node);
           81 
           82 /*
           83  * These are internal functions for the dhtnode.
           84  */
           85 void dhtnode_print(dhtnode_t *node);
           86 int dhtnode_cmp(dhtnode_t *node1, dhtnode_t *node2);
           87 dhtnode_t *dhtnode_xor(dhtnode_t *node1, dhtnode_t *node2);
           88 int dhtnode_prefixlen(dhtnode_t *node);
           89 
           90 /*
           91  * These are internal routing functions.
           92  */
           93 dhtrouting_t *dhtrouting_new(dhtnode_t *node);
           94 void dhtrouting_free(dhtrouting_t *route);
           95 dhtrouting_t *dhtrouting_update(dhtrouting_t *route, dhtnode_t *node);
           96 dhtlist_t *dhtrouting_findclosest(dhtrouting_t *route, dhtnode_t *target,
           97                 int max);
           98 
           99 /*
          100  * These are the high-level functions for dht handling.
          101  */
          102 dht_t *dht_new(char *network); /* network is a unique identifier */
          103 void dht_free(dht_t *dht);
          104 
          105 /*
          106  * This function will return a list of nodes, with a maximum of max entries,
          107  * which are close to the target node and known by you.
          108  */
          109 dhtlist_t *dht_find(dht_t *dht, dhtnode_t *target, int max);
          110 
          111 /*
          112  * When a new node appears, add it to your internal routing buckets.
          113  */
          114 dht_t *dht_update(dht_t *dht, dhtnode_t *node);
          115 
          116 #endif
          117