From 6cf8b83e76e5e12eee90539a794e8c2a50c49f4a Mon Sep 17 00:00:00 2001 From: Green Sky Date: Fri, 11 Oct 2024 12:11:05 +0200 Subject: [PATCH] feat: implement the last 2 missing network struct functions and make use of them --- toxcore/network.c | 231 ++++++++++++++++++++++++++++------- toxcore/network.h | 5 +- toxcore/network_test_util.cc | 4 +- toxcore/network_test_util.hh | 2 +- toxcore/tox.c | 2 +- toxcore/tox_private.c | 2 +- 6 files changed, 193 insertions(+), 53 deletions(-) diff --git a/toxcore/network.c b/toxcore/network.c index 8800bf7e40..848f8dd68d 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -592,6 +592,108 @@ static int sys_setsockopt(void *obj, Socket sock, int level, int optname, const return setsockopt(net_socket_to_native(sock), level, optname, (const char *)optval, optlen); } +// sets and fills an array of addrs for address +// returns the number of entries in addrs +non_null() +static int sys_getaddrinfo(void *obj, const char *address, int family, int sock_type, Network_Addr **addrs) +{ + assert(addrs != nullptr); + *addrs = nullptr; + + struct addrinfo hints = {0}; + hints.ai_family = family; + + + // different platforms favour a different field + // hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. + hints.ai_socktype = sock_type; + // hints.ai_protocol = protocol; + + struct addrinfo *infos = nullptr; + + const int rc = getaddrinfo(address, nullptr, &hints, &infos); + + // Lookup failed. + if (rc != 0) { + // TODO(Green-Sky): log error + return 0; + } + + const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(Network_Addr); + + // we count number of "valid" results + int result = 0; + for (struct addrinfo *walker = infos; walker != nullptr && result < max_count; walker = walker->ai_next) { + if (walker->ai_family == family || family == AF_UNSPEC) { + ++result; + } + + // do we need to check socktype/protocol? + } + + assert(max_count >= result); + + // HACK: use default memory allocator + const Memory* mem = os_memory(); + + *addrs = (Network_Addr *)mem_valloc(mem, result, sizeof(Network_Addr)); + if (*addrs == nullptr) { + freeaddrinfo(infos); + return 0; + } + + // now we fill in + int i = 0; + for (struct addrinfo *walker = infos; walker != nullptr; walker = walker->ai_next) { + if (walker->ai_family == family || family == AF_UNSPEC) { + (*addrs)[i].size = sizeof(struct sockaddr_storage); + (*addrs)[i].addr.ss_family = walker->ai_family; + + // according to spec, storage is supposed to be large enough (and source shows they are) + // storage is 128 bytes + assert(walker->ai_addrlen <= (*addrs)[i].size); + + memcpy(&(*addrs)[i].addr, walker->ai_addr, walker->ai_addrlen); + (*addrs)[i].size = walker->ai_addrlen; + + i++; + } + } + + assert(i == result); + + freeaddrinfo(infos); + + // number of entries in addrs + return result; +} + +non_null() +static int sys_getaddrinfo_dummy(void *obj, const char *address, int family, int protocol, Network_Addr **addrs) +{ + return 0; +} + +non_null() +static int sys_freeaddrinfo(void *obj, Network_Addr *addrs) +{ + if (addrs == nullptr) { + return 0; + } + + // HACK: use default memory allocator + const Memory* mem = os_memory(); + mem_delete(mem, addrs); + + return 0; +} + +non_null() +static int sys_freeaddrinfo_dummy(void *obj, Network_Addr *addrs) +{ + return 0; +} + static const Network_Funcs os_network_funcs = { sys_close, sys_accept, @@ -607,8 +709,10 @@ static const Network_Funcs os_network_funcs = { sys_socket_nonblock, sys_getsockopt, sys_setsockopt, + sys_getaddrinfo, + sys_freeaddrinfo }; -static const Network os_network_obj = {&os_network_funcs}; +static const Network os_network_obj = {&os_network_funcs, nullptr}; const Network *os_network(void) { @@ -627,6 +731,44 @@ const Network *os_network(void) return &os_network_obj; } +static const Network_Funcs os_network_no_dns_funcs = { + sys_close, + sys_accept, + sys_bind, + sys_listen, + sys_connect, + sys_recvbuf, + sys_recv, + sys_recvfrom, + sys_send, + sys_sendto, + sys_socket, + sys_socket_nonblock, + sys_getsockopt, + sys_setsockopt, + sys_getaddrinfo_dummy, + sys_freeaddrinfo_dummy +}; + +static const Network os_network_no_dns_obj = {&os_network_no_dns_funcs, nullptr}; + +const Network *os_network_no_dns(void) +{ +#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION + if ((true)) { + return nullptr; + } +#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ +#ifdef OS_WIN32 + WSADATA wsaData; + + if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { + return nullptr; + } +#endif /* OS_WIN32 */ + return &os_network_no_dns_obj; +} + #if 0 /* TODO(iphydf): Call this from functions that use `os_network()`. */ void os_network_deinit(const Network *ns) @@ -1840,19 +1982,16 @@ static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extr const Family tox_family = to->family; const int family = make_family(tox_family); - struct addrinfo hints = {0}; - hints.ai_family = family; - hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. - - struct addrinfo *server = nullptr; + Network_Addr* addrs = nullptr; + const int rc = ns->funcs->getaddrinfo(ns->obj, address, family, 0, &addrs); - const int rc = getaddrinfo(address, nullptr, &hints, &server); - - // Lookup failed. - if (rc != 0) { + // Lookup failed / empty. + if (rc <= 0) { return 0; } + assert(addrs != nullptr); + IP ip4; ip_init(&ip4, false); // ipv6enabled = false IP ip6; @@ -1861,16 +2000,16 @@ static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extr int result = 0; bool done = false; - for (struct addrinfo *walker = server; walker != nullptr && !done; walker = walker->ai_next) { - switch (walker->ai_family) { + for (int i = 0; i < rc && !done; i++) { + switch (addrs[i].addr.ss_family) { case AF_INET: { - if (walker->ai_family == family) { /* AF_INET requested, done */ - const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)walker->ai_addr; + if (addrs[i].addr.ss_family == family) { /* AF_INET requested, done */ + const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; get_ip4(&to->ip.v4, &addr->sin_addr); result = TOX_ADDR_RESOLVE_INET; done = true; } else if ((result & TOX_ADDR_RESOLVE_INET) == 0) { /* AF_UNSPEC requested, store away */ - const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)walker->ai_addr; + const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; get_ip4(&ip4.ip.v4, &addr->sin_addr); result |= TOX_ADDR_RESOLVE_INET; } @@ -1879,16 +2018,16 @@ static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extr } case AF_INET6: { - if (walker->ai_family == family) { /* AF_INET6 requested, done */ - if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { - const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr; + if (addrs[i].addr.ss_family == family) { /* AF_INET6 requested, done */ + if (addrs[i].size == sizeof(struct sockaddr_in6)) { + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)&addrs[i].addr; get_ip6(&to->ip.v6, &addr->sin6_addr); result = TOX_ADDR_RESOLVE_INET6; done = true; } } else if ((result & TOX_ADDR_RESOLVE_INET6) == 0) { /* AF_UNSPEC requested, store away */ - if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { - const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr; + if (addrs[i].size == sizeof(struct sockaddr_in6)) { + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)&addrs[i].addr; get_ip6(&ip6.ip.v6, &addr->sin6_addr); result |= TOX_ADDR_RESOLVE_INET6; } @@ -1913,7 +2052,7 @@ static int addr_resolve(const Network *ns, const char *address, IP *to, IP *extr } } - freeaddrinfo(server); + ns->funcs->freeaddrinfo(ns->obj, addrs); return result; } @@ -1980,7 +2119,7 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket return true; } -int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type) +int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP_Port **res, int tox_type) { assert(node != nullptr); @@ -2016,25 +2155,29 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to } #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ + int type = make_socktype(tox_type); + // ugly + if (tox_type == -1) { + type = 0; + } + // It's not an IP address, so now we try doing a DNS lookup. - struct addrinfo *infos; - const int ret = getaddrinfo(node, nullptr, nullptr, &infos); + Network_Addr* addrs = nullptr; + const int rc = ns->funcs->getaddrinfo(ns->obj, node, AF_UNSPEC, type, &addrs); - if (ret != 0) { + // Lookup failed / empty. + if (rc <= 0) { return -1; } + assert(addrs != nullptr); + // Used to avoid calloc parameter overflow const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); - const int type = make_socktype(tox_type); size_t count = 0; - for (struct addrinfo *cur = infos; count < max_count && cur != nullptr; cur = cur->ai_next) { - if (cur->ai_socktype != 0 && type > 0 && cur->ai_socktype != type) { - continue; - } - - if (cur->ai_family != AF_INET && cur->ai_family != AF_INET6) { + for (int i = 0; i < rc && count < max_count; i++) { + if (addrs[i].addr.ss_family != AF_INET && addrs[i].addr.ss_family != AF_INET6) { continue; } @@ -2044,40 +2187,36 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to assert(count <= max_count); if (count == 0) { - freeaddrinfo(infos); + ns->funcs->freeaddrinfo(ns->obj, addrs); return 0; } IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); if (ip_port == nullptr) { - freeaddrinfo(infos); + ns->funcs->freeaddrinfo(ns->obj, addrs); *res = nullptr; return -1; } *res = ip_port; - for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) { - if (cur->ai_socktype != 0 && type > 0 && cur->ai_socktype != type) { - continue; - } - - if (cur->ai_family == AF_INET) { - const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)cur->ai_addr; + for (int i = 0; i < rc && count < max_count; i++) { + if (addrs[i].addr.ss_family == AF_INET) { + const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; ip_port->ip.ip.v4.uint32 = addr->sin_addr.s_addr; - } else if (cur->ai_family == AF_INET6) { - const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)cur->ai_addr; + } else if (addrs[i].addr.ss_family == AF_INET6) { + const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)&addrs[i].addr; memcpy(ip_port->ip.ip.v6.uint8, addr->sin6_addr.s6_addr, sizeof(IP6)); } else { continue; } - const Family *const family = make_tox_family(cur->ai_family); + const Family *const family = make_tox_family(addrs[i].addr.ss_family); assert(family != nullptr); if (family == nullptr) { - freeaddrinfo(infos); + ns->funcs->freeaddrinfo(ns->obj, addrs); return -1; } @@ -2086,7 +2225,7 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to ++ip_port; } - freeaddrinfo(infos); + ns->funcs->freeaddrinfo(ns->obj, addrs); return count; } diff --git a/toxcore/network.h b/toxcore/network.h index 8c6cc5de2e..92d43cee04 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -49,7 +49,7 @@ typedef Socket net_socket_cb(void *obj, int domain, int type, int proto); typedef int net_socket_nonblock_cb(void *obj, Socket sock, bool nonblock); typedef int net_getsockopt_cb(void *obj, Socket sock, int level, int optname, void *optval, size_t *optlen); typedef int net_setsockopt_cb(void *obj, Socket sock, int level, int optname, const void *optval, size_t optlen); -typedef int net_getaddrinfo_cb(void *obj, int family, Network_Addr **addrs); +typedef int net_getaddrinfo_cb(void *obj, const char *address, int family, int protocol, Network_Addr **addrs); typedef int net_freeaddrinfo_cb(void *obj, Network_Addr *addrs); /** @brief Functions wrapping POSIX network functions. @@ -82,6 +82,7 @@ typedef struct Network { } Network; const Network *os_network(void); +const Network *os_network_no_dns(void); typedef struct Family { uint8_t value; @@ -519,7 +520,7 @@ bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket * @retval -1 on error. */ non_null() -int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type); +int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP_Port **res, int tox_type); /** Deallocates memory allocated by net_getipport */ non_null(1) nullable(2) diff --git a/toxcore/network_test_util.cc b/toxcore/network_test_util.cc index bdb42ae1d9..d9462fe371 100644 --- a/toxcore/network_test_util.cc +++ b/toxcore/network_test_util.cc @@ -75,9 +75,9 @@ int Test_Network::setsockopt( { return net->funcs->setsockopt(net->obj, sock, level, optname, optval, optlen); } -int Test_Network::getaddrinfo(void *obj, int family, Network_Addr **addrs) +int Test_Network::getaddrinfo(void *obj, const char *address, int family, int protocol, Network_Addr **addrs) { - return net->funcs->getaddrinfo(net->obj, family, addrs); + return net->funcs->getaddrinfo(net->obj, address, family, protocol, addrs); } int Test_Network::freeaddrinfo(void *obj, Network_Addr *addrs) { diff --git a/toxcore/network_test_util.hh b/toxcore/network_test_util.hh index 88084b1fb9..7dd51e9b73 100644 --- a/toxcore/network_test_util.hh +++ b/toxcore/network_test_util.hh @@ -62,7 +62,7 @@ class Test_Network : public Network_Class { void *obj, Socket sock, int level, int optname, void *optval, size_t *optlen) override; int setsockopt( void *obj, Socket sock, int level, int optname, const void *optval, size_t optlen) override; - int getaddrinfo(void *obj, int family, Network_Addr **addrs) override; + int getaddrinfo(void *obj, const char *address, int family, int protocol, Network_Addr **addrs) override; int freeaddrinfo(void *obj, Network_Addr *addrs) override; }; diff --git a/toxcore/tox.c b/toxcore/tox.c index b02eb4e98b..e9e33b4011 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -1139,7 +1139,7 @@ static int32_t resolve_bootstrap_node(Tox *tox, const char *host, uint16_t port, return -1; } - const int32_t count = net_getipport(tox->sys.mem, host, root, TOX_SOCK_DGRAM); + const int32_t count = net_getipport(tox->sys.ns, tox->sys.mem, host, root, TOX_SOCK_DGRAM); if (count < 1) { LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host); diff --git a/toxcore/tox_private.c b/toxcore/tox_private.c index 7b6050a9f8..75a8b2dff1 100644 --- a/toxcore/tox_private.c +++ b/toxcore/tox_private.c @@ -124,7 +124,7 @@ bool tox_dht_get_nodes(const Tox *tox, const uint8_t *public_key, const char *ip IP_Port *root; - const int32_t count = net_getipport(tox->sys.mem, ip, &root, TOX_SOCK_DGRAM); + const int32_t count = net_getipport(tox->sys.ns, tox->sys.mem, ip, &root, TOX_SOCK_DGRAM); if (count < 1) { SET_ERROR_PARAMETER(error, TOX_ERR_DHT_GET_NODES_BAD_IP);