summaryrefslogtreecommitdiff
path: root/toxcore/net_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/net_crypto.c')
-rw-r--r--toxcore/net_crypto.c70
1 files changed, 50 insertions, 20 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index be2f967d..a1286c5d 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -421,37 +421,48 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
421 //TODO: detect and kill bad relays. 421 //TODO: detect and kill bad relays.
422 uint32_t i; 422 uint32_t i;
423 423
424 unsigned int r = crypt_connection_id; 424 unsigned int r;
425 425
426 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 426 if (!conn->last_relay_sentto) {
427 pthread_mutex_lock(&c->tcp_mutex); 427 r = rand();
428 } else {
429 r = conn->last_relay_sentto - 1;
430 }
428 431
429 int ret = 0; 432 if (conn->num_tcp_online) {
433 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
434 pthread_mutex_lock(&c->tcp_mutex);
430 435
431 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */ 436 unsigned int tcp_index = (i + r) % MAX_TCP_CONNECTIONS;
432 ret = send_data(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->con_number_tcp[(i + r) % MAX_TCP_CONNECTIONS], 437 int ret = 0;
433 data, length);
434 }
435 438
436 pthread_mutex_unlock(&c->tcp_mutex); 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 }
437 442
438 if (ret == 1) { 443 pthread_mutex_unlock(&c->tcp_mutex);
439 return 0; 444
445 if (ret == 1) {
446 conn->last_relay_sentto = tcp_index + 1;
447 return 0;
448 }
440 } 449 }
441 } 450 }
442 451
443 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 452 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
444 pthread_mutex_lock(&c->tcp_mutex); 453 pthread_mutex_lock(&c->tcp_mutex);
445 454
455 unsigned int tcp_index = (i + r) % MAX_TCP_CONNECTIONS;
446 int ret = 0; 456 int ret = 0;
447 457
448 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_INVISIBLE) { 458 if (conn->status_tcp[tcp_index] == STATUS_TCP_INVISIBLE) {
449 ret = send_oob_packet(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->dht_public_key, data, length); 459 ret = send_oob_packet(c->tcp_connections[tcp_index], conn->dht_public_key, data, length);
450 } 460 }
451 461
452 pthread_mutex_unlock(&c->tcp_mutex); 462 pthread_mutex_unlock(&c->tcp_mutex);
453 463
454 if (ret == 1) { 464 if (ret == 1) {
465 conn->last_relay_sentto = tcp_index + 1;
455 return 0; 466 return 0;
456 } 467 }
457 } 468 }
@@ -1595,6 +1606,25 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key)
1595 return crypt_connection_id; 1606 return crypt_connection_id;
1596} 1607}
1597 1608
1609/* Set the status for the TCP connection for conn in location to status.
1610 */
1611static void set_conn_tcp_status(Crypto_Connection *conn, unsigned int location, unsigned int status)
1612{
1613 if (conn->status_tcp[location] == status) {
1614 return;
1615 }
1616
1617 if (conn->status_tcp[location] == STATUS_TCP_ONLINE) {
1618 --conn->num_tcp_online;
1619 }
1620
1621 if (status == STATUS_TCP_ONLINE) {
1622 ++conn->num_tcp_online;
1623 }
1624
1625 conn->status_tcp[location] = status;
1626}
1627
1598/* Disconnect peer from all associated TCP connections. 1628/* Disconnect peer from all associated TCP connections.
1599 * 1629 *
1600 * return -1 on failure. 1630 * return -1 on failure.
@@ -1613,7 +1643,7 @@ static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1613 if (conn->status_tcp[i] != STATUS_TCP_NULL) { 1643 if (conn->status_tcp[i] != STATUS_TCP_NULL) {
1614 pthread_mutex_lock(&c->tcp_mutex); 1644 pthread_mutex_lock(&c->tcp_mutex);
1615 send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]); 1645 send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]);
1616 conn->status_tcp[i] = STATUS_TCP_NULL; 1646 set_conn_tcp_status(conn, i, STATUS_TCP_NULL);
1617 conn->con_number_tcp[i] = 0; 1647 conn->con_number_tcp[i] = 0;
1618 pthread_mutex_unlock(&c->tcp_mutex); 1648 pthread_mutex_unlock(&c->tcp_mutex);
1619 } 1649 }
@@ -1769,12 +1799,12 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint
1769 1799
1770 for (i = 0; i < conn->num_tcp_relays; ++i) { 1800 for (i = 0; i < conn->num_tcp_relays; ++i) {
1771 if (memcmp(TCP_con->public_key, conn->tcp_relays[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) { 1801 if (memcmp(TCP_con->public_key, conn->tcp_relays[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) {
1772 conn->status_tcp[location] = STATUS_TCP_INVISIBLE; 1802 set_conn_tcp_status(conn, location, STATUS_TCP_INVISIBLE);
1773 return 0; 1803 return 0;
1774 } 1804 }
1775 } 1805 }
1776 1806
1777 conn->status_tcp[location] = STATUS_TCP_OFFLINE; 1807 set_conn_tcp_status(conn, location, STATUS_TCP_OFFLINE);
1778 return 0; 1808 return 0;
1779} 1809}
1780 1810
@@ -1797,9 +1827,9 @@ static int tcp_status_callback(void *object, uint32_t number, uint8_t connection
1797 return -1; 1827 return -1;
1798 1828
1799 if (status == 1) { 1829 if (status == 1) {
1800 conn->status_tcp[location] = STATUS_TCP_OFFLINE; 1830 set_conn_tcp_status(conn, location, STATUS_TCP_OFFLINE);
1801 } else if (status == 2) { 1831 } else if (status == 2) {
1802 conn->status_tcp[location] = STATUS_TCP_ONLINE; 1832 set_conn_tcp_status(conn, location, STATUS_TCP_ONLINE);
1803 } 1833 }
1804 1834
1805 conn->con_number_tcp[location] = connection_id; 1835 conn->con_number_tcp[location] = connection_id;
@@ -1969,7 +1999,7 @@ int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port,
1969 1999
1970 if (memcmp(c->tcp_connections[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) { 2000 if (memcmp(c->tcp_connections[i]->public_key, public_key, crypto_box_PUBLICKEYBYTES) == 0) {
1971 if (conn->status_tcp[i] == STATUS_TCP_OFFLINE) 2001 if (conn->status_tcp[i] == STATUS_TCP_OFFLINE)
1972 conn->status_tcp[i] = STATUS_TCP_INVISIBLE; 2002 set_conn_tcp_status(conn, i, STATUS_TCP_INVISIBLE);
1973 } 2003 }
1974 } 2004 }
1975 2005
@@ -2173,7 +2203,7 @@ static void clear_disconnected_tcp_peer(Crypto_Connection *conn, uint32_t number
2173 if (number >= MAX_TCP_CONNECTIONS) 2203 if (number >= MAX_TCP_CONNECTIONS)
2174 return; 2204 return;
2175 2205
2176 conn->status_tcp[number] = STATUS_TCP_NULL; 2206 set_conn_tcp_status(conn, number, STATUS_TCP_NULL);
2177 conn->con_number_tcp[number] = 0; 2207 conn->con_number_tcp[number] = 0;
2178} 2208}
2179 2209