diff options
author | irungentoo <irungentoo@gmail.com> | 2013-09-22 11:08:23 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-09-22 11:08:23 -0400 |
commit | 83c6e9dd357a64cb60ebeb33dd96d85256820366 (patch) | |
tree | 6fcdd6b7b2cada55918607e0fa9b7b693bd28aa4 | |
parent | 1cc47101fec6a13a3eb5894cdb029420d3ae0ec4 (diff) |
Fixed the connection bug and cleaned up some stuff.
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 2 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.c | 28 | ||||
-rw-r--r-- | toxcore/Lossless_UDP.h | 13 | ||||
-rw-r--r-- | toxcore/Messenger.c | 4 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 69 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 5 |
6 files changed, 55 insertions, 66 deletions
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 8deace82..5a89b00e 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c | |||
@@ -185,7 +185,7 @@ int main(int argc, char *argv[]) | |||
185 | while (1) { | 185 | while (1) { |
186 | networking_poll(ludp->net); | 186 | networking_poll(ludp->net); |
187 | do_lossless_udp(ludp); | 187 | do_lossless_udp(ludp); |
188 | connection = incoming_connection(ludp); | 188 | connection = incoming_connection(ludp, 0); |
189 | 189 | ||
190 | if (connection != -1) { | 190 | if (connection != -1) { |
191 | if (is_connected(ludp, connection) == 2) { | 191 | if (is_connected(ludp, connection) == 2) { |
diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index 25d38b8d..feae6767 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c | |||
@@ -303,13 +303,13 @@ static int new_inconnection(Lossless_UDP *ludp, IP_Port ip_port) | |||
303 | } | 303 | } |
304 | 304 | ||
305 | /* | 305 | /* |
306 | * return an integer corresponding to the next connection in our incoming connection list. | 306 | * return an integer corresponding to the next connection in our incoming connection list with at least numpackets in the recieve queue. |
307 | * return -1 if there are no new incoming connections in the list. | 307 | * return -1 if there are no new incoming connections in the list. |
308 | */ | 308 | */ |
309 | int incoming_connection(Lossless_UDP *ludp) | 309 | int incoming_connection(Lossless_UDP *ludp, uint32_t numpackets) |
310 | { | 310 | { |
311 | tox_array_for_each(&ludp->connections, Connection, tmp) { | 311 | tox_array_for_each(&ludp->connections, Connection, tmp) { |
312 | if (tmp->inbound == 2) { | 312 | if (tmp->inbound == 2 && tmp->recv_packetnum - tmp->successful_read >= numpackets) { |
313 | tmp->inbound = 1; | 313 | tmp->inbound = 1; |
314 | return tmp_i; | 314 | return tmp_i; |
315 | } | 315 | } |
@@ -392,6 +392,27 @@ int is_connected(Lossless_UDP *ludp, int connection_id) | |||
392 | return 0; | 392 | return 0; |
393 | } | 393 | } |
394 | 394 | ||
395 | /* Check if connection is confirmed. | ||
396 | * | ||
397 | * returns 1 if yes. | ||
398 | * returns 0 if no/failure. | ||
399 | */ | ||
400 | int connection_confirmed(Lossless_UDP *ludp, int connection_id) | ||
401 | { | ||
402 | if ((unsigned int)connection_id >= ludp->connections.len) | ||
403 | return 0; | ||
404 | |||
405 | Connection *connection = &tox_array_get(&ludp->connections, connection_id, Connection); | ||
406 | |||
407 | if (connection->status == 0) | ||
408 | return 0; | ||
409 | |||
410 | if (connection->confirmed == 1) | ||
411 | return 1; | ||
412 | |||
413 | return 0; | ||
414 | } | ||
415 | |||
395 | /* Confirm an incoming connection. | 416 | /* Confirm an incoming connection. |
396 | * Also disables the auto kill timeout on incomming connections. | 417 | * Also disables the auto kill timeout on incomming connections. |
397 | * | 418 | * |
@@ -410,6 +431,7 @@ int confirm_connection(Lossless_UDP *ludp, int connection_id) | |||
410 | 431 | ||
411 | connection->killat = ~0; | 432 | connection->killat = ~0; |
412 | connection->confirmed = 1; | 433 | connection->confirmed = 1; |
434 | connection->inbound = 0; | ||
413 | return 0; | 435 | return 0; |
414 | } | 436 | } |
415 | 437 | ||
diff --git a/toxcore/Lossless_UDP.h b/toxcore/Lossless_UDP.h index 9b5c2406..2ef6f4d9 100644 --- a/toxcore/Lossless_UDP.h +++ b/toxcore/Lossless_UDP.h | |||
@@ -155,10 +155,11 @@ int new_connection(Lossless_UDP *ludp, IP_Port ip_port); | |||
155 | */ | 155 | */ |
156 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); | 156 | int getconnection_id(Lossless_UDP *ludp, IP_Port ip_port); |
157 | 157 | ||
158 | /* return an integer corresponding to the next connection in our imcoming connection list. | 158 | /* |
159 | * return an integer corresponding to the next connection in our incoming connection list with at least numpackets in the recieve queue. | ||
159 | * return -1 if there are no new incoming connections in the list. | 160 | * return -1 if there are no new incoming connections in the list. |
160 | */ | 161 | */ |
161 | int incoming_connection(Lossless_UDP *ludp); | 162 | int incoming_connection(Lossless_UDP *ludp, uint32_t numpackets); |
162 | 163 | ||
163 | /* return -1 if it could not kill the connection. | 164 | /* return -1 if it could not kill the connection. |
164 | * return 0 if killed successfully. | 165 | * return 0 if killed successfully. |
@@ -173,6 +174,14 @@ int kill_connection(Lossless_UDP *ludp, int connection_id); | |||
173 | */ | 174 | */ |
174 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); | 175 | int kill_connection_in(Lossless_UDP *ludp, int connection_id, uint32_t seconds); |
175 | 176 | ||
177 | |||
178 | /* Check if connection is confirmed. | ||
179 | * | ||
180 | * returns 1 if yes. | ||
181 | * returns 0 if no. | ||
182 | */ | ||
183 | int connection_confirmed(Lossless_UDP *ludp, int connection_id); | ||
184 | |||
176 | /* Confirm an incoming connection. | 185 | /* Confirm an incoming connection. |
177 | * Also disables the auto kill timeout on incomming connections. | 186 | * Also disables the auto kill timeout on incomming connections. |
178 | * | 187 | * |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index d595c67d..43db2a9d 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1232,7 +1232,7 @@ void doInbound(Messenger *m) | |||
1232 | 1232 | ||
1233 | if (friend_id != -1) { | 1233 | if (friend_id != -1) { |
1234 | if (m_get_friend_connectionstatus(m, friend_id) == 1) { | 1234 | if (m_get_friend_connectionstatus(m, friend_id) == 1) { |
1235 | crypto_kill(m->net_crypto, inconnection); | 1235 | kill_connection(m->net_crypto->lossless_udp, inconnection); |
1236 | return; | 1236 | return; |
1237 | } | 1237 | } |
1238 | 1238 | ||
@@ -1242,8 +1242,6 @@ void doInbound(Messenger *m) | |||
1242 | 1242 | ||
1243 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); | 1243 | set_friend_status(m, friend_id, FRIEND_CONFIRMED); |
1244 | } | 1244 | } |
1245 | |||
1246 | crypto_kill(m->net_crypto, inconnection); | ||
1247 | } | 1245 | } |
1248 | } | 1246 | } |
1249 | 1247 | ||
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 41c8c45c..2da2ba3b 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -503,27 +503,28 @@ int crypto_connect(Net_Crypto *c, uint8_t *public_key, IP_Port ip_port) | |||
503 | */ | 503 | */ |
504 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) | 504 | int crypto_inbound(Net_Crypto *c, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
505 | { | 505 | { |
506 | uint32_t i; | 506 | uint32_t i, j; |
507 | |||
508 | while (1) { | ||
509 | int incoming_con = incoming_connection(c->lossless_udp, 1); | ||
507 | 510 | ||
508 | for (i = 0; i < MAX_INCOMING; ++i) { | 511 | if (incoming_con != -1) { |
509 | if (c->incoming_connections[i] != -1) { | 512 | if (is_connected(c->lossless_udp, incoming_con) == 4 |
510 | if (is_connected(c->lossless_udp, c->incoming_connections[i]) == 4 | 513 | || is_connected(c->lossless_udp, incoming_con) == 0) { |
511 | || is_connected(c->lossless_udp, c->incoming_connections[i]) == 0) { | 514 | kill_connection(c->lossless_udp, incoming_con); |
512 | kill_connection(c->lossless_udp, c->incoming_connections[i]); | ||
513 | c->incoming_connections[i] = -1; | ||
514 | continue; | 515 | continue; |
515 | } | 516 | } |
516 | 517 | ||
517 | if (id_packet(c->lossless_udp, c->incoming_connections[i]) == 2) { | 518 | if (id_packet(c->lossless_udp, incoming_con) == 2) { |
518 | uint8_t temp_data[MAX_DATA_SIZE]; | 519 | uint8_t temp_data[MAX_DATA_SIZE]; |
519 | uint16_t len = read_packet_silent(c->lossless_udp, c->incoming_connections[i], temp_data); | 520 | uint16_t len = read_packet_silent(c->lossless_udp, incoming_con, temp_data); |
520 | 521 | ||
521 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { | 522 | if (handle_cryptohandshake(c, public_key, secret_nonce, session_key, temp_data, len)) { |
522 | int connection_id = c->incoming_connections[i]; | 523 | return incoming_con; |
523 | c->incoming_connections[i] = -1; /* Remove this connection from the incoming connection list. */ | ||
524 | return connection_id; | ||
525 | } | 524 | } |
526 | } | 525 | } |
526 | } else { | ||
527 | break; | ||
527 | } | 528 | } |
528 | } | 529 | } |
529 | 530 | ||
@@ -579,6 +580,10 @@ int accept_crypto_inbound(Net_Crypto *c, int connection_id, uint8_t *public_key, | |||
579 | * return -1; | 580 | * return -1; |
580 | * } | 581 | * } |
581 | */ | 582 | */ |
583 | |||
584 | /* Connection is accepted. */ | ||
585 | confirm_connection(c->lossless_udp, connection_id); | ||
586 | |||
582 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 | 587 | if (realloc_cryptoconnection(c, c->crypto_connections_length + 1) == -1 |
583 | || c->crypto_connections == NULL) | 588 | || c->crypto_connections == NULL) |
584 | return -1; | 589 | return -1; |
@@ -659,41 +664,6 @@ void load_keys(Net_Crypto *c, uint8_t *keys) | |||
659 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 664 | memcpy(c->self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
660 | } | 665 | } |
661 | 666 | ||
662 | /* Adds an incoming connection to the incoming_connection list. | ||
663 | * TODO: Optimize this. | ||
664 | * | ||
665 | * returns 0 if successful | ||
666 | * returns 1 if failure. | ||
667 | */ | ||
668 | static int new_incoming(Net_Crypto *c, int id) | ||
669 | { | ||
670 | uint32_t i; | ||
671 | |||
672 | for (i = 0; i < MAX_INCOMING; ++i) { | ||
673 | if (c->incoming_connections[i] == -1) { | ||
674 | c->incoming_connections[i] = id; | ||
675 | return 0; | ||
676 | } | ||
677 | } | ||
678 | |||
679 | return 1; | ||
680 | } | ||
681 | |||
682 | /* Handle all new incoming connections. | ||
683 | * TODO: Optimize this. | ||
684 | */ | ||
685 | static void handle_incomings(Net_Crypto *c) | ||
686 | { | ||
687 | int income; | ||
688 | |||
689 | while (1) { | ||
690 | income = incoming_connection(c->lossless_udp); | ||
691 | |||
692 | if (income == -1 || new_incoming(c, income) ) | ||
693 | break; | ||
694 | } | ||
695 | } | ||
696 | |||
697 | /* Handle received packets for not yet established crypto connections. */ | 667 | /* Handle received packets for not yet established crypto connections. */ |
698 | static void receive_crypto(Net_Crypto *c) | 668 | static void receive_crypto(Net_Crypto *c) |
699 | { | 669 | { |
@@ -748,9 +718,6 @@ static void receive_crypto(Net_Crypto *c) | |||
748 | c->crypto_connections[i].sessionsecret_key, | 718 | c->crypto_connections[i].sessionsecret_key, |
749 | c->crypto_connections[i].shared_key); | 719 | c->crypto_connections[i].shared_key); |
750 | c->crypto_connections[i].status = CONN_ESTABLISHED; | 720 | c->crypto_connections[i].status = CONN_ESTABLISHED; |
751 | |||
752 | /* Connection is accepted. */ | ||
753 | confirm_connection(c->lossless_udp, c->crypto_connections[i].number); | ||
754 | } else { | 721 | } else { |
755 | /* This should not happen, kill the connection if it does. */ | 722 | /* This should not happen, kill the connection if it does. */ |
756 | crypto_kill(c, i); | 723 | crypto_kill(c, i); |
@@ -785,7 +752,6 @@ Net_Crypto *new_net_crypto(Networking_Core *net) | |||
785 | return NULL; | 752 | return NULL; |
786 | } | 753 | } |
787 | 754 | ||
788 | memset(temp->incoming_connections, -1 , sizeof(int) * MAX_INCOMING); | ||
789 | return temp; | 755 | return temp; |
790 | } | 756 | } |
791 | 757 | ||
@@ -810,7 +776,6 @@ static void kill_timedout(Net_Crypto *c) | |||
810 | void do_net_crypto(Net_Crypto *c) | 776 | void do_net_crypto(Net_Crypto *c) |
811 | { | 777 | { |
812 | do_lossless_udp(c->lossless_udp); | 778 | do_lossless_udp(c->lossless_udp); |
813 | handle_incomings(c); | ||
814 | kill_timedout(c); | 779 | kill_timedout(c); |
815 | receive_crypto(c); | 780 | receive_crypto(c); |
816 | } | 781 | } |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index 55c1e3e3..f8eeb424 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -26,8 +26,6 @@ | |||
26 | 26 | ||
27 | #include "Lossless_UDP.h" | 27 | #include "Lossless_UDP.h" |
28 | 28 | ||
29 | #define MAX_INCOMING 64 | ||
30 | |||
31 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ | 29 | #define CRYPTO_PACKET_FRIEND_REQ 32 /* Friend request crypto packet ID. */ |
32 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ | 30 | #define CRYPTO_PACKET_NAT_PING 254 /* NAT ping crypto packet ID. */ |
33 | 31 | ||
@@ -66,9 +64,6 @@ typedef struct { | |||
66 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 64 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
67 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 65 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
68 | 66 | ||
69 | /* keeps track of the connection numbers for friends request so we can check later if they were sent. */ | ||
70 | int incoming_connections[MAX_INCOMING]; | ||
71 | |||
72 | Cryptopacket_Handles cryptopackethandlers[256]; | 67 | Cryptopacket_Handles cryptopackethandlers[256]; |
73 | } Net_Crypto; | 68 | } Net_Crypto; |
74 | 69 | ||