From 7e341fb171e360e7b1218fd2003e6a1d27863120 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 24 Jun 2013 08:28:19 -0400 Subject: Now compiles. Functions starting to take form. --- core/DHT.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++------- core/DHT.h | 27 +++++++++++++++--- testing/DHT_test.c | 17 +++++++++++ 3 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 testing/DHT_test.c diff --git a/core/DHT.c b/core/DHT.c index c6ea6a22..7c40e9fe 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -1,12 +1,27 @@ #include "DHT.h" -//send a getnodes request -int getnodes() + +//Function to send packet(data) of length length to ip_port +int sendpacket(IP_Port ip_port, char * data, uint32_t length) { + ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port}; - - - + return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); +} + + + +//send a getnodes request +//Currently incomplete: missing the ping_id part +int getnodes(IP_Port ip_port, char * client_id) +{ + char data[69]; + data[0] = 2; + + memcpy(data + 5, self_client_id, 32); + memcpy(data + 37, client_id, 32); + + sendpacket(ip_port, data, sizeof(data)); } //send a ping request @@ -14,15 +29,45 @@ int getnodes() int ping(IP_Port ip_port) { char data[37]; - data[0] = 00; + data[0] = 0; + memcpy(data + 5, self_client_id, 32); -//ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port}; -//return sendto(sock, data, sizeof(data) - 1, 0, (struct sockaddr *)&addr, addrlen); - //sendto(int socket_descriptor, char *buffer, int buffer_length, int flags, struct sockaddr *destination_address, int address_length); + sendpacket(ip_port, data, sizeof(data)); + } +//Packet handling functions +//One to handle each types of packets + +int handle_pingreq(char * packet, uint32_t length, IP_Port source) +{ + + +} + +int handle_pingres(char * packet, uint32_t length, IP_Port source) +{ + + +} + +int handle_getnodes(char * packet, uint32_t length, IP_Port source) +{ + + +} + +int handle_sendnodes(char * packet, uint32_t length, IP_Port source) +{ + + +} + + + + void addfriend(char * client_id) { @@ -58,8 +103,26 @@ IP_Port getfriendip(char * client_id) -void DHT_recvpacket(char * packet, uint32_t length) +void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) { + switch (packet[0]) { + case 0: + handle_pingreq(packet, length, source); + break; + case 1: + handle_pingres(packet, length, source); + break; + case 2: + handle_getnodes(packet, length, source); + break; + case 3: + handle_sendnodes(packet, length, source); + break; + default: + break; + + } + } diff --git a/core/DHT.h b/core/DHT.h index f0ccd184..2e6dde6f 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -59,6 +59,19 @@ typedef struct }Pinged; +typedef struct +{ + int16_t family; + uint16_t port; + IP ip; + uint8_t zeroes[8]; + #ifdef ENABLE_IPV6 + uint8_t zeroes2[12]; + #endif +}ADDR; + + + //Add a new friend to the friends list //client_id must be 32 bytes long. void addfriend(char * client_id); @@ -83,7 +96,7 @@ 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); +void 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 @@ -103,7 +116,9 @@ char self_client_id[32]; //We only use one so it's much easier to have it as a global variable int sock; -Client_data client_list[32]; +#define LCLIENT_LIST 32 + +Client_data client_list[LCLIENT_LIST]; //Let's start with a static array for testing. Friend friends_list[256]; @@ -111,8 +126,12 @@ uint16_t num_friends; //The list of ip ports along with the ping_id of what we sent them and a timestamp //TODO: make this more efficient looping up to 128 times is a bit... -Pinged pings[128]; +#define LPING_ARRAY 128 + +Pinged pings[LPING_ARRAY]; + +#define LSEND_NODES_ARRAY LPING_ARRAY/2 -Pinged send_nodes[64]; +Pinged send_nodes[LSEND_NODES_ARRAY]; diff --git a/testing/DHT_test.c b/testing/DHT_test.c new file mode 100644 index 00000000..8c5d2cac --- /dev/null +++ b/testing/DHT_test.c @@ -0,0 +1,17 @@ +/* DHT test + * A file with a main that runs our DHT for testing. + * + * Compile with: gcc -Wall -o test ../core/DHT.c DHT_test.c + */ + +#include "../core/DHT.h" + +#define PORT 45344 + +int main() +{ + + + + return 0; +} \ No newline at end of file -- cgit v1.2.3