Skip to content

Commit

Permalink
feat(net): add missing connect to network struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Oct 16, 2024
1 parent 03e9fbf commit 3df118a
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
Expand Down
3 changes: 3 additions & 0 deletions testing/fuzzing/fuzz_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 14 additions & 8 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
5 changes: 5 additions & 0 deletions toxcore/network_test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Network_Funcs const Network_Class::vtable = {
Method<net_accept_cb, Network_Class>::invoke<&Network_Class::accept>,
Method<net_bind_cb, Network_Class>::invoke<&Network_Class::bind>,
Method<net_listen_cb, Network_Class>::invoke<&Network_Class::listen>,
Method<net_connect_cb, Network_Class>::invoke<&Network_Class::connect>,
Method<net_recvbuf_cb, Network_Class>::invoke<&Network_Class::recvbuf>,
Method<net_recv_cb, Network_Class>::invoke<&Network_Class::recv>,
Method<net_recvfrom_cb, Network_Class>::invoke<&Network_Class::recvfrom>,
Expand All @@ -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)
{
Expand Down
2 changes: 2 additions & 0 deletions toxcore/network_test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 3df118a

Please sign in to comment.