diff options
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r-- | toxcore/net_crypto.c | 91 |
1 files changed, 84 insertions, 7 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 1e3456c3..b47460bc 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c | |||
@@ -424,12 +424,18 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, uint8_t *data, | |||
424 | uint32_t i; | 424 | uint32_t i; |
425 | 425 | ||
426 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { | 426 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { |
427 | if (conn->status_tcp[i] == 2) {/* friend is connected to this relay. */ | 427 | if (conn->status_tcp[i] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */ |
428 | if (send_data(c->tcp_connections[i], conn->con_number_tcp[i], data, length) == 1) | 428 | if (send_data(c->tcp_connections[i], conn->con_number_tcp[i], data, length) == 1) |
429 | return 0; | 429 | return 0; |
430 | } | 430 | } |
431 | } | 431 | } |
432 | 432 | ||
433 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { | ||
434 | if (conn->status_tcp[i] == STATUS_TCP_INVISIBLE) { | ||
435 | if (send_oob_packet(c->tcp_connections[i], conn->dht_public_key, data, length) == 1) | ||
436 | return 0; | ||
437 | } | ||
438 | } | ||
433 | return -1; | 439 | return -1; |
434 | } | 440 | } |
435 | 441 | ||
@@ -1461,9 +1467,9 @@ static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id) | |||
1461 | uint32_t i; | 1467 | uint32_t i; |
1462 | 1468 | ||
1463 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { | 1469 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { |
1464 | if (conn->status_tcp[i]) { | 1470 | if (conn->status_tcp[i] != STATUS_TCP_NULL) { |
1465 | send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]); | 1471 | send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]); |
1466 | conn->status_tcp[i] = 0; | 1472 | conn->status_tcp[i] = STATUS_TCP_NULL; |
1467 | conn->con_number_tcp[i] = 0; | 1473 | conn->con_number_tcp[i] = 0; |
1468 | } | 1474 | } |
1469 | } | 1475 | } |
@@ -1575,9 +1581,10 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port) | |||
1575 | if (!ipport_equal(&ip_port, &conn->ip_port)) { | 1581 | if (!ipport_equal(&ip_port, &conn->ip_port)) { |
1576 | conn->ip_port = ip_port; | 1582 | conn->ip_port = ip_port; |
1577 | conn->direct_lastrecv_time = 0; | 1583 | conn->direct_lastrecv_time = 0; |
1584 | return 0; | ||
1578 | } | 1585 | } |
1579 | 1586 | ||
1580 | return 0; | 1587 | return -1; |
1581 | } | 1588 | } |
1582 | 1589 | ||
1583 | static int tcp_response_callback(void *object, uint8_t connection_id, uint8_t *public_key) | 1590 | static int tcp_response_callback(void *object, uint8_t connection_id, uint8_t *public_key) |
@@ -1605,8 +1612,17 @@ static int tcp_response_callback(void *object, uint8_t connection_id, uint8_t *p | |||
1605 | if (c->tcp_connections[location] != TCP_con) | 1612 | if (c->tcp_connections[location] != TCP_con) |
1606 | return -1; | 1613 | return -1; |
1607 | 1614 | ||
1608 | conn->status_tcp[location] = 1; | ||
1609 | conn->con_number_tcp[location] = connection_id; | 1615 | conn->con_number_tcp[location] = connection_id; |
1616 | uint32_t i; | ||
1617 | |||
1618 | for (i = 0; i < conn->num_tcp_relays; ++i) { | ||
1619 | if (memcmp(TCP_con->public_key, conn->tcp_relays[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) { | ||
1620 | conn->status_tcp[location] = STATUS_TCP_INVISIBLE; | ||
1621 | return 0; | ||
1622 | } | ||
1623 | } | ||
1624 | |||
1625 | conn->status_tcp[location] = STATUS_TCP_OFFLINE; | ||
1610 | return 0; | 1626 | return 0; |
1611 | } | 1627 | } |
1612 | 1628 | ||
@@ -1628,13 +1644,19 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection | |||
1628 | if (c->tcp_connections[location] != TCP_con) | 1644 | if (c->tcp_connections[location] != TCP_con) |
1629 | return -1; | 1645 | return -1; |
1630 | 1646 | ||
1631 | conn->status_tcp[location] = status; | 1647 | if (status == 1) { |
1648 | conn->status_tcp[location] = STATUS_TCP_OFFLINE; | ||
1649 | } else if (status == 2) { | ||
1650 | conn->status_tcp[location] = STATUS_TCP_ONLINE; | ||
1651 | } | ||
1652 | |||
1632 | conn->con_number_tcp[location] = connection_id; | 1653 | conn->con_number_tcp[location] = connection_id; |
1633 | return 0; | 1654 | return 0; |
1634 | } | 1655 | } |
1635 | 1656 | ||
1636 | static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length) | 1657 | static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t *data, uint16_t length) |
1637 | { | 1658 | { |
1659 | |||
1638 | if (length == 0) | 1660 | if (length == 0) |
1639 | return -1; | 1661 | return -1; |
1640 | 1662 | ||
@@ -1729,6 +1751,56 @@ static int tcp_connection_check(Net_Crypto *c, uint8_t *public_key) | |||
1729 | return 0; | 1751 | return 0; |
1730 | } | 1752 | } |
1731 | 1753 | ||
1754 | /* Add a tcp relay, associating it to a crypt_connection_id. | ||
1755 | * | ||
1756 | * return 0 if it was added. | ||
1757 | * return -1 if it wasn't. | ||
1758 | */ | ||
1759 | int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, uint8_t *public_key) | ||
1760 | { | ||
1761 | Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); | ||
1762 | |||
1763 | if (conn == 0) | ||
1764 | return -1; | ||
1765 | |||
1766 | if (ip_port.ip.family == TCP_INET) { | ||
1767 | ip_port.ip.family = AF_INET; | ||
1768 | } else if (ip_port.ip.family == TCP_INET6) { | ||
1769 | ip_port.ip.family = AF_INET6; | ||
1770 | } | ||
1771 | |||
1772 | uint32_t i; | ||
1773 | |||
1774 | for (i = 0; i < conn->num_tcp_relays; ++i) { | ||
1775 | if (memcmp(conn->tcp_relays[i].client_id, public_key, crypto_box_PUBLICKEYBYTES) == 0) { | ||
1776 | conn->tcp_relays[i].ip_port = ip_port; | ||
1777 | return 0; | ||
1778 | } | ||
1779 | } | ||
1780 | |||
1781 | if (conn->num_tcp_relays == MAX_TCP_RELAYS_PEER) { | ||
1782 | uint16_t index = rand() % MAX_TCP_RELAYS_PEER; | ||
1783 | conn->tcp_relays[index].ip_port = ip_port; | ||
1784 | memcpy(conn->tcp_relays[index].client_id, public_key, crypto_box_PUBLICKEYBYTES); | ||
1785 | } else { | ||
1786 | conn->tcp_relays[conn->num_tcp_relays].ip_port = ip_port; | ||
1787 | memcpy(conn->tcp_relays[conn->num_tcp_relays].client_id, public_key, crypto_box_PUBLICKEYBYTES); | ||
1788 | ++conn->num_tcp_relays; | ||
1789 | } | ||
1790 | |||
1791 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { | ||
1792 | if (c->tcp_connections[i] == NULL) | ||
1793 | continue; | ||
1794 | |||
1795 | if (memcmp(c->tcp_connections[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) { | ||
1796 | if (conn->status_tcp[i] == STATUS_TCP_OFFLINE) | ||
1797 | conn->status_tcp[i] = STATUS_TCP_INVISIBLE; | ||
1798 | } | ||
1799 | } | ||
1800 | |||
1801 | return add_tcp_relay(c, ip_port, public_key); | ||
1802 | } | ||
1803 | |||
1732 | /* Add a tcp relay to the array. | 1804 | /* Add a tcp relay to the array. |
1733 | * | 1805 | * |
1734 | * return 0 if it was added. | 1806 | * return 0 if it was added. |
@@ -1834,7 +1906,7 @@ static void clear_disconnected_tcp_peer(Crypto_Connection *conn, uint32_t number | |||
1834 | if (number >= MAX_TCP_CONNECTIONS) | 1906 | if (number >= MAX_TCP_CONNECTIONS) |
1835 | return; | 1907 | return; |
1836 | 1908 | ||
1837 | conn->status_tcp[number] = 0; | 1909 | conn->status_tcp[number] = STATUS_TCP_NULL; |
1838 | conn->con_number_tcp[number] = 0; | 1910 | conn->con_number_tcp[number] = 0; |
1839 | } | 1911 | } |
1840 | 1912 | ||
@@ -2255,6 +2327,11 @@ void kill_net_crypto(Net_Crypto *c) | |||
2255 | crypto_kill(c, i); | 2327 | crypto_kill(c, i); |
2256 | } | 2328 | } |
2257 | 2329 | ||
2330 | for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { | ||
2331 | kill_TCP_connection(c->tcp_connections_new[i]); | ||
2332 | kill_TCP_connection(c->tcp_connections[i]); | ||
2333 | } | ||
2334 | |||
2258 | networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL); | 2335 | networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL); |
2259 | networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); | 2336 | networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); |
2260 | networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_HS, NULL, NULL); | 2337 | networking_registerhandler(c->dht->net, NET_PACKET_CRYPTO_HS, NULL, NULL); |