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 ++++++++++++++++- 2 files changed, 58 insertions(+), 13 deletions(-) (limited to 'core') 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 -- cgit v1.2.3