summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/TCP_server.c116
-rw-r--r--toxcore/TCP_server.h2
2 files changed, 108 insertions, 10 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index 6427b348..c5488862 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -170,6 +170,11 @@ static int add_accepted(TCP_Server *TCP_server, TCP_Secure_Connection *con)
170 return index; 170 return index;
171} 171}
172 172
173/* Delete accepted connection from list.
174 *
175 * return 0 on success
176 * return -1 on failure
177 */
173static int del_accepted(TCP_Server *TCP_server, int index) 178static int del_accepted(TCP_Server *TCP_server, int index)
174{ 179{
175 if ((uint32_t)index >= TCP_server->size_accepted_connections) 180 if ((uint32_t)index >= TCP_server->size_accepted_connections)
@@ -187,6 +192,22 @@ static int del_accepted(TCP_Server *TCP_server, int index)
187 return 0; 192 return 0;
188} 193}
189 194
195/* return index corresponding to connection with peer on success
196 * return -1 on failure.
197 */
198static int get_TCP_connection_index(TCP_Server *TCP_server, uint8_t *public_key)
199{
200 //TODO optimize this function.
201 uint32_t i;
202
203 for (i = 0; i < TCP_server->size_accepted_connections; ++i) {
204 if (memcmp(TCP_server->accepted_connection_array[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
205 return i;
206 }
207
208 return -1;
209}
210
190/* return length on success 211/* return length on success
191 * return 0 if nothing has been read from socket. 212 * return 0 if nothing has been read from socket.
192 * return ~0 on failure. 213 * return ~0 on failure.
@@ -386,24 +407,71 @@ static int read_connection_handshake(TCP_Secure_Connection *con, uint8_t *self_s
386 return 0; 407 return 0;
387} 408}
388 409
410/* return 1 on success.
411 * return 0 if could not send packet.
412 * return -1 on failure (connection must be killed).
413 */
414static int send_routing_response(TCP_Secure_Connection *con, uint8_t rpid, uint8_t *public_key)
415{
416 uint8_t data[1 + 1 + crypto_box_PUBLICKEYBYTES];
417 data[0] = TCP_PACKET_ROUTING_RESPONSE;
418 data[1] = rpid;
419 memcpy(data + 2, public_key, crypto_box_PUBLICKEYBYTES);
420
421 return write_packet_TCP_secure_connection(con, data, sizeof(data));
422}
423
424/* return 1 on success.
425 * return 0 if could not send packet.
426 * return -1 on failure (connection must be killed).
427 */
428static int send_connect_notification(TCP_Secure_Connection *con, uint8_t id)
429{
430 uint8_t data[2] = {TCP_PACKET_CONNECTION_NOTIFICATION, id};
431 return write_packet_TCP_secure_connection(con, data, sizeof(data));
432}
433
434/* return 1 on success.
435 * return 0 if could not send packet.
436 * return -1 on failure (connection must be killed).
437 */
438static int send_disconnect_notification(TCP_Secure_Connection *con, uint8_t id)
439{
440 uint8_t data[2] = {TCP_PACKET_DISCONNECT_NOTIFICATION, id};
441 return write_packet_TCP_secure_connection(con, data, sizeof(data));
442}
443
444/* return 0 on success.
445 * return -1 on failure (connection must be killed).
446 */
447static int handle_TCP_routing_req(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t *public_key)
448{
449 //TODO
450 return 0;
451}
452
389static int disconnect_conection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number) 453static int disconnect_conection_index(TCP_Server *TCP_server, TCP_Secure_Connection *con, uint8_t con_number)
390{ 454{
391 if (con_number >= NUM_CLIENT_CONNECTIONS) 455 if (con_number >= NUM_CLIENT_CONNECTIONS)
392 return -1; 456 return -1;
393 457
394 uint32_t index = con->connections[con_number].index; 458 if (con->connections[con_number].status) {
395 uint8_t other_id = con->connections[con_number].other_id; 459 uint32_t index = con->connections[con_number].index;
460 uint8_t other_id = con->connections[con_number].other_id;
396 461
397 if (index) { 462 if (con->connections[con_number].status == 2) {
398 --index;
399 463
400 if (index >= TCP_server->size_accepted_connections) 464 if (index >= TCP_server->size_accepted_connections)
401 return -1; 465 return -1;
466
467 TCP_server->accepted_connection_array[index].connections[other_id].other_id = 0;
468 TCP_server->accepted_connection_array[index].connections[other_id].index = 0;
469 TCP_server->accepted_connection_array[index].connections[other_id].status = 1;
470 }
402 471
403 TCP_server->accepted_connection_array[index].connections[other_id].other_id = 0;
404 TCP_server->accepted_connection_array[index].connections[other_id].index = 0;
405 con->connections[con_number].index = 0; 472 con->connections[con_number].index = 0;
406 con->connections[con_number].other_id = 0; 473 con->connections[con_number].other_id = 0;
474 con->connections[con_number].status = 0;
407 return 0; 475 return 0;
408 } else { 476 } else {
409 return -1; 477 return -1;
@@ -420,11 +488,15 @@ static int handle_TCP_packet(TCP_Server *TCP_server, TCP_Secure_Connection *con,
420 488
421 switch (data[0]) { 489 switch (data[0]) {
422 case TCP_PACKET_ROUTING_REQUEST: { 490 case TCP_PACKET_ROUTING_REQUEST: {
491 if (length != 1 + crypto_box_PUBLICKEYBYTES)
492 return -1;
423 493
424 break; 494 return handle_TCP_routing_req(TCP_server, con, data + 1);
425 } 495 }
426 496
427 case TCP_PACKET_CONNECTION_NOTIFICATION: { 497 case TCP_PACKET_CONNECTION_NOTIFICATION: {
498 if (length != 2)
499 return -1;
428 500
429 break; 501 break;
430 } 502 }
@@ -447,7 +519,31 @@ static int handle_TCP_packet(TCP_Server *TCP_server, TCP_Secure_Connection *con,
447 } 519 }
448 520
449 default: { 521 default: {
450 break; 522 if (data[0] < NUM_RESERVED_PORTS)
523 return -1;
524
525 uint8_t con_id = data[0] - NUM_RESERVED_PORTS;
526
527 if (con_id >= NUM_CLIENT_CONNECTIONS)
528 return -1;
529
530 if (con->connections[con_id].status == 0)
531 return -1;
532
533 if (con->connections[con_id].status != 2)
534 return 0;
535
536 uint32_t index = con->connections[con_id].index;
537 uint8_t other_con_id = con->connections[con_id].other_id;
538 uint8_t new_data[length];
539 memcpy(new_data, data, length);
540 new_data[0] = other_con_id;
541 int ret = write_packet_TCP_secure_connection(&TCP_server->accepted_connection_array[index], new_data, length);
542
543 if (ret == -1)
544 return -1;
545
546 return 0;
451 } 547 }
452 } 548 }
453 549
diff --git a/toxcore/TCP_server.h b/toxcore/TCP_server.h
index 3d2dfff7..22a992de 100644
--- a/toxcore/TCP_server.h
+++ b/toxcore/TCP_server.h
@@ -60,6 +60,8 @@ typedef struct TCP_Secure_Connection {
60 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 60 uint8_t shared_key[crypto_box_BEFORENMBYTES];
61 uint16_t next_packet_length; 61 uint16_t next_packet_length;
62 struct { 62 struct {
63 uint8_t status; /* 0 if not used, 1 if other is offline, 2 if other is online. */
64 uint8_t public_key[crypto_box_PUBLICKEYBYTES];
63 uint32_t index; 65 uint32_t index;
64 uint8_t other_id; 66 uint8_t other_id;
65 } connections[NUM_CLIENT_CONNECTIONS]; 67 } connections[NUM_CLIENT_CONNECTIONS];