diff options
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 54 |
1 files changed, 42 insertions, 12 deletions
@@ -1,6 +1,9 @@ | |||
1 | #include "DHT.h" | 1 | #include "DHT.h" |
2 | 2 | ||
3 | 3 | ||
4 | //Basic network functions: | ||
5 | //TODO: put them somewhere else than here | ||
6 | |||
4 | //Function to send packet(data) of length length to ip_port | 7 | //Function to send packet(data) of length length to ip_port |
5 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) | 8 | int sendpacket(IP_Port ip_port, char * data, uint32_t length) |
6 | { | 9 | { |
@@ -9,6 +12,26 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length) | |||
9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 12 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
10 | } | 13 | } |
11 | 14 | ||
15 | //Function to recieve data, ip and port of sender is put into ip_port | ||
16 | //the packet data into data | ||
17 | //the packet length into length. | ||
18 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) | ||
19 | { | ||
20 | ADDR addr; | ||
21 | uint32_t addrlen = sizeof(addr); | ||
22 | (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | ||
23 | if(*(int *)length == -1) | ||
24 | { | ||
25 | //nothing recieved | ||
26 | return -1; | ||
27 | } | ||
28 | ip_port->ip = addr.ip; | ||
29 | ip_port->port = addr.port; | ||
30 | return 0; | ||
31 | } | ||
32 | |||
33 | |||
34 | |||
12 | //Compares client_id1 and client_id2 with client_id | 35 | //Compares client_id1 and client_id2 with client_id |
13 | //return 0 if both are same distance | 36 | //return 0 if both are same distance |
14 | //return 1 if client_id1 is closer. | 37 | //return 1 if client_id1 is closer. |
@@ -288,7 +311,7 @@ int sendnodes(IP_Port ip_port, char * client_id) | |||
288 | 311 | ||
289 | 312 | ||
290 | //Packet handling functions | 313 | //Packet handling functions |
291 | //One to handle each types of packets | 314 | //One to handle each types of packets we recieve |
292 | 315 | ||
293 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) | 316 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) |
294 | { | 317 | { |
@@ -340,16 +363,16 @@ int handle_sendnodes(char * packet, uint32_t length, IP_Port source) | |||
340 | 363 | ||
341 | } | 364 | } |
342 | 365 | ||
343 | 366 | //END of packet handling functions | |
344 | 367 | ||
345 | 368 | ||
346 | 369 | ||
347 | void addfriend(char * client_id) | 370 | void addfriend(char * client_id) |
348 | { | 371 | { |
349 | 372 | //TODO: Make the array of friends dynamic instead of a static array with 256 places.. | |
350 | 373 | //WARNING:This will segfault if the number of friends exceeds 256. | |
351 | 374 | memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); | |
352 | 375 | num_friends++; | |
353 | } | 376 | } |
354 | 377 | ||
355 | 378 | ||
@@ -358,10 +381,17 @@ void addfriend(char * client_id) | |||
358 | 381 | ||
359 | char delfriend(char * client_id) | 382 | char delfriend(char * client_id) |
360 | { | 383 | { |
361 | 384 | uint32_t i; | |
362 | 385 | for(i = 0; i < num_friends; i++) | |
363 | 386 | { | |
364 | 387 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal | |
388 | { | ||
389 | memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE); | ||
390 | num_friends--; | ||
391 | return 0; | ||
392 | } | ||
393 | } | ||
394 | return 1; | ||
365 | } | 395 | } |
366 | 396 | ||
367 | 397 | ||
@@ -378,7 +408,7 @@ IP_Port getfriendip(char * client_id) | |||
378 | 408 | ||
379 | 409 | ||
380 | 410 | ||
381 | void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) | 411 | int DHT_recvpacket(char * packet, uint32_t length, IP_Port source) |
382 | { | 412 | { |
383 | switch (packet[0]) { | 413 | switch (packet[0]) { |
384 | case 0: | 414 | case 0: |
@@ -394,7 +424,7 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) | |||
394 | handle_sendnodes(packet, length, source); | 424 | handle_sendnodes(packet, length, source); |
395 | break; | 425 | break; |
396 | default: | 426 | default: |
397 | return; | 427 | return 1; |
398 | 428 | ||
399 | } | 429 | } |
400 | 430 | ||