diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-26 18:57:42 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-26 18:57:42 -0700 |
commit | 792a02e9f62a6ba5ec40a6166f6bd58f197735d7 (patch) | |
tree | 6ca35c268bee6fd452b3a9ef2b3918b2cff61dab /core/DHT.c | |
parent | 1b4ea2e1aeb874e872a2c767326633450de12d20 (diff) | |
parent | 11e18d71c9723ac6c77403db48f6b4d497edac5f (diff) |
Merge pull request #111 from nfkd/patch-3
Fix braces and a few whitespaces
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 470 |
1 files changed, 170 insertions, 300 deletions
@@ -94,15 +94,12 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) | |||
94 | { | 94 | { |
95 | uint32_t i; | 95 | uint32_t i; |
96 | for(i = 0; i < CLIENT_ID_SIZE; ++i) { | 96 | for(i = 0; i < CLIENT_ID_SIZE; ++i) { |
97 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { | 97 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) |
98 | return 1; | 98 | return 1; |
99 | } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { | 99 | else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) |
100 | return 2; | 100 | return 2; |
101 | } | ||
102 | } | 101 | } |
103 | |||
104 | return 0; | 102 | return 0; |
105 | |||
106 | } | 103 | } |
107 | 104 | ||
108 | /* check if client with client_id is already in list of length length. | 105 | /* check if client with client_id is already in list of length length. |
@@ -114,14 +111,14 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
114 | { | 111 | { |
115 | uint32_t i; | 112 | uint32_t i; |
116 | uint32_t temp_time = unix_time(); | 113 | uint32_t temp_time = unix_time(); |
117 | 114 | ||
118 | for(i = 0; i < length; ++i) { | 115 | for(i = 0; i < length; ++i) { |
119 | /*If ip_port is assigned to a different client_id replace it*/ | 116 | /*If ip_port is assigned to a different client_id replace it*/ |
120 | if(list[i].ip_port.ip.i == ip_port.ip.i && | 117 | if(list[i].ip_port.ip.i == ip_port.ip.i && |
121 | list[i].ip_port.port == ip_port.port) { | 118 | list[i].ip_port.port == ip_port.port) { |
122 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 119 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
123 | } | 120 | } |
124 | 121 | ||
125 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { | 122 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
126 | /* Refresh the client timestamp. */ | 123 | /* Refresh the client timestamp. */ |
127 | list[i].timestamp = temp_time; | 124 | list[i].timestamp = temp_time; |
@@ -131,7 +128,7 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
131 | } | 128 | } |
132 | } | 129 | } |
133 | return 0; | 130 | return 0; |
134 | 131 | ||
135 | } | 132 | } |
136 | 133 | ||
137 | /* check if client with client_id is already in node format list of length length. | 134 | /* check if client with client_id is already in node format list of length length. |
@@ -139,14 +136,10 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
139 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 136 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) |
140 | { | 137 | { |
141 | uint32_t i; | 138 | uint32_t i; |
142 | for(i = 0; i < length; ++i) { | 139 | for(i = 0; i < length; ++i) |
143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { | 140 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
144 | |||
145 | return 1; | 141 | return 1; |
146 | } | ||
147 | } | ||
148 | return 0; | 142 | return 0; |
149 | |||
150 | } | 143 | } |
151 | 144 | ||
152 | /*Return the friend number from the client_id | 145 | /*Return the friend number from the client_id |
@@ -154,12 +147,9 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | |||
154 | static int friend_number(uint8_t * client_id) | 147 | static int friend_number(uint8_t * client_id) |
155 | { | 148 | { |
156 | uint32_t i; | 149 | uint32_t i; |
157 | for(i = 0; i < num_friends; ++i) { | 150 | for(i = 0; i < num_friends; ++i) |
158 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 151 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ |
159 | { | ||
160 | return i; | 152 | return i; |
161 | } | ||
162 | } | ||
163 | return -1; | 153 | return -1; |
164 | } | 154 | } |
165 | 155 | ||
@@ -176,7 +166,7 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
176 | uint32_t i, j, k; | 166 | uint32_t i, j, k; |
177 | int num_nodes=0; | 167 | int num_nodes=0; |
178 | uint32_t temp_time = unix_time(); | 168 | uint32_t temp_time = unix_time(); |
179 | for(i = 0; i < LCLIENT_LIST; ++i) { | 169 | for(i = 0; i < LCLIENT_LIST; ++i) |
180 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && | 170 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && |
181 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { | 171 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { |
182 | /* if node is good and not already in list. */ | 172 | /* if node is good and not already in list. */ |
@@ -185,17 +175,16 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
185 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; | 175 | nodes_list[num_nodes].ip_port = close_clientlist[i].ip_port; |
186 | num_nodes++; | 176 | num_nodes++; |
187 | } | 177 | } |
188 | else for(j = 0; j < MAX_SENT_NODES; ++j) { | 178 | else for(j = 0; j < MAX_SENT_NODES; ++j) |
189 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { | 179 | if(id_closest(client_id, nodes_list[j].client_id, close_clientlist[i].client_id) == 2) { |
190 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 180 | memcpy(nodes_list[j].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
191 | nodes_list[j].ip_port = close_clientlist[i].ip_port; | 181 | nodes_list[j].ip_port = close_clientlist[i].ip_port; |
192 | break; | 182 | break; |
193 | } | 183 | } |
194 | } | 184 | } |
195 | } | 185 | |
196 | } | 186 | for(i = 0; i < num_friends; ++i) |
197 | for(i = 0; i < num_friends; ++i) { | 187 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
198 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | ||
199 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && | 188 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && |
200 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { | 189 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { |
201 | /* if node is good and not already in list. */ | 190 | /* if node is good and not already in list. */ |
@@ -203,17 +192,13 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
203 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 192 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; | 193 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; |
205 | num_nodes++; | 194 | num_nodes++; |
206 | } else for(k = 0; k < MAX_SENT_NODES; ++k) { | 195 | } else for(k = 0; k < MAX_SENT_NODES; ++k) |
207 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) { | 196 | if(id_closest(client_id, nodes_list[k].client_id, friends_list[i].client_list[j].client_id) == 2) { |
208 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 197 | memcpy(nodes_list[k].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
209 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; | 198 | nodes_list[k].ip_port = friends_list[i].client_list[j].ip_port; |
210 | break; | 199 | break; |
211 | } | 200 | } |
212 | } | ||
213 | } | 201 | } |
214 | } | ||
215 | } | ||
216 | |||
217 | return num_nodes; | 202 | return num_nodes; |
218 | } | 203 | } |
219 | 204 | ||
@@ -224,7 +209,7 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
224 | { | 209 | { |
225 | uint32_t i; | 210 | uint32_t i; |
226 | uint32_t temp_time = unix_time(); | 211 | uint32_t temp_time = unix_time(); |
227 | for(i = 0; i < length; ++i) { | 212 | for(i = 0; i < length; ++i) |
228 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { | 213 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { |
229 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 214 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
230 | list[i].ip_port = ip_port; | 215 | list[i].ip_port = ip_port; |
@@ -234,9 +219,8 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
234 | list[i].ret_timestamp = 0; | 219 | list[i].ret_timestamp = 0; |
235 | return 0; | 220 | return 0; |
236 | } | 221 | } |
237 | } | 222 | |
238 | return 1; | 223 | return 1; |
239 | |||
240 | } | 224 | } |
241 | 225 | ||
242 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ | 226 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ |
@@ -245,7 +229,7 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
245 | uint32_t i; | 229 | uint32_t i; |
246 | uint32_t temp_time = unix_time(); | 230 | uint32_t temp_time = unix_time(); |
247 | 231 | ||
248 | for(i = 0; i < length; ++i) { | 232 | for(i = 0; i < length; ++i) |
249 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { | 233 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { |
250 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 234 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
251 | list[i].ip_port = ip_port; | 235 | list[i].ip_port = ip_port; |
@@ -255,9 +239,8 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
255 | list[i].ret_timestamp = 0; | 239 | list[i].ret_timestamp = 0; |
256 | return 0; | 240 | return 0; |
257 | } | 241 | } |
258 | } | 242 | |
259 | return 1; | 243 | return 1; |
260 | |||
261 | } | 244 | } |
262 | 245 | ||
263 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ | 246 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ |
@@ -266,21 +249,16 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
266 | uint32_t i; | 249 | uint32_t i; |
267 | 250 | ||
268 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ | 251 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ |
269 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 252 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) |
270 | 253 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | |
271 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | ||
272 | /* if we can't replace bad nodes we try replacing good ones */ | 254 | /* if we can't replace bad nodes we try replacing good ones */ |
273 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | 255 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); |
274 | } | 256 | |
275 | } | 257 | for(i = 0; i < num_friends; ++i) |
276 | for(i = 0; i < num_friends; ++i) { | 258 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) |
277 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { | 259 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) |
278 | if(replace_bad(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { | ||
279 | /* if we can't replace bad nodes we try replacing good ones. */ | 260 | /* if we can't replace bad nodes we try replacing good ones. */ |
280 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); | 261 | replace_good(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port, friends_list[i].client_id); |
281 | } | ||
282 | } | ||
283 | } | ||
284 | } | 262 | } |
285 | 263 | ||
286 | /* If client_id is a friend or us, update ret_ip_port | 264 | /* If client_id is a friend or us, update ret_ip_port |
@@ -290,26 +268,21 @@ void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient | |||
290 | uint32_t i, j; | 268 | uint32_t i, j; |
291 | uint32_t temp_time = unix_time(); | 269 | uint32_t temp_time = unix_time(); |
292 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { | 270 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { |
293 | for(i = 0; i < LCLIENT_LIST; ++i) { | 271 | for(i = 0; i < LCLIENT_LIST; ++i) |
294 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { | 272 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
295 | close_clientlist[i].ret_ip_port = ip_port; | 273 | close_clientlist[i].ret_ip_port = ip_port; |
296 | close_clientlist[i].ret_timestamp = temp_time; | 274 | close_clientlist[i].ret_timestamp = temp_time; |
297 | return; | 275 | return; |
298 | } | 276 | } |
299 | } | 277 | } else |
300 | } | 278 | for(i = 0; i < num_friends; ++i) |
301 | else | 279 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) |
302 | for(i = 0; i < num_friends; ++i) { | 280 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
303 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { | ||
304 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | ||
305 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { | 281 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { |
306 | friends_list[i].client_list[j].ret_ip_port = ip_port; | 282 | friends_list[i].client_list[j].ret_ip_port = ip_port; |
307 | friends_list[i].client_list[j].ret_timestamp = temp_time; | 283 | friends_list[i].client_list[j].ret_timestamp = temp_time; |
308 | return; | 284 | return; |
309 | } | 285 | } |
310 | } | ||
311 | } | ||
312 | } | ||
313 | } | 286 | } |
314 | 287 | ||
315 | /* ping timeout in seconds */ | 288 | /* ping timeout in seconds */ |
@@ -326,30 +299,21 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
326 | uint8_t pinging; | 299 | uint8_t pinging; |
327 | uint32_t temp_time = unix_time(); | 300 | uint32_t temp_time = unix_time(); |
328 | 301 | ||
329 | for(i = 0; i < LPING_ARRAY; ++i ) { | 302 | for(i = 0; i < LPING_ARRAY; ++i ) |
330 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { | 303 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { |
331 | pinging = 0; | 304 | pinging = 0; |
332 | if(ip_port.ip.i != 0) { | 305 | if(ip_port.ip.i != 0) |
333 | if(pings[i].ip_port.ip.i == ip_port.ip.i && | 306 | if(pings[i].ip_port.ip.i == ip_port.ip.i && |
334 | pings[i].ip_port.port == ip_port.port) { | 307 | pings[i].ip_port.port == ip_port.port) |
335 | ++pinging; | 308 | ++pinging; |
336 | } | 309 | if(ping_id != 0) |
337 | } | ||
338 | if(ping_id != 0) { | ||
339 | if(pings[i].ping_id == ping_id) | 310 | if(pings[i].ping_id == ping_id) |
340 | { | ||
341 | ++pinging; | 311 | ++pinging; |
342 | } | 312 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
343 | } | ||
344 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { | ||
345 | return 1; | 313 | return 1; |
346 | } | ||
347 | |||
348 | } | 314 | } |
349 | } | ||
350 | 315 | ||
351 | return 0; | 316 | return 0; |
352 | |||
353 | } | 317 | } |
354 | 318 | ||
355 | /* Same as last function but for get_node requests. */ | 319 | /* Same as last function but for get_node requests. */ |
@@ -359,29 +323,22 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
359 | uint8_t pinging; | 323 | uint8_t pinging; |
360 | uint32_t temp_time = unix_time(); | 324 | uint32_t temp_time = unix_time(); |
361 | 325 | ||
362 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { | 326 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) |
363 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { | 327 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { |
364 | pinging = 0; | 328 | pinging = 0; |
365 | if(ip_port.ip.i != 0) { | 329 | if(ip_port.ip.i != 0) |
366 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && | 330 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && |
367 | send_nodes[i].ip_port.port == ip_port.port) { | 331 | send_nodes[i].ip_port.port == ip_port.port) |
368 | ++pinging; | ||
369 | } | ||
370 | } | ||
371 | if(ping_id != 0) { | ||
372 | if(send_nodes[i].ping_id == ping_id) { | ||
373 | ++pinging; | 332 | ++pinging; |
374 | } | 333 | if(ping_id != 0) |
375 | } | 334 | if(send_nodes[i].ping_id == ping_id) |
376 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { | 335 | ++pinging; |
377 | return 1; | 336 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
378 | } | 337 | return 1; |
379 | 338 | ||
380 | } | 339 | } |
381 | } | ||
382 | 340 | ||
383 | return 0; | 341 | return 0; |
384 | |||
385 | } | 342 | } |
386 | 343 | ||
387 | /* Add a new ping request to the list of ping requests | 344 | /* Add a new ping request to the list of ping requests |
@@ -393,19 +350,17 @@ uint64_t add_pinging(IP_Port ip_port) | |||
393 | uint32_t i, j; | 350 | uint32_t i, j; |
394 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 351 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
395 | uint32_t temp_time = unix_time(); | 352 | uint32_t temp_time = unix_time(); |
396 | 353 | ||
397 | for(i = 0; i < PING_TIMEOUT; ++i ) { | 354 | for(i = 0; i < PING_TIMEOUT; ++i ) |
398 | for(j = 0; j < LPING_ARRAY; ++j ) { | 355 | for(j = 0; j < LPING_ARRAY; ++j ) |
399 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { | 356 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
400 | pings[j].timestamp = temp_time; | 357 | pings[j].timestamp = temp_time; |
401 | pings[j].ip_port = ip_port; | 358 | pings[j].ip_port = ip_port; |
402 | pings[j].ping_id = ping_id; | 359 | pings[j].ping_id = ping_id; |
403 | return ping_id; | 360 | return ping_id; |
404 | } | 361 | } |
405 | } | 362 | |
406 | } | ||
407 | return 0; | 363 | return 0; |
408 | |||
409 | } | 364 | } |
410 | 365 | ||
411 | /* Same but for get node requests */ | 366 | /* Same but for get node requests */ |
@@ -414,99 +369,86 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
414 | uint32_t i, j; | 369 | uint32_t i, j; |
415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 370 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
416 | uint32_t temp_time = unix_time(); | 371 | uint32_t temp_time = unix_time(); |
417 | 372 | ||
418 | for(i = 0; i < PING_TIMEOUT; ++i ) { | 373 | for(i = 0; i < PING_TIMEOUT; ++i ) |
419 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { | 374 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) |
420 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { | 375 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
421 | send_nodes[j].timestamp = temp_time; | 376 | send_nodes[j].timestamp = temp_time; |
422 | send_nodes[j].ip_port = ip_port; | 377 | send_nodes[j].ip_port = ip_port; |
423 | send_nodes[j].ping_id = ping_id; | 378 | send_nodes[j].ping_id = ping_id; |
424 | return ping_id; | 379 | return ping_id; |
425 | } | 380 | } |
426 | } | 381 | |
427 | } | ||
428 | return 0; | 382 | return 0; |
429 | |||
430 | } | 383 | } |
431 | 384 | ||
432 | /* send a ping request | 385 | /* send a ping request |
433 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ | 386 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ |
434 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | 387 | static int pingreq(IP_Port ip_port, uint8_t * public_key) |
435 | { | 388 | { |
436 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { | 389 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
437 | return 1; | 390 | return 1; |
438 | } | 391 | |
439 | 392 | if(is_pinging(ip_port, 0)) | |
440 | if(is_pinging(ip_port, 0)) { | ||
441 | return 1; | 393 | return 1; |
442 | } | 394 | |
443 | |||
444 | uint64_t ping_id = add_pinging(ip_port); | 395 | uint64_t ping_id = add_pinging(ip_port); |
445 | if(ping_id == 0) { | 396 | if(ping_id == 0) |
446 | return 1; | 397 | return 1; |
447 | } | 398 | |
448 | |||
449 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | 399 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; |
450 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | 400 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; |
451 | uint8_t nonce[crypto_box_NONCEBYTES]; | 401 | uint8_t nonce[crypto_box_NONCEBYTES]; |
452 | random_nonce(nonce); | 402 | random_nonce(nonce); |
453 | 403 | ||
454 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 404 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
455 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { | 405 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) |
456 | return -1; | 406 | return -1; |
457 | } | ||
458 | data[0] = 0; | 407 | data[0] = 0; |
459 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 408 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
460 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 409 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
461 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 410 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
462 | 411 | ||
463 | return sendpacket(ip_port, data, sizeof(data)); | 412 | return sendpacket(ip_port, data, sizeof(data)); |
464 | |||
465 | } | 413 | } |
466 | 414 | ||
467 | /* send a ping response */ | 415 | /* send a ping response */ |
468 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | 416 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) |
469 | { | 417 | { |
470 | /* check if packet is gonna be sent to ourself */ | 418 | /* check if packet is gonna be sent to ourself */ |
471 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { | 419 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) |
472 | return 1; | 420 | return 1; |
473 | } | 421 | |
474 | |||
475 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | 422 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; |
476 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | 423 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; |
477 | uint8_t nonce[crypto_box_NONCEBYTES]; | 424 | uint8_t nonce[crypto_box_NONCEBYTES]; |
478 | random_nonce(nonce); | 425 | random_nonce(nonce); |
479 | 426 | ||
480 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 427 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
481 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { | 428 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) |
482 | return -1; | 429 | return -1; |
483 | } | ||
484 | data[0] = 1; | 430 | data[0] = 1; |
485 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 431 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
486 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 432 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
487 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 433 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
488 | 434 | ||
489 | return sendpacket(ip_port, data, sizeof(data)); | 435 | return sendpacket(ip_port, data, sizeof(data)); |
490 | |||
491 | } | 436 | } |
492 | 437 | ||
493 | /* send a getnodes request */ | 438 | /* send a getnodes request */ |
494 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 439 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) |
495 | { | 440 | { |
496 | /* check if packet is gonna be sent to ourself */ | 441 | /* check if packet is gonna be sent to ourself */ |
497 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { | 442 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) |
498 | return 1; | 443 | return 1; |
499 | } | ||
500 | 444 | ||
501 | if(is_gettingnodes(ip_port, 0)) { | 445 | if(is_gettingnodes(ip_port, 0)) |
502 | return 1; | 446 | return 1; |
503 | } | ||
504 | 447 | ||
505 | uint64_t ping_id = add_gettingnodes(ip_port); | 448 | uint64_t ping_id = add_gettingnodes(ip_port); |
506 | 449 | ||
507 | if(ping_id == 0) { | 450 | if(ping_id == 0) |
508 | return 1; | 451 | return 1; |
509 | } | ||
510 | 452 | ||
511 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; | 453 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; |
512 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 454 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
@@ -519,34 +461,30 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
519 | 461 | ||
520 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); | 462 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); |
521 | 463 | ||
522 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { | 464 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
523 | return -1; | 465 | return -1; |
524 | } | ||
525 | data[0] = 2; | 466 | data[0] = 2; |
526 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 467 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
527 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 468 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
528 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 469 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
529 | return sendpacket(ip_port, data, sizeof(data)); | 470 | return sendpacket(ip_port, data, sizeof(data)); |
530 | |||
531 | } | 471 | } |
532 | 472 | ||
533 | /* send a send nodes response */ | 473 | /* send a send nodes response */ |
534 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 474 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) |
535 | { | 475 | { |
536 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { | 476 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
537 | return 1; | 477 | return 1; |
538 | } | 478 | |
539 | |||
540 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 479 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
541 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 480 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
542 | 481 | ||
543 | Node_format nodes_list[MAX_SENT_NODES]; | 482 | Node_format nodes_list[MAX_SENT_NODES]; |
544 | int num_nodes = get_close_nodes(client_id, nodes_list); | 483 | int num_nodes = get_close_nodes(client_id, nodes_list); |
545 | 484 | ||
546 | if(num_nodes == 0) { | 485 | if(num_nodes == 0) |
547 | return 0; | 486 | return 0; |
548 | } | 487 | |
549 | |||
550 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 488 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; |
551 | uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 489 | uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
552 | uint8_t nonce[crypto_box_NONCEBYTES]; | 490 | uint8_t nonce[crypto_box_NONCEBYTES]; |
@@ -558,17 +496,15 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
558 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, | 496 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, |
559 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); | 497 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); |
560 | 498 | ||
561 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { | 499 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) |
562 | return -1; | 500 | return -1; |
563 | } | ||
564 | 501 | ||
565 | data[0] = 3; | 502 | data[0] = 3; |
566 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 503 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
567 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 504 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
568 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); | 505 | memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); |
569 | 506 | ||
570 | return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); | 507 | return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); |
571 | |||
572 | } | 508 | } |
573 | 509 | ||
574 | /* Packet handling functions | 510 | /* Packet handling functions |
@@ -577,50 +513,38 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
577 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 513 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) |
578 | { | 514 | { |
579 | uint64_t ping_id; | 515 | uint64_t ping_id; |
580 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { | 516 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
581 | return 1; | 517 | return 1; |
582 | } | ||
583 | /* check if packet is from ourself. */ | 518 | /* check if packet is from ourself. */ |
584 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { | 519 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
585 | return 1; | 520 | return 1; |
586 | } | 521 | |
587 | |||
588 | |||
589 | |||
590 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 522 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
591 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 523 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
592 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 524 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
593 | if(len != sizeof(ping_id)) { | 525 | if(len != sizeof(ping_id)) |
594 | return 1; | 526 | return 1; |
595 | } | ||
596 | 527 | ||
597 | |||
598 | pingres(source, packet + 1, ping_id); | 528 | pingres(source, packet + 1, ping_id); |
599 | 529 | ||
600 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 530 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
601 | 531 | ||
602 | return 0; | 532 | return 0; |
603 | |||
604 | } | 533 | } |
605 | 534 | ||
606 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | 535 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) |
607 | { | 536 | { |
608 | uint64_t ping_id; | 537 | uint64_t ping_id; |
609 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { | 538 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
610 | return 1; | 539 | return 1; |
611 | } | 540 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ |
612 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ { | ||
613 | return 1; | 541 | return 1; |
614 | } | 542 | |
615 | |||
616 | |||
617 | |||
618 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 543 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
619 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 544 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
620 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 545 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
621 | if(len != sizeof(ping_id)) { | 546 | if(len != sizeof(ping_id)) |
622 | return 1; | 547 | return 1; |
623 | } | ||
624 | 548 | ||
625 | if(is_pinging(source, ping_id)) { | 549 | if(is_pinging(source, ping_id)) { |
626 | addto_lists(source, packet + 1); | 550 | addto_lists(source, packet + 1); |
@@ -633,30 +557,26 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
633 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 557 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) |
634 | { | 558 | { |
635 | uint64_t ping_id; | 559 | uint64_t ping_id; |
636 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { | 560 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
637 | return 1; | 561 | return 1; |
638 | } | ||
639 | /* check if packet is from ourself. */ | 562 | /* check if packet is from ourself. */ |
640 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { | 563 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
641 | return 1; | 564 | return 1; |
642 | } | 565 | |
643 | |||
644 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 566 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
645 | 567 | ||
646 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 568 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
647 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 569 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
648 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); | 570 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); |
649 | 571 | ||
650 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { | 572 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) |
651 | return 1; | 573 | return 1; |
652 | } | 574 | |
653 | |||
654 | |||
655 | memcpy(&ping_id, plain, sizeof(ping_id)); | 575 | memcpy(&ping_id, plain, sizeof(ping_id)); |
656 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); | 576 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); |
657 | 577 | ||
658 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 578 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
659 | 579 | ||
660 | return 0; | 580 | return 0; |
661 | 581 | ||
662 | } | 582 | } |
@@ -664,6 +584,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
664 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | 584 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) |
665 | { | 585 | { |
666 | uint64_t ping_id; | 586 | uint64_t ping_id; |
587 | /* TODO: make this more readable */ | ||
667 | if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 588 | if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
668 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || | 589 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || |
669 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 590 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
@@ -671,37 +592,34 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
671 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 592 | length < 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
672 | + sizeof(Node_format) + ENCRYPTION_PADDING) { | 593 | + sizeof(Node_format) + ENCRYPTION_PADDING) { |
673 | return 1; | 594 | return 1; |
674 | } | 595 | } |
675 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES | 596 | uint32_t num_nodes = (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES |
676 | + sizeof(ping_id) + ENCRYPTION_PADDING)) / sizeof(Node_format); | 597 | + sizeof(ping_id) + ENCRYPTION_PADDING)) / sizeof(Node_format); |
677 | 598 | ||
678 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 599 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; |
679 | 600 | ||
680 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 601 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
681 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 602 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
682 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); | 603 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); |
683 | 604 | ||
684 | 605 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) | |
685 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { | ||
686 | return 1; | 606 | return 1; |
687 | } | 607 | |
688 | |||
689 | memcpy(&ping_id, plain, sizeof(ping_id)); | 608 | memcpy(&ping_id, plain, sizeof(ping_id)); |
690 | if(!is_gettingnodes(source, ping_id)) { | 609 | if(!is_gettingnodes(source, ping_id)) |
691 | return 1; | 610 | return 1; |
692 | } | 611 | |
693 | |||
694 | Node_format nodes_list[MAX_SENT_NODES]; | 612 | Node_format nodes_list[MAX_SENT_NODES]; |
695 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | 613 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); |
696 | 614 | ||
697 | addto_lists(source, packet + 1); | 615 | addto_lists(source, packet + 1); |
698 | 616 | ||
699 | uint32_t i; | 617 | uint32_t i; |
700 | for(i = 0; i < num_nodes; ++i) { | 618 | for(i = 0; i < num_nodes; ++i) { |
701 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 619 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); |
702 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 620 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
703 | } | 621 | } |
704 | 622 | ||
705 | return 0; | 623 | return 0; |
706 | } | 624 | } |
707 | 625 | ||
@@ -710,15 +628,13 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
710 | int DHT_addfriend(uint8_t * client_id) | 628 | int DHT_addfriend(uint8_t * client_id) |
711 | { | 629 | { |
712 | Friend * temp; | 630 | Friend * temp; |
713 | if(num_friends == 0) { | 631 | if(num_friends == 0) |
714 | temp = malloc(sizeof(Friend)); | 632 | temp = malloc(sizeof(Friend)); |
715 | } else { | 633 | else |
716 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); | 634 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); |
717 | } | 635 | if(temp == NULL) |
718 | if(temp == NULL) { | ||
719 | return 1; | 636 | return 1; |
720 | } | 637 | |
721 | |||
722 | friends_list = temp; | 638 | friends_list = temp; |
723 | memset(&friends_list[num_friends], 0, sizeof(Friend)); | 639 | memset(&friends_list[num_friends], 0, sizeof(Friend)); |
724 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | 640 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); |
@@ -731,20 +647,18 @@ int DHT_delfriend(uint8_t * client_id) | |||
731 | { | 647 | { |
732 | uint32_t i; | 648 | uint32_t i; |
733 | Friend * temp; | 649 | Friend * temp; |
734 | for(i = 0; i < num_friends; ++i) { | 650 | for(i = 0; i < num_friends; ++i) |
735 | /* Equal */ | 651 | /* Equal */ |
736 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ | 652 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
737 | --num_friends; | 653 | --num_friends; |
738 | if(num_friends != i) { | 654 | if(num_friends != i) |
739 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); | 655 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); |
740 | } | ||
741 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); | 656 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); |
742 | if(temp != NULL) { | 657 | if(temp != NULL) |
743 | friends_list = temp; | 658 | friends_list = temp; |
744 | } | ||
745 | return 0; | 659 | return 0; |
746 | } | 660 | } |
747 | } | 661 | |
748 | return 1; | 662 | return 1; |
749 | } | 663 | } |
750 | 664 | ||
@@ -754,18 +668,16 @@ IP_Port DHT_getfriendip(uint8_t * client_id) | |||
754 | uint32_t i, j; | 668 | uint32_t i, j; |
755 | IP_Port empty = {{{0}}, 0}; | 669 | IP_Port empty = {{{0}}, 0}; |
756 | uint32_t temp_time = unix_time(); | 670 | uint32_t temp_time = unix_time(); |
757 | for(i = 0; i < num_friends; ++i) { | 671 | for(i = 0; i < num_friends; ++i) |
758 | /* Equal */ | 672 | /* Equal */ |
759 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { | 673 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
760 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 674 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
761 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && | 675 | if(memcmp(friends_list[i].client_list[j].client_id, client_id, CLIENT_ID_SIZE) == 0 && |
762 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) { | 676 | friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) |
763 | return friends_list[i].client_list[j].ip_port; | 677 | return friends_list[i].client_list[j].ip_port; |
764 | } | 678 | |
765 | } | ||
766 | return empty; | 679 | return empty; |
767 | } | 680 | } |
768 | } | ||
769 | empty.ip.i = 1; | 681 | empty.ip.i = 1; |
770 | return empty; | 682 | return empty; |
771 | 683 | ||
@@ -791,7 +703,7 @@ void doDHTFriends() | |||
791 | 703 | ||
792 | for(i = 0; i < num_friends; ++i) { | 704 | for(i = 0; i < num_friends; ++i) { |
793 | uint32_t num_nodes = 0; | 705 | uint32_t num_nodes = 0; |
794 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 706 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
795 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { | 707 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { |
796 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { | 708 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
797 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); | 709 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); |
@@ -802,7 +714,6 @@ void doDHTFriends() | |||
802 | ++num_nodes; | 714 | ++num_nodes; |
803 | } | 715 | } |
804 | } | 716 | } |
805 | } | ||
806 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 717 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
807 | rand_node = rand() % num_nodes; | 718 | rand_node = rand() % num_nodes; |
808 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, | 719 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, |
@@ -825,11 +736,10 @@ void doClose() /* tested */ | |||
825 | uint32_t rand_node; | 736 | uint32_t rand_node; |
826 | uint32_t index[LCLIENT_LIST]; | 737 | uint32_t index[LCLIENT_LIST]; |
827 | 738 | ||
828 | for(i = 0; i < LCLIENT_LIST; ++i) { | 739 | for(i = 0; i < LCLIENT_LIST; ++i) |
829 | /* if node is not dead. */ | 740 | /* if node is not dead. */ |
830 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { | 741 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { |
831 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) | 742 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { |
832 | { | ||
833 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); | 743 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); |
834 | close_clientlist[i].last_pinged = temp_time; | 744 | close_clientlist[i].last_pinged = temp_time; |
835 | } | 745 | } |
@@ -838,8 +748,7 @@ void doClose() /* tested */ | |||
838 | index[num_nodes] = i; | 748 | index[num_nodes] = i; |
839 | ++num_nodes; | 749 | ++num_nodes; |
840 | } | 750 | } |
841 | } | 751 | } |
842 | } | ||
843 | 752 | ||
844 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 753 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
845 | rand_node = rand() % num_nodes; | 754 | rand_node = rand() % num_nodes; |
@@ -860,11 +769,9 @@ void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | |||
860 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) | 769 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) |
861 | { | 770 | { |
862 | uint32_t i; | 771 | uint32_t i; |
863 | for(i = 0; i < LCLIENT_LIST; ++i) { | 772 | for(i = 0; i < LCLIENT_LIST; ++i) |
864 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { | 773 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) |
865 | return sendpacket(close_clientlist[i].ip_port, packet, length); | 774 | return sendpacket(close_clientlist[i].ip_port, packet, length); |
866 | } | ||
867 | } | ||
868 | return -1; | 775 | return -1; |
869 | } | 776 | } |
870 | 777 | ||
@@ -878,21 +785,17 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | |||
878 | int num_ips = 0; | 785 | int num_ips = 0; |
879 | uint32_t i; | 786 | uint32_t i; |
880 | uint32_t temp_time = unix_time(); | 787 | uint32_t temp_time = unix_time(); |
881 | if(friend_num >= num_friends) { | 788 | if(friend_num >= num_friends) |
882 | return -1; | 789 | return -1; |
883 | } | ||
884 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) | 790 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) |
885 | { | ||
886 | /*If ip is not zero and node is good */ | 791 | /*If ip is not zero and node is good */ |
887 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && | 792 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && |
888 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | 793 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
889 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { | 794 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) |
890 | return 0; | 795 | return 0; |
891 | } | ||
892 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; | 796 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; |
893 | ++num_ips; | 797 | ++num_ips; |
894 | } | 798 | } |
895 | } | ||
896 | return num_ips; | 799 | return num_ips; |
897 | } | 800 | } |
898 | 801 | ||
@@ -903,21 +806,17 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
903 | uint32_t i, j; | 806 | uint32_t i, j; |
904 | uint32_t sent = 0; | 807 | uint32_t sent = 0; |
905 | uint32_t temp_time = unix_time(); | 808 | uint32_t temp_time = unix_time(); |
906 | for(i = 0; i < num_friends; ++i) { | 809 | for(i = 0; i < num_friends; ++i) |
907 | /* Equal */ | 810 | /* Equal */ |
908 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { | 811 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { |
909 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 812 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
910 | /*If ip is not zero and node is good */ | 813 | /*If ip is not zero and node is good */ |
911 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && | 814 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && |
912 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | 815 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) |
913 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { | 816 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) |
914 | ++sent; | 817 | ++sent; |
915 | } | ||
916 | } | ||
917 | } | ||
918 | return sent; | 818 | return sent; |
919 | } | 819 | } |
920 | } | ||
921 | return 0; | 820 | return 0; |
922 | } | 821 | } |
923 | 822 | ||
@@ -926,28 +825,24 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | |||
926 | int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | 825 | int routeone_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) |
927 | { | 826 | { |
928 | int num = friend_number(friend_id); | 827 | int num = friend_number(friend_id); |
929 | if(num == -1) { | 828 | if(num == -1) |
930 | return 0; | 829 | return 0; |
931 | } | ||
932 | 830 | ||
933 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 831 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
934 | int n = 0; | 832 | int n = 0; |
935 | uint32_t i; | 833 | uint32_t i; |
936 | uint32_t temp_time = unix_time(); | 834 | uint32_t temp_time = unix_time(); |
937 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) { | 835 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) |
938 | /*If ip is not zero and node is good */ | 836 | /*If ip is not zero and node is good */ |
939 | if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && | 837 | if(friends_list[num].client_list[i].ret_ip_port.ip.i != 0 && |
940 | friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | 838 | friends_list[num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
941 | ip_list[n] = friends_list[num].client_list[i].ip_port; | 839 | ip_list[n] = friends_list[num].client_list[i].ip_port; |
942 | ++n; | 840 | ++n; |
943 | } | 841 | } |
944 | } | 842 | if(n < 1) |
945 | if(n < 1) { | ||
946 | return 0; | 843 | return 0; |
947 | } | 844 | if(sendpacket(ip_list[rand() % n], packet, length) == length) |
948 | if(sendpacket(ip_list[rand() % n], packet, length) == length) { | ||
949 | return 1; | 845 | return 1; |
950 | } | ||
951 | return 0; | 846 | return 0; |
952 | } | 847 | } |
953 | 848 | ||
@@ -960,12 +855,10 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id) | |||
960 | { | 855 | { |
961 | 856 | ||
962 | uint32_t i; | 857 | uint32_t i; |
963 | for(i = 0; i < num_friends; ++i) { | 858 | for(i = 0; i < num_friends; ++i) |
964 | /* Equal */ | 859 | /* Equal */ |
965 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { | 860 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) |
966 | return friend_iplist(ip_portlist, i); | 861 | return friend_iplist(ip_portlist, i); |
967 | } | ||
968 | } | ||
969 | return -1; | 862 | return -1; |
970 | } | 863 | } |
971 | 864 | ||
@@ -979,19 +872,17 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) | |||
979 | 872 | ||
980 | uint8_t packet[MAX_DATA_SIZE]; | 873 | uint8_t packet[MAX_DATA_SIZE]; |
981 | int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ | 874 | int len = create_request(packet, public_key, data, sizeof(uint64_t) + 1, 254); /* 254 is NAT ping request packet id */ |
982 | if(len == -1) { | 875 | if(len == -1) |
983 | return -1; | 876 | return -1; |
984 | } | 877 | |
985 | int num = 0; | 878 | int num = 0; |
986 | if(type == 0) { | 879 | |
880 | if(type == 0) | ||
987 | num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ | 881 | num = route_tofriend(public_key, packet, len);/*If packet is request use many people to route it*/ |
988 | } | 882 | else if(type == 1) |
989 | else if(type == 1) { | ||
990 | num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ | 883 | num = routeone_tofriend(public_key, packet, len);/*If packet is response use only one person to route it*/ |
991 | } | 884 | if(num == 0) |
992 | if(num == 0) { | ||
993 | return -1; | 885 | return -1; |
994 | } | ||
995 | return num; | 886 | return num; |
996 | } | 887 | } |
997 | 888 | ||
@@ -999,44 +890,37 @@ int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type) | |||
999 | int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | 890 | int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) |
1000 | { | 891 | { |
1001 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 892 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
1002 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) { | 893 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
1003 | return 1; | 894 | return 1; |
1004 | } | ||
1005 | /* check if request is for us. */ | 895 | /* check if request is for us. */ |
1006 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 896 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
1007 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 897 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
1008 | uint8_t data[MAX_DATA_SIZE]; | 898 | uint8_t data[MAX_DATA_SIZE]; |
1009 | int len = handle_request(public_key, data, packet, length); | 899 | int len = handle_request(public_key, data, packet, length); |
1010 | if(len != sizeof(uint64_t) + 1) { | 900 | if(len != sizeof(uint64_t) + 1) |
1011 | return 1; | 901 | return 1; |
1012 | } | ||
1013 | uint64_t ping_id; | 902 | uint64_t ping_id; |
1014 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); | 903 | memcpy(&ping_id, data + 1, sizeof(uint64_t)); |
1015 | 904 | ||
1016 | int friendnumber = friend_number(public_key); | 905 | int friendnumber = friend_number(public_key); |
1017 | if(friendnumber == -1) { | 906 | if(friendnumber == -1) |
1018 | return 1; | 907 | return 1; |
1019 | } | ||
1020 | 908 | ||
1021 | if(data[0] == 0) { | 909 | if(data[0] == 0) { |
1022 | send_NATping(public_key, ping_id, 1);/*1 is reply*/ | 910 | send_NATping(public_key, ping_id, 1); /*1 is reply*/ |
1023 | return 0; | 911 | return 0; |
1024 | } else if (data[0] == 1) { | 912 | } else if (data[0] == 1) |
1025 | if(friends_list[friendnumber].NATping_id == ping_id) | 913 | if(friends_list[friendnumber].NATping_id == ping_id) { |
1026 | { | ||
1027 | friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); | 914 | friends_list[friendnumber].NATping_id = ((uint64_t)random_int() << 32) + random_int(); |
1028 | friends_list[friendnumber].hole_punching = 1; | 915 | friends_list[friendnumber].hole_punching = 1; |
1029 | return 0; | 916 | return 0; |
1030 | } | 917 | } |
1031 | } | ||
1032 | return 1; | 918 | return 1; |
1033 | } | 919 | } |
1034 | /* if request is not for us, try routing it. */ | 920 | /* if request is not for us, try routing it. */ |
1035 | else { | 921 | else |
1036 | if(route_packet(packet + 1, packet, length) == length) { | 922 | if(route_packet(packet + 1, packet, length) == length) |
1037 | return 0; | 923 | return 0; |
1038 | } | ||
1039 | } | ||
1040 | return 0; | 924 | return 0; |
1041 | } | 925 | } |
1042 | 926 | ||
@@ -1047,21 +931,17 @@ int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) | |||
1047 | static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) | 931 | static IP NAT_commonip(IP_Port * ip_portlist, uint16_t len, uint16_t min_num) |
1048 | { | 932 | { |
1049 | IP zero = {{0}}; | 933 | IP zero = {{0}}; |
1050 | if(len > MAX_FRIEND_CLIENTS) { | 934 | if(len > MAX_FRIEND_CLIENTS) |
1051 | return zero; | 935 | return zero; |
1052 | } | ||
1053 | 936 | ||
1054 | uint32_t i, j; | 937 | uint32_t i, j; |
1055 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; | 938 | uint16_t numbers[MAX_FRIEND_CLIENTS] = {0}; |
1056 | for(i = 0; i < len; ++i) { | 939 | for(i = 0; i < len; ++i) { |
1057 | for(j = 0; j < len; ++j) { | 940 | for(j = 0; j < len; ++j) |
1058 | if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) { | 941 | if(ip_portlist[i].ip.i == ip_portlist[j].ip.i) |
1059 | ++numbers[i]; | 942 | ++numbers[i]; |
1060 | } | 943 | if(numbers[i] >= min_num) |
1061 | } | ||
1062 | if(numbers[i] >= min_num) { | ||
1063 | return ip_portlist[i].ip; | 944 | return ip_portlist[i].ip; |
1064 | } | ||
1065 | } | 945 | } |
1066 | return zero; | 946 | return zero; |
1067 | } | 947 | } |
@@ -1074,12 +954,11 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ | |||
1074 | { | 954 | { |
1075 | uint32_t i; | 955 | uint32_t i; |
1076 | uint16_t num = 0; | 956 | uint16_t num = 0; |
1077 | for(i = 0; i < len; ++i) { | 957 | for(i = 0; i < len; ++i) |
1078 | if(ip_portlist[i].ip.i == ip.i) { | 958 | if(ip_portlist[i].ip.i == ip.i) { |
1079 | portlist[num] = ntohs(ip_portlist[i].port); | 959 | portlist[num] = ntohs(ip_portlist[i].port); |
1080 | ++num; | 960 | ++num; |
1081 | } | 961 | } |
1082 | } | ||
1083 | return num; | 962 | return num; |
1084 | } | 963 | } |
1085 | 964 | ||
@@ -1087,9 +966,8 @@ static uint16_t NAT_getports(uint16_t * portlist, IP_Port * ip_portlist, uint16_ | |||
1087 | 966 | ||
1088 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) | 967 | static void punch_holes(IP ip, uint16_t * port_list, uint16_t numports, uint16_t friend_num) |
1089 | { | 968 | { |
1090 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) { | 969 | if(numports > MAX_FRIEND_CLIENTS || numports == 0) |
1091 | return; | 970 | return; |
1092 | } | ||
1093 | uint32_t i; | 971 | uint32_t i; |
1094 | uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; | 972 | uint32_t top = friends_list[friend_num].punching_index + MAX_PUNCHING_PORTS; |
1095 | for(i = friends_list[friend_num].punching_index; i != top; i++) { | 973 | for(i = friends_list[friend_num].punching_index; i != top; i++) { |
@@ -1112,9 +990,9 @@ static void doNAT() | |||
1112 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; | 990 | IP_Port ip_list[MAX_FRIEND_CLIENTS]; |
1113 | int num = friend_iplist(ip_list, i); | 991 | int num = friend_iplist(ip_list, i); |
1114 | /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ | 992 | /*If we are connected to friend or if friend is not online don't try to hole punch with him*/ |
1115 | if(num < MAX_FRIEND_CLIENTS/2) { | 993 | if(num < MAX_FRIEND_CLIENTS/2) |
1116 | continue; | 994 | continue; |
1117 | } | 995 | |
1118 | if(friends_list[i].hole_punching != 1) { | 996 | if(friends_list[i].hole_punching != 1) { |
1119 | if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { | 997 | if(friends_list[i].NATping_timestamp + PUNCH_INTERVAL < temp_time) { |
1120 | send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ | 998 | send_NATping(friends_list[i].client_id, friends_list[i].NATping_id, 0); /*0 is request*/ |
@@ -1123,9 +1001,9 @@ static void doNAT() | |||
1123 | } | 1001 | } |
1124 | else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { | 1002 | else if(friends_list[i].punching_timestamp + PUNCH_INTERVAL < temp_time) { |
1125 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); | 1003 | IP ip = NAT_commonip(ip_list, num, MAX_FRIEND_CLIENTS/2); |
1126 | if(ip.i == 0) { | 1004 | if(ip.i == 0) |
1127 | continue; | 1005 | continue; |
1128 | } | 1006 | |
1129 | uint16_t port_list[MAX_FRIEND_CLIENTS]; | 1007 | uint16_t port_list[MAX_FRIEND_CLIENTS]; |
1130 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); | 1008 | uint16_t numports = NAT_getports(port_list, ip_list, num, ip); |
1131 | punch_holes(ip, port_list, numports, i); | 1009 | punch_holes(ip, port_list, numports, i); |
@@ -1162,7 +1040,6 @@ int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | |||
1162 | } | 1040 | } |
1163 | 1041 | ||
1164 | return 0; | 1042 | return 0; |
1165 | |||
1166 | } | 1043 | } |
1167 | 1044 | ||
1168 | void doDHT() | 1045 | void doDHT() |
@@ -1190,12 +1067,10 @@ void DHT_save(uint8_t * data) | |||
1190 | return 0 if success */ | 1067 | return 0 if success */ |
1191 | int DHT_load(uint8_t * data, uint32_t size) | 1068 | int DHT_load(uint8_t * data, uint32_t size) |
1192 | { | 1069 | { |
1193 | if(size < sizeof(close_clientlist)) { | 1070 | if(size < sizeof(close_clientlist)) |
1194 | return -1; | 1071 | return -1; |
1195 | } | 1072 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) |
1196 | if((size - sizeof(close_clientlist)) % sizeof(Friend) != 0) { | ||
1197 | return -1; | 1073 | return -1; |
1198 | } | ||
1199 | uint32_t i, j; | 1074 | uint32_t i, j; |
1200 | /* uint32_t temp_time = unix_time(); */ | 1075 | /* uint32_t temp_time = unix_time(); */ |
1201 | uint16_t temp; | 1076 | uint16_t temp; |
@@ -1207,21 +1082,18 @@ int DHT_load(uint8_t * data, uint32_t size) | |||
1207 | 1082 | ||
1208 | for(i = 0; i < temp; ++i) { | 1083 | for(i = 0; i < temp; ++i) { |
1209 | DHT_addfriend(tempfriends_list[i].client_id); | 1084 | DHT_addfriend(tempfriends_list[i].client_id); |
1210 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 1085 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) |
1211 | if(tempfriends_list[i].client_list[j].timestamp != 0) { | 1086 | if(tempfriends_list[i].client_list[j].timestamp != 0) { |
1212 | getnodes(tempfriends_list[i].client_list[j].ip_port, | 1087 | getnodes(tempfriends_list[i].client_list[j].ip_port, |
1213 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); | 1088 | tempfriends_list[i].client_list[j].client_id, tempfriends_list[i].client_id); |
1214 | } | 1089 | } |
1215 | } | ||
1216 | } | 1090 | } |
1217 | } | 1091 | } |
1218 | Client_data * tempclose_clientlist = (Client_data *)data; | 1092 | Client_data * tempclose_clientlist = (Client_data *)data; |
1219 | 1093 | ||
1220 | for(i = 0; i < LCLIENT_LIST; ++i) { | 1094 | for(i = 0; i < LCLIENT_LIST; ++i) |
1221 | if(tempclose_clientlist[i].timestamp != 0) { | 1095 | if(tempclose_clientlist[i].timestamp != 0) |
1222 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); | 1096 | DHT_bootstrap(tempclose_clientlist[i].ip_port, tempclose_clientlist[i].client_id); |
1223 | } | ||
1224 | } | ||
1225 | return 0; | 1097 | return 0; |
1226 | } | 1098 | } |
1227 | 1099 | ||
@@ -1231,10 +1103,8 @@ int DHT_isconnected() | |||
1231 | { | 1103 | { |
1232 | uint32_t i; | 1104 | uint32_t i; |
1233 | uint32_t temp_time = unix_time(); | 1105 | uint32_t temp_time = unix_time(); |
1234 | for(i = 0; i < LCLIENT_LIST; ++i) { | 1106 | for(i = 0; i < LCLIENT_LIST; ++i) |
1235 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { | 1107 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) |
1236 | return 1; | 1108 | return 1; |
1237 | } | ||
1238 | } | ||
1239 | return 0; | 1109 | return 0; |
1240 | } \ No newline at end of file | 1110 | } |