From af1b3b7f9b3e669f1c8f5db095d9de1d2ccde256 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 24 Jun 2013 16:50:43 -0400 Subject: Made small application to test the DHT. Core DHT: finished some more functions. --- core/DHT.c | 54 ++++++++++++++++++++++++++++++--------- core/DHT.h | 17 ++++++++++++- testing/DHT_test.c | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 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 @@ #include "DHT.h" +//Basic network functions: +//TODO: put them somewhere else than here + //Function to send packet(data) of length length to ip_port int sendpacket(IP_Port ip_port, char * data, uint32_t length) { @@ -9,6 +12,26 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length) return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); } +//Function to recieve data, ip and port of sender is put into ip_port +//the packet data into data +//the packet length into length. +int recievepacket(IP_Port * ip_port, char * data, uint32_t * length) +{ + ADDR addr; + uint32_t addrlen = sizeof(addr); + (*(int *)length) = recvfrom(sock, data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); + if(*(int *)length == -1) + { + //nothing recieved + return -1; + } + ip_port->ip = addr.ip; + ip_port->port = addr.port; + return 0; +} + + + //Compares client_id1 and client_id2 with client_id //return 0 if both are same distance //return 1 if client_id1 is closer. @@ -288,7 +311,7 @@ int sendnodes(IP_Port ip_port, char * client_id) //Packet handling functions -//One to handle each types of packets +//One to handle each types of packets we recieve int handle_pingreq(char * packet, uint32_t length, IP_Port source) { @@ -340,16 +363,16 @@ int handle_sendnodes(char * packet, uint32_t length, IP_Port source) } - +//END of packet handling functions void addfriend(char * client_id) { - - - - + //TODO: Make the array of friends dynamic instead of a static array with 256 places.. + //WARNING:This will segfault if the number of friends exceeds 256. + memcpy(friends_list[num_friends].client_id, client_id, CLIENT_ID_SIZE); + num_friends++; } @@ -358,10 +381,17 @@ void addfriend(char * client_id) char delfriend(char * client_id) { - - - - + uint32_t i; + for(i = 0; i < num_friends; i++) + { + if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0)//Equal + { + memcpy(friends_list[num_friends].client_id, friends_list[i].client_id, CLIENT_ID_SIZE); + num_friends--; + return 0; + } + } + return 1; } @@ -378,7 +408,7 @@ IP_Port getfriendip(char * client_id) -void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) +int DHT_recvpacket(char * packet, uint32_t length, IP_Port source) { switch (packet[0]) { case 0: @@ -394,7 +424,7 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) handle_sendnodes(packet, length, source); break; default: - return; + return 1; } 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 @@ //Current time, unix format #define unix_time() ((uint32_t)time(NULL)) +//size of the client_id in bytes #define CLIENT_ID_SIZE 32 +#define MAX_UDP_PACKET_SIZE 65507 + typedef union { uint8_t c[4]; @@ -100,7 +103,9 @@ IP_Port getfriendip(char * client_id); void doDHT(); //if we recieve a DHT packet we call this function so it can be handled. -void DHT_recvpacket(char * packet, uint32_t length, IP_Port source); +//Return 0 if packet is handled correctly or if the packet was shit. +//return 1 if it didn't handle the packet. +int DHT_recvpacket(char * packet, uint32_t length, IP_Port source); //Use this function to bootstrap the client //Sends a get nodes request to the given ip port @@ -139,3 +144,13 @@ Pinged pings[LPING_ARRAY]; Pinged send_nodes[LSEND_NODES_ARRAY]; +//Basic network functions: +//TODO: put them somewhere else than here + +//Function to send packet(data) of length length to ip_port +int sendpacket(IP_Port ip_port, char * data, uint32_t length); + +//Function to recieve data, ip and port of sender is put into ip_port +//the packet data into data +//the packet length into length. +int 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 @@ * A file with a main that runs our DHT for testing. * * Compile with: gcc -Wall -o test ../core/DHT.c DHT_test.c + * + * Command line arguments are the ip and port of a node + * EX: ./test 127.0.0.1 33445 */ #include "../core/DHT.h" -#define PORT 45344 +//Sleep function (x = milliseconds) +#ifdef WIN32 + +#define c_sleep(x) Sleep(1*x) + +#else +#include +#define c_sleep(x) usleep(1000*x) + +#endif + +#define PORT 33445 -int main() + + +int main(int argc, char *argv[]) { + if (argc < 3) { + printf("usage %s ip port\n", argv[0]); + exit(0); + } + + //initialize our socket + sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + //Set socket nonblocking + #ifdef WIN32 + //I think this works for windows + ioctl(sock, FIONBIO, &mode); + #else + fcntl(sock, F_SETFL, O_NONBLOCK, 1); + #endif + + //Bind our socket to port PORT and address 0.0.0.0 + ADDR addr = {.family = AF_INET, .ip.i = 0, .port = htons(PORT)}; + bind(sock, (struct sockaddr*)&addr, sizeof(addr)); + + IP_Port bootstrap_ip_port; + bootstrap_ip_port.port = htons(atoi(argv[2])); + //bootstrap_ip_port.ip.c[0] = 127; + //bootstrap_ip_port.ip.c[1] = 0; + //bootstrap_ip_port.ip.c[2] = 0; + //bootstrap_ip_port.ip.c[3] = 1; + bootstrap_ip_port.ip.i = inet_addr(argv[1]); + bootstrap(bootstrap_ip_port); + IP_Port ip_port; + char data[MAX_UDP_PACKET_SIZE]; + uint32_t length; + uint32_t i; - return 0; + while(1) + { + + doDHT(); + + if(recievepacket(&ip_port, data, &length) != -1) + { + if(DHT_recvpacket(data, length, ip_port)) + { + printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); + printf("--------------------BEGIN-----------------------------\n"); + for(i = 0; i < length; i++) + printf("%c",data[i]); + printf("\n--------------------END-----------------------------\n\n\n"); + } + else + { + printf("Received handled packet with length: %u\n", length); + } + } + c_sleep(100); + } + return 0; } \ No newline at end of file -- cgit v1.2.3