diff options
-rw-r--r-- | core/Lossless_UDP.c | 218 | ||||
-rw-r--r-- | core/net_crypto.c | 144 |
2 files changed, 71 insertions, 291 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 83bb1785..43f61b5b 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -101,13 +101,9 @@ int getconnection_id(IP_Port ip_port) | |||
101 | { | 101 | { |
102 | uint32_t i; | 102 | uint32_t i; |
103 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 103 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
104 | { | ||
105 | if(connections[i].ip_port.ip.i == ip_port.ip.i && | 104 | if(connections[i].ip_port.ip.i == ip_port.ip.i && |
106 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) | 105 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) |
107 | { | ||
108 | return i; | 106 | return i; |
109 | } | ||
110 | } | ||
111 | return -1; | 107 | return -1; |
112 | } | 108 | } |
113 | 109 | ||
@@ -120,18 +116,13 @@ static uint32_t randtable[6][256]; | |||
120 | uint32_t handshake_id(IP_Port source) | 116 | uint32_t handshake_id(IP_Port source) |
121 | { | 117 | { |
122 | uint32_t id = 0, i; | 118 | uint32_t id = 0, i; |
123 | for(i = 0; i < 6; ++i) | 119 | for(i = 0; i < 6; ++i) { |
124 | { | ||
125 | if(randtable[i][((uint8_t *)&source)[i]] == 0) | 120 | if(randtable[i][((uint8_t *)&source)[i]] == 0) |
126 | { | ||
127 | randtable[i][((uint8_t *)&source)[i]] = random_int(); | 121 | randtable[i][((uint8_t *)&source)[i]] = random_int(); |
128 | } | ||
129 | id ^= randtable[i][((uint8_t *)&source)[i]]; | 122 | id ^= randtable[i][((uint8_t *)&source)[i]]; |
130 | } | 123 | } |
131 | if(id == 0) /* id can't be zero */ | 124 | if(id == 0) /* id can't be zero */ |
132 | { | ||
133 | id = 1; | 125 | id = 1; |
134 | } | ||
135 | return id; | 126 | return id; |
136 | } | 127 | } |
137 | 128 | ||
@@ -151,14 +142,10 @@ int new_connection(IP_Port ip_port) | |||
151 | { | 142 | { |
152 | int connect = getconnection_id(ip_port); | 143 | int connect = getconnection_id(ip_port); |
153 | if(connect != -1) | 144 | if(connect != -1) |
154 | { | ||
155 | return connect; | 145 | return connect; |
156 | } | ||
157 | uint32_t i; | 146 | uint32_t i; |
158 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 147 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
159 | { | 148 | if(connections[i].status == 0) { |
160 | if(connections[i].status == 0) | ||
161 | { | ||
162 | memset(&connections[i], 0, sizeof(Connection)); | 149 | memset(&connections[i], 0, sizeof(Connection)); |
163 | connections[i].ip_port = ip_port; | 150 | connections[i].ip_port = ip_port; |
164 | connections[i].status = 1; | 151 | connections[i].status = 1; |
@@ -187,14 +174,10 @@ int new_connection(IP_Port ip_port) | |||
187 | int new_inconnection(IP_Port ip_port) | 174 | int new_inconnection(IP_Port ip_port) |
188 | { | 175 | { |
189 | if(getconnection_id(ip_port) != -1) | 176 | if(getconnection_id(ip_port) != -1) |
190 | { | ||
191 | return -1; | 177 | return -1; |
192 | } | ||
193 | uint32_t i; | 178 | uint32_t i; |
194 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 179 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
195 | { | 180 | if(connections[i].status == 0) { |
196 | if(connections[i].status == 0) | ||
197 | { | ||
198 | memset(&connections[i], 0, sizeof(Connection)); | 181 | memset(&connections[i], 0, sizeof(Connection)); |
199 | connections[i].ip_port = ip_port; | 182 | connections[i].ip_port = ip_port; |
200 | connections[i].status = 2; | 183 | connections[i].status = 2; |
@@ -220,13 +203,10 @@ int incoming_connection() | |||
220 | { | 203 | { |
221 | uint32_t i; | 204 | uint32_t i; |
222 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 205 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
223 | { | 206 | if(connections[i].inbound == 2) { |
224 | if(connections[i].inbound == 2) | ||
225 | { | ||
226 | connections[i].inbound = 1; | 207 | connections[i].inbound = 1; |
227 | return i; | 208 | return i; |
228 | } | 209 | } |
229 | } | ||
230 | return -1; | 210 | return -1; |
231 | } | 211 | } |
232 | 212 | ||
@@ -235,14 +215,11 @@ int incoming_connection() | |||
235 | int kill_connection(int connection_id) | 215 | int kill_connection(int connection_id) |
236 | { | 216 | { |
237 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 217 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
238 | { | 218 | if(connections[connection_id].status > 0) { |
239 | if(connections[connection_id].status > 0) | ||
240 | { | ||
241 | connections[connection_id].status = 0; | 219 | connections[connection_id].status = 0; |
242 | change_handshake(connections[connection_id].ip_port); | 220 | change_handshake(connections[connection_id].ip_port); |
243 | return 0; | 221 | return 0; |
244 | } | 222 | } |
245 | } | ||
246 | return -1; | 223 | return -1; |
247 | } | 224 | } |
248 | 225 | ||
@@ -252,13 +229,10 @@ int kill_connection(int connection_id) | |||
252 | int kill_connection_in(int connection_id, uint32_t seconds) | 229 | int kill_connection_in(int connection_id, uint32_t seconds) |
253 | { | 230 | { |
254 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 231 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
255 | { | 232 | if(connections[connection_id].status > 0) { |
256 | if(connections[connection_id].status > 0) | ||
257 | { | ||
258 | connections[connection_id].killat = current_time() + 1000000UL*seconds; | 233 | connections[connection_id].killat = current_time() + 1000000UL*seconds; |
259 | return 0; | 234 | return 0; |
260 | } | 235 | } |
261 | } | ||
262 | return -1; | 236 | return -1; |
263 | } | 237 | } |
264 | 238 | ||
@@ -271,9 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds) | |||
271 | int is_connected(int connection_id) | 245 | int is_connected(int connection_id) |
272 | { | 246 | { |
273 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 247 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
274 | { | ||
275 | return connections[connection_id].status; | 248 | return connections[connection_id].status; |
276 | } | ||
277 | return 0; | 249 | return 0; |
278 | } | 250 | } |
279 | 251 | ||
@@ -281,9 +253,7 @@ int is_connected(int connection_id) | |||
281 | IP_Port connection_ip(int connection_id) | 253 | IP_Port connection_ip(int connection_id) |
282 | { | 254 | { |
283 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 255 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
284 | { | ||
285 | return connections[connection_id].ip_port; | 256 | return connections[connection_id].ip_port; |
286 | } | ||
287 | IP_Port zero = {{{0}}, 0}; | 257 | IP_Port zero = {{{0}}, 0}; |
288 | return zero; | 258 | return zero; |
289 | } | 259 | } |
@@ -305,9 +275,7 @@ uint32_t recvqueue(int connection_id) | |||
305 | char id_packet(int connection_id) | 275 | char id_packet(int connection_id) |
306 | { | 276 | { |
307 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 277 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
308 | { | ||
309 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; | 278 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; |
310 | } | ||
311 | return -1; | 279 | return -1; |
312 | } | 280 | } |
313 | 281 | ||
@@ -315,8 +283,7 @@ char id_packet(int connection_id) | |||
315 | return length of received packet if successful */ | 283 | return length of received packet if successful */ |
316 | int read_packet(int connection_id, uint8_t * data) | 284 | int read_packet(int connection_id, uint8_t * data) |
317 | { | 285 | { |
318 | if(recvqueue(connection_id) != 0) | 286 | if(recvqueue(connection_id) != 0) { |
319 | { | ||
320 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; | 287 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; |
321 | uint16_t size = connections[connection_id].recvbuffer[index].size; | 288 | uint16_t size = connections[connection_id].recvbuffer[index].size; |
322 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); | 289 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); |
@@ -332,15 +299,10 @@ int read_packet(int connection_id, uint8_t * data) | |||
332 | int write_packet(int connection_id, uint8_t * data, uint32_t length) | 299 | int write_packet(int connection_id, uint8_t * data, uint32_t length) |
333 | { | 300 | { |
334 | if(length > MAX_DATA_SIZE) | 301 | if(length > MAX_DATA_SIZE) |
335 | { | ||
336 | return 0; | 302 | return 0; |
337 | } | ||
338 | if(length == 0) | 303 | if(length == 0) |
339 | { | ||
340 | return 0; | 304 | return 0; |
341 | } | 305 | if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { |
342 | if(sendqueue(connection_id) < BUFFER_PACKET_NUM) | ||
343 | { | ||
344 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; | 306 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; |
345 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); | 307 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); |
346 | connections[connection_id].sendbuffer[index].size = length; | 308 | connections[connection_id].sendbuffer[index].size = length; |
@@ -357,24 +319,16 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
357 | uint32_t i; | 319 | uint32_t i; |
358 | uint32_t temp; | 320 | uint32_t temp; |
359 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ | 321 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ |
360 | { | ||
361 | return 0; | 322 | return 0; |
362 | } | ||
363 | for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) | 323 | for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) |
364 | { | 324 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { |
365 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) | ||
366 | { | ||
367 | temp = htonl(i); | 325 | temp = htonl(i); |
368 | memcpy(requested + number, &temp, 4); | 326 | memcpy(requested + number, &temp, 4); |
369 | ++number; | 327 | ++number; |
370 | } | 328 | } |
371 | } | ||
372 | if(number == 0) | 329 | if(number == 0) |
373 | { | ||
374 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; | 330 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; |
375 | } | ||
376 | return number; | 331 | return number; |
377 | |||
378 | } | 332 | } |
379 | 333 | ||
380 | /* Packet sending functions | 334 | /* Packet sending functions |
@@ -384,29 +338,28 @@ int send_handshake(IP_Port ip_port, uint32_t handshake_id1, uint32_t handshake_i | |||
384 | { | 338 | { |
385 | uint8_t packet[1 + 4 + 4]; | 339 | uint8_t packet[1 + 4 + 4]; |
386 | uint32_t temp; | 340 | uint32_t temp; |
387 | 341 | ||
388 | packet[0] = 16; | 342 | packet[0] = 16; |
389 | temp = htonl(handshake_id1); | 343 | temp = htonl(handshake_id1); |
390 | memcpy(packet + 1, &temp, 4); | 344 | memcpy(packet + 1, &temp, 4); |
391 | temp = htonl(handshake_id2); | 345 | temp = htonl(handshake_id2); |
392 | memcpy(packet + 5, &temp, 4); | 346 | memcpy(packet + 5, &temp, 4); |
393 | return sendpacket(ip_port, packet, sizeof(packet)); | 347 | return sendpacket(ip_port, packet, sizeof(packet)); |
394 | |||
395 | } | 348 | } |
396 | 349 | ||
397 | int send_SYNC(uint32_t connection_id) | 350 | int send_SYNC(uint32_t connection_id) |
398 | { | 351 | { |
399 | 352 | ||
400 | uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; | 353 | uint8_t packet[(BUFFER_PACKET_NUM*4 + 4 + 4 + 2)]; |
401 | uint16_t index = 0; | 354 | uint16_t index = 0; |
402 | 355 | ||
403 | IP_Port ip_port = connections[connection_id].ip_port; | 356 | IP_Port ip_port = connections[connection_id].ip_port; |
404 | uint8_t counter = connections[connection_id].send_counter; | 357 | uint8_t counter = connections[connection_id].send_counter; |
405 | uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); | 358 | uint32_t recv_packetnum = htonl(connections[connection_id].recv_packetnum); |
406 | uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); | 359 | uint32_t sent_packetnum = htonl(connections[connection_id].sent_packetnum); |
407 | uint32_t requested[BUFFER_PACKET_NUM]; | 360 | uint32_t requested[BUFFER_PACKET_NUM]; |
408 | uint32_t number = missing_packets(connection_id, requested); | 361 | uint32_t number = missing_packets(connection_id, requested); |
409 | 362 | ||
410 | packet[0] = 17; | 363 | packet[0] = 17; |
411 | index += 1; | 364 | index += 1; |
412 | memcpy(packet + index, &counter, 1); | 365 | memcpy(packet + index, &counter, 1); |
@@ -418,7 +371,7 @@ int send_SYNC(uint32_t connection_id) | |||
418 | memcpy(packet + index, requested, 4 * number); | 371 | memcpy(packet + index, requested, 4 * number); |
419 | 372 | ||
420 | return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); | 373 | return sendpacket(ip_port, packet, (number*4 + 4 + 4 + 2)); |
421 | 374 | ||
422 | } | 375 | } |
423 | 376 | ||
424 | int send_data_packet(uint32_t connection_id, uint32_t packet_num) | 377 | int send_data_packet(uint32_t connection_id, uint32_t packet_num) |
@@ -440,16 +393,14 @@ int send_DATA(uint32_t connection_id) | |||
440 | { | 393 | { |
441 | int ret; | 394 | int ret; |
442 | uint32_t buffer[BUFFER_PACKET_NUM]; | 395 | uint32_t buffer[BUFFER_PACKET_NUM]; |
443 | if(connections[connection_id].num_req_paquets > 0) | 396 | if(connections[connection_id].num_req_paquets > 0) { |
444 | { | ||
445 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); | 397 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); |
446 | connections[connection_id].num_req_paquets--; | 398 | connections[connection_id].num_req_paquets--; |
447 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); | 399 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); |
448 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); | 400 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); |
449 | return ret; | 401 | return ret; |
450 | } | 402 | } |
451 | if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) | 403 | if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { |
452 | { | ||
453 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); | 404 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); |
454 | connections[connection_id].sent_packetnum++; | 405 | connections[connection_id].sent_packetnum++; |
455 | return ret; | 406 | return ret; |
@@ -465,9 +416,7 @@ int send_DATA(uint32_t connection_id) | |||
465 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | 416 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) |
466 | { | 417 | { |
467 | if(length != (1 + 4 + 4)) | 418 | if(length != (1 + 4 + 4)) |
468 | { | ||
469 | return 1; | 419 | return 1; |
470 | } | ||
471 | uint32_t temp; | 420 | uint32_t temp; |
472 | uint32_t handshake_id1, handshake_id2; | 421 | uint32_t handshake_id1, handshake_id2; |
473 | int connection = getconnection_id(source); | 422 | int connection = getconnection_id(source); |
@@ -476,17 +425,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
476 | memcpy(&temp, packet + 5, 4); | 425 | memcpy(&temp, packet + 5, 4); |
477 | handshake_id2 = ntohl(temp); | 426 | handshake_id2 = ntohl(temp); |
478 | 427 | ||
479 | if(handshake_id2 == 0) | 428 | if(handshake_id2 == 0) { |
480 | { | ||
481 | send_handshake(source, handshake_id(source), handshake_id1); | 429 | send_handshake(source, handshake_id(source), handshake_id1); |
482 | return 0; | 430 | return 0; |
483 | } | 431 | } |
484 | if(is_connected(connection) != 1) | 432 | if(is_connected(connection) != 1) |
485 | { | ||
486 | return 1; | 433 | return 1; |
487 | } | 434 | if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ |
488 | if(handshake_id2 == connections[connection].handshake_id1) /* if handshake_id2 is what we sent previously as handshake_id1 */ | ||
489 | { | ||
490 | connections[connection].status = 2; | 435 | connections[connection].status = 2; |
491 | /* NOTE: is this necessary? | 436 | /* NOTE: is this necessary? |
492 | connections[connection].handshake_id2 = handshake_id1; */ | 437 | connections[connection].handshake_id2 = handshake_id1; */ |
@@ -504,25 +449,19 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
504 | int SYNC_valid(uint32_t length) | 449 | int SYNC_valid(uint32_t length) |
505 | { | 450 | { |
506 | if(length < 4 + 4 + 2) | 451 | if(length < 4 + 4 + 2) |
507 | { | ||
508 | return 0; | 452 | return 0; |
509 | } | ||
510 | if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || | 453 | if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || |
511 | ((length - 4 - 4 - 2) % 4) != 0) | 454 | ((length - 4 - 4 - 2) % 4) != 0) |
512 | { | ||
513 | return 0; | 455 | return 0; |
514 | } | ||
515 | return 1; | 456 | return 1; |
516 | } | 457 | } |
517 | 458 | ||
518 | /* case 1: */ | 459 | /* case 1: */ |
519 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 460 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
520 | { | 461 | { |
521 | if(handshake_id(source) == recv_packetnum) | 462 | if(handshake_id(source) == recv_packetnum) { |
522 | { | ||
523 | int x = new_inconnection(source); | 463 | int x = new_inconnection(source); |
524 | if(x != -1) | 464 | if(x != -1) { |
525 | { | ||
526 | connections[x].orecv_packetnum = recv_packetnum; | 465 | connections[x].orecv_packetnum = recv_packetnum; |
527 | connections[x].sent_packetnum = recv_packetnum; | 466 | connections[x].sent_packetnum = recv_packetnum; |
528 | connections[x].sendbuff_packetnum = recv_packetnum; | 467 | connections[x].sendbuff_packetnum = recv_packetnum; |
@@ -540,9 +479,8 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu | |||
540 | /* case 2: */ | 479 | /* case 2: */ |
541 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 480 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) |
542 | { | 481 | { |
543 | if(recv_packetnum == connections[connection_id].orecv_packetnum) | 482 | if(recv_packetnum == connections[connection_id].orecv_packetnum) { |
544 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ | 483 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ |
545 | { | ||
546 | connections[connection_id].status = 3; | 484 | connections[connection_id].status = 3; |
547 | connections[connection_id].recv_counter = counter; | 485 | connections[connection_id].recv_counter = counter; |
548 | ++connections[connection_id].send_counter; | 486 | ++connections[connection_id].send_counter; |
@@ -561,16 +499,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
561 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ | 499 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ |
562 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); | 500 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); |
563 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); | 501 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); |
564 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) /* packet valid */ | 502 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ |
565 | { | ||
566 | connections[connection_id].orecv_packetnum = recv_packetnum; | 503 | connections[connection_id].orecv_packetnum = recv_packetnum; |
567 | connections[connection_id].osent_packetnum = sent_packetnum; | 504 | connections[connection_id].osent_packetnum = sent_packetnum; |
568 | connections[connection_id].successful_sent = recv_packetnum; | 505 | connections[connection_id].successful_sent = recv_packetnum; |
569 | connections[connection_id].last_recvSYNC = current_time(); | 506 | connections[connection_id].last_recvSYNC = current_time(); |
570 | connections[connection_id].recv_counter = counter; | 507 | connections[connection_id].recv_counter = counter; |
571 | ++connections[connection_id].send_counter; | 508 | ++connections[connection_id].send_counter; |
572 | for(i = 0; i < number; ++i) | 509 | for(i = 0; i < number; ++i) { |
573 | { | ||
574 | temp = ntohl(req_packets[i]); | 510 | temp = ntohl(req_packets[i]); |
575 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); | 511 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); |
576 | } | 512 | } |
@@ -584,9 +520,7 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
584 | { | 520 | { |
585 | 521 | ||
586 | if(!SYNC_valid(length)) | 522 | if(!SYNC_valid(length)) |
587 | { | ||
588 | return 1; | 523 | return 1; |
589 | } | ||
590 | int connection = getconnection_id(source); | 524 | int connection = getconnection_id(source); |
591 | uint8_t counter; | 525 | uint8_t counter; |
592 | uint32_t temp; | 526 | uint32_t temp; |
@@ -600,21 +534,13 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
600 | memcpy(&temp,packet + 6, 4); | 534 | memcpy(&temp,packet + 6, 4); |
601 | sent_packetnum = ntohl(temp); | 535 | sent_packetnum = ntohl(temp); |
602 | if(number != 0) | 536 | if(number != 0) |
603 | { | ||
604 | memcpy(req_packets, packet + 10, 4 * number); | 537 | memcpy(req_packets, packet + 10, 4 * number); |
605 | } | ||
606 | if(connection == -1) | 538 | if(connection == -1) |
607 | { | ||
608 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); | 539 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); |
609 | } | ||
610 | if(connections[connection].status == 2) | 540 | if(connections[connection].status == 2) |
611 | { | ||
612 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); | 541 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); |
613 | } | ||
614 | if(connections[connection].status == 3) | 542 | if(connections[connection].status == 3) |
615 | { | ||
616 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); | 543 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); |
617 | } | ||
618 | return 0; | 544 | return 0; |
619 | } | 545 | } |
620 | 546 | ||
@@ -623,37 +549,26 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
623 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) | 549 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) |
624 | { | 550 | { |
625 | if(size > MAX_DATA_SIZE) | 551 | if(size > MAX_DATA_SIZE) |
626 | { | ||
627 | return 1; | 552 | return 1; |
628 | } | ||
629 | 553 | ||
630 | uint32_t i; | 554 | uint32_t i; |
631 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; | 555 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; |
632 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; | 556 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; |
633 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) | 557 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
634 | { | 558 | if(i == data_num) { |
635 | if(i == data_num) | ||
636 | { | ||
637 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); | 559 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); |
638 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; | 560 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; |
639 | connections[connection_id].last_recvdata = current_time(); | 561 | connections[connection_id].last_recvdata = current_time(); |
640 | if(sent_packet < BUFFER_PACKET_NUM) | 562 | if(sent_packet < BUFFER_PACKET_NUM) |
641 | { | ||
642 | connections[connection_id].osent_packetnum = data_num; | 563 | connections[connection_id].osent_packetnum = data_num; |
643 | } | ||
644 | break; | 564 | break; |
645 | } | 565 | } |
646 | } | 566 | } |
647 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) | 567 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
648 | { | ||
649 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) | 568 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) |
650 | { | ||
651 | connections[connection_id].recv_packetnum = i; | 569 | connections[connection_id].recv_packetnum = i; |
652 | } | ||
653 | else | 570 | else |
654 | { | ||
655 | break; | 571 | break; |
656 | } | ||
657 | } | 572 | } |
658 | 573 | ||
659 | return 0; | 574 | return 0; |
@@ -662,53 +577,43 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size | |||
662 | int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | 577 | int handle_data(uint8_t * packet, uint32_t length, IP_Port source) |
663 | { | 578 | { |
664 | int connection = getconnection_id(source); | 579 | int connection = getconnection_id(source); |
665 | 580 | ||
666 | if(connection == -1) | 581 | if(connection == -1) |
667 | { | ||
668 | return 1; | 582 | return 1; |
669 | } | 583 | |
670 | |||
671 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ | 584 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ |
672 | { | ||
673 | return 1; | 585 | return 1; |
674 | } | 586 | |
675 | |||
676 | if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) | 587 | if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) |
677 | { | ||
678 | return 1; | 588 | return 1; |
679 | } | ||
680 | uint32_t temp; | 589 | uint32_t temp; |
681 | uint32_t number; | 590 | uint32_t number; |
682 | uint16_t size = length - 1 - 4; | 591 | uint16_t size = length - 1 - 4; |
683 | 592 | ||
684 | memcpy(&temp, packet + 1, 4); | 593 | memcpy(&temp, packet + 1, 4); |
685 | number = ntohl(temp); | 594 | number = ntohl(temp); |
686 | return add_recv(connection, number, packet + 5, size); | 595 | return add_recv(connection, number, packet + 5, size); |
687 | |||
688 | } | 596 | } |
689 | 597 | ||
690 | /* END of packet handling functions */ | 598 | /* END of packet handling functions */ |
691 | 599 | ||
692 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 600 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) |
693 | { | 601 | { |
694 | |||
695 | switch (packet[0]) { | 602 | switch (packet[0]) { |
696 | case 16: | 603 | case 16: |
697 | return handle_handshake(packet, length, source); | 604 | return handle_handshake(packet, length, source); |
698 | 605 | ||
699 | case 17: | 606 | case 17: |
700 | return handle_SYNC(packet, length, source); | 607 | return handle_SYNC(packet, length, source); |
701 | 608 | ||
702 | case 18: | 609 | case 18: |
703 | return handle_data(packet, length, source); | 610 | return handle_data(packet, length, source); |
704 | 611 | ||
705 | default: | 612 | default: |
706 | return 1; | 613 | return 1; |
707 | |||
708 | } | 614 | } |
709 | 615 | ||
710 | return 0; | 616 | return 0; |
711 | |||
712 | } | 617 | } |
713 | 618 | ||
714 | /* Send handshake requests | 619 | /* Send handshake requests |
@@ -717,28 +622,20 @@ void doNew() | |||
717 | { | 622 | { |
718 | uint32_t i; | 623 | uint32_t i; |
719 | uint64_t temp_time = current_time(); | 624 | uint64_t temp_time = current_time(); |
720 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 625 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
721 | { | ||
722 | if(connections[i].status == 1) | 626 | if(connections[i].status == 1) |
723 | { | 627 | if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
724 | if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) | ||
725 | { | ||
726 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); | 628 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); |
727 | connections[i].last_sent = temp_time; | 629 | connections[i].last_sent = temp_time; |
728 | } | 630 | } |
729 | 631 | ||
730 | } | ||
731 | /* kill all timed out connections */ | 632 | /* kill all timed out connections */ |
732 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && | 633 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && |
733 | connections[i].status != 4) | 634 | connections[i].status != 4) |
734 | { | ||
735 | /* kill_connection(i); */ | 635 | /* kill_connection(i); */ |
736 | connections[i].status = 4; | 636 | connections[i].status = 4; |
737 | } | ||
738 | if(connections[i].status > 0 && connections[i].killat < temp_time) | 637 | if(connections[i].status > 0 && connections[i].killat < temp_time) |
739 | { | ||
740 | kill_connection(i); | 638 | kill_connection(i); |
741 | } | ||
742 | } | 639 | } |
743 | } | 640 | } |
744 | 641 | ||
@@ -746,16 +643,12 @@ void doSYNC() | |||
746 | { | 643 | { |
747 | uint32_t i; | 644 | uint32_t i; |
748 | uint64_t temp_time = current_time(); | 645 | uint64_t temp_time = current_time(); |
749 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 646 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
750 | { | ||
751 | if(connections[i].status == 2 || connections[i].status == 3) | 647 | if(connections[i].status == 2 || connections[i].status == 3) |
752 | { | 648 | if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
753 | if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) | ||
754 | { | ||
755 | send_SYNC(i); | 649 | send_SYNC(i); |
756 | connections[i].last_SYNC = temp_time; | 650 | connections[i].last_SYNC = temp_time; |
757 | } | 651 | } |
758 | } | ||
759 | } | 652 | } |
760 | } | 653 | } |
761 | 654 | ||
@@ -765,19 +658,12 @@ void doData() | |||
765 | uint64_t j; | 658 | uint64_t j; |
766 | uint64_t temp_time = current_time(); | 659 | uint64_t temp_time = current_time(); |
767 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 660 | for(i = 0; i < MAX_CONNECTIONS; ++i) |
768 | { | ||
769 | if(connections[i].status == 3 && sendqueue(i) != 0) | 661 | if(connections[i].status == 3 && sendqueue(i) != 0) |
770 | { | 662 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { |
771 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) | ||
772 | { | ||
773 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) | 663 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) |
774 | { | ||
775 | send_DATA(i); | 664 | send_DATA(i); |
776 | } | ||
777 | connections[i].last_sent = temp_time; | 665 | connections[i].last_sent = temp_time; |
778 | } | 666 | } |
779 | } | ||
780 | } | ||
781 | } | 667 | } |
782 | 668 | ||
783 | /* TODO: flow control. | 669 | /* TODO: flow control. |
@@ -789,32 +675,20 @@ void adjustRates() | |||
789 | { | 675 | { |
790 | uint32_t i; | 676 | uint32_t i; |
791 | uint64_t temp_time = current_time(); | 677 | uint64_t temp_time = current_time(); |
792 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 678 | for(i = 0; i < MAX_CONNECTIONS; ++i) { |
793 | { | ||
794 | if(connections[i].status == 1 || connections[i].status == 2) | 679 | if(connections[i].status == 1 || connections[i].status == 2) |
795 | { | ||
796 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 680 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
797 | } | 681 | if(connections[i].status == 3) { |
798 | if(connections[i].status == 3) | 682 | if(sendqueue(i) != 0) { |
799 | { | ||
800 | if(sendqueue(i) != 0) | ||
801 | { | ||
802 | |||
803 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; | 683 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; |
804 | |||
805 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 684 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
806 | |||
807 | } | 685 | } |
808 | else if(connections[i].last_recvdata + 1000000UL > temp_time) | 686 | else if(connections[i].last_recvdata + 1000000UL > temp_time) |
809 | { | ||
810 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 687 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
811 | } | ||
812 | else | 688 | else |
813 | { | ||
814 | connections[i].SYNC_rate = SYNC_RATE; | 689 | connections[i].SYNC_rate = SYNC_RATE; |
815 | } | ||
816 | } | 690 | } |
817 | } | 691 | } |
818 | } | 692 | } |
819 | 693 | ||
820 | /* Call this function a couple times per second | 694 | /* Call this function a couple times per second |
@@ -825,6 +699,4 @@ void doLossless_UDP() | |||
825 | doSYNC(); | 699 | doSYNC(); |
826 | doData(); | 700 | doData(); |
827 | adjustRates(); | 701 | adjustRates(); |
828 | |||
829 | |||
830 | } | 702 | } |
diff --git a/core/net_crypto.c b/core/net_crypto.c index 044845f0..28cb83e8 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -62,9 +62,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | 62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) |
63 | { | 63 | { |
64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) | 64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) |
65 | { | ||
66 | return -1; | 65 | return -1; |
67 | } | ||
68 | 66 | ||
69 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; | 67 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; |
70 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; | 68 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; |
@@ -76,9 +74,8 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
76 | 74 | ||
77 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ | 75 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ |
78 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 76 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
79 | { | ||
80 | return -1; | 77 | return -1; |
81 | } | 78 | |
82 | /* unpad the encrypted message */ | 79 | /* unpad the encrypted message */ |
83 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 80 | memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
84 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; | 81 | return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; |
@@ -92,9 +89,8 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
92 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | 89 | uint8_t * encrypted, uint32_t length, uint8_t * plain) |
93 | { | 90 | { |
94 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) | 91 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) |
95 | { | ||
96 | return -1; | 92 | return -1; |
97 | } | 93 | |
98 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; | 94 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; |
99 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; | 95 | uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; |
100 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; | 96 | uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; |
@@ -103,14 +99,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
103 | 99 | ||
104 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 100 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
105 | nonce, public_key, secret_key) == -1) | 101 | nonce, public_key, secret_key) == -1) |
106 | { | ||
107 | return -1; | 102 | return -1; |
108 | } | 103 | |
109 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ | 104 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ |
110 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) | 105 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) |
111 | { | ||
112 | return -1; | 106 | return -1; |
113 | } | 107 | |
114 | /* unpad the plain message */ | 108 | /* unpad the plain message */ |
115 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); | 109 | memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); |
116 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; | 110 | return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; |
@@ -120,13 +114,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
120 | void increment_nonce(uint8_t * nonce) | 114 | void increment_nonce(uint8_t * nonce) |
121 | { | 115 | { |
122 | uint32_t i; | 116 | uint32_t i; |
123 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) | 117 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) { |
124 | { | ||
125 | ++nonce[i]; | 118 | ++nonce[i]; |
126 | if(nonce[i] != 0) | 119 | if(nonce[i] != 0) |
127 | { | ||
128 | break; | 120 | break; |
129 | } | ||
130 | } | 121 | } |
131 | } | 122 | } |
132 | 123 | ||
@@ -134,8 +125,7 @@ void increment_nonce(uint8_t * nonce) | |||
134 | void random_nonce(uint8_t * nonce) | 125 | void random_nonce(uint8_t * nonce) |
135 | { | 126 | { |
136 | uint32_t i, temp; | 127 | uint32_t i, temp; |
137 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) | 128 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { |
138 | { | ||
139 | temp = random_int(); | 129 | temp = random_int(); |
140 | memcpy(nonce + 4 * i, &temp, 4); | 130 | memcpy(nonce + 4 * i, &temp, 4); |
141 | } | 131 | } |
@@ -147,28 +137,19 @@ void random_nonce(uint8_t * nonce) | |||
147 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 137 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) |
148 | { | 138 | { |
149 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 139 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
150 | { | ||
151 | return 0; | 140 | return 0; |
152 | } | ||
153 | if(crypto_connections[crypt_connection_id].status != 3) | 141 | if(crypto_connections[crypt_connection_id].status != 3) |
154 | { | ||
155 | return 0; | 142 | return 0; |
156 | } | ||
157 | uint8_t temp_data[MAX_DATA_SIZE]; | 143 | uint8_t temp_data[MAX_DATA_SIZE]; |
158 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); | 144 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); |
159 | if(length == 0) | 145 | if(length == 0) |
160 | { | ||
161 | return 0; | 146 | return 0; |
162 | } | ||
163 | if(temp_data[0] != 3) | 147 | if(temp_data[0] != 3) |
164 | { | ||
165 | return -1; | 148 | return -1; |
166 | } | ||
167 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 149 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
168 | crypto_connections[crypt_connection_id].sessionsecret_key, | 150 | crypto_connections[crypt_connection_id].sessionsecret_key, |
169 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | 151 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); |
170 | if(len != -1) | 152 | if(len != -1) { |
171 | { | ||
172 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); | 153 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); |
173 | return len; | 154 | return len; |
174 | } | 155 | } |
@@ -180,30 +161,20 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) | |||
180 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 161 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) |
181 | { | 162 | { |
182 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 163 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
183 | { | ||
184 | return 0; | 164 | return 0; |
185 | } | ||
186 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 165 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
187 | { | ||
188 | return 0; | 166 | return 0; |
189 | } | ||
190 | if(crypto_connections[crypt_connection_id].status != 3) | 167 | if(crypto_connections[crypt_connection_id].status != 3) |
191 | { | ||
192 | return 0; | 168 | return 0; |
193 | } | ||
194 | uint8_t temp_data[MAX_DATA_SIZE]; | 169 | uint8_t temp_data[MAX_DATA_SIZE]; |
195 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 170 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
196 | crypto_connections[crypt_connection_id].sessionsecret_key, | 171 | crypto_connections[crypt_connection_id].sessionsecret_key, |
197 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | 172 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); |
198 | if(len == -1) | 173 | if(len == -1) |
199 | { | ||
200 | return 0; | 174 | return 0; |
201 | } | ||
202 | temp_data[0] = 3; | 175 | temp_data[0] = 3; |
203 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) | 176 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) |
204 | { | ||
205 | return 0; | 177 | return 0; |
206 | } | ||
207 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); | 178 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); |
208 | return 1; | 179 | return 1; |
209 | } | 180 | } |
@@ -217,17 +188,13 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
217 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) | 188 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) |
218 | { | 189 | { |
219 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) | 190 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) |
220 | { | ||
221 | return -1; | 191 | return -1; |
222 | } | ||
223 | uint8_t nonce[crypto_box_NONCEBYTES]; | 192 | uint8_t nonce[crypto_box_NONCEBYTES]; |
224 | random_nonce(nonce); | 193 | random_nonce(nonce); |
225 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, | 194 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
226 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); | 195 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
227 | if(len == -1) | 196 | if(len == -1) |
228 | { | ||
229 | return -1; | 197 | return -1; |
230 | } | ||
231 | packet[0] = request_id; | 198 | packet[0] = request_id; |
232 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); | 199 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); |
233 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); | 200 | memcpy(packet + 1 + crypto_box_PUBLICKEYBYTES, self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -253,15 +220,11 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1 | |||
253 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, | 220 | int len1 = decrypt_data(public_key, self_secret_key, nonce, packet + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES, |
254 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); | 221 | length - (crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1), data); |
255 | if(len1 == -1) | 222 | if(len1 == -1) |
256 | { | ||
257 | return -1; | 223 | return -1; |
258 | } | ||
259 | return len1; | 224 | return len1; |
260 | } | 225 | } |
261 | else | 226 | else |
262 | { | ||
263 | return -1; | 227 | return -1; |
264 | } | ||
265 | } | 228 | } |
266 | 229 | ||
267 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 230 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
@@ -280,9 +243,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
280 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 243 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
281 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 244 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
282 | if(len == -1) | 245 | if(len == -1) |
283 | { | ||
284 | return 0; | 246 | return 0; |
285 | } | ||
286 | temp_data[0] = 2; | 247 | temp_data[0] = 2; |
287 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | 248 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); |
288 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); | 249 | memcpy(temp_data + 1 + crypto_box_PUBLICKEYBYTES, nonce, crypto_box_NONCEBYTES); |
@@ -302,9 +263,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
302 | return 0; | 263 | return 0; |
303 | } | 264 | } |
304 | if(data[0] != 2) | 265 | if(data[0] != 2) |
305 | { | ||
306 | return 0; | 266 | return 0; |
307 | } | ||
308 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 267 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
309 | 268 | ||
310 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); | 269 | memcpy(public_key, data + 1, crypto_box_PUBLICKEYBYTES); |
@@ -314,9 +273,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
314 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); | 273 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); |
315 | 274 | ||
316 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) | 275 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) |
317 | { | ||
318 | return 0; | 276 | return 0; |
319 | } | ||
320 | 277 | ||
321 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 278 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
322 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); | 279 | memcpy(session_key, temp + crypto_box_NONCEBYTES, crypto_box_PUBLICKEYBYTES); |
@@ -330,15 +287,9 @@ int getcryptconnection_id(uint8_t * public_key) | |||
330 | { | 287 | { |
331 | uint32_t i; | 288 | uint32_t i; |
332 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 289 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
333 | { | ||
334 | if(crypto_connections[i].status > 0) | 290 | if(crypto_connections[i].status > 0) |
335 | { | ||
336 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 291 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
337 | { | ||
338 | return i; | 292 | return i; |
339 | } | ||
340 | } | ||
341 | } | ||
342 | return -1; | 293 | return -1; |
343 | } | 294 | } |
344 | 295 | ||
@@ -349,23 +300,17 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
349 | { | 300 | { |
350 | uint32_t i; | 301 | uint32_t i; |
351 | int id = getcryptconnection_id(public_key); | 302 | int id = getcryptconnection_id(public_key); |
352 | if(id != -1) | 303 | if(id != -1) { |
353 | { | ||
354 | IP_Port c_ip = connection_ip(crypto_connections[id].number); | 304 | IP_Port c_ip = connection_ip(crypto_connections[id].number); |
355 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) | 305 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) |
356 | { | ||
357 | return -1; | 306 | return -1; |
358 | } | ||
359 | } | 307 | } |
360 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 308 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
361 | { | 309 | { |
362 | if(crypto_connections[i].status == 0) | 310 | if(crypto_connections[i].status == 0) { |
363 | { | ||
364 | int id = new_connection(ip_port); | 311 | int id = new_connection(ip_port); |
365 | if(id == -1) | 312 | if(id == -1) |
366 | { | ||
367 | return -1; | 313 | return -1; |
368 | } | ||
369 | crypto_connections[i].number = id; | 314 | crypto_connections[i].number = id; |
370 | crypto_connections[i].status = 1; | 315 | crypto_connections[i].status = 1; |
371 | random_nonce(crypto_connections[i].recv_nonce); | 316 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -396,20 +341,16 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
396 | uint32_t i; | 341 | uint32_t i; |
397 | for(i = 0; i < MAX_INCOMING; ++i) | 342 | for(i = 0; i < MAX_INCOMING; ++i) |
398 | { | 343 | { |
399 | if(incoming_connections[i] != -1) | 344 | if(incoming_connections[i] != -1) { |
400 | { | 345 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) { |
401 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) | ||
402 | { | ||
403 | kill_connection(incoming_connections[i]); | 346 | kill_connection(incoming_connections[i]); |
404 | incoming_connections[i] = -1; | 347 | incoming_connections[i] = -1; |
405 | continue; | 348 | continue; |
406 | } | 349 | } |
407 | if(id_packet(incoming_connections[i]) == 2) | 350 | if(id_packet(incoming_connections[i]) == 2) { |
408 | { | ||
409 | uint8_t temp_data[MAX_DATA_SIZE]; | 351 | uint8_t temp_data[MAX_DATA_SIZE]; |
410 | uint16_t len = read_packet(incoming_connections[i], temp_data); | 352 | uint16_t len = read_packet(incoming_connections[i], temp_data); |
411 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 353 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
412 | { | ||
413 | int connection_id = incoming_connections[i]; | 354 | int connection_id = incoming_connections[i]; |
414 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ | 355 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ |
415 | return connection_id; | 356 | return connection_id; |
@@ -426,11 +367,8 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
426 | int crypto_kill(int crypt_connection_id) | 367 | int crypto_kill(int crypt_connection_id) |
427 | { | 368 | { |
428 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 369 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
429 | { | ||
430 | return 1; | 370 | return 1; |
431 | } | 371 | if(crypto_connections[crypt_connection_id].status != 0) { |
432 | if(crypto_connections[crypt_connection_id].status != 0) | ||
433 | { | ||
434 | crypto_connections[crypt_connection_id].status = 0; | 372 | crypto_connections[crypt_connection_id].status = 0; |
435 | kill_connection(crypto_connections[crypt_connection_id].number); | 373 | kill_connection(crypto_connections[crypt_connection_id].number); |
436 | crypto_connections[crypt_connection_id].number = ~0; | 374 | crypto_connections[crypt_connection_id].number = ~0; |
@@ -446,9 +384,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
446 | { | 384 | { |
447 | uint32_t i; | 385 | uint32_t i; |
448 | if(connection_id == -1) | 386 | if(connection_id == -1) |
449 | { | ||
450 | return -1; | 387 | return -1; |
451 | } | ||
452 | /* | 388 | /* |
453 | if(getcryptconnection_id(public_key) != -1) | 389 | if(getcryptconnection_id(public_key) != -1) |
454 | { | 390 | { |
@@ -456,8 +392,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
456 | }*/ | 392 | }*/ |
457 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 393 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
458 | { | 394 | { |
459 | if(crypto_connections[i].status == 0) | 395 | if(crypto_connections[i].status == 0) { |
460 | { | ||
461 | crypto_connections[i].number = connection_id; | 396 | crypto_connections[i].number = connection_id; |
462 | crypto_connections[i].status = 2; | 397 | crypto_connections[i].status = 2; |
463 | random_nonce(crypto_connections[i].recv_nonce); | 398 | random_nonce(crypto_connections[i].recv_nonce); |
@@ -490,9 +425,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
490 | int is_cryptoconnected(int crypt_connection_id) | 425 | int is_cryptoconnected(int crypt_connection_id) |
491 | { | 426 | { |
492 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 427 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
493 | { | ||
494 | return crypto_connections[crypt_connection_id].status; | 428 | return crypto_connections[crypt_connection_id].status; |
495 | } | ||
496 | return 0; | 429 | return 0; |
497 | } | 430 | } |
498 | 431 | ||
@@ -526,10 +459,8 @@ void load_keys(uint8_t * keys) | |||
526 | int new_incoming(int id) | 459 | int new_incoming(int id) |
527 | { | 460 | { |
528 | uint32_t i; | 461 | uint32_t i; |
529 | for(i = 0; i < MAX_INCOMING; ++i) | 462 | for(i = 0; i < MAX_INCOMING; ++i) { |
530 | { | 463 | if(incoming_connections[i] == -1) { |
531 | if(incoming_connections[i] == -1) | ||
532 | { | ||
533 | incoming_connections[i] = id; | 464 | incoming_connections[i] = id; |
534 | return 0; | 465 | return 0; |
535 | } | 466 | } |
@@ -542,13 +473,10 @@ int new_incoming(int id) | |||
542 | static void handle_incomings() | 473 | static void handle_incomings() |
543 | { | 474 | { |
544 | int income; | 475 | int income; |
545 | while(1) | 476 | while(1) { |
546 | { | ||
547 | income = incoming_connection(); | 477 | income = incoming_connection(); |
548 | if(income == -1 || new_incoming(income) ) | 478 | if(income == -1 || new_incoming(income) ) |
549 | { | ||
550 | break; | 479 | break; |
551 | } | ||
552 | } | 480 | } |
553 | } | 481 | } |
554 | 482 | ||
@@ -567,17 +495,11 @@ static void receive_crypto() | |||
567 | uint16_t len; | 495 | uint16_t len; |
568 | if(id_packet(crypto_connections[i].number) == 1) | 496 | if(id_packet(crypto_connections[i].number) == 1) |
569 | /* if the packet is a friend request drop it (because we are already friends) */ | 497 | /* if the packet is a friend request drop it (because we are already friends) */ |
570 | { | ||
571 | len = read_packet(crypto_connections[i].number, temp_data); | 498 | len = read_packet(crypto_connections[i].number, temp_data); |
572 | 499 | if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ | |
573 | } | ||
574 | if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */ | ||
575 | { | ||
576 | len = read_packet(crypto_connections[i].number, temp_data); | 500 | len = read_packet(crypto_connections[i].number, temp_data); |
577 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) | 501 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
578 | { | 502 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
579 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | ||
580 | { | ||
581 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 503 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
582 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 504 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
583 | increment_nonce(crypto_connections[i].sent_nonce); | 505 | increment_nonce(crypto_connections[i].sent_nonce); |
@@ -589,17 +511,14 @@ static void receive_crypto() | |||
589 | } | 511 | } |
590 | } | 512 | } |
591 | else if(id_packet(crypto_connections[i].number) != -1) | 513 | else if(id_packet(crypto_connections[i].number) != -1) |
592 | { | ||
593 | /* This should not happen | 514 | /* This should not happen |
594 | kill the connection if it does */ | 515 | kill the connection if it does */ |
595 | crypto_kill(crypto_connections[i].number); | 516 | crypto_kill(crypto_connections[i].number); |
596 | } | ||
597 | 517 | ||
598 | } | 518 | } |
599 | if(crypto_connections[i].status == 2) | 519 | if(crypto_connections[i].status == 2) |
600 | { | 520 | { |
601 | if(id_packet(crypto_connections[i].number) == 3) | 521 | if(id_packet(crypto_connections[i].number) == 3) { |
602 | { | ||
603 | uint8_t temp_data[MAX_DATA_SIZE]; | 522 | uint8_t temp_data[MAX_DATA_SIZE]; |
604 | uint8_t data[MAX_DATA_SIZE]; | 523 | uint8_t data[MAX_DATA_SIZE]; |
605 | int length = read_packet(crypto_connections[i].number, temp_data); | 524 | int length = read_packet(crypto_connections[i].number, temp_data); |
@@ -607,8 +526,7 @@ static void receive_crypto() | |||
607 | crypto_connections[i].sessionsecret_key, | 526 | crypto_connections[i].sessionsecret_key, |
608 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | 527 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); |
609 | uint32_t zero = 0; | 528 | uint32_t zero = 0; |
610 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) | 529 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { |
611 | { | ||
612 | increment_nonce(crypto_connections[i].recv_nonce); | 530 | increment_nonce(crypto_connections[i].recv_nonce); |
613 | crypto_connections[i].status = 3; | 531 | crypto_connections[i].status = 3; |
614 | 532 | ||
@@ -616,18 +534,14 @@ static void receive_crypto() | |||
616 | kill_connection_in(crypto_connections[i].number, 3000000); | 534 | kill_connection_in(crypto_connections[i].number, 3000000); |
617 | } | 535 | } |
618 | else | 536 | else |
619 | { | ||
620 | /* This should not happen | 537 | /* This should not happen |
621 | kill the connection if it does */ | 538 | kill the connection if it does */ |
622 | crypto_kill(crypto_connections[i].number); | 539 | crypto_kill(crypto_connections[i].number); |
623 | } | ||
624 | } | 540 | } |
625 | else if(id_packet(crypto_connections[i].number) != -1) | 541 | else if(id_packet(crypto_connections[i].number) != -1) |
626 | { | ||
627 | /* This should not happen | 542 | /* This should not happen |
628 | kill the connection if it does */ | 543 | kill the connection if it does */ |
629 | crypto_kill(crypto_connections[i].number); | 544 | crypto_kill(crypto_connections[i].number); |
630 | } | ||
631 | } | 545 | } |
632 | } | 546 | } |
633 | } | 547 | } |
@@ -640,22 +554,16 @@ void initNetCrypto() | |||
640 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 554 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
641 | uint32_t i; | 555 | uint32_t i; |
642 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 556 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
643 | { | ||
644 | crypto_connections[i].number = ~0; | 557 | crypto_connections[i].number = ~0; |
645 | } | ||
646 | } | 558 | } |
647 | 559 | ||
648 | static void killTimedout() | 560 | static void killTimedout() |
649 | { | 561 | { |
650 | uint32_t i; | 562 | uint32_t i; |
651 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 563 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
652 | { | ||
653 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) | 564 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) |
654 | { | ||
655 | crypto_connections[i].status = 4; | 565 | crypto_connections[i].status = 4; |
656 | } | 566 | else if(is_connected(crypto_connections[i].number) == 4) { |
657 | else if(is_connected(crypto_connections[i].number) == 4) | ||
658 | { | ||
659 | kill_connection(crypto_connections[i].number); | 567 | kill_connection(crypto_connections[i].number); |
660 | crypto_connections[i].number = ~0; | 568 | crypto_connections[i].number = ~0; |
661 | } | 569 | } |
@@ -671,4 +579,4 @@ void doNetCrypto() | |||
671 | handle_incomings(); | 579 | handle_incomings(); |
672 | receive_crypto(); | 580 | receive_crypto(); |
673 | killTimedout(); | 581 | killTimedout(); |
674 | } \ No newline at end of file | 582 | } |