Skip to content

Commit

Permalink
refactor: move some network object declarations to its own header.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Green-Sky committed Oct 16, 2024
1 parent afeeef4 commit 7fb9d41
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 62 deletions.
7 changes: 1 addition & 6 deletions testing/fuzzing/fuzz_support.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Tox_System> in_sys, std::unique_ptr<Memory> in_mem,
std::unique_ptr<Network> in_ns, std::unique_ptr<Random> in_rng)
: sys(std::move(in_sys))
Expand Down
5 changes: 4 additions & 1 deletion toxcore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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__",
Expand Down
1 change: 1 addition & 0 deletions toxcore/Makefile.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
102 changes: 102 additions & 0 deletions toxcore/net_obj.h
Original file line number Diff line number Diff line change
@@ -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 <stdbool.h> // bool
#include <stddef.h> // size_t
#include <stdint.h> // uint*_t

// for sockaddr_storage
#ifdef OS_WIN32
# include <winsock2.h>
#else
# include <sys/socket.h>
#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 */

6 changes: 1 addition & 5 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down
52 changes: 2 additions & 50 deletions toxcore/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions toxcore/network_test_util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "crypto_core.h"
#include "network.h"
#include "net_obj.h"
#include "test_util.hh"

struct Network_Class {
Expand Down

0 comments on commit 7fb9d41

Please sign in to comment.