diff options
Diffstat (limited to 'toxcore/DHT.h')
-rw-r--r-- | toxcore/DHT.h | 107 |
1 files changed, 58 insertions, 49 deletions
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 6295581b..e4ba2154 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -31,21 +31,21 @@ extern "C" { | |||
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | 33 | ||
34 | /* size of the client_id in bytes */ | 34 | /* Size of the client_id in bytes. */ |
35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
36 | 36 | ||
37 | /* maximum number of clients stored per friend. */ | 37 | /* Maximum number of clients stored per friend. */ |
38 | #define MAX_FRIEND_CLIENTS 8 | 38 | #define MAX_FRIEND_CLIENTS 8 |
39 | 39 | ||
40 | /* A list of the clients mathematically closest to ours. */ | 40 | /* A list of the clients mathematically closest to ours. */ |
41 | #define LCLIENT_LIST 32 | 41 | #define LCLIENT_LIST 32 |
42 | 42 | ||
43 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | 43 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp. */ |
44 | #define LPING_ARRAY 256 //NOTE Deprecated (doesn't do anything) | 44 | #define LPING_ARRAY 256 // NOTE: Deprecated (doesn't do anything). |
45 | 45 | ||
46 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 | 46 | #define LSEND_NODES_ARRAY LPING_ARRAY/2 |
47 | 47 | ||
48 | /*Maximum newly announced nodes to ping per TIME_TOPING seconds*/ | 48 | /* Maximum newly announced nodes to ping per TIME_TOPING seconds. */ |
49 | #define MAX_TOPING 16 | 49 | #define MAX_TOPING 16 |
50 | 50 | ||
51 | typedef struct { | 51 | typedef struct { |
@@ -54,7 +54,7 @@ typedef struct { | |||
54 | uint64_t timestamp; | 54 | uint64_t timestamp; |
55 | uint64_t last_pinged; | 55 | uint64_t last_pinged; |
56 | 56 | ||
57 | /* Returned by this node. Either our friend or us */ | 57 | /* Returned by this node. Either our friend or us. */ |
58 | IP_Port ret_ip_port; | 58 | IP_Port ret_ip_port; |
59 | uint64_t ret_timestamp; | 59 | uint64_t ret_timestamp; |
60 | } Client_data; | 60 | } Client_data; |
@@ -65,10 +65,10 @@ typedef struct { | |||
65 | uint8_t client_id[CLIENT_ID_SIZE]; | 65 | uint8_t client_id[CLIENT_ID_SIZE]; |
66 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 66 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
67 | 67 | ||
68 | /* time at which the last get_nodes request was sent. */ | 68 | /* Time at which the last get_nodes request was sent. */ |
69 | uint64_t lastgetnode; | 69 | uint64_t lastgetnode; |
70 | 70 | ||
71 | /* Symetric NAT hole punching stuff */ | 71 | /* Symetric NAT hole punching stuff. */ |
72 | 72 | ||
73 | /* 1 if currently hole punching, otherwise 0 */ | 73 | /* 1 if currently hole punching, otherwise 0 */ |
74 | uint8_t hole_punching; | 74 | uint8_t hole_punching; |
@@ -106,82 +106,91 @@ typedef struct { | |||
106 | 106 | ||
107 | Client_data *DHT_get_close_list(DHT *dht); | 107 | Client_data *DHT_get_close_list(DHT *dht); |
108 | 108 | ||
109 | /* Add a new friend to the friends list | 109 | /* Add a new friend to the friends list. |
110 | client_id must be CLIENT_ID_SIZE bytes long. | 110 | * client_id must be CLIENT_ID_SIZE bytes long. |
111 | returns 0 if success | 111 | * returns 0 if success. |
112 | returns 1 if failure (friends list is full) */ | 112 | * returns 1 if failure (friends list is full). |
113 | */ | ||
113 | int DHT_addfriend(DHT *dht, uint8_t *client_id); | 114 | int DHT_addfriend(DHT *dht, uint8_t *client_id); |
114 | 115 | ||
115 | /* Delete a friend from the friends list | 116 | /* Delete a friend from the friends list. |
116 | client_id must be CLIENT_ID_SIZE bytes long. | 117 | * client_id must be CLIENT_ID_SIZE bytes long. |
117 | returns 0 if success | 118 | * returns 0 if success. |
118 | returns 1 if failure (client_id not in friends list) */ | 119 | * returns 1 if failure (client_id not in friends list). |
120 | */ | ||
119 | int DHT_delfriend(DHT *dht, uint8_t *client_id); | 121 | int DHT_delfriend(DHT *dht, uint8_t *client_id); |
120 | 122 | ||
121 | /* Get ip of friend | 123 | /* Get ip of friend. |
122 | client_id must be CLIENT_ID_SIZE bytes long. | 124 | * client_id must be CLIENT_ID_SIZE bytes long. |
123 | ip must be 4 bytes long. | 125 | * ip must be 4 bytes long. |
124 | port must be 2 bytes long. | 126 | * port must be 2 bytes long. |
125 | returns ip if success | 127 | * returns ip if success. |
126 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 128 | * returns ip of 0 if failure (This means the friend is either offline or we have not found him yet). |
127 | returns ip of 1 if friend is not in list. */ | 129 | * returns ip of 1 if friend is not in list. |
130 | */ | ||
128 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); | 131 | IP_Port DHT_getfriendip(DHT *dht, uint8_t *client_id); |
129 | 132 | ||
130 | /* Run this function at least a couple times per second (It's the main loop) */ | 133 | /* Run this function at least a couple times per second (It's the main loop). */ |
131 | void do_DHT(DHT *dht); | 134 | void do_DHT(DHT *dht); |
132 | 135 | ||
133 | /* Use this function to bootstrap the client | 136 | /* Use this function to bootstrap the client. |
134 | Sends a get nodes request to the given node with ip port and public_key */ | 137 | * Sends a get nodes request to the given node with ip port and public_key. |
138 | */ | ||
135 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); | 139 | void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); |
136 | 140 | ||
137 | /* Add nodes to the toping list | 141 | /* Add nodes to the toping list. |
138 | all nodes in this list are pinged every TIME_TOPING seconds | 142 | * All nodes in this list are pinged every TIME_TOPING seconds |
139 | and are then removed from the list. | 143 | * and are then removed from the list. |
140 | if the list is full the nodes farthest from our client_id are replaced | 144 | * If the list is full the nodes farthest from our client_id are replaced. |
141 | the purpose of this list is to enable quick integration of new nodes into the | 145 | * The purpose of this list is to enable quick integration of new nodes into the |
142 | network while preventing amplification attacks. | 146 | * network while preventing amplification attacks. |
143 | return 0 if node was added | 147 | * return 0 if node was added. |
144 | return -1 if node was not added */ | 148 | * return -1 if node was not added. |
149 | */ | ||
145 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); | 150 | int add_toping(DHT *dht, uint8_t *client_id, IP_Port ip_port); |
146 | 151 | ||
147 | /* ROUTING FUNCTIONS */ | 152 | /* ROUTING FUNCTIONS */ |
148 | 153 | ||
149 | /* send the given packet to node with client_id | 154 | /* Send the given packet to node with client_id. |
150 | returns -1 if failure */ | 155 | returns -1 if failure. */ |
151 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); | 156 | int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length); |
152 | 157 | ||
153 | /* Send the following packet to everyone who tells us they are connected to friend_id | 158 | /* Send the following packet to everyone who tells us they are connected to friend_id. |
154 | returns the number of nodes it sent the packet to */ | 159 | * returns the number of nodes it sent the packet to. |
160 | */ | ||
155 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); | 161 | int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t length); |
156 | 162 | ||
157 | /* NAT PUNCHING FUNCTIONS */ | 163 | /* NAT PUNCHING FUNCTIONS */ |
158 | 164 | ||
159 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist | 165 | /* Puts all the different ips returned by the nodes for a friend_id into array ip_portlist. |
160 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 166 | * ip_portlist must be at least MAX_FRIEND_CLIENTS big. |
161 | returns the number of ips returned | 167 | * returns the number of ips returned. |
162 | returns -1 if no such friend*/ | 168 | * returns -1 if no such friend. |
169 | */ | ||
163 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); | 170 | int friend_ips(DHT *dht, IP_Port *ip_portlist, uint8_t *friend_id); |
164 | 171 | ||
165 | /* SAVE/LOAD functions */ | 172 | /* SAVE/LOAD functions */ |
166 | 173 | ||
167 | /* get the size of the DHT (for saving) */ | 174 | /* Get the size of the DHT (for saving). */ |
168 | uint32_t DHT_size(DHT *dht); | 175 | uint32_t DHT_size(DHT *dht); |
169 | 176 | ||
170 | /* save the DHT in data where data is an array of size DHT_size() */ | 177 | /* Save the DHT in data where data is an array of size DHT_size(). */ |
171 | void DHT_save(DHT *dht, uint8_t *data); | 178 | void DHT_save(DHT *dht, uint8_t *data); |
172 | 179 | ||
173 | /* init DHT */ | 180 | /* Initialize DHT. */ |
174 | DHT *new_DHT(Net_Crypto *c); | 181 | DHT *new_DHT(Net_Crypto *c); |
175 | 182 | ||
176 | void kill_DHT(DHT *dht); | 183 | void kill_DHT(DHT *dht); |
177 | 184 | ||
178 | /* load the DHT from data of size size; | 185 | /* Load the DHT from data of size size. |
179 | return -1 if failure | 186 | * return -1 if failure. |
180 | return 0 if success */ | 187 | * return 0 if success. |
188 | */ | ||
181 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size); | 189 | int DHT_load(DHT *dht, uint8_t *data, uint32_t size); |
182 | 190 | ||
183 | /* returns 0 if we are not connected to the DHT | 191 | /* returns 0 if we are not connected to the DHT |
184 | returns 1 if we are */ | 192 | * returns 1 if we are |
193 | */ | ||
185 | int DHT_isconnected(DHT *dht); | 194 | int DHT_isconnected(DHT *dht); |
186 | 195 | ||
187 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); | 196 | void addto_lists(DHT *dht, IP_Port ip_port, uint8_t *client_id); |