Coverage Report

Created: 2026-10-24 08:16

/work/toxcore/net.h
Line
Count
Source
1
/* SPDX-License-Identifier: GPL-3.0-or-later
2
 * Copyright © 2016-2026 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
#ifndef C_TOXCORE_TOXCORE_NET_H
7
#define C_TOXCORE_TOXCORE_NET_H
8
9
#include <stdbool.h>
10
#include <stddef.h>
11
#include <stdint.h>
12
13
#include "attributes.h"
14
#include "mem.h"
15
16
#ifdef __cplusplus
17
extern "C" {
18
#endif
19
20
typedef bitwise int Socket_Value;
21
typedef struct Socket {
22
    Socket_Value value;
23
} Socket;
24
25
2.79M
#define SIZE_IP4 4
26
6.19M
#define SIZE_IP6 16
27
5.63M
#define SIZE_IP (1 + SIZE_IP6)
28
5.63M
#define SIZE_PORT 2
29
4.96M
#define SIZE_IPPORT (SIZE_IP + SIZE_PORT)
30
31
typedef struct Family {
32
    uint8_t value;
33
} Family;
34
35
typedef union IP4 {
36
    uint32_t uint32;
37
    uint16_t uint16[2];
38
    uint8_t uint8[4];
39
} IP4;
40
41
typedef union IP6 {
42
    uint8_t uint8[16];
43
    uint16_t uint16[8];
44
    uint32_t uint32[4];
45
    uint64_t uint64[2];
46
} IP6;
47
48
typedef union IP_Union {
49
    IP4 v4;
50
    IP6 v6;
51
} IP_Union;
52
53
typedef struct IP {
54
    Family family;
55
    IP_Union ip;
56
} IP;
57
58
typedef struct IP_Port {
59
    IP ip;
60
    uint16_t port;
61
} IP_Port;
62
63
/** Redefinitions of variables for safe transfer over wire. */
64
35
#define TOX_AF_UNSPEC 0
65
1.61M
#define TOX_AF_INET 2
66
144k
#define TOX_AF_INET6 10
67
153k
#define TOX_TCP_INET 130
68
1.88k
#define TOX_TCP_INET6 138
69
70
287
#define TOX_SOCK_STREAM 1
71
31.0k
#define TOX_SOCK_DGRAM 2
72
73
239
#define TOX_PROTO_TCP 1
74
2.46k
#define TOX_PROTO_UDP 2
75
76
/** TCP related */
77
2
#define TCP_CLIENT_FAMILY (TOX_AF_INET6 + 1)
78
140k
#define TCP_INET (TOX_AF_INET6 + 2)
79
38
#define TCP_INET6 (TOX_AF_INET6 + 3)
80
2
#define TCP_SERVER_FAMILY (TOX_AF_INET6 + 4)
81
82
int net_socket_to_native(Socket sock);
83
Socket net_socket_from_native(int sock);
84
Socket net_invalid_socket(void);
85
86
typedef int net_close_cb(void *_Nullable obj, Socket sock);
87
typedef Socket net_accept_cb(void *_Nullable obj, Socket sock);
88
typedef int net_bind_cb(void *_Nullable obj, Socket sock, const IP_Port *_Nonnull addr);
89
typedef int net_listen_cb(void *_Nullable obj, Socket sock, int backlog);
90
typedef int net_connect_cb(void *_Nullable obj, Socket sock, const IP_Port *_Nonnull addr);
91
typedef int net_recvbuf_cb(void *_Nullable obj, Socket sock);
92
typedef int net_recv_cb(void *_Nullable obj, Socket sock, uint8_t *_Nonnull buf, size_t len);
93
typedef int net_recvfrom_cb(void *_Nullable obj, Socket sock, uint8_t *_Nonnull buf, size_t len, IP_Port *_Nonnull addr);
94
typedef int net_send_cb(void *_Nullable obj, Socket sock, const uint8_t *_Nonnull buf, size_t len);
95
typedef int net_sendto_cb(void *_Nullable obj, Socket sock, const uint8_t *_Nonnull buf, size_t len, const IP_Port *_Nonnull addr);
96
typedef Socket net_socket_cb(void *_Nullable obj, int domain, int type, int proto);
97
typedef int net_socket_nonblock_cb(void *_Nullable obj, Socket sock, bool nonblock);
98
typedef int net_getsockopt_cb(void *_Nullable obj, Socket sock, int level, int optname, void *_Nonnull optval, size_t *_Nonnull optlen);
99
typedef int net_setsockopt_cb(void *_Nullable obj, Socket sock, int level, int optname, const void *_Nonnull optval, size_t optlen);
100
typedef int net_getaddrinfo_cb(void *_Nullable obj, const Memory *_Nonnull mem, const char *_Nonnull address, int family, int protocol, IP_Port *_Nullable *_Nonnull addrs);
101
typedef int net_freeaddrinfo_cb(void *_Nullable obj, const Memory *_Nonnull mem, IP_Port *_Nullable addrs);
102
103
typedef struct Network_Funcs {
104
    net_close_cb *_Nullable close;
105
    net_accept_cb *_Nullable accept;
106
    net_bind_cb *_Nullable bind;
107
    net_listen_cb *_Nullable listen;
108
    net_connect_cb *_Nullable connect;
109
    net_recvbuf_cb *_Nullable recvbuf;
110
    net_recv_cb *_Nullable recv;
111
    net_recvfrom_cb *_Nullable recvfrom;
112
    net_send_cb *_Nullable send;
113
    net_sendto_cb *_Nullable sendto;
114
    net_socket_cb *_Nullable socket;
115
    net_socket_nonblock_cb *_Nullable socket_nonblock;
116
    net_getsockopt_cb *_Nullable getsockopt;
117
    net_setsockopt_cb *_Nullable setsockopt;
118
    net_getaddrinfo_cb *_Nullable getaddrinfo;
119
    net_freeaddrinfo_cb *_Nullable freeaddrinfo;
120
} Network_Funcs;
121
122
typedef struct Network {
123
    const Network_Funcs *_Nullable funcs;
124
    void *_Nullable obj;
125
} Network;
126
127
int ns_close(const Network *_Nonnull ns, Socket sock);
128
Socket ns_accept(const Network *_Nonnull ns, Socket sock);
129
int ns_bind(const Network *_Nonnull ns, Socket sock, const IP_Port *_Nonnull addr);
130
int ns_listen(const Network *_Nonnull ns, Socket sock, int backlog);
131
int ns_connect(const Network *_Nonnull ns, Socket sock, const IP_Port *_Nonnull addr);
132
int ns_recvbuf(const Network *_Nonnull ns, Socket sock);
133
int ns_recv(const Network *_Nonnull ns, Socket sock, uint8_t *_Nonnull buf, size_t len);
134
int ns_recvfrom(const Network *_Nonnull ns, Socket sock, uint8_t *_Nonnull buf, size_t len, IP_Port *_Nonnull addr);
135
int ns_send(const Network *_Nonnull ns, Socket sock, const uint8_t *_Nonnull buf, size_t len);
136
int ns_sendto(const Network *_Nonnull ns, Socket sock, const uint8_t *_Nonnull buf, size_t len, const IP_Port *_Nonnull addr);
137
Socket ns_socket(const Network *_Nonnull ns, int domain, int type, int proto);
138
int ns_socket_nonblock(const Network *_Nonnull ns, Socket sock, bool nonblock);
139
int ns_getsockopt(const Network *_Nonnull ns, Socket sock, int level, int optname, void *_Nonnull optval, size_t *_Nonnull optlen);
140
int ns_setsockopt(const Network *_Nonnull ns, Socket sock, int level, int optname, const void *_Nonnull optval, size_t optlen);
141
int ns_getaddrinfo(const Network *_Nonnull ns, const Memory *_Nonnull mem, const char *_Nonnull address, int family, int protocol, IP_Port *_Nullable *_Nonnull addrs);
142
int ns_freeaddrinfo(const Network *_Nonnull ns, const Memory *_Nonnull mem, IP_Port *_Nullable addrs);
143
144
bool net_family_is_unspec(Family family);
145
bool net_family_is_ipv4(Family family);
146
bool net_family_is_ipv6(Family family);
147
bool net_family_is_tcp_server(Family family);
148
bool net_family_is_tcp_client(Family family);
149
bool net_family_is_tcp_ipv4(Family family);
150
bool net_family_is_tcp_ipv6(Family family);
151
bool net_family_is_tox_tcp_ipv4(Family family);
152
bool net_family_is_tox_tcp_ipv6(Family family);
153
154
Family net_family_unspec(void);
155
Family net_family_ipv4(void);
156
Family net_family_ipv6(void);
157
Family net_family_tcp_server(void);
158
Family net_family_tcp_client(void);
159
Family net_family_tcp_ipv4(void);
160
Family net_family_tcp_ipv6(void);
161
Family net_family_tox_tcp_ipv4(void);
162
Family net_family_tox_tcp_ipv6(void);
163
164
const char *_Nonnull net_family_to_string(Family family);
165
166
uint32_t net_htonl(uint32_t hostlong);
167
uint16_t net_htons(uint16_t hostshort);
168
uint32_t net_ntohl(uint32_t hostlong);
169
uint16_t net_ntohs(uint16_t hostshort);
170
171
const char *_Nullable net_inet_ntop4(const IP4 *_Nonnull addr, char *_Nonnull buf, size_t bufsize);
172
const char *_Nullable net_inet_ntop6(const IP6 *_Nonnull addr, char *_Nonnull buf, size_t bufsize);
173
int net_inet_pton4(const char *_Nonnull addr_string, IP4 *_Nonnull addrbuf);
174
int net_inet_pton6(const char *_Nonnull addr_string, IP6 *_Nonnull addrbuf);
175
176
bool net_should_ignore_recv_error(int err);
177
bool net_should_ignore_connect_error(int err);
178
179
size_t net_pack_bool(uint8_t *_Nonnull bytes, bool v);
180
size_t net_pack_u16(uint8_t *_Nonnull bytes, uint16_t v);
181
size_t net_pack_u32(uint8_t *_Nonnull bytes, uint32_t v);
182
size_t net_pack_u64(uint8_t *_Nonnull bytes, uint64_t v);
183
184
size_t net_unpack_bool(const uint8_t *_Nonnull bytes, bool *_Nonnull v);
185
size_t net_unpack_u16(const uint8_t *_Nonnull bytes, uint16_t *_Nonnull v);
186
size_t net_unpack_u32(const uint8_t *_Nonnull bytes, uint32_t *_Nonnull v);
187
size_t net_unpack_u64(const uint8_t *_Nonnull bytes, uint64_t *_Nonnull v);
188
189
int net_error(void);
190
void net_clear_error(void);
191
192
2.24k
#define NET_STRERROR_SIZE 256
193
194
/** @brief Contains a null terminated formatted error message.
195
 *
196
 * This struct should not contain more than at most the 2 fields.
197
 */
198
typedef struct Net_Strerror {
199
    char     data[NET_STRERROR_SIZE];
200
    uint16_t size;
201
} Net_Strerror;
202
203
/** @brief Get a text explanation for the error code from `net_error()`.
204
 *
205
 * @param error The error code to get a string for.
206
 * @param buf The struct to store the error message in (usually on stack).
207
 *
208
 * @return pointer to a NULL-terminated string describing the error code.
209
 */
210
char *_Nonnull net_strerror(int error, Net_Strerror *_Nonnull buf);
211
212
IP4 net_get_ip4_loopback(void);
213
IP4 net_get_ip4_broadcast(void);
214
215
IP6 net_get_ip6_loopback(void);
216
IP6 net_get_ip6_broadcast(void);
217
218
bool net_join_multicast(const Network *_Nonnull ns, Socket sock, Family family);
219
220
bool net_set_socket_nonblock(const Network *_Nonnull ns, Socket sock);
221
bool net_set_socket_nosigpipe(const Network *_Nonnull ns, Socket sock);
222
bool net_set_socket_reuseaddr(const Network *_Nonnull ns, Socket sock);
223
bool net_set_socket_dualstack(const Network *_Nonnull ns, Socket sock);
224
bool net_set_socket_buffer_size(const Network *_Nonnull ns, Socket sock, int size);
225
bool net_set_socket_broadcast(const Network *_Nonnull ns, Socket sock);
226
227
#ifdef __cplusplus
228
} /* extern "C" */
229
#endif
230
231
#endif /* C_TOXCORE_TOXCORE_NET_H */