Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2025 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Functions for the core networking. |
8 | | */ |
9 | | |
10 | | #ifdef __APPLE__ |
11 | | #define _DARWIN_C_SOURCE |
12 | | #endif /* __APPLE__ */ |
13 | | |
14 | | // For Solaris. |
15 | | #ifdef __sun |
16 | | #define __EXTENSIONS__ 1 |
17 | | #endif /* __sun */ |
18 | | |
19 | | // For Linux (and some BSDs). |
20 | | #ifndef _XOPEN_SOURCE |
21 | | #define _XOPEN_SOURCE 700 |
22 | | #endif /* _XOPEN_SOURCE */ |
23 | | |
24 | | #if defined(_WIN32) && defined(_WIN32_WINNT) && defined(_WIN32_WINNT_WINXP) && _WIN32_WINNT >= _WIN32_WINNT_WINXP |
25 | | #undef _WIN32_WINNT |
26 | | #define _WIN32_WINNT 0x501 |
27 | | #endif /* defined(_WIN32) && defined(_WIN32_WINNT) && defined(_WIN32_WINNT_WINXP) && _WIN32_WINNT >= _WIN32_WINNT_WINXP */ |
28 | | |
29 | | #if !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) |
30 | | #define OS_WIN32 |
31 | | #endif /* !defined(OS_WIN32) && (defined(_WIN32) || defined(__WIN32__) || defined(WIN32)) */ |
32 | | |
33 | | #if defined(OS_WIN32) && !defined(WINVER) |
34 | | // Windows XP |
35 | | #define WINVER 0x0501 |
36 | | #endif /* defined(OS_WIN32) && !defined(WINVER) */ |
37 | | |
38 | | #include "network.h" |
39 | | |
40 | | #ifdef OS_WIN32 // Put win32 includes here |
41 | | // The mingw32/64 Windows library warns about including winsock2.h after |
42 | | // windows.h even though with the above it's a valid thing to do. So, to make |
43 | | // mingw32 headers happy, we include winsock2.h first. |
44 | | #include <winsock2.h> |
45 | | // Comment line here to avoid reordering by source code formatters. |
46 | | #include <windows.h> |
47 | | #include <ws2tcpip.h> |
48 | | #endif /* OS_WIN32 */ |
49 | | |
50 | | #ifdef __APPLE__ |
51 | | #include <mach/clock.h> |
52 | | #include <mach/mach.h> |
53 | | #endif /* __APPLE__ */ |
54 | | |
55 | | #if !defined(OS_WIN32) |
56 | | #include <arpa/inet.h> |
57 | | #include <errno.h> |
58 | | #include <fcntl.h> |
59 | | #include <netdb.h> |
60 | | #include <netinet/in.h> |
61 | | #include <sys/ioctl.h> |
62 | | #include <sys/socket.h> |
63 | | #include <sys/time.h> |
64 | | #include <sys/types.h> |
65 | | #include <unistd.h> |
66 | | |
67 | | #ifdef __sun |
68 | | #include <stropts.h> |
69 | | #include <sys/filio.h> |
70 | | #endif /* __sun */ |
71 | | |
72 | | #else |
73 | | #ifndef IPV6_V6ONLY |
74 | | #define IPV6_V6ONLY 27 |
75 | | #endif /* IPV6_V6ONLY */ |
76 | | #endif /* !defined(OS_WIN32) */ |
77 | | |
78 | | #include <assert.h> |
79 | | #include <limits.h> |
80 | | #include <stdio.h> |
81 | | #include <stdlib.h> |
82 | | #include <string.h> |
83 | | |
84 | | #include "attributes.h" |
85 | | #include "bin_pack.h" |
86 | | #include "ccompat.h" |
87 | | #include "logger.h" |
88 | | #include "mem.h" |
89 | | #include "net_log.h" |
90 | | #include "net_profile.h" |
91 | | #include "util.h" |
92 | | |
93 | | // Disable MSG_NOSIGNAL on systems not supporting it, e.g. Windows, FreeBSD |
94 | | #if !defined(MSG_NOSIGNAL) |
95 | | #define MSG_NOSIGNAL 0 |
96 | | #endif /* !defined(MSG_NOSIGNAL) */ |
97 | | |
98 | | #ifndef IPV6_ADD_MEMBERSHIP |
99 | | #ifdef IPV6_JOIN_GROUP |
100 | | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP |
101 | | #endif /* IPV6_JOIN_GROUP */ |
102 | | #endif /* IPV6_ADD_MEMBERSHIP */ |
103 | | |
104 | | static_assert(sizeof(IP4) == SIZE_IP4, "IP4 size must be 4"); |
105 | | |
106 | | // TODO(iphydf): Stop relying on this. We memcpy this struct (and IP4 above) |
107 | | // into packets but really should be serialising it properly. |
108 | | static_assert(sizeof(IP6) == SIZE_IP6, "IP6 size must be 16"); |
109 | | |
110 | | #if !defined(OS_WIN32) |
111 | | |
112 | | static bool should_ignore_recv_error(int err) |
113 | 130k | { |
114 | 130k | return err == EWOULDBLOCK; |
115 | 130k | } |
116 | | |
117 | | static bool should_ignore_connect_error(int err) |
118 | 69 | { |
119 | 69 | return err == EWOULDBLOCK || err == EINPROGRESS; |
120 | 69 | } |
121 | | |
122 | | static const char *inet_ntop4(const struct in_addr *_Nonnull addr, char *_Nonnull buf, size_t bufsize) |
123 | 2.71M | { |
124 | 2.71M | return inet_ntop(AF_INET, addr, buf, bufsize); |
125 | 2.71M | } |
126 | | |
127 | | static const char *inet_ntop6(const struct in6_addr *_Nonnull addr, char *_Nonnull buf, size_t bufsize) |
128 | 1.40k | { |
129 | 1.40k | return inet_ntop(AF_INET6, addr, buf, bufsize); |
130 | 1.40k | } |
131 | | |
132 | | static int inet_pton4(const char *_Nonnull addr_string, struct in_addr *_Nonnull addrbuf) |
133 | 27.6k | { |
134 | 27.6k | return inet_pton(AF_INET, addr_string, addrbuf); |
135 | 27.6k | } |
136 | | |
137 | | static int inet_pton6(const char *_Nonnull addr_string, struct in6_addr *_Nonnull addrbuf) |
138 | 553 | { |
139 | 553 | return inet_pton(AF_INET6, addr_string, addrbuf); |
140 | 553 | } |
141 | | |
142 | | #else |
143 | | #ifndef IPV6_V6ONLY |
144 | | #define IPV6_V6ONLY 27 |
145 | | #endif /* IPV6_V6ONLY */ |
146 | | |
147 | | static bool should_ignore_recv_error(int err) |
148 | | { |
149 | | // We ignore WSAECONNRESET as Windows helpfully* sends that error if a |
150 | | // previously sent UDP packet wasn't delivered. |
151 | | return err == WSAEWOULDBLOCK || err == WSAECONNRESET; |
152 | | } |
153 | | |
154 | | static bool should_ignore_connect_error(int err) |
155 | | { |
156 | | return err == WSAEWOULDBLOCK || err == WSAEINPROGRESS; |
157 | | } |
158 | | |
159 | | static const char *inet_ntop4(const struct in_addr *_Nonnull addr, char *_Nonnull buf, size_t bufsize) |
160 | | { |
161 | | struct sockaddr_in saddr = {0}; |
162 | | |
163 | | saddr.sin_family = AF_INET; |
164 | | saddr.sin_addr = *addr; |
165 | | |
166 | | DWORD len = bufsize; |
167 | | |
168 | | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { |
169 | | return nullptr; |
170 | | } |
171 | | |
172 | | return buf; |
173 | | } |
174 | | |
175 | | static const char *inet_ntop6(const struct in6_addr *_Nonnull addr, char *_Nonnull buf, size_t bufsize) |
176 | | { |
177 | | struct sockaddr_in6 saddr = {0}; |
178 | | |
179 | | saddr.sin6_family = AF_INET6; |
180 | | saddr.sin6_addr = *addr; |
181 | | |
182 | | DWORD len = bufsize; |
183 | | |
184 | | if (WSAAddressToString((LPSOCKADDR)&saddr, sizeof(saddr), nullptr, buf, &len)) { |
185 | | return nullptr; |
186 | | } |
187 | | |
188 | | return buf; |
189 | | } |
190 | | |
191 | | static int inet_pton4(const char *_Nonnull addrString, struct in_addr *_Nonnull addrbuf) |
192 | | { |
193 | | struct sockaddr_in saddr = {0}; |
194 | | |
195 | | INT len = sizeof(saddr); |
196 | | |
197 | | if (WSAStringToAddress((LPTSTR)addrString, AF_INET, nullptr, (LPSOCKADDR)&saddr, &len)) { |
198 | | return 0; |
199 | | } |
200 | | |
201 | | *addrbuf = saddr.sin_addr; |
202 | | |
203 | | return 1; |
204 | | } |
205 | | |
206 | | static int inet_pton6(const char *_Nonnull addrString, struct in6_addr *_Nonnull addrbuf) |
207 | | { |
208 | | struct sockaddr_in6 saddr = {0}; |
209 | | |
210 | | INT len = sizeof(saddr); |
211 | | |
212 | | if (WSAStringToAddress((LPTSTR)addrString, AF_INET6, nullptr, (LPSOCKADDR)&saddr, &len)) { |
213 | | return 0; |
214 | | } |
215 | | |
216 | | *addrbuf = saddr.sin6_addr; |
217 | | |
218 | | return 1; |
219 | | } |
220 | | |
221 | | #endif /* !defined(OS_WIN32) */ |
222 | | |
223 | | static_assert(TOX_INET6_ADDRSTRLEN >= INET6_ADDRSTRLEN, |
224 | | "TOX_INET6_ADDRSTRLEN should be greater or equal to INET6_ADDRSTRLEN (#INET6_ADDRSTRLEN)"); |
225 | | static_assert(TOX_INET_ADDRSTRLEN >= INET_ADDRSTRLEN, |
226 | | "TOX_INET_ADDRSTRLEN should be greater or equal to INET_ADDRSTRLEN (#INET_ADDRSTRLEN)"); |
227 | | |
228 | | static int make_proto(int proto) |
229 | 3.30k | { |
230 | 3.30k | switch (proto) { |
231 | 94 | case TOX_PROTO_TCP: |
232 | 94 | return IPPROTO_TCP; |
233 | | |
234 | 3.17k | case TOX_PROTO_UDP: |
235 | 3.17k | return IPPROTO_UDP; |
236 | | |
237 | 33 | default: |
238 | 33 | return proto; |
239 | 3.30k | } |
240 | 3.30k | } |
241 | | |
242 | | static int make_socktype(int type) |
243 | 3.85k | { |
244 | 3.85k | switch (type) { |
245 | 127 | case TOX_SOCK_STREAM: |
246 | 127 | return SOCK_STREAM; |
247 | | |
248 | 3.73k | case TOX_SOCK_DGRAM: |
249 | 3.73k | return SOCK_DGRAM; |
250 | | |
251 | 0 | default: |
252 | 0 | return type; |
253 | 3.85k | } |
254 | 3.85k | } |
255 | | |
256 | | static int make_family(Family tox_family) |
257 | 2.72M | { |
258 | 2.72M | switch (tox_family.value) { |
259 | 2.72M | case TOX_AF_INET: |
260 | 2.72M | case TCP_INET: |
261 | 2.72M | return AF_INET; |
262 | | |
263 | 2.80k | case TOX_AF_INET6: |
264 | 2.80k | case TCP_INET6: |
265 | 2.80k | return AF_INET6; |
266 | | |
267 | 3 | case TOX_AF_UNSPEC: |
268 | 3 | return AF_UNSPEC; |
269 | | |
270 | 0 | default: |
271 | 0 | return tox_family.value; |
272 | 2.72M | } |
273 | 2.72M | } |
274 | | |
275 | | static const Family family_unspec = {TOX_AF_UNSPEC}; |
276 | | static const Family family_ipv4 = {TOX_AF_INET}; |
277 | | static const Family family_ipv6 = {TOX_AF_INET6}; |
278 | | static const Family family_tcp_server = {TCP_SERVER_FAMILY}; |
279 | | static const Family family_tcp_client = {TCP_CLIENT_FAMILY}; |
280 | | static const Family family_tcp_ipv4 = {TCP_INET}; |
281 | | static const Family family_tcp_ipv6 = {TCP_INET6}; |
282 | | static const Family family_tox_tcp_ipv4 = {TOX_TCP_INET}; |
283 | | static const Family family_tox_tcp_ipv6 = {TOX_TCP_INET6}; |
284 | | |
285 | | static const Family *make_tox_family(int family) |
286 | 958k | { |
287 | 958k | switch (family) { |
288 | 958k | case AF_INET: |
289 | 958k | return &family_ipv4; |
290 | | |
291 | 493 | case AF_INET6: |
292 | 493 | return &family_ipv6; |
293 | | |
294 | 0 | case AF_UNSPEC: |
295 | 0 | return &family_unspec; |
296 | | |
297 | 0 | default: |
298 | 0 | return nullptr; |
299 | 958k | } |
300 | 958k | } |
301 | | |
302 | | static void get_ip4(IP4 *_Nonnull result, const struct in_addr *_Nonnull addr) |
303 | 984k | { |
304 | 984k | static_assert(sizeof(result->uint32) == sizeof(addr->s_addr), |
305 | 984k | "Tox and operating system don't agree on size of IPv4 addresses"); |
306 | 984k | result->uint32 = addr->s_addr; |
307 | 984k | } |
308 | | |
309 | | static void get_ip6(IP6 *_Nonnull result, const struct in6_addr *_Nonnull addr) |
310 | 6 | { |
311 | 6 | static_assert(sizeof(result->uint8) == sizeof(addr->s6_addr), |
312 | 6 | "Tox and operating system don't agree on size of IPv6 addresses"); |
313 | 6 | memcpy(result->uint8, addr->s6_addr, sizeof(result->uint8)); |
314 | 6 | } |
315 | | |
316 | | static void fill_addr4(const IP4 *_Nonnull ip, struct in_addr *_Nonnull addr) |
317 | 4.57M | { |
318 | 4.57M | addr->s_addr = ip->uint32; |
319 | 4.57M | } |
320 | | |
321 | | static void fill_addr6(const IP6 *_Nonnull ip, struct in6_addr *_Nonnull addr) |
322 | 2.80k | { |
323 | 2.80k | memcpy(addr->s6_addr, ip->uint8, sizeof(ip->uint8)); |
324 | 2.80k | } |
325 | | |
326 | | #if !defined(INADDR_LOOPBACK) |
327 | | #define INADDR_LOOPBACK 0x7f000001 |
328 | | #endif /* !defined(INADDR_LOOPBACK) */ |
329 | | |
330 | | IP4 get_ip4_broadcast(void) |
331 | 207 | { |
332 | 207 | const IP4 ip4_broadcast = { INADDR_BROADCAST }; |
333 | 207 | return ip4_broadcast; |
334 | 207 | } |
335 | | |
336 | | IP6 get_ip6_broadcast(void) |
337 | 0 | { |
338 | 0 | const IP6 ip6_broadcast = { |
339 | 0 | { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } |
340 | 0 | }; |
341 | 0 | return ip6_broadcast; |
342 | 0 | } |
343 | | |
344 | | IP4 get_ip4_loopback(void) |
345 | 103 | { |
346 | 103 | IP4 loopback; |
347 | 103 | loopback.uint32 = htonl(INADDR_LOOPBACK); |
348 | 103 | return loopback; |
349 | 103 | } |
350 | | |
351 | | IP6 get_ip6_loopback(void) |
352 | 3 | { |
353 | | /* in6addr_loopback isn't available everywhere, so we do it ourselves. */ |
354 | 3 | IP6 loopback = {{0}}; |
355 | 3 | loopback.uint8[15] = 1; |
356 | 3 | return loopback; |
357 | 3 | } |
358 | | |
359 | | #ifndef OS_WIN32 |
360 | 5.59k | #define INVALID_SOCKET (-1) |
361 | | #endif /* OS_WIN32 */ |
362 | | |
363 | | int net_socket_to_native(Socket sock) |
364 | 2.29M | { |
365 | 2.29M | return (force int)sock.value; |
366 | 2.29M | } |
367 | | |
368 | | Socket net_socket_from_native(int sock) |
369 | 9.74k | { |
370 | 9.74k | const Socket res = {(force Socket_Value)sock}; |
371 | 9.74k | return res; |
372 | 9.74k | } |
373 | | |
374 | | Socket net_invalid_socket(void) |
375 | 5.59k | { |
376 | 5.59k | return net_socket_from_native(INVALID_SOCKET); |
377 | 5.59k | } |
378 | | |
379 | | Family net_family_unspec(void) |
380 | 227k | { |
381 | 227k | return family_unspec; |
382 | 227k | } |
383 | | |
384 | | Family net_family_ipv4(void) |
385 | 561k | { |
386 | 561k | return family_ipv4; |
387 | 561k | } |
388 | | |
389 | | Family net_family_ipv6(void) |
390 | 6.29k | { |
391 | 6.29k | return family_ipv6; |
392 | 6.29k | } |
393 | | |
394 | | Family net_family_tcp_server(void) |
395 | 1.34k | { |
396 | 1.34k | return family_tcp_server; |
397 | 1.34k | } |
398 | | |
399 | | Family net_family_tcp_client(void) |
400 | 990 | { |
401 | 990 | return family_tcp_client; |
402 | 990 | } |
403 | | |
404 | | Family net_family_tcp_ipv4(void) |
405 | 1.53k | { |
406 | 1.53k | return family_tcp_ipv4; |
407 | 1.53k | } |
408 | | |
409 | | Family net_family_tcp_ipv6(void) |
410 | 7.03k | { |
411 | 7.03k | return family_tcp_ipv6; |
412 | 7.03k | } |
413 | | |
414 | | Family net_family_tox_tcp_ipv4(void) |
415 | 0 | { |
416 | 0 | return family_tox_tcp_ipv4; |
417 | 0 | } |
418 | | |
419 | | Family net_family_tox_tcp_ipv6(void) |
420 | 0 | { |
421 | 0 | return family_tox_tcp_ipv6; |
422 | 0 | } |
423 | | |
424 | | bool net_family_is_unspec(Family family) |
425 | 7.13M | { |
426 | 7.13M | return family.value == family_unspec.value; |
427 | 7.13M | } |
428 | | |
429 | | bool net_family_is_ipv4(Family family) |
430 | 136M | { |
431 | 136M | return family.value == family_ipv4.value; |
432 | 136M | } |
433 | | |
434 | | bool net_family_is_ipv6(Family family) |
435 | 121M | { |
436 | 121M | return family.value == family_ipv6.value; |
437 | 121M | } |
438 | | |
439 | | bool net_family_is_tcp_server(Family family) |
440 | 1.04k | { |
441 | 1.04k | return family.value == family_tcp_server.value; |
442 | 1.04k | } |
443 | | |
444 | | bool net_family_is_tcp_client(Family family) |
445 | 803 | { |
446 | 803 | return family.value == family_tcp_client.value; |
447 | 803 | } |
448 | | |
449 | | bool net_family_is_tcp_ipv4(Family family) |
450 | 36.5k | { |
451 | 36.5k | return family.value == family_tcp_ipv4.value; |
452 | 36.5k | } |
453 | | |
454 | | bool net_family_is_tcp_ipv6(Family family) |
455 | 27.7k | { |
456 | 27.7k | return family.value == family_tcp_ipv6.value; |
457 | 27.7k | } |
458 | | |
459 | | bool net_family_is_tox_tcp_ipv4(Family family) |
460 | 1.79k | { |
461 | 1.79k | return family.value == family_tox_tcp_ipv4.value; |
462 | 1.79k | } |
463 | | |
464 | | bool net_family_is_tox_tcp_ipv6(Family family) |
465 | 0 | { |
466 | 0 | return family.value == family_tox_tcp_ipv6.value; |
467 | 0 | } |
468 | | |
469 | | bool sock_valid(Socket sock) |
470 | 5.59k | { |
471 | 5.59k | const Socket invalid_socket = net_invalid_socket(); |
472 | 5.59k | return sock.value != invalid_socket.value; |
473 | 5.59k | } |
474 | | |
475 | | struct Network_Addr { |
476 | | struct sockaddr_storage addr; |
477 | | size_t size; |
478 | | }; |
479 | | |
480 | | static int sys_close(void *_Nonnull obj, Socket sock) |
481 | 990 | { |
482 | | #if defined(OS_WIN32) |
483 | | return closesocket(net_socket_to_native(sock)); |
484 | | #else // !OS_WIN32 |
485 | 990 | return close(net_socket_to_native(sock)); |
486 | 990 | #endif /* OS_WIN32 */ |
487 | 990 | } |
488 | | |
489 | | static Socket sys_accept(void *_Nonnull obj, Socket sock) |
490 | 2.27k | { |
491 | 2.27k | return net_socket_from_native(accept(net_socket_to_native(sock), nullptr, nullptr)); |
492 | 2.27k | } |
493 | | |
494 | | static int sys_bind(void *_Nonnull obj, Socket sock, const Network_Addr *_Nonnull addr) |
495 | 61.1k | { |
496 | 61.1k | return bind(net_socket_to_native(sock), (const struct sockaddr *)&addr->addr, addr->size); |
497 | 61.1k | } |
498 | | |
499 | | static int sys_listen(void *_Nonnull obj, Socket sock, int backlog) |
500 | 19 | { |
501 | 19 | return listen(net_socket_to_native(sock), backlog); |
502 | 19 | } |
503 | | |
504 | | static int sys_connect(void *_Nonnull obj, Socket sock, const Network_Addr *_Nonnull addr) |
505 | 75 | { |
506 | 75 | return connect(net_socket_to_native(sock), (const struct sockaddr *)&addr->addr, addr->size); |
507 | 75 | } |
508 | | |
509 | | static int sys_recvbuf(void *_Nonnull obj, Socket sock) |
510 | 33.5k | { |
511 | | #ifdef OS_WIN32 |
512 | | u_long count = 0; |
513 | | ioctlsocket(net_socket_to_native(sock), FIONREAD, &count); |
514 | | #else |
515 | 33.5k | int count = 0; |
516 | 33.5k | ioctl(net_socket_to_native(sock), FIONREAD, &count); |
517 | 33.5k | #endif /* OS_WIN32 */ |
518 | | |
519 | 33.5k | return count; |
520 | 33.5k | } |
521 | | |
522 | | static int sys_recv(void *_Nonnull obj, Socket sock, uint8_t *_Nonnull buf, size_t len) |
523 | 8.77k | { |
524 | 8.77k | return recv(net_socket_to_native(sock), (char *)buf, len, MSG_NOSIGNAL); |
525 | 8.77k | } |
526 | | |
527 | | static int sys_send(void *_Nonnull obj, Socket sock, const uint8_t *_Nonnull buf, size_t len) |
528 | 5.55k | { |
529 | 5.55k | return send(net_socket_to_native(sock), (const char *)buf, len, MSG_NOSIGNAL); |
530 | 5.55k | } |
531 | | |
532 | | static int sys_sendto(void *_Nonnull obj, Socket sock, const uint8_t *_Nonnull buf, size_t len, const Network_Addr *_Nonnull addr) |
533 | 1.08M | { |
534 | 1.08M | return sendto(net_socket_to_native(sock), (const char *)buf, len, 0, (const struct sockaddr *)&addr->addr, addr->size); |
535 | 1.08M | } |
536 | | |
537 | | static int sys_recvfrom(void *_Nonnull obj, Socket sock, uint8_t *_Nonnull buf, size_t len, Network_Addr *_Nonnull addr) |
538 | 1.08M | { |
539 | 1.08M | socklen_t size = addr->size; |
540 | 1.08M | const int ret = recvfrom(net_socket_to_native(sock), (char *)buf, len, 0, (struct sockaddr *)&addr->addr, &size); |
541 | 1.08M | addr->size = size; |
542 | 1.08M | return ret; |
543 | 1.08M | } |
544 | | |
545 | | static Socket sys_socket(void *_Nonnull obj, int domain, int type, int proto) |
546 | 1.88k | { |
547 | 1.88k | return net_socket_from_native(socket(domain, type, proto)); |
548 | 1.88k | } |
549 | | |
550 | | static int sys_socket_nonblock(void *_Nonnull obj, Socket sock, bool nonblock) |
551 | 1.89k | { |
552 | | #ifdef OS_WIN32 |
553 | | u_long mode = nonblock ? 1 : 0; |
554 | | return ioctlsocket(net_socket_to_native(sock), FIONBIO, &mode); |
555 | | #else |
556 | 1.89k | return fcntl(net_socket_to_native(sock), F_SETFL, O_NONBLOCK, nonblock ? 1 : 0); |
557 | 1.89k | #endif /* OS_WIN32 */ |
558 | 1.89k | } |
559 | | |
560 | | static int sys_getsockopt(void *_Nonnull obj, Socket sock, int level, int optname, void *_Nonnull optval, size_t *_Nonnull optlen) |
561 | 5 | { |
562 | 5 | socklen_t len = *optlen; |
563 | 5 | const int ret = getsockopt(net_socket_to_native(sock), level, optname, (char *)optval, &len); |
564 | 5 | *optlen = len; |
565 | 5 | return ret; |
566 | 5 | } |
567 | | |
568 | | static int sys_setsockopt(void *_Nonnull obj, Socket sock, int level, int optname, const void *_Nonnull optval, size_t optlen) |
569 | 5.23k | { |
570 | | #ifdef EMSCRIPTEN |
571 | | return 0; |
572 | | #else |
573 | 5.23k | return setsockopt(net_socket_to_native(sock), level, optname, (const char *)optval, optlen); |
574 | 5.23k | #endif /* EMSCRIPTEN */ |
575 | 5.23k | } |
576 | | |
577 | | // sets and fills an array of addrs for address |
578 | | // returns the number of entries in addrs |
579 | | static int sys_getaddrinfo(void *_Nonnull obj, const Memory *_Nonnull mem, const char *_Nonnull address, int family, int sock_type, Network_Addr **_Nonnull addrs) |
580 | 566 | { |
581 | 566 | assert(addrs != nullptr); |
582 | | |
583 | 566 | struct addrinfo hints = {0}; |
584 | 566 | hints.ai_family = family; |
585 | | |
586 | | |
587 | | // different platforms favour a different field |
588 | | // hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. |
589 | 566 | hints.ai_socktype = sock_type; |
590 | | // hints.ai_protocol = protocol; |
591 | | |
592 | 566 | struct addrinfo *infos = nullptr; |
593 | | |
594 | 566 | const int rc = getaddrinfo(address, nullptr, &hints, &infos); |
595 | | |
596 | | // Lookup failed. |
597 | 566 | if (rc != 0) { |
598 | | // TODO(Green-Sky): log error |
599 | 60 | return 0; |
600 | 60 | } |
601 | | |
602 | 506 | const int32_t max_count = INT32_MAX / sizeof(Network_Addr); |
603 | | |
604 | | // we count number of "valid" results |
605 | 506 | int result = 0; |
606 | 1.54k | for (struct addrinfo *walker = infos; walker != nullptr && result < max_count; walker = walker->ai_next) { |
607 | 1.04k | if (walker->ai_family == family || family == AF_UNSPEC) { |
608 | 1.04k | ++result; |
609 | 1.04k | } |
610 | | |
611 | | // do we need to check socktype/protocol? |
612 | 1.04k | } |
613 | | |
614 | 506 | assert(max_count >= result); |
615 | | |
616 | 506 | Network_Addr *tmp_addrs = (Network_Addr *)mem_valloc(mem, result, sizeof(Network_Addr)); |
617 | 506 | if (tmp_addrs == nullptr) { |
618 | 0 | freeaddrinfo(infos); |
619 | 0 | return 0; |
620 | 0 | } |
621 | | |
622 | | // now we fill in |
623 | 506 | int i = 0; |
624 | 1.54k | for (struct addrinfo *walker = infos; walker != nullptr; walker = walker->ai_next) { |
625 | 1.04k | if (walker->ai_family == family || family == AF_UNSPEC) { |
626 | 1.04k | tmp_addrs[i].size = sizeof(struct sockaddr_storage); |
627 | 1.04k | tmp_addrs[i].addr.ss_family = walker->ai_family; |
628 | | |
629 | | // according to spec, storage is supposed to be large enough (and source shows they are) |
630 | | // storage is 128 bytes |
631 | 1.04k | assert(walker->ai_addrlen <= tmp_addrs[i].size); |
632 | | |
633 | 1.04k | memcpy(&tmp_addrs[i].addr, walker->ai_addr, walker->ai_addrlen); |
634 | 1.04k | tmp_addrs[i].size = walker->ai_addrlen; |
635 | | |
636 | 1.04k | ++i; |
637 | 1.04k | } |
638 | 1.04k | } |
639 | | |
640 | 506 | assert(i == result); |
641 | | |
642 | 506 | freeaddrinfo(infos); |
643 | | |
644 | 506 | *addrs = tmp_addrs; |
645 | | |
646 | | // number of entries in addrs |
647 | 506 | return result; |
648 | 506 | } Unexecuted instantiation: network.c:sys_getaddrinfo network.c:sys_getaddrinfo Line | Count | Source | 580 | 566 | { | 581 | 566 | assert(addrs != nullptr); | 582 | | | 583 | 566 | struct addrinfo hints = {0}; | 584 | 566 | hints.ai_family = family; | 585 | | | 586 | | | 587 | | // different platforms favour a different field | 588 | | // hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | 589 | 566 | hints.ai_socktype = sock_type; | 590 | | // hints.ai_protocol = protocol; | 591 | | | 592 | 566 | struct addrinfo *infos = nullptr; | 593 | | | 594 | 566 | const int rc = getaddrinfo(address, nullptr, &hints, &infos); | 595 | | | 596 | | // Lookup failed. | 597 | 566 | if (rc != 0) { | 598 | | // TODO(Green-Sky): log error | 599 | 60 | return 0; | 600 | 60 | } | 601 | | | 602 | 506 | const int32_t max_count = INT32_MAX / sizeof(Network_Addr); | 603 | | | 604 | | // we count number of "valid" results | 605 | 506 | int result = 0; | 606 | 1.54k | for (struct addrinfo *walker = infos; walker != nullptr && result < max_count; walker = walker->ai_next) { | 607 | 1.04k | if (walker->ai_family == family || family == AF_UNSPEC) { | 608 | 1.04k | ++result; | 609 | 1.04k | } | 610 | | | 611 | | // do we need to check socktype/protocol? | 612 | 1.04k | } | 613 | | | 614 | 506 | assert(max_count >= result); | 615 | | | 616 | 506 | Network_Addr *tmp_addrs = (Network_Addr *)mem_valloc(mem, result, sizeof(Network_Addr)); | 617 | 506 | if (tmp_addrs == nullptr) { | 618 | 0 | freeaddrinfo(infos); | 619 | 0 | return 0; | 620 | 0 | } | 621 | | | 622 | | // now we fill in | 623 | 506 | int i = 0; | 624 | 1.54k | for (struct addrinfo *walker = infos; walker != nullptr; walker = walker->ai_next) { | 625 | 1.04k | if (walker->ai_family == family || family == AF_UNSPEC) { | 626 | 1.04k | tmp_addrs[i].size = sizeof(struct sockaddr_storage); | 627 | 1.04k | tmp_addrs[i].addr.ss_family = walker->ai_family; | 628 | | | 629 | | // according to spec, storage is supposed to be large enough (and source shows they are) | 630 | | // storage is 128 bytes | 631 | 1.04k | assert(walker->ai_addrlen <= tmp_addrs[i].size); | 632 | | | 633 | 1.04k | memcpy(&tmp_addrs[i].addr, walker->ai_addr, walker->ai_addrlen); | 634 | 1.04k | tmp_addrs[i].size = walker->ai_addrlen; | 635 | | | 636 | 1.04k | ++i; | 637 | 1.04k | } | 638 | 1.04k | } | 639 | | | 640 | 506 | assert(i == result); | 641 | | | 642 | 506 | freeaddrinfo(infos); | 643 | | | 644 | 506 | *addrs = tmp_addrs; | 645 | | | 646 | | // number of entries in addrs | 647 | 506 | return result; | 648 | 506 | } |
|
649 | | |
650 | | static int sys_freeaddrinfo(void *_Nonnull obj, const Memory *_Nonnull mem, Network_Addr *_Nonnull addrs) |
651 | 506 | { |
652 | 506 | if (addrs == nullptr) { |
653 | 0 | return 0; |
654 | 0 | } |
655 | | |
656 | 506 | mem_delete(mem, addrs); |
657 | | |
658 | 506 | return 0; |
659 | 506 | } |
660 | | |
661 | | static const Network_Funcs os_network_funcs = { |
662 | | sys_close, |
663 | | sys_accept, |
664 | | sys_bind, |
665 | | sys_listen, |
666 | | sys_connect, |
667 | | sys_recvbuf, |
668 | | sys_recv, |
669 | | sys_recvfrom, |
670 | | sys_send, |
671 | | sys_sendto, |
672 | | sys_socket, |
673 | | sys_socket_nonblock, |
674 | | sys_getsockopt, |
675 | | sys_setsockopt, |
676 | | sys_getaddrinfo, |
677 | | sys_freeaddrinfo, |
678 | | }; |
679 | | static const Network os_network_obj = {&os_network_funcs, nullptr}; |
680 | | |
681 | | const Network *os_network(void) |
682 | 3.29k | { |
683 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
684 | 1.40k | if ((true)) { |
685 | 1.40k | return nullptr; |
686 | 1.40k | } |
687 | 0 | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
688 | | #ifdef OS_WIN32 |
689 | | WSADATA wsaData; |
690 | | |
691 | | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { |
692 | | return nullptr; |
693 | | } |
694 | | #endif /* OS_WIN32 */ |
695 | 0 | return &os_network_obj; |
696 | 3.29k | } Line | Count | Source | 682 | 1.40k | { | 683 | 1.40k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION | 684 | 1.40k | if ((true)) { | 685 | 1.40k | return nullptr; | 686 | 1.40k | } | 687 | 0 | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ | 688 | | #ifdef OS_WIN32 | 689 | | WSADATA wsaData; | 690 | | | 691 | | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { | 692 | | return nullptr; | 693 | | } | 694 | | #endif /* OS_WIN32 */ | 695 | 0 | return &os_network_obj; | 696 | 1.40k | } |
Line | Count | Source | 682 | 1.88k | { | 683 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION | 684 | | if ((true)) { | 685 | | return nullptr; | 686 | | } | 687 | | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ | 688 | | #ifdef OS_WIN32 | 689 | | WSADATA wsaData; | 690 | | | 691 | | if (WSAStartup(MAKEWORD(2, 2), &wsaData) != NO_ERROR) { | 692 | | return nullptr; | 693 | | } | 694 | | #endif /* OS_WIN32 */ | 695 | 1.88k | return &os_network_obj; | 696 | 1.88k | } |
|
697 | | |
698 | | #if 0 |
699 | | /* TODO(iphydf): Call this from functions that use `os_network()`. */ |
700 | | void os_network_deinit(const Network *ns) |
701 | | { |
702 | | #ifdef OS_WIN32 |
703 | | WSACleanup(); |
704 | | #endif /* OS_WIN32 */ |
705 | | } |
706 | | #endif /* 0 */ |
707 | | |
708 | | static int net_setsockopt(const Network *_Nonnull ns, Socket sock, int level, int optname, const void *_Nonnull optval, size_t optlen) |
709 | 10.9k | { |
710 | 10.9k | return ns->funcs->setsockopt(ns->obj, sock, level, optname, optval, optlen); |
711 | 10.9k | } |
712 | | |
713 | | static int net_getsockopt(const Network *_Nonnull ns, Socket sock, int level, int optname, void *_Nonnull optval, size_t *_Nonnull optlen) |
714 | 1.40k | { |
715 | 1.40k | return ns->funcs->getsockopt(ns->obj, sock, level, optname, optval, optlen); |
716 | 1.40k | } |
717 | | |
718 | | int net_send(const Network *ns, const Logger *log, |
719 | | Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port, Net_Profile *net_profile) |
720 | 5.55k | { |
721 | 5.55k | const int res = ns->funcs->send(ns->obj, sock, buf, len); |
722 | | |
723 | 5.55k | if (res > 0) { |
724 | 4.85k | netprof_record_packet(net_profile, buf[0], res, PACKET_DIRECTION_SEND); |
725 | 4.85k | } |
726 | | |
727 | 5.55k | net_log_data(log, "T=>", buf, len, ip_port, res); |
728 | 5.55k | return res; |
729 | 5.55k | } |
730 | | |
731 | | static int net_sendto(const Network *_Nonnull ns, Socket sock, const uint8_t *_Nonnull buf, size_t len, const Network_Addr *_Nonnull addr, const IP_Port *_Nonnull ip_port) |
732 | 1.08M | { |
733 | 1.08M | return ns->funcs->sendto(ns->obj, sock, buf, len, addr); |
734 | 1.08M | } |
735 | | |
736 | | int net_recv(const Network *ns, const Logger *log, |
737 | | Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port) |
738 | 8.77k | { |
739 | 8.77k | const int res = ns->funcs->recv(ns->obj, sock, buf, len); |
740 | 8.77k | net_log_data(log, "=>T", buf, len, ip_port, res); |
741 | 8.77k | return res; |
742 | 8.77k | } |
743 | | |
744 | | static int net_recvfrom(const Network *_Nonnull ns, Socket sock, uint8_t *_Nonnull buf, size_t len, Network_Addr *_Nonnull addr) |
745 | 1.08M | { |
746 | 1.08M | return ns->funcs->recvfrom(ns->obj, sock, buf, len, addr); |
747 | 1.08M | } |
748 | | |
749 | | int net_listen(const Network *ns, Socket sock, int backlog) |
750 | 19 | { |
751 | 19 | return ns->funcs->listen(ns->obj, sock, backlog); |
752 | 19 | } |
753 | | |
754 | | static int net_bind(const Network *_Nonnull ns, Socket sock, const Network_Addr *_Nonnull addr) |
755 | 62.6k | { |
756 | 62.6k | return ns->funcs->bind(ns->obj, sock, addr); |
757 | 62.6k | } |
758 | | |
759 | | Socket net_accept(const Network *ns, Socket sock) |
760 | 2.27k | { |
761 | 2.27k | return ns->funcs->accept(ns->obj, sock); |
762 | 2.27k | } |
763 | | |
764 | | /** Close the socket. */ |
765 | | void kill_sock(const Network *ns, Socket sock) |
766 | 2.41k | { |
767 | 2.41k | ns->funcs->close(ns->obj, sock); |
768 | 2.41k | } |
769 | | |
770 | | bool set_socket_nonblock(const Network *ns, Socket sock) |
771 | 3.31k | { |
772 | 3.31k | return ns->funcs->socket_nonblock(ns->obj, sock, true) == 0; |
773 | 3.31k | } |
774 | | |
775 | | bool set_socket_nosigpipe(const Network *ns, Socket sock) |
776 | 3.29k | { |
777 | | #if defined(__APPLE__) |
778 | | int set = 1; |
779 | | return net_setsockopt(ns, sock, SOL_SOCKET, SO_NOSIGPIPE, &set, sizeof(int)) == 0; |
780 | | #else |
781 | 3.29k | return true; |
782 | 3.29k | #endif /* __APPLE__ */ |
783 | 3.29k | } |
784 | | |
785 | | bool set_socket_reuseaddr(const Network *ns, Socket sock) |
786 | 19 | { |
787 | 19 | int set = 1; |
788 | | #if defined(OS_WIN32) |
789 | | return net_setsockopt(ns, sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, &set, sizeof(set)) == 0; |
790 | | #else |
791 | 19 | return net_setsockopt(ns, sock, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)) == 0; |
792 | 19 | #endif /* OS_WIN32 */ |
793 | 19 | } |
794 | | |
795 | | bool set_socket_dualstack(const Network *ns, Socket sock) |
796 | 1.40k | { |
797 | 1.40k | int ipv6only = 0; |
798 | 1.40k | size_t optsize = sizeof(ipv6only); |
799 | 1.40k | const int res = net_getsockopt(ns, sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, &optsize); |
800 | | |
801 | 1.40k | if ((res == 0) && (ipv6only == 0)) { |
802 | 1.40k | return true; |
803 | 1.40k | } |
804 | | |
805 | 0 | ipv6only = 0; |
806 | 0 | return net_setsockopt(ns, sock, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only)) == 0; |
807 | 1.40k | } |
808 | | |
809 | | typedef struct Packet_Handler { |
810 | | packet_handler_cb *function; |
811 | | void *object; |
812 | | } Packet_Handler; |
813 | | |
814 | | struct Networking_Core { |
815 | | const Logger *log; |
816 | | const Memory *mem; |
817 | | Packet_Handler packethandlers[256]; |
818 | | const Network *ns; |
819 | | |
820 | | Family family; |
821 | | uint16_t port; |
822 | | /* Our UDP socket. */ |
823 | | Socket sock; |
824 | | |
825 | | Net_Profile *udp_net_profile; |
826 | | }; |
827 | | |
828 | | Family net_family(const Networking_Core *net) |
829 | 29.0k | { |
830 | 29.0k | return net->family; |
831 | 29.0k | } |
832 | | |
833 | | uint16_t net_port(const Networking_Core *net) |
834 | 612 | { |
835 | 612 | return net->port; |
836 | 612 | } |
837 | | |
838 | | /* Basic network functions: |
839 | | */ |
840 | | |
841 | | int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet) |
842 | 1.08M | { |
843 | 1.08M | IP_Port ipp_copy = *ip_port; |
844 | | |
845 | 1.08M | if (net_family_is_unspec(ip_port->ip.family)) { |
846 | | // TODO(iphydf): Make this an error. Currently this fails sometimes when |
847 | | // called from DHT.c:do_ping_and_sendnode_requests. |
848 | 3.25k | return -1; |
849 | 3.25k | } |
850 | | |
851 | 1.08M | if (net_family_is_unspec(net->family)) { /* Socket not initialized */ |
852 | | // TODO(iphydf): Make this an error. Currently, the onion client calls |
853 | | // this via DHT nodes requests. |
854 | 4 | LOGGER_WARNING(net->log, "attempted to send message of length %u on uninitialised socket", packet.length); |
855 | 4 | return -1; |
856 | 4 | } |
857 | | |
858 | | /* socket TOX_AF_INET, but target IP NOT: can't send */ |
859 | 1.08M | if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ipp_copy.ip.family)) { |
860 | | // TODO(iphydf): Make this an error. Occasionally we try to send to an |
861 | | // all-zero ip_port. |
862 | 15 | Ip_Ntoa ip_str; |
863 | 15 | LOGGER_WARNING(net->log, "attempted to send message with network family %d (probably IPv6) on IPv4 socket (%s)", |
864 | 15 | ipp_copy.ip.family.value, net_ip_ntoa(&ipp_copy.ip, &ip_str)); |
865 | 15 | return -1; |
866 | 15 | } |
867 | | |
868 | 1.08M | if (net_family_is_ipv4(ipp_copy.ip.family) && net_family_is_ipv6(net->family)) { |
869 | | /* must convert to IPV4-in-IPV6 address */ |
870 | 0 | IP6 ip6; |
871 | | |
872 | | /* there should be a macro for this in a standards compliant |
873 | | * environment, not found */ |
874 | 0 | ip6.uint32[0] = 0; |
875 | 0 | ip6.uint32[1] = 0; |
876 | 0 | ip6.uint32[2] = net_htonl(0xFFFF); |
877 | 0 | ip6.uint32[3] = ipp_copy.ip.ip.v4.uint32; |
878 | |
|
879 | 0 | ipp_copy.ip.family = net_family_ipv6(); |
880 | 0 | ipp_copy.ip.ip.v6 = ip6; |
881 | 0 | } |
882 | | |
883 | 1.08M | Network_Addr addr; |
884 | | |
885 | 1.08M | if (net_family_is_ipv4(ipp_copy.ip.family)) { |
886 | 1.08M | struct sockaddr_in *const addr4 = (struct sockaddr_in *)&addr.addr; |
887 | | |
888 | 1.08M | addr.size = sizeof(struct sockaddr_in); |
889 | 1.08M | addr4->sin_family = AF_INET; |
890 | 1.08M | addr4->sin_port = ipp_copy.port; |
891 | 1.08M | fill_addr4(&ipp_copy.ip.ip.v4, &addr4->sin_addr); |
892 | 1.08M | } else if (net_family_is_ipv6(ipp_copy.ip.family)) { |
893 | 0 | struct sockaddr_in6 *const addr6 = (struct sockaddr_in6 *)&addr.addr; |
894 | |
|
895 | 0 | addr.size = sizeof(struct sockaddr_in6); |
896 | 0 | addr6->sin6_family = AF_INET6; |
897 | 0 | addr6->sin6_port = ipp_copy.port; |
898 | 0 | fill_addr6(&ipp_copy.ip.ip.v6, &addr6->sin6_addr); |
899 | |
|
900 | 0 | addr6->sin6_flowinfo = 0; |
901 | 0 | addr6->sin6_scope_id = 0; |
902 | 0 | } else { |
903 | 0 | LOGGER_ERROR(net->log, "unknown address type: %d", ipp_copy.ip.family.value); |
904 | 0 | return -1; |
905 | 0 | } |
906 | | |
907 | 1.08M | const long res = net_sendto(net->ns, net->sock, packet.data, packet.length, &addr, &ipp_copy); |
908 | 1.08M | net_log_data(net->log, "O=>", packet.data, packet.length, ip_port, res); |
909 | | |
910 | 1.08M | assert(res <= INT_MAX); |
911 | | |
912 | 1.08M | if (res == packet.length && packet.data != nullptr) { |
913 | 1.08M | netprof_record_packet(net->udp_net_profile, packet.data[0], packet.length, PACKET_DIRECTION_SEND); |
914 | 1.08M | } |
915 | | |
916 | 1.08M | return (int)res; |
917 | 1.08M | } |
918 | | |
919 | | /** |
920 | | * Function to send packet(data) of length length to ip_port. |
921 | | * |
922 | | * @deprecated Use send_packet instead. |
923 | | */ |
924 | | int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length) |
925 | 1.08M | { |
926 | 1.08M | const Packet packet = {data, length}; |
927 | 1.08M | return send_packet(net, ip_port, packet); |
928 | 1.08M | } |
929 | | |
930 | | /** @brief Function to receive data |
931 | | * ip and port of sender is put into ip_port. |
932 | | * Packet data is put into data. |
933 | | * Packet length is put into length. |
934 | | */ |
935 | | static int receivepacket(const Network *_Nonnull ns, const Logger *_Nonnull log, Socket sock, IP_Port *_Nonnull ip_port, uint8_t *_Nonnull data, uint32_t *_Nonnull length) |
936 | 1.08M | { |
937 | 1.08M | memset(ip_port, 0, sizeof(IP_Port)); |
938 | 1.08M | Network_Addr addr = {{0}}; |
939 | 1.08M | addr.size = sizeof(addr.addr); |
940 | 1.08M | *length = 0; |
941 | | |
942 | 1.08M | const int fail_or_len = net_recvfrom(ns, sock, data, MAX_UDP_PACKET_SIZE, &addr); |
943 | | |
944 | 1.08M | if (fail_or_len < 0) { |
945 | 130k | const int error = net_error(); |
946 | | |
947 | 130k | if (!should_ignore_recv_error(error)) { |
948 | 26 | Net_Strerror error_str; |
949 | 26 | LOGGER_ERROR(log, "unexpected error reading from socket: %u, %s", (unsigned int)error, net_strerror(error, &error_str)); |
950 | 26 | } |
951 | | |
952 | 130k | return -1; /* Nothing received. */ |
953 | 130k | } |
954 | | |
955 | 957k | *length = (uint32_t)fail_or_len; |
956 | | |
957 | 957k | if (addr.addr.ss_family == AF_INET) { |
958 | 957k | const struct sockaddr_in *addr_in = (const struct sockaddr_in *)&addr.addr; |
959 | | |
960 | 957k | const Family *const family = make_tox_family(addr_in->sin_family); |
961 | 957k | assert(family != nullptr); |
962 | | |
963 | 957k | if (family == nullptr) { |
964 | 0 | return -1; |
965 | 0 | } |
966 | | |
967 | 957k | ip_port->ip.family = *family; |
968 | 957k | get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); |
969 | 957k | ip_port->port = addr_in->sin_port; |
970 | 957k | } else if (addr.addr.ss_family == AF_INET6) { |
971 | 0 | const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)&addr.addr; |
972 | 0 | const Family *const family = make_tox_family(addr_in6->sin6_family); |
973 | 0 | assert(family != nullptr); |
974 | | |
975 | 0 | if (family == nullptr) { |
976 | 0 | return -1; |
977 | 0 | } |
978 | | |
979 | 0 | ip_port->ip.family = *family; |
980 | 0 | get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); |
981 | 0 | ip_port->port = addr_in6->sin6_port; |
982 | |
|
983 | 0 | if (ipv6_ipv4_in_v6(&ip_port->ip.ip.v6)) { |
984 | 0 | ip_port->ip.family = net_family_ipv4(); |
985 | 0 | ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; |
986 | 0 | } |
987 | 0 | } else { |
988 | 0 | return -1; |
989 | 0 | } |
990 | | |
991 | 957k | net_log_data(log, "=>O", data, MAX_UDP_PACKET_SIZE, ip_port, *length); |
992 | | |
993 | 957k | return 0; |
994 | 957k | } |
995 | | |
996 | | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_cb *cb, void *object) |
997 | 156k | { |
998 | 156k | net->packethandlers[byte].function = cb; |
999 | 156k | net->packethandlers[byte].object = object; |
1000 | 156k | } |
1001 | | |
1002 | | void networking_poll(const Networking_Core *net, void *userdata) |
1003 | 130k | { |
1004 | 130k | if (net_family_is_unspec(net->family)) { |
1005 | | /* Socket not initialized */ |
1006 | 650 | return; |
1007 | 650 | } |
1008 | | |
1009 | 130k | IP_Port ip_port; |
1010 | 130k | uint8_t data[MAX_UDP_PACKET_SIZE] = {0}; |
1011 | 130k | uint32_t length; |
1012 | | |
1013 | 1.08M | while (receivepacket(net->ns, net->log, net->sock, &ip_port, data, &length) != -1) { |
1014 | 957k | if (length < 1) { |
1015 | 0 | continue; |
1016 | 0 | } |
1017 | | |
1018 | 957k | netprof_record_packet(net->udp_net_profile, data[0], length, PACKET_DIRECTION_RECV); |
1019 | | |
1020 | 957k | const Packet_Handler *const handler = &net->packethandlers[data[0]]; |
1021 | | |
1022 | 957k | if (handler->function == nullptr) { |
1023 | | // TODO(https://github.com/TokTok/c-toxcore/issues/1115): Make this |
1024 | | // a warning or error again. |
1025 | 1.39k | LOGGER_DEBUG(net->log, "[%02u] -- Packet has no handler", data[0]); |
1026 | 1.39k | continue; |
1027 | 1.39k | } |
1028 | | |
1029 | 956k | handler->function(handler->object, &ip_port, data, length, userdata); |
1030 | 956k | } |
1031 | 130k | } |
1032 | | |
1033 | | /** @brief Initialize networking. |
1034 | | * Bind to ip and port. |
1035 | | * ip must be in network order EX: 127.0.0.1 = (7F000001). |
1036 | | * port is in host byte order (this means don't worry about it). |
1037 | | * |
1038 | | * @return Networking_Core object if no problems |
1039 | | * @retval NULL if there are problems. |
1040 | | * |
1041 | | * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. |
1042 | | */ |
1043 | | Networking_Core *new_networking_ex( |
1044 | | const Logger *log, const Memory *mem, const Network *ns, const IP *ip, |
1045 | | uint16_t port_from, uint16_t port_to, unsigned int *error) |
1046 | 3.20k | { |
1047 | | /* If both from and to are 0, use default port range |
1048 | | * If one is 0 and the other is non-0, use the non-0 value as only port |
1049 | | * If from > to, swap |
1050 | | */ |
1051 | 3.20k | if (port_from == 0 && port_to == 0) { |
1052 | 1.38k | port_from = TOX_PORTRANGE_FROM; |
1053 | 1.38k | port_to = TOX_PORTRANGE_TO; |
1054 | 1.81k | } else if (port_from == 0 && port_to != 0) { |
1055 | 6 | port_from = port_to; |
1056 | 1.81k | } else if (port_from != 0 && port_to == 0) { |
1057 | 1 | port_to = port_from; |
1058 | 1.81k | } else if (port_from > port_to) { |
1059 | 1 | const uint16_t temp_port = port_from; |
1060 | 1 | port_from = port_to; |
1061 | 1 | port_to = temp_port; |
1062 | 1 | } |
1063 | | |
1064 | 3.20k | if (error != nullptr) { |
1065 | 3.08k | *error = 2; |
1066 | 3.08k | } |
1067 | | |
1068 | | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ |
1069 | 3.20k | if (!net_family_is_ipv4(ip->family) && !net_family_is_ipv6(ip->family)) { |
1070 | 2 | LOGGER_ERROR(log, "invalid address family: %u", ip->family.value); |
1071 | 2 | return nullptr; |
1072 | 2 | } |
1073 | | |
1074 | 3.20k | Networking_Core *temp = (Networking_Core *)mem_alloc(mem, sizeof(Networking_Core)); |
1075 | | |
1076 | 3.20k | if (temp == nullptr) { |
1077 | 12 | return nullptr; |
1078 | 12 | } |
1079 | | |
1080 | 3.18k | Net_Profile *np = netprof_new(log, mem); |
1081 | | |
1082 | 3.18k | if (np == nullptr) { |
1083 | 12 | free(temp); |
1084 | 12 | return nullptr; |
1085 | 12 | } |
1086 | | |
1087 | 3.17k | temp->udp_net_profile = np; |
1088 | 3.17k | temp->ns = ns; |
1089 | 3.17k | temp->log = log; |
1090 | 3.17k | temp->mem = mem; |
1091 | 3.17k | temp->family = ip->family; |
1092 | 3.17k | temp->port = 0; |
1093 | | |
1094 | | /* Initialize our socket. */ |
1095 | | /* add log message what we're creating */ |
1096 | 3.17k | temp->sock = net_socket(ns, temp->family, TOX_SOCK_DGRAM, TOX_PROTO_UDP); |
1097 | | |
1098 | | /* Check for socket error. */ |
1099 | 3.17k | if (!sock_valid(temp->sock)) { |
1100 | 16 | const int neterror = net_error(); |
1101 | 16 | Net_Strerror error_str; |
1102 | 16 | LOGGER_ERROR(log, "failed to get a socket?! %d, %s", neterror, net_strerror(neterror, &error_str)); |
1103 | 16 | netprof_kill(mem, temp->udp_net_profile); |
1104 | 16 | mem_delete(mem, temp); |
1105 | | |
1106 | 16 | if (error != nullptr) { |
1107 | 16 | *error = 1; |
1108 | 16 | } |
1109 | | |
1110 | 16 | return nullptr; |
1111 | 16 | } |
1112 | | |
1113 | | /* Functions to increase the size of the send and receive UDP buffers. |
1114 | | */ |
1115 | 3.16k | int n = 1024 * 1024 * 2; |
1116 | | |
1117 | 3.16k | if (net_setsockopt(ns, temp->sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof(n)) != 0) { |
1118 | 17 | LOGGER_WARNING(log, "failed to set socket option %d", SO_RCVBUF); |
1119 | 17 | } |
1120 | | |
1121 | 3.16k | if (net_setsockopt(ns, temp->sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof(n)) != 0) { |
1122 | 17 | LOGGER_WARNING(log, "failed to set socket option %d", SO_SNDBUF); |
1123 | 17 | } |
1124 | | |
1125 | | /* Enable broadcast on socket */ |
1126 | 3.16k | int broadcast = 1; |
1127 | | |
1128 | 3.16k | if (net_setsockopt(ns, temp->sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) != 0) { |
1129 | 17 | LOGGER_ERROR(log, "failed to set socket option %d", SO_BROADCAST); |
1130 | 17 | } |
1131 | | |
1132 | | /* iOS UDP sockets are weird and apparently can SIGPIPE */ |
1133 | 3.16k | if (!set_socket_nosigpipe(ns, temp->sock)) { |
1134 | 0 | kill_networking(temp); |
1135 | |
|
1136 | 0 | if (error != nullptr) { |
1137 | 0 | *error = 1; |
1138 | 0 | } |
1139 | |
|
1140 | 0 | return nullptr; |
1141 | 0 | } |
1142 | | |
1143 | | /* Set socket nonblocking. */ |
1144 | 3.16k | if (!set_socket_nonblock(ns, temp->sock)) { |
1145 | 0 | kill_networking(temp); |
1146 | |
|
1147 | 0 | if (error != nullptr) { |
1148 | 0 | *error = 1; |
1149 | 0 | } |
1150 | |
|
1151 | 0 | return nullptr; |
1152 | 0 | } |
1153 | | |
1154 | | /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ |
1155 | 3.16k | uint16_t *portptr = nullptr; |
1156 | 3.16k | Network_Addr addr = {{0}}; |
1157 | | |
1158 | 3.16k | if (net_family_is_ipv4(temp->family)) { |
1159 | 1.75k | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr.addr; |
1160 | | |
1161 | 1.75k | addr.size = sizeof(struct sockaddr_in); |
1162 | 1.75k | addr4->sin_family = AF_INET; |
1163 | 1.75k | addr4->sin_port = 0; |
1164 | 1.75k | fill_addr4(&ip->ip.v4, &addr4->sin_addr); |
1165 | | |
1166 | 1.75k | portptr = &addr4->sin_port; |
1167 | 1.75k | } else if (net_family_is_ipv6(temp->family)) { |
1168 | 1.40k | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr.addr; |
1169 | | |
1170 | 1.40k | addr.size = sizeof(struct sockaddr_in6); |
1171 | 1.40k | addr6->sin6_family = AF_INET6; |
1172 | 1.40k | addr6->sin6_port = 0; |
1173 | 1.40k | fill_addr6(&ip->ip.v6, &addr6->sin6_addr); |
1174 | | |
1175 | 1.40k | addr6->sin6_flowinfo = 0; |
1176 | 1.40k | addr6->sin6_scope_id = 0; |
1177 | | |
1178 | 1.40k | portptr = &addr6->sin6_port; |
1179 | 1.40k | } else { |
1180 | 0 | mem_delete(mem, temp); |
1181 | 0 | return nullptr; |
1182 | 0 | } |
1183 | | |
1184 | 3.16k | if (net_family_is_ipv6(ip->family)) { |
1185 | 1.40k | const bool is_dualstack = set_socket_dualstack(ns, temp->sock); |
1186 | | |
1187 | 1.40k | if (is_dualstack) { |
1188 | 1.40k | LOGGER_TRACE(log, "Dual-stack socket: enabled"); |
1189 | 1.40k | } else { |
1190 | 0 | LOGGER_ERROR(log, "Dual-stack socket failed to enable, won't be able to receive from/send to IPv4 addresses"); |
1191 | 0 | } |
1192 | | |
1193 | 1.40k | #ifndef ESP_PLATFORM |
1194 | | /* multicast local nodes */ |
1195 | 1.40k | struct ipv6_mreq mreq = {{{{0}}}}; |
1196 | 1.40k | mreq.ipv6mr_multiaddr.s6_addr[0] = 0xFF; |
1197 | 1.40k | mreq.ipv6mr_multiaddr.s6_addr[1] = 0x02; |
1198 | 1.40k | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; |
1199 | 1.40k | mreq.ipv6mr_interface = 0; |
1200 | | |
1201 | 1.40k | const int res = net_setsockopt(ns, temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)); |
1202 | | |
1203 | 1.40k | const int neterror = net_error(); |
1204 | 1.40k | Net_Strerror error_str; |
1205 | | |
1206 | 1.40k | if (res < 0) { |
1207 | 5 | LOGGER_INFO(log, "Failed to activate local multicast membership in FF02::1. (%d, %s)", neterror, net_strerror(neterror, &error_str)); |
1208 | 1.39k | } else { |
1209 | 1.39k | LOGGER_TRACE(log, "Local multicast group joined successfully. (%d, %s)", neterror, net_strerror(neterror, &error_str)); |
1210 | 1.39k | } |
1211 | 1.40k | #endif /* ESP_PLATFORM */ |
1212 | 1.40k | } |
1213 | | |
1214 | | /* A hanging program or a different user might block the standard port. |
1215 | | * As long as it isn't a parameter coming from the commandline, |
1216 | | * try a few ports after it, to see if we can find a "free" one. |
1217 | | * |
1218 | | * If we go on without binding, the first sendto() automatically binds to |
1219 | | * a free port chosen by the system (i.e. anything from 1024 to 65535). |
1220 | | * |
1221 | | * Returning NULL after bind fails has both advantages and disadvantages: |
1222 | | * advantage: |
1223 | | * we can rely on getting the port in the range 33445..33450, which |
1224 | | * enables us to tell joe user to open their firewall to a small range |
1225 | | * |
1226 | | * disadvantage: |
1227 | | * some clients might not test return of tox_new(), blindly assuming that |
1228 | | * it worked ok (which it did previously without a successful bind) |
1229 | | */ |
1230 | 3.16k | uint16_t port_to_try = port_from; |
1231 | 3.16k | *portptr = net_htons(port_to_try); |
1232 | | |
1233 | 62.6k | for (uint16_t tries = port_from; tries <= port_to; ++tries) { |
1234 | 62.5k | const int res = net_bind(ns, temp->sock, &addr); |
1235 | | |
1236 | 62.5k | if (res == 0) { |
1237 | 3.15k | temp->port = *portptr; |
1238 | | |
1239 | 3.15k | Ip_Ntoa ip_str; |
1240 | 3.15k | LOGGER_DEBUG(log, "Bound successfully to %s:%u", net_ip_ntoa(ip, &ip_str), |
1241 | 3.15k | net_ntohs(temp->port)); |
1242 | | |
1243 | | /* errno isn't reset on success, only set on failure, the failed |
1244 | | * binds with parallel clients yield a -EPERM to the outside if |
1245 | | * errno isn't cleared here */ |
1246 | 3.15k | if (tries > 0) { |
1247 | 3.15k | errno = 0; |
1248 | 3.15k | } |
1249 | | |
1250 | 3.15k | if (error != nullptr) { |
1251 | 3.04k | *error = 0; |
1252 | 3.04k | } |
1253 | | |
1254 | 3.15k | return temp; |
1255 | 3.15k | } |
1256 | | |
1257 | 59.4k | ++port_to_try; |
1258 | | |
1259 | 59.4k | if (port_to_try > port_to) { |
1260 | 8 | port_to_try = port_from; |
1261 | 8 | } |
1262 | | |
1263 | 59.4k | *portptr = net_htons(port_to_try); |
1264 | 59.4k | } |
1265 | | |
1266 | 8 | Ip_Ntoa ip_str; |
1267 | 8 | const int neterror = net_error(); |
1268 | 8 | Net_Strerror error_str; |
1269 | 8 | LOGGER_ERROR(log, "failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", |
1270 | 8 | neterror, net_strerror(neterror, &error_str), net_ip_ntoa(ip, &ip_str), port_from, port_to); |
1271 | 8 | kill_networking(temp); |
1272 | | |
1273 | 8 | if (error != nullptr) { |
1274 | 8 | *error = 1; |
1275 | 8 | } |
1276 | | |
1277 | 8 | return nullptr; |
1278 | 3.16k | } |
1279 | | |
1280 | | Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, const Network *ns) |
1281 | 64 | { |
1282 | | /* this is the easiest way to completely disable UDP without changing too much code. */ |
1283 | 64 | Networking_Core *net = (Networking_Core *)mem_alloc(mem, sizeof(Networking_Core)); |
1284 | | |
1285 | 64 | if (net == nullptr) { |
1286 | 1 | return nullptr; |
1287 | 1 | } |
1288 | | |
1289 | 63 | net->ns = ns; |
1290 | 63 | net->log = log; |
1291 | 63 | net->mem = mem; |
1292 | | |
1293 | 63 | return net; |
1294 | 64 | } |
1295 | | |
1296 | | /** Function to cleanup networking stuff (doesn't do much right now). */ |
1297 | | void kill_networking(Networking_Core *net) |
1298 | 2.35k | { |
1299 | 2.35k | if (net == nullptr) { |
1300 | 0 | return; |
1301 | 0 | } |
1302 | | |
1303 | 2.35k | if (!net_family_is_unspec(net->family)) { |
1304 | | /* Socket is initialized, so we close it. */ |
1305 | 2.28k | kill_sock(net->ns, net->sock); |
1306 | 2.28k | } |
1307 | | |
1308 | 2.35k | netprof_kill(net->mem, net->udp_net_profile); |
1309 | 2.35k | mem_delete(net->mem, net); |
1310 | 2.35k | } |
1311 | | |
1312 | | bool ip_equal(const IP *a, const IP *b) |
1313 | 387k | { |
1314 | 387k | if (a == nullptr || b == nullptr) { |
1315 | 9 | return false; |
1316 | 9 | } |
1317 | | |
1318 | | /* same family */ |
1319 | 387k | if (a->family.value == b->family.value) { |
1320 | 387k | if (net_family_is_ipv4(a->family) || net_family_is_tcp_ipv4(a->family)) { |
1321 | 387k | struct in_addr addr_a; |
1322 | 387k | struct in_addr addr_b; |
1323 | 387k | fill_addr4(&a->ip.v4, &addr_a); |
1324 | 387k | fill_addr4(&b->ip.v4, &addr_b); |
1325 | 387k | return addr_a.s_addr == addr_b.s_addr; |
1326 | 387k | } |
1327 | | |
1328 | 6 | if (net_family_is_ipv6(a->family) || net_family_is_tcp_ipv6(a->family)) { |
1329 | 6 | return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && |
1330 | 6 | a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; |
1331 | 6 | } |
1332 | | |
1333 | 0 | return false; |
1334 | 6 | } |
1335 | | |
1336 | | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ |
1337 | 9 | if (net_family_is_ipv4(a->family) && net_family_is_ipv6(b->family)) { |
1338 | 6 | if (ipv6_ipv4_in_v6(&b->ip.v6)) { |
1339 | 3 | struct in_addr addr_a; |
1340 | 3 | fill_addr4(&a->ip.v4, &addr_a); |
1341 | 3 | return addr_a.s_addr == b->ip.v6.uint32[3]; |
1342 | 3 | } |
1343 | 6 | } else if (net_family_is_ipv6(a->family) && net_family_is_ipv4(b->family)) { |
1344 | 0 | if (ipv6_ipv4_in_v6(&a->ip.v6)) { |
1345 | 0 | struct in_addr addr_b; |
1346 | 0 | fill_addr4(&b->ip.v4, &addr_b); |
1347 | 0 | return a->ip.v6.uint32[3] == addr_b.s_addr; |
1348 | 0 | } |
1349 | 0 | } |
1350 | | |
1351 | 6 | return false; |
1352 | 9 | } |
1353 | | |
1354 | | bool ipport_equal(const IP_Port *a, const IP_Port *b) |
1355 | 12.8M | { |
1356 | 12.8M | if (a == nullptr || b == nullptr) { |
1357 | 0 | return false; |
1358 | 0 | } |
1359 | | |
1360 | 12.8M | if (a->port == 0 || (a->port != b->port)) { |
1361 | 12.4M | return false; |
1362 | 12.4M | } |
1363 | | |
1364 | 387k | return ip_equal(&a->ip, &b->ip); |
1365 | 12.8M | } |
1366 | | |
1367 | | static int ip4_cmp(const IP4 *_Nonnull a, const IP4 *_Nonnull b) |
1368 | 360k | { |
1369 | 360k | return cmp_uint(a->uint32, b->uint32); |
1370 | 360k | } |
1371 | | |
1372 | | static int ip6_cmp(const IP6 *_Nonnull a, const IP6 *_Nonnull b) |
1373 | 0 | { |
1374 | 0 | const int res = cmp_uint(a->uint64[0], b->uint64[0]); |
1375 | 0 | if (res != 0) { |
1376 | 0 | return res; |
1377 | 0 | } |
1378 | 0 | return cmp_uint(a->uint64[1], b->uint64[1]); |
1379 | 0 | } |
1380 | | |
1381 | | static int ip_cmp(const IP *_Nonnull a, const IP *_Nonnull b) |
1382 | 366k | { |
1383 | 366k | const int res = cmp_uint(a->family.value, b->family.value); |
1384 | 366k | if (res != 0) { |
1385 | 5.95k | return res; |
1386 | 5.95k | } |
1387 | 360k | switch (a->family.value) { |
1388 | 0 | case TOX_AF_UNSPEC: |
1389 | 0 | return 0; |
1390 | 360k | case TOX_AF_INET: |
1391 | 360k | case TCP_INET: |
1392 | 360k | case TOX_TCP_INET: |
1393 | 360k | return ip4_cmp(&a->ip.v4, &b->ip.v4); |
1394 | 0 | case TOX_AF_INET6: |
1395 | 0 | case TCP_INET6: |
1396 | 0 | case TOX_TCP_INET6: |
1397 | 0 | case TCP_SERVER_FAMILY: // these happen to be ipv6 according to TCP_server.c. |
1398 | 0 | case TCP_CLIENT_FAMILY: |
1399 | 0 | return ip6_cmp(&a->ip.v6, &b->ip.v6); |
1400 | 360k | } |
1401 | | // Invalid, we don't compare any further and consider them equal. |
1402 | 0 | return 0; |
1403 | 360k | } |
1404 | | |
1405 | | int ipport_cmp_handler(const void *a, const void *b, size_t size) |
1406 | 366k | { |
1407 | 366k | const IP_Port *ipp_a = (const IP_Port *)a; |
1408 | 366k | const IP_Port *ipp_b = (const IP_Port *)b; |
1409 | 366k | assert(size == sizeof(IP_Port)); |
1410 | | |
1411 | 366k | const int ip_res = ip_cmp(&ipp_a->ip, &ipp_b->ip); |
1412 | 366k | if (ip_res != 0) { |
1413 | 5.95k | return ip_res; |
1414 | 5.95k | } |
1415 | | |
1416 | 360k | return cmp_uint(ipp_a->port, ipp_b->port); |
1417 | 366k | } |
1418 | | |
1419 | | static const IP empty_ip = {{0}}; |
1420 | | |
1421 | | /** nulls out ip */ |
1422 | | void ip_reset(IP *ip) |
1423 | 36.9k | { |
1424 | 36.9k | if (ip == nullptr) { |
1425 | 0 | return; |
1426 | 0 | } |
1427 | | |
1428 | 36.9k | *ip = empty_ip; |
1429 | 36.9k | } |
1430 | | |
1431 | | static const IP_Port empty_ip_port = {{{0}}}; |
1432 | | |
1433 | | /** nulls out ip_port */ |
1434 | | void ipport_reset(IP_Port *ipport) |
1435 | 532k | { |
1436 | 532k | if (ipport == nullptr) { |
1437 | 0 | return; |
1438 | 0 | } |
1439 | | |
1440 | 532k | *ipport = empty_ip_port; |
1441 | 532k | } |
1442 | | |
1443 | | /** nulls out ip, sets family according to flag */ |
1444 | | void ip_init(IP *ip, bool ipv6enabled) |
1445 | 3.15k | { |
1446 | 3.15k | if (ip == nullptr) { |
1447 | 0 | return; |
1448 | 0 | } |
1449 | | |
1450 | 3.15k | ip_reset(ip); |
1451 | 3.15k | ip->family = ipv6enabled ? net_family_ipv6() : net_family_ipv4(); |
1452 | 3.15k | } |
1453 | | |
1454 | | /** checks if ip is valid */ |
1455 | | bool ip_isset(const IP *ip) |
1456 | 3.69M | { |
1457 | 3.69M | if (ip == nullptr) { |
1458 | 0 | return false; |
1459 | 0 | } |
1460 | | |
1461 | 3.69M | return !net_family_is_unspec(ip->family); |
1462 | 3.69M | } |
1463 | | |
1464 | | /** checks if ip is valid */ |
1465 | | bool ipport_isset(const IP_Port *ipport) |
1466 | 347k | { |
1467 | 347k | if (ipport == nullptr) { |
1468 | 0 | return false; |
1469 | 0 | } |
1470 | | |
1471 | 347k | if (ipport->port == 0) { |
1472 | 113k | return false; |
1473 | 113k | } |
1474 | | |
1475 | 234k | return ip_isset(&ipport->ip); |
1476 | 347k | } |
1477 | | |
1478 | | /** copies an ip structure (careful about direction) */ |
1479 | | void ip_copy(IP *target, const IP *source) |
1480 | 6 | { |
1481 | 6 | if (source == nullptr || target == nullptr) { |
1482 | 0 | return; |
1483 | 0 | } |
1484 | | |
1485 | 6 | *target = *source; |
1486 | 6 | } |
1487 | | |
1488 | | /** copies an ip_port structure (careful about direction) */ |
1489 | | void ipport_copy(IP_Port *target, const IP_Port *source) |
1490 | 5.58k | { |
1491 | 5.58k | if (source == nullptr || target == nullptr) { |
1492 | 0 | return; |
1493 | 0 | } |
1494 | | |
1495 | | // Write to a temporary object first, so that padding bytes are |
1496 | | // uninitialised and msan can catch mistakes in downstream code. |
1497 | 5.58k | IP_Port tmp; |
1498 | 5.58k | tmp.ip.family = source->ip.family; |
1499 | 5.58k | tmp.ip.ip = source->ip.ip; |
1500 | 5.58k | tmp.port = source->port; |
1501 | | |
1502 | 5.58k | *target = tmp; |
1503 | 5.58k | } |
1504 | | |
1505 | | /** @brief Packs an IP structure. |
1506 | | * |
1507 | | * It's the caller's responsibility to make sure `is_ipv4` tells the truth. This |
1508 | | * function is an implementation detail of @ref bin_pack_ip_port. |
1509 | | * |
1510 | | * @param is_ipv4 whether this IP is an IP4 or IP6. |
1511 | | * |
1512 | | * @retval true on success. |
1513 | | */ |
1514 | | static bool bin_pack_ip(Bin_Pack *_Nonnull bp, const IP *_Nonnull ip, bool is_ipv4) |
1515 | 1.45M | { |
1516 | 1.45M | if (is_ipv4) { |
1517 | 1.45M | return bin_pack_bin_b(bp, ip->ip.v4.uint8, SIZE_IP4); |
1518 | 1.45M | } else { |
1519 | 2.09k | return bin_pack_bin_b(bp, ip->ip.v6.uint8, SIZE_IP6); |
1520 | 2.09k | } |
1521 | 1.45M | } |
1522 | | |
1523 | | /** @brief Packs an IP_Port structure. |
1524 | | * |
1525 | | * @retval true on success. |
1526 | | */ |
1527 | | bool bin_pack_ip_port(Bin_Pack *bp, const Logger *logger, const IP_Port *ip_port) |
1528 | 1.46M | { |
1529 | 1.46M | bool is_ipv4; |
1530 | 1.46M | uint8_t family; |
1531 | | |
1532 | 1.46M | if (net_family_is_ipv4(ip_port->ip.family)) { |
1533 | | // TODO(irungentoo): use functions to convert endianness |
1534 | 1.45M | is_ipv4 = true; |
1535 | 1.45M | family = TOX_AF_INET; |
1536 | 1.45M | } else if (net_family_is_tcp_ipv4(ip_port->ip.family)) { |
1537 | 2.46k | is_ipv4 = true; |
1538 | 2.46k | family = TOX_TCP_INET; |
1539 | 7.72k | } else if (net_family_is_ipv6(ip_port->ip.family)) { |
1540 | 1.50k | is_ipv4 = false; |
1541 | 1.50k | family = TOX_AF_INET6; |
1542 | 6.21k | } else if (net_family_is_tcp_ipv6(ip_port->ip.family)) { |
1543 | 588 | is_ipv4 = false; |
1544 | 588 | family = TOX_TCP_INET6; |
1545 | 5.62k | } else { |
1546 | 5.62k | Ip_Ntoa ip_str; |
1547 | | // TODO(iphydf): Find out why we're trying to pack invalid IPs, stop |
1548 | | // doing that, and turn this into an error. |
1549 | 5.62k | LOGGER_TRACE(logger, "cannot pack invalid IP: %s", net_ip_ntoa(&ip_port->ip, &ip_str)); |
1550 | 5.62k | return false; |
1551 | 5.62k | } |
1552 | | |
1553 | 1.45M | return bin_pack_u08_b(bp, family) |
1554 | 1.45M | && bin_pack_ip(bp, &ip_port->ip, is_ipv4) |
1555 | 1.45M | && bin_pack_u16_b(bp, net_ntohs(ip_port->port)); |
1556 | 1.46M | } |
1557 | | |
1558 | | static bool bin_pack_ip_port_handler(const void *_Nonnull obj, const Logger *_Nonnull logger, Bin_Pack *_Nonnull bp) |
1559 | 276k | { |
1560 | 276k | const IP_Port *ip_port = (const IP_Port *)obj; |
1561 | 276k | return bin_pack_ip_port(bp, logger, ip_port); |
1562 | 276k | } |
1563 | | |
1564 | | int pack_ip_port(const Logger *logger, uint8_t *data, uint16_t length, const IP_Port *ip_port) |
1565 | 138k | { |
1566 | 138k | const uint32_t size = bin_pack_obj_size(bin_pack_ip_port_handler, ip_port, logger); |
1567 | | |
1568 | 138k | if (size > length) { |
1569 | 0 | return -1; |
1570 | 0 | } |
1571 | | |
1572 | 138k | if (!bin_pack_obj(bin_pack_ip_port_handler, ip_port, logger, data, length)) { |
1573 | 0 | return -1; |
1574 | 0 | } |
1575 | | |
1576 | 138k | assert(size < INT_MAX); |
1577 | 138k | return (int)size; |
1578 | 138k | } |
1579 | | |
1580 | | int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled) |
1581 | 532k | { |
1582 | 532k | if (data == nullptr) { |
1583 | 0 | return -1; |
1584 | 0 | } |
1585 | | |
1586 | 532k | bool is_ipv4; |
1587 | 532k | Family host_family; |
1588 | | |
1589 | 532k | if (data[0] == TOX_AF_INET) { |
1590 | 528k | is_ipv4 = true; |
1591 | 528k | host_family = net_family_ipv4(); |
1592 | 528k | } else if (data[0] == TOX_TCP_INET) { |
1593 | 1.24k | if (!tcp_enabled) { |
1594 | 177 | return -1; |
1595 | 177 | } |
1596 | | |
1597 | 1.07k | is_ipv4 = true; |
1598 | 1.07k | host_family = net_family_tcp_ipv4(); |
1599 | 2.82k | } else if (data[0] == TOX_AF_INET6) { |
1600 | 1.52k | is_ipv4 = false; |
1601 | 1.52k | host_family = net_family_ipv6(); |
1602 | 1.52k | } else if (data[0] == TOX_TCP_INET6) { |
1603 | 458 | if (!tcp_enabled) { |
1604 | 78 | return -1; |
1605 | 78 | } |
1606 | | |
1607 | 380 | is_ipv4 = false; |
1608 | 380 | host_family = net_family_tcp_ipv6(); |
1609 | 840 | } else { |
1610 | 840 | return -1; |
1611 | 840 | } |
1612 | | |
1613 | 531k | ipport_reset(ip_port); |
1614 | | |
1615 | 531k | if (is_ipv4) { |
1616 | 529k | const uint32_t size = 1 + SIZE_IP4 + sizeof(uint16_t); |
1617 | | |
1618 | 529k | if (size > length) { |
1619 | 126 | return -1; |
1620 | 126 | } |
1621 | | |
1622 | 529k | ip_port->ip.family = host_family; |
1623 | 529k | memcpy(ip_port->ip.ip.v4.uint8, data + 1, SIZE_IP4); |
1624 | 529k | memcpy(&ip_port->port, data + 1 + SIZE_IP4, sizeof(uint16_t)); |
1625 | 529k | return size; |
1626 | 529k | } else { |
1627 | 1.90k | const uint32_t size = 1 + SIZE_IP6 + sizeof(uint16_t); |
1628 | | |
1629 | 1.90k | if (size > length) { |
1630 | 150 | return -1; |
1631 | 150 | } |
1632 | | |
1633 | 1.75k | ip_port->ip.family = host_family; |
1634 | 1.75k | memcpy(ip_port->ip.ip.v6.uint8, data + 1, SIZE_IP6); |
1635 | 1.75k | memcpy(&ip_port->port, data + 1 + SIZE_IP6, sizeof(uint16_t)); |
1636 | 1.75k | return size; |
1637 | 1.90k | } |
1638 | 531k | } |
1639 | | |
1640 | | const char *net_ip_ntoa(const IP *ip, Ip_Ntoa *ip_str) |
1641 | 2.73M | { |
1642 | 2.73M | assert(ip_str != nullptr); |
1643 | | |
1644 | 2.73M | ip_str->ip_is_valid = false; |
1645 | | |
1646 | 2.73M | if (ip == nullptr) { |
1647 | 0 | snprintf(ip_str->buf, sizeof(ip_str->buf), "(IP invalid: NULL)"); |
1648 | 0 | ip_str->length = (uint16_t)strlen(ip_str->buf); |
1649 | 0 | return ip_str->buf; |
1650 | 0 | } |
1651 | | |
1652 | 2.73M | if (!ip_parse_addr(ip, ip_str->buf, sizeof(ip_str->buf))) { |
1653 | 14.7k | snprintf(ip_str->buf, sizeof(ip_str->buf), "(IP invalid, family %u)", ip->family.value); |
1654 | 14.7k | ip_str->length = (uint16_t)strlen(ip_str->buf); |
1655 | 14.7k | return ip_str->buf; |
1656 | 14.7k | } |
1657 | | |
1658 | | /* brute force protection against lacking termination */ |
1659 | 2.72M | ip_str->buf[sizeof(ip_str->buf) - 1] = '\0'; |
1660 | 2.72M | ip_str->length = (uint16_t)strlen(ip_str->buf); |
1661 | 2.72M | ip_str->ip_is_valid = true; |
1662 | | |
1663 | 2.72M | return ip_str->buf; |
1664 | 2.73M | } |
1665 | | |
1666 | | bool ip_parse_addr(const IP *ip, char *address, size_t length) |
1667 | 2.73M | { |
1668 | 2.73M | if (address == nullptr || ip == nullptr) { |
1669 | 0 | return false; |
1670 | 0 | } |
1671 | | |
1672 | 2.73M | if (net_family_is_ipv4(ip->family) || net_family_is_tcp_ipv4(ip->family)) { |
1673 | 2.71M | struct in_addr addr; |
1674 | 2.71M | assert(make_family(ip->family) == AF_INET); |
1675 | 2.71M | fill_addr4(&ip->ip.v4, &addr); |
1676 | 2.71M | return inet_ntop4(&addr, address, length) != nullptr; |
1677 | 2.71M | } |
1678 | | |
1679 | 16.1k | if (net_family_is_ipv6(ip->family) || net_family_is_tcp_ipv6(ip->family)) { |
1680 | 1.40k | struct in6_addr addr; |
1681 | 1.40k | assert(make_family(ip->family) == AF_INET6); |
1682 | 1.40k | fill_addr6(&ip->ip.v6, &addr); |
1683 | 1.40k | return inet_ntop6(&addr, address, length) != nullptr; |
1684 | 1.40k | } |
1685 | | |
1686 | 14.7k | return false; |
1687 | 16.1k | } |
1688 | | |
1689 | | bool addr_parse_ip(const char *address, IP *to) |
1690 | 27.6k | { |
1691 | 27.6k | if (address == nullptr || to == nullptr) { |
1692 | 0 | return false; |
1693 | 0 | } |
1694 | | |
1695 | 27.6k | struct in_addr addr4; |
1696 | | |
1697 | 27.6k | if (inet_pton4(address, &addr4) == 1) { |
1698 | 27.1k | to->family = net_family_ipv4(); |
1699 | 27.1k | get_ip4(&to->ip.v4, &addr4); |
1700 | 27.1k | return true; |
1701 | 27.1k | } |
1702 | | |
1703 | 553 | struct in6_addr addr6; |
1704 | | |
1705 | 553 | if (inet_pton6(address, &addr6) == 1) { |
1706 | 0 | to->family = net_family_ipv6(); |
1707 | 0 | get_ip6(&to->ip.v6, &addr6); |
1708 | 0 | return true; |
1709 | 0 | } |
1710 | | |
1711 | 553 | return false; |
1712 | 553 | } |
1713 | | |
1714 | | /** addr_resolve return values */ |
1715 | 0 | #define TOX_ADDR_RESOLVE_INET 1 |
1716 | 0 | #define TOX_ADDR_RESOLVE_INET6 2 |
1717 | | |
1718 | | /** |
1719 | | * Uses getaddrinfo to resolve an address into an IP address. |
1720 | | * |
1721 | | * Uses the first IPv4/IPv6 addresses returned by getaddrinfo. |
1722 | | * |
1723 | | * @param address a hostname (or something parseable to an IP address) |
1724 | | * @param to to.family MUST be initialized, either set to a specific IP version |
1725 | | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both |
1726 | | * IP versions are acceptable |
1727 | | * @param extra can be NULL and is only set in special circumstances, see returns |
1728 | | * |
1729 | | * Returns in `*to` a valid IPAny (v4/v6), |
1730 | | * prefers v6 if `ip.family` was TOX_AF_UNSPEC and both available |
1731 | | * Returns in `*extra` an IPv4 address, if family was TOX_AF_UNSPEC and `*to` is TOX_AF_INET6 |
1732 | | * |
1733 | | * @return false on failure, true on success. |
1734 | | */ |
1735 | | static bool addr_resolve(const Network *_Nonnull ns, const Memory *_Nonnull mem, const char *_Nonnull address, IP *_Nonnull to, IP *_Nullable extra) |
1736 | 11 | { |
1737 | 11 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1738 | 11 | if ((true)) { |
1739 | 11 | return false; |
1740 | 11 | } |
1741 | 0 | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
1742 | | |
1743 | 0 | if (address == nullptr || to == nullptr) { |
1744 | 0 | return false; |
1745 | 0 | } |
1746 | | |
1747 | 0 | const Family tox_family = to->family; |
1748 | 0 | const int family = make_family(tox_family); |
1749 | |
|
1750 | 0 | Network_Addr *addrs = nullptr; |
1751 | 0 | const int rc = ns->funcs->getaddrinfo(ns->obj, mem, address, family, 0, &addrs); |
1752 | | |
1753 | | // Lookup failed / empty. |
1754 | 0 | if (rc <= 0) { |
1755 | 0 | return false; |
1756 | 0 | } |
1757 | | |
1758 | 0 | assert(addrs != nullptr); |
1759 | | |
1760 | 0 | IP ip4; |
1761 | 0 | ip_init(&ip4, false); // ipv6enabled = false |
1762 | 0 | IP ip6; |
1763 | 0 | ip_init(&ip6, true); // ipv6enabled = true |
1764 | |
|
1765 | 0 | int result = 0; |
1766 | 0 | bool done = false; |
1767 | |
|
1768 | 0 | for (int i = 0; i < rc && !done; ++i) { |
1769 | 0 | switch (addrs[i].addr.ss_family) { |
1770 | 0 | case AF_INET: { |
1771 | 0 | if (addrs[i].addr.ss_family == family) { /* AF_INET requested, done */ |
1772 | 0 | const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; |
1773 | 0 | get_ip4(&to->ip.v4, &addr->sin_addr); |
1774 | 0 | result = TOX_ADDR_RESOLVE_INET; |
1775 | 0 | done = true; |
1776 | 0 | } else if ((result & TOX_ADDR_RESOLVE_INET) == 0) { /* AF_UNSPEC requested, store away */ |
1777 | 0 | const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; |
1778 | 0 | get_ip4(&ip4.ip.v4, &addr->sin_addr); |
1779 | 0 | result |= TOX_ADDR_RESOLVE_INET; |
1780 | 0 | } |
1781 | |
|
1782 | 0 | break; /* switch */ |
1783 | 0 | } |
1784 | | |
1785 | 0 | case AF_INET6: { |
1786 | 0 | if (addrs[i].addr.ss_family == family) { /* AF_INET6 requested, done */ |
1787 | 0 | if (addrs[i].size == sizeof(struct sockaddr_in6)) { |
1788 | 0 | const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)&addrs[i].addr; |
1789 | 0 | get_ip6(&to->ip.v6, &addr->sin6_addr); |
1790 | 0 | result = TOX_ADDR_RESOLVE_INET6; |
1791 | 0 | done = true; |
1792 | 0 | } |
1793 | 0 | } else if ((result & TOX_ADDR_RESOLVE_INET6) == 0) { /* AF_UNSPEC requested, store away */ |
1794 | 0 | if (addrs[i].size == sizeof(struct sockaddr_in6)) { |
1795 | 0 | const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)&addrs[i].addr; |
1796 | 0 | get_ip6(&ip6.ip.v6, &addr->sin6_addr); |
1797 | 0 | result |= TOX_ADDR_RESOLVE_INET6; |
1798 | 0 | } |
1799 | 0 | } |
1800 | |
|
1801 | 0 | break; /* switch */ |
1802 | 0 | } |
1803 | 0 | } |
1804 | 0 | } |
1805 | | |
1806 | 0 | if (family == AF_UNSPEC) { |
1807 | 0 | if ((result & TOX_ADDR_RESOLVE_INET6) != 0) { |
1808 | 0 | ip_copy(to, &ip6); |
1809 | |
|
1810 | 0 | if ((result & TOX_ADDR_RESOLVE_INET) != 0 && (extra != nullptr)) { |
1811 | 0 | ip_copy(extra, &ip4); |
1812 | 0 | } |
1813 | 0 | } else if ((result & TOX_ADDR_RESOLVE_INET) != 0) { |
1814 | 0 | ip_copy(to, &ip4); |
1815 | 0 | } else { |
1816 | 0 | result = 0; |
1817 | 0 | } |
1818 | 0 | } |
1819 | |
|
1820 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); |
1821 | 0 | return result != 0; |
1822 | 0 | } |
1823 | | |
1824 | | bool addr_resolve_or_parse_ip(const Network *ns, const Memory *mem, const char *address, IP *to, IP *extra, bool dns_enabled) |
1825 | 24 | { |
1826 | 24 | if (dns_enabled && addr_resolve(ns, mem, address, to, extra)) { |
1827 | 13 | return true; |
1828 | 13 | } |
1829 | | |
1830 | 11 | return addr_parse_ip(address, to); |
1831 | 24 | } |
1832 | | |
1833 | | const char *net_err_connect_to_string(Net_Err_Connect err) |
1834 | 0 | { |
1835 | 0 | switch (err) { |
1836 | 0 | case NET_ERR_CONNECT_OK: |
1837 | 0 | return "NET_ERR_CONNECT_OK"; |
1838 | 0 | case NET_ERR_CONNECT_INVALID_FAMILY: |
1839 | 0 | return "NET_ERR_CONNECT_INVALID_FAMILY"; |
1840 | 0 | case NET_ERR_CONNECT_FAILED: |
1841 | 0 | return "NET_ERR_CONNECT_FAILED"; |
1842 | 0 | } |
1843 | | |
1844 | 0 | return "<invalid Net_Err_Connect>"; |
1845 | 0 | } |
1846 | | |
1847 | | bool net_connect(const Network *ns, const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port, Net_Err_Connect *err) |
1848 | 0 | { |
1849 | 0 | Network_Addr addr = {{0}}; |
1850 | |
|
1851 | 0 | if (net_family_is_ipv4(ip_port->ip.family)) { |
1852 | 0 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr.addr; |
1853 | |
|
1854 | 0 | addr.size = sizeof(struct sockaddr_in); |
1855 | 0 | addr4->sin_family = AF_INET; |
1856 | 0 | fill_addr4(&ip_port->ip.ip.v4, &addr4->sin_addr); |
1857 | 0 | addr4->sin_port = ip_port->port; |
1858 | 0 | } else if (net_family_is_ipv6(ip_port->ip.family)) { |
1859 | 0 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr.addr; |
1860 | |
|
1861 | 0 | addr.size = sizeof(struct sockaddr_in6); |
1862 | 0 | addr6->sin6_family = AF_INET6; |
1863 | 0 | fill_addr6(&ip_port->ip.ip.v6, &addr6->sin6_addr); |
1864 | 0 | addr6->sin6_port = ip_port->port; |
1865 | 0 | } else { |
1866 | 0 | Ip_Ntoa ip_str; |
1867 | 0 | LOGGER_ERROR(log, "cannot connect to %s:%d which is neither IPv4 nor IPv6", |
1868 | 0 | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port)); |
1869 | 0 | *err = NET_ERR_CONNECT_INVALID_FAMILY; |
1870 | 0 | return false; |
1871 | 0 | } |
1872 | | |
1873 | 0 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1874 | 0 | if ((true)) { |
1875 | 0 | *err = NET_ERR_CONNECT_OK; |
1876 | 0 | return true; |
1877 | 0 | } |
1878 | 0 | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
1879 | | |
1880 | 0 | Ip_Ntoa ip_str; |
1881 | 0 | LOGGER_DEBUG(log, "connecting socket %d to %s:%d", |
1882 | 0 | net_socket_to_native(sock), net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port)); |
1883 | 0 | errno = 0; |
1884 | |
|
1885 | 0 | if (ns->funcs->connect(ns->obj, sock, &addr) == -1) { |
1886 | 0 | const int error = net_error(); |
1887 | | |
1888 | | // Non-blocking socket: "Operation in progress" means it's connecting. |
1889 | 0 | if (!should_ignore_connect_error(error)) { |
1890 | 0 | Net_Strerror error_str; |
1891 | 0 | LOGGER_WARNING(log, "failed to connect to %s:%d: %d (%s)", |
1892 | 0 | net_ip_ntoa(&ip_port->ip, &ip_str), net_ntohs(ip_port->port), error, net_strerror(error, &error_str)); |
1893 | 0 | *err = NET_ERR_CONNECT_FAILED; |
1894 | 0 | return false; |
1895 | 0 | } |
1896 | 0 | } |
1897 | | |
1898 | 0 | *err = NET_ERR_CONNECT_OK; |
1899 | 0 | return true; |
1900 | 0 | } |
1901 | | |
1902 | | int32_t net_getipport(const Network *ns, const Memory *mem, const char *node, IP_Port **res, int tox_type, bool dns_enabled) |
1903 | 27.6k | { |
1904 | 27.6k | assert(node != nullptr); |
1905 | | |
1906 | | // Try parsing as IP address first. |
1907 | 27.6k | IP_Port parsed = {{{0}}}; |
1908 | | // Initialise to nullptr. In error paths, at least we initialise the out |
1909 | | // parameter. |
1910 | 27.6k | *res = nullptr; |
1911 | | |
1912 | 27.6k | if (addr_parse_ip(node, &parsed.ip)) { |
1913 | 27.0k | IP_Port *tmp = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); |
1914 | | |
1915 | 27.0k | if (tmp == nullptr) { |
1916 | 0 | return -1; |
1917 | 0 | } |
1918 | | |
1919 | 27.0k | tmp[0] = parsed; |
1920 | 27.0k | *res = tmp; |
1921 | 27.0k | return 1; |
1922 | 27.0k | } |
1923 | | |
1924 | 553 | if (!dns_enabled) { |
1925 | 0 | return -1; |
1926 | 0 | } |
1927 | | |
1928 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
1929 | 0 | if ((true)) { |
1930 | 0 | IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); |
1931 | 0 | if (ip_port == nullptr) { |
1932 | 0 | abort(); |
1933 | 0 | } |
1934 | 0 | ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3 |
1935 | 0 | ip_port->ip.family = *make_tox_family(AF_INET); |
1936 | |
|
1937 | 0 | *res = ip_port; |
1938 | 0 | return 1; |
1939 | 0 | } |
1940 | 0 | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ |
1941 | | |
1942 | 553 | int type = make_socktype(tox_type); |
1943 | | // ugly |
1944 | 553 | if (tox_type == -1) { |
1945 | 0 | type = 0; |
1946 | 0 | } |
1947 | | |
1948 | | // It's not an IP address, so now we try doing a DNS lookup. |
1949 | 553 | Network_Addr *addrs = nullptr; |
1950 | 553 | const int rc = ns->funcs->getaddrinfo(ns->obj, mem, node, AF_UNSPEC, type, &addrs); |
1951 | | |
1952 | | // Lookup failed / empty. |
1953 | 553 | if (rc <= 0) { |
1954 | 60 | return -1; |
1955 | 60 | } |
1956 | | |
1957 | 493 | assert(addrs != nullptr); |
1958 | | |
1959 | | // Used to avoid calloc parameter overflow |
1960 | 493 | const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); |
1961 | 493 | size_t count = 0; |
1962 | | |
1963 | 1.47k | for (int i = 0; i < rc && count < max_count; ++i) { |
1964 | 986 | if (addrs[i].addr.ss_family != AF_INET && addrs[i].addr.ss_family != AF_INET6) { |
1965 | 0 | continue; |
1966 | 0 | } |
1967 | | |
1968 | 986 | ++count; |
1969 | 986 | } |
1970 | | |
1971 | 493 | assert(count <= max_count); |
1972 | | |
1973 | 493 | if (count == 0) { |
1974 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); |
1975 | 0 | return 0; |
1976 | 0 | } |
1977 | | |
1978 | 493 | IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); |
1979 | | |
1980 | 493 | if (ip_port == nullptr) { |
1981 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); |
1982 | 0 | *res = nullptr; |
1983 | 0 | return -1; |
1984 | 0 | } |
1985 | | |
1986 | 493 | *res = ip_port; |
1987 | | |
1988 | 1.47k | for (int i = 0; i < rc && count < max_count; ++i) { |
1989 | 986 | if (addrs[i].addr.ss_family == AF_INET) { |
1990 | 493 | const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; |
1991 | 493 | ip_port->ip.ip.v4.uint32 = addr->sin_addr.s_addr; |
1992 | 493 | } else if (addrs[i].addr.ss_family == AF_INET6) { |
1993 | 493 | const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)&addrs[i].addr; |
1994 | 493 | memcpy(ip_port->ip.ip.v6.uint8, addr->sin6_addr.s6_addr, sizeof(IP6)); |
1995 | 493 | } else { |
1996 | 0 | continue; |
1997 | 0 | } |
1998 | | |
1999 | 986 | const Family *const family = make_tox_family(addrs[i].addr.ss_family); |
2000 | 986 | assert(family != nullptr); |
2001 | | |
2002 | 986 | if (family == nullptr) { |
2003 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); |
2004 | 0 | return -1; |
2005 | 0 | } |
2006 | | |
2007 | 986 | ip_port->ip.family = *family; |
2008 | | |
2009 | 986 | ++ip_port; |
2010 | 986 | } |
2011 | | |
2012 | 493 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); |
2013 | | |
2014 | 493 | return count; |
2015 | 493 | } Unexecuted instantiation: net_getipport Line | Count | Source | 1903 | 27.6k | { | 1904 | 27.6k | assert(node != nullptr); | 1905 | | | 1906 | | // Try parsing as IP address first. | 1907 | 27.6k | IP_Port parsed = {{{0}}}; | 1908 | | // Initialise to nullptr. In error paths, at least we initialise the out | 1909 | | // parameter. | 1910 | 27.6k | *res = nullptr; | 1911 | | | 1912 | 27.6k | if (addr_parse_ip(node, &parsed.ip)) { | 1913 | 27.0k | IP_Port *tmp = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); | 1914 | | | 1915 | 27.0k | if (tmp == nullptr) { | 1916 | 0 | return -1; | 1917 | 0 | } | 1918 | | | 1919 | 27.0k | tmp[0] = parsed; | 1920 | 27.0k | *res = tmp; | 1921 | 27.0k | return 1; | 1922 | 27.0k | } | 1923 | | | 1924 | 553 | if (!dns_enabled) { | 1925 | 0 | return -1; | 1926 | 0 | } | 1927 | | | 1928 | | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION | 1929 | | if ((true)) { | 1930 | | IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port)); | 1931 | | if (ip_port == nullptr) { | 1932 | | abort(); | 1933 | | } | 1934 | | ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3 | 1935 | | ip_port->ip.family = *make_tox_family(AF_INET); | 1936 | | | 1937 | | *res = ip_port; | 1938 | | return 1; | 1939 | | } | 1940 | | #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */ | 1941 | | | 1942 | 553 | int type = make_socktype(tox_type); | 1943 | | // ugly | 1944 | 553 | if (tox_type == -1) { | 1945 | 0 | type = 0; | 1946 | 0 | } | 1947 | | | 1948 | | // It's not an IP address, so now we try doing a DNS lookup. | 1949 | 553 | Network_Addr *addrs = nullptr; | 1950 | 553 | const int rc = ns->funcs->getaddrinfo(ns->obj, mem, node, AF_UNSPEC, type, &addrs); | 1951 | | | 1952 | | // Lookup failed / empty. | 1953 | 553 | if (rc <= 0) { | 1954 | 60 | return -1; | 1955 | 60 | } | 1956 | | | 1957 | 493 | assert(addrs != nullptr); | 1958 | | | 1959 | | // Used to avoid calloc parameter overflow | 1960 | 493 | const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); | 1961 | 493 | size_t count = 0; | 1962 | | | 1963 | 1.47k | for (int i = 0; i < rc && count < max_count; ++i) { | 1964 | 986 | if (addrs[i].addr.ss_family != AF_INET && addrs[i].addr.ss_family != AF_INET6) { | 1965 | 0 | continue; | 1966 | 0 | } | 1967 | | | 1968 | 986 | ++count; | 1969 | 986 | } | 1970 | | | 1971 | 493 | assert(count <= max_count); | 1972 | | | 1973 | 493 | if (count == 0) { | 1974 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); | 1975 | 0 | return 0; | 1976 | 0 | } | 1977 | | | 1978 | 493 | IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port)); | 1979 | | | 1980 | 493 | if (ip_port == nullptr) { | 1981 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); | 1982 | 0 | *res = nullptr; | 1983 | 0 | return -1; | 1984 | 0 | } | 1985 | | | 1986 | 493 | *res = ip_port; | 1987 | | | 1988 | 1.47k | for (int i = 0; i < rc && count < max_count; ++i) { | 1989 | 986 | if (addrs[i].addr.ss_family == AF_INET) { | 1990 | 493 | const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)&addrs[i].addr; | 1991 | 493 | ip_port->ip.ip.v4.uint32 = addr->sin_addr.s_addr; | 1992 | 493 | } else if (addrs[i].addr.ss_family == AF_INET6) { | 1993 | 493 | const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)&addrs[i].addr; | 1994 | 493 | memcpy(ip_port->ip.ip.v6.uint8, addr->sin6_addr.s6_addr, sizeof(IP6)); | 1995 | 493 | } else { | 1996 | 0 | continue; | 1997 | 0 | } | 1998 | | | 1999 | 986 | const Family *const family = make_tox_family(addrs[i].addr.ss_family); | 2000 | 986 | assert(family != nullptr); | 2001 | | | 2002 | 986 | if (family == nullptr) { | 2003 | 0 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); | 2004 | 0 | return -1; | 2005 | 0 | } | 2006 | | | 2007 | 986 | ip_port->ip.family = *family; | 2008 | | | 2009 | 986 | ++ip_port; | 2010 | 986 | } | 2011 | | | 2012 | 493 | ns->funcs->freeaddrinfo(ns->obj, mem, addrs); | 2013 | | | 2014 | 493 | return count; | 2015 | 493 | } |
|
2016 | | |
2017 | | void net_freeipport(const Memory *mem, IP_Port *ip_ports) |
2018 | 27.6k | { |
2019 | 27.6k | mem_delete(mem, ip_ports); |
2020 | 27.6k | } |
2021 | | |
2022 | | bool bind_to_port(const Network *ns, Socket sock, Family family, uint16_t port) |
2023 | 19 | { |
2024 | 19 | Network_Addr addr = {{0}}; |
2025 | | |
2026 | 19 | if (net_family_is_ipv4(family)) { |
2027 | 19 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr.addr; |
2028 | | |
2029 | 19 | addr.size = sizeof(struct sockaddr_in); |
2030 | 19 | addr4->sin_family = AF_INET; |
2031 | 19 | addr4->sin_port = net_htons(port); |
2032 | 19 | } else if (net_family_is_ipv6(family)) { |
2033 | 0 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr.addr; |
2034 | |
|
2035 | 0 | addr.size = sizeof(struct sockaddr_in6); |
2036 | 0 | addr6->sin6_family = AF_INET6; |
2037 | 0 | addr6->sin6_port = net_htons(port); |
2038 | 0 | } else { |
2039 | 0 | return false; |
2040 | 0 | } |
2041 | | |
2042 | 19 | return net_bind(ns, sock, &addr) == 0; |
2043 | 19 | } |
2044 | | |
2045 | | Socket net_socket(const Network *ns, Family domain, int type, int protocol) |
2046 | 3.30k | { |
2047 | 3.30k | const int platform_domain = make_family(domain); |
2048 | 3.30k | const int platform_type = make_socktype(type); |
2049 | 3.30k | const int platform_prot = make_proto(protocol); |
2050 | 3.30k | return ns->funcs->socket(ns->obj, platform_domain, platform_type, platform_prot); |
2051 | 3.30k | } |
2052 | | |
2053 | | uint16_t net_socket_data_recv_buffer(const Network *ns, Socket sock) |
2054 | 33.5k | { |
2055 | 33.5k | const int count = ns->funcs->recvbuf(ns->obj, sock); |
2056 | 33.5k | return (uint16_t)max_s32(0, min_s32(count, UINT16_MAX)); |
2057 | 33.5k | } |
2058 | | |
2059 | | uint32_t net_htonl(uint32_t hostlong) |
2060 | 1.23M | { |
2061 | 1.23M | return htonl(hostlong); |
2062 | 1.23M | } |
2063 | | |
2064 | | uint16_t net_htons(uint16_t hostshort) |
2065 | 161k | { |
2066 | 161k | return htons(hostshort); |
2067 | 161k | } |
2068 | | |
2069 | | uint32_t net_ntohl(uint32_t hostlong) |
2070 | 1.01M | { |
2071 | 1.01M | return ntohl(hostlong); |
2072 | 1.01M | } |
2073 | | |
2074 | | uint16_t net_ntohs(uint16_t hostshort) |
2075 | 4.57M | { |
2076 | 4.57M | return ntohs(hostshort); |
2077 | 4.57M | } |
2078 | | |
2079 | | size_t net_pack_bool(uint8_t *bytes, bool v) |
2080 | 18 | { |
2081 | 18 | bytes[0] = v ? 1 : 0; |
2082 | 18 | return 1; |
2083 | 18 | } |
2084 | | |
2085 | | size_t net_pack_u16(uint8_t *bytes, uint16_t v) |
2086 | 208k | { |
2087 | 208k | bytes[0] = (v >> 8) & 0xff; |
2088 | 208k | bytes[1] = v & 0xff; |
2089 | 208k | return sizeof(v); |
2090 | 208k | } |
2091 | | |
2092 | | size_t net_pack_u32(uint8_t *bytes, uint32_t v) |
2093 | 99.4k | { |
2094 | 99.4k | uint8_t *p = bytes; |
2095 | 99.4k | p += net_pack_u16(p, (v >> 16) & 0xffff); |
2096 | 99.4k | p += net_pack_u16(p, v & 0xffff); |
2097 | 99.4k | return p - bytes; |
2098 | 99.4k | } |
2099 | | |
2100 | | size_t net_pack_u64(uint8_t *bytes, uint64_t v) |
2101 | 40.9k | { |
2102 | 40.9k | uint8_t *p = bytes; |
2103 | 40.9k | p += net_pack_u32(p, (v >> 32) & 0xffffffff); |
2104 | 40.9k | p += net_pack_u32(p, v & 0xffffffff); |
2105 | 40.9k | return p - bytes; |
2106 | 40.9k | } |
2107 | | |
2108 | | size_t net_unpack_bool(const uint8_t *bytes, bool *v) |
2109 | 8.82k | { |
2110 | 8.82k | *v = bytes[0] != 0; |
2111 | 8.82k | return 1; |
2112 | 8.82k | } |
2113 | | |
2114 | | size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v) |
2115 | 8.61M | { |
2116 | 8.61M | const uint8_t hi = bytes[0]; |
2117 | 8.61M | const uint8_t lo = bytes[1]; |
2118 | 8.61M | *v = ((uint16_t)hi << 8) | lo; |
2119 | 8.61M | return sizeof(*v); |
2120 | 8.61M | } |
2121 | | |
2122 | | size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v) |
2123 | 4.18M | { |
2124 | 4.18M | const uint8_t *p = bytes; |
2125 | 4.18M | uint16_t hi; |
2126 | 4.18M | uint16_t lo; |
2127 | 4.18M | p += net_unpack_u16(p, &hi); |
2128 | 4.18M | p += net_unpack_u16(p, &lo); |
2129 | 4.18M | *v = ((uint32_t)hi << 16) | lo; |
2130 | 4.18M | return p - bytes; |
2131 | 4.18M | } |
2132 | | |
2133 | | size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v) |
2134 | 31.5k | { |
2135 | 31.5k | const uint8_t *p = bytes; |
2136 | 31.5k | uint32_t hi; |
2137 | 31.5k | uint32_t lo; |
2138 | 31.5k | p += net_unpack_u32(p, &hi); |
2139 | 31.5k | p += net_unpack_u32(p, &lo); |
2140 | 31.5k | *v = ((uint64_t)hi << 32) | lo; |
2141 | 31.5k | return p - bytes; |
2142 | 31.5k | } |
2143 | | |
2144 | | bool ipv6_ipv4_in_v6(const IP6 *a) |
2145 | 11.7k | { |
2146 | 11.7k | return a->uint64[0] == 0 && a->uint32[2] == net_htonl(0xffff); |
2147 | 11.7k | } |
2148 | | |
2149 | | int net_error(void) |
2150 | 132k | { |
2151 | | #ifdef OS_WIN32 |
2152 | | return WSAGetLastError(); |
2153 | | #else |
2154 | 132k | return errno; |
2155 | 132k | #endif /* OS_WIN32 */ |
2156 | 132k | } |
2157 | | |
2158 | | #ifdef OS_WIN32 |
2159 | | char *net_strerror(int error, Net_Strerror *buf) |
2160 | | { |
2161 | | FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, |
2162 | | error, 0, buf->data, NET_STRERROR_SIZE, nullptr); |
2163 | | return buf->data; |
2164 | | } |
2165 | | #else |
2166 | | #if defined(_GNU_SOURCE) && defined(__GLIBC__) |
2167 | | static const char *net_strerror_r(int error, char *_Nonnull tmp, size_t tmp_size) |
2168 | | { |
2169 | | const char *retstr = strerror_r(error, tmp, tmp_size); |
2170 | | |
2171 | | if (errno != 0) { |
2172 | | snprintf(tmp, tmp_size, "error %d (strerror_r failed with errno %d)", error, errno); |
2173 | | } |
2174 | | |
2175 | | return retstr; |
2176 | | } |
2177 | | #else |
2178 | | static const char *net_strerror_r(int error, char *_Nonnull tmp, size_t tmp_size) |
2179 | 2.56k | { |
2180 | 2.56k | const int fmt_error = strerror_r(error, tmp, tmp_size); |
2181 | | |
2182 | 2.56k | if (fmt_error != 0) { |
2183 | 0 | snprintf(tmp, tmp_size, "error %d (strerror_r failed with error %d, errno %d)", error, fmt_error, errno); |
2184 | 0 | } |
2185 | | |
2186 | 2.56k | return tmp; |
2187 | 2.56k | } |
2188 | | #endif /* GNU */ |
2189 | | char *net_strerror(int error, Net_Strerror *buf) |
2190 | 2.56k | { |
2191 | 2.56k | errno = 0; |
2192 | | |
2193 | 2.56k | const char *retstr = net_strerror_r(error, buf->data, NET_STRERROR_SIZE); |
2194 | 2.56k | const size_t retstr_len = strlen(retstr); |
2195 | 2.56k | assert(retstr_len < NET_STRERROR_SIZE); |
2196 | 2.56k | buf->size = (uint16_t)retstr_len; |
2197 | | |
2198 | 2.56k | return buf->data; |
2199 | 2.56k | } |
2200 | | #endif /* OS_WIN32 */ |
2201 | | |
2202 | | const Net_Profile *net_get_net_profile(const Networking_Core *net) |
2203 | 1.02k | { |
2204 | 1.02k | if (net == nullptr) { |
2205 | 0 | return nullptr; |
2206 | 0 | } |
2207 | | |
2208 | 1.02k | return net->udp_net_profile; |
2209 | 1.02k | } |