diff options
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 554 |
1 files changed, 317 insertions, 237 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 8d2ccfce..a1d83842 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -1,172 +1,211 @@ | |||
1 | /* network.h | 1 | /* |
2 | * | ||
3 | * Datatypes, functions and includes for the core networking. | 2 | * Datatypes, functions and includes for the core networking. |
3 | */ | ||
4 | |||
5 | /* | ||
6 | * Copyright © 2016-2018 The TokTok team. | ||
7 | * Copyright © 2013 Tox project. | ||
4 | * | 8 | * |
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | 9 | * This file is part of Tox, the free peer to peer instant messenger. |
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | 10 | * |
14 | * Tox is distributed in the hope that it will be useful, | 11 | * Tox is free software: you can redistribute it and/or modify |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 | * it under the terms of the GNU General Public License as published by |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 | * the Free Software Foundation, either version 3 of the License, or |
17 | * GNU General Public License for more details. | 14 | * (at your option) any later version. |
18 | * | 15 | * |
19 | * You should have received a copy of the GNU General Public License | 16 | * Tox is distributed in the hope that it will be useful, |
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
19 | * GNU General Public License for more details. | ||
21 | * | 20 | * |
21 | * You should have received a copy of the GNU General Public License | ||
22 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
22 | */ | 23 | */ |
24 | #ifndef C_TOXCORE_TOXCORE_NETWORK_H | ||
25 | #define C_TOXCORE_TOXCORE_NETWORK_H | ||
23 | 26 | ||
24 | #ifndef NETWORK_H | 27 | #include "logger.h" |
25 | #define NETWORK_H | ||
26 | |||
27 | #ifdef PLAN9 | ||
28 | #include <u.h> //Plan 9 requires this is imported first | ||
29 | #include <libc.h> | ||
30 | #endif | ||
31 | |||
32 | #include <stdlib.h> | ||
33 | #include <stdio.h> | ||
34 | #include <stdint.h> | ||
35 | #include <string.h> | ||
36 | #include <time.h> | ||
37 | |||
38 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) /* Put win32 includes here */ | ||
39 | #ifndef WINVER | ||
40 | //Windows XP | ||
41 | #define WINVER 0x0501 | ||
42 | #endif | ||
43 | #include <winsock2.h> | ||
44 | #include <windows.h> | ||
45 | #include <ws2tcpip.h> | ||
46 | |||
47 | #ifndef IPV6_V6ONLY | ||
48 | #define IPV6_V6ONLY 27 | ||
49 | #endif | ||
50 | 28 | ||
51 | typedef unsigned int sock_t; | 29 | #include <stdbool.h> // bool |
52 | /* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ | 30 | #include <stddef.h> // size_t |
53 | typedef short sa_family_t; | 31 | #include <stdint.h> // uint*_t |
54 | 32 | ||
55 | #ifndef EWOULDBLOCK | 33 | #ifdef __cplusplus |
56 | #define EWOULDBLOCK WSAEWOULDBLOCK | 34 | extern "C" { |
57 | #endif | 35 | #endif |
58 | 36 | ||
59 | #else // Linux includes | 37 | typedef struct Family { |
60 | 38 | uint8_t value; | |
61 | #include <fcntl.h> | 39 | } Family; |
62 | #include <sys/socket.h> | 40 | |
63 | #include <netinet/in.h> | 41 | bool net_family_is_unspec(Family family); |
64 | #include <arpa/inet.h> | 42 | bool net_family_is_ipv4(Family family); |
65 | #include <errno.h> | 43 | bool net_family_is_ipv6(Family family); |
66 | #include <sys/time.h> | 44 | bool net_family_is_tcp_family(Family family); |
67 | #include <sys/types.h> | 45 | bool net_family_is_tcp_onion(Family family); |
68 | #include <netdb.h> | 46 | bool net_family_is_tcp_ipv4(Family family); |
69 | #include <unistd.h> | 47 | bool net_family_is_tcp_ipv6(Family family); |
70 | 48 | bool net_family_is_tox_tcp_ipv4(Family family); | |
71 | typedef int sock_t; | 49 | bool net_family_is_tox_tcp_ipv6(Family family); |
72 | 50 | ||
73 | #endif | 51 | extern const Family net_family_unspec; |
52 | extern const Family net_family_ipv4; | ||
53 | extern const Family net_family_ipv6; | ||
54 | extern const Family net_family_tcp_family; | ||
55 | extern const Family net_family_tcp_onion; | ||
56 | extern const Family net_family_tcp_ipv4; | ||
57 | extern const Family net_family_tcp_ipv6; | ||
58 | extern const Family net_family_tox_tcp_ipv4; | ||
59 | extern const Family net_family_tox_tcp_ipv6; | ||
60 | |||
61 | typedef struct Socket { | ||
62 | int socket; | ||
63 | } Socket; | ||
64 | |||
65 | Socket net_socket(Family domain, int type, int protocol); | ||
66 | |||
67 | /** | ||
68 | * Check if socket is valid. | ||
69 | * | ||
70 | * @return true if valid, false otherwise. | ||
71 | */ | ||
72 | bool sock_valid(Socket sock); | ||
74 | 73 | ||
75 | #if defined(__AIX__) | 74 | extern const Socket net_invalid_socket; |
76 | # define _XOPEN_SOURCE 1 | ||
77 | #endif | ||
78 | 75 | ||
79 | #if defined(__sun__) | 76 | /** |
80 | #define __EXTENSIONS__ 1 // SunOS! | 77 | * Calls send(sockfd, buf, len, MSG_NOSIGNAL). |
81 | #if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) || defined(__SunOS5_9__) || defined(__SunOS5_10__) | 78 | */ |
82 | //Nothing needed | 79 | int net_send(Socket sock, const void *buf, size_t len); |
83 | #else | 80 | /** |
84 | #define __MAKECONTEXT_V2_SOURCE 1 | 81 | * Calls recv(sockfd, buf, len, MSG_NOSIGNAL). |
85 | #endif | 82 | */ |
86 | #endif | 83 | int net_recv(Socket sock, void *buf, size_t len); |
84 | /** | ||
85 | * Calls listen(sockfd, backlog). | ||
86 | */ | ||
87 | int net_listen(Socket sock, int backlog); | ||
88 | /** | ||
89 | * Calls accept(sockfd, nullptr, nullptr). | ||
90 | */ | ||
91 | Socket net_accept(Socket sock); | ||
87 | 92 | ||
88 | #ifndef IPV6_ADD_MEMBERSHIP | 93 | /** |
89 | #ifdef IPV6_JOIN_GROUP | 94 | * return the amount of data in the tcp recv buffer. |
90 | #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP | 95 | * return 0 on failure. |
91 | #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP | 96 | */ |
92 | #endif | 97 | size_t net_socket_data_recv_buffer(Socket sock); |
93 | #endif | ||
94 | 98 | ||
95 | #define MAX_UDP_PACKET_SIZE 2048 | 99 | #define MAX_UDP_PACKET_SIZE 2048 |
96 | 100 | ||
97 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ | 101 | typedef enum Net_Packet_Type { |
98 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ | 102 | NET_PACKET_PING_REQUEST = 0x00, /* Ping request packet ID. */ |
99 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ | 103 | NET_PACKET_PING_RESPONSE = 0x01, /* Ping response packet ID. */ |
100 | #define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */ | 104 | NET_PACKET_GET_NODES = 0x02, /* Get nodes request packet ID. */ |
101 | #define NET_PACKET_COOKIE_REQUEST 24 /* Cookie request packet */ | 105 | NET_PACKET_SEND_NODES_IPV6 = 0x04, /* Send nodes response packet ID for other addresses. */ |
102 | #define NET_PACKET_COOKIE_RESPONSE 25 /* Cookie response packet */ | 106 | NET_PACKET_COOKIE_REQUEST = 0x18, /* Cookie request packet */ |
103 | #define NET_PACKET_CRYPTO_HS 26 /* Crypto handshake packet */ | 107 | NET_PACKET_COOKIE_RESPONSE = 0x19, /* Cookie response packet */ |
104 | #define NET_PACKET_CRYPTO_DATA 27 /* Crypto data packet */ | 108 | NET_PACKET_CRYPTO_HS = 0x1a, /* Crypto handshake packet */ |
105 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ | 109 | NET_PACKET_CRYPTO_DATA = 0x1b, /* Crypto data packet */ |
106 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ | 110 | NET_PACKET_CRYPTO = 0x20, /* Encrypted data packet ID. */ |
111 | NET_PACKET_LAN_DISCOVERY = 0x21, /* LAN discovery packet ID. */ | ||
112 | |||
113 | /* See: docs/Prevent_Tracking.txt and onion.{c,h} */ | ||
114 | NET_PACKET_ONION_SEND_INITIAL = 0x80, | ||
115 | NET_PACKET_ONION_SEND_1 = 0x81, | ||
116 | NET_PACKET_ONION_SEND_2 = 0x82, | ||
107 | 117 | ||
108 | /* See: docs/Prevent_Tracking.txt and onion.{c, h} */ | 118 | NET_PACKET_ANNOUNCE_REQUEST = 0x83, |
109 | #define NET_PACKET_ONION_SEND_INITIAL 128 | 119 | NET_PACKET_ANNOUNCE_RESPONSE = 0x84, |
110 | #define NET_PACKET_ONION_SEND_1 129 | 120 | NET_PACKET_ONION_DATA_REQUEST = 0x85, |
111 | #define NET_PACKET_ONION_SEND_2 130 | 121 | NET_PACKET_ONION_DATA_RESPONSE = 0x86, |
112 | 122 | ||
113 | #define NET_PACKET_ANNOUNCE_REQUEST 131 | 123 | NET_PACKET_ONION_RECV_3 = 0x8c, |
114 | #define NET_PACKET_ANNOUNCE_RESPONSE 132 | 124 | NET_PACKET_ONION_RECV_2 = 0x8d, |
115 | #define NET_PACKET_ONION_DATA_REQUEST 133 | 125 | NET_PACKET_ONION_RECV_1 = 0x8e, |
116 | #define NET_PACKET_ONION_DATA_RESPONSE 134 | ||
117 | 126 | ||
118 | #define NET_PACKET_ONION_RECV_3 140 | 127 | BOOTSTRAP_INFO_PACKET_ID = 0xf0, /* Only used for bootstrap nodes */ |
119 | #define NET_PACKET_ONION_RECV_2 141 | ||
120 | #define NET_PACKET_ONION_RECV_1 142 | ||
121 | 128 | ||
122 | /* Only used for bootstrap nodes */ | 129 | NET_PACKET_MAX = 0xff, /* This type must remain within a single uint8. */ |
123 | #define BOOTSTRAP_INFO_PACKET_ID 240 | 130 | } Net_Packet_Type; |
124 | 131 | ||
125 | 132 | ||
126 | #define TOX_PORTRANGE_FROM 33445 | 133 | #define TOX_PORTRANGE_FROM 33445 |
127 | #define TOX_PORTRANGE_TO 33545 | 134 | #define TOX_PORTRANGE_TO 33545 |
128 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM | 135 | #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM |
129 | 136 | ||
137 | /* Redefinitions of variables for safe transfer over wire. */ | ||
138 | #define TOX_AF_UNSPEC 0 | ||
139 | #define TOX_AF_INET 2 | ||
140 | #define TOX_AF_INET6 10 | ||
141 | #define TOX_TCP_INET 130 | ||
142 | #define TOX_TCP_INET6 138 | ||
143 | |||
144 | #define TOX_SOCK_STREAM 1 | ||
145 | #define TOX_SOCK_DGRAM 2 | ||
146 | |||
147 | #define TOX_PROTO_TCP 1 | ||
148 | #define TOX_PROTO_UDP 2 | ||
149 | |||
130 | /* TCP related */ | 150 | /* TCP related */ |
131 | #define TCP_ONION_FAMILY (AF_INET6 + 1) | 151 | #define TCP_ONION_FAMILY (TOX_AF_INET6 + 1) |
132 | #define TCP_INET (AF_INET6 + 2) | 152 | #define TCP_INET (TOX_AF_INET6 + 2) |
133 | #define TCP_INET6 (AF_INET6 + 3) | 153 | #define TCP_INET6 (TOX_AF_INET6 + 3) |
134 | #define TCP_FAMILY (AF_INET6 + 4) | 154 | #define TCP_FAMILY (TOX_AF_INET6 + 4) |
135 | 155 | ||
136 | typedef union { | 156 | typedef union IP4 { |
137 | uint8_t uint8[4]; | ||
138 | uint16_t uint16[2]; | ||
139 | uint32_t uint32; | 157 | uint32_t uint32; |
140 | struct in_addr in_addr; | 158 | uint16_t uint16[2]; |
141 | } | 159 | uint8_t uint8[4]; |
142 | IP4; | 160 | } IP4; |
143 | 161 | ||
144 | typedef union { | 162 | IP4 get_ip4_loopback(void); |
163 | extern const IP4 ip4_broadcast; | ||
164 | |||
165 | typedef union IP6 { | ||
145 | uint8_t uint8[16]; | 166 | uint8_t uint8[16]; |
146 | uint16_t uint16[8]; | 167 | uint16_t uint16[8]; |
147 | uint32_t uint32[4]; | 168 | uint32_t uint32[4]; |
148 | uint64_t uint64[2]; | 169 | uint64_t uint64[2]; |
149 | struct in6_addr in6_addr; | 170 | } IP6; |
150 | } | 171 | |
151 | IP6; | 172 | IP6 get_ip6_loopback(void); |
152 | 173 | extern const IP6 ip6_broadcast; | |
153 | typedef struct { | 174 | |
154 | uint8_t family; | 175 | typedef union IP_Union { |
155 | union { | 176 | IP4 v4; |
156 | IP4 ip4; | 177 | IP6 v6; |
157 | IP6 ip6; | 178 | } IP_Union; |
158 | }; | 179 | |
159 | } | 180 | #define IP_DEFINED |
160 | IP; | 181 | typedef struct IP { |
161 | 182 | Family family; | |
162 | typedef struct { | 183 | IP_Union ip; |
184 | } IP; | ||
185 | |||
186 | #define IP_PORT_DEFINED | ||
187 | typedef struct IP_Port { | ||
163 | IP ip; | 188 | IP ip; |
164 | uint16_t port; | 189 | uint16_t port; |
165 | } | 190 | } IP_Port; |
166 | IP_Port; | 191 | |
192 | /* Convert values between host and network byte order. | ||
193 | */ | ||
194 | uint32_t net_htonl(uint32_t hostlong); | ||
195 | uint16_t net_htons(uint16_t hostshort); | ||
196 | uint32_t net_ntohl(uint32_t hostlong); | ||
197 | uint16_t net_ntohs(uint16_t hostshort); | ||
198 | |||
199 | size_t net_pack_u16(uint8_t *bytes, uint16_t v); | ||
200 | size_t net_pack_u32(uint8_t *bytes, uint32_t v); | ||
201 | size_t net_pack_u64(uint8_t *bytes, uint64_t v); | ||
202 | |||
203 | size_t net_unpack_u16(const uint8_t *bytes, uint16_t *v); | ||
204 | size_t net_unpack_u32(const uint8_t *bytes, uint32_t *v); | ||
205 | size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v); | ||
167 | 206 | ||
168 | /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ | 207 | /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ |
169 | #define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) | 208 | bool ipv6_ipv4_in_v6(IP6 a); |
170 | 209 | ||
171 | #define SIZE_IP4 4 | 210 | #define SIZE_IP4 4 |
172 | #define SIZE_IP6 16 | 211 | #define SIZE_IP6 16 |
@@ -174,137 +213,130 @@ IP_Port; | |||
174 | #define SIZE_PORT 2 | 213 | #define SIZE_PORT 2 |
175 | #define SIZE_IPPORT (SIZE_IP + SIZE_PORT) | 214 | #define SIZE_IPPORT (SIZE_IP + SIZE_PORT) |
176 | 215 | ||
177 | #define TOX_ENABLE_IPV6_DEFAULT 1 | 216 | #define TOX_ENABLE_IPV6_DEFAULT true |
178 | 217 | ||
179 | /* addr_resolve return values */ | 218 | /* addr_resolve return values */ |
180 | #define TOX_ADDR_RESOLVE_INET 1 | 219 | #define TOX_ADDR_RESOLVE_INET 1 |
181 | #define TOX_ADDR_RESOLVE_INET6 2 | 220 | #define TOX_ADDR_RESOLVE_INET6 2 |
182 | 221 | ||
222 | #define TOX_INET6_ADDRSTRLEN 66 | ||
223 | #define TOX_INET_ADDRSTRLEN 22 | ||
224 | |||
183 | /* ip_ntoa | 225 | /* ip_ntoa |
184 | * converts ip into a string | 226 | * converts ip into a string |
185 | * uses a static buffer, so mustn't used multiple times in the same output | 227 | * ip_str must be of length at least IP_NTOA_LEN |
186 | * | 228 | * |
187 | * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]" | 229 | * IPv6 addresses are enclosed into square brackets, i.e. "[IPv6]" |
188 | * writes error message into the buffer on error | 230 | * writes error message into the buffer on error |
231 | * | ||
232 | * returns ip_str | ||
189 | */ | 233 | */ |
190 | const char *ip_ntoa(const IP *ip); | 234 | /* this would be TOX_INET6_ADDRSTRLEN, but it might be too short for the error message */ |
235 | #define IP_NTOA_LEN 96 // TODO(irungentoo): magic number. Why not INET6_ADDRSTRLEN ? | ||
236 | const char *ip_ntoa(const IP *ip, char *ip_str, size_t length); | ||
191 | 237 | ||
192 | /* | 238 | /** |
193 | * ip_parse_addr | 239 | * Parses IP structure into an address string. |
194 | * parses IP structure into an address string | ||
195 | * | 240 | * |
196 | * input | 241 | * @param ip IP of TOX_AF_INET or TOX_AF_INET6 families. |
197 | * ip: ip of AF_INET or AF_INET6 families | 242 | * @param length length of the address buffer. |
198 | * length: length of the address buffer | 243 | * Must be at least TOX_INET_ADDRSTRLEN for TOX_AF_INET |
199 | * Must be at least INET_ADDRSTRLEN for AF_INET | 244 | * and TOX_INET6_ADDRSTRLEN for TOX_AF_INET6 |
200 | * and INET6_ADDRSTRLEN for AF_INET6 | ||
201 | * | 245 | * |
202 | * output | 246 | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
203 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
204 | * | 247 | * |
205 | * returns 1 on success, 0 on failure | 248 | * @return true on success, false on failure. |
206 | */ | 249 | */ |
207 | int ip_parse_addr(const IP *ip, char *address, size_t length); | 250 | bool ip_parse_addr(const IP *ip, char *address, size_t length); |
208 | 251 | ||
209 | /* | 252 | /** |
210 | * addr_parse_ip | 253 | * Directly parses the input into an IP structure. |
211 | * directly parses the input into an IP structure | ||
212 | * tries IPv4 first, then IPv6 | ||
213 | * | 254 | * |
214 | * input | 255 | * Tries IPv4 first, then IPv6. |
215 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
216 | * | 256 | * |
217 | * output | 257 | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
218 | * IP: family and the value is set on success | 258 | * @param to family and the value is set on success. |
219 | * | 259 | * |
220 | * returns 1 on success, 0 on failure | 260 | * @return true on success, false on failure. |
221 | */ | 261 | */ |
222 | int addr_parse_ip(const char *address, IP *to); | 262 | bool addr_parse_ip(const char *address, IP *to); |
223 | 263 | ||
224 | /* ip_equal | 264 | /** |
225 | * compares two IPAny structures | 265 | * Compares two IPAny structures. |
226 | * unset means unequal | 266 | * |
267 | * Unset means unequal. | ||
227 | * | 268 | * |
228 | * returns 0 when not equal or when uninitialized | 269 | * @return false when not equal or when uninitialized. |
229 | */ | 270 | */ |
230 | int ip_equal(const IP *a, const IP *b); | 271 | bool ip_equal(const IP *a, const IP *b); |
231 | 272 | ||
232 | /* ipport_equal | 273 | /** |
233 | * compares two IPAny_Port structures | 274 | * Compares two IPAny_Port structures. |
234 | * unset means unequal | ||
235 | * | 275 | * |
236 | * returns 0 when not equal or when uninitialized | 276 | * Unset means unequal. |
277 | * | ||
278 | * @return false when not equal or when uninitialized. | ||
237 | */ | 279 | */ |
238 | int ipport_equal(const IP_Port *a, const IP_Port *b); | 280 | bool ipport_equal(const IP_Port *a, const IP_Port *b); |
239 | 281 | ||
240 | /* nulls out ip */ | 282 | /* nulls out ip */ |
241 | void ip_reset(IP *ip); | 283 | void ip_reset(IP *ip); |
242 | /* nulls out ip, sets family according to flag */ | 284 | /* nulls out ip, sets family according to flag */ |
243 | void ip_init(IP *ip, uint8_t ipv6enabled); | 285 | void ip_init(IP *ip, bool ipv6enabled); |
244 | /* checks if ip is valid */ | 286 | /* checks if ip is valid */ |
245 | int ip_isset(const IP *ip); | 287 | bool ip_isset(const IP *ip); |
246 | /* checks if ip is valid */ | 288 | /* checks if ip is valid */ |
247 | int ipport_isset(const IP_Port *ipport); | 289 | bool ipport_isset(const IP_Port *ipport); |
248 | /* copies an ip structure */ | 290 | /* copies an ip structure */ |
249 | void ip_copy(IP *target, const IP *source); | 291 | void ip_copy(IP *target, const IP *source); |
250 | /* copies an ip_port structure */ | 292 | /* copies an ip_port structure */ |
251 | void ipport_copy(IP_Port *target, const IP_Port *source); | 293 | void ipport_copy(IP_Port *target, const IP_Port *source); |
252 | 294 | ||
253 | /* | 295 | /** |
254 | * addr_resolve(): | 296 | * Uses getaddrinfo to resolve an address into an IP address. |
255 | * uses getaddrinfo to resolve an address into an IP address | 297 | * |
256 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | 298 | * Uses the first IPv4/IPv6 addresses returned by getaddrinfo. |
257 | * | 299 | * |
258 | * input | 300 | * @param address a hostname (or something parseable to an IP address) |
259 | * address: a hostname (or something parseable to an IP address) | 301 | * @param to to.family MUST be initialized, either set to a specific IP version |
260 | * to: to.family MUST be initialized, either set to a specific IP version | 302 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both |
261 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
262 | * IP versions are acceptable | 303 | * IP versions are acceptable |
263 | * extra can be NULL and is only set in special circumstances, see returns | 304 | * @param extra can be NULL and is only set in special circumstances, see returns |
264 | * | 305 | * |
265 | * returns in *to a valid IPAny (v4/v6), | 306 | * returns in *to a valid IPAny (v4/v6), |
266 | * prefers v6 if ip.family was AF_UNSPEC and both available | 307 | * prefers v6 if ip.family was TOX_AF_UNSPEC and both available |
267 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 | 308 | * returns in *extra an IPv4 address, if family was TOX_AF_UNSPEC and *to is TOX_AF_INET6 |
268 | * returns 0 on failure | 309 | * |
310 | * @return 0 on failure, TOX_ADDR_RESOLVE_* on success. | ||
269 | */ | 311 | */ |
270 | int addr_resolve(const char *address, IP *to, IP *extra); | 312 | int addr_resolve(const char *address, IP *to, IP *extra); |
271 | 313 | ||
272 | /* | 314 | /** |
273 | * addr_resolve_or_parse_ip | 315 | * Resolves string into an IP address |
274 | * resolves string into an IP address | ||
275 | * | 316 | * |
276 | * address: a hostname (or something parseable to an IP address) | 317 | * @param address a hostname (or something parseable to an IP address) |
277 | * to: to.family MUST be initialized, either set to a specific IP version | 318 | * @param to to.family MUST be initialized, either set to a specific IP version |
278 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | 319 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both |
279 | * IP versions are acceptable | 320 | * IP versions are acceptable |
280 | * extra can be NULL and is only set in special circumstances, see returns | 321 | * @param extra can be NULL and is only set in special circumstances, see returns |
322 | * | ||
323 | * returns in *tro a matching address (IPv6 or IPv4) | ||
324 | * returns in *extra, if not NULL, an IPv4 address, if to->family was TOX_AF_UNSPEC | ||
281 | * | 325 | * |
282 | * returns in *tro a matching address (IPv6 or IPv4) | 326 | * @return true on success, false on failure |
283 | * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC | ||
284 | * returns 1 on success | ||
285 | * returns 0 on failure | ||
286 | */ | 327 | */ |
287 | int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra); | 328 | bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra); |
288 | 329 | ||
289 | /* Function to receive data, ip and port of sender is put into ip_port. | 330 | /* Function to receive data, ip and port of sender is put into ip_port. |
290 | * Packet data is put into data. | 331 | * Packet data is put into data. |
291 | * Packet length is put into length. | 332 | * Packet length is put into length. |
292 | */ | 333 | */ |
293 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, const uint8_t *data, uint16_t len); | 334 | typedef int packet_handler_cb(void *object, IP_Port ip_port, const uint8_t *data, uint16_t len, void *userdata); |
294 | 335 | ||
295 | typedef struct { | 336 | typedef struct Networking_Core Networking_Core; |
296 | packet_handler_callback function; | ||
297 | void *object; | ||
298 | } Packet_Handles; | ||
299 | 337 | ||
300 | typedef struct { | 338 | Family net_family(const Networking_Core *net); |
301 | Packet_Handles packethandlers[256]; | 339 | uint16_t net_port(const Networking_Core *net); |
302 | |||
303 | sa_family_t family; | ||
304 | uint16_t port; | ||
305 | /* Our UDP socket. */ | ||
306 | sock_t sock; | ||
307 | } Networking_Core; | ||
308 | 340 | ||
309 | /* Run this before creating sockets. | 341 | /* Run this before creating sockets. |
310 | * | 342 | * |
@@ -313,47 +345,37 @@ typedef struct { | |||
313 | */ | 345 | */ |
314 | int networking_at_startup(void); | 346 | int networking_at_startup(void); |
315 | 347 | ||
316 | /* Check if socket is valid. | ||
317 | * | ||
318 | * return 1 if valid | ||
319 | * return 0 if not valid | ||
320 | */ | ||
321 | int sock_valid(sock_t sock); | ||
322 | |||
323 | /* Close the socket. | 348 | /* Close the socket. |
324 | */ | 349 | */ |
325 | void kill_sock(sock_t sock); | 350 | void kill_sock(Socket sock); |
326 | 351 | ||
327 | /* Set socket as nonblocking | 352 | /** |
353 | * Set socket as nonblocking | ||
328 | * | 354 | * |
329 | * return 1 on success | 355 | * @return true on success, false on failure. |
330 | * return 0 on failure | ||
331 | */ | 356 | */ |
332 | int set_socket_nonblock(sock_t sock); | 357 | bool set_socket_nonblock(Socket sock); |
333 | 358 | ||
334 | /* Set socket to not emit SIGPIPE | 359 | /** |
360 | * Set socket to not emit SIGPIPE | ||
335 | * | 361 | * |
336 | * return 1 on success | 362 | * @return true on success, false on failure. |
337 | * return 0 on failure | ||
338 | */ | 363 | */ |
339 | int set_socket_nosigpipe(sock_t sock); | 364 | bool set_socket_nosigpipe(Socket sock); |
340 | 365 | ||
341 | /* Enable SO_REUSEADDR on socket. | 366 | /** |
367 | * Enable SO_REUSEADDR on socket. | ||
342 | * | 368 | * |
343 | * return 1 on success | 369 | * @return true on success, false on failure. |
344 | * return 0 on failure | ||
345 | */ | 370 | */ |
346 | int set_socket_reuseaddr(sock_t sock); | 371 | bool set_socket_reuseaddr(Socket sock); |
347 | 372 | ||
348 | /* Set socket to dual (IPv4 + IPv6 socket) | 373 | /** |
374 | * Set socket to dual (IPv4 + IPv6 socket) | ||
349 | * | 375 | * |
350 | * return 1 on success | 376 | * @return true on success, false on failure. |
351 | * return 0 on failure | ||
352 | */ | 377 | */ |
353 | int set_socket_dualstack(sock_t sock); | 378 | bool set_socket_dualstack(Socket sock); |
354 | |||
355 | /* return current monotonic time in milliseconds (ms). */ | ||
356 | uint64_t current_time_monotonic(void); | ||
357 | 379 | ||
358 | /* Basic network functions: */ | 380 | /* Basic network functions: */ |
359 | 381 | ||
@@ -361,10 +383,63 @@ uint64_t current_time_monotonic(void); | |||
361 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length); | 383 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length); |
362 | 384 | ||
363 | /* Function to call when packet beginning with byte is received. */ | 385 | /* Function to call when packet beginning with byte is received. */ |
364 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 386 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_cb *cb, void *object); |
365 | 387 | ||
366 | /* Call this several times a second. */ | 388 | /* Call this several times a second. */ |
367 | void networking_poll(Networking_Core *net); | 389 | void networking_poll(Networking_Core *net, void *userdata); |
390 | |||
391 | /* Connect a socket to the address specified by the ip_port. */ | ||
392 | int net_connect(Socket sock, IP_Port ip_port); | ||
393 | |||
394 | /* High-level getaddrinfo implementation. | ||
395 | * Given node, which identifies an Internet host, net_getipport() fills an array | ||
396 | * with one or more IP_Port structures, each of which contains an Internet | ||
397 | * address that can be specified by calling net_connect(), the port is ignored. | ||
398 | * | ||
399 | * Skip all addresses with socktype != type (use type = -1 to get all addresses) | ||
400 | * To correctly deallocate array memory use net_freeipport() | ||
401 | * | ||
402 | * return number of elements in res array | ||
403 | * and -1 on error. | ||
404 | */ | ||
405 | int32_t net_getipport(const char *node, IP_Port **res, int tox_type); | ||
406 | |||
407 | /* Deallocates memory allocated by net_getipport | ||
408 | */ | ||
409 | void net_freeipport(IP_Port *ip_ports); | ||
410 | |||
411 | /** | ||
412 | * @return true on success, false on failure. | ||
413 | */ | ||
414 | bool bind_to_port(Socket sock, Family family, uint16_t port); | ||
415 | |||
416 | /* Get the last networking error code. | ||
417 | * | ||
418 | * Similar to Unix's errno, but cross-platform, as not all platforms use errno | ||
419 | * to indicate networking errors. | ||
420 | * | ||
421 | * Note that different platforms may return different codes for the same error, | ||
422 | * so you likely shouldn't be checking the value returned by this function | ||
423 | * unless you know what you are doing, you likely just want to use it in | ||
424 | * combination with net_new_strerror() to print the error. | ||
425 | * | ||
426 | * return platform-dependent network error code, if any. | ||
427 | */ | ||
428 | int net_error(void); | ||
429 | |||
430 | /* Get a text explanation for the error code from net_error(). | ||
431 | * | ||
432 | * return NULL on failure. | ||
433 | * return pointer to a NULL-terminated string describing the error code on | ||
434 | * success. The returned string must be freed using net_kill_strerror(). | ||
435 | */ | ||
436 | const char *net_new_strerror(int error); | ||
437 | |||
438 | /* Frees the string returned by net_new_strerror(). | ||
439 | * It's valid to pass NULL as the argument, the function does nothing in this | ||
440 | * case. | ||
441 | */ | ||
442 | void net_kill_strerror(const char *strerror); | ||
368 | 443 | ||
369 | /* Initialize networking. | 444 | /* Initialize networking. |
370 | * bind to ip and port. | 445 | * bind to ip and port. |
@@ -376,10 +451,15 @@ void networking_poll(Networking_Core *net); | |||
376 | * | 451 | * |
377 | * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. | 452 | * If error is non NULL it is set to 0 if no issues, 1 if socket related error, 2 if other. |
378 | */ | 453 | */ |
379 | Networking_Core *new_networking(IP ip, uint16_t port); | 454 | Networking_Core *new_networking(const Logger *log, IP ip, uint16_t port); |
380 | Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error); | 455 | Networking_Core *new_networking_ex(const Logger *log, IP ip, uint16_t port_from, uint16_t port_to, unsigned int *error); |
456 | Networking_Core *new_networking_no_udp(const Logger *log); | ||
381 | 457 | ||
382 | /* Function to cleanup networking stuff (doesn't do much right now). */ | 458 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
383 | void kill_networking(Networking_Core *net); | 459 | void kill_networking(Networking_Core *net); |
384 | 460 | ||
461 | #ifdef __cplusplus | ||
462 | } // extern "C" | ||
463 | #endif | ||
464 | |||
385 | #endif | 465 | #endif |