From 3df118aa5d968c5ff5b7b935871416030a9bc5ee Mon Sep 17 00:00:00 2001 From: Green Sky Date: Wed, 16 Oct 2024 10:39:25 +0200 Subject: [PATCH] feat(net): add missing connect to network struct --- auto_tests/TCP_test.c | 4 ++-- testing/fuzzing/fuzz_support.cc | 3 +++ toxcore/TCP_client.c | 8 ++++---- toxcore/network.c | 22 ++++++++++++++-------- toxcore/network.h | 4 +++- toxcore/network_test_util.cc | 5 +++++ toxcore/network_test_util.hh | 2 ++ 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index c4ea79b08d..e1fe0d0cd6 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -74,7 +74,7 @@ static void test_basic(void) for (uint8_t i = 0; i < NUM_PORTS; i++) { sock = net_socket(ns, net_family_ipv6(), TOX_SOCK_STREAM, TOX_PROTO_TCP); localhost.port = net_htons(ports[i]); - bool ret = net_connect(mem, logger, sock, &localhost); + bool ret = net_connect(ns, mem, 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. @@ -213,7 +213,7 @@ static struct sec_TCP_con *new_tcp_con(const Logger *logger, const Memory *mem, localhost.ip = get_loopback(); localhost.port = net_htons(ports[random_u32(rng) % NUM_PORTS]); - bool ok = net_connect(mem, logger, sock, &localhost); + bool ok = net_connect(ns, mem, 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]; diff --git a/testing/fuzzing/fuzz_support.cc b/testing/fuzzing/fuzz_support.cc index fe760f9958..5cbdb7d515 100644 --- a/testing/fuzzing/fuzz_support.cc +++ b/testing/fuzzing/fuzz_support.cc @@ -111,6 +111,7 @@ static constexpr Network_Funcs fuzz_network_funcs = { /* .accept = */ ![](Fuzz_System *self, Socket sock) { return Socket{1337}; }, /* .bind = */ ![](Fuzz_System *self, Socket sock, const Network_Addr *addr) { return 0; }, /* .listen = */ ![](Fuzz_System *self, Socket sock, int backlog) { return 0; }, + /* .connect = */ ![](Fuzz_System *self, Socket sock, const Network_Addr *addr) { return 0; }, /* .recvbuf = */ ![](Fuzz_System *self, Socket sock) { assert(sock.value == 42 || sock.value == 1337); @@ -225,6 +226,7 @@ static constexpr Network_Funcs null_network_funcs = { /* .accept = */ ![](Null_System *self, Socket sock) { return Socket{1337}; }, /* .bind = */ ![](Null_System *self, Socket sock, const Network_Addr *addr) { return 0; }, /* .listen = */ ![](Null_System *self, Socket sock, int backlog) { return 0; }, + /* .connect = */ ![](Null_System *self, Socket sock, const Network_Addr *addr) { return 0; }, /* .recvbuf = */ ![](Null_System *self, Socket sock) { return 0; }, /* .recv = */ ![](Null_System *self, Socket sock, uint8_t *buf, size_t len) { @@ -341,6 +343,7 @@ static constexpr Network_Funcs record_network_funcs = { return 0; }, /* .listen = */ ![](Record_System *self, Socket sock, int backlog) { return 0; }, + /* .connect = */ ![](Record_System *self, Socket sock, const Network_Addr *addr) { return 0; }, /* .recvbuf = */ ![](Record_System *self, Socket sock) { return 0; }, /* .recv = */ ![](Record_System *self, Socket sock, uint8_t *buf, size_t len) { diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 2b8c6e448f..938c5cf83a 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -107,12 +107,12 @@ void tcp_con_set_custom_uint(TCP_Client_Connection *con, uint32_t value) * @retval false on failure */ non_null() -static bool connect_sock_to(const Logger *logger, const Memory *mem, Socket sock, const IP_Port *ip_port, const TCP_Proxy_Info *proxy_info) +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) { if (proxy_info->proxy_type != TCP_PROXY_NONE) { - return net_connect(mem, logger, sock, &proxy_info->ip_port); + return net_connect(ns, mem, logger, sock, &proxy_info->ip_port); } else { - return net_connect(mem, logger, sock, ip_port); + return net_connect(ns, mem, logger, sock, ip_port); } } @@ -617,7 +617,7 @@ TCP_Client_Connection *new_tcp_connection( return nullptr; } - if (!(set_socket_nonblock(ns, sock) && connect_sock_to(logger, mem, sock, ip_port, proxy_info))) { + if (!(set_socket_nonblock(ns, sock) && connect_sock_to(ns, logger, mem, sock, ip_port, proxy_info))) { kill_sock(ns, sock); return nullptr; } diff --git a/toxcore/network.c b/toxcore/network.c index 55aa4e2818..8800bf7e40 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -513,6 +513,12 @@ static int sys_listen(void *obj, Socket sock, int backlog) return listen(net_socket_to_native(sock), backlog); } +non_null() +static int sys_connect(void *obj, Socket sock, const Network_Addr *addr) +{ + return connect(net_socket_to_native(sock), (const struct sockaddr *)&addr->addr, addr->size); +} + non_null() static int sys_recvbuf(void *obj, Socket sock) { @@ -591,6 +597,7 @@ static const Network_Funcs os_network_funcs = { sys_accept, sys_bind, sys_listen, + sys_connect, sys_recvbuf, sys_recv, sys_recvfrom, @@ -1921,22 +1928,21 @@ bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP return true; } -bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port) +bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port) { - struct sockaddr_storage addr = {0}; - size_t addrsize; + Network_Addr addr = {{0}}; if (net_family_is_ipv4(ip_port->ip.family)) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; + struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr.addr; - addrsize = sizeof(struct sockaddr_in); + addr.size = sizeof(struct sockaddr_in); addr4->sin_family = AF_INET; fill_addr4(&ip_port->ip.ip.v4, &addr4->sin_addr); addr4->sin_port = ip_port->port; } else if (net_family_is_ipv6(ip_port->ip.family)) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr.addr; - addrsize = sizeof(struct sockaddr_in6); + addr.size = sizeof(struct sockaddr_in6); addr6->sin6_family = AF_INET6; fill_addr6(&ip_port->ip.ip.v6, &addr6->sin6_addr); addr6->sin6_port = ip_port->port; @@ -1958,7 +1964,7 @@ bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Por net_socket_to_native(sock), net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port)); errno = 0; - if (connect(net_socket_to_native(sock), (struct sockaddr *)&addr, addrsize) == -1) { + if (ns->funcs->connect(ns->obj, sock, &addr) == -1) { const int error = net_error(); // Non-blocking socket: "Operation in progress" means it's connecting. diff --git a/toxcore/network.h b/toxcore/network.h index 06857b8917..8c6cc5de2e 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -39,6 +39,7 @@ typedef int net_close_cb(void *obj, Socket sock); typedef Socket net_accept_cb(void *obj, Socket sock); typedef int net_bind_cb(void *obj, Socket sock, const Network_Addr *addr); typedef int net_listen_cb(void *obj, Socket sock, int backlog); +typedef int net_connect_cb(void *obj, Socket sock, const Network_Addr *addr); typedef int net_recvbuf_cb(void *obj, Socket sock); typedef int net_recv_cb(void *obj, Socket sock, uint8_t *buf, size_t len); typedef int net_recvfrom_cb(void *obj, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr); @@ -61,6 +62,7 @@ typedef struct Network_Funcs { net_accept_cb *accept; net_bind_cb *bind; net_listen_cb *listen; + net_connect_cb *connect; net_recvbuf_cb *recvbuf; net_recv_cb *recv; net_recvfrom_cb *recvfrom; @@ -501,7 +503,7 @@ void networking_poll(const Networking_Core *net, void *userdata); * Return false on failure. */ non_null() -bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port); +bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port); /** @brief High-level getaddrinfo implementation. * diff --git a/toxcore/network_test_util.cc b/toxcore/network_test_util.cc index 758e49500d..bdb42ae1d9 100644 --- a/toxcore/network_test_util.cc +++ b/toxcore/network_test_util.cc @@ -11,6 +11,7 @@ Network_Funcs const Network_Class::vtable = { Method::invoke<&Network_Class::accept>, Method::invoke<&Network_Class::bind>, Method::invoke<&Network_Class::listen>, + Method::invoke<&Network_Class::connect>, Method::invoke<&Network_Class::recvbuf>, Method::invoke<&Network_Class::recv>, Method::invoke<&Network_Class::recvfrom>, @@ -34,6 +35,10 @@ int Test_Network::listen(void *obj, Socket sock, int backlog) { return net->funcs->listen(net->obj, sock, backlog); } +int Test_Network::connect(void *obj, Socket sock, const Network_Addr *addr) +{ + return net->funcs->connect(net->obj, sock, addr); +} int Test_Network::recvbuf(void *obj, Socket sock) { return net->funcs->recvbuf(net->obj, sock); } int Test_Network::recv(void *obj, Socket sock, uint8_t *buf, size_t len) { diff --git a/toxcore/network_test_util.hh b/toxcore/network_test_util.hh index 70f238aa92..88084b1fb9 100644 --- a/toxcore/network_test_util.hh +++ b/toxcore/network_test_util.hh @@ -24,6 +24,7 @@ struct Network_Class { virtual net_accept_cb accept = 0; virtual net_bind_cb bind = 0; virtual net_listen_cb listen = 0; + virtual net_connect_cb connect = 0; virtual net_recvbuf_cb recvbuf = 0; virtual net_recv_cb recv = 0; virtual net_recvfrom_cb recvfrom = 0; @@ -48,6 +49,7 @@ class Test_Network : public Network_Class { Socket accept(void *obj, Socket sock) override; int bind(void *obj, Socket sock, const Network_Addr *addr) override; int listen(void *obj, Socket sock, int backlog) override; + int connect(void *obj, Socket sock, const Network_Addr *addr) override; int recvbuf(void *obj, Socket sock) override; int recv(void *obj, Socket sock, uint8_t *buf, size_t len) override; int recvfrom(void *obj, Socket sock, uint8_t *buf, size_t len, Network_Addr *addr) override;