Coverage Report

Created: 2026-02-06 10:54

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
21.3k
{
10
21.3k
    return (force int)sock.value;
11
21.3k
}
12
13
Socket net_socket_from_native(int sock)
14
6.33k
{
15
6.33k
    const Socket res = {(force Socket_Value)sock};
16
6.33k
    return res;
17
6.33k
}
18
19
int ns_close(const Network *ns, Socket sock)
20
1.76k
{
21
1.76k
    return ns->funcs->close(ns->obj, sock);
22
1.76k
}
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.40k
{
31
3.40k
    return ns->funcs->bind(ns->obj, sock, addr);
32
3.40k
}
33
34
int ns_listen(const Network *ns, Socket sock, int backlog)
35
132
{
36
132
    return ns->funcs->listen(ns->obj, sock, backlog);
37
132
}
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.39k
{
56
2.39k
    return ns->funcs->recvfrom(ns->obj, sock, buf, len, addr);
57
2.39k
}
58
59
int ns_send(const Network *ns, Socket sock, const uint8_t *buf, size_t len)
60
101
{
61
101
    return ns->funcs->send(ns->obj, sock, buf, len);
62
101
}
63
64
int ns_sendto(const Network *ns, Socket sock, const uint8_t *buf, size_t len, const IP_Port *addr)
65
696
{
66
696
    return ns->funcs->sendto(ns->obj, sock, buf, len, addr);
67
696
}
68
69
Socket ns_socket(const Network *ns, int domain, int type, int proto)
70
1.86k
{
71
1.86k
    return ns->funcs->socket(ns->obj, domain, type, proto);
72
1.86k
}
73
74
int ns_socket_nonblock(const Network *ns, Socket sock, bool nonblock)
75
1.86k
{
76
1.86k
    return ns->funcs->socket_nonblock(ns->obj, sock, nonblock);
77
1.86k
}
78
79
int ns_getsockopt(const Network *ns, Socket sock, int level, int optname, void *optval, size_t *optlen)
80
1.65k
{
81
1.65k
    return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen);
82
1.65k
}
83
84
int ns_setsockopt(const Network *ns, Socket sock, int level, int optname, const void *optval, size_t optlen)
85
6.61k
{
86
6.61k
    return ns->funcs->setsockopt(ns->obj, sock, level, optname, optval, optlen);
87
6.61k
}
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.61k
{
130
2.61k
    *v = bytes[0] != 0;
131
2.61k
    return 1;
132
2.61k
}
133
134
size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v)
135
1.35k
{
136
1.35k
    const uint8_t hi = bytes[0];
137
1.35k
    const uint8_t lo = bytes[1];
138
1.35k
    *v = ((uint16_t)hi << 8) | lo;
139
1.35k
    return sizeof(*v);
140
1.35k
}
141
142
size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v)
143
676
{
144
676
    const uint8_t *p = bytes;
145
676
    uint16_t hi;
146
676
    uint16_t lo;
147
676
    p += net_unpack_u16(p, &hi);
148
676
    p += net_unpack_u16(p, &lo);
149
676
    *v = ((uint32_t)hi << 16) | lo;
150
676
    return p - bytes;
151
676
}
152
153
size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
154
337
{
155
337
    const uint8_t *p = bytes;
156
337
    uint32_t hi;
157
337
    uint32_t lo;
158
337
    p += net_unpack_u32(p, &hi);
159
337
    p += net_unpack_u32(p, &lo);
160
337
    *v = ((uint64_t)hi << 32) | lo;
161
337
    return p - bytes;
162
337
}
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.36k
{
176
3.36k
    return family_unspec;
177
3.36k
}
178
179
Family net_family_ipv4(void)
180
26.5k
{
181
26.5k
    return family_ipv4;
182
26.5k
}
183
184
Family net_family_ipv6(void)
185
9.24k
{
186
9.24k
    return family_ipv6;
187
9.24k
}
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
971
{
201
971
    return family_tcp_ipv4;
202
971
}
203
204
Family net_family_tcp_ipv6(void)
205
6.30k
{
206
6.30k
    return family_tcp_ipv6;
207
6.30k
}
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
107k
{
221
107k
    return family.value == family_unspec.value;
222
107k
}
223
224
bool net_family_is_ipv4(Family family)
225
3.41M
{
226
3.41M
    return family.value == family_ipv4.value;
227
3.41M
}
228
229
bool net_family_is_ipv6(Family family)
230
3.39M
{
231
3.39M
    return family.value == family_ipv6.value;
232
3.39M
}
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.1k
{
246
12.1k
    return family.value == family_tcp_ipv4.value;
247
12.1k
}
248
249
bool net_family_is_tcp_ipv6(Family family)
250
6.59k
{
251
6.59k
    return family.value == family_tcp_ipv6.value;
252
6.59k
}
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
}