Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Datatypes, functions and includes for the core networking. |
8 | | */ |
9 | | #ifndef C_TOXCORE_TOXCORE_NETWORK_H |
10 | | #define C_TOXCORE_TOXCORE_NETWORK_H |
11 | | |
12 | | #include <stdbool.h> // bool |
13 | | #include <stddef.h> // size_t |
14 | | #include <stdint.h> // uint*_t |
15 | | |
16 | | #include "logger.h" |
17 | | #include "mem.h" |
18 | | |
19 | | #ifdef __cplusplus |
20 | | extern "C" { |
21 | | #endif |
22 | | |
23 | | /** |
24 | | * @brief Wrapper for sockaddr_storage and size. |
25 | | */ |
26 | | typedef struct Network_Addr Network_Addr; |
27 | | |
28 | | typedef int net_close_cb(void *obj, int sock); |
29 | | typedef int net_accept_cb(void *obj, int sock); |
30 | | typedef int net_bind_cb(void *obj, int sock, const Network_Addr *addr); |
31 | | typedef int net_listen_cb(void *obj, int sock, int backlog); |
32 | | typedef int net_recvbuf_cb(void *obj, int sock); |
33 | | typedef int net_recv_cb(void *obj, int sock, uint8_t *buf, size_t len); |
34 | | typedef int net_recvfrom_cb(void *obj, int sock, uint8_t *buf, size_t len, Network_Addr *addr); |
35 | | typedef int net_send_cb(void *obj, int sock, const uint8_t *buf, size_t len); |
36 | | typedef int net_sendto_cb(void *obj, int sock, const uint8_t *buf, size_t len, const Network_Addr *addr); |
37 | | typedef int net_socket_cb(void *obj, int domain, int type, int proto); |
38 | | typedef int net_socket_nonblock_cb(void *obj, int sock, bool nonblock); |
39 | | typedef int net_getsockopt_cb(void *obj, int sock, int level, int optname, void *optval, size_t *optlen); |
40 | | typedef int net_setsockopt_cb(void *obj, int sock, int level, int optname, const void *optval, size_t optlen); |
41 | | typedef int net_getaddrinfo_cb(void *obj, int family, Network_Addr **addrs); |
42 | | typedef int net_freeaddrinfo_cb(void *obj, Network_Addr *addrs); |
43 | | |
44 | | /** @brief Functions wrapping POSIX network functions. |
45 | | * |
46 | | * Refer to POSIX man pages for documentation of what these functions are |
47 | | * expected to do when providing alternative Network implementations. |
48 | | */ |
49 | | typedef struct Network_Funcs { |
50 | | net_close_cb *close; |
51 | | net_accept_cb *accept; |
52 | | net_bind_cb *bind; |
53 | | net_listen_cb *listen; |
54 | | net_recvbuf_cb *recvbuf; |
55 | | net_recv_cb *recv; |
56 | | net_recvfrom_cb *recvfrom; |
57 | | net_send_cb *send; |
58 | | net_sendto_cb *sendto; |
59 | | net_socket_cb *socket; |
60 | | net_socket_nonblock_cb *socket_nonblock; |
61 | | net_getsockopt_cb *getsockopt; |
62 | | net_setsockopt_cb *setsockopt; |
63 | | net_getaddrinfo_cb *getaddrinfo; |
64 | | net_freeaddrinfo_cb *freeaddrinfo; |
65 | | } Network_Funcs; |
66 | | |
67 | | typedef struct Network { |
68 | | const Network_Funcs *funcs; |
69 | | void *obj; |
70 | | } Network; |
71 | | |
72 | | const Network *os_network(void); |
73 | | |
74 | | typedef struct Family { |
75 | | uint8_t value; |
76 | | } Family; |
77 | | |
78 | | bool net_family_is_unspec(Family family); |
79 | | bool net_family_is_ipv4(Family family); |
80 | | bool net_family_is_ipv6(Family family); |
81 | | bool net_family_is_tcp_server(Family family); |
82 | | bool net_family_is_tcp_client(Family family); |
83 | | bool net_family_is_tcp_ipv4(Family family); |
84 | | bool net_family_is_tcp_ipv6(Family family); |
85 | | bool net_family_is_tox_tcp_ipv4(Family family); |
86 | | bool net_family_is_tox_tcp_ipv6(Family family); |
87 | | |
88 | | Family net_family_unspec(void); |
89 | | Family net_family_ipv4(void); |
90 | | Family net_family_ipv6(void); |
91 | | Family net_family_tcp_server(void); |
92 | | Family net_family_tcp_client(void); |
93 | | Family net_family_tcp_ipv4(void); |
94 | | Family net_family_tcp_ipv6(void); |
95 | | Family net_family_tox_tcp_ipv4(void); |
96 | | Family net_family_tox_tcp_ipv6(void); |
97 | | |
98 | 2.12M | #define MAX_UDP_PACKET_SIZE 2048 |
99 | | |
100 | | typedef enum Net_Packet_Type { |
101 | | NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */ |
102 | | NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */ |
103 | | NET_PACKET_GET_NODES = 0x02, /* Get nodes request packet ID. */ |
104 | | NET_PACKET_SEND_NODES_IPV6 = 0x04, /* Send nodes response packet ID for other addresses. */ |
105 | | NET_PACKET_COOKIE_REQUEST = 0x18, /* Cookie request packet */ |
106 | | NET_PACKET_COOKIE_RESPONSE = 0x19, /* Cookie response packet */ |
107 | | NET_PACKET_CRYPTO_HS = 0x1a, /* Crypto handshake packet */ |
108 | | NET_PACKET_CRYPTO_DATA = 0x1b, /* Crypto data packet */ |
109 | | NET_PACKET_CRYPTO = 0x20, /* Encrypted data packet ID. */ |
110 | | NET_PACKET_LAN_DISCOVERY = 0x21, /* LAN discovery packet ID. */ |
111 | | |
112 | | NET_PACKET_GC_HANDSHAKE = 0x5a, /* Group chat handshake packet ID */ |
113 | | NET_PACKET_GC_LOSSLESS = 0x5b, /* Group chat lossless packet ID */ |
114 | | NET_PACKET_GC_LOSSY = 0x5c, /* Group chat lossy packet ID */ |
115 | | |
116 | | /* See: `docs/Prevent_Tracking.txt` and `onion.{c,h}` */ |
117 | | NET_PACKET_ONION_SEND_INITIAL = 0x80, |
118 | | NET_PACKET_ONION_SEND_1 = 0x81, |
119 | | NET_PACKET_ONION_SEND_2 = 0x82, |
120 | | |
121 | | NET_PACKET_ANNOUNCE_REQUEST_OLD = 0x83, /* TODO: DEPRECATE */ |
122 | | NET_PACKET_ANNOUNCE_RESPONSE_OLD = 0x84, /* TODO: DEPRECATE */ |
123 | | |
124 | | NET_PACKET_ONION_DATA_REQUEST = 0x85, |
125 | | NET_PACKET_ONION_DATA_RESPONSE = 0x86, |
126 | | NET_PACKET_ANNOUNCE_REQUEST = 0x87, |
127 | | NET_PACKET_ANNOUNCE_RESPONSE = 0x88, |
128 | | |
129 | | NET_PACKET_ONION_RECV_3 = 0x8c, |
130 | | NET_PACKET_ONION_RECV_2 = 0x8d, |
131 | | NET_PACKET_ONION_RECV_1 = 0x8e, |
132 | | |
133 | | NET_PACKET_FORWARD_REQUEST = 0x90, |
134 | | NET_PACKET_FORWARDING = 0x91, |
135 | | NET_PACKET_FORWARD_REPLY = 0x92, |
136 | | |
137 | | NET_PACKET_DATA_SEARCH_REQUEST = 0x93, |
138 | | NET_PACKET_DATA_SEARCH_RESPONSE = 0x94, |
139 | | NET_PACKET_DATA_RETRIEVE_REQUEST = 0x95, |
140 | | NET_PACKET_DATA_RETRIEVE_RESPONSE = 0x96, |
141 | | NET_PACKET_STORE_ANNOUNCE_REQUEST = 0x97, |
142 | | NET_PACKET_STORE_ANNOUNCE_RESPONSE = 0x98, |
143 | | |
144 | | BOOTSTRAP_INFO_PACKET_ID = 0xf0, /* Only used for bootstrap nodes */ |
145 | | |
146 | | NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ |
147 | | } Net_Packet_Type; |
148 | | |
149 | | |
150 | 7.43k | #define TOX_PORTRANGE_FROM 33445 |
151 | 5.57k | #define TOX_PORTRANGE_TO 33545 |
152 | 1.83k | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM |
153 | | |
154 | | /** Redefinitions of variables for safe transfer over wire. */ |
155 | 3 | #define TOX_AF_UNSPEC 0 |
156 | 4.60M | #define TOX_AF_INET 2 |
157 | 21.4k | #define TOX_AF_INET6 10 |
158 | 4.05k | #define TOX_TCP_INET 130 |
159 | 1.80k | #define TOX_TCP_INET6 138 |
160 | | |
161 | 1.02k | #define TOX_SOCK_STREAM 1 |
162 | 37.8k | #define TOX_SOCK_DGRAM 2 |
163 | | |
164 | 956 | #define TOX_PROTO_TCP 1 |
165 | 7.99k | #define TOX_PROTO_UDP 2 |
166 | | |
167 | | /** TCP related */ |
168 | | #define TCP_CLIENT_FAMILY (TOX_AF_INET6 + 1) |
169 | | #define TCP_INET (TOX_AF_INET6 + 2) |
170 | | #define TCP_INET6 (TOX_AF_INET6 + 3) |
171 | | #define TCP_SERVER_FAMILY (TOX_AF_INET6 + 4) |
172 | | |
173 | 3.50M | #define SIZE_IP4 4 |
174 | 11.0M | #define SIZE_IP6 16 |
175 | 10.1M | #define SIZE_IP (1 + SIZE_IP6) |
176 | 10.1M | #define SIZE_PORT 2 |
177 | 8.84M | #define SIZE_IPPORT (SIZE_IP + SIZE_PORT) |
178 | | |
179 | | typedef union IP4 { |
180 | | uint32_t uint32; |
181 | | uint16_t uint16[2]; |
182 | | uint8_t uint8[4]; |
183 | | } IP4; |
184 | | |
185 | | IP4 get_ip4_loopback(void); |
186 | | IP4 get_ip4_broadcast(void); |
187 | | |
188 | | typedef union IP6 { |
189 | | uint8_t uint8[16]; |
190 | | uint16_t uint16[8]; |
191 | | uint32_t uint32[4]; |
192 | | uint64_t uint64[2]; |
193 | | } IP6; |
194 | | |
195 | | IP6 get_ip6_loopback(void); |
196 | | IP6 get_ip6_broadcast(void); |
197 | | |
198 | | typedef union IP_Union { |
199 | | IP4 v4; |
200 | | IP6 v6; |
201 | | } IP_Union; |
202 | | |
203 | | typedef struct IP { |
204 | | Family family; |
205 | | IP_Union ip; |
206 | | } IP; |
207 | | |
208 | | typedef struct IP_Port { |
209 | | IP ip; |
210 | | uint16_t port; |
211 | | } IP_Port; |
212 | | |
213 | | typedef struct Socket { |
214 | | int sock; |
215 | | } Socket; |
216 | | |
217 | | non_null() |
218 | | Socket net_socket(const Network *ns, Family domain, int type, int protocol); |
219 | | |
220 | | /** |
221 | | * Check if socket is valid. |
222 | | * |
223 | | * @return true if valid, false otherwise. |
224 | | */ |
225 | | bool sock_valid(Socket sock); |
226 | | |
227 | | Socket net_invalid_socket(void); |
228 | | |
229 | | /** |
230 | | * Calls send(sockfd, buf, len, MSG_NOSIGNAL). |
231 | | */ |
232 | | non_null() |
233 | | int net_send(const Network *ns, const Logger *log, Socket sock, const uint8_t *buf, size_t len, const IP_Port *ip_port); |
234 | | /** |
235 | | * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). |
236 | | */ |
237 | | non_null() |
238 | | int net_recv(const Network *ns, const Logger *log, Socket sock, uint8_t *buf, size_t len, const IP_Port *ip_port); |
239 | | /** |
240 | | * Calls listen(sockfd, backlog). |
241 | | */ |
242 | | non_null() |
243 | | int net_listen(const Network *ns, Socket sock, int backlog); |
244 | | /** |
245 | | * Calls accept(sockfd, nullptr, nullptr). |
246 | | */ |
247 | | non_null() |
248 | | Socket net_accept(const Network *ns, Socket sock); |
249 | | |
250 | | /** |
251 | | * return the size of data in the tcp recv buffer. |
252 | | * return 0 on failure. |
253 | | */ |
254 | | non_null() |
255 | | uint16_t net_socket_data_recv_buffer(const Network *ns, Socket sock); |
256 | | |
257 | | /** Convert values between host and network byte order. */ |
258 | | uint32_t net_htonl(uint32_t hostlong); |
259 | | uint16_t net_htons(uint16_t hostshort); |
260 | | uint32_t net_ntohl(uint32_t hostlong); |
261 | | uint16_t net_ntohs(uint16_t hostshort); |
262 | | |
263 | | non_null() |
264 | | size_t net_pack_bool(uint8_t *bytes, bool v); |
265 | | non_null() |
266 | | size_t net_pack_u16(uint8_t *bytes, uint16_t v); |
267 | | non_null() |
268 | | size_t net_pack_u32(uint8_t *bytes, uint32_t v); |
269 | | non_null() |
270 | | size_t net_pack_u64(uint8_t *bytes, uint64_t v); |
271 | | |
272 | | non_null() |
273 | | size_t net_unpack_bool(const uint8_t *bytes, bool *v); |
274 | | non_null() |
275 | | size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v); |
276 | | non_null() |
277 | | size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v); |
278 | | non_null() |
279 | | size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v); |
280 | | |
281 | | /** Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ |
282 | | non_null() |
283 | | bool ipv6_ipv4_in_v6(const IP6 *a); |
284 | | |
285 | | #define TOX_ENABLE_IPV6_DEFAULT true |
286 | | |
287 | | #define TOX_INET6_ADDRSTRLEN 66 |
288 | | #define TOX_INET_ADDRSTRLEN 22 |
289 | | |
290 | | /** this would be TOX_INET6_ADDRSTRLEN, but it might be too short for the error message */ |
291 | | #define IP_NTOA_LEN 96 // TODO(irungentoo): magic number. Why not INET6_ADDRSTRLEN ? |
292 | | |
293 | | /** Contains a null terminated string of an IP address. */ |
294 | | typedef struct Ip_Ntoa { |
295 | | char buf[IP_NTOA_LEN]; // a string formatted IP address or an error message. |
296 | | uint16_t length; // the length of the string (not including the null byte). |
297 | | bool ip_is_valid; // if this is false `buf` will contain an error message. |
298 | | } Ip_Ntoa; |
299 | | |
300 | | /** @brief Converts IP into a null terminated string. |
301 | | * |
302 | | * Writes error message into the buffer on error. |
303 | | * |
304 | | * @param ip_str contains a buffer of the required size. |
305 | | * |
306 | | * @return Pointer to the buffer inside `ip_str` containing the IP string. |
307 | | */ |
308 | | non_null() |
309 | | const char *net_ip_ntoa(const IP *ip, Ip_Ntoa *ip_str); |
310 | | |
311 | | /** |
312 | | * Parses IP structure into an address string. |
313 | | * |
314 | | * @param ip IP of TOX_AF_INET or TOX_AF_INET6 families. |
315 | | * @param length length of the address buffer. |
316 | | * Must be at least TOX_INET_ADDRSTRLEN for TOX_AF_INET |
317 | | * and TOX_INET6_ADDRSTRLEN for TOX_AF_INET6 |
318 | | * |
319 | | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
320 | | * |
321 | | * @return true on success, false on failure. |
322 | | */ |
323 | | non_null() |
324 | | bool ip_parse_addr(const IP *ip, char *address, size_t length); |
325 | | |
326 | | /** |
327 | | * Directly parses the input into an IP structure. |
328 | | * |
329 | | * Tries IPv4 first, then IPv6. |
330 | | * |
331 | | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
332 | | * @param to family and the value is set on success. |
333 | | * |
334 | | * @return true on success, false on failure. |
335 | | */ |
336 | | non_null() |
337 | | bool addr_parse_ip(const char *address, IP *to); |
338 | | |
339 | | /** |
340 | | * Compares two IPAny structures. |
341 | | * |
342 | | * Unset means unequal. |
343 | | * |
344 | | * @return false when not equal or when uninitialized. |
345 | | */ |
346 | | nullable(1, 2) |
347 | | bool ip_equal(const IP *a, const IP *b); |
348 | | |
349 | | /** |
350 | | * Compares two IPAny_Port structures. |
351 | | * |
352 | | * Unset means unequal. |
353 | | * |
354 | | * @return false when not equal or when uninitialized. |
355 | | */ |
356 | | nullable(1, 2) |
357 | | bool ipport_equal(const IP_Port *a, const IP_Port *b); |
358 | | |
359 | | /** nulls out ip */ |
360 | | non_null() |
361 | | void ip_reset(IP *ip); |
362 | | /** nulls out ip_port */ |
363 | | non_null() |
364 | | void ipport_reset(IP_Port *ipport); |
365 | | /** nulls out ip, sets family according to flag */ |
366 | | non_null() |
367 | | void ip_init(IP *ip, bool ipv6enabled); |
368 | | /** checks if ip is valid */ |
369 | | non_null() |
370 | | bool ip_isset(const IP *ip); |
371 | | /** checks if ip is valid */ |
372 | | non_null() |
373 | | bool ipport_isset(const IP_Port *ipport); |
374 | | /** copies an ip structure (careful about direction) */ |
375 | | non_null() |
376 | | void ip_copy(IP *target, const IP *source); |
377 | | /** copies an ip_port structure (careful about direction) */ |
378 | | non_null() |
379 | | void ipport_copy(IP_Port *target, const IP_Port *source); |
380 | | |
381 | | /** |
382 | | * Resolves string into an IP address |
383 | | * |
384 | | * @param address a hostname (or something parseable to an IP address) |
385 | | * @param to to.family MUST be initialized, either set to a specific IP version |
386 | | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (0), if both |
387 | | * IP versions are acceptable |
388 | | * @param extra can be NULL and is only set in special circumstances, see returns |
389 | | * |
390 | | * Returns in `*to` a matching address (IPv6 or IPv4) |
391 | | * Returns in `*extra`, if not NULL, an IPv4 address, if `to->family` was TOX_AF_UNSPEC |
392 | | * |
393 | | * @return true on success, false on failure |
394 | | */ |
395 | | non_null(1, 2, 3) nullable(4) |
396 | | bool addr_resolve_or_parse_ip(const Network *ns, const char *address, IP *to, IP *extra); |
397 | | |
398 | | /** @brief Function to receive data, ip and port of sender is put into ip_port. |
399 | | * Packet data is put into data. |
400 | | * Packet length is put into length. |
401 | | */ |
402 | | typedef int packet_handler_cb(void *object, const IP_Port *source, const uint8_t *packet, uint16_t length, void *userdata); |
403 | | |
404 | | typedef struct Networking_Core Networking_Core; |
405 | | |
406 | | non_null() |
407 | | Family net_family(const Networking_Core *net); |
408 | | non_null() |
409 | | uint16_t net_port(const Networking_Core *net); |
410 | | |
411 | | /** Close the socket. */ |
412 | | non_null() |
413 | | void kill_sock(const Network *ns, Socket sock); |
414 | | |
415 | | /** |
416 | | * Set socket as nonblocking |
417 | | * |
418 | | * @return true on success, false on failure. |
419 | | */ |
420 | | non_null() |
421 | | bool set_socket_nonblock(const Network *ns, Socket sock); |
422 | | |
423 | | /** |
424 | | * Set socket to not emit SIGPIPE |
425 | | * |
426 | | * @return true on success, false on failure. |
427 | | */ |
428 | | non_null() |
429 | | bool set_socket_nosigpipe(const Network *ns, Socket sock); |
430 | | |
431 | | /** |
432 | | * Enable SO_REUSEADDR on socket. |
433 | | * |
434 | | * @return true on success, false on failure. |
435 | | */ |
436 | | non_null() |
437 | | bool set_socket_reuseaddr(const Network *ns, Socket sock); |
438 | | |
439 | | /** |
440 | | * Set socket to dual (IPv4 + IPv6 socket) |
441 | | * |
442 | | * @return true on success, false on failure. |
443 | | */ |
444 | | non_null() |
445 | | bool set_socket_dualstack(const Network *ns, Socket sock); |
446 | | |
447 | | /* Basic network functions: */ |
448 | | |
449 | | /** |
450 | | * An outgoing network packet. |
451 | | * |
452 | | * Use `send_packet` to send it to an IP/port endpoint. |
453 | | */ |
454 | | typedef struct Packet { |
455 | | const uint8_t *data; |
456 | | uint16_t length; |
457 | | } Packet; |
458 | | |
459 | | /** |
460 | | * Function to send a network packet to a given IP/port. |
461 | | */ |
462 | | non_null() |
463 | | int send_packet(const Networking_Core *net, const IP_Port *ip_port, Packet packet); |
464 | | |
465 | | /** |
466 | | * Function to send packet(data) of length length to ip_port. |
467 | | * |
468 | | * @deprecated Use send_packet instead. |
469 | | */ |
470 | | non_null() |
471 | | int sendpacket(const Networking_Core *net, const IP_Port *ip_port, const uint8_t *data, uint16_t length); |
472 | | |
473 | | /** Function to call when packet beginning with byte is received. */ |
474 | | non_null(1) nullable(3, 4) |
475 | | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_cb *cb, void *object); |
476 | | |
477 | | /** Call this several times a second. */ |
478 | | non_null(1) nullable(2) |
479 | | void networking_poll(const Networking_Core *net, void *userdata); |
480 | | |
481 | | /** @brief Connect a socket to the address specified by the ip_port. |
482 | | * |
483 | | * Return true on success. |
484 | | * Return false on failure. |
485 | | */ |
486 | | non_null() |
487 | | bool net_connect(const Memory *mem, const Logger *log, Socket sock, const IP_Port *ip_port); |
488 | | |
489 | | /** @brief High-level getaddrinfo implementation. |
490 | | * |
491 | | * Given node, which identifies an Internet host, `net_getipport()` fills an array |
492 | | * with one or more IP_Port structures, each of which contains an Internet |
493 | | * address that can be specified by calling `net_connect()`, the port is ignored. |
494 | | * |
495 | | * Skip all addresses with socktype != type (use type = -1 to get all addresses) |
496 | | * To correctly deallocate array memory use `net_freeipport()` |
497 | | * |
498 | | * @return number of elements in res array. |
499 | | * @retval 0 if res array empty. |
500 | | * @retval -1 on error. |
501 | | */ |
502 | | non_null() |
503 | | int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int tox_type); |
504 | | |
505 | | /** Deallocates memory allocated by net_getipport */ |
506 | | non_null(1) nullable(2) |
507 | | void net_freeipport(const Memory *mem, IP_Port *ip_ports); |
508 | | |
509 | | /** |
510 | | * @return true on success, false on failure. |
511 | | */ |
512 | | non_null() |
513 | | bool bind_to_port(const Network *ns, Socket sock, Family family, uint16_t port); |
514 | | |
515 | | /** @brief Get the last networking error code. |
516 | | * |
517 | | * Similar to Unix's errno, but cross-platform, as not all platforms use errno |
518 | | * to indicate networking errors. |
519 | | * |
520 | | * Note that different platforms may return different codes for the same error, |
521 | | * so you likely shouldn't be checking the value returned by this function |
522 | | * unless you know what you are doing, you likely just want to use it in |
523 | | * combination with `net_new_strerror()` to print the error. |
524 | | * |
525 | | * return platform-dependent network error code, if any. |
526 | | */ |
527 | | int net_error(void); |
528 | | |
529 | | /** @brief Get a text explanation for the error code from `net_error()`. |
530 | | * |
531 | | * return NULL on failure. |
532 | | * return pointer to a NULL-terminated string describing the error code on |
533 | | * success. The returned string must be freed using `net_kill_strerror()`. |
534 | | */ |
535 | | char *net_new_strerror(int error); |
536 | | |
537 | | /** @brief Frees the string returned by `net_new_strerror()`. |
538 | | * It's valid to pass NULL as the argument, the function does nothing in this |
539 | | * case. |
540 | | */ |
541 | | nullable(1) |
542 | | void net_kill_strerror(char *strerror); |
543 | | |
544 | | /** @brief Initialize networking. |
545 | | * Bind to ip and port. |
546 | | * ip must be in network order EX: 127.0.0.1 = (7F000001). |
547 | | * port is in host byte order (this means don't worry about it). |
548 | | * |
549 | | * @return Networking_Core object if no problems |
550 | | * @retval NULL if there are problems. |
551 | | * |
552 | | * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. |
553 | | */ |
554 | | non_null(1, 2, 3, 4) nullable(7) |
555 | | Networking_Core *new_networking_ex( |
556 | | const Logger *log, const Memory *mem, const Network *ns, const IP *ip, |
557 | | uint16_t port_from, uint16_t port_to, unsigned int *error); |
558 | | |
559 | | non_null() |
560 | | Networking_Core *new_networking_no_udp(const Logger *log, const Memory *mem, const Network *ns); |
561 | | |
562 | | /** Function to cleanup networking stuff (doesn't do much right now). */ |
563 | | nullable(1) |
564 | | void kill_networking(Networking_Core *net); |
565 | | |
566 | | #ifdef __cplusplus |
567 | | } /* extern "C" */ |
568 | | #endif |
569 | | |
570 | | #endif /* C_TOXCORE_TOXCORE_NETWORK_H */ |