summaryrefslogtreecommitdiff
path: root/testing/DHT_test.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/DHT_test.c')
-rw-r--r--testing/DHT_test.c75
1 files changed, 72 insertions, 3 deletions
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