summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-07-15 20:00:02 -0400
committerirungentoo <irungentoo@gmail.com>2014-07-15 20:00:02 -0400
commit0486510edc134f1ae90af16c5ab1422034192568 (patch)
tree5889fd1481aee6c42ea3fd86ef7ad428df1ee373 /toxcore
parentae7a11cae9e220ce0a66bc48977104495759a01a (diff)
parent96249e8b020c76e9f80da6538ea19699612479dc (diff)
Merge branch 'notsecure-split-video'
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/TCP_server.c115
-rw-r--r--toxcore/TCP_server.h10
2 files changed, 111 insertions, 14 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index e7ef0d3a..f3cdd287 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -288,7 +288,7 @@ int read_packet_TCP_secure_connection(sock_t sock, uint16_t *next_packet_length,
288/* return 0 if pending data was sent completely 288/* return 0 if pending data was sent completely
289 * return -1 if it wasn't 289 * return -1 if it wasn't
290 */ 290 */
291static int send_pending_data(TCP_Secure_Connection *con) 291static int send_pending_data_nonpriority(TCP_Secure_Connection *con)
292{ 292{
293 if (con->last_packet_length == 0) { 293 if (con->last_packet_length == 0) {
294 return 0; 294 return 0;
@@ -306,25 +306,96 @@ static int send_pending_data(TCP_Secure_Connection *con)
306 return 0; 306 return 0;
307 } 307 }
308 308
309 if (len > left) 309 con->last_packet_sent += len;
310 return -1;
311
312}
313
314/* return 0 if pending data was sent completely
315 * return -1 if it wasn't
316 */
317static int send_pending_data(TCP_Secure_Connection *con)
318{
319 /* finish sending current non-priority packet */
320 if (send_pending_data_nonpriority(con) == -1) {
310 return -1; 321 return -1;
322 }
323
324 TCP_Priority_List *p = con->priority_queue_start;
325
326 while (p) {
327 uint16_t left = p->size - p->sent;
328 int len = send(con->sock, p->data + p->sent, left, MSG_NOSIGNAL);
329
330 if (len != left) {
331 if (len > 0) {
332 p->sent += len;
333 }
334
335 break;
336 }
337
338 TCP_Priority_List *pp = p;
339 p = p->next;
340 free(pp);
341 }
342
343 con->priority_queue_start = p;
344
345 if (!p) {
346 con->priority_queue_end = NULL;
347 return 0;
348 }
311 349
312 con->last_packet_sent += len;
313 return -1; 350 return -1;
351}
352
353/* return 0 on failure (only if malloc fails)
354 * return 1 on success
355 */
356static _Bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent)
357{
358 TCP_Priority_List *p = con->priority_queue_end, *new;
359 new = malloc(sizeof(TCP_Priority_List) + size);
360
361 if (!new) {
362 return 0;
363 }
314 364
365 new->next = NULL;
366 new->size = size;
367 new->sent = sent;
368 memcpy(new->data, packet, size);
369
370 if (p) {
371 p->next = new;
372 } else {
373 con->priority_queue_start = new;
374 }
375
376 con->priority_queue_end = new;
377 return 1;
315} 378}
316 379
317/* return 1 on success. 380/* return 1 on success.
318 * return 0 if could not send packet. 381 * return 0 if could not send packet.
319 * return -1 on failure (connection must be killed). 382 * return -1 on failure (connection must be killed).
320 */ 383 */
321static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length) 384static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const uint8_t *data, uint16_t length,
385 _Bool priority)
322{ 386{
323 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE) 387 if (length + crypto_box_MACBYTES > MAX_PACKET_SIZE)
324 return -1; 388 return -1;
325 389
326 if (send_pending_data(con) == -1) 390 _Bool sendpriority = 1;
327 return 0; 391
392 if (send_pending_data(con) == -1) {
393 if (priority) {
394 sendpriority = 0;
395 } else {
396 return 0;
397 }
398 }
328 399
329 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES]; 400 uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES];
330 401
@@ -335,6 +406,22 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const
335 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t))) 406 if ((unsigned int)len != (sizeof(packet) - sizeof(uint16_t)))
336 return -1; 407 return -1;
337 408
409 if (priority) {
410 len = sendpriority ? send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL) : 0;
411
412 if (len <= 0) {
413 len = 0;
414 } else {
415 increment_nonce(con->sent_nonce);
416 }
417
418 if (len == sizeof(packet)) {
419 return 1;
420 }
421
422 return add_priority(con, packet, sizeof(packet), len);
423 }
424
338 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL); 425 len = send(con->sock, packet, sizeof(packet), MSG_NOSIGNAL);
339 426
340 if (len <= 0) 427 if (len <= 0)
@@ -459,7 +546,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const
459 data[1] = rpid; 546 data[1] = rpid;
460 memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES); 547 memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES);
461 548
462 return write_packet_TCP_secure_connection(con, data, sizeof(data)); 549 return write_packet_TCP_secure_connection(con, data, sizeof(data), 1);
463} 550}
464 551
465/* return 1 on success. 552/* return 1 on success.
@@ -469,7 +556,7 @@ static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, const
469static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id) 556static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id)
470{ 557{
471 uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id + NUM_RESERVED_PORTS}; 558 uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id + NUM_RESERVED_PORTS};
472 return write_packet_TCP_secure_connection(con, data, sizeof(data)); 559 return write_packet_TCP_secure_connection(con, data, sizeof(data), 1);
473} 560}
474 561
475/* return 1 on success. 562/* return 1 on success.
@@ -479,7 +566,7 @@ static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id)
479static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id) 566static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id)
480{ 567{
481 uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id + NUM_RESERVED_PORTS}; 568 uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id + NUM_RESERVED_PORTS};
482 return write_packet_TCP_secure_connection(con, data, sizeof(data)); 569 return write_packet_TCP_secure_connection(con, data, sizeof(data), 1);
483} 570}
484 571
485/* return 0 on success. 572/* return 0 on success.
@@ -579,7 +666,7 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui
579 memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES); 666 memcpy(resp_packet + 1, con->public_key, crypto_box_PUBLICKEYBYTES);
580 memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length); 667 memcpy(resp_packet + 1 + crypto_box_PUBLICKEYBYTES, data, length);
581 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, 668 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet,
582 sizeof(resp_packet)); 669 sizeof(resp_packet), 0);
583 } 670 }
584 671
585 return 0; 672 return 0;
@@ -637,7 +724,7 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data,
637 memcpy(packet + 1, data, length); 724 memcpy(packet + 1, data, length);
638 packet[0] = TCP_PACKET_ONION_RESPONSE; 725 packet[0] = TCP_PACKET_ONION_RESPONSE;
639 726
640 if (write_packet_TCP_secure_connection(con, packet, sizeof(packet)) != 1) 727 if (write_packet_TCP_secure_connection(con, packet, sizeof(packet), 0) != 1)
641 return 1; 728 return 1;
642 729
643 return 0; 730 return 0;
@@ -682,7 +769,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
682 uint8_t response[1 + sizeof(uint64_t)]; 769 uint8_t response[1 + sizeof(uint64_t)];
683 response[0] = TCP_PACKET_PONG; 770 response[0] = TCP_PACKET_PONG;
684 memcpy(response + 1, data + 1, sizeof(uint64_t)); 771 memcpy(response + 1, data + 1, sizeof(uint64_t));
685 write_packet_TCP_secure_connection(con, response, sizeof(response)); 772 write_packet_TCP_secure_connection(con, response, sizeof(response), 1);
686 return 0; 773 return 0;
687 } 774 }
688 775
@@ -752,7 +839,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
752 uint8_t new_data[length]; 839 uint8_t new_data[length];
753 memcpy(new_data, data, length); 840 memcpy(new_data, data, length);
754 new_data[0] = other_c_id; 841 new_data[0] = other_c_id;
755 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length); 842 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0);
756 843
757 if (ret == -1) 844 if (ret == -1)
758 return -1; 845 return -1;
@@ -1058,7 +1145,7 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
1058 ++ping_id; 1145 ++ping_id;
1059 1146
1060 memcpy(ping + 1, &ping_id, sizeof(uint64_t)); 1147 memcpy(ping + 1, &ping_id, sizeof(uint64_t));
1061 int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping)); 1148 int ret = write_packet_TCP_secure_connection(conn, ping, sizeof(ping), 1);
1062 1149
1063 if (ret == 1) { 1150 if (ret == 1) {
1064 conn->last_pinged = unix_time(); 1151 conn->last_pinged = unix_time();
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index def0a978..81507acb 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -80,6 +80,14 @@ enum {
80 TCP_STATUS_CONFIRMED, 80 TCP_STATUS_CONFIRMED,
81}; 81};
82 82
83typedef struct TCP_Priority_List TCP_Priority_List;
84
85struct TCP_Priority_List {
86 TCP_Priority_List *next;
87 uint16_t size, sent;
88 uint8_t data[0];
89};
90
83typedef struct TCP_Secure_Connection { 91typedef struct TCP_Secure_Connection {
84 uint8_t status; 92 uint8_t status;
85 sock_t sock; 93 sock_t sock;
@@ -98,6 +106,8 @@ typedef struct TCP_Secure_Connection {
98 uint16_t last_packet_length; 106 uint16_t last_packet_length;
99 uint16_t last_packet_sent; 107 uint16_t last_packet_sent;
100 108
109 TCP_Priority_List *priority_queue_start, *priority_queue_end;
110
101 uint64_t identifier; 111 uint64_t identifier;
102 112
103 uint64_t last_pinged; 113 uint64_t last_pinged;