Skip to content

Commit ab88700

Browse files
committed
fix: Allow TCP connections to fail connect calls.
1 parent 7603170 commit ab88700

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

auto_tests/TCP_test.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../toxcore/TCP_server.h"
99
#include "../toxcore/crypto_core.h"
1010
#include "../toxcore/mono_time.h"
11+
#include "../toxcore/network.h"
1112
#include "auto_test_support.h"
1213

1314
#define NUM_PORTS 3
@@ -73,8 +74,9 @@ static void test_basic(void)
7374
for (uint8_t i = 0; i < NUM_PORTS; i++) {
7475
sock = net_socket(ns, net_family_ipv6(), TOX_SOCK_STREAM, TOX_PROTO_TCP);
7576
localhost.port = net_htons(ports[i]);
76-
bool ret = net_connect(ns, mem, logger, sock, &localhost);
77-
ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d).", ports[i], errno);
77+
Net_Err_Connect err;
78+
bool ret = net_connect(ns, mem, logger, sock, &localhost, &err);
79+
ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d, %s).", ports[i], errno, net_err_connect_to_string(err));
7880

7981
// Leave open one connection for the next test.
8082
if (i + 1 < NUM_PORTS) {
@@ -218,8 +220,9 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem,
218220
localhost.ip = get_loopback();
219221
localhost.port = net_htons(ports[random_u32(rng) % NUM_PORTS]);
220222

221-
bool ok = net_connect(ns, mem, logger, sock, &localhost);
222-
ck_assert_msg(ok, "Failed to connect to the test TCP relay server.");
223+
Net_Err_Connect err;
224+
bool ok = net_connect(ns, mem, logger, sock, &localhost, &err);
225+
ck_assert_msg(ok, "Failed to connect to the test TCP relay server: %s.", net_err_connect_to_string(err));
223226

224227
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
225228
crypto_new_keypair(rng, sec_c->public_key, f_secret_key);

toxcore/TCP_client.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,23 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value)
109109
non_null()
110110
static bool connect_sock_to(const Network *ns, const Logger *logger, const Memory *mem, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info)
111111
{
112+
Net_Err_Connect err;
112113
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
113-
return net_connect(ns, mem, logger, sock, &proxy_info->ip_port);
114+
net_connect(ns, mem, logger, sock, &proxy_info->ip_port, &err);
114115
} else {
115-
return net_connect(ns, mem, logger, sock, ip_port);
116+
net_connect(ns, mem, logger, sock, ip_port, &err);
116117
}
118+
switch (err) {
119+
case NET_ERR_CONNECT_OK:
120+
case NET_ERR_CONNECT_FAILED: {
121+
/* nonblocking socket, connect will never return success */
122+
return true;
123+
}
124+
case NET_ERR_CONNECT_INVALID_FAMILY:
125+
return false;
126+
}
127+
LOGGER_ERROR(logger, "unexpected error code %s from net_connect", net_err_connect_to_string(err));
128+
return false;
117129
}
118130

119131
/**

toxcore/network.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2015,7 +2015,21 @@ bool addr_resolve_or_parse_ip(const Network *ns, const Memory *mem, const char *
20152015
return addr_parse_ip(address, to);
20162016
}
20172017

2018-
bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port)
2018+
const char *net_err_connect_to_string(Net_Err_Connect err)
2019+
{
2020+
switch (err) {
2021+
case NET_ERR_CONNECT_OK:
2022+
return "NET_ERR_CONNECT_OK";
2023+
case NET_ERR_CONNECT_INVALID_FAMILY:
2024+
return "NET_ERR_CONNECT_INVALID_FAMILY";
2025+
case NET_ERR_CONNECT_FAILED:
2026+
return "NET_ERR_CONNECT_FAILED";
2027+
}
2028+
2029+
return "<invalid Net_Err_Connect>";
2030+
}
2031+
2032+
bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port, Net_Err_Connect *err)
20192033
{
20202034
Network_Addr addr = {{0}};
20212035

@@ -2037,11 +2051,13 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket
20372051
Ip_Ntoa ip_str;
20382052
LOGGER_ERROR(log, "cannot connect to %s:%d which is neither IPv4 nor IPv6",
20392053
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port));
2054+
*err = NET_ERR_CONNECT_INVALID_FAMILY;
20402055
return false;
20412056
}
20422057

20432058
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
20442059
if ((true)) {
2060+
*err = NET_ERR_CONNECT_OK;
20452061
return true;
20462062
}
20472063
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
@@ -2060,10 +2076,12 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket
20602076
LOGGER_WARNING(log, "failed to connect to %s:%d: %d (%s)",
20612077
net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror);
20622078
net_kill_strerror(net_strerror);
2079+
*err = NET_ERR_CONNECT_FAILED;
20632080
return false;
20642081
}
20652082
}
20662083

2084+
*err = NET_ERR_CONNECT_OK;
20672085
return true;
20682086
}
20692087

toxcore/network.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,22 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl
499499
non_null(1) nullable(2)
500500
void networking_poll(const Networking_Core *net, void *userdata);
501501

502+
typedef enum Net_Err_Connect {
503+
NET_ERR_CONNECT_OK,
504+
NET_ERR_CONNECT_INVALID_FAMILY,
505+
NET_ERR_CONNECT_FAILED,
506+
} Net_Err_Connect;
507+
508+
const char *net_err_connect_to_string(Net_Err_Connect err);
509+
502510
/** @brief Connect a socket to the address specified by the ip_port.
503511
*
504-
* Return true on success.
505-
* Return false on failure.
512+
* @param[out] err Set to NET_ERR_CONNECT_OK on success, otherwise an error code.
513+
*
514+
* @retval true on success, false on failure.
506515
*/
507516
non_null()
508-
bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port);
517+
bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port, Net_Err_Connect *err);
509518

510519
/** @brief High-level getaddrinfo implementation.
511520
*

0 commit comments

Comments
 (0)