From 7fb9d410d160f6e93cd1c37fad4cae7a6e209115 Mon Sep 17 00:00:00 2001 From: Green Sky Date: Sun, 13 Oct 2024 11:10:31 +0200 Subject: [PATCH] refactor: move some network object declarations to its own header. We want this, so we can actually define a different Network Object. The problem is the sockaddr_storage, which requires platform headers which we don't want to leak, unless absolutely necessary. --- testing/fuzzing/fuzz_support.cc | 7 +-- toxcore/BUILD.bazel | 5 +- toxcore/Makefile.inc | 1 + toxcore/net_obj.h | 102 ++++++++++++++++++++++++++++++++ toxcore/network.c | 6 +- toxcore/network.h | 52 +--------------- toxcore/network_test_util.hh | 1 + 7 files changed, 112 insertions(+), 62 deletions(-) create mode 100644 toxcore/net_obj.h diff --git a/testing/fuzzing/fuzz_support.cc b/testing/fuzzing/fuzz_support.cc index 5cbdb7d515..b86af198f1 100644 --- a/testing/fuzzing/fuzz_support.cc +++ b/testing/fuzzing/fuzz_support.cc @@ -17,15 +17,10 @@ #include "../../toxcore/crypto_core.h" #include "../../toxcore/network.h" +#include "../../toxcore/net_obj.h" #include "../../toxcore/tox_private.h" #include "func_conversion.hh" -// TODO(iphydf): Put this somewhere shared. -struct Network_Addr { - struct sockaddr_storage addr; - size_t size; -}; - System::System(std::unique_ptr in_sys, std::unique_ptr in_mem, std::unique_ptr in_ns, std::unique_ptr in_rng) : sys(std::move(in_sys)) diff --git a/toxcore/BUILD.bazel b/toxcore/BUILD.bazel index 0f313e2af5..f468f6bd81 100644 --- a/toxcore/BUILD.bazel +++ b/toxcore/BUILD.bazel @@ -302,7 +302,10 @@ cc_library( cc_library( name = "network", srcs = ["network.c"], - hdrs = ["network.h"], + hdrs = [ + "net_obj.h" + "network.h", + ], visibility = [ "//c-toxcore/auto_tests:__pkg__", "//c-toxcore/other:__pkg__", diff --git a/toxcore/Makefile.inc b/toxcore/Makefile.inc index db3e193243..f96ade0ef4 100644 --- a/toxcore/Makefile.inc +++ b/toxcore/Makefile.inc @@ -62,6 +62,7 @@ libtoxcore_la_SOURCES = ../third_party/cmp/cmp.c \ ../toxcore/mem.c \ ../toxcore/mono_time.h \ ../toxcore/mono_time.c \ + ../toxcore/net_obj.h \ ../toxcore/network.h \ ../toxcore/network.c \ ../toxcore/crypto_core.h \ diff --git a/toxcore/net_obj.h b/toxcore/net_obj.h new file mode 100644 index 0000000000..54f696f048 --- /dev/null +++ b/toxcore/net_obj.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later + * Copyright © 2016-2024 The TokTok team. + * Copyright © 2013 Tox project. + */ + +/** + * The networking object. + */ +#ifndef C_TOXCORE_TOXCORE_NET_OBJ_H +#define C_TOXCORE_TOXCORE_NET_OBJ_H + +#if defined(_WIN32) && defined(_WIN32_WINNT) && defined(_WIN32_WINNT_WINXP) && _WIN32_WINNT >= _WIN32_WINNT_WINXP +# undef _WIN32_WINNT +# define _WIN32_WINNT 0x501 +#endif /* defined(_WIN32) && defined(_WIN32_WINNT) && defined(_WIN32_WINNT_WINXP) && _WIN32_WINNT >= _WIN32_WINNT_WINXP */ + +#if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) +# define OS_WIN32 +#endif /* !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) */ + +#if defined(OS_WIN32) && !defined(WINVER) +// Windows XP +# define WINVER 0x0501 +#endif /* defined(OS_WIN32) && !defined(WINVER) */ + +#include // bool +#include // size_t +#include // uint*_t + +// for sockaddr_storage +#ifdef OS_WIN32 +# include +#else +# include +#endif /* OS_WIN32 */ + +#include "network.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Wrapper for sockaddr_storage and size. + */ +typedef struct Network_Addr { + struct sockaddr_storage addr; + size_t size; +} Network_Addr; + +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); +typedef int net_send_cb(void *obj, Socket sock, const uint8_t *buf, size_t len); +typedef int net_sendto_cb(void *obj, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr); +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, 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. + * + * Refer to POSIX man pages for documentation of what these functions are + * expected to do when providing alternative Network implementations. + */ +typedef struct Network_Funcs { + net_close_cb *close; + 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; + net_send_cb *send; + net_sendto_cb *sendto; + net_socket_cb *socket; + net_socket_nonblock_cb *socket_nonblock; + net_getsockopt_cb *getsockopt; + net_setsockopt_cb *setsockopt; + net_getaddrinfo_cb *getaddrinfo; + net_freeaddrinfo_cb *freeaddrinfo; +} Network_Funcs; + +typedef struct Network { + const Network_Funcs *funcs; + void *obj; +} Network; + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* C_TOXCORE_TOXCORE_NET_OBJ_H */ + diff --git a/toxcore/network.c b/toxcore/network.c index 848f8dd68d..6d314277cc 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -86,6 +86,7 @@ #include "ccompat.h" #include "logger.h" #include "mem.h" +#include "net_obj.h" #include "util.h" // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD @@ -480,11 +481,6 @@ bool sock_valid(Socket sock) return sock.value != invalid_socket.value; } -struct Network_Addr { - struct sockaddr_storage addr; - size_t size; -}; - non_null() static int sys_close(void *obj, Socket sock) { diff --git a/toxcore/network.h b/toxcore/network.h index 92d43cee04..817407cc8b 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -22,11 +22,6 @@ extern "C" { #endif -/** - * @brief Wrapper for sockaddr_storage and size. - */ -typedef struct Network_Addr Network_Addr; - typedef bitwise int Socket_Value; typedef struct Socket { Socket_Value value; @@ -35,51 +30,8 @@ typedef struct Socket { int net_socket_to_native(Socket sock); Socket net_socket_from_native(int sock); -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); -typedef int net_send_cb(void *obj, Socket sock, const uint8_t *buf, size_t len); -typedef int net_sendto_cb(void *obj, Socket sock, const uint8_t *buf, size_t len, const Network_Addr *addr); -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, 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. - * - * Refer to POSIX man pages for documentation of what these functions are - * expected to do when providing alternative Network implementations. - */ -typedef struct Network_Funcs { - net_close_cb *close; - 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; - net_send_cb *send; - net_sendto_cb *sendto; - net_socket_cb *socket; - net_socket_nonblock_cb *socket_nonblock; - net_getsockopt_cb *getsockopt; - net_setsockopt_cb *setsockopt; - net_getaddrinfo_cb *getaddrinfo; - net_freeaddrinfo_cb *freeaddrinfo; -} Network_Funcs; - -typedef struct Network { - const Network_Funcs *funcs; - void *obj; -} Network; +// definition in net_obj.h +typedef struct Network Network; const Network *os_network(void); const Network *os_network_no_dns(void); diff --git a/toxcore/network_test_util.hh b/toxcore/network_test_util.hh index 7dd51e9b73..e9a91bdb8b 100644 --- a/toxcore/network_test_util.hh +++ b/toxcore/network_test_util.hh @@ -5,6 +5,7 @@ #include "crypto_core.h" #include "network.h" +#include "net_obj.h" #include "test_util.hh" struct Network_Class {