summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2018-07-12 17:22:20 +0000
committeriphydf <iphydf@users.noreply.github.com>2018-07-12 20:21:42 +0000
commitbeeb9b4335d9ca6f947a52528453753a51f194f3 (patch)
tree242cad42e2e90ebf5ed04283fd79f42f4e3afa60 /toxcore/TCP_server.c
parentcbda01021c561bd061cb03a1c1bab58199ac2307 (diff)
Style fixes in TCP code; remove MIN and PAIR from util.h.
* Moved PAIR to toxav, where it's used (but really this should die). * Replace most MIN calls with typed `min_*` calls. Didn't replace the ones where the desired semantics are unclear. Moved the MIN macro to the one place where it's still used. * Avoid assignments in `while` loops. Instead, factored out the loop body into a separate `bool`-returning function. * Use named types for callbacks (`_cb` types). * Avoid assignments in `if` conditions. * Removed `MAKE_REALLOC` and expanded its two calls. We can't have templates in C, and this fake templating is ugly and hard to analyse and debug (it expands on a single line). * Moved epoll system include to the .c file, out of the .h file. * Avoid assignments in expressions (`a = b = c;`). * Avoid multiple declarators per struct member declaration. * Fix naming inconsistencies. * Replace `net_to_host` macro with function.
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r--toxcore/TCP_server.c473
1 files changed, 251 insertions, 222 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 032b07b3..cc59d088 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -42,6 +42,21 @@
42#include "mono_time.h" 42#include "mono_time.h"
43#include "util.h" 43#include "util.h"
44 44
45#ifdef TCP_SERVER_USE_EPOLL
46#define TCP_SOCKET_LISTENING 0
47#define TCP_SOCKET_INCOMING 1
48#define TCP_SOCKET_UNCONFIRMED 2
49#define TCP_SOCKET_CONFIRMED 3
50#endif
51
52typedef struct TCP_Secure_Conn {
53 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
54 uint32_t index;
55 // TODO(iphydf): Add an enum for this (same as in TCP_client.c, probably).
56 uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
57 uint8_t other_id;
58} TCP_Secure_Conn;
59
45typedef struct TCP_Secure_Connection { 60typedef struct TCP_Secure_Connection {
46 Socket sock; 61 Socket sock;
47 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; 62 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
@@ -49,18 +64,14 @@ typedef struct TCP_Secure_Connection {
49 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */ 64 uint8_t sent_nonce[CRYPTO_NONCE_SIZE]; /* Nonce of sent packets. */
50 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; 65 uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE];
51 uint16_t next_packet_length; 66 uint16_t next_packet_length;
52 struct { 67 TCP_Secure_Conn connections[NUM_CLIENT_CONNECTIONS];
53 uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE];
54 uint32_t index;
55 uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
56 uint8_t other_id;
57 } connections[NUM_CLIENT_CONNECTIONS];
58 uint8_t last_packet[2 + MAX_PACKET_SIZE]; 68 uint8_t last_packet[2 + MAX_PACKET_SIZE];
59 uint8_t status; 69 uint8_t status;
60 uint16_t last_packet_length; 70 uint16_t last_packet_length;
61 uint16_t last_packet_sent; 71 uint16_t last_packet_sent;
62 72
63 TCP_Priority_List *priority_queue_start, *priority_queue_end; 73 TCP_Priority_List *priority_queue_start;
74 TCP_Priority_List *priority_queue_end;
64 75
65 uint64_t identifier; 76 uint64_t identifier;
66 77
@@ -118,74 +129,74 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server)
118 * return -1 if realloc fails. 129 * return -1 if realloc fails.
119 * return 0 if it succeeds. 130 * return 0 if it succeeds.
120 */ 131 */
121static int realloc_connection(TCP_Server *TCP_server, uint32_t num) 132static int realloc_connection(TCP_Server *tcp_server, uint32_t num)
122{ 133{
123 if (num == 0) { 134 if (num == 0) {
124 free(TCP_server->accepted_connection_array); 135 free(tcp_server->accepted_connection_array);
125 TCP_server->accepted_connection_array = nullptr; 136 tcp_server->accepted_connection_array = nullptr;
126 TCP_server->size_accepted_connections = 0; 137 tcp_server->size_accepted_connections = 0;
127 return 0; 138 return 0;
128 } 139 }
129 140
130 if (num == TCP_server->size_accepted_connections) { 141 if (num == tcp_server->size_accepted_connections) {
131 return 0; 142 return 0;
132 } 143 }
133 144
134 TCP_Secure_Connection *new_connections = (TCP_Secure_Connection *)realloc( 145 TCP_Secure_Connection *new_connections = (TCP_Secure_Connection *)realloc(
135 TCP_server->accepted_connection_array, 146 tcp_server->accepted_connection_array,
136 num * sizeof(TCP_Secure_Connection)); 147 num * sizeof(TCP_Secure_Connection));
137 148
138 if (new_connections == nullptr) { 149 if (new_connections == nullptr) {
139 return -1; 150 return -1;
140 } 151 }
141 152
142 if (num > TCP_server->size_accepted_connections) { 153 if (num > tcp_server->size_accepted_connections) {
143 uint32_t old_size = TCP_server->size_accepted_connections; 154 uint32_t old_size = tcp_server->size_accepted_connections;
144 uint32_t size_new_entries = (num - old_size) * sizeof(TCP_Secure_Connection); 155 uint32_t size_new_entries = (num - old_size) * sizeof(TCP_Secure_Connection);
145 memset(new_connections + old_size, 0, size_new_entries); 156 memset(new_connections + old_size, 0, size_new_entries);
146 } 157 }
147 158
148 TCP_server->accepted_connection_array = new_connections; 159 tcp_server->accepted_connection_array = new_connections;
149 TCP_server->size_accepted_connections = num; 160 tcp_server->size_accepted_connections = num;
150 return 0; 161 return 0;
151} 162}
152 163
153/* return index corresponding to connection with peer on success 164/* return index corresponding to connection with peer on success
154 * return -1 on failure. 165 * return -1 on failure.
155 */ 166 */
156static int get_TCP_connection_index(const TCP_Server *TCP_server, const uint8_t *public_key) 167static int get_TCP_connection_index(const TCP_Server *tcp_server, const uint8_t *public_key)
157{ 168{
158 return bs_list_find(&TCP_server->accepted_key_list, public_key); 169 return bs_list_find(&tcp_server->accepted_key_list, public_key);
159} 170}
160 171
161 172
162static int kill_accepted(TCP_Server *TCP_server, int index); 173static int kill_accepted(TCP_Server *tcp_server, int index);
163 174
164/* Add accepted TCP connection to the list. 175/* Add accepted TCP connection to the list.
165 * 176 *
166 * return index on success 177 * return index on success
167 * return -1 on failure 178 * return -1 on failure
168 */ 179 */
169static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con) 180static int add_accepted(TCP_Server *tcp_server, const TCP_Secure_Connection *con)
170{ 181{
171 int index = get_TCP_connection_index(TCP_server, con->public_key); 182 int index = get_TCP_connection_index(tcp_server, con->public_key);
172 183
173 if (index != -1) { /* If an old connection to the same public key exists, kill it. */ 184 if (index != -1) { /* If an old connection to the same public key exists, kill it. */
174 kill_accepted(TCP_server, index); 185 kill_accepted(tcp_server, index);
175 index = -1; 186 index = -1;
176 } 187 }
177 188
178 if (TCP_server->size_accepted_connections == TCP_server->num_accepted_connections) { 189 if (tcp_server->size_accepted_connections == tcp_server->num_accepted_connections) {
179 if (realloc_connection(TCP_server, TCP_server->size_accepted_connections + 4) == -1) { 190 if (realloc_connection(tcp_server, tcp_server->size_accepted_connections + 4) == -1) {
180 return -1; 191 return -1;
181 } 192 }
182 193
183 index = TCP_server->num_accepted_connections; 194 index = tcp_server->num_accepted_connections;
184 } else { 195 } else {
185 uint32_t i; 196 uint32_t i;
186 197
187 for (i = TCP_server->size_accepted_connections; i != 0; --i) { 198 for (i = tcp_server->size_accepted_connections; i != 0; --i) {
188 if (TCP_server->accepted_connection_array[i - 1].status == TCP_STATUS_NO_STATUS) { 199 if (tcp_server->accepted_connection_array[i - 1].status == TCP_STATUS_NO_STATUS) {
189 index = i - 1; 200 index = i - 1;
190 break; 201 break;
191 } 202 }
@@ -197,16 +208,16 @@ static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con
197 return -1; 208 return -1;
198 } 209 }
199 210
200 if (!bs_list_add(&TCP_server->accepted_key_list, con->public_key, index)) { 211 if (!bs_list_add(&tcp_server->accepted_key_list, con->public_key, index)) {
201 return -1; 212 return -1;
202 } 213 }
203 214
204 memcpy(&TCP_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection)); 215 memcpy(&tcp_server->accepted_connection_array[index], con, sizeof(TCP_Secure_Connection));
205 TCP_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED; 216 tcp_server->accepted_connection_array[index].status = TCP_STATUS_CONFIRMED;
206 ++TCP_server->num_accepted_connections; 217 ++tcp_server->num_accepted_connections;
207 TCP_server->accepted_connection_array[index].identifier = ++TCP_server->counter; 218 tcp_server->accepted_connection_array[index].identifier = ++tcp_server->counter;
208 TCP_server->accepted_connection_array[index].last_pinged = unix_time(); 219 tcp_server->accepted_connection_array[index].last_pinged = unix_time();
209 TCP_server->accepted_connection_array[index].ping_id = 0; 220 tcp_server->accepted_connection_array[index].ping_id = 0;
210 221
211 return index; 222 return index;
212} 223}
@@ -216,25 +227,25 @@ static int add_accepted(TCP_Server *TCP_server, const TCP_Secure_Connection *con
216 * return 0 on success 227 * return 0 on success
217 * return -1 on failure 228 * return -1 on failure
218 */ 229 */
219static int del_accepted(TCP_Server *TCP_server, int index) 230static int del_accepted(TCP_Server *tcp_server, int index)
220{ 231{
221 if ((uint32_t)index >= TCP_server->size_accepted_connections) { 232 if ((uint32_t)index >= tcp_server->size_accepted_connections) {
222 return -1; 233 return -1;
223 } 234 }
224 235
225 if (TCP_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) { 236 if (tcp_server->accepted_connection_array[index].status == TCP_STATUS_NO_STATUS) {
226 return -1; 237 return -1;
227 } 238 }
228 239
229 if (!bs_list_remove(&TCP_server->accepted_key_list, TCP_server->accepted_connection_array[index].public_key, index)) { 240 if (!bs_list_remove(&tcp_server->accepted_key_list, tcp_server->accepted_connection_array[index].public_key, index)) {
230 return -1; 241 return -1;
231 } 242 }
232 243
233 crypto_memzero(&TCP_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection)); 244 crypto_memzero(&tcp_server->accepted_connection_array[index], sizeof(TCP_Secure_Connection));
234 --TCP_server->num_accepted_connections; 245 --tcp_server->num_accepted_connections;
235 246
236 if (TCP_server->num_accepted_connections == 0) { 247 if (tcp_server->num_accepted_connections == 0) {
237 realloc_connection(TCP_server, 0); 248 realloc_connection(tcp_server, 0);
238 } 249 }
239 250
240 return 0; 251 return 0;
@@ -505,28 +516,28 @@ static void kill_TCP_secure_connection(TCP_Secure_Connection *con)
505 crypto_memzero(con, sizeof(TCP_Secure_Connection)); 516 crypto_memzero(con, sizeof(TCP_Secure_Connection));
506} 517}
507 518
508static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number); 519static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *con, uint8_t con_number);
509 520
510/* Kill an accepted TCP_Secure_Connection 521/* Kill an accepted TCP_Secure_Connection
511 * 522 *
512 * return -1 on failure. 523 * return -1 on failure.
513 * return 0 on success. 524 * return 0 on success.
514 */ 525 */
515static int kill_accepted(TCP_Server *TCP_server, int index) 526static int kill_accepted(TCP_Server *tcp_server, int index)
516{ 527{
517 if ((uint32_t)index >= TCP_server->size_accepted_connections) { 528 if ((uint32_t)index >= tcp_server->size_accepted_connections) {
518 return -1; 529 return -1;
519 } 530 }
520 531
521 uint32_t i; 532 uint32_t i;
522 533
523 for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { 534 for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
524 rm_connection_index(TCP_server, &TCP_server->accepted_connection_array[index], i); 535 rm_connection_index(tcp_server, &tcp_server->accepted_connection_array[index], i);
525 } 536 }
526 537
527 Socket sock = TCP_server->accepted_connection_array[index].sock; 538 Socket sock = tcp_server->accepted_connection_array[index].sock;
528 539
529 if (del_accepted(TCP_server, index) != 0) { 540 if (del_accepted(tcp_server, index) != 0) {
530 return -1; 541 return -1;
531 } 542 }
532 543
@@ -592,9 +603,9 @@ static int handle_TCP_handshake(TCP_Secure_Connection *con, const uint8_t *data,
592static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key) 603static int read_connection_handshake(TCP_Secure_Connection *con, const uint8_t *self_secret_key)
593{ 604{
594 uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE]; 605 uint8_t data[TCP_CLIENT_HANDSHAKE_SIZE];
595 int len = 0; 606 const int len = read_TCP_packet(con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE);
596 607
597 if ((len = read_TCP_packet(con->sock, data, TCP_CLIENT_HANDSHAKE_SIZE)) != -1) { 608 if (len != -1) {
598 return handle_TCP_handshake(con, data, len, self_secret_key); 609 return handle_TCP_handshake(con, data, len, self_secret_key);
599 } 610 }
600 611
@@ -638,11 +649,11 @@ static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id)
638/* return 0 on success. 649/* return 0 on success.
639 * return -1 on failure (connection must be killed). 650 * return -1 on failure (connection must be killed).
640 */ 651 */
641static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key) 652static int handle_TCP_routing_req(TCP_Server *tcp_server, uint32_t con_id, const uint8_t *public_key)
642{ 653{
643 uint32_t i; 654 uint32_t i;
644 uint32_t index = ~0; 655 uint32_t index = ~0;
645 TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; 656 TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];
646 657
647 /* If person tries to cennect to himself we deny the request*/ 658 /* If person tries to cennect to himself we deny the request*/
648 if (public_key_cmp(con->public_key, public_key) == 0) { 659 if (public_key_cmp(con->public_key, public_key) == 0) {
@@ -687,11 +698,11 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const
687 698
688 con->connections[index].status = 1; 699 con->connections[index].status = 1;
689 memcpy(con->connections[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); 700 memcpy(con->connections[index].public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE);
690 int other_index = get_TCP_connection_index(TCP_server, public_key); 701 int other_index = get_TCP_connection_index(tcp_server, public_key);
691 702
692 if (other_index != -1) { 703 if (other_index != -1) {
693 uint32_t other_id = ~0; 704 uint32_t other_id = ~0;
694 TCP_Secure_Connection *other_conn = &TCP_server->accepted_connection_array[other_index]; 705 TCP_Secure_Connection *other_conn = &tcp_server->accepted_connection_array[other_index];
695 706
696 for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) { 707 for (i = 0; i < NUM_CLIENT_CONNECTIONS; ++i) {
697 if (other_conn->connections[i].status == 1 708 if (other_conn->connections[i].status == 1
@@ -720,23 +731,23 @@ static int handle_TCP_routing_req(TCP_Server *TCP_server, uint32_t con_id, const
720/* return 0 on success. 731/* return 0 on success.
721 * return -1 on failure (connection must be killed). 732 * return -1 on failure (connection must be killed).
722 */ 733 */
723static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *public_key, const uint8_t *data, 734static int handle_TCP_oob_send(TCP_Server *tcp_server, uint32_t con_id, const uint8_t *public_key, const uint8_t *data,
724 uint16_t length) 735 uint16_t length)
725{ 736{
726 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) { 737 if (length == 0 || length > TCP_MAX_OOB_DATA_LENGTH) {
727 return -1; 738 return -1;
728 } 739 }
729 740
730 TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; 741 TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];
731 742
732 int other_index = get_TCP_connection_index(TCP_server, public_key); 743 int other_index = get_TCP_connection_index(tcp_server, public_key);
733 744
734 if (other_index != -1) { 745 if (other_index != -1) {
735 VLA(uint8_t, resp_packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length); 746 VLA(uint8_t, resp_packet, 1 + CRYPTO_PUBLIC_KEY_SIZE + length);
736 resp_packet[0] = TCP_PACKET_OOB_RECV; 747 resp_packet[0] = TCP_PACKET_OOB_RECV;
737 memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE); 748 memcpy(resp_packet + 1, con->public_key, CRYPTO_PUBLIC_KEY_SIZE);
738 memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length); 749 memcpy(resp_packet + 1 + CRYPTO_PUBLIC_KEY_SIZE, data, length);
739 write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[other_index], resp_packet, 750 write_packet_TCP_secure_connection(&tcp_server->accepted_connection_array[other_index], resp_packet,
740 SIZEOF_VLA(resp_packet), 0); 751 SIZEOF_VLA(resp_packet), 0);
741 } 752 }
742 753
@@ -748,7 +759,7 @@ static int handle_TCP_oob_send(TCP_Server *TCP_server, uint32_t con_id, const ui
748 * return -1 on failure. 759 * return -1 on failure.
749 * return 0 on success. 760 * return 0 on success.
750 */ 761 */
751static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) 762static int rm_connection_index(TCP_Server *tcp_server, TCP_Secure_Connection *con, uint8_t con_number)
752{ 763{
753 if (con_number >= NUM_CLIENT_CONNECTIONS) { 764 if (con_number >= NUM_CLIENT_CONNECTIONS) {
754 return -1; 765 return -1;
@@ -760,15 +771,15 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co
760 771
761 if (con->connections[con_number].status == 2) { 772 if (con->connections[con_number].status == 2) {
762 773
763 if (index >= TCP_server->size_accepted_connections) { 774 if (index >= tcp_server->size_accepted_connections) {
764 return -1; 775 return -1;
765 } 776 }
766 777
767 TCP_server->accepted_connection_array[index].connections[other_id].other_id = 0; 778 tcp_server->accepted_connection_array[index].connections[other_id].other_id = 0;
768 TCP_server->accepted_connection_array[index].connections[other_id].index = 0; 779 tcp_server->accepted_connection_array[index].connections[other_id].index = 0;
769 TCP_server->accepted_connection_array[index].connections[other_id].status = 1; 780 tcp_server->accepted_connection_array[index].connections[other_id].status = 1;
770 // TODO(irungentoo): return values? 781 // TODO(irungentoo): return values?
771 send_disconnect_notification(&TCP_server->accepted_connection_array[index], other_id); 782 send_disconnect_notification(&tcp_server->accepted_connection_array[index], other_id);
772 } 783 }
773 784
774 con->connections[con_number].index = 0; 785 con->connections[con_number].index = 0;
@@ -782,14 +793,14 @@ static int rm_connection_index(TCP_Server *TCP_server, TCP_Secure_Connection *co
782 793
783static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, uint16_t length) 794static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data, uint16_t length)
784{ 795{
785 TCP_Server *TCP_server = (TCP_Server *)object; 796 TCP_Server *tcp_server = (TCP_Server *)object;
786 uint32_t index = dest.ip.ip.v6.uint32[0]; 797 uint32_t index = dest.ip.ip.v6.uint32[0];
787 798
788 if (index >= TCP_server->size_accepted_connections) { 799 if (index >= tcp_server->size_accepted_connections) {
789 return 1; 800 return 1;
790 } 801 }
791 802
792 TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[index]; 803 TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[index];
793 804
794 if (con->identifier != dest.ip.ip.v6.uint64[1]) { 805 if (con->identifier != dest.ip.ip.v6.uint64[1]) {
795 return 1; 806 return 1;
@@ -809,13 +820,13 @@ static int handle_onion_recv_1(void *object, IP_Port dest, const uint8_t *data,
809/* return 0 on success 820/* return 0 on success
810 * return -1 on failure 821 * return -1 on failure
811 */ 822 */
812static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint8_t *data, uint16_t length) 823static int handle_TCP_packet(TCP_Server *tcp_server, uint32_t con_id, const uint8_t *data, uint16_t length)
813{ 824{
814 if (length == 0) { 825 if (length == 0) {
815 return -1; 826 return -1;
816 } 827 }
817 828
818 TCP_Secure_Connection *con = &TCP_server->accepted_connection_array[con_id]; 829 TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];
819 830
820 switch (data[0]) { 831 switch (data[0]) {
821 case TCP_PACKET_ROUTING_REQUEST: { 832 case TCP_PACKET_ROUTING_REQUEST: {
@@ -823,7 +834,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
823 return -1; 834 return -1;
824 } 835 }
825 836
826 return handle_TCP_routing_req(TCP_server, con_id, data + 1); 837 return handle_TCP_routing_req(tcp_server, con_id, data + 1);
827 } 838 }
828 839
829 case TCP_PACKET_CONNECTION_NOTIFICATION: { 840 case TCP_PACKET_CONNECTION_NOTIFICATION: {
@@ -839,7 +850,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
839 return -1; 850 return -1;
840 } 851 }
841 852
842 return rm_connection_index(TCP_server, con, data[1] - NUM_RESERVED_PORTS); 853 return rm_connection_index(tcp_server, con, data[1] - NUM_RESERVED_PORTS);
843 } 854 }
844 855
845 case TCP_PACKET_PING: { 856 case TCP_PACKET_PING: {
@@ -878,12 +889,12 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
878 return -1; 889 return -1;
879 } 890 }
880 891
881 return handle_TCP_oob_send(TCP_server, con_id, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE, 892 return handle_TCP_oob_send(tcp_server, con_id, data + 1, data + 1 + CRYPTO_PUBLIC_KEY_SIZE,
882 length - (1 + CRYPTO_PUBLIC_KEY_SIZE)); 893 length - (1 + CRYPTO_PUBLIC_KEY_SIZE));
883 } 894 }
884 895
885 case TCP_PACKET_ONION_REQUEST: { 896 case TCP_PACKET_ONION_REQUEST: {
886 if (TCP_server->onion) { 897 if (tcp_server->onion) {
887 if (length <= 1 + CRYPTO_NONCE_SIZE + ONION_SEND_BASE * 2) { 898 if (length <= 1 + CRYPTO_NONCE_SIZE + ONION_SEND_BASE * 2) {
888 return -1; 899 return -1;
889 } 900 }
@@ -894,7 +905,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
894 source.ip.ip.v6.uint32[0] = con_id; 905 source.ip.ip.v6.uint32[0] = con_id;
895 source.ip.ip.v6.uint32[1] = 0; 906 source.ip.ip.v6.uint32[1] = 0;
896 source.ip.ip.v6.uint64[1] = con->identifier; 907 source.ip.ip.v6.uint64[1] = con->identifier;
897 onion_send_1(TCP_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), source, 908 onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), source,
898 data + 1); 909 data + 1);
899 } 910 }
900 911
@@ -929,7 +940,7 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
929 VLA(uint8_t, new_data, length); 940 VLA(uint8_t, new_data, length);
930 memcpy(new_data, data, length); 941 memcpy(new_data, data, length);
931 new_data[0] = other_c_id; 942 new_data[0] = other_c_id;
932 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length, 0); 943 int ret = write_packet_TCP_secure_connection(&tcp_server->accepted_connection_array[index], new_data, length, 0);
933 944
934 if (ret == -1) { 945 if (ret == -1) {
935 return -1; 946 return -1;
@@ -943,10 +954,10 @@ static int handle_TCP_packet(TCP_Server *TCP_server, uint32_t con_id, const uint
943} 954}
944 955
945 956
946static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection *con, const uint8_t *data, 957static int confirm_TCP_connection(TCP_Server *tcp_server, TCP_Secure_Connection *con, const uint8_t *data,
947 uint16_t length) 958 uint16_t length)
948{ 959{
949 int index = add_accepted(TCP_server, con); 960 int index = add_accepted(tcp_server, con);
950 961
951 if (index == -1) { 962 if (index == -1) {
952 kill_TCP_secure_connection(con); 963 kill_TCP_secure_connection(con);
@@ -955,8 +966,8 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection
955 966
956 crypto_memzero(con, sizeof(TCP_Secure_Connection)); 967 crypto_memzero(con, sizeof(TCP_Secure_Connection));
957 968
958 if (handle_TCP_packet(TCP_server, index, data, length) == -1) { 969 if (handle_TCP_packet(tcp_server, index, data, length) == -1) {
959 kill_accepted(TCP_server, index); 970 kill_accepted(tcp_server, index);
960 return -1; 971 return -1;
961 } 972 }
962 973
@@ -966,7 +977,7 @@ static int confirm_TCP_connection(TCP_Server *TCP_server, TCP_Secure_Connection
966/* return index on success 977/* return index on success
967 * return -1 on failure 978 * return -1 on failure
968 */ 979 */
969static int accept_connection(TCP_Server *TCP_server, Socket sock) 980static int accept_connection(TCP_Server *tcp_server, Socket sock)
970{ 981{
971 if (!sock_valid(sock)) { 982 if (!sock_valid(sock)) {
972 return -1; 983 return -1;
@@ -982,9 +993,9 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock)
982 return -1; 993 return -1;
983 } 994 }
984 995
985 uint16_t index = TCP_server->incoming_connection_queue_index % MAX_INCOMING_CONNECTIONS; 996 uint16_t index = tcp_server->incoming_connection_queue_index % MAX_INCOMING_CONNECTIONS;
986 997
987 TCP_Secure_Connection *conn = &TCP_server->incoming_connection_queue[index]; 998 TCP_Secure_Connection *conn = &tcp_server->incoming_connection_queue[index];
988 999
989 if (conn->status != TCP_STATUS_NO_STATUS) { 1000 if (conn->status != TCP_STATUS_NO_STATUS) {
990 kill_TCP_secure_connection(conn); 1001 kill_TCP_secure_connection(conn);
@@ -994,7 +1005,7 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock)
994 conn->sock = sock; 1005 conn->sock = sock;
995 conn->next_packet_length = 0; 1006 conn->next_packet_length = 0;
996 1007
997 ++TCP_server->incoming_connection_queue_index; 1008 ++tcp_server->incoming_connection_queue_index;
998 return index; 1009 return index;
999} 1010}
1000 1011
@@ -1107,34 +1118,34 @@ TCP_Server *new_TCP_server(uint8_t ipv6_enabled, uint16_t num_sockets, const uin
1107} 1118}
1108 1119
1109#ifndef TCP_SERVER_USE_EPOLL 1120#ifndef TCP_SERVER_USE_EPOLL
1110static void do_TCP_accept_new(TCP_Server *TCP_server) 1121static void do_TCP_accept_new(TCP_Server *tcp_server)
1111{ 1122{
1112 uint32_t i; 1123 uint32_t i;
1113 1124
1114 for (i = 0; i < TCP_server->num_listening_socks; ++i) { 1125 for (i = 0; i < tcp_server->num_listening_socks; ++i) {
1115 Socket sock; 1126 Socket sock;
1116 1127
1117 do { 1128 do {
1118 sock = net_accept(TCP_server->socks_listening[i]); 1129 sock = net_accept(tcp_server->socks_listening[i]);
1119 } while (accept_connection(TCP_server, sock) != -1); 1130 } while (accept_connection(tcp_server, sock) != -1);
1120 } 1131 }
1121} 1132}
1122#endif 1133#endif
1123 1134
1124static int do_incoming(TCP_Server *TCP_server, uint32_t i) 1135static int do_incoming(TCP_Server *tcp_server, uint32_t i)
1125{ 1136{
1126 if (TCP_server->incoming_connection_queue[i].status != TCP_STATUS_CONNECTED) { 1137 if (tcp_server->incoming_connection_queue[i].status != TCP_STATUS_CONNECTED) {
1127 return -1; 1138 return -1;
1128 } 1139 }
1129 1140
1130 int ret = read_connection_handshake(&TCP_server->incoming_connection_queue[i], TCP_server->secret_key); 1141 int ret = read_connection_handshake(&tcp_server->incoming_connection_queue[i], tcp_server->secret_key);
1131 1142
1132 if (ret == -1) { 1143 if (ret == -1) {
1133 kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[i]); 1144 kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[i]);
1134 } else if (ret == 1) { 1145 } else if (ret == 1) {
1135 int index_new = TCP_server->unconfirmed_connection_queue_index % MAX_INCOMING_CONNECTIONS; 1146 int index_new = tcp_server->unconfirmed_connection_queue_index % MAX_INCOMING_CONNECTIONS;
1136 TCP_Secure_Connection *conn_old = &TCP_server->incoming_connection_queue[i]; 1147 TCP_Secure_Connection *conn_old = &tcp_server->incoming_connection_queue[i];
1137 TCP_Secure_Connection *conn_new = &TCP_server->unconfirmed_connection_queue[index_new]; 1148 TCP_Secure_Connection *conn_new = &tcp_server->unconfirmed_connection_queue[index_new];
1138 1149
1139 if (conn_new->status != TCP_STATUS_NO_STATUS) { 1150 if (conn_new->status != TCP_STATUS_NO_STATUS) {
1140 kill_TCP_secure_connection(conn_new); 1151 kill_TCP_secure_connection(conn_new);
@@ -1142,7 +1153,7 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i)
1142 1153
1143 memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection)); 1154 memcpy(conn_new, conn_old, sizeof(TCP_Secure_Connection));
1144 crypto_memzero(conn_old, sizeof(TCP_Secure_Connection)); 1155 crypto_memzero(conn_old, sizeof(TCP_Secure_Connection));
1145 ++TCP_server->unconfirmed_connection_queue_index; 1156 ++tcp_server->unconfirmed_connection_queue_index;
1146 1157
1147 return index_new; 1158 return index_new;
1148 } 1159 }
@@ -1150,9 +1161,9 @@ static int do_incoming(TCP_Server *TCP_server, uint32_t i)
1150 return -1; 1161 return -1;
1151} 1162}
1152 1163
1153static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i) 1164static int do_unconfirmed(TCP_Server *tcp_server, uint32_t i)
1154{ 1165{
1155 TCP_Secure_Connection *conn = &TCP_server->unconfirmed_connection_queue[i]; 1166 TCP_Secure_Connection *conn = &tcp_server->unconfirmed_connection_queue[i];
1156 1167
1157 if (conn->status != TCP_STATUS_UNCONFIRMED) { 1168 if (conn->status != TCP_STATUS_UNCONFIRMED) {
1158 return -1; 1169 return -1;
@@ -1171,64 +1182,76 @@ static int do_unconfirmed(TCP_Server *TCP_server, uint32_t i)
1171 return -1; 1182 return -1;
1172 } 1183 }
1173 1184
1174 return confirm_TCP_connection(TCP_server, conn, packet, len); 1185 return confirm_TCP_connection(tcp_server, conn, packet, len);
1175} 1186}
1176 1187
1177static void do_confirmed_recv(TCP_Server *TCP_server, uint32_t i) 1188static bool tcp_process_secure_packet(TCP_Server *tcp_server, uint32_t i)
1178{ 1189{
1179 TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i]; 1190 TCP_Secure_Connection *const conn = &tcp_server->accepted_connection_array[i];
1180 1191
1181 uint8_t packet[MAX_PACKET_SIZE]; 1192 uint8_t packet[MAX_PACKET_SIZE];
1182 int len; 1193 int len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key,
1194 conn->recv_nonce, packet, sizeof(packet));
1183 1195
1184 while ((len = read_packet_TCP_secure_connection(conn->sock, &conn->next_packet_length, conn->shared_key, 1196 if (len == 0) {
1185 conn->recv_nonce, packet, sizeof(packet)))) { 1197 return false;
1186 if (len == -1) { 1198 }
1187 kill_accepted(TCP_server, i);
1188 break;
1189 }
1190 1199
1191 if (handle_TCP_packet(TCP_server, i, packet, len) == -1) { 1200 if (len == -1) {
1192 kill_accepted(TCP_server, i); 1201 kill_accepted(tcp_server, i);
1193 break; 1202 return false;
1194 } 1203 }
1204
1205 if (handle_TCP_packet(tcp_server, i, packet, len) == -1) {
1206 kill_accepted(tcp_server, i);
1207 return false;
1208 }
1209
1210 return true;
1211}
1212
1213static void do_confirmed_recv(TCP_Server *tcp_server, uint32_t i)
1214{
1215 while (tcp_process_secure_packet(tcp_server, i)) {
1216 // Keep reading until an error occurs or there is no more data to read.
1217 continue;
1195 } 1218 }
1196} 1219}
1197 1220
1198#ifndef TCP_SERVER_USE_EPOLL 1221#ifndef TCP_SERVER_USE_EPOLL
1199static void do_TCP_incoming(TCP_Server *TCP_server) 1222static void do_TCP_incoming(TCP_Server *tcp_server)
1200{ 1223{
1201 uint32_t i; 1224 uint32_t i;
1202 1225
1203 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) { 1226 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1204 do_incoming(TCP_server, i); 1227 do_incoming(tcp_server, i);
1205 } 1228 }
1206} 1229}
1207 1230
1208static void do_TCP_unconfirmed(TCP_Server *TCP_server) 1231static void do_TCP_unconfirmed(TCP_Server *tcp_server)
1209{ 1232{
1210 uint32_t i; 1233 uint32_t i;
1211 1234
1212 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) { 1235 for (i = 0; i < MAX_INCOMING_CONNECTIONS; ++i) {
1213 do_unconfirmed(TCP_server, i); 1236 do_unconfirmed(tcp_server, i);
1214 } 1237 }
1215} 1238}
1216#endif 1239#endif
1217 1240
1218static void do_TCP_confirmed(TCP_Server *TCP_server) 1241static void do_TCP_confirmed(TCP_Server *tcp_server)
1219{ 1242{
1220#ifdef TCP_SERVER_USE_EPOLL 1243#ifdef TCP_SERVER_USE_EPOLL
1221 1244
1222 if (TCP_server->last_run_pinged == unix_time()) { 1245 if (tcp_server->last_run_pinged == unix_time()) {
1223 return; 1246 return;
1224 } 1247 }
1225 1248
1226 TCP_server->last_run_pinged = unix_time(); 1249 tcp_server->last_run_pinged = unix_time();
1227#endif 1250#endif
1228 uint32_t i; 1251 uint32_t i;
1229 1252
1230 for (i = 0; i < TCP_server->size_accepted_connections; ++i) { 1253 for (i = 0; i < tcp_server->size_accepted_connections; ++i) {
1231 TCP_Secure_Connection *conn = &TCP_server->accepted_connection_array[i]; 1254 TCP_Secure_Connection *conn = &tcp_server->accepted_connection_array[i];
1232 1255
1233 if (conn->status != TCP_STATUS_CONFIRMED) { 1256 if (conn->status != TCP_STATUS_CONFIRMED) {
1234 continue; 1257 continue;
@@ -1251,14 +1274,14 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
1251 conn->ping_id = ping_id; 1274 conn->ping_id = ping_id;
1252 } else { 1275 } else {
1253 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) { 1276 if (is_timeout(conn->last_pinged, TCP_PING_FREQUENCY + TCP_PING_TIMEOUT)) {
1254 kill_accepted(TCP_server, i); 1277 kill_accepted(tcp_server, i);
1255 continue; 1278 continue;
1256 } 1279 }
1257 } 1280 }
1258 } 1281 }
1259 1282
1260 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) { 1283 if (conn->ping_id && is_timeout(conn->last_pinged, TCP_PING_TIMEOUT)) {
1261 kill_accepted(TCP_server, i); 1284 kill_accepted(tcp_server, i);
1262 continue; 1285 continue;
1263 } 1286 }
1264 1287
@@ -1266,168 +1289,174 @@ static void do_TCP_confirmed(TCP_Server *TCP_server)
1266 1289
1267#ifndef TCP_SERVER_USE_EPOLL 1290#ifndef TCP_SERVER_USE_EPOLL
1268 1291
1269 do_confirmed_recv(TCP_server, i); 1292 do_confirmed_recv(tcp_server, i);
1270 1293
1271#endif 1294#endif
1272 } 1295 }
1273} 1296}
1274 1297
1275#ifdef TCP_SERVER_USE_EPOLL 1298#ifdef TCP_SERVER_USE_EPOLL
1276static void do_TCP_epoll(TCP_Server *TCP_server) 1299static bool tcp_epoll_process(TCP_Server *tcp_server)
1277{ 1300{
1278#define MAX_EVENTS 16 1301#define MAX_EVENTS 16
1279 struct epoll_event events[MAX_EVENTS]; 1302 struct epoll_event events[MAX_EVENTS];
1280 int nfds; 1303 const int nfds = epoll_wait(tcp_server->efd, events, MAX_EVENTS, 0);
1281 1304#undef MAX_EVENTS
1282 while ((nfds = epoll_wait(TCP_server->efd, events, MAX_EVENTS, 0)) > 0) {
1283 int n;
1284 1305
1285 for (n = 0; n < nfds; ++n) { 1306 for (int n = 0; n < nfds; ++n) {
1286 const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)}; 1307 const Socket sock = {(int)(events[n].data.u64 & 0xFFFFFFFF)};
1287 const int status = (events[n].data.u64 >> 32) & 0xFF; 1308 const int status = (events[n].data.u64 >> 32) & 0xFF;
1288 const int index = events[n].data.u64 >> 40; 1309 const int index = events[n].data.u64 >> 40;
1289 1310
1290 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) { 1311 if ((events[n].events & EPOLLERR) || (events[n].events & EPOLLHUP) || (events[n].events & EPOLLRDHUP)) {
1291 switch (status) { 1312 switch (status) {
1292 case TCP_SOCKET_LISTENING: { 1313 case TCP_SOCKET_LISTENING: {
1293 //should never happen 1314 // should never happen
1294 break; 1315 break;
1295 } 1316 }
1296
1297 case TCP_SOCKET_INCOMING: {
1298 kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[index]);
1299 break;
1300 }
1301 1317
1302 case TCP_SOCKET_UNCONFIRMED: { 1318 case TCP_SOCKET_INCOMING: {
1303 kill_TCP_secure_connection(&TCP_server->unconfirmed_connection_queue[index]); 1319 kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[index]);
1304 break; 1320 break;
1305 } 1321 }
1306 1322
1307 case TCP_SOCKET_CONFIRMED: { 1323 case TCP_SOCKET_UNCONFIRMED: {
1308 kill_accepted(TCP_server, index); 1324 kill_TCP_secure_connection(&tcp_server->unconfirmed_connection_queue[index]);
1309 break; 1325 break;
1310 }
1311 } 1326 }
1312 1327
1313 continue; 1328 case TCP_SOCKET_CONFIRMED: {
1329 kill_accepted(tcp_server, index);
1330 break;
1331 }
1314 } 1332 }
1315 1333
1334 continue;
1335 }
1316 1336
1317 if (!(events[n].events & EPOLLIN)) {
1318 continue;
1319 }
1320 1337
1321 switch (status) { 1338 if (!(events[n].events & EPOLLIN)) {
1322 case TCP_SOCKET_LISTENING: { 1339 continue;
1323 //socket is from socks_listening, accept connection 1340 }
1324 while (1) {
1325 Socket sock_new = net_accept(sock);
1326
1327 if (!sock_valid(sock_new)) {
1328 break;
1329 }
1330 1341
1331 int index_new = accept_connection(TCP_server, sock_new); 1342 switch (status) {
1343 case TCP_SOCKET_LISTENING: {
1344 // socket is from socks_listening, accept connection
1345 while (1) {
1346 Socket sock_new = net_accept(sock);
1332 1347
1333 if (index_new == -1) { 1348 if (!sock_valid(sock_new)) {
1334 continue; 1349 break;
1335 } 1350 }
1336 1351
1337 struct epoll_event ev = { 1352 int index_new = accept_connection(tcp_server, sock_new);
1338 .events = EPOLLIN | EPOLLET | EPOLLRDHUP,
1339 .data.u64 = sock_new.socket | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40)
1340 };
1341 1353
1342 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_ADD, sock_new.socket, &ev) == -1) { 1354 if (index_new == -1) {
1343 kill_TCP_secure_connection(&TCP_server->incoming_connection_queue[index_new]); 1355 continue;
1344 continue;
1345 }
1346 } 1356 }
1347 1357
1348 break; 1358 struct epoll_event ev;
1349 }
1350 1359
1351 case TCP_SOCKET_INCOMING: { 1360 ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1352 int index_new;
1353 1361
1354 if ((index_new = do_incoming(TCP_server, index)) != -1) { 1362 ev.data.u64 = sock_new.socket | ((uint64_t)TCP_SOCKET_INCOMING << 32) | ((uint64_t)index_new << 40);
1355 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1356 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
1357 1363
1358 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { 1364 if (epoll_ctl(tcp_server->efd, EPOLL_CTL_ADD, sock_new.socket, &ev) == -1) {
1359 kill_TCP_secure_connection(&TCP_server->unconfirmed_connection_queue[index_new]); 1365 kill_TCP_secure_connection(&tcp_server->incoming_connection_queue[index_new]);
1360 break; 1366 continue;
1361 }
1362 } 1367 }
1363
1364 break;
1365 } 1368 }
1366 1369
1367 case TCP_SOCKET_UNCONFIRMED: { 1370 break;
1368 int index_new; 1371 }
1369 1372
1370 if ((index_new = do_unconfirmed(TCP_server, index)) != -1) { 1373 case TCP_SOCKET_INCOMING: {
1371 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP; 1374 const int index_new = do_incoming(tcp_server, index);
1372 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
1373 1375
1374 if (epoll_ctl(TCP_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) { 1376 if (index_new != -1) {
1375 //remove from confirmed connections 1377 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1376 kill_accepted(TCP_server, index_new); 1378 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_UNCONFIRMED << 32) | ((uint64_t)index_new << 40);
1377 break;
1378 }
1379 }
1380 1379
1381 break; 1380 if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) {
1381 kill_TCP_secure_connection(&tcp_server->unconfirmed_connection_queue[index_new]);
1382 break;
1383 }
1382 } 1384 }
1383 1385
1384 case TCP_SOCKET_CONFIRMED: { 1386 break;
1385 do_confirmed_recv(TCP_server, index); 1387 }
1386 break; 1388
1389 case TCP_SOCKET_UNCONFIRMED: {
1390 const int index_new = do_unconfirmed(tcp_server, index);
1391
1392 if (index_new != -1) {
1393 events[n].events = EPOLLIN | EPOLLET | EPOLLRDHUP;
1394 events[n].data.u64 = sock.socket | ((uint64_t)TCP_SOCKET_CONFIRMED << 32) | ((uint64_t)index_new << 40);
1395
1396 if (epoll_ctl(tcp_server->efd, EPOLL_CTL_MOD, sock.socket, &events[n]) == -1) {
1397 // remove from confirmed connections
1398 kill_accepted(tcp_server, index_new);
1399 break;
1400 }
1387 } 1401 }
1402
1403 break;
1404 }
1405
1406 case TCP_SOCKET_CONFIRMED: {
1407 do_confirmed_recv(tcp_server, index);
1408 break;
1388 } 1409 }
1389 } 1410 }
1390 } 1411 }
1391 1412
1392#undef MAX_EVENTS 1413 return nfds > 0;
1414}
1415
1416static void do_TCP_epoll(TCP_Server *tcp_server)
1417{
1418 while (tcp_epoll_process(tcp_server)) {
1419 // Keep processing packets until there are no more FDs ready for reading.
1420 continue;
1421 }
1393} 1422}
1394#endif 1423#endif
1395 1424
1396void do_TCP_server(TCP_Server *TCP_server) 1425void do_TCP_server(TCP_Server *tcp_server)
1397{ 1426{
1398 unix_time_update(); 1427 unix_time_update();
1399 1428
1400#ifdef TCP_SERVER_USE_EPOLL 1429#ifdef TCP_SERVER_USE_EPOLL
1401 do_TCP_epoll(TCP_server); 1430 do_TCP_epoll(tcp_server);
1402 1431
1403#else 1432#else
1404 do_TCP_accept_new(TCP_server); 1433 do_TCP_accept_new(tcp_server);
1405 do_TCP_incoming(TCP_server); 1434 do_TCP_incoming(tcp_server);
1406 do_TCP_unconfirmed(TCP_server); 1435 do_TCP_unconfirmed(tcp_server);
1407#endif 1436#endif
1408 1437
1409 do_TCP_confirmed(TCP_server); 1438 do_TCP_confirmed(tcp_server);
1410} 1439}
1411 1440
1412void kill_TCP_server(TCP_Server *TCP_server) 1441void kill_TCP_server(TCP_Server *tcp_server)
1413{ 1442{
1414 uint32_t i; 1443 uint32_t i;
1415 1444
1416 for (i = 0; i < TCP_server->num_listening_socks; ++i) { 1445 for (i = 0; i < tcp_server->num_listening_socks; ++i) {
1417 kill_sock(TCP_server->socks_listening[i]); 1446 kill_sock(tcp_server->socks_listening[i]);
1418 } 1447 }
1419 1448
1420 if (TCP_server->onion) { 1449 if (tcp_server->onion) {
1421 set_callback_handle_recv_1(TCP_server->onion, nullptr, nullptr); 1450 set_callback_handle_recv_1(tcp_server->onion, nullptr, nullptr);
1422 } 1451 }
1423 1452
1424 bs_list_free(&TCP_server->accepted_key_list); 1453 bs_list_free(&tcp_server->accepted_key_list);
1425 1454
1426#ifdef TCP_SERVER_USE_EPOLL 1455#ifdef TCP_SERVER_USE_EPOLL
1427 close(TCP_server->efd); 1456 close(tcp_server->efd);
1428#endif 1457#endif
1429 1458
1430 free(TCP_server->socks_listening); 1459 free(tcp_server->socks_listening);
1431 free(TCP_server->accepted_connection_array); 1460 free(tcp_server->accepted_connection_array);
1432 free(TCP_server); 1461 free(tcp_server);
1433} 1462}