From 388c0f38f7d2096efd1049b59851b00c448f940f Mon Sep 17 00:00:00 2001 From: irungentoo Date: Mon, 24 Jun 2013 08:59:42 -0400 Subject: [PATCH] More Functions. --- core/DHT.c | 67 +++++++++++++++++++++++++++++++++++++++++++----------- core/DHT.h | 3 ++- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/core/DHT.c b/core/DHT.c index 7c40e9fe..4b1c6f13 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -9,8 +9,42 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length) return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); } +//Attempt to add client with ip_port and client_id to the friends client list and close_clientlist +int addto_lists(IP_Port ip_port, char * client_id) +{ + + + +} +//send a ping request +//Currently incomplete: missing the ping_id part +int pingreq(IP_Port ip_port) +{ + char data[37]; + data[0] = 0; + + memcpy(data + 5, self_client_id, 32); + + sendpacket(ip_port, data, sizeof(data)); + +} + +//send a ping response +//Currently incomplete: missing the ping_id part +int pingres(IP_Port ip_port, uint32_t ping_id) +{ + char data[37]; + data[0] = 1; + + memcpy(data + 1, &ping_id, 4); + memcpy(data + 5, self_client_id, 32); + + sendpacket(ip_port, data, sizeof(data)); + +} + //send a getnodes request //Currently incomplete: missing the ping_id part int getnodes(IP_Port ip_port, char * client_id) @@ -24,26 +58,32 @@ int getnodes(IP_Port ip_port, char * client_id) sendpacket(ip_port, data, sizeof(data)); } -//send a ping request +//send a getnodes request //Currently incomplete: missing the ping_id part -int ping(IP_Port ip_port) +int sendnodes(IP_Port ip_port, char * client_id) { - char data[37]; - data[0] = 0; - - memcpy(data + 5, self_client_id, 32); - - sendpacket(ip_port, data, sizeof(data)); - + char data[325]; + data[0] = 3; + + memcpy(data + 5, self_client_id, 32); + memcpy(data + 37, client_id, 32); + + 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) { + uint32_t ping_id; + memcpy(&ping_id, packet + 1, 4); + + pingres(source, ping_id); } @@ -108,18 +148,20 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) switch (packet[0]) { case 0: handle_pingreq(packet, length, source); + //TODO: try to add requesting node to client_list if packet is valid break; case 1: handle_pingres(packet, length, source); break; case 2: handle_getnodes(packet, length, source); + //TODO: try to add requesting node to client_list if packet is valid break; case 3: handle_sendnodes(packet, length, source); break; default: - break; + return; } @@ -141,9 +183,8 @@ void doDHT() void bootstrap(IP_Port ip_port) { - - - + + getnodes(ip_port, self_client_id); } diff --git a/core/DHT.h b/core/DHT.h index 2e6dde6f..d89d0f76 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -116,9 +116,10 @@ char self_client_id[32]; //We only use one so it's much easier to have it as a global variable int sock; +//A list of the clients mathematically closest to ours. #define LCLIENT_LIST 32 +Client_data close_clientlist[LCLIENT_LIST]; -Client_data client_list[LCLIENT_LIST]; //Let's start with a static array for testing. Friend friends_list[256];