diff options
-rw-r--r-- | core/DHT.c | 67 | ||||
-rw-r--r-- | core/DHT.h | 3 |
2 files changed, 56 insertions, 14 deletions
@@ -9,8 +9,42 @@ int sendpacket(IP_Port ip_port, char * data, uint32_t length) | |||
9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 9 | return sendto(sock, data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
10 | } | 10 | } |
11 | 11 | ||
12 | //Attempt to add client with ip_port and client_id to the friends client list and close_clientlist | ||
13 | int addto_lists(IP_Port ip_port, char * client_id) | ||
14 | { | ||
15 | |||
16 | |||
17 | |||
18 | } | ||
12 | 19 | ||
13 | 20 | ||
21 | //send a ping request | ||
22 | //Currently incomplete: missing the ping_id part | ||
23 | int pingreq(IP_Port ip_port) | ||
24 | { | ||
25 | char data[37]; | ||
26 | data[0] = 0; | ||
27 | |||
28 | memcpy(data + 5, self_client_id, 32); | ||
29 | |||
30 | sendpacket(ip_port, data, sizeof(data)); | ||
31 | |||
32 | } | ||
33 | |||
34 | //send a ping response | ||
35 | //Currently incomplete: missing the ping_id part | ||
36 | int pingres(IP_Port ip_port, uint32_t ping_id) | ||
37 | { | ||
38 | char data[37]; | ||
39 | data[0] = 1; | ||
40 | |||
41 | memcpy(data + 1, &ping_id, 4); | ||
42 | memcpy(data + 5, self_client_id, 32); | ||
43 | |||
44 | sendpacket(ip_port, data, sizeof(data)); | ||
45 | |||
46 | } | ||
47 | |||
14 | //send a getnodes request | 48 | //send a getnodes request |
15 | //Currently incomplete: missing the ping_id part | 49 | //Currently incomplete: missing the ping_id part |
16 | int getnodes(IP_Port ip_port, char * client_id) | 50 | int getnodes(IP_Port ip_port, char * client_id) |
@@ -24,26 +58,32 @@ int getnodes(IP_Port ip_port, char * client_id) | |||
24 | sendpacket(ip_port, data, sizeof(data)); | 58 | sendpacket(ip_port, data, sizeof(data)); |
25 | } | 59 | } |
26 | 60 | ||
27 | //send a ping request | 61 | //send a getnodes request |
28 | //Currently incomplete: missing the ping_id part | 62 | //Currently incomplete: missing the ping_id part |
29 | int ping(IP_Port ip_port) | 63 | int sendnodes(IP_Port ip_port, char * client_id) |
30 | { | 64 | { |
31 | char data[37]; | 65 | char data[325]; |
32 | data[0] = 0; | 66 | data[0] = 3; |
33 | 67 | ||
34 | memcpy(data + 5, self_client_id, 32); | 68 | memcpy(data + 5, self_client_id, 32); |
35 | 69 | memcpy(data + 37, client_id, 32); | |
36 | sendpacket(ip_port, data, sizeof(data)); | 70 | |
37 | 71 | sendpacket(ip_port, data, sizeof(data)); | |
38 | } | 72 | } |
39 | 73 | ||
40 | 74 | ||
75 | |||
76 | |||
41 | //Packet handling functions | 77 | //Packet handling functions |
42 | //One to handle each types of packets | 78 | //One to handle each types of packets |
43 | 79 | ||
44 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) | 80 | int handle_pingreq(char * packet, uint32_t length, IP_Port source) |
45 | { | 81 | { |
82 | uint32_t ping_id; | ||
46 | 83 | ||
84 | memcpy(&ping_id, packet + 1, 4); | ||
85 | |||
86 | pingres(source, ping_id); | ||
47 | 87 | ||
48 | } | 88 | } |
49 | 89 | ||
@@ -108,18 +148,20 @@ void DHT_recvpacket(char * packet, uint32_t length, IP_Port source) | |||
108 | switch (packet[0]) { | 148 | switch (packet[0]) { |
109 | case 0: | 149 | case 0: |
110 | handle_pingreq(packet, length, source); | 150 | handle_pingreq(packet, length, source); |
151 | //TODO: try to add requesting node to client_list if packet is valid | ||
111 | break; | 152 | break; |
112 | case 1: | 153 | case 1: |
113 | handle_pingres(packet, length, source); | 154 | handle_pingres(packet, length, source); |
114 | break; | 155 | break; |
115 | case 2: | 156 | case 2: |
116 | handle_getnodes(packet, length, source); | 157 | handle_getnodes(packet, length, source); |
158 | //TODO: try to add requesting node to client_list if packet is valid | ||
117 | break; | 159 | break; |
118 | case 3: | 160 | case 3: |
119 | handle_sendnodes(packet, length, source); | 161 | handle_sendnodes(packet, length, source); |
120 | break; | 162 | break; |
121 | default: | 163 | default: |
122 | break; | 164 | return; |
123 | 165 | ||
124 | } | 166 | } |
125 | 167 | ||
@@ -141,9 +183,8 @@ void doDHT() | |||
141 | 183 | ||
142 | void bootstrap(IP_Port ip_port) | 184 | void bootstrap(IP_Port ip_port) |
143 | { | 185 | { |
144 | 186 | ||
145 | 187 | getnodes(ip_port, self_client_id); | |
146 | |||
147 | 188 | ||
148 | } | 189 | } |
149 | 190 | ||
@@ -116,9 +116,10 @@ char self_client_id[32]; | |||
116 | //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 |
117 | int sock; | 117 | int sock; |
118 | 118 | ||
119 | //A list of the clients mathematically closest to ours. | ||
119 | #define LCLIENT_LIST 32 | 120 | #define LCLIENT_LIST 32 |
121 | Client_data close_clientlist[LCLIENT_LIST]; | ||
120 | 122 | ||
121 | Client_data client_list[LCLIENT_LIST]; | ||
122 | 123 | ||
123 | //Let's start with a static array for testing. | 124 | //Let's start with a static array for testing. |
124 | Friend friends_list[256]; | 125 | Friend friends_list[256]; |