diff options
author | irungentoo <irungentoo@gmail.com> | 2013-06-24 16:50:43 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-06-24 16:50:43 -0400 |
commit | af1b3b7f9b3e669f1c8f5db095d9de1d2ccde256 (patch) | |
tree | ec2133af9e800de7c466fc8ad99dae222564c3cf /core | |
parent | 61e9103076c6780668b3d47df5d7c479dd287167 (diff) |
Made small application to test the DHT. Core DHT: finished some more functions.
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.c | 54 | ||||
-rw-r--r-- | core/DHT.h | 17 |
2 files changed, 58 insertions, 13 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 | ||
@@ -21,8 +21,11 @@ | |||
21 | //Current time, unix format | 21 | //Current time, unix format |
22 | #define unix_time() ((uint32_t)time(NULL)) | 22 | #define unix_time() ((uint32_t)time(NULL)) |
23 | 23 | ||
24 | //size of the client_id in bytes | ||
24 | #define CLIENT_ID_SIZE 32 | 25 | #define CLIENT_ID_SIZE 32 |
25 | 26 | ||
27 | #define MAX_UDP_PACKET_SIZE 65507 | ||
28 | |||
26 | typedef union | 29 | typedef union |
27 | { | 30 | { |
28 | uint8_t c[4]; | 31 | uint8_t c[4]; |
@@ -100,7 +103,9 @@ IP_Port getfriendip(char * client_id); | |||
100 | void doDHT(); | 103 | void doDHT(); |
101 | 104 | ||
102 | //if we recieve a DHT packet we call this function so it can be handled. | 105 | //if we recieve a DHT packet we call this function so it can be handled. |
103 | void DHT_recvpacket(char * packet, uint32_t length, IP_Port source); | 106 | //Return 0 if packet is handled correctly or if the packet was shit. |
107 | //return 1 if it didn't handle the packet. | ||
108 | int DHT_recvpacket(char * packet, uint32_t length, IP_Port source); | ||
104 | 109 | ||
105 | //Use this function to bootstrap the client | 110 | //Use this function to bootstrap the client |
106 | //Sends a get nodes request to the given ip port | 111 | //Sends a get nodes request to the given ip port |
@@ -139,3 +144,13 @@ Pinged pings[LPING_ARRAY]; | |||
139 | Pinged send_nodes[LSEND_NODES_ARRAY]; | 144 | Pinged send_nodes[LSEND_NODES_ARRAY]; |
140 | 145 | ||
141 | 146 | ||
147 | //Basic network functions: | ||
148 | //TODO: put them somewhere else than here | ||
149 | |||
150 | //Function to send packet(data) of length length to ip_port | ||
151 | int sendpacket(IP_Port ip_port, char * data, uint32_t length); | ||
152 | |||
153 | //Function to recieve data, ip and port of sender is put into ip_port | ||
154 | //the packet data into data | ||
155 | //the packet length into length. | ||
156 | int recievepacket(IP_Port * ip_port, char * data, uint32_t * length); \ No newline at end of file | ||