summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-06-24 08:28:19 -0400
committerirungentoo <irungentoo@gmail.com>2013-06-24 08:28:19 -0400
commit7e341fb171e360e7b1218fd2003e6a1d27863120 (patch)
tree13d0eedb9293df5fe1b611f525fa14765b974c9c
parentbaf14f81211dbc63aac13d76ef0fbbacc3391950 (diff)
Now compiles. Functions starting to take form.
-rw-r--r--core/DHT.c83
-rw-r--r--core/DHT.h27
-rw-r--r--testing/DHT_test.c17
3 files changed, 113 insertions, 14 deletions
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 @@
1#include "DHT.h" 1#include "DHT.h"
2 2
3//send a getnodes request 3
4int getnodes() 4//Function to send packet(data) of length length to ip_port
5int sendpacket(IP_Port ip_port, char * data, uint32_t length)
5{ 6{
7 ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port};
6 8
7 9 return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
8 10}
9 11
12
13
14//send a getnodes request
15//Currently incomplete: missing the ping_id part
16int getnodes(IP_Port ip_port, char * client_id)
17{
18 char data[69];
19 data[0] = 2;
20
21 memcpy(data + 5, self_client_id, 32);
22 memcpy(data + 37, client_id, 32);
23
24 sendpacket(ip_port, data, sizeof(data));
10} 25}
11 26
12//send a ping request 27//send a ping request
@@ -14,15 +29,45 @@ int getnodes()
14int ping(IP_Port ip_port) 29int ping(IP_Port ip_port)
15{ 30{
16 char data[37]; 31 char data[37];
17 data[0] = 00; 32 data[0] = 0;
33
18 memcpy(data + 5, self_client_id, 32); 34 memcpy(data + 5, self_client_id, 32);
19//ADDR addr = {.family = AF_INET, .ip = ip_port.ip, .port = ip_port.port};
20 35
21//return sendto(sock, data, sizeof(data) - 1, 0, (struct sockaddr *)&addr, addrlen); 36 sendpacket(ip_port, data, sizeof(data));
22 //sendto(int socket_descriptor, char *buffer, int buffer_length, int flags, struct sockaddr *destination_address, int address_length); 37
23} 38}
24 39
25 40
41//Packet handling functions
42//One to handle each types of packets
43
44int handle_pingreq(char * packet, uint32_t length, IP_Port source)
45{
46
47
48}
49
50int handle_pingres(char * packet, uint32_t length, IP_Port source)
51{
52
53
54}
55
56int handle_getnodes(char * packet, uint32_t length, IP_Port source)
57{
58
59
60}
61
62int handle_sendnodes(char * packet, uint32_t length, IP_Port source)
63{
64
65
66}
67
68
69
70
26 71
27void addfriend(char * client_id) 72void addfriend(char * client_id)
28{ 73{
@@ -58,8 +103,26 @@ IP_Port getfriendip(char * client_id)
58 103
59 104
60 105
61void DHT_recvpacket(char * packet, uint32_t length) 106void DHT_recvpacket(char * packet, uint32_t length, IP_Port source)
62{ 107{
108 switch (packet[0]) {
109 case 0:
110 handle_pingreq(packet, length, source);
111 break;
112 case 1:
113 handle_pingres(packet, length, source);
114 break;
115 case 2:
116 handle_getnodes(packet, length, source);
117 break;
118 case 3:
119 handle_sendnodes(packet, length, source);
120 break;
121 default:
122 break;
123
124 }
125
63 126
64} 127}
65 128
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
59}Pinged; 59}Pinged;
60 60
61 61
62typedef struct
63{
64 int16_t family;
65 uint16_t port;
66 IP ip;
67 uint8_t zeroes[8];
68 #ifdef ENABLE_IPV6
69 uint8_t zeroes2[12];
70 #endif
71}ADDR;
72
73
74
62//Add a new friend to the friends list 75//Add a new friend to the friends list
63//client_id must be 32 bytes long. 76//client_id must be 32 bytes long.
64void addfriend(char * client_id); 77void addfriend(char * client_id);
@@ -83,7 +96,7 @@ IP_Port getfriendip(char * client_id);
83void doDHT(); 96void doDHT();
84 97
85//if we recieve a DHT packet we call this function so it can be handled. 98//if we recieve a DHT packet we call this function so it can be handled.
86void DHT_recvpacket(char * packet, uint32_t length); 99void DHT_recvpacket(char * packet, uint32_t length, IP_Port source);
87 100
88//Use this function to bootstrap the client 101//Use this function to bootstrap the client
89//Sends a get nodes request to the given ip port 102//Sends a get nodes request to the given ip port
@@ -103,7 +116,9 @@ char self_client_id[32];
103//We only use one so it's much easier to have it as a global variable 116//We only use one so it's much easier to have it as a global variable
104int sock; 117int sock;
105 118
106Client_data client_list[32]; 119#define LCLIENT_LIST 32
120
121Client_data client_list[LCLIENT_LIST];
107 122
108//Let's start with a static array for testing. 123//Let's start with a static array for testing.
109Friend friends_list[256]; 124Friend friends_list[256];
@@ -111,8 +126,12 @@ uint16_t num_friends;
111 126
112//The list of ip ports along with the ping_id of what we sent them and a timestamp 127//The list of ip ports along with the ping_id of what we sent them and a timestamp
113//TODO: make this more efficient looping up to 128 times is a bit... 128//TODO: make this more efficient looping up to 128 times is a bit...
114Pinged pings[128]; 129#define LPING_ARRAY 128
130
131Pinged pings[LPING_ARRAY];
132
133#define LSEND_NODES_ARRAY LPING_ARRAY/2
115 134
116Pinged send_nodes[64]; 135Pinged send_nodes[LSEND_NODES_ARRAY];
117 136
118 137
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 @@
1/* DHT test
2 * A file with a main that runs our DHT for testing.
3 *
4 * Compile with: gcc -Wall -o test ../core/DHT.c DHT_test.c
5 */
6
7#include "../core/DHT.h"
8
9#define PORT 45344
10
11int main()
12{
13
14
15
16 return 0;
17} \ No newline at end of file