summaryrefslogtreecommitdiff
path: root/core/DHT.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/DHT.c')
-rw-r--r--core/DHT.c129
1 files changed, 114 insertions, 15 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 5a48c7b6..2071eb74 100644
--- a/core/DHT.c
+++ b/core/DHT.c
@@ -33,6 +33,7 @@ int id_closest(char * client_id, char * client_id1, char * client_id2)
33} 33}
34 34
35//check if client with client_id is already in list of length length. 35//check if client with client_id is already in list of length length.
36//if it is set it's corresponding timestamp to current time.
36//return True(1) or False(0) 37//return True(1) or False(0)
37int client_in_list(Client_data * list, uint32_t length, char * client_id) 38int client_in_list(Client_data * list, uint32_t length, char * client_id)
38{ 39{
@@ -49,6 +50,8 @@ int client_in_list(Client_data * list, uint32_t length, char * client_id)
49 } 50 }
50 if((j - 1) == CLIENT_ID_SIZE) 51 if((j - 1) == CLIENT_ID_SIZE)
51 { 52 {
53 //Refresh the client timestamp.
54 list[i].timestamp = unix_time();
52 return 1; 55 return 1;
53 } 56 }
54 } 57 }
@@ -128,21 +131,110 @@ int addto_lists(IP_Port ip_port, char * client_id)
128} 131}
129 132
130 133
134//ping timeout in seconds
135#define PING_TIMEOUT 10
136//check if we are currently pinging an ip_port
137//if we are already, return 1
138//else return 0
139//TODO: Maybe optimize this
140int is_pinging(IP_Port ip_port)
141{
142 uint32_t i;
143
144 for(i = 0; i < LPING_ARRAY; i++ )
145 {
146 if((pings[i].timestamp + PING_TIMEOUT) > unix_time() && pings[i].ip_port == ip_port)
147 {
148 return 1;
149 }
150 }
151
152 return 0;
153
154}
155
156
157//Same as last function but for get_node requests.
158int is_gettingnodes(IP_Port ip_port)
159{
160 uint32_t i;
161
162 for(i = 0; i < LSEND_NODES_ARRAY; i++ )
163 {
164 if((send_nodes[i].timestamp + PING_TIMEOUT) > unix_time() && send_nodes[i].ip_port == ip_port)
165 {
166 return 1;
167 }
168 }
169
170 return 0;
171
172}
173
174//Add a new ping request to the list of ping requests
175//returns the ping_id to put in the ping request
176//TODO: Maybe optimize this
177int add_pinging(IP_Port ip_port)
178{
179 uint32_t i, j;
180 int ping_id = rand();
181 for(i = 0; i < PING_TIMEOUT; i++ )
182 {
183 for(j = 0; j < LPING_ARRAY; j++ )
184 {
185 if((pings[j].timestamp + PING_TIMEOUT - i) < unix_time())
186 {
187 pings[j].timestamp = unix_time();
188 pings[j].ip_port = ip_port;
189 pings[j].ping_id = ping_id;
190 return ping_id;
191 }
192 }
193 }
194}
195
196//Same but for get node requests
197int add_gettingnodes(IP_Port ip_port)
198{
199 uint32_t i, j;
200 int ping_id = rand();
201 for(i = 0; i < PING_TIMEOUT; i++ )
202 {
203 for(j = 0; j < LSEND_NODES_ARRAY; j++ )
204 {
205 if((send_nodes[j].timestamp + PING_TIMEOUT - i) < unix_time())
206 {
207 send_nodes[j].timestamp = unix_time();
208 send_nodes[j].ip_port = ip_port;
209 send_nodes[j].ping_id = ping_id;
210 return ping_id;
211 }
212 }
213 }
214}
215
216
131//send a ping request 217//send a ping request
132//Currently incomplete: missing the ping_id part
133int pingreq(IP_Port ip_port) 218int pingreq(IP_Port ip_port)
134{ 219{
220 if(is_pinging(ip_port))
221 {
222 return 1;
223 }
224
225 int ping_id = add_pinging(ip_port);
226
135 char data[5 + CLIENT_ID_SIZE]; 227 char data[5 + CLIENT_ID_SIZE];
136 data[0] = 0; 228 data[0] = 0;
137 229 memcpy(data + 1, &ping_id, 4);
138 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); 230 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
139 231
140 sendpacket(ip_port, data, sizeof(data)); 232 return sendpacket(ip_port, data, sizeof(data));
141 233
142} 234}
143 235
236
144//send a ping response 237//send a ping response
145//Currently incomplete: missing the ping_id part
146int pingres(IP_Port ip_port, uint32_t ping_id) 238int pingres(IP_Port ip_port, uint32_t ping_id)
147{ 239{
148 char data[5 + CLIENT_ID_SIZE]; 240 char data[5 + CLIENT_ID_SIZE];
@@ -151,25 +243,32 @@ int pingres(IP_Port ip_port, uint32_t ping_id)
151 memcpy(data + 1, &ping_id, 4); 243 memcpy(data + 1, &ping_id, 4);
152 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); 244 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
153 245
154 sendpacket(ip_port, data, sizeof(data)); 246 return sendpacket(ip_port, data, sizeof(data));
155 247
156} 248}
157 249
158//send a getnodes request 250//send a getnodes request
159//Currently incomplete: missing the ping_id part
160int getnodes(IP_Port ip_port, char * client_id) 251int getnodes(IP_Port ip_port, char * client_id)
161{ 252{
162 char data[5 + CLIENT_ID_SIZE*2]; 253 if(is_gettingnodes(ip_port))
163 data[0] = 2; 254 {
164 255 return 1;
165 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE); 256 }
166 memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE); 257
167 258 int ping_id = add_pinging(ip_port);
168 sendpacket(ip_port, data, sizeof(data)); 259
260 char data[5 + CLIENT_ID_SIZE*2];
261 data[0] = 2;
262
263 memcpy(data + 1, &ping_id, 4);
264 memcpy(data + 5, self_client_id, CLIENT_ID_SIZE);
265 memcpy(data + 5 + CLIENT_ID_SIZE, client_id, CLIENT_ID_SIZE);
266
267 return sendpacket(ip_port, data, sizeof(data));
169} 268}
170 269
171//send a getnodes request 270//send a send nodes response
172//Currently incomplete: missing the ping_id part 271//Currently incomplete: missing bunch of stuff
173int sendnodes(IP_Port ip_port, char * client_id) 272int sendnodes(IP_Port ip_port, char * client_id)
174{ 273{
175 char data[5 + (CLIENT_ID_SIZE + 6)*8]; 274 char data[5 + (CLIENT_ID_SIZE + 6)*8];