mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
cleanup: Log at ERROR level when connect()
fails.
Failure is defined as either being passed an invalid IP/Port, or some unexpected error code.
This commit is contained in:
parent
3ef96f7096
commit
fe4da6a541
|
@ -1,3 +1,4 @@
|
|||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -8,7 +9,7 @@
|
|||
#include "../toxcore/crypto_core.h"
|
||||
#include "../toxcore/mono_time.h"
|
||||
#include "../toxcore/util.h"
|
||||
#include "check_compat.h"
|
||||
#include "auto_test_support.h"
|
||||
|
||||
#define NUM_PORTS 3
|
||||
|
||||
|
@ -46,6 +47,7 @@ static void test_basic(void)
|
|||
{
|
||||
Mono_Time *mono_time = mono_time_new();
|
||||
Logger *logger = logger_new();
|
||||
logger_callback_log(logger, (logger_cb *)print_debug_log, nullptr, nullptr);
|
||||
|
||||
// Attempt to create a new TCP_Server instance.
|
||||
uint8_t self_public_key[CRYPTO_PUBLIC_KEY_SIZE];
|
||||
|
@ -65,8 +67,8 @@ static void test_basic(void)
|
|||
for (uint8_t i = 0; i < NUM_PORTS; i++) {
|
||||
sock = net_socket(net_family_ipv6, TOX_SOCK_STREAM, TOX_PROTO_TCP);
|
||||
localhost.port = net_htons(ports[i]);
|
||||
int ret = net_connect(logger, sock, &localhost);
|
||||
ck_assert_msg(ret == 0, "Failed to connect to created TCP relay server on port %d.", ports[i]);
|
||||
bool ret = net_connect(logger, sock, &localhost);
|
||||
ck_assert_msg(ret, "Failed to connect to created TCP relay server on port %d (%d).", ports[i], errno);
|
||||
|
||||
// Leave open one connection for the next test.
|
||||
if (i + 1 < NUM_PORTS) {
|
||||
|
@ -194,8 +196,8 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s,
|
|||
localhost.ip = get_loopback();
|
||||
localhost.port = net_htons(ports[random_u32() % NUM_PORTS]);
|
||||
|
||||
int ret = net_connect(logger, sock, &localhost);
|
||||
ck_assert_msg(ret == 0, "Failed to connect to the test TCP relay server.");
|
||||
bool ok = net_connect(logger, sock, &localhost);
|
||||
ck_assert_msg(ok, "Failed to connect to the test TCP relay server.");
|
||||
|
||||
uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE];
|
||||
crypto_new_keypair(sec_c->public_key, f_secret_key);
|
||||
|
@ -209,7 +211,7 @@ static struct sec_TCP_con *new_TCP_con(const Logger *logger, TCP_Server *tcp_s,
|
|||
memcpy(handshake, sec_c->public_key, CRYPTO_PUBLIC_KEY_SIZE);
|
||||
random_nonce(handshake + CRYPTO_PUBLIC_KEY_SIZE);
|
||||
|
||||
ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
|
||||
int ret = encrypt_data(tcp_server_public_key(tcp_s), f_secret_key, handshake + CRYPTO_PUBLIC_KEY_SIZE, handshake_plain,
|
||||
TCP_HANDSHAKE_PLAIN_SIZE, handshake + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE);
|
||||
ck_assert_msg(ret == TCP_CLIENT_HANDSHAKE_SIZE - (CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_NONCE_SIZE),
|
||||
"Failed to encrypt the outgoing handshake.");
|
||||
|
|
|
@ -1 +1 @@
|
|||
6b4d6b18a788c6a400b05f78de42717a4c6c588c920ed967527def7fc2b03740 /usr/local/bin/tox-bootstrapd
|
||||
eb9e7efd034de17ba6af095bbfd595308ff994148f394e715087078859d860f6 /usr/local/bin/tox-bootstrapd
|
||||
|
|
|
@ -98,16 +98,11 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value)
|
|||
non_null()
|
||||
static bool connect_sock_to(const Logger *logger, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info)
|
||||
{
|
||||
IP_Port ipp_copy = *ip_port;
|
||||
|
||||
if (proxy_info->proxy_type != TCP_PROXY_NONE) {
|
||||
ipp_copy = proxy_info->ip_port;
|
||||
return net_connect(logger, sock, &proxy_info->ip_port);
|
||||
} else {
|
||||
return net_connect(logger, sock, ip_port);
|
||||
}
|
||||
|
||||
/* nonblocking socket, connect will never return success */
|
||||
net_connect(logger, sock, &ipp_copy);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/** return 1 on success.
|
||||
|
|
|
@ -120,6 +120,11 @@ static bool should_ignore_recv_error(int err)
|
|||
return err == EWOULDBLOCK;
|
||||
}
|
||||
|
||||
static bool should_ignore_connect_error(int err)
|
||||
{
|
||||
return err == EWOULDBLOCK || err == EINPROGRESS || err == EAGAIN;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static const char *inet_ntop4(const struct in_addr *addr, char *buf, size_t bufsize)
|
||||
{
|
||||
|
@ -156,6 +161,11 @@ static bool should_ignore_recv_error(int err)
|
|||
return err == WSAEWOULDBLOCK || err == WSAECONNRESET;
|
||||
}
|
||||
|
||||
static bool should_ignore_connect_error(int err)
|
||||
{
|
||||
return err == WSAEWOULDBLOCK || err == WSAEINPROGRESS;
|
||||
}
|
||||
|
||||
non_null()
|
||||
static const char *inet_ntop4(const struct in_addr *addr, char *buf, size_t bufsize)
|
||||
{
|
||||
|
@ -1421,7 +1431,7 @@ bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
|
|||
return true;
|
||||
}
|
||||
|
||||
int net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
|
||||
bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
|
||||
{
|
||||
struct sockaddr_storage addr = {0};
|
||||
size_t addrsize;
|
||||
|
@ -1441,14 +1451,32 @@ int net_connect(const Logger *log, Socket sock, const IP_Port *ip_port)
|
|||
fill_addr6(&ip_port->ip.ip.v6, &addr6->sin6_addr);
|
||||
addr6->sin6_port = ip_port->port;
|
||||
} else {
|
||||
return 0;
|
||||
char ip_str[IP_NTOA_LEN];
|
||||
LOGGER_ERROR(log, "cannot connect to %s:%d which is neither IPv4 nor IPv6",
|
||||
ip_ntoa(&ip_port->ip, ip_str, sizeof(ip_str)), ip_port->port);
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||
return 0;
|
||||
return true;
|
||||
#else
|
||||
LOGGER_DEBUG(log, "connecting socket %d", (int)sock.socket);
|
||||
return connect(sock.socket, (struct sockaddr *)&addr, addrsize);
|
||||
errno = 0;
|
||||
if (connect(sock.socket, (struct sockaddr *)&addr, addrsize) == -1) {
|
||||
const int error = net_error();
|
||||
|
||||
// Non-blocking socket: "Operation in progress" means it's connecting.
|
||||
if (!should_ignore_connect_error(error)) {
|
||||
char *net_strerror = net_new_strerror(error);
|
||||
char ip_str[IP_NTOA_LEN];
|
||||
LOGGER_ERROR(log, "failed to connect to %s:%d: %d (%s)",
|
||||
ip_ntoa(&ip_port->ip, ip_str, sizeof(ip_str)), ip_port->port,
|
||||
error, net_strerror);
|
||||
net_kill_strerror(net_strerror);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -428,11 +428,11 @@ void networking_poll(const Networking_Core *net, void *userdata);
|
|||
|
||||
/** Connect a socket to the address specified by the ip_port.
|
||||
*
|
||||
* Return 0 on success.
|
||||
* Return -1 on failure.
|
||||
* Return true on success.
|
||||
* Return false on failure.
|
||||
*/
|
||||
non_null()
|
||||
int net_connect(const Logger *log, Socket sock, const IP_Port *ip_port);
|
||||
bool net_connect(const Logger *log, Socket sock, const IP_Port *ip_port);
|
||||
|
||||
/** High-level getaddrinfo implementation.
|
||||
* Given node, which identifies an Internet host, net_getipport() fills an array
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
*/
|
||||
#include "onion.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -473,9 +474,7 @@ static int handle_send_2(void *object, const IP_Port *source, const uint8_t *pac
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (len <= SIZE_IPPORT) {
|
||||
return 1;
|
||||
}
|
||||
assert(len > SIZE_IPPORT);
|
||||
|
||||
const uint8_t packet_id = plain[SIZE_IPPORT];
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user