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/network.c b/toxcore/network.c index db7107abb1..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, @@ -1923,20 +1930,19 @@ bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP 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 Network *ns, const Memory *mem, const Logger *log, Socket 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 1d6fe5db4f..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; 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;