summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-08-16 22:09:29 -0400
committerirungentoo <irungentoo@gmail.com>2014-08-16 22:09:29 -0400
commit9134048bb59323c02f973f25f2ad4e327830193c (patch)
treebca53c133aabf0599cd31575e960c96d613d9be8
parent162a90066020a6086d1f5fc9f54d288b99892462 (diff)
Some thread safety related fixes to TCP connections in net_crypto.
Added a recursive mutex to fix possible thread issues when the A/V thread sends data at the same time as the main thread.
-rw-r--r--toxcore/net_crypto.c91
-rw-r--r--toxcore/net_crypto.h7
2 files changed, 68 insertions, 30 deletions
diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c
index 5b89f69c..af255f07 100644
--- a/toxcore/net_crypto.c
+++ b/toxcore/net_crypto.c
@@ -385,7 +385,7 @@ static Crypto_Connection *get_crypto_connection(const Net_Crypto *c, int crypt_c
385 * return -1 on failure. 385 * return -1 on failure.
386 * return 0 on success. 386 * return 0 on success.
387 */ 387 */
388static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) 388static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
389{ 389{
390//TODO TCP, etc... 390//TODO TCP, etc...
391 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 391 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -417,17 +417,35 @@ static int send_packet_to(const Net_Crypto *c, int crypt_connection_id, const ui
417 unsigned int r = crypt_connection_id; 417 unsigned int r = crypt_connection_id;
418 418
419 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 419 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
420 pthread_mutex_lock(&c->tcp_mutex);
421
422 int ret = 0;
423
420 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */ 424 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_ONLINE) {/* friend is connected to this relay. */
421 if (send_data(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->con_number_tcp[(i + r) % MAX_TCP_CONNECTIONS], 425 ret = send_data(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->con_number_tcp[(i + r) % MAX_TCP_CONNECTIONS],
422 data, length) == 1) 426 data, length);
423 return 0; 427 }
428
429 pthread_mutex_unlock(&c->tcp_mutex);
430
431 if (ret == 1) {
432 return 0;
424 } 433 }
425 } 434 }
426 435
427 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 436 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
437 pthread_mutex_lock(&c->tcp_mutex);
438
439 int ret = 0;
440
428 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_INVISIBLE) { 441 if (conn->status_tcp[(i + r) % MAX_TCP_CONNECTIONS] == STATUS_TCP_INVISIBLE) {
429 if (send_oob_packet(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->dht_public_key, data, length) == 1) 442 ret = send_oob_packet(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], conn->dht_public_key, data, length);
430 return 0; 443 }
444
445 pthread_mutex_unlock(&c->tcp_mutex);
446
447 if (ret == 1) {
448 return 0;
431 } 449 }
432 } 450 }
433 451
@@ -706,7 +724,7 @@ static int handle_request_packet(Packets_Array *send_array, const uint8_t *data,
706 * return -1 on failure. 724 * return -1 on failure.
707 * return 0 on success. 725 * return 0 on success.
708 */ 726 */
709static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length) 727static int send_data_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint16_t length)
710{ 728{
711 if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE) 729 if (length == 0 || length + (1 + sizeof(uint16_t) + crypto_box_MACBYTES) > MAX_CRYPTO_PACKET_SIZE)
712 return -1; 730 return -1;
@@ -738,7 +756,7 @@ static int send_data_packet(const Net_Crypto *c, int crypt_connection_id, const
738 * return -1 on failure. 756 * return -1 on failure.
739 * return 0 on success. 757 * return 0 on success.
740 */ 758 */
741static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num, 759static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint32_t buffer_start, uint32_t num,
742 const uint8_t *data, uint32_t length) 760 const uint8_t *data, uint32_t length)
743{ 761{
744 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 762 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
@@ -759,7 +777,7 @@ static int send_data_packet_helper(const Net_Crypto *c, int crypt_connection_id,
759/* return -1 if data could not be put in packet queue. 777/* return -1 if data could not be put in packet queue.
760 * return positive packet number if data was put into the queue. 778 * return positive packet number if data was put into the queue.
761 */ 779 */
762static int64_t send_lossless_packet(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) 780static int64_t send_lossless_packet(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
763{ 781{
764 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE) 782 if (length == 0 || length > MAX_CRYPTO_DATA_SIZE)
765 return -1; 783 return -1;
@@ -868,7 +886,7 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint
868 * return -1 on failure. 886 * return -1 on failure.
869 * return 0 on success. 887 * return 0 on success.
870 */ 888 */
871static int send_request_packet(const Net_Crypto *c, int crypt_connection_id) 889static int send_request_packet(Net_Crypto *c, int crypt_connection_id)
872{ 890{
873 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 891 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
874 892
@@ -890,7 +908,7 @@ static int send_request_packet(const Net_Crypto *c, int crypt_connection_id)
890 * return -1 on failure. 908 * return -1 on failure.
891 * return number of packets sent on success. 909 * return number of packets sent on success.
892 */ 910 */
893static int send_requested_packets(const Net_Crypto *c, int crypt_connection_id, uint16_t max_num) 911static int send_requested_packets(Net_Crypto *c, int crypt_connection_id, uint16_t max_num)
894{ 912{
895 if (max_num == 0) 913 if (max_num == 0)
896 return -1; 914 return -1;
@@ -990,7 +1008,7 @@ static int clear_temp_packet(const Net_Crypto *c, int crypt_connection_id)
990 * return -1 on failure. 1008 * return -1 on failure.
991 * return 0 on success. 1009 * return 0 on success.
992 */ 1010 */
993static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id) 1011static int send_temp_packet(Net_Crypto *c, int crypt_connection_id)
994{ 1012{
995 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1013 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
996 1014
@@ -1014,7 +1032,7 @@ static int send_temp_packet(const Net_Crypto *c, int crypt_connection_id)
1014 * return -1 on failure. 1032 * return -1 on failure.
1015 * return 0 on success. 1033 * return 0 on success.
1016 */ 1034 */
1017static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie, 1035static int create_send_handshake(Net_Crypto *c, int crypt_connection_id, const uint8_t *cookie,
1018 const uint8_t *dht_public_key) 1036 const uint8_t *dht_public_key)
1019{ 1037{
1020 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1038 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -1040,7 +1058,7 @@ static int create_send_handshake(const Net_Crypto *c, int crypt_connection_id, c
1040 * return -1 on failure. 1058 * return -1 on failure.
1041 * return 0 on success. 1059 * return 0 on success.
1042 */ 1060 */
1043static int send_kill_packet(const Net_Crypto *c, int crypt_connection_id) 1061static int send_kill_packet(Net_Crypto *c, int crypt_connection_id)
1044{ 1062{
1045 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1063 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1046 1064
@@ -1531,7 +1549,7 @@ int new_crypto_connection(Net_Crypto *c, const uint8_t *real_public_key)
1531 * return -1 on failure. 1549 * return -1 on failure.
1532 * return 0 on success. 1550 * return 0 on success.
1533 */ 1551 */
1534static int disconnect_peer_tcp(const Net_Crypto *c, int crypt_connection_id) 1552static int disconnect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1535{ 1553{
1536 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1554 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1537 1555
@@ -1542,9 +1560,11 @@ static int disconnect_peer_tcp(const Net_Crypto *c, int crypt_connection_id)
1542 1560
1543 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 1561 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1544 if (conn->status_tcp[i] != STATUS_TCP_NULL) { 1562 if (conn->status_tcp[i] != STATUS_TCP_NULL) {
1563 pthread_mutex_lock(&c->tcp_mutex);
1545 send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]); 1564 send_disconnect_request(c->tcp_connections[i], conn->con_number_tcp[i]);
1546 conn->status_tcp[i] = STATUS_TCP_NULL; 1565 conn->status_tcp[i] = STATUS_TCP_NULL;
1547 conn->con_number_tcp[i] = 0; 1566 conn->con_number_tcp[i] = 0;
1567 pthread_mutex_unlock(&c->tcp_mutex);
1548 } 1568 }
1549 } 1569 }
1550 1570
@@ -1556,7 +1576,7 @@ static int disconnect_peer_tcp(const Net_Crypto *c, int crypt_connection_id)
1556 * return -1 on failure. 1576 * return -1 on failure.
1557 * return 0 on success. 1577 * return 0 on success.
1558 */ 1578 */
1559static int connect_peer_tcp(const Net_Crypto *c, int crypt_connection_id) 1579static int connect_peer_tcp(Net_Crypto *c, int crypt_connection_id)
1560{ 1580{
1561 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1581 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
1562 1582
@@ -1569,8 +1589,10 @@ static int connect_peer_tcp(const Net_Crypto *c, int crypt_connection_id)
1569 if (c->tcp_connections[i] == NULL) 1589 if (c->tcp_connections[i] == NULL)
1570 continue; 1590 continue;
1571 1591
1592 pthread_mutex_lock(&c->tcp_mutex);
1572 //TODO check function return? 1593 //TODO check function return?
1573 send_routing_request(c->tcp_connections[i], conn->dht_public_key); 1594 send_routing_request(c->tcp_connections[i], conn->dht_public_key);
1595 pthread_mutex_unlock(&c->tcp_mutex);
1574 } 1596 }
1575 1597
1576 return 0; 1598 return 0;
@@ -1603,7 +1625,7 @@ uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, ui
1603 * return -1 on failure. 1625 * return -1 on failure.
1604 * return 0 on success. 1626 * return 0 on success.
1605 */ 1627 */
1606int set_connection_dht_public_key(const Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, 1628int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key,
1607 uint64_t timestamp) 1629 uint64_t timestamp)
1608{ 1630{
1609 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id); 1631 Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
@@ -1936,13 +1958,17 @@ int add_tcp_relay(Net_Crypto *c, IP_Port ip_port, const uint8_t *public_key)
1936 * return 0 on success. 1958 * return 0 on success.
1937 * return -1 on failure. 1959 * return -1 on failure.
1938 */ 1960 */
1939int send_tcp_onion_request(const Net_Crypto *c, const uint8_t *data, uint16_t length) 1961int send_tcp_onion_request(Net_Crypto *c, const uint8_t *data, uint16_t length)
1940{ 1962{
1941 unsigned int i, r = rand(); 1963 unsigned int i, r = rand();
1942 1964
1943 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) { 1965 for (i = 0; i < MAX_TCP_CONNECTIONS; ++i) {
1944 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) { 1966 if (c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS]) {
1945 if (send_onion_request(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], data, length) == 1) 1967 pthread_mutex_lock(&c->tcp_mutex);
1968 int ret = send_onion_request(c->tcp_connections[(i + r) % MAX_TCP_CONNECTIONS], data, length);
1969 pthread_mutex_unlock(&c->tcp_mutex);
1970
1971 if (ret == 1)
1946 return 0; 1972 return 0;
1947 } 1973 }
1948 } 1974 }
@@ -2053,7 +2079,11 @@ static void do_tcp(Net_Crypto *c)
2053 do_TCP_connection(c->tcp_connections_new[i]); 2079 do_TCP_connection(c->tcp_connections_new[i]);
2054 2080
2055 if (c->tcp_connections_new[i]->status == TCP_CLIENT_CONFIRMED) { 2081 if (c->tcp_connections_new[i]->status == TCP_CLIENT_CONFIRMED) {
2056 if (add_tcp_connected(c, c->tcp_connections_new[i]) == 0) { 2082 pthread_mutex_lock(&c->tcp_mutex);
2083 int ret = add_tcp_connected(c, c->tcp_connections_new[i]);
2084 pthread_mutex_unlock(&c->tcp_mutex);
2085
2086 if (ret == 0) {
2057 c->tcp_connections_new[i] = NULL; 2087 c->tcp_connections_new[i] = NULL;
2058 } else { 2088 } else {
2059 kill_TCP_connection(c->tcp_connections_new[i]); 2089 kill_TCP_connection(c->tcp_connections_new[i]);
@@ -2066,7 +2096,9 @@ static void do_tcp(Net_Crypto *c)
2066 if (c->tcp_connections[i] == NULL) 2096 if (c->tcp_connections[i] == NULL)
2067 continue; 2097 continue;
2068 2098
2099 pthread_mutex_lock(&c->tcp_mutex);
2069 do_TCP_connection(c->tcp_connections[i]); 2100 do_TCP_connection(c->tcp_connections[i]);
2101 pthread_mutex_unlock(&c->tcp_mutex);
2070 } 2102 }
2071} 2103}
2072 2104
@@ -2106,19 +2138,23 @@ static void clear_disconnected_tcp(Net_Crypto *c)
2106 if (tcp_con->status != TCP_CLIENT_DISCONNECTED) 2138 if (tcp_con->status != TCP_CLIENT_DISCONNECTED)
2107 continue; 2139 continue;
2108 2140
2109 c->tcp_connections[i] = NULL;
2110 /* Try reconnecting to relay on disconnect. */ 2141 /* Try reconnecting to relay on disconnect. */
2111 add_tcp_relay(c, tcp_con->ip_port, tcp_con->public_key); 2142 add_tcp_relay(c, tcp_con->ip_port, tcp_con->public_key);
2143
2144 pthread_mutex_lock(&c->tcp_mutex);
2145 c->tcp_connections[i] = NULL;
2112 kill_TCP_connection(tcp_con); 2146 kill_TCP_connection(tcp_con);
2113 2147
2114 for (j = 0; j < c->crypto_connections_length; ++j) { 2148 for (j = 0; j < c->crypto_connections_length; ++j) {
2115 Crypto_Connection *conn = get_crypto_connection(c, j); 2149 Crypto_Connection *conn = get_crypto_connection(c, j);
2116 2150
2117 if (conn == 0) 2151 if (conn == 0)
2118 return; 2152 continue;
2119 2153
2120 clear_disconnected_tcp_peer(conn, i); 2154 clear_disconnected_tcp_peer(conn, i);
2121 } 2155 }
2156
2157 pthread_mutex_unlock(&c->tcp_mutex);
2122 } 2158 }
2123} 2159}
2124 2160
@@ -2240,9 +2276,6 @@ static int udp_handle_packet(void *object, IP_Port source, const uint8_t *packet
2240 return 0; 2276 return 0;
2241} 2277}
2242 2278
2243/* Value to set sending variable */
2244#define CONN_SENDING_VALUE 2
2245
2246/* The dT for the average packet recieving rate calculations. 2279/* The dT for the average packet recieving rate calculations.
2247 Also used as the */ 2280 Also used as the */
2248#define PACKET_COUNTER_AVERAGE_INTERVAL 100 2281#define PACKET_COUNTER_AVERAGE_INTERVAL 100
@@ -2306,7 +2339,6 @@ static void send_crypto_packets(Net_Crypto *c)
2306 2339
2307 /* conjestion control 2340 /* conjestion control
2308 calculate a new value of conn->packet_send_rate based on some data 2341 calculate a new value of conn->packet_send_rate based on some data
2309 notes: needs improvement but seems to work fine for packet loss <1%
2310 */ 2342 */
2311 2343
2312 unsigned int pos = conn->last_sendqueue_counter % CONGESTION_QUEUE_ARRAY_SIZE; 2344 unsigned int pos = conn->last_sendqueue_counter % CONGESTION_QUEUE_ARRAY_SIZE;
@@ -2409,7 +2441,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
2409 * 2441 *
2410 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 2442 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
2411 */ 2443 */
2412int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length) 2444int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length)
2413{ 2445{
2414 if (length == 0) 2446 if (length == 0)
2415 return -1; 2447 return -1;
@@ -2583,6 +2615,10 @@ Net_Crypto *new_net_crypto(DHT *dht, TCP_Proxy_Info *proxy_info)
2583 2615
2584 bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8); 2616 bs_list_init(&temp->ip_port_list, sizeof(IP_Port), 8);
2585 2617
2618 pthread_mutexattr_t attr;
2619 pthread_mutexattr_init(&attr);
2620 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
2621 pthread_mutex_init(&temp->tcp_mutex, &attr);
2586 pthread_mutex_init(&temp->connections_mutex, NULL); 2622 pthread_mutex_init(&temp->connections_mutex, NULL);
2587 2623
2588 if (proxy_info) { 2624 if (proxy_info) {
@@ -2663,6 +2699,7 @@ void kill_net_crypto(Net_Crypto *c)
2663 kill_TCP_connection(c->tcp_connections[i]); 2699 kill_TCP_connection(c->tcp_connections[i]);
2664 } 2700 }
2665 2701
2702 pthread_mutex_destroy(&c->tcp_mutex);
2666 pthread_mutex_destroy(&c->connections_mutex); 2703 pthread_mutex_destroy(&c->connections_mutex);
2667 2704
2668 bs_list_free(&c->ip_port_list); 2705 bs_list_free(&c->ip_port_list);
diff --git a/toxcore/net_crypto.h b/toxcore/net_crypto.h
index b931d1db..17636ebc 100644
--- a/toxcore/net_crypto.h
+++ b/toxcore/net_crypto.h
@@ -179,6 +179,7 @@ typedef struct {
179 Crypto_Connection *crypto_connections; 179 Crypto_Connection *crypto_connections;
180 TCP_Client_Connection *tcp_connections_new[MAX_TCP_CONNECTIONS]; 180 TCP_Client_Connection *tcp_connections_new[MAX_TCP_CONNECTIONS];
181 TCP_Client_Connection *tcp_connections[MAX_TCP_CONNECTIONS]; 181 TCP_Client_Connection *tcp_connections[MAX_TCP_CONNECTIONS];
182 pthread_mutex_t tcp_mutex;
182 183
183 pthread_mutex_t connections_mutex; 184 pthread_mutex_t connections_mutex;
184 unsigned int connection_use_counter; 185 unsigned int connection_use_counter;
@@ -246,7 +247,7 @@ uint64_t get_connection_dht_key(const Net_Crypto *c, int crypt_connection_id, ui
246 * return -1 on failure. 247 * return -1 on failure.
247 * return 0 on success. 248 * return 0 on success.
248 */ 249 */
249int set_connection_dht_public_key(const Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key, 250int set_connection_dht_public_key(Net_Crypto *c, int crypt_connection_id, const uint8_t *dht_public_key,
250 uint64_t timestamp); 251 uint64_t timestamp);
251 252
252/* Set the direct ip of the crypto connection. 253/* Set the direct ip of the crypto connection.
@@ -305,7 +306,7 @@ uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connecti
305 * 306 *
306 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range. 307 * The first byte of data must be in the CRYPTO_RESERVED_PACKETS to PACKET_ID_LOSSY_RANGE_START range.
307 */ 308 */
308int64_t write_cryptpacket(const Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length); 309int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t *data, uint32_t length);
309 310
310/* return -1 on failure. 311/* return -1 on failure.
311 * return 0 on success. 312 * return 0 on success.
@@ -338,7 +339,7 @@ void tcp_onion_response_handler(Net_Crypto *c, int (*tcp_onion_callback)(void *o
338 * return 0 on success. 339 * return 0 on success.
339 * return -1 on failure. 340 * return -1 on failure.
340 */ 341 */
341int send_tcp_onion_request(const Net_Crypto *c, const uint8_t *data, uint16_t length); 342int send_tcp_onion_request(Net_Crypto *c, const uint8_t *data, uint16_t length);
342 343
343/* Copy a maximum of num TCP relays we are connected to to tcp_relays. 344/* Copy a maximum of num TCP relays we are connected to to tcp_relays.
344 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6. 345 * NOTE that the family of the copied ip ports will be set to TCP_INET or TCP_INET6.