diff options
-rw-r--r-- | toxcore/Messenger.c | 20 | ||||
-rw-r--r-- | toxcore/TCP_client.c | 3 | ||||
-rw-r--r-- | toxcore/net_crypto.c | 91 | ||||
-rw-r--r-- | toxcore/net_crypto.h | 20 | ||||
-rw-r--r-- | toxcore/onion_client.c | 1 |
5 files changed, 115 insertions, 20 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b0d5a2e5..472c2ae4 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2203,17 +2203,15 @@ void do_friends(Messenger *m) | |||
2203 | } | 2203 | } |
2204 | 2204 | ||
2205 | if (m->friendlist[i].crypt_connection_id != -1) { | 2205 | if (m->friendlist[i].crypt_connection_id != -1) { |
2206 | uint8_t dht_public_key[crypto_box_PUBLICKEYBYTES]; | 2206 | uint8_t dht_public_key1[crypto_box_PUBLICKEYBYTES]; |
2207 | uint64_t timestamp = onion_getfriend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key); | 2207 | uint64_t timestamp1 = onion_getfriend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key1); |
2208 | 2208 | uint8_t dht_public_key2[crypto_box_PUBLICKEYBYTES]; | |
2209 | if (timestamp != 0) { | 2209 | uint64_t timestamp2 = get_connection_dht_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key2); |
2210 | set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key, timestamp); | 2210 | |
2211 | } | 2211 | if (timestamp1 > timestamp2) { |
2212 | 2212 | set_connection_dht_public_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key1, timestamp1); | |
2213 | timestamp = get_connection_dht_key(m->net_crypto, m->friendlist[i].crypt_connection_id, dht_public_key); | 2213 | } else if (timestamp1 < timestamp2) { |
2214 | 2214 | onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key2, timestamp2); | |
2215 | if (timestamp != 0) { | ||
2216 | onion_set_friend_DHT_pubkey(m->onion_c, m->friendlist[i].onion_friendnum, dht_public_key, timestamp); | ||
2217 | } | 2215 | } |
2218 | 2216 | ||
2219 | uint8_t direct_connected; | 2217 | uint8_t direct_connected; |
diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 9091900f..5abb7232 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c | |||
@@ -604,6 +604,9 @@ void do_TCP_connection(TCP_Client_Connection *TCP_connection) | |||
604 | */ | 604 | */ |
605 | void kill_TCP_connection(TCP_Client_Connection *TCP_connection) | 605 | void kill_TCP_connection(TCP_Client_Connection *TCP_connection) |
606 | { | 606 | { |
607 | if (TCP_connection == NULL) | ||
608 | return; | ||
609 | |||
607 | kill_sock(TCP_connection->sock); | 610 | kill_sock(TCP_connection->sock); |
608 | memset(TCP_connection, 0, sizeof(TCP_Client_Connection)); | 611 | memset(TCP_connection, 0, sizeof(TCP_Client_Connection)); |
609 | free(TCP_connection); | 612 | free(TCP_connection); |
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); |
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h index e41e7e1d..14b14229 100644 --- a/toxcore/net_crypto.h +++ b/toxcore/net_crypto.h | |||
@@ -61,7 +61,13 @@ | |||
61 | 61 | ||
62 | #define CRYPTO_RESERVED_PACKETS 16 | 62 | #define CRYPTO_RESERVED_PACKETS 16 |
63 | 63 | ||
64 | #define MAX_TCP_CONNECTIONS 8 | 64 | #define MAX_TCP_CONNECTIONS 32 |
65 | #define MAX_TCP_RELAYS_PEER 4 | ||
66 | |||
67 | #define STATUS_TCP_NULL 0 | ||
68 | #define STATUS_TCP_OFFLINE 1 | ||
69 | #define STATUS_TCP_INVISIBLE 2 /* we know the other peer is connected to this relay but he isn't appearing online */ | ||
70 | #define STATUS_TCP_ONLINE 3 | ||
65 | 71 | ||
66 | typedef struct { | 72 | typedef struct { |
67 | uint64_t time; | 73 | uint64_t time; |
@@ -127,8 +133,11 @@ typedef struct { | |||
127 | 133 | ||
128 | uint8_t killed; /* set to 1 to kill the connection. */ | 134 | uint8_t killed; /* set to 1 to kill the connection. */ |
129 | 135 | ||
130 | uint8_t status_tcp[MAX_TCP_CONNECTIONS]; | 136 | uint8_t status_tcp[MAX_TCP_CONNECTIONS]; /* set to one of STATUS_TCP_* */ |
131 | uint8_t con_number_tcp[MAX_TCP_CONNECTIONS]; | 137 | uint8_t con_number_tcp[MAX_TCP_CONNECTIONS]; |
138 | |||
139 | Node_format tcp_relays[MAX_TCP_RELAYS_PEER]; | ||
140 | uint16_t num_tcp_relays; | ||
132 | } Crypto_Connection; | 141 | } Crypto_Connection; |
133 | 142 | ||
134 | typedef struct { | 143 | typedef struct { |
@@ -244,6 +253,13 @@ uint32_t crypto_num_free_sendqueue_slots(Net_Crypto *c, int crypt_connection_id) | |||
244 | */ | 253 | */ |
245 | int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); | 254 | int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, uint8_t *data, uint32_t length); |
246 | 255 | ||
256 | /* Add a tcp relay, associating it to a crypt_connection_id. | ||
257 | * | ||
258 | * return 0 if it was added. | ||
259 | * return -1 if it wasn't. | ||
260 | */ | ||
261 | int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, uint8_t *public_key); | ||
262 | |||
247 | /* Add a tcp relay to the array. | 263 | /* Add a tcp relay to the array. |
248 | * | 264 | * |
249 | * return 0 if it was added. | 265 | * return 0 if it was added. |
diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index be4d12aa..7fab76a7 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c | |||
@@ -830,6 +830,7 @@ int onion_set_friend_DHT_pubkey(Onion_Client *onion_c, int friend_num, uint8_t * | |||
830 | return -1; | 830 | return -1; |
831 | } | 831 | } |
832 | 832 | ||
833 | onion_c->friends_list[friend_num].last_seen = unix_time(); | ||
833 | onion_c->friends_list[friend_num].is_fake_clientid = 1; | 834 | onion_c->friends_list[friend_num].is_fake_clientid = 1; |
834 | onion_c->friends_list[friend_num].fake_client_id_timestamp = timestamp; | 835 | onion_c->friends_list[friend_num].fake_client_id_timestamp = timestamp; |
835 | memcpy(onion_c->friends_list[friend_num].fake_client_id, dht_key, crypto_box_PUBLICKEYBYTES); | 836 | memcpy(onion_c->friends_list[friend_num].fake_client_id, dht_key, crypto_box_PUBLICKEYBYTES); |