diff options
Diffstat (limited to 'toxcore/TCP_connection.c')
-rw-r--r-- | toxcore/TCP_connection.c | 216 |
1 files changed, 206 insertions, 10 deletions
diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index f2ef55ca..ddcbd1d8 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c | |||
@@ -294,11 +294,12 @@ int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int | |||
294 | 294 | ||
295 | int connections_number = create_connection(tcp_c); | 295 | int connections_number = create_connection(tcp_c); |
296 | 296 | ||
297 | TCP_Connection_to *con_to = get_connection(tcp_c, connections_number); | 297 | if (connections_number == -1) |
298 | |||
299 | if (!con_to) | ||
300 | return -1; | 298 | return -1; |
301 | 299 | ||
300 | TCP_Connection_to *con_to = &tcp_c->connections[connections_number]; | ||
301 | |||
302 | con_to->status = TCP_CONN_VALID; | ||
302 | memcpy(con_to->public_key, public_key, crypto_box_PUBLICKEYBYTES); | 303 | memcpy(con_to->public_key, public_key, crypto_box_PUBLICKEYBYTES); |
303 | con_to->id = id; | 304 | con_to->id = id; |
304 | 305 | ||
@@ -319,6 +320,19 @@ int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number) | |||
319 | return wipe_connection(tcp_c, connections_number); | 320 | return wipe_connection(tcp_c, connections_number); |
320 | } | 321 | } |
321 | 322 | ||
323 | static _Bool tcp_connection_in_conn(TCP_Connection_to *con_to, int tcp_connections_number) | ||
324 | { | ||
325 | unsigned int i; | ||
326 | |||
327 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { | ||
328 | if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) { | ||
329 | return 1; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | return 0; | ||
334 | } | ||
335 | |||
322 | /* return index on success. | 336 | /* return index on success. |
323 | * return -1 on failure. | 337 | * return -1 on failure. |
324 | */ | 338 | */ |
@@ -326,6 +340,9 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, int tcp_connect | |||
326 | { | 340 | { |
327 | unsigned int i; | 341 | unsigned int i; |
328 | 342 | ||
343 | if (tcp_connection_in_conn(con_to, tcp_connections_number)) | ||
344 | return -1; | ||
345 | |||
329 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { | 346 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { |
330 | if (con_to->connections[i].tcp_connection == 0) { | 347 | if (con_to->connections[i].tcp_connection == 0) { |
331 | con_to->connections[i].tcp_connection = tcp_connections_number + 1; | 348 | con_to->connections[i].tcp_connection = tcp_connections_number + 1; |
@@ -341,6 +358,25 @@ static int add_tcp_connection_to_conn(TCP_Connection_to *con_to, int tcp_connect | |||
341 | /* return index on success. | 358 | /* return index on success. |
342 | * return -1 on failure. | 359 | * return -1 on failure. |
343 | */ | 360 | */ |
361 | static int rm_tcp_connection_from_conn(TCP_Connection_to *con_to, int tcp_connections_number) | ||
362 | { | ||
363 | unsigned int i; | ||
364 | |||
365 | for (i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) { | ||
366 | if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) { | ||
367 | con_to->connections[i].tcp_connection = 0; | ||
368 | con_to->connections[i].status = TCP_CONNECTIONS_STATUS_NONE; | ||
369 | con_to->connections[i].connection_id = 0; | ||
370 | return i; | ||
371 | } | ||
372 | } | ||
373 | |||
374 | return -1; | ||
375 | } | ||
376 | |||
377 | /* return index on success. | ||
378 | * return -1 on failure. | ||
379 | */ | ||
344 | static int set_tcp_connection_status(TCP_Connection_to *con_to, int tcp_connections_number, unsigned int status, | 380 | static int set_tcp_connection_status(TCP_Connection_to *con_to, int tcp_connections_number, unsigned int status, |
345 | uint8_t connection_id) | 381 | uint8_t connection_id) |
346 | { | 382 | { |
@@ -357,6 +393,51 @@ static int set_tcp_connection_status(TCP_Connection_to *con_to, int tcp_connecti | |||
357 | return -1; | 393 | return -1; |
358 | } | 394 | } |
359 | 395 | ||
396 | /* Kill a TCP relay connection. | ||
397 | * | ||
398 | * return 0 on success. | ||
399 | * return -1 on failure. | ||
400 | */ | ||
401 | static int kill_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connections_number) | ||
402 | { | ||
403 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
404 | |||
405 | if (!tcp_con) | ||
406 | return -1; | ||
407 | |||
408 | unsigned int i; | ||
409 | |||
410 | for (i = 0; i < tcp_c->connections_length; ++i) { | ||
411 | TCP_Connection_to *con_to = get_connection(tcp_c, i); | ||
412 | |||
413 | if (con_to) { | ||
414 | rm_tcp_connection_from_conn(con_to, tcp_connections_number); | ||
415 | } | ||
416 | } | ||
417 | |||
418 | kill_TCP_connection(tcp_con->connection); | ||
419 | |||
420 | return wipe_tcp_connection(tcp_c, tcp_connections_number); | ||
421 | } | ||
422 | |||
423 | /* Send a TCP routing request. | ||
424 | * | ||
425 | * return 0 on success. | ||
426 | * return -1 on failure. | ||
427 | */ | ||
428 | static int send_tcp_relay_routing_request(TCP_Connections *tcp_c, int tcp_connections_number, uint8_t *public_key) | ||
429 | { | ||
430 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
431 | |||
432 | if (!tcp_con) | ||
433 | return -1; | ||
434 | |||
435 | if (send_routing_request(tcp_con->connection, public_key) != 1) | ||
436 | return -1; | ||
437 | |||
438 | return 0; | ||
439 | } | ||
440 | |||
360 | static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) | 441 | static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key) |
361 | { | 442 | { |
362 | TCP_Client_Connection *TCP_client_con = object; | 443 | TCP_Client_Connection *TCP_client_con = object; |
@@ -470,6 +551,76 @@ static int tcp_onion_callback(void *object, const uint8_t *data, uint16_t length | |||
470 | return 0; | 551 | return 0; |
471 | } | 552 | } |
472 | 553 | ||
554 | /* Set callbacks for the TCP relay connection. | ||
555 | * | ||
556 | * return 0 on success. | ||
557 | * return -1 on failure. | ||
558 | */ | ||
559 | static int tcp_relay_set_callbacks(TCP_Connections *tcp_c, int tcp_connections_number) | ||
560 | { | ||
561 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
562 | |||
563 | if (!tcp_con) | ||
564 | return -1; | ||
565 | |||
566 | TCP_Client_Connection *con = tcp_con->connection; | ||
567 | |||
568 | con->custom_object = tcp_c; | ||
569 | con->custom_uint = tcp_connections_number; | ||
570 | onion_response_handler(con, &tcp_onion_callback, tcp_c); | ||
571 | routing_response_handler(con, &tcp_response_callback, con); | ||
572 | routing_status_handler(con, &tcp_status_callback, con); | ||
573 | routing_data_handler(con, &tcp_data_callback, con); | ||
574 | oob_data_handler(con, &tcp_oob_callback, con); | ||
575 | |||
576 | return 0; | ||
577 | } | ||
578 | |||
579 | static int tcp_relay_on_online(TCP_Connections *tcp_c, int tcp_connections_number) | ||
580 | { | ||
581 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
582 | |||
583 | if (!tcp_con) | ||
584 | return -1; | ||
585 | |||
586 | unsigned int i; | ||
587 | |||
588 | for (i = 0; i < tcp_c->connections_length; ++i) { | ||
589 | TCP_Connection_to *con_to = get_connection(tcp_c, i); | ||
590 | |||
591 | if (con_to) { | ||
592 | if (tcp_connection_in_conn(con_to, tcp_connections_number)) { | ||
593 | send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); | ||
594 | } | ||
595 | } | ||
596 | } | ||
597 | |||
598 | tcp_relay_set_callbacks(tcp_c, tcp_connections_number); | ||
599 | tcp_con->status = TCP_CONN_CONNECTED; | ||
600 | return 0; | ||
601 | } | ||
602 | |||
603 | static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *relay_pk) | ||
604 | { | ||
605 | int tcp_connections_number = create_tcp_connection(tcp_c); | ||
606 | |||
607 | if (tcp_connections_number == -1) | ||
608 | return -1; | ||
609 | |||
610 | TCP_con *tcp_con = &tcp_c->tcp_connections[tcp_connections_number]; | ||
611 | |||
612 | |||
613 | tcp_con->connection = new_TCP_connection(ip_port, relay_pk, tcp_c->dht->self_public_key, tcp_c->dht->self_secret_key, | ||
614 | &tcp_c->proxy_info); | ||
615 | |||
616 | if (!tcp_con->connection) | ||
617 | return -1; | ||
618 | |||
619 | tcp_con->status = TCP_CONN_VALID; | ||
620 | |||
621 | return tcp_connections_number; | ||
622 | } | ||
623 | |||
473 | /* Add a TCP relay tied to a connection. | 624 | /* Add a TCP relay tied to a connection. |
474 | * | 625 | * |
475 | * return 0 on success. | 626 | * return 0 on success. |
@@ -485,18 +636,38 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_ | |||
485 | int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk); | 636 | int tcp_connections_number = find_tcp_connection_relay(tcp_c, relay_pk); |
486 | 637 | ||
487 | if (tcp_connections_number != -1) { | 638 | if (tcp_connections_number != -1) { |
488 | return add_tcp_connection_to_conn(con_to, tcp_connections_number); | 639 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) |
489 | //TODO if tcp_connections_number online, make a conn request. | 640 | return -1; |
641 | |||
642 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
643 | |||
644 | if (!tcp_con) | ||
645 | return -1; | ||
646 | |||
647 | ++tcp_con->lock_count; | ||
648 | |||
649 | if (tcp_con->status == TCP_CONN_CONNECTED) { | ||
650 | send_tcp_relay_routing_request(tcp_c, tcp_connections_number, con_to->public_key); | ||
651 | } | ||
652 | |||
653 | return 0; | ||
490 | } else { | 654 | } else { |
491 | //TODO | 655 | int tcp_connections_number = add_tcp_relay(tcp_c, ip_port, relay_pk); |
656 | |||
657 | if (add_tcp_connection_to_conn(con_to, tcp_connections_number) == -1) { | ||
658 | return -1; | ||
659 | } | ||
660 | |||
661 | TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number); | ||
662 | |||
663 | if (!tcp_con) | ||
664 | return -1; | ||
665 | |||
666 | ++tcp_con->lock_count; | ||
492 | return 0; | 667 | return 0; |
493 | } | 668 | } |
494 | } | 669 | } |
495 | 670 | ||
496 | static int add_tcp_relay(TCP_Connections *tcp_c, IP_Port ip_port, const uint8_t *public_key) | ||
497 | { | ||
498 | |||
499 | } | ||
500 | 671 | ||
501 | TCP_Connections *new_tcp_connections(DHT *dht) | 672 | TCP_Connections *new_tcp_connections(DHT *dht) |
502 | { | 673 | { |
@@ -512,14 +683,39 @@ TCP_Connections *new_tcp_connections(DHT *dht) | |||
512 | return temp; | 683 | return temp; |
513 | } | 684 | } |
514 | 685 | ||
686 | static void do_tcp_conns(TCP_Connections *tcp_c) | ||
687 | { | ||
688 | unsigned int i; | ||
689 | |||
690 | for (i = 0; i < tcp_c->tcp_connections_length; ++i) { | ||
691 | TCP_con *tcp_con = get_tcp_connection(tcp_c, i); | ||
692 | |||
693 | if (tcp_con) { | ||
694 | do_TCP_connection(tcp_con->connection); | ||
695 | |||
696 | if (tcp_con->connection->status == TCP_CLIENT_DISCONNECTED) { | ||
697 | kill_tcp_relay_connection(tcp_c, i); | ||
698 | continue; | ||
699 | } | ||
700 | |||
701 | if (tcp_con->status == TCP_CONN_VALID && tcp_con->connection->status == TCP_CLIENT_CONFIRMED) { | ||
702 | tcp_relay_on_online(tcp_c, i); | ||
703 | } | ||
704 | } | ||
705 | } | ||
706 | } | ||
707 | |||
515 | 708 | ||
516 | void do_tcp_connections(TCP_Connections *tcp_c) | 709 | void do_tcp_connections(TCP_Connections *tcp_c) |
517 | { | 710 | { |
711 | //TODO kill unused conns | ||
518 | 712 | ||
713 | do_tcp_conns(tcp_c); | ||
519 | } | 714 | } |
520 | 715 | ||
521 | void kill_tcp_connections(TCP_Connections *tcp_c) | 716 | void kill_tcp_connections(TCP_Connections *tcp_c) |
522 | { | 717 | { |
718 | //TODO | ||
523 | free(tcp_c); | 719 | free(tcp_c); |
524 | } | 720 | } |
525 | 721 | ||