Now compiles. Functions starting to take form.

This commit is contained in:
irungentoo 2013-06-24 08:28:19 -04:00
parent baf14f8121
commit 7e341fb171
3 changed files with 114 additions and 15 deletions

View File

@ -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;
memcpy(data + 5, self_client_id, 32);
//ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port};
data[0] = 0;
memcpy(data + 5, self_client_id, 32);
sendpacket(ip_port, data, sizeof(data));
//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);
}
//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;
}
}

View File

@ -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 send_nodes[64];
Pinged pings[LPING_ARRAY];
#define LSEND_NODES_ARRAY LPING_ARRAY/2
Pinged send_nodes[LSEND_NODES_ARRAY];

17
testing/DHT_test.c Normal file
View File

@ -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;
}