diff options
author | Konstantin Kowalski <kostyakow42@gmail.com> | 2013-07-26 23:07:25 -0400 |
---|---|---|
committer | Konstantin Kowalski <kostyakow42@gmail.com> | 2013-07-26 23:07:25 -0400 |
commit | 241aca98bdc8106221ee7d7dbcea9f2fa17f24bc (patch) | |
tree | 15c324c865eb8c61c17dc442009f33b24505714d /core/DHT.c | |
parent | 1b4ea2e1aeb874e872a2c767326633450de12d20 (diff) |
A *lot* of style changes.
Diffstat (limited to 'core/DHT.c')
-rw-r--r-- | core/DHT.c | 367 |
1 files changed, 155 insertions, 212 deletions
@@ -23,8 +23,7 @@ | |||
23 | 23 | ||
24 | #include "DHT.h" | 24 | #include "DHT.h" |
25 | 25 | ||
26 | typedef struct | 26 | typedef struct { |
27 | { | ||
28 | uint8_t client_id[CLIENT_ID_SIZE]; | 27 | uint8_t client_id[CLIENT_ID_SIZE]; |
29 | IP_Port ip_port; | 28 | IP_Port ip_port; |
30 | uint32_t timestamp; | 29 | uint32_t timestamp; |
@@ -32,13 +31,12 @@ typedef struct | |||
32 | IP_Port ret_ip_port;/* The ip_port returned by this node for the friend | 31 | IP_Port ret_ip_port;/* The ip_port returned by this node for the friend |
33 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ | 32 | (for nodes in friends_list) or us (for nodes in close_clientlist) */ |
34 | uint32_t ret_timestamp; | 33 | uint32_t ret_timestamp; |
35 | }Client_data; | 34 | } Client_data; |
36 | 35 | ||
37 | /* maximum number of clients stored per friend. */ | 36 | /* maximum number of clients stored per friend. */ |
38 | #define MAX_FRIEND_CLIENTS 8 | 37 | #define MAX_FRIEND_CLIENTS 8 |
39 | 38 | ||
40 | typedef struct | 39 | typedef struct { |
41 | { | ||
42 | uint8_t client_id[CLIENT_ID_SIZE]; | 40 | uint8_t client_id[CLIENT_ID_SIZE]; |
43 | Client_data client_list[MAX_FRIEND_CLIENTS]; | 41 | Client_data client_list[MAX_FRIEND_CLIENTS]; |
44 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ | 42 | uint32_t lastgetnode; /* time at which the last get_nodes request was sent. */ |
@@ -49,21 +47,19 @@ typedef struct | |||
49 | uint32_t punching_timestamp; | 47 | uint32_t punching_timestamp; |
50 | uint64_t NATping_id; | 48 | uint64_t NATping_id; |
51 | uint32_t NATping_timestamp; | 49 | uint32_t NATping_timestamp; |
52 | }Friend; | 50 | } Friend; |
53 | 51 | ||
54 | typedef struct | 52 | typedef struct { |
55 | { | ||
56 | uint8_t client_id[CLIENT_ID_SIZE]; | 53 | uint8_t client_id[CLIENT_ID_SIZE]; |
57 | IP_Port ip_port; | 54 | IP_Port ip_port; |
58 | }Node_format; | 55 | } Node_format; |
59 | 56 | ||
60 | typedef struct | 57 | typedef struct { |
61 | { | ||
62 | IP_Port ip_port; | 58 | IP_Port ip_port; |
63 | uint64_t ping_id; | 59 | uint64_t ping_id; |
64 | uint32_t timestamp; | 60 | uint32_t timestamp; |
65 | 61 | ||
66 | }Pinged; | 62 | } Pinged; |
67 | 63 | ||
68 | /* Our client id/public key */ | 64 | /* Our client id/public key */ |
69 | uint8_t self_public_key[CLIENT_ID_SIZE]; | 65 | uint8_t self_public_key[CLIENT_ID_SIZE]; |
@@ -74,7 +70,7 @@ uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
74 | #define LCLIENT_LIST 32 | 70 | #define LCLIENT_LIST 32 |
75 | static Client_data close_clientlist[LCLIENT_LIST]; | 71 | static Client_data close_clientlist[LCLIENT_LIST]; |
76 | 72 | ||
77 | static Friend * friends_list; | 73 | static Friend *friends_list; |
78 | static uint16_t num_friends; | 74 | static uint16_t num_friends; |
79 | 75 | ||
80 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ | 76 | /* The list of ip ports along with the ping_id of what we sent them and a timestamp */ |
@@ -90,19 +86,16 @@ static Pinged send_nodes[LSEND_NODES_ARRAY]; | |||
90 | return 0 if both are same distance | 86 | return 0 if both are same distance |
91 | return 1 if client_id1 is closer | 87 | return 1 if client_id1 is closer |
92 | return 2 if client_id2 is closer */ | 88 | return 2 if client_id2 is closer */ |
93 | int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) /* tested */ | 89 | int id_closest(uint8_t *client_id, uint8_t *client_id1, uint8_t *client_id2) /* tested */ |
94 | { | 90 | { |
95 | uint32_t i; | 91 | uint32_t i; |
96 | for(i = 0; i < CLIENT_ID_SIZE; ++i) { | 92 | for (i = 0; i < CLIENT_ID_SIZE; ++i) { |
97 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) { | 93 | if(abs(client_id[i] ^ client_id1[i]) < abs(client_id[i] ^ client_id2[i])) |
98 | return 1; | 94 | return 1; |
99 | } else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) { | 95 | else if(abs(client_id[i] ^ client_id1[i]) > abs(client_id[i] ^ client_id2[i])) |
100 | return 2; | 96 | return 2; |
101 | } | ||
102 | } | 97 | } |
103 | |||
104 | return 0; | 98 | return 0; |
105 | |||
106 | } | 99 | } |
107 | 100 | ||
108 | /* check if client with client_id is already in list of length length. | 101 | /* check if client with client_id is already in list of length length. |
@@ -110,15 +103,16 @@ int id_closest(uint8_t * client_id, uint8_t * client_id1, uint8_t * client_id2) | |||
110 | if the id is already in the list with a different ip_port, update it. | 103 | if the id is already in the list with a different ip_port, update it. |
111 | return True(1) or False(0) | 104 | return True(1) or False(0) |
112 | TODO: maybe optimize this. */ | 105 | TODO: maybe optimize this. */ |
113 | int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) | 106 | int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) |
114 | { | 107 | { |
115 | uint32_t i; | 108 | uint32_t i; |
116 | uint32_t temp_time = unix_time(); | 109 | uint32_t temp_time = unix_time(); |
117 | 110 | ||
118 | for(i = 0; i < length; ++i) { | 111 | for (i = 0; i < length; ++i) { |
119 | /*If ip_port is assigned to a different client_id replace it*/ | 112 | /*If ip_port is assigned to a different client_id replace it*/ |
120 | if(list[i].ip_port.ip.i == ip_port.ip.i && | 113 | if(list[i].ip_port.ip.i == ip_port.ip.i && |
121 | list[i].ip_port.port == ip_port.port) { | 114 | list[i].ip_port.port == ip_port.port) |
115 | { | ||
122 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 116 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
123 | } | 117 | } |
124 | 118 | ||
@@ -136,14 +130,12 @@ int client_in_list(Client_data * list, uint32_t length, uint8_t * client_id, IP_ | |||
136 | 130 | ||
137 | /* check if client with client_id is already in node format list of length length. | 131 | /* check if client with client_id is already in node format list of length length. |
138 | return True(1) or False(0) */ | 132 | return True(1) or False(0) */ |
139 | int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | 133 | int client_in_nodelist(Node_format *list, uint32_t length, uint8_t *client_id) |
140 | { | 134 | { |
141 | uint32_t i; | 135 | uint32_t i; |
142 | for(i = 0; i < length; ++i) { | 136 | for (i = 0; i < length; ++i) { |
143 | if(memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { | 137 | if (memcmp(list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) |
144 | |||
145 | return 1; | 138 | return 1; |
146 | } | ||
147 | } | 139 | } |
148 | return 0; | 140 | return 0; |
149 | 141 | ||
@@ -151,14 +143,12 @@ int client_in_nodelist(Node_format * list, uint32_t length, uint8_t * client_id) | |||
151 | 143 | ||
152 | /*Return the friend number from the client_id | 144 | /*Return the friend number from the client_id |
153 | Return -1 if failure, number of friend if success*/ | 145 | Return -1 if failure, number of friend if success*/ |
154 | static int friend_number(uint8_t * client_id) | 146 | static int friend_number(uint8_t *client_id) |
155 | { | 147 | { |
156 | uint32_t i; | 148 | uint32_t i; |
157 | for(i = 0; i < num_friends; ++i) { | 149 | for (i = 0; i < num_friends; ++i) { |
158 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ | 150 | if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) /* Equal */ |
159 | { | ||
160 | return i; | 151 | return i; |
161 | } | ||
162 | } | 152 | } |
163 | return -1; | 153 | return -1; |
164 | } | 154 | } |
@@ -171,14 +161,15 @@ static int friend_number(uint8_t * client_id) | |||
171 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: | 161 | /* Find MAX_SENT_NODES nodes closest to the client_id for the send nodes request: |
172 | put them in the nodes_list and return how many were found. | 162 | put them in the nodes_list and return how many were found. |
173 | TODO: Make this function much more efficient. */ | 163 | TODO: Make this function much more efficient. */ |
174 | int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | 164 | int get_close_nodes(uint8_t *client_id, Node_format * nodes_list) |
175 | { | 165 | { |
176 | uint32_t i, j, k; | 166 | uint32_t i, j, k; |
177 | int num_nodes=0; | 167 | int num_nodes=0; |
178 | uint32_t temp_time = unix_time(); | 168 | uint32_t temp_time = unix_time(); |
179 | for(i = 0; i < LCLIENT_LIST; ++i) { | 169 | for (i = 0; i < LCLIENT_LIST; ++i) { |
180 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && | 170 | if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time && |
181 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) { | 171 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,close_clientlist[i].client_id)) |
172 | { | ||
182 | /* if node is good and not already in list. */ | 173 | /* if node is good and not already in list. */ |
183 | if(num_nodes < MAX_SENT_NODES) { | 174 | if(num_nodes < MAX_SENT_NODES) { |
184 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); | 175 | memcpy(nodes_list[num_nodes].client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE); |
@@ -194,12 +185,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
194 | } | 185 | } |
195 | } | 186 | } |
196 | } | 187 | } |
197 | for(i = 0; i < num_friends; ++i) { | 188 | for (i = 0; i < num_friends; ++i) { |
198 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 189 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
199 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && | 190 | if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time && |
200 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { | 191 | !client_in_nodelist(nodes_list, MAX_SENT_NODES,friends_list[i].client_list[j].client_id)) { |
201 | /* if node is good and not already in list. */ | 192 | /* if node is good and not already in list. */ |
202 | if(num_nodes < MAX_SENT_NODES) { | 193 | if (num_nodes < MAX_SENT_NODES) { |
203 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); | 194 | memcpy(nodes_list[num_nodes].client_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE); |
204 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; | 195 | nodes_list[num_nodes].ip_port = friends_list[i].client_list[j].ip_port; |
205 | num_nodes++; | 196 | num_nodes++; |
@@ -220,12 +211,12 @@ int get_close_nodes(uint8_t * client_id, Node_format * nodes_list) | |||
220 | /* replace first bad (or empty) node with this one | 211 | /* replace first bad (or empty) node with this one |
221 | return 0 if successful | 212 | return 0 if successful |
222 | return 1 if not (list contains no bad nodes) */ | 213 | return 1 if not (list contains no bad nodes) */ |
223 | int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port) /* tested */ | 214 | int replace_bad(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port) /* tested */ |
224 | { | 215 | { |
225 | uint32_t i; | 216 | uint32_t i; |
226 | uint32_t temp_time = unix_time(); | 217 | uint32_t temp_time = unix_time(); |
227 | for(i = 0; i < length; ++i) { | 218 | for (i = 0; i < length; ++i) { |
228 | if(list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { | 219 | if (list[i].timestamp + BAD_NODE_TIMEOUT < temp_time) /* if node is bad. */ { |
229 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 220 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
230 | list[i].ip_port = ip_port; | 221 | list[i].ip_port = ip_port; |
231 | list[i].timestamp = temp_time; | 222 | list[i].timestamp = temp_time; |
@@ -240,13 +231,13 @@ int replace_bad(Client_data * list, uint32_t length, uint8_t * client_id, IP_Por | |||
240 | } | 231 | } |
241 | 232 | ||
242 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ | 233 | /* replace the first good node that is further to the comp_client_id than that of the client_id in the list */ |
243 | int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Port ip_port, uint8_t * comp_client_id) | 234 | int replace_good(Client_data *list, uint32_t length, uint8_t *client_id, IP_Port ip_port, uint8_t *comp_client_id) |
244 | { | 235 | { |
245 | uint32_t i; | 236 | uint32_t i; |
246 | uint32_t temp_time = unix_time(); | 237 | uint32_t temp_time = unix_time(); |
247 | 238 | ||
248 | for(i = 0; i < length; ++i) { | 239 | for (i = 0; i < length; ++i) { |
249 | if(id_closest(comp_client_id, list[i].client_id, client_id) == 2) { | 240 | if (id_closest(comp_client_id, list[i].client_id, client_id) == 2) { |
250 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); | 241 | memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); |
251 | list[i].ip_port = ip_port; | 242 | list[i].ip_port = ip_port; |
252 | list[i].timestamp = temp_time; | 243 | list[i].timestamp = temp_time; |
@@ -261,17 +252,14 @@ int replace_good(Client_data * list, uint32_t length, uint8_t * client_id, IP_Po | |||
261 | } | 252 | } |
262 | 253 | ||
263 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ | 254 | /* Attempt to add client with ip_port and client_id to the friends client list and close_clientlist */ |
264 | void addto_lists(IP_Port ip_port, uint8_t * client_id) | 255 | void addto_lists(IP_Port ip_port, uint8_t *client_id) |
265 | { | 256 | { |
266 | uint32_t i; | 257 | uint32_t i; |
267 | 258 | ||
268 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ | 259 | /* NOTE: current behavior if there are two clients with the same id is to replace the first ip by the second. */ |
269 | if(!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 260 | if (!client_in_list(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { |
270 | 261 | if (replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) | |
271 | if(replace_bad(close_clientlist, LCLIENT_LIST, client_id, ip_port)) { | 262 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); /* if we can't replace bad nodes we try replacing good ones */ |
272 | /* if we can't replace bad nodes we try replacing good ones */ | ||
273 | replace_good(close_clientlist, LCLIENT_LIST, client_id, ip_port, self_public_key); | ||
274 | } | ||
275 | } | 263 | } |
276 | for(i = 0; i < num_friends; ++i) { | 264 | for(i = 0; i < num_friends; ++i) { |
277 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { | 265 | if(!client_in_list(friends_list[i].client_list, MAX_FRIEND_CLIENTS, client_id, ip_port)) { |
@@ -285,30 +273,31 @@ void addto_lists(IP_Port ip_port, uint8_t * client_id) | |||
285 | 273 | ||
286 | /* If client_id is a friend or us, update ret_ip_port | 274 | /* If client_id is a friend or us, update ret_ip_port |
287 | nodeclient_id is the id of the node that sent us this info */ | 275 | nodeclient_id is the id of the node that sent us this info */ |
288 | void returnedip_ports(IP_Port ip_port, uint8_t * client_id, uint8_t * nodeclient_id) | 276 | void returnedip_ports(IP_Port ip_port, uint8_t *client_id, uint8_t *nodeclient_id) |
289 | { | 277 | { |
290 | uint32_t i, j; | 278 | uint32_t i, j; |
291 | uint32_t temp_time = unix_time(); | 279 | uint32_t temp_time = unix_time(); |
292 | if(memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { | 280 | if (memcmp(client_id, self_public_key, CLIENT_ID_SIZE) == 0) { |
293 | for(i = 0; i < LCLIENT_LIST; ++i) { | 281 | for (i = 0; i < LCLIENT_LIST; ++i) { |
294 | if(memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { | 282 | if (memcmp(nodeclient_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
295 | close_clientlist[i].ret_ip_port = ip_port; | 283 | close_clientlist[i].ret_ip_port = ip_port; |
296 | close_clientlist[i].ret_timestamp = temp_time; | 284 | close_clientlist[i].ret_timestamp = temp_time; |
297 | return; | 285 | return; |
298 | } | 286 | } |
299 | } | 287 | } |
300 | } | 288 | } |
301 | else | 289 | else { |
302 | for(i = 0; i < num_friends; ++i) { | 290 | for (i = 0; i < num_friends; ++i) { |
303 | if(memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { | 291 | if (memcmp(client_id, friends_list[i].client_id, CLIENT_ID_SIZE) == 0) { |
304 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 292 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
305 | if(memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { | 293 | if (memcmp(nodeclient_id, friends_list[i].client_list[j].client_id, CLIENT_ID_SIZE) == 0) { |
306 | friends_list[i].client_list[j].ret_ip_port = ip_port; | 294 | friends_list[i].client_list[j].ret_ip_port = ip_port; |
307 | friends_list[i].client_list[j].ret_timestamp = temp_time; | 295 | friends_list[i].client_list[j].ret_timestamp = temp_time; |
308 | return; | 296 | return; |
309 | } | 297 | } |
310 | } | 298 | } |
311 | } | 299 | } |
300 | } | ||
312 | } | 301 | } |
313 | } | 302 | } |
314 | 303 | ||
@@ -326,25 +315,23 @@ int is_pinging(IP_Port ip_port, uint64_t ping_id) | |||
326 | uint8_t pinging; | 315 | uint8_t pinging; |
327 | uint32_t temp_time = unix_time(); | 316 | uint32_t temp_time = unix_time(); |
328 | 317 | ||
329 | for(i = 0; i < LPING_ARRAY; ++i ) { | 318 | for (i = 0; i < LPING_ARRAY; ++i ) { |
330 | if((pings[i].timestamp + PING_TIMEOUT) > temp_time) { | 319 | if ((pings[i].timestamp + PING_TIMEOUT) > temp_time) { |
331 | pinging = 0; | 320 | pinging = 0; |
332 | if(ip_port.ip.i != 0) { | 321 | if (ip_port.ip.i != 0) { |
333 | if(pings[i].ip_port.ip.i == ip_port.ip.i && | 322 | if(pings[i].ip_port.ip.i == ip_port.ip.i && |
334 | pings[i].ip_port.port == ip_port.port) { | 323 | pings[i].ip_port.port == ip_port.port) |
324 | { | ||
335 | ++pinging; | 325 | ++pinging; |
336 | } | 326 | } |
337 | } | 327 | } |
338 | if(ping_id != 0) { | 328 | if (ping_id != 0) { |
339 | if(pings[i].ping_id == ping_id) | 329 | if(pings[i].ping_id == ping_id) |
340 | { | ||
341 | ++pinging; | 330 | ++pinging; |
342 | } | 331 | |
343 | } | 332 | } |
344 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { | 333 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
345 | return 1; | 334 | return 1; |
346 | } | ||
347 | |||
348 | } | 335 | } |
349 | } | 336 | } |
350 | 337 | ||
@@ -359,24 +346,21 @@ int is_gettingnodes(IP_Port ip_port, uint64_t ping_id) | |||
359 | uint8_t pinging; | 346 | uint8_t pinging; |
360 | uint32_t temp_time = unix_time(); | 347 | uint32_t temp_time = unix_time(); |
361 | 348 | ||
362 | for(i = 0; i < LSEND_NODES_ARRAY; ++i ) { | 349 | for(i = 0; i < LSEND_NODES_ARRAY; ++i) { |
363 | if((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { | 350 | if ((send_nodes[i].timestamp + PING_TIMEOUT) > temp_time) { |
364 | pinging = 0; | 351 | pinging = 0; |
365 | if(ip_port.ip.i != 0) { | 352 | if (ip_port.ip.i != 0) { |
366 | if(send_nodes[i].ip_port.ip.i == ip_port.ip.i && | 353 | if (send_nodes[i].ip_port.ip.i == ip_port.ip.i && |
367 | send_nodes[i].ip_port.port == ip_port.port) { | 354 | send_nodes[i].ip_port.port == ip_port.port) { |
368 | ++pinging; | 355 | ++pinging; |
369 | } | 356 | } |
370 | } | 357 | } |
371 | if(ping_id != 0) { | 358 | if (ping_id != 0) { |
372 | if(send_nodes[i].ping_id == ping_id) { | 359 | if (send_nodes[i].ping_id == ping_id) |
373 | ++pinging; | 360 | ++pinging; |
374 | } | ||
375 | } | 361 | } |
376 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) { | 362 | if(pinging == (ping_id != 0) + (ip_port.ip.i != 0)) |
377 | return 1; | 363 | return 1; |
378 | } | ||
379 | |||
380 | } | 364 | } |
381 | } | 365 | } |
382 | 366 | ||
@@ -394,9 +378,9 @@ uint64_t add_pinging(IP_Port ip_port) | |||
394 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 378 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
395 | uint32_t temp_time = unix_time(); | 379 | uint32_t temp_time = unix_time(); |
396 | 380 | ||
397 | for(i = 0; i < PING_TIMEOUT; ++i ) { | 381 | for (i = 0; i < PING_TIMEOUT; ++i ) { |
398 | for(j = 0; j < LPING_ARRAY; ++j ) { | 382 | for (j = 0; j < LPING_ARRAY; ++j ) { |
399 | if((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { | 383 | if ((pings[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
400 | pings[j].timestamp = temp_time; | 384 | pings[j].timestamp = temp_time; |
401 | pings[j].ip_port = ip_port; | 385 | pings[j].ip_port = ip_port; |
402 | pings[j].ping_id = ping_id; | 386 | pings[j].ping_id = ping_id; |
@@ -415,9 +399,9 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
415 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); | 399 | uint64_t ping_id = ((uint64_t)random_int() << 32) + random_int(); |
416 | uint32_t temp_time = unix_time(); | 400 | uint32_t temp_time = unix_time(); |
417 | 401 | ||
418 | for(i = 0; i < PING_TIMEOUT; ++i ) { | 402 | for (i = 0; i < PING_TIMEOUT; ++i ) { |
419 | for(j = 0; j < LSEND_NODES_ARRAY; ++j ) { | 403 | for (j = 0; j < LSEND_NODES_ARRAY; ++j ) { |
420 | if((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { | 404 | if ((send_nodes[j].timestamp + PING_TIMEOUT - i) < temp_time) { |
421 | send_nodes[j].timestamp = temp_time; | 405 | send_nodes[j].timestamp = temp_time; |
422 | send_nodes[j].ip_port = ip_port; | 406 | send_nodes[j].ip_port = ip_port; |
423 | send_nodes[j].ping_id = ping_id; | 407 | send_nodes[j].ping_id = ping_id; |
@@ -431,20 +415,17 @@ uint64_t add_gettingnodes(IP_Port ip_port) | |||
431 | 415 | ||
432 | /* send a ping request | 416 | /* send a ping request |
433 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ | 417 | Ping request only works if none has been sent to that ip/port in the last 5 seconds. */ |
434 | static int pingreq(IP_Port ip_port, uint8_t * public_key) | 418 | static int pingreq(IP_Port ip_port, uint8_t *public_key) |
435 | { | 419 | { |
436 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { | 420 | if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
437 | return 1; | 421 | return 1; |
438 | } | ||
439 | 422 | ||
440 | if(is_pinging(ip_port, 0)) { | 423 | if (is_pinging(ip_port, 0)) |
441 | return 1; | 424 | return 1; |
442 | } | ||
443 | 425 | ||
444 | uint64_t ping_id = add_pinging(ip_port); | 426 | uint64_t ping_id = add_pinging(ip_port); |
445 | if(ping_id == 0) { | 427 | if (ping_id == 0) |
446 | return 1; | 428 | return 1; |
447 | } | ||
448 | 429 | ||
449 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | 430 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; |
450 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | 431 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; |
@@ -452,9 +433,8 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
452 | random_nonce(nonce); | 433 | random_nonce(nonce); |
453 | 434 | ||
454 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 435 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
455 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { | 436 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) |
456 | return -1; | 437 | return -1; |
457 | } | ||
458 | data[0] = 0; | 438 | data[0] = 0; |
459 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 439 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
460 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 440 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
@@ -465,12 +445,11 @@ static int pingreq(IP_Port ip_port, uint8_t * public_key) | |||
465 | } | 445 | } |
466 | 446 | ||
467 | /* send a ping response */ | 447 | /* send a ping response */ |
468 | static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | 448 | static int pingres(IP_Port ip_port, uint8_t *public_key, uint64_t ping_id) |
469 | { | 449 | { |
470 | /* check if packet is gonna be sent to ourself */ | 450 | /* check if packet is gonna be sent to ourself */ |
471 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { | 451 | if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) |
472 | return 1; | 452 | return 1; |
473 | } | ||
474 | 453 | ||
475 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; | 454 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING]; |
476 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; | 455 | uint8_t encrypt[sizeof(ping_id) + ENCRYPTION_PADDING]; |
@@ -478,9 +457,8 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
478 | random_nonce(nonce); | 457 | random_nonce(nonce); |
479 | 458 | ||
480 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); | 459 | int len = encrypt_data(public_key, self_secret_key, nonce, (uint8_t *)&ping_id, sizeof(ping_id), encrypt); |
481 | if(len != sizeof(ping_id) + ENCRYPTION_PADDING) { | 460 | if (len != sizeof(ping_id) + ENCRYPTION_PADDING) |
482 | return -1; | 461 | return -1; |
483 | } | ||
484 | data[0] = 1; | 462 | data[0] = 1; |
485 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 463 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
486 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 464 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
@@ -491,22 +469,18 @@ static int pingres(IP_Port ip_port, uint8_t * public_key, uint64_t ping_id) | |||
491 | } | 469 | } |
492 | 470 | ||
493 | /* send a getnodes request */ | 471 | /* send a getnodes request */ |
494 | static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | 472 | static int getnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id) |
495 | { | 473 | { |
496 | /* check if packet is gonna be sent to ourself */ | 474 | /* check if packet is gonna be sent to ourself */ |
497 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) { | 475 | if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) |
498 | return 1; | 476 | return 1; |
499 | } | 477 | if (is_gettingnodes(ip_port, 0)) |
500 | |||
501 | if(is_gettingnodes(ip_port, 0)) { | ||
502 | return 1; | 478 | return 1; |
503 | } | ||
504 | 479 | ||
505 | uint64_t ping_id = add_gettingnodes(ip_port); | 480 | uint64_t ping_id = add_gettingnodes(ip_port); |
506 | 481 | ||
507 | if(ping_id == 0) { | 482 | if (ping_id == 0) |
508 | return 1; | 483 | return 1; |
509 | } | ||
510 | 484 | ||
511 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; | 485 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING]; |
512 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 486 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
@@ -519,9 +493,8 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
519 | 493 | ||
520 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); | 494 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, sizeof(ping_id) + CLIENT_ID_SIZE, encrypt); |
521 | 495 | ||
522 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { | 496 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
523 | return -1; | 497 | return -1; |
524 | } | ||
525 | data[0] = 2; | 498 | data[0] = 2; |
526 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 499 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
527 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); | 500 | memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); |
@@ -531,11 +504,10 @@ static int getnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id) | |||
531 | } | 504 | } |
532 | 505 | ||
533 | /* send a send nodes response */ | 506 | /* send a send nodes response */ |
534 | static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, uint64_t ping_id) | 507 | static int sendnodes(IP_Port ip_port, uint8_t *public_key, uint8_t *client_id, uint64_t ping_id) |
535 | { | 508 | { |
536 | if(memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ { | 509 | if (memcmp(public_key, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is gonna be sent to ourself */ |
537 | return 1; | 510 | return 1; |
538 | } | ||
539 | 511 | ||
540 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 512 | uint8_t data[1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
541 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 513 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
@@ -543,9 +515,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
543 | Node_format nodes_list[MAX_SENT_NODES]; | 515 | Node_format nodes_list[MAX_SENT_NODES]; |
544 | int num_nodes = get_close_nodes(client_id, nodes_list); | 516 | int num_nodes = get_close_nodes(client_id, nodes_list); |
545 | 517 | ||
546 | if(num_nodes == 0) { | 518 | if (num_nodes == 0) |
547 | return 0; | 519 | return 0; |
548 | } | ||
549 | 520 | ||
550 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; | 521 | uint8_t plain[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES]; |
551 | uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; | 522 | uint8_t encrypt[sizeof(ping_id) + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING]; |
@@ -558,9 +529,8 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
558 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, | 529 | int len = encrypt_data(public_key, self_secret_key, nonce, plain, |
559 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); | 530 | sizeof(ping_id) + num_nodes * sizeof(Node_format), encrypt); |
560 | 531 | ||
561 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) { | 532 | if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING) |
562 | return -1; | 533 | return -1; |
563 | } | ||
564 | 534 | ||
565 | data[0] = 3; | 535 | data[0] = 3; |
566 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); | 536 | memcpy(data + 1, self_public_key, CLIENT_ID_SIZE); |
@@ -574,27 +544,20 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id, | |||
574 | /* Packet handling functions | 544 | /* Packet handling functions |
575 | One to handle each types of packets we receive | 545 | One to handle each types of packets we receive |
576 | return 0 if handled correctly, 1 if packet is bad. */ | 546 | return 0 if handled correctly, 1 if packet is bad. */ |
577 | int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | 547 | int handle_pingreq(uint8_t *packet, uint32_t length, IP_Port source) |
578 | { | 548 | { |
579 | uint64_t ping_id; | 549 | uint64_t ping_id; |
580 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { | 550 | if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
581 | return 1; | 551 | return 1; |
582 | } | ||
583 | /* check if packet is from ourself. */ | 552 | /* check if packet is from ourself. */ |
584 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { | 553 | if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
585 | return 1; | 554 | return 1; |
586 | } | ||
587 | |||
588 | |||
589 | |||
590 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 555 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
591 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 556 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
592 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 557 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
593 | if(len != sizeof(ping_id)) { | 558 | if (len != sizeof(ping_id)) |
594 | return 1; | 559 | return 1; |
595 | } | 560 | |
596 | |||
597 | |||
598 | pingres(source, packet + 1, ping_id); | 561 | pingres(source, packet + 1, ping_id); |
599 | 562 | ||
600 | pingreq(source, packet + 1); /* TODO: make this smarter? */ | 563 | pingreq(source, packet + 1); /* TODO: make this smarter? */ |
@@ -603,26 +566,21 @@ int handle_pingreq(uint8_t * packet, uint32_t length, IP_Port source) | |||
603 | 566 | ||
604 | } | 567 | } |
605 | 568 | ||
606 | int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | 569 | int handle_pingres(uint8_t *packet, uint32_t length, IP_Port source) |
607 | { | 570 | { |
608 | uint64_t ping_id; | 571 | uint64_t ping_id; |
609 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) { | 572 | if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + ENCRYPTION_PADDING) |
610 | return 1; | 573 | return 1; |
611 | } | 574 | if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ |
612 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) /* check if packet is from ourself. */ { | ||
613 | return 1; | 575 | return 1; |
614 | } | 576 | |
615 | |||
616 | |||
617 | |||
618 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, | 577 | int len = decrypt_data(packet + 1, self_secret_key, packet + 1 + CLIENT_ID_SIZE, |
619 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 578 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
620 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); | 579 | sizeof(ping_id) + ENCRYPTION_PADDING, (uint8_t *)&ping_id); |
621 | if(len != sizeof(ping_id)) { | 580 | if (len != sizeof(ping_id)) |
622 | return 1; | 581 | return 1; |
623 | } | ||
624 | 582 | ||
625 | if(is_pinging(source, ping_id)) { | 583 | if (is_pinging(source, ping_id)) { |
626 | addto_lists(source, packet + 1); | 584 | addto_lists(source, packet + 1); |
627 | return 0; | 585 | return 0; |
628 | } | 586 | } |
@@ -630,16 +588,14 @@ int handle_pingres(uint8_t * packet, uint32_t length, IP_Port source) | |||
630 | 588 | ||
631 | } | 589 | } |
632 | 590 | ||
633 | int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | 591 | int handle_getnodes(uint8_t *packet, uint32_t length, IP_Port source) |
634 | { | 592 | { |
635 | uint64_t ping_id; | 593 | uint64_t ping_id; |
636 | if(length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) { | 594 | if (length != 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING) |
637 | return 1; | 595 | return 1; |
638 | } | ||
639 | /* check if packet is from ourself. */ | 596 | /* check if packet is from ourself. */ |
640 | if(memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) { | 597 | if (memcmp(packet + 1, self_public_key, CLIENT_ID_SIZE) == 0) |
641 | return 1; | 598 | return 1; |
642 | } | ||
643 | 599 | ||
644 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; | 600 | uint8_t plain[sizeof(ping_id) + CLIENT_ID_SIZE]; |
645 | 601 | ||
@@ -647,10 +603,8 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
647 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, | 603 | packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, |
648 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); | 604 | sizeof(ping_id) + CLIENT_ID_SIZE + ENCRYPTION_PADDING, plain); |
649 | 605 | ||
650 | if(len != sizeof(ping_id) + CLIENT_ID_SIZE) { | 606 | if (len != sizeof(ping_id) + CLIENT_ID_SIZE) |
651 | return 1; | 607 | return 1; |
652 | } | ||
653 | |||
654 | 608 | ||
655 | memcpy(&ping_id, plain, sizeof(ping_id)); | 609 | memcpy(&ping_id, plain, sizeof(ping_id)); |
656 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); | 610 | sendnodes(source, packet + 1, plain + sizeof(ping_id), ping_id); |
@@ -664,7 +618,7 @@ int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
664 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | 618 | int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) |
665 | { | 619 | { |
666 | uint64_t ping_id; | 620 | uint64_t ping_id; |
667 | if(length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 621 | if (length > (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) //TODO: rewrite this monstrosity |
668 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || | 622 | + sizeof(Node_format) * MAX_SENT_NODES + ENCRYPTION_PADDING) || |
669 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) | 623 | (length - (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(ping_id) |
670 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || | 624 | + ENCRYPTION_PADDING)) % (sizeof(Node_format)) != 0 || |
@@ -682,42 +636,36 @@ int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) | |||
682 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); | 636 | sizeof(ping_id) + num_nodes * sizeof(Node_format) + ENCRYPTION_PADDING, plain); |
683 | 637 | ||
684 | 638 | ||
685 | if(len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) { | 639 | if (len != sizeof(ping_id) + num_nodes * sizeof(Node_format)) |
686 | return 1; | 640 | return 1; |
687 | } | ||
688 | |||
689 | memcpy(&ping_id, plain, sizeof(ping_id)); | 641 | memcpy(&ping_id, plain, sizeof(ping_id)); |
690 | if(!is_gettingnodes(source, ping_id)) { | 642 | if(!is_gettingnodes(source, ping_id)) |
691 | return 1; | 643 | return 1; |
692 | } | 644 | |
693 | |||
694 | Node_format nodes_list[MAX_SENT_NODES]; | 645 | Node_format nodes_list[MAX_SENT_NODES]; |
695 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); | 646 | memcpy(nodes_list, plain + sizeof(ping_id), num_nodes * sizeof(Node_format)); |
696 | 647 | ||
697 | addto_lists(source, packet + 1); | 648 | addto_lists(source, packet + 1); |
698 | 649 | ||
699 | uint32_t i; | 650 | uint32_t i; |
700 | for(i = 0; i < num_nodes; ++i) { | 651 | for (i = 0; i < num_nodes; ++i) |
701 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); | 652 | pingreq(nodes_list[i].ip_port, nodes_list[i].client_id); |
702 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); | 653 | returnedip_ports(nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); |
703 | } | ||
704 | 654 | ||
705 | return 0; | 655 | return 0; |
706 | } | 656 | } |
707 | 657 | ||
708 | /* END of packet handling functions */ | 658 | /* END of packet handling functions */ |
709 | 659 | ||
710 | int DHT_addfriend(uint8_t * client_id) | 660 | int DHT_addfriend(uint8_t *client_id) |
711 | { | 661 | { |
712 | Friend * temp; | 662 | Friend * temp; |
713 | if(num_friends == 0) { | 663 | if (num_friends == 0) |
714 | temp = malloc(sizeof(Friend)); | 664 | temp = malloc(sizeof(Friend)); |
715 | } else { | 665 | else |
716 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); | 666 | temp = realloc(friends_list, sizeof(Friend) * (num_friends + 1)); |
717 | } | 667 | if (temp == NULL) |
718 | if(temp == NULL) { | ||
719 | return 1; | 668 | return 1; |
720 | } | ||
721 | 669 | ||
722 | friends_list = temp; | 670 | friends_list = temp; |
723 | memset(&friends_list[num_friends], 0, sizeof(Friend)); | 671 | memset(&friends_list[num_friends], 0, sizeof(Friend)); |
@@ -727,21 +675,19 @@ int DHT_addfriend(uint8_t * client_id) | |||
727 | return 0; | 675 | return 0; |
728 | } | 676 | } |
729 | 677 | ||
730 | int DHT_delfriend(uint8_t * client_id) | 678 | int DHT_delfriend(uint8_t *client_id) |
731 | { | 679 | { |
732 | uint32_t i; | 680 | uint32_t i; |
733 | Friend * temp; | 681 | Friend * temp; |
734 | for(i = 0; i < num_friends; ++i) { | 682 | for (i = 0; i < num_friends; ++i) { |
735 | /* Equal */ | 683 | /* Equal */ |
736 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ | 684 | if (memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0){ |
737 | --num_friends; | 685 | --num_friends; |
738 | if(num_friends != i) { | 686 | if (num_friends != i) |
739 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); | 687 | memcpy(friends_list[i].client_id, friends_list[num_friends].client_id, CLIENT_ID_SIZE); |
740 | } | ||
741 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); | 688 | temp = realloc(friends_list, sizeof(Friend) * (num_friends)); |
742 | if(temp != NULL) { | 689 | if (temp != NULL) |
743 | friends_list = temp; | 690 | friends_list = temp; |
744 | } | ||
745 | return 0; | 691 | return 0; |
746 | } | 692 | } |
747 | } | 693 | } |
@@ -749,12 +695,12 @@ int DHT_delfriend(uint8_t * client_id) | |||
749 | } | 695 | } |
750 | 696 | ||
751 | /* TODO: Optimize this. */ | 697 | /* TODO: Optimize this. */ |
752 | IP_Port DHT_getfriendip(uint8_t * client_id) | 698 | IP_Port DHT_getfriendip(uint8_t *client_id) |
753 | { | 699 | { |
754 | uint32_t i, j; | 700 | uint32_t i, j; |
755 | IP_Port empty = {{{0}}, 0}; | 701 | IP_Port empty = {{{0}}, 0}; |
756 | uint32_t temp_time = unix_time(); | 702 | uint32_t temp_time = unix_time(); |
757 | for(i = 0; i < num_friends; ++i) { | 703 | for (i = 0; i < num_friends; ++i) { |
758 | /* Equal */ | 704 | /* Equal */ |
759 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { | 705 | if(memcmp(friends_list[i].client_id, client_id, CLIENT_ID_SIZE) == 0) { |
760 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 706 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
@@ -789,21 +735,21 @@ void doDHTFriends() | |||
789 | uint32_t rand_node; | 735 | uint32_t rand_node; |
790 | uint32_t index[MAX_FRIEND_CLIENTS]; | 736 | uint32_t index[MAX_FRIEND_CLIENTS]; |
791 | 737 | ||
792 | for(i = 0; i < num_friends; ++i) { | 738 | for (i = 0; i < num_friends; ++i) { |
793 | uint32_t num_nodes = 0; | 739 | uint32_t num_nodes = 0; |
794 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 740 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
795 | if(friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { | 741 | if (friends_list[i].client_list[j].timestamp + Kill_NODE_TIMEOUT > temp_time) /* if node is not dead. */ { |
796 | if((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { | 742 | if ((friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { |
797 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); | 743 | pingreq(friends_list[i].client_list[j].ip_port, friends_list[i].client_list[j].client_id); |
798 | friends_list[i].client_list[j].last_pinged = temp_time; | 744 | friends_list[i].client_list[j].last_pinged = temp_time; |
799 | } | 745 | } |
800 | if(friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { | 746 | if (friends_list[i].client_list[j].timestamp + BAD_NODE_TIMEOUT > temp_time) /* if node is good. */ { |
801 | index[num_nodes] = j; | 747 | index[num_nodes] = j; |
802 | ++num_nodes; | 748 | ++num_nodes; |
803 | } | 749 | } |
804 | } | 750 | } |
805 | } | 751 | } |
806 | if(friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 752 | if (friends_list[i].lastgetnode + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
807 | rand_node = rand() % num_nodes; | 753 | rand_node = rand() % num_nodes; |
808 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, | 754 | getnodes(friends_list[i].client_list[index[rand_node]].ip_port, |
809 | friends_list[i].client_list[index[rand_node]].client_id, | 755 | friends_list[i].client_list[index[rand_node]].client_id, |
@@ -825,23 +771,23 @@ void doClose() /* tested */ | |||
825 | uint32_t rand_node; | 771 | uint32_t rand_node; |
826 | uint32_t index[LCLIENT_LIST]; | 772 | uint32_t index[LCLIENT_LIST]; |
827 | 773 | ||
828 | for(i = 0; i < LCLIENT_LIST; ++i) { | 774 | for (i = 0; i < LCLIENT_LIST; ++i) { |
829 | /* if node is not dead. */ | 775 | /* if node is not dead. */ |
830 | if(close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { | 776 | if (close_clientlist[i].timestamp + Kill_NODE_TIMEOUT > temp_time) { |
831 | if((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) | 777 | if ((close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) |
832 | { | 778 | { |
833 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); | 779 | pingreq(close_clientlist[i].ip_port, close_clientlist[i].client_id); |
834 | close_clientlist[i].last_pinged = temp_time; | 780 | close_clientlist[i].last_pinged = temp_time; |
835 | } | 781 | } |
836 | /* if node is good. */ | 782 | /* if node is good. */ |
837 | if(close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { | 783 | if (close_clientlist[i].timestamp + BAD_NODE_TIMEOUT > temp_time) { |
838 | index[num_nodes] = i; | 784 | index[num_nodes] = i; |
839 | ++num_nodes; | 785 | ++num_nodes; |
840 | } | 786 | } |
841 | } | 787 | } |
842 | } | 788 | } |
843 | 789 | ||
844 | if(close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { | 790 | if (close_lastgetnodes + GET_NODE_INTERVAL <= temp_time && num_nodes != 0) { |
845 | rand_node = rand() % num_nodes; | 791 | rand_node = rand() % num_nodes; |
846 | getnodes(close_clientlist[index[rand_node]].ip_port, | 792 | getnodes(close_clientlist[index[rand_node]].ip_port, |
847 | close_clientlist[index[rand_node]].client_id, | 793 | close_clientlist[index[rand_node]].client_id, |
@@ -850,18 +796,18 @@ void doClose() /* tested */ | |||
850 | } | 796 | } |
851 | } | 797 | } |
852 | 798 | ||
853 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key) | 799 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key) |
854 | { | 800 | { |
855 | getnodes(ip_port, public_key, self_public_key); | 801 | getnodes(ip_port, public_key, self_public_key); |
856 | } | 802 | } |
857 | 803 | ||
858 | /* send the given packet to node with client_id | 804 | /* send the given packet to node with client_id |
859 | returns -1 if failure */ | 805 | returns -1 if failure */ |
860 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) | 806 | int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length) |
861 | { | 807 | { |
862 | uint32_t i; | 808 | uint32_t i; |
863 | for(i = 0; i < LCLIENT_LIST; ++i) { | 809 | for (i = 0; i < LCLIENT_LIST; ++i) { |
864 | if(memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { | 810 | if (memcmp(client_id, close_clientlist[i].client_id, CLIENT_ID_SIZE) == 0) { |
865 | return sendpacket(close_clientlist[i].ip_port, packet, length); | 811 | return sendpacket(close_clientlist[i].ip_port, packet, length); |
866 | } | 812 | } |
867 | } | 813 | } |
@@ -873,22 +819,20 @@ int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length) | |||
873 | returns the number of ips returned | 819 | returns the number of ips returned |
874 | return 0 if we are connected to friend or if no ips were found. | 820 | return 0 if we are connected to friend or if no ips were found. |
875 | returns -1 if no such friend*/ | 821 | returns -1 if no such friend*/ |
876 | static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | 822 | static int friend_iplist(IP_Port *ip_portlist, uint16_t friend_num) |
877 | { | 823 | { |
878 | int num_ips = 0; | 824 | int num_ips = 0; |
879 | uint32_t i; | 825 | uint32_t i; |
880 | uint32_t temp_time = unix_time(); | 826 | uint32_t temp_time = unix_time(); |
881 | if(friend_num >= num_friends) { | 827 | if (friend_num >= num_friends) |
882 | return -1; | 828 | return -1; |
883 | } | 829 | for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { |
884 | for(i = 0; i < MAX_FRIEND_CLIENTS; ++i) | ||
885 | { | ||
886 | /*If ip is not zero and node is good */ | 830 | /*If ip is not zero and node is good */ |
887 | if(friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && | 831 | if (friends_list[friend_num].client_list[i].ret_ip_port.ip.i != 0 && |
888 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | 832 | friends_list[friend_num].client_list[i].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) |
889 | if(memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0 ) { | 833 | { |
834 | if (memcmp(friends_list[friend_num].client_list[i].client_id, friends_list[friend_num].client_id, CLIENT_ID_SIZE) == 0) | ||
890 | return 0; | 835 | return 0; |
891 | } | ||
892 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; | 836 | ip_portlist[num_ips] = friends_list[friend_num].client_list[i].ret_ip_port; |
893 | ++num_ips; | 837 | ++num_ips; |
894 | } | 838 | } |
@@ -898,21 +842,20 @@ static int friend_iplist(IP_Port * ip_portlist, uint16_t friend_num) | |||
898 | 842 | ||
899 | /* Send the following packet to everyone who tells us they are connected to friend_id | 843 | /* Send the following packet to everyone who tells us they are connected to friend_id |
900 | returns the number of nodes it sent the packet to */ | 844 | returns the number of nodes it sent the packet to */ |
901 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length) | 845 | int route_tofriend(uint8_t * friend_id, uint8_t *packet, uint32_t length) |
902 | { | 846 | { |
903 | uint32_t i, j; | 847 | uint32_t i, j; |
904 | uint32_t sent = 0; | 848 | uint32_t sent = 0; |
905 | uint32_t temp_time = unix_time(); | 849 | uint32_t temp_time = unix_time(); |
906 | for(i = 0; i < num_friends; ++i) { | 850 | for (i = 0; i < num_friends; ++i) { |
907 | /* Equal */ | 851 | /* Equal */ |
908 | if(memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { | 852 | if (memcmp(friends_list[i].client_id, friend_id, CLIENT_ID_SIZE) == 0) { |
909 | for(j = 0; j < MAX_FRIEND_CLIENTS; ++j) { | 853 | for (j = 0; j < MAX_FRIEND_CLIENTS; ++j) { |
910 | /*If ip is not zero and node is good */ | 854 | /*If ip is not zero and node is good */ |
911 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && | 855 | if(friends_list[i].client_list[j].ret_ip_port.ip.i != 0 && |
912 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { | 856 | friends_list[i].client_list[j].ret_timestamp + BAD_NODE_TIMEOUT > temp_time) { |
913 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) { | 857 | if(sendpacket(friends_list[i].client_list[j].ip_port, packet, length) == length) |
914 | ++sent; | 858 | ++sent; |
915 | } | ||
916 | } | 859 | } |
917 | } | 860 | } |
918 | return sent; | 861 | return sent; |