Coverage Report

Created: 2026-02-01 10:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/c-toxcore/toxcore/net.c
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
#include "net.h"
7
8
int net_socket_to_native(Socket sock)
9
19.9k
{
10
19.9k
    return (force int)sock.value;
11
19.9k
}
12
13
Socket net_socket_from_native(int sock)
14
5.98k
{
15
5.98k
    const Socket res = {(force Socket_Value)sock};
16
5.98k
    return res;
17
5.98k
}
18
19
int ns_close(const Network *ns, Socket sock)
20
1.63k
{
21
1.63k
    return ns->funcs->close(ns->obj, sock);
22
1.63k
}
23
24
Socket ns_accept(const Network *ns, Socket sock)
25
0
{
26
0
    return ns->funcs->accept(ns->obj, sock);
27
0
}
28
29
int ns_bind(const Network *ns, Socket sock, const IP_Port *addr)
30
3.26k
{
31
3.26k
    return ns->funcs->bind(ns->obj, sock, addr);
32
3.26k
}
33
34
int ns_listen(const Network *ns, Socket sock, int backlog)
35
102
{
36
102
    return ns->funcs->listen(ns->obj, sock, backlog);
37
102
}
38
39
int ns_connect(const Network *ns, Socket sock, const IP_Port *addr)
40
0
{
41
0
    return ns->funcs->connect(ns->obj, sock, addr);
42
0
}
43
44
int ns_recvbuf(const Network *ns, Socket sock)
45
0
{
46
0
    return ns->funcs->recvbuf(ns->obj, sock);
47
0
}
48
49
int ns_recv(const Network *ns, Socket sock, uint8_t *buf, size_t len)
50
0
{
51
0
    return ns->funcs->recv(ns->obj, sock, buf, len);
52
0
}
53
54
int ns_recvfrom(const Network *ns, Socket sock, uint8_t *buf, size_t len, IP_Port *addr)
55
2.34k
{
56
2.34k
    return ns->funcs->recvfrom(ns->obj, sock, buf, len, addr);
57
2.34k
}
58
59
int ns_send(const Network *ns, Socket sock, const uint8_t *buf, size_t len)
60
17
{
61
17
    return ns->funcs->send(ns->obj, sock, buf, len);
62
17
}
63
64
int ns_sendto(const Network *ns, Socket sock, const uint8_t *buf, size_t len, const IP_Port *addr)
65
198
{
66
198
    return ns->funcs->sendto(ns->obj, sock, buf, len, addr);
67
198
}
68
69
Socket ns_socket(const Network *ns, int domain, int type, int proto)
70
1.73k
{
71
1.73k
    return ns->funcs->socket(ns->obj, domain, type, proto);
72
1.73k
}
73
74
int ns_socket_nonblock(const Network *ns, Socket sock, bool nonblock)
75
1.73k
{
76
1.73k
    return ns->funcs->socket_nonblock(ns->obj, sock, nonblock);
77
1.73k
}
78
79
int ns_getsockopt(const Network *ns, Socket sock, int level, int optname, void *optval, size_t *optlen)
80
1.60k
{
81
1.60k
    return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen);
82
1.60k
}
83
84
int ns_setsockopt(const Network *ns, Socket sock, int level, int optname, const void *optval, size_t optlen)
85
6.40k
{
86
6.40k
    return ns->funcs->setsockopt(ns->obj, sock, level, optname, optval, optlen);
87
6.40k
}
88
89
int ns_getaddrinfo(const Network *ns, const Memory *mem, const char *address, int family, int protocol, IP_Port **addrs)
90
0
{
91
0
    return ns->funcs->getaddrinfo(ns->obj, mem, address, family, protocol, addrs);
92
0
}
93
94
int ns_freeaddrinfo(const Network *ns, const Memory *mem, IP_Port *addrs)
95
0
{
96
0
    return ns->funcs->freeaddrinfo(ns->obj, mem, addrs);
97
0
}
98
99
size_t net_pack_bool(uint8_t *bytes, bool v)
100
0
{
101
0
    bytes[0] = v ? 1 : 0;
102
0
    return 1;
103
0
}
104
105
size_t net_pack_u16(uint8_t *bytes, uint16_t v)
106
404
{
107
404
    bytes[0] = (v >> 8) & 0xff;
108
404
    bytes[1] = v & 0xff;
109
404
    return sizeof(v);
110
404
}
111
112
size_t net_pack_u32(uint8_t *bytes, uint32_t v)
113
202
{
114
202
    uint8_t *p = bytes;
115
202
    p += net_pack_u16(p, (v >> 16) & 0xffff);
116
202
    p += net_pack_u16(p, v & 0xffff);
117
202
    return p - bytes;
118
202
}
119
120
size_t net_pack_u64(uint8_t *bytes, uint64_t v)
121
101
{
122
101
    uint8_t *p = bytes;
123
101
    p += net_pack_u32(p, (v >> 32) & 0xffffffff);
124
101
    p += net_pack_u32(p, v & 0xffffffff);
125
101
    return p - bytes;
126
101
}
127
128
size_t net_unpack_bool(const uint8_t *bytes, bool *v)
129
2.59k
{
130
2.59k
    *v = bytes[0] != 0;
131
2.59k
    return 1;
132
2.59k
}
133
134
size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
135
1.37k
{
136
1.37k
    const uint8_t hi = bytes[0];
137
1.37k
    const uint8_t lo = bytes[1];
138
1.37k
    *v = ((uint16_t)hi << 8) | lo;
139
1.37k
    return sizeof(*v);
140
1.37k
}
141
142
size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
143
686
{
144
686
    const uint8_t *p = bytes;
145
686
    uint16_t hi;
146
686
    uint16_t lo;
147
686
    p += net_unpack_u16(p, &hi);
148
686
    p += net_unpack_u16(p, &lo);
149
686
    *v = ((uint32_t)hi << 16) | lo;
150
686
    return p - bytes;
151
686
}
152
153
size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
154
342
{
155
342
    const uint8_t *p = bytes;
156
342
    uint32_t hi;
157
342
    uint32_t lo;
158
342
    p += net_unpack_u32(p, &hi);
159
342
    p += net_unpack_u32(p, &lo);
160
342
    *v = ((uint64_t)hi << 32) | lo;
161
342
    return p - bytes;
162
342
}
163
164
static const Family family_unspec = {TOX_AF_UNSPEC};
165
static const Family family_ipv4 = {TOX_AF_INET};
166
static const Family family_ipv6 = {TOX_AF_INET6};
167
static const Family family_tcp_server = {TCP_SERVER_FAMILY};
168
static const Family family_tcp_client = {TCP_CLIENT_FAMILY};
169
static const Family family_tcp_ipv4 = {TCP_INET};
170
static const Family family_tcp_ipv6 = {TCP_INET6};
171
static const Family family_tox_tcp_ipv4 = {TOX_TCP_INET};
172
static const Family family_tox_tcp_ipv6 = {TOX_TCP_INET6};
173
174
Family net_family_unspec(void)
175
3.30k
{
176
3.30k
    return family_unspec;
177
3.30k
}
178
179
Family net_family_ipv4(void)
180
25.3k
{
181
25.3k
    return family_ipv4;
182
25.3k
}
183
184
Family net_family_ipv6(void)
185
8.16k
{
186
8.16k
    return family_ipv6;
187
8.16k
}
188
189
Family net_family_tcp_server(void)
190
0
{
191
0
    return family_tcp_server;
192
0
}
193
194
Family net_family_tcp_client(void)
195
0
{
196
0
    return family_tcp_client;
197
0
}
198
199
Family net_family_tcp_ipv4(void)
200
983
{
201
983
    return family_tcp_ipv4;
202
983
}
203
204
Family net_family_tcp_ipv6(void)
205
6.28k
{
206
6.28k
    return family_tcp_ipv6;
207
6.28k
}
208
209
Family net_family_tox_tcp_ipv4(void)
210
0
{
211
0
    return family_tox_tcp_ipv4;
212
0
}
213
214
Family net_family_tox_tcp_ipv6(void)
215
0
{
216
0
    return family_tox_tcp_ipv6;
217
0
}
218
219
bool net_family_is_unspec(Family family)
220
43.5k
{
221
43.5k
    return family.value == family_unspec.value;
222
43.5k
}
223
224
bool net_family_is_ipv4(Family family)
225
3.35M
{
226
3.35M
    return family.value == family_ipv4.value;
227
3.35M
}
228
229
bool net_family_is_ipv6(Family family)
230
3.33M
{
231
3.33M
    return family.value == family_ipv6.value;
232
3.33M
}
233
234
bool net_family_is_tcp_server(Family family)
235
0
{
236
0
    return family.value == family_tcp_server.value;
237
0
}
238
239
bool net_family_is_tcp_client(Family family)
240
0
{
241
0
    return family.value == family_tcp_client.value;
242
0
}
243
244
bool net_family_is_tcp_ipv4(Family family)
245
12.0k
{
246
12.0k
    return family.value == family_tcp_ipv4.value;
247
12.0k
}
248
249
bool net_family_is_tcp_ipv6(Family family)
250
6.51k
{
251
6.51k
    return family.value == family_tcp_ipv6.value;
252
6.51k
}
253
254
bool net_family_is_tox_tcp_ipv4(Family family)
255
0
{
256
0
    return family.value == family_tox_tcp_ipv4.value;
257
0
}
258
259
bool net_family_is_tox_tcp_ipv6(Family family)
260
0
{
261
0
    return family.value == family_tox_tcp_ipv6.value;
262
0
}
263
264
const char *net_family_to_string(Family family)
265
0
{
266
0
    if (net_family_is_unspec(family)) {
267
0
        return "TOX_AF_UNSPEC";
268
0
    }
269
270
0
    if (net_family_is_ipv4(family)) {
271
0
        return "TOX_AF_INET";
272
0
    }
273
274
0
    if (net_family_is_ipv6(family)) {
275
0
        return "TOX_AF_INET6";
276
0
    }
277
278
0
    if (net_family_is_tcp_server(family)) {
279
0
        return "TCP_SERVER_FAMILY";
280
0
    }
281
282
0
    if (net_family_is_tcp_client(family)) {
283
0
        return "TCP_CLIENT_FAMILY";
284
0
    }
285
286
0
    if (net_family_is_tcp_ipv4(family)) {
287
0
        return "TCP_INET";
288
0
    }
289
290
0
    if (net_family_is_tcp_ipv6(family)) {
291
0
        return "TCP_INET6";
292
0
    }
293
294
0
    if (net_family_is_tox_tcp_ipv4(family)) {
295
0
        return "TOX_TCP_INET";
296
0
    }
297
298
0
    if (net_family_is_tox_tcp_ipv6(family)) {
299
0
        return "TOX_TCP_INET6";
300
0
    }
301
302
0
    return "<invalid Family>";
303
0
}