summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--toxcore/net_crypto.c620
-rw-r--r--toxcore/net_crypto.h29
-rw-r--r--toxcore/onion_client.c4
3 files changed, 85 insertions, 568 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 3e596a5f..c93f4f41 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -206,8 +206,7 @@ static int udp_handle_cookie_request(void *object, IP_Port source, const uint8_t
206 206
207/* Handle the cookie request packet (for TCP) 207/* Handle the cookie request packet (for TCP)
208 */ 208 */
209static int tcp_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con, uint8_t conn_id, 209static int tcp_handle_cookie_request(Net_Crypto *c, int connections_number, const uint8_t *packet, uint16_t length)
210 const uint8_t *packet, uint16_t length)
211{ 210{
212 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; 211 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH];
213 uint8_t shared_key[crypto_box_BEFORENMBYTES]; 212 uint8_t shared_key[crypto_box_BEFORENMBYTES];
@@ -221,15 +220,13 @@ static int tcp_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection
221 if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) 220 if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data))
222 return -1; 221 return -1;
223 222
224 if (send_data(TCP_con, conn_id, data, sizeof(data)) != 1) 223 int ret = send_packet_tcp_connection(c->tcp_c, connections_number, data, sizeof(data));
225 return -1; 224 return ret;
226
227 return 0;
228} 225}
229 226
230/* Handle the cookie request packet (for TCP oob packets) 227/* Handle the cookie request packet (for TCP oob packets)
231 */ 228 */
232static int tcp_oob_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connection *TCP_con, 229static int tcp_oob_handle_cookie_request(const Net_Crypto *c, unsigned int tcp_connections_number,
233 const uint8_t *dht_public_key, const uint8_t *packet, uint16_t length) 230 const uint8_t *dht_public_key, const uint8_t *packet, uint16_t length)
234{ 231{
235 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH]; 232 uint8_t request_plain[COOKIE_REQUEST_PLAIN_LENGTH];
@@ -247,10 +244,8 @@ static int tcp_oob_handle_cookie_request(const Net_Crypto *c, TCP_Client_Connect
247 if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data)) 244 if (create_cookie_response(c, data, request_plain, shared_key, dht_public_key) != sizeof(data))
248 return -1; 245 return -1;
249 246
250 if (send_oob_packet(TCP_con, dht_public_key, data, sizeof(data)) != 1) 247 int ret = tcp_send_oob_packet(c->tcp_c, tcp_connections_number, dht_public_key, data, sizeof(data));
251 return -1; 248 return ret;
252
253 return 0;
254} 249}
255 250
256/* Handle a cookie response packet of length encrypted with shared_key. 251/* Handle a cookie response packet of length encrypted with shared_key.
@@ -417,57 +412,11 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
417 } 412 }
418 413
419 pthread_mutex_unlock(&conn->mutex); 414 pthread_mutex_unlock(&conn->mutex);
415 pthread_mutex_lock(&c->tcp_mutex);
416 int ret = send_packet_tcp_connection(c->tcp_c, conn->connection_number_tcp, data, length);
417 pthread_mutex_unlock(&c->tcp_mutex);
420 418
421 //TODO: detect and kill bad relays. 419 if (ret == 0 || direct_send_attempt) {
422 uint32_t i;
423
424 unsigned int r;
425
426 if (!conn->last_relay_sentto) {
427 r = rand();
428 } else {
429 r = conn->last_relay_sentto - 1;
430 }
431
432 if (conn->num_tcp_online) {
433 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
434 pthread_mutex_lock(&c->tcp_mutex);
435
436 unsigned int tcp_index = (i + r) % MAX_TCP_CONNECTIONS;
437 int ret = 0;
438
439 if (conn->status_tcp[tcp_index] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */
440 ret = send_data(c->tcp_connections[tcp_index], conn->con_number_tcp[tcp_index], data, length);
441 }
442
443 pthread_mutex_unlock(&c->tcp_mutex);
444
445 if (ret == 1) {
446 conn->last_relay_sentto = tcp_index + 1;
447 return 0;
448 }
449 }
450 }
451
452 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
453 pthread_mutex_lock(&c->tcp_mutex);
454
455 unsigned int tcp_index = (i + r) % MAX_TCP_CONNECTIONS;
456 int ret = 0;
457
458 if (conn->status_tcp[tcp_index] == STATUS_TCP_INVISIBLE) {
459 ret = send_oob_packet(c->tcp_connections[tcp_index], conn->dht_public_key, data, length);
460 }
461
462 pthread_mutex_unlock(&c->tcp_mutex);
463
464 if (ret == 1) {
465 conn->last_relay_sentto = tcp_index + 1;
466 return 0;
467 }
468 }
469
470 if (direct_send_attempt) {
471 return 0; 420 return 0;
472 } 421 }
473 422
@@ -1472,7 +1421,6 @@ static int getcryptconnection_id_dht_pubkey(const Net_Crypto *c, const uint8_t *
1472 * return -1 on failure. 1421 * return -1 on failure.
1473 * return positive number on success. 1422 * return positive number on success.
1474 * 0 if source was a direct UDP connection. 1423 * 0 if source was a direct UDP connection.
1475 * TODO
1476 */ 1424 */
1477static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, IP_Port source) 1425static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id, IP_Port source)
1478{ 1426{
@@ -1492,6 +1440,9 @@ static int crypto_connection_add_source(Net_Crypto *c, int crypt_connection_id,
1492 1440
1493 conn->direct_lastrecv_time = current_time_monotonic(); 1441 conn->direct_lastrecv_time = current_time_monotonic();
1494 return 0; 1442 return 0;
1443 } else if (source.ip.family == TCP_FAMILY) {
1444 if (add_tcp_number_relay_connection(c->tcp_c, conn->connection_number_tcp, source.ip.ip6.uint32[0]) == 0)
1445 return 1;
1495 } 1446 }
1496 1447
1497 return -1; 1448 return -1;
@@ -1588,6 +1539,7 @@ int accept_crypto_connection(Net_Crypto *c, New_Connection *n_c)
1588 if (conn == 0) 1539 if (conn == 0)
1589 return -1; 1540 return -1;
1590 1541
1542 conn->connection_number_tcp = -1;
1591 memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES); 1543 memcpy(conn->public_key, n_c->public_key, crypto_box_PUBLICKEYBYTES);
1592 memcpy(conn->recv_nonce, n_c->recv_nonce, crypto_box_NONCEBYTES); 1544 memcpy(conn->recv_nonce, n_c->recv_nonce, crypto_box_NONCEBYTES);
1593 memcpy(conn->peersessionpublic_key, n_c->peersessionpublic_key, crypto_box_PUBLICKEYBYTES); 1545 memcpy(conn->peersessionpublic_key, n_c->peersessionpublic_key, crypto_box_PUBLICKEYBYTES);
@@ -1633,6 +1585,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key)
1633 if (conn == 0) 1585 if (conn == 0)
1634 return -1; 1586 return -1;
1635 1587
1588 conn->connection_number_tcp = -1;
1636 memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES); 1589 memcpy(conn->public_key, real_public_key, crypto_box_PUBLICKEYBYTES);
1637 random_nonce(conn->sent_nonce); 1590 random_nonce(conn->sent_nonce);
1638 crypto_box_keypair(conn->sessionpublic_key, conn->sessionsecret_key); 1591 crypto_box_keypair(conn->sessionpublic_key, conn->sessionsecret_key);
@@ -1642,79 +1595,6 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key)
1642 return crypt_connection_id; 1595 return crypt_connection_id;
1643} 1596}
1644 1597
1645/* Set the status for the TCP connection for conn in location to status.
1646 */
1647static void set_conn_tcp_status(Crypto_Connection *conn, unsigned int location, unsigned int status)
1648{
1649 if (conn->status_tcp[location] == status) {
1650 return;
1651 }
1652
1653 if (conn->status_tcp[location] == STATUS_TCP_ONLINE) {
1654 --conn->num_tcp_online;
1655 }
1656
1657 if (status == STATUS_TCP_ONLINE) {
1658 ++conn->num_tcp_online;
1659 }
1660
1661 conn->status_tcp[location] = status;
1662}
1663
1664/* Disconnect peer from all associated TCP connections.
1665 *
1666 * return -1 on failure.
1667 * return 0 on success.
1668 */
1669static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1670{
1671 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1672
1673 if (conn == 0)
1674 return -1;
1675
1676 uint32_t i;
1677
1678 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1679 if (conn->status_tcp[i] != STATUS_TCP_NULL) {
1680 pthread_mutex_lock(&c->tcp_mutex);
1681 send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]);
1682 set_conn_tcp_status(conn, i, STATUS_TCP_NULL);
1683 conn->con_number_tcp[i] = 0;
1684 pthread_mutex_unlock(&c->tcp_mutex);
1685 }
1686 }
1687
1688 return 0;
1689}
1690
1691/* Connect peer to all associated TCP connections.
1692 *
1693 * return -1 on failure.
1694 * return 0 on success.
1695 */
1696static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1697{
1698 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1699
1700 if (conn == 0)
1701 return -1;
1702
1703 uint32_t i;
1704
1705 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1706 if (c->tcp_connections[i] == NULL)
1707 continue;
1708
1709 pthread_mutex_lock(&c->tcp_mutex);
1710 //TODO check function return?
1711 send_routing_request(c->tcp_connections[i], conn->dht_public_key);
1712 pthread_mutex_unlock(&c->tcp_mutex);
1713 }
1714
1715 return 0;
1716}
1717
1718/* Copy friends DHT public key into dht_key. 1598/* Copy friends DHT public key into dht_key.
1719 * 1599 *
1720 * return 0 on failure (no key copied). 1600 * return 0 on failure (no key copied).
@@ -1751,7 +1631,10 @@ int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const
1751 return -1; 1631 return -1;
1752 1632
1753 if (conn->dht_public_key_set == 1) { 1633 if (conn->dht_public_key_set == 1) {
1754 disconnect_peer_tcp(c, crypt_connection_id); 1634 pthread_mutex_lock(&c->tcp_mutex);
1635 kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp);
1636 pthread_mutex_unlock(&c->tcp_mutex);
1637 conn->connection_number_tcp = -1;
1755 } 1638 }
1756 1639
1757 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES); 1640 memcpy(conn->dht_public_key, dht_public_key, crypto_box_PUBLICKEYBYTES);
@@ -1769,7 +1652,9 @@ int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const
1769 return -1; 1652 return -1;
1770 }//TODO 1653 }//TODO
1771 1654
1772 connect_peer_tcp(c, crypt_connection_id); 1655 pthread_mutex_lock(&c->tcp_mutex);
1656 conn->connection_number_tcp = new_tcp_connection_to(c->tcp_c, conn->dht_public_key, crypt_connection_id);
1657 pthread_mutex_unlock(&c->tcp_mutex);
1773 return 0; 1658 return 0;
1774} 1659}
1775 1660
@@ -1805,93 +1690,25 @@ int set_direct_ip_port(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port)
1805 return -1; 1690 return -1;
1806} 1691}
1807 1692
1808static int tcp_response_callback(void *object, uint8_t connection_id, const uint8_t *public_key)
1809{
1810 TCP_Client_Connection *TCP_con = object;
1811 Net_Crypto *c = TCP_con->net_crypto_pointer;
1812
1813 int crypt_connection_id = getcryptconnection_id_dht_pubkey(c, public_key);
1814
1815 if (crypt_connection_id == -1)
1816 return -1;
1817
1818 set_tcp_connection_number(TCP_con, connection_id, crypt_connection_id);
1819
1820 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1821
1822 if (conn == 0)
1823 return -1;
1824
1825 uint32_t location = TCP_con->net_crypto_location;
1826
1827 if (location >= MAX_TCP_CONNECTIONS)
1828 return -1;
1829
1830 if (c->tcp_connections[location] != TCP_con)
1831 return -1;
1832
1833 conn->con_number_tcp[location] = connection_id;
1834 uint32_t i;
1835
1836 for (i = 0; i < conn->num_tcp_relays; ++i) {
1837 if (memcmp(TCP_con->public_key, conn->tcp_relays[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) {
1838 set_conn_tcp_status(conn, location, STATUS_TCP_INVISIBLE);
1839 return 0;
1840 }
1841 }
1842
1843 set_conn_tcp_status(conn, location, STATUS_TCP_OFFLINE);
1844 return 0;
1845}
1846 1693
1847static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status) 1694static int tcp_data_callback(void *object, int id, const uint8_t *data, uint16_t length)
1848{ 1695{
1849 TCP_Client_Connection *TCP_con = object; 1696 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
1850 Net_Crypto *c = TCP_con->net_crypto_pointer;
1851
1852 Crypto_Connection *conn = get_crypto_connection(c, number);
1853
1854 if (conn == 0)
1855 return -1;
1856
1857 uint32_t location = TCP_con->net_crypto_location;
1858
1859 if (location >= MAX_TCP_CONNECTIONS)
1860 return -1;
1861
1862 if (c->tcp_connections[location] != TCP_con)
1863 return -1; 1697 return -1;
1864 1698
1865 if (status == 1) { 1699 Net_Crypto *c = object;
1866 set_conn_tcp_status(conn, location, STATUS_TCP_OFFLINE);
1867 } else if (status == 2) {
1868 set_conn_tcp_status(conn, location, STATUS_TCP_ONLINE);
1869 }
1870
1871 conn->con_number_tcp[location] = connection_id;
1872 return 0;
1873}
1874 1700
1875static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_id, const uint8_t *data, uint16_t length) 1701 Crypto_Connection *conn = get_crypto_connection(c, id);
1876{
1877 1702
1878 if (length == 0) 1703 if (conn == 0)
1879 return -1; 1704 return -1;
1880 1705
1881 TCP_Client_Connection *TCP_con = object;
1882 Net_Crypto *c = TCP_con->net_crypto_pointer;
1883
1884 if (data[0] == NET_PACKET_COOKIE_REQUEST) { 1706 if (data[0] == NET_PACKET_COOKIE_REQUEST) {
1885 return tcp_handle_cookie_request(c, TCP_con, connection_id, data, length); 1707 return tcp_handle_cookie_request(c, conn->connection_number_tcp, data, length);
1886 } 1708 }
1887 1709
1888 Crypto_Connection *conn = get_crypto_connection(c, number);
1889
1890 if (conn == 0)
1891 return -1;
1892
1893 pthread_mutex_unlock(&c->tcp_mutex); 1710 pthread_mutex_unlock(&c->tcp_mutex);
1894 int ret = handle_packet_connection(c, number, data, length); 1711 int ret = handle_packet_connection(c, id, data, length);
1895 pthread_mutex_lock(&c->tcp_mutex); 1712 pthread_mutex_lock(&c->tcp_mutex);
1896 1713
1897 if (ret != 0) 1714 if (ret != 0)
@@ -1901,92 +1718,29 @@ static int tcp_data_callback(void *object, uint32_t number, uint8_t connection_i
1901 return 0; 1718 return 0;
1902} 1719}
1903 1720
1904static int tcp_oob_callback(void *object, const uint8_t *public_key, const uint8_t *data, uint16_t length) 1721static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned int tcp_connections_number,
1722 const uint8_t *data, uint16_t length)
1905{ 1723{
1906 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE) 1724 if (length == 0 || length > MAX_CRYPTO_PACKET_SIZE)
1907 return -1; 1725 return -1;
1908 1726
1909 TCP_Client_Connection *TCP_con = object; 1727 Net_Crypto *c = object;
1910 Net_Crypto *c = TCP_con->net_crypto_pointer;
1911 uint32_t location = TCP_con->net_crypto_location;
1912 1728
1913 if (data[0] == NET_PACKET_COOKIE_REQUEST) { 1729 if (data[0] == NET_PACKET_COOKIE_REQUEST) {
1914 return tcp_oob_handle_cookie_request(c, TCP_con, public_key, data, length); 1730 return tcp_oob_handle_cookie_request(c, tcp_connections_number, public_key, data, length);
1915 } 1731 } else if (data[0] == NET_PACKET_CRYPTO_HS) {
1916
1917 int crypt_connection_id = getcryptconnection_id_dht_pubkey(c, public_key);
1918
1919 if (crypt_connection_id == -1) {
1920 IP_Port source; 1732 IP_Port source;
1921 source.port = 0; 1733 source.port = 0;
1922 source.ip.family = TCP_FAMILY; 1734 source.ip.family = TCP_FAMILY;
1923 source.ip.ip6.uint32[0] = location; 1735 source.ip.ip6.uint32[0] = tcp_connections_number;
1924
1925 if (data[0] != NET_PACKET_CRYPTO_HS) {
1926 LOGGER_DEBUG("tcp snhappen %u\n", data[0]);
1927 return -1;
1928 }
1929 1736
1930 if (handle_new_connection_handshake(c, source, data, length) != 0) 1737 if (handle_new_connection_handshake(c, source, data, length) != 0)
1931 return -1; 1738 return -1;
1932 1739
1933 return 0; 1740 return 0;
1934 } 1741 } else {
1935
1936 pthread_mutex_unlock(&c->tcp_mutex);
1937 int ret = handle_packet_connection(c, crypt_connection_id, data, length);
1938 pthread_mutex_lock(&c->tcp_mutex);
1939
1940 if (ret != 0)
1941 return -1; 1742 return -1;
1942
1943 return 0;
1944}
1945
1946static int tcp_onion_callback(void *object, const uint8_t *data, uint16_t length)
1947{
1948 Net_Crypto *c = object;
1949
1950 if (c->tcp_onion_callback)
1951 return c->tcp_onion_callback(c->tcp_onion_callback_object, data, length);
1952
1953 return 1;
1954}
1955
1956
1957/* Check if tcp connection to public key can be created.
1958 *
1959 * return -1 if it can't.
1960 * return 0 if it can.
1961 */
1962static int tcp_connection_check(const Net_Crypto *c, const uint8_t *public_key)
1963{
1964 uint32_t i;
1965
1966 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1967 if (c->tcp_connections_new[i] == NULL)
1968 continue;
1969
1970 if (memcmp(c->tcp_connections_new[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
1971 return -1;
1972 }
1973
1974 uint32_t num = 0;
1975
1976 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1977 if (c->tcp_connections[i] == NULL)
1978 continue;
1979
1980 if (memcmp(c->tcp_connections[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0)
1981 return -1;
1982
1983 ++num;
1984 } 1743 }
1985
1986 if (num == MAX_TCP_CONNECTIONS)
1987 return -1;
1988
1989 return 0;
1990} 1744}
1991 1745
1992/* Add a tcp relay, associating it to a crypt_connection_id. 1746/* Add a tcp relay, associating it to a crypt_connection_id.
@@ -2001,45 +1755,10 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
2001 if (conn == 0) 1755 if (conn == 0)
2002 return -1; 1756 return -1;
2003 1757
2004 if (ip_port.ip.family == TCP_INET) { 1758 pthread_mutex_lock(&c->tcp_mutex);
2005 ip_port.ip.family = AF_INET; 1759 int ret = add_tcp_relay_connection(c->tcp_c, conn->connection_number_tcp, ip_port, public_key);
2006 } else if (ip_port.ip.family == TCP_INET6) { 1760 pthread_mutex_unlock(&c->tcp_mutex);
2007 ip_port.ip.family = AF_INET6; 1761 return ret;
2008 }
2009
2010 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
2011 return -1;
2012
2013 uint32_t i;
2014
2015 for (i = 0; i < conn->num_tcp_relays; ++i) {
2016 if (memcmp(conn->tcp_relays[i].public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
2017 conn->tcp_relays[i].ip_port = ip_port;
2018 return 0;
2019 }
2020 }
2021
2022 if (conn->num_tcp_relays == MAX_TCP_RELAYS_PEER) {
2023 uint16_t index = rand() % MAX_TCP_RELAYS_PEER;
2024 conn->tcp_relays[index].ip_port = ip_port;
2025 memcpy(conn->tcp_relays[index].public_key, public_key, crypto_box_PUBLICKEYBYTES);
2026 } else {
2027 conn->tcp_relays[conn->num_tcp_relays].ip_port = ip_port;
2028 memcpy(conn->tcp_relays[conn->num_tcp_relays].public_key, public_key, crypto_box_PUBLICKEYBYTES);
2029 ++conn->num_tcp_relays;
2030 }
2031
2032 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2033 if (c->tcp_connections[i] == NULL)
2034 continue;
2035
2036 if (memcmp(c->tcp_connections[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
2037 if (conn->status_tcp[i] == STATUS_TCP_OFFLINE)
2038 set_conn_tcp_status(conn, i, STATUS_TCP_INVISIBLE);
2039 }
2040 }
2041
2042 return add_tcp_relay(c, ip_port, public_key);
2043} 1762}
2044 1763
2045/* Add a tcp relay to the array. 1764/* Add a tcp relay to the array.
@@ -2049,30 +1768,10 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
2049 */ 1768 */
2050int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key) 1769int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
2051{ 1770{
2052 if (ip_port.ip.family == TCP_INET) { 1771 pthread_mutex_lock(&c->tcp_mutex);
2053 ip_port.ip.family = AF_INET; 1772 int ret = add_tcp_relay_global(c->tcp_c, ip_port, public_key);
2054 } else if (ip_port.ip.family == TCP_INET6) { 1773 pthread_mutex_unlock(&c->tcp_mutex);
2055 ip_port.ip.family = AF_INET6; 1774 return ret;
2056 }
2057
2058 if (ip_port.ip.family != AF_INET && ip_port.ip.family != AF_INET6)
2059 return -1;
2060
2061 if (tcp_connection_check(c, public_key) != 0)
2062 return -1;
2063
2064 uint32_t i;
2065
2066 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2067 if (c->tcp_connections_new[i] == NULL) {
2068 c->tcp_connections_new[i] = new_TCP_connection(ip_port, public_key, c->dht->self_public_key, c->dht->self_secret_key,
2069 &c->proxy_info);
2070
2071 return 0;
2072 }
2073 }
2074
2075 return -1;
2076} 1775}
2077 1776
2078/* Return a random TCP connection number for use in send_tcp_onion_request. 1777/* Return a random TCP connection number for use in send_tcp_onion_request.
@@ -2085,47 +1784,25 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
2085 */ 1784 */
2086int get_random_tcp_con_number(Net_Crypto *c) 1785int get_random_tcp_con_number(Net_Crypto *c)
2087{ 1786{
2088 unsigned int i, r = rand(); 1787 pthread_mutex_lock(&c->tcp_mutex);
2089 1788 int ret = get_random_tcp_conn_number(c->tcp_c);
2090 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 1789 pthread_mutex_unlock(&c->tcp_mutex);
2091 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) {
2092 return (i + r) % MAX_TCP_CONNECTIONS;
2093 }
2094 }
2095 1790
2096 return -1; 1791 return ret;
2097} 1792}
2098 1793
2099/* Send an onion packet via the TCP relay corresponding to TCP_conn_number. 1794/* Send an onion packet via the TCP relay corresponding to tcp_connections_number.
2100 * 1795 *
2101 * return 0 on success. 1796 * return 0 on success.
2102 * return -1 on failure. 1797 * return -1 on failure.
2103 */ 1798 */
2104int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const uint8_t *data, uint16_t length) 1799int send_tcp_onion_request(Net_Crypto *c, unsigned int tcp_connections_number, const uint8_t *data, uint16_t length)
2105{ 1800{
2106 if (TCP_conn_number > MAX_TCP_CONNECTIONS) { 1801 pthread_mutex_lock(&c->tcp_mutex);
2107 return -1; 1802 int ret = tcp_send_onion_request(c->tcp_c, tcp_connections_number, data, length);
2108 } 1803 pthread_mutex_unlock(&c->tcp_mutex);
2109
2110 if (c->tcp_connections[TCP_conn_number]) {
2111 pthread_mutex_lock(&c->tcp_mutex);
2112 int ret = send_onion_request(c->tcp_connections[TCP_conn_number], data, length);
2113 pthread_mutex_unlock(&c->tcp_mutex);
2114
2115 if (ret == 1)
2116 return 0;
2117 }
2118
2119 return -1;
2120}
2121 1804
2122/* Set the function to be called when an onion response packet is received by one of the TCP connections. 1805 return ret;
2123 */
2124void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
2125 uint16_t length), void *object)
2126{
2127 c->tcp_onion_callback = tcp_onion_callback;
2128 c->tcp_onion_callback_object = object;
2129} 1806}
2130 1807
2131/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 1808/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
@@ -2134,173 +1811,23 @@ void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *o
2134 * return number of relays copied to tcp_relays on success. 1811 * return number of relays copied to tcp_relays on success.
2135 * return 0 on failure. 1812 * return 0 on failure.
2136 */ 1813 */
2137unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num) 1814unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num)
2138{ 1815{
2139 if (num == 0) 1816 if (num == 0)
2140 return 0; 1817 return 0;
2141 1818
2142 uint32_t i; 1819 pthread_mutex_lock(&c->tcp_mutex);
2143 uint16_t copied = 0; 1820 unsigned int ret = tcp_copy_connected_relays(c->tcp_c, tcp_relays, num);
2144 1821 pthread_mutex_unlock(&c->tcp_mutex);
2145 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2146 if (c->tcp_connections[i] != NULL) {
2147 memcpy(tcp_relays[copied].public_key, c->tcp_connections[i]->public_key, crypto_box_PUBLICKEYBYTES);
2148 tcp_relays[copied].ip_port = c->tcp_connections[i]->ip_port;
2149
2150 if (tcp_relays[copied].ip_port.ip.family == AF_INET) {
2151 tcp_relays[copied].ip_port.ip.family = TCP_INET;
2152 } else if (tcp_relays[copied].ip_port.ip.family == AF_INET6) {
2153 tcp_relays[copied].ip_port.ip.family = TCP_INET6;
2154 }
2155
2156 ++copied;
2157
2158 if (copied == num)
2159 return copied;
2160 }
2161 }
2162
2163 return copied;
2164}
2165
2166/* Add a connected tcp connection to the tcp_connections array.
2167 *
2168 * return 0 if it was added.
2169 * return -1 if it wasn't.
2170 */
2171static int add_tcp_connected(Net_Crypto *c, TCP_Client_Connection *tcp_con)
2172{
2173 uint32_t i;
2174
2175 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2176 if (c->tcp_connections[i] == NULL)
2177 break;
2178 }
2179
2180 if (i == MAX_TCP_CONNECTIONS)
2181 return -1;
2182
2183 uint32_t tcp_num = i;
2184
2185 for (i = 0; i < c->crypto_connections_length; ++i) {
2186 Crypto_Connection *conn = get_crypto_connection(c, i);
2187
2188 if (conn == 0)
2189 return -1;
2190
2191 if (conn->status == CRYPTO_CONN_NO_CONNECTION)
2192 continue;
2193
2194 if (conn->status == CRYPTO_CONN_TIMED_OUT)
2195 continue;
2196
2197 if (conn->dht_public_key_set)
2198 if (send_routing_request(tcp_con, conn->dht_public_key) != 1)
2199 return -1;
2200
2201 }
2202 1822
2203 tcp_con->net_crypto_pointer = c; 1823 return ret;
2204 tcp_con->net_crypto_location = tcp_num;
2205 routing_response_handler(tcp_con, tcp_response_callback, tcp_con);
2206 routing_status_handler(tcp_con, tcp_status_callback, tcp_con);
2207 routing_data_handler(tcp_con, tcp_data_callback, tcp_con);
2208 oob_data_handler(tcp_con, tcp_oob_callback, tcp_con);
2209 onion_response_handler(tcp_con, tcp_onion_callback, c);
2210 c->tcp_connections[tcp_num] = tcp_con;
2211 return 0;
2212} 1824}
2213 1825
2214static void do_tcp(Net_Crypto *c) 1826static void do_tcp(Net_Crypto *c)
2215{ 1827{
2216 uint32_t i; 1828 pthread_mutex_lock(&c->tcp_mutex);
2217 1829 do_tcp_connections(c->tcp_c);
2218 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 1830 pthread_mutex_unlock(&c->tcp_mutex);
2219 if (c->tcp_connections_new[i] == NULL)
2220 continue;
2221
2222 pthread_mutex_lock(&c->tcp_mutex);
2223 do_TCP_connection(c->tcp_connections_new[i]);
2224 pthread_mutex_unlock(&c->tcp_mutex);
2225
2226 if (c->tcp_connections_new[i]->status == TCP_CLIENT_CONFIRMED) {
2227 pthread_mutex_lock(&c->tcp_mutex);
2228 int ret = add_tcp_connected(c, c->tcp_connections_new[i]);
2229 pthread_mutex_unlock(&c->tcp_mutex);
2230
2231 if (ret == 0) {
2232 c->tcp_connections_new[i] = NULL;
2233 } else {
2234 kill_TCP_connection(c->tcp_connections_new[i]);
2235 c->tcp_connections_new[i] = NULL;
2236 }
2237 }
2238 }
2239
2240 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2241 if (c->tcp_connections[i] == NULL)
2242 continue;
2243
2244 pthread_mutex_lock(&c->tcp_mutex);
2245 do_TCP_connection(c->tcp_connections[i]);
2246 pthread_mutex_unlock(&c->tcp_mutex);
2247 }
2248}
2249
2250static void clear_disconnected_tcp_peer(Crypto_Connection *conn, uint32_t number)
2251{
2252 if (conn->status == CRYPTO_CONN_NO_CONNECTION)
2253 return;
2254
2255 if (number >= MAX_TCP_CONNECTIONS)
2256 return;
2257
2258 set_conn_tcp_status(conn, number, STATUS_TCP_NULL);
2259 conn->con_number_tcp[number] = 0;
2260}
2261
2262static void clear_disconnected_tcp(Net_Crypto *c)
2263{
2264 uint32_t i, j;
2265
2266 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2267 if (c->tcp_connections_new[i] == NULL)
2268 continue;
2269
2270 if (c->tcp_connections_new[i]->status != TCP_CLIENT_DISCONNECTED)
2271 continue;
2272
2273 kill_TCP_connection(c->tcp_connections_new[i]);
2274 c->tcp_connections_new[i] = NULL;
2275 }
2276
2277 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2278 if (c->tcp_connections[i] == NULL)
2279 continue;
2280
2281 TCP_Client_Connection *tcp_con = c->tcp_connections[i];
2282
2283 if (tcp_con->status != TCP_CLIENT_DISCONNECTED)
2284 continue;
2285
2286 /* Try reconnecting to relay on disconnect. */
2287 add_tcp_relay(c, tcp_con->ip_port, tcp_con->public_key);
2288
2289 pthread_mutex_lock(&c->tcp_mutex);
2290 c->tcp_connections[i] = NULL;
2291 kill_TCP_connection(tcp_con);
2292
2293 for (j = 0; j < c->crypto_connections_length; ++j) {
2294 Crypto_Connection *conn = get_crypto_connection(c, j);
2295
2296 if (conn == 0)
2297 continue;
2298
2299 clear_disconnected_tcp_peer(conn, i);
2300 }
2301
2302 pthread_mutex_unlock(&c->tcp_mutex);
2303 }
2304} 1831}
2305 1832
2306/* Set function to be called when connection with crypt_connection_id goes connects/disconnects. 1833/* Set function to be called when connection with crypt_connection_id goes connects/disconnects.
@@ -2746,7 +2273,10 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
2746 if (conn->status == CRYPTO_CONN_ESTABLISHED) 2273 if (conn->status == CRYPTO_CONN_ESTABLISHED)
2747 send_kill_packet(c, crypt_connection_id); 2274 send_kill_packet(c, crypt_connection_id);
2748 2275
2749 disconnect_peer_tcp(c, crypt_connection_id); 2276 pthread_mutex_lock(&c->tcp_mutex);
2277 kill_tcp_connection_to(c->tcp_c, conn->connection_number_tcp);
2278 pthread_mutex_unlock(&c->tcp_mutex);
2279
2750 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id); 2280 bs_list_remove(&c->ip_port_list, &conn->ip_port, crypt_connection_id);
2751 clear_temp_packet(c, crypt_connection_id); 2281 clear_temp_packet(c, crypt_connection_id);
2752 clear_buffer(&conn->send_array); 2282 clear_buffer(&conn->send_array);
@@ -2816,8 +2346,19 @@ Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info)
2816 if (temp == NULL) 2346 if (temp == NULL)
2817 return NULL; 2347 return NULL;
2818 2348
2349 temp->tcp_c = new_tcp_connections(dht);
2350
2351 if (temp->tcp_c == NULL) {
2352 free(temp);
2353 return NULL;
2354 }
2355
2356 set_packet_tcp_connection_callback(temp->tcp_c, &tcp_data_callback, temp);
2357 set_oob_packet_tcp_connection_callback(temp->tcp_c, &tcp_oob_callback, temp);
2358
2819 if (create_recursive_mutex(&temp->tcp_mutex) != 0 || 2359 if (create_recursive_mutex(&temp->tcp_mutex) != 0 ||
2820 pthread_mutex_init(&temp->connections_mutex, NULL) != 0) { 2360 pthread_mutex_init(&temp->connections_mutex, NULL) != 0) {
2361 kill_tcp_connections(temp->tcp_c);
2821 free(temp); 2362 free(temp);
2822 return NULL; 2363 return NULL;
2823 } 2364 }
@@ -2894,7 +2435,6 @@ void do_net_crypto(Net_Crypto *c)
2894 unix_time_update(); 2435 unix_time_update();
2895 kill_timedout(c); 2436 kill_timedout(c);
2896 do_tcp(c); 2437 do_tcp(c);
2897 clear_disconnected_tcp(c);
2898 send_crypto_packets(c); 2438 send_crypto_packets(c);
2899} 2439}
2900 2440
@@ -2906,14 +2446,10 @@ void kill_net_crypto(Net_Crypto *c)
2906 crypto_kill(c, i); 2446 crypto_kill(c, i);
2907 } 2447 }
2908 2448
2909 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
2910 kill_TCP_connection(c->tcp_connections_new[i]);
2911 kill_TCP_connection(c->tcp_connections[i]);
2912 }
2913
2914 pthread_mutex_destroy(&c->tcp_mutex); 2449 pthread_mutex_destroy(&c->tcp_mutex);
2915 pthread_mutex_destroy(&c->connections_mutex); 2450 pthread_mutex_destroy(&c->connections_mutex);
2916 2451
2452 kill_tcp_connections(c->tcp_c);
2917 bs_list_free(&c->ip_port_list); 2453 bs_list_free(&c->ip_port_list);
2918 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL); 2454 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_REQUEST, NULL, NULL);
2919 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL); 2455 networking_registerhandler(c->dht->net, NET_PACKET_COOKIE_RESPONSE, NULL, NULL);
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index fab72cb3..2b107f20 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -26,7 +26,7 @@
26 26
27#include "DHT.h" 27#include "DHT.h"
28#include "LAN_discovery.h" 28#include "LAN_discovery.h"
29#include "TCP_client.h" 29#include "TCP_connection.h"
30#include <pthread.h> 30#include <pthread.h>
31 31
32#define CRYPTO_CONN_NO_CONNECTION 0 32#define CRYPTO_CONN_NO_CONNECTION 0
@@ -73,11 +73,6 @@
73#define MAX_TCP_CONNECTIONS 64 73#define MAX_TCP_CONNECTIONS 64
74#define MAX_TCP_RELAYS_PEER 4 74#define MAX_TCP_RELAYS_PEER 4
75 75
76#define STATUS_TCP_NULL 0
77#define STATUS_TCP_OFFLINE 1
78#define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */
79#define STATUS_TCP_ONLINE 3
80
81/* All packets starting with a byte in this range are considered lossy packets. */ 76/* All packets starting with a byte in this range are considered lossy packets. */
82#define PACKET_ID_LOSSY_RANGE_START 192 77#define PACKET_ID_LOSSY_RANGE_START 192
83#define PACKET_ID_LOSSY_RANGE_SIZE 63 78#define PACKET_ID_LOSSY_RANGE_SIZE 63
@@ -157,13 +152,8 @@ typedef struct {
157 152
158 uint8_t killed; /* set to 1 to kill the connection. */ 153 uint8_t killed; /* set to 1 to kill the connection. */
159 154
160 uint8_t status_tcp[MAX_TCP_CONNECTIONS]; /* set to one of STATUS_TCP_* */ 155 /* TCP_connection connection_number */
161 uint8_t con_number_tcp[MAX_TCP_CONNECTIONS]; 156 unsigned int connection_number_tcp;
162 unsigned int last_relay_sentto;
163 unsigned int num_tcp_online;
164
165 Node_format tcp_relays[MAX_TCP_RELAYS_PEER];
166 uint16_t num_tcp_relays;
167 157
168 uint8_t maximum_speed_reached; 158 uint8_t maximum_speed_reached;
169 159
@@ -186,10 +176,9 @@ typedef struct {
186 176
187typedef struct { 177typedef struct {
188 DHT *dht; 178 DHT *dht;
179 TCP_Connections *tcp_c;
189 180
190 Crypto_Connection *crypto_connections; 181 Crypto_Connection *crypto_connections;
191 TCP_Client_Connection *tcp_connections_new[MAX_TCP_CONNECTIONS];
192 TCP_Client_Connection *tcp_connections[MAX_TCP_CONNECTIONS];
193 pthread_mutex_t tcp_mutex; 182 pthread_mutex_t tcp_mutex;
194 183
195 pthread_mutex_t connections_mutex; 184 pthread_mutex_t connections_mutex;
@@ -212,9 +201,6 @@ typedef struct {
212 201
213 BS_LIST ip_port_list; 202 BS_LIST ip_port_list;
214 203
215 int (*tcp_onion_callback)(void *object, const uint8_t *data, uint16_t length);
216 void *tcp_onion_callback_object;
217
218 TCP_Proxy_Info proxy_info; 204 TCP_Proxy_Info proxy_info;
219} Net_Crypto; 205} Net_Crypto;
220 206
@@ -364,11 +350,6 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
364 */ 350 */
365int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key); 351int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key);
366 352
367/* Set the function to be called when an onion response packet is received by one of the TCP connections.
368 */
369void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *object, const uint8_t *data,
370 uint16_t length), void *object);
371
372/* Return a random TCP connection number for use in send_tcp_onion_request. 353/* Return a random TCP connection number for use in send_tcp_onion_request.
373 * 354 *
374 * return TCP connection number on success. 355 * return TCP connection number on success.
@@ -389,7 +370,7 @@ int send_tcp_onion_request(Net_Crypto *c, unsigned int TCP_conn_number, const ui
389 * return number of relays copied to tcp_relays on success. 370 * return number of relays copied to tcp_relays on success.
390 * return 0 on failure. 371 * return 0 on failure.
391 */ 372 */
392unsigned int copy_connected_tcp_relays(const Net_Crypto *c, Node_format *tcp_relays, uint16_t num); 373unsigned int copy_connected_tcp_relays(Net_Crypto *c, Node_format *tcp_relays, uint16_t num);
393 374
394/* Kill a crypto connection. 375/* Kill a crypto connection.
395 * 376 *
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c
index a9fc1643..8ab62da2 100644
--- a/toxcore/onion_client.c
+++ b/toxcore/onion_client.c
@@ -1467,7 +1467,7 @@ Onion_Client *new_onion_client(Net_Crypto *c)
1467 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c); 1467 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, &handle_data_response, onion_c);
1468 oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, &handle_dhtpk_announce, onion_c); 1468 oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, &handle_dhtpk_announce, onion_c);
1469 cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, &handle_dht_dhtpk, onion_c); 1469 cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, &handle_dht_dhtpk, onion_c);
1470 tcp_onion_response_handler(onion_c->c, &handle_tcp_onion, onion_c); 1470 set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, &handle_tcp_onion, onion_c);
1471 1471
1472 return onion_c; 1472 return onion_c;
1473} 1473}
@@ -1483,7 +1483,7 @@ void kill_onion_client(Onion_Client *onion_c)
1483 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL); 1483 networking_registerhandler(onion_c->net, NET_PACKET_ONION_DATA_RESPONSE, NULL, NULL);
1484 oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL); 1484 oniondata_registerhandler(onion_c, ONION_DATA_DHTPK, NULL, NULL);
1485 cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL); 1485 cryptopacket_registerhandler(onion_c->dht, CRYPTO_PACKET_DHTPK, NULL, NULL);
1486 tcp_onion_response_handler(onion_c->c, NULL, NULL); 1486 set_onion_packet_tcp_connection_callback(onion_c->c->tcp_c, NULL, NULL);
1487 memset(onion_c, 0, sizeof(Onion_Client)); 1487 memset(onion_c, 0, sizeof(Onion_Client));
1488 free(onion_c); 1488 free(onion_c);
1489} 1489}