summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-06-24 16:50:43 -0400
committerirungentoo <irungentoo@gmail.com>2013-06-24 16:50:43 -0400
commitaf1b3b7f9b3e669f1c8f5db095d9de1d2ccde256 (patch)
treeec2133af9e800de7c466fc8ad99dae222564c3cf
parent61e9103076c6780668b3d47df5d7c479dd287167 (diff)
Made small application to test the DHT. Core DHT: finished some more functions.
-rw-r--r--core/DHT.c54
-rw-r--r--core/DHT.h17
-rw-r--r--testing/DHT_test.c75
3 files changed, 130 insertions, 16 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 9f2d1f56..911e23e7 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -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
5int sendpacket(IP_Port ip_port, char * data, uint32_t length) 8int 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.
18int 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
293int handle_pingreq(char * packet, uint32_t length, IP_Port source) 316int 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
347void addfriend(char * client_id) 370void 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
359char delfriend(char * client_id) 382char 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
381void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) 411int 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
diff --git a/core/DHT.h b/core/DHT.h
index 0acc6602..7493761a 100644
--- a/core/DHT.h
+++ b/core/DHT.h
@@ -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
26typedef union 29typedef union
27{ 30{
28 uint8_t c[4]; 31 uint8_t c[4];
@@ -100,7 +103,9 @@ IP_Port getfriendip(char * client_id);
100void doDHT(); 103void 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.
103void 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.
108int 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];
139Pinged send_nodes[LSEND_NODES_ARRAY]; 144Pinged 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
151int 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.
156int recievepacket(IP_Port * ip_port, char * data, uint32_t * length); \ No newline at end of file
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index 8c5d2cac..a7c61e7f 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -2,16 +2,85 @@
2 * A file with a main that runs our DHT for testing. 2 * A file with a main that runs our DHT for testing.
3 * 3 *
4 * Compile with: gcc -Wall -o test ../core/DHT.c DHT_test.c 4 * Compile with: gcc -Wall -o test ../core/DHT.c DHT_test.c
5 *
6 * Command line arguments are the ip and port of a node
7 * EX: ./test 127.0.0.1 33445
5 */ 8 */
6 9
7#include "../core/DHT.h" 10#include "../core/DHT.h"
8 11
9#define PORT 45344 12//Sleep function (x = milliseconds)
13#ifdef WIN32
14
15#define c_sleep(x) Sleep(1*x)
16
17#else
18#include <unistd.h>
19#define c_sleep(x) usleep(1000*x)
20
21#endif
22
23#define PORT 33445
10 24
11int main() 25
26
27int main(int argc, char *argv[])
12{ 28{
29 if (argc < 3) {
30 printf("usage %s ip port\n", argv[0]);
31 exit(0);
32 }
33
34 //initialize our socket
35 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
36 //Set socket nonblocking
37 #ifdef WIN32
38 //I think this works for windows
39 ioctl(sock, FIONBIO, &mode);
40 #else
41 fcntl(sock, F_SETFL, O_NONBLOCK, 1);
42 #endif
43
44 //Bind our socket to port PORT and address 0.0.0.0
45 ADDR addr = {.family = AF_INET, .ip.i = 0, .port = htons(PORT)};
46 bind(sock, (struct sockaddr*)&addr, sizeof(addr));
47
48 IP_Port bootstrap_ip_port;
49 bootstrap_ip_port.port = htons(atoi(argv[2]));
50 //bootstrap_ip_port.ip.c[0] = 127;
51 //bootstrap_ip_port.ip.c[1] = 0;
52 //bootstrap_ip_port.ip.c[2] = 0;
53 //bootstrap_ip_port.ip.c[3] = 1;
54 bootstrap_ip_port.ip.i = inet_addr(argv[1]);
55 bootstrap(bootstrap_ip_port);
13 56
57 IP_Port ip_port;
58 char data[MAX_UDP_PACKET_SIZE];
59 uint32_t length;
14 60
61 uint32_t i;
15 62
16 return 0; 63 while(1)
64 {
65
66 doDHT();
67
68 if(recievepacket(&ip_port, data, &length) != -1)
69 {
70 if(DHT_recvpacket(data, length, ip_port))
71 {
72 printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length);
73 printf("--------------------BEGIN-----------------------------\n");
74 for(i = 0; i < length; i++)
75 printf("%c",data[i]);
76 printf("\n--------------------END-----------------------------\n\n\n");
77 }
78 else
79 {
80 printf("Received handled packet with length: %u\n", length);
81 }
82 }
83 c_sleep(100);
84 }
85 return 0;
17} \ No newline at end of file 86} \ No newline at end of file