diff options
-rw-r--r-- | toxcore/DHT.c | 6 | ||||
-rw-r--r-- | toxcore/DHT.h | 6 | ||||
-rw-r--r-- | toxcore/network.c | 158 | ||||
-rw-r--r-- | toxcore/network.h | 145 |
4 files changed, 105 insertions, 210 deletions
diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 46db6e76..dc21575e 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c | |||
@@ -281,7 +281,7 @@ void get_shared_key(const Mono_Time *mono_time, Shared_Keys *shared_keys, uint8_ | |||
281 | 281 | ||
282 | if (num != UINT32_MAX) { | 282 | if (num != UINT32_MAX) { |
283 | Shared_Key *const key = &shared_keys->keys[curr]; | 283 | Shared_Key *const key = &shared_keys->keys[curr]; |
284 | key->stored = 1; | 284 | key->stored = true; |
285 | key->times_requested = 1; | 285 | key->times_requested = 1; |
286 | memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); | 286 | memcpy(key->public_key, public_key, CRYPTO_PUBLIC_KEY_SIZE); |
287 | memcpy(key->shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE); | 287 | memcpy(key->shared_key, shared_key, CRYPTO_SHARED_KEY_SIZE); |
@@ -491,7 +491,7 @@ static int dht_create_packet(const uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE], | |||
491 | * Return size of unpacked ip_port on success. | 491 | * Return size of unpacked ip_port on success. |
492 | * Return -1 on failure. | 492 | * Return -1 on failure. |
493 | */ | 493 | */ |
494 | int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled) | 494 | int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled) |
495 | { | 495 | { |
496 | if (data == nullptr) { | 496 | if (data == nullptr) { |
497 | return -1; | 497 | return -1; |
@@ -589,7 +589,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ | |||
589 | * return -1 on failure. | 589 | * return -1 on failure. |
590 | */ | 590 | */ |
591 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, | 591 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, |
592 | uint16_t length, uint8_t tcp_enabled) | 592 | uint16_t length, bool tcp_enabled) |
593 | { | 593 | { |
594 | uint32_t num = 0, len_processed = 0; | 594 | uint32_t num = 0, len_processed = 0; |
595 | 595 | ||
diff --git a/toxcore/DHT.h b/toxcore/DHT.h index bb17b3ea..ef3fd0b0 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h | |||
@@ -174,7 +174,7 @@ int pack_ip_port(uint8_t *data, uint16_t length, const IP_Port *ip_port); | |||
174 | * Return size of unpacked ip_port on success. | 174 | * Return size of unpacked ip_port on success. |
175 | * Return -1 on failure. | 175 | * Return -1 on failure. |
176 | */ | 176 | */ |
177 | int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, uint8_t tcp_enabled); | 177 | int unpack_ip_port(IP_Port *ip_port, const uint8_t *data, uint16_t length, bool tcp_enabled); |
178 | 178 | ||
179 | /* Pack number of nodes into data of maxlength length. | 179 | /* Pack number of nodes into data of maxlength length. |
180 | * | 180 | * |
@@ -191,7 +191,7 @@ int pack_nodes(uint8_t *data, uint16_t length, const Node_format *nodes, uint16_ | |||
191 | * return -1 on failure. | 191 | * return -1 on failure. |
192 | */ | 192 | */ |
193 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, | 193 | int unpack_nodes(Node_format *nodes, uint16_t max_num_nodes, uint16_t *processed_data_len, const uint8_t *data, |
194 | uint16_t length, uint8_t tcp_enabled); | 194 | uint16_t length, bool tcp_enabled); |
195 | 195 | ||
196 | 196 | ||
197 | /*----------------------------------------------------------------------------------*/ | 197 | /*----------------------------------------------------------------------------------*/ |
@@ -203,7 +203,7 @@ typedef struct Shared_Key { | |||
203 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; | 203 | uint8_t public_key[CRYPTO_PUBLIC_KEY_SIZE]; |
204 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; | 204 | uint8_t shared_key[CRYPTO_SHARED_KEY_SIZE]; |
205 | uint32_t times_requested; | 205 | uint32_t times_requested; |
206 | uint8_t stored; /* 0 if not, 1 if is */ | 206 | bool stored; |
207 | uint64_t time_last_requested; | 207 | uint64_t time_last_requested; |
208 | } Shared_Key; | 208 | } Shared_Key; |
209 | 209 | ||
diff --git a/toxcore/network.c b/toxcore/network.c index efc7f7e4..917556ac 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -346,12 +346,7 @@ bool net_family_is_tox_tcp_ipv6(Family family) | |||
346 | return family.value == net_family_tox_tcp_ipv6.value; | 346 | return family.value == net_family_tox_tcp_ipv6.value; |
347 | } | 347 | } |
348 | 348 | ||
349 | /* Check if socket is valid. | 349 | bool sock_valid(Socket sock) |
350 | * | ||
351 | * return 1 if valid | ||
352 | * return 0 if not valid | ||
353 | */ | ||
354 | int sock_valid(Socket sock) | ||
355 | { | 350 | { |
356 | return sock.socket != net_invalid_socket.socket; | 351 | return sock.socket != net_invalid_socket.socket; |
357 | } | 352 | } |
@@ -367,60 +362,40 @@ void kill_sock(Socket sock) | |||
367 | #endif | 362 | #endif |
368 | } | 363 | } |
369 | 364 | ||
370 | /* Set socket as nonblocking | 365 | bool set_socket_nonblock(Socket sock) |
371 | * | ||
372 | * return 1 on success | ||
373 | * return 0 on failure | ||
374 | */ | ||
375 | int set_socket_nonblock(Socket sock) | ||
376 | { | 366 | { |
377 | #ifdef OS_WIN32 | 367 | #ifdef OS_WIN32 |
378 | u_long mode = 1; | 368 | u_long mode = 1; |
379 | return (ioctlsocket(sock.socket, FIONBIO, &mode) == 0); | 369 | return ioctlsocket(sock.socket, FIONBIO, &mode) == 0; |
380 | #else | 370 | #else |
381 | return (fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0); | 371 | return fcntl(sock.socket, F_SETFL, O_NONBLOCK, 1) == 0; |
382 | #endif | 372 | #endif |
383 | } | 373 | } |
384 | 374 | ||
385 | /* Set socket to not emit SIGPIPE | 375 | bool set_socket_nosigpipe(Socket sock) |
386 | * | ||
387 | * return 1 on success | ||
388 | * return 0 on failure | ||
389 | */ | ||
390 | int set_socket_nosigpipe(Socket sock) | ||
391 | { | 376 | { |
392 | #if defined(__APPLE__) | 377 | #if defined(__APPLE__) |
393 | int set = 1; | 378 | int set = 1; |
394 | return setsockopt(sock.socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0; | 379 | return setsockopt(sock.socket, SOL_SOCKET, SO_NOSIGPIPE, (const char *)&set, sizeof(int)) == 0; |
395 | #else | 380 | #else |
396 | return 1; | 381 | return true; |
397 | #endif | 382 | #endif |
398 | } | 383 | } |
399 | 384 | ||
400 | /* Enable SO_REUSEADDR on socket. | 385 | bool set_socket_reuseaddr(Socket sock) |
401 | * | ||
402 | * return 1 on success | ||
403 | * return 0 on failure | ||
404 | */ | ||
405 | int set_socket_reuseaddr(Socket sock) | ||
406 | { | 386 | { |
407 | int set = 1; | 387 | int set = 1; |
408 | return setsockopt(sock.socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0; | 388 | return setsockopt(sock.socket, SOL_SOCKET, SO_REUSEADDR, (const char *)&set, sizeof(set)) == 0; |
409 | } | 389 | } |
410 | 390 | ||
411 | /* Set socket to dual (IPv4 + IPv6 socket) | 391 | bool set_socket_dualstack(Socket sock) |
412 | * | ||
413 | * return 1 on success | ||
414 | * return 0 on failure | ||
415 | */ | ||
416 | int set_socket_dualstack(Socket sock) | ||
417 | { | 392 | { |
418 | int ipv6only = 0; | 393 | int ipv6only = 0; |
419 | socklen_t optsize = sizeof(ipv6only); | 394 | socklen_t optsize = sizeof(ipv6only); |
420 | int res = getsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize); | 395 | int res = getsockopt(sock.socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, &optsize); |
421 | 396 | ||
422 | if ((res == 0) && (ipv6only == 0)) { | 397 | if ((res == 0) && (ipv6only == 0)) { |
423 | return 1; | 398 | return true; |
424 | } | 399 | } |
425 | 400 | ||
426 | ipv6only = 0; | 401 | ipv6only = 0; |
@@ -973,16 +948,10 @@ void kill_networking(Networking_Core *net) | |||
973 | } | 948 | } |
974 | 949 | ||
975 | 950 | ||
976 | /* ip_equal | 951 | bool ip_equal(const IP *a, const IP *b) |
977 | * compares two IPAny structures | ||
978 | * unset means unequal | ||
979 | * | ||
980 | * returns 0 when not equal or when uninitialized | ||
981 | */ | ||
982 | int ip_equal(const IP *a, const IP *b) | ||
983 | { | 952 | { |
984 | if (!a || !b) { | 953 | if (!a || !b) { |
985 | return 0; | 954 | return false; |
986 | } | 955 | } |
987 | 956 | ||
988 | /* same family */ | 957 | /* same family */ |
@@ -1000,7 +969,7 @@ int ip_equal(const IP *a, const IP *b) | |||
1000 | a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; | 969 | a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; |
1001 | } | 970 | } |
1002 | 971 | ||
1003 | return 0; | 972 | return false; |
1004 | } | 973 | } |
1005 | 974 | ||
1006 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ | 975 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ |
@@ -1018,23 +987,17 @@ int ip_equal(const IP *a, const IP *b) | |||
1018 | } | 987 | } |
1019 | } | 988 | } |
1020 | 989 | ||
1021 | return 0; | 990 | return false; |
1022 | } | 991 | } |
1023 | 992 | ||
1024 | /* ipport_equal | 993 | bool ipport_equal(const IP_Port *a, const IP_Port *b) |
1025 | * compares two IPAny_Port structures | ||
1026 | * unset means unequal | ||
1027 | * | ||
1028 | * returns 0 when not equal or when uninitialized | ||
1029 | */ | ||
1030 | int ipport_equal(const IP_Port *a, const IP_Port *b) | ||
1031 | { | 994 | { |
1032 | if (!a || !b) { | 995 | if (!a || !b) { |
1033 | return 0; | 996 | return false; |
1034 | } | 997 | } |
1035 | 998 | ||
1036 | if (!a->port || (a->port != b->port)) { | 999 | if (!a->port || (a->port != b->port)) { |
1037 | return 0; | 1000 | return false; |
1038 | } | 1001 | } |
1039 | 1002 | ||
1040 | return ip_equal(&a->ip, &b->ip); | 1003 | return ip_equal(&a->ip, &b->ip); |
@@ -1153,25 +1116,10 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
1153 | return ip_str; | 1116 | return ip_str; |
1154 | } | 1117 | } |
1155 | 1118 | ||
1156 | /* | 1119 | bool ip_parse_addr(const IP *ip, char *address, size_t length) |
1157 | * ip_parse_addr | ||
1158 | * parses IP structure into an address string | ||
1159 | * | ||
1160 | * input | ||
1161 | * ip: ip of TOX_AF_INET or TOX_AF_INET6 families | ||
1162 | * length: length of the address buffer | ||
1163 | * Must be at least INET_ADDRSTRLEN for TOX_AF_INET | ||
1164 | * and INET6_ADDRSTRLEN for TOX_AF_INET6 | ||
1165 | * | ||
1166 | * output | ||
1167 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
1168 | * | ||
1169 | * returns 1 on success, 0 on failure | ||
1170 | */ | ||
1171 | int ip_parse_addr(const IP *ip, char *address, size_t length) | ||
1172 | { | 1120 | { |
1173 | if (!address || !ip) { | 1121 | if (!address || !ip) { |
1174 | return 0; | 1122 | return false; |
1175 | } | 1123 | } |
1176 | 1124 | ||
1177 | if (net_family_is_ipv4(ip->family)) { | 1125 | if (net_family_is_ipv4(ip->family)) { |
@@ -1184,26 +1132,13 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) | |||
1184 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; | 1132 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; |
1185 | } | 1133 | } |
1186 | 1134 | ||
1187 | return 0; | 1135 | return false; |
1188 | } | 1136 | } |
1189 | 1137 | ||
1190 | /* | 1138 | bool addr_parse_ip(const char *address, IP *to) |
1191 | * addr_parse_ip | ||
1192 | * directly parses the input into an IP structure | ||
1193 | * tries IPv4 first, then IPv6 | ||
1194 | * | ||
1195 | * input | ||
1196 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
1197 | * | ||
1198 | * output | ||
1199 | * IP: family and the value is set on success | ||
1200 | * | ||
1201 | * returns 1 on success, 0 on failure | ||
1202 | */ | ||
1203 | int addr_parse_ip(const char *address, IP *to) | ||
1204 | { | 1139 | { |
1205 | if (!address || !to) { | 1140 | if (!address || !to) { |
1206 | return 0; | 1141 | return false; |
1207 | } | 1142 | } |
1208 | 1143 | ||
1209 | struct in_addr addr4; | 1144 | struct in_addr addr4; |
@@ -1211,7 +1146,7 @@ int addr_parse_ip(const char *address, IP *to) | |||
1211 | if (inet_pton(AF_INET, address, &addr4) == 1) { | 1146 | if (inet_pton(AF_INET, address, &addr4) == 1) { |
1212 | to->family = net_family_ipv4; | 1147 | to->family = net_family_ipv4; |
1213 | get_ip4(&to->ip.v4, &addr4); | 1148 | get_ip4(&to->ip.v4, &addr4); |
1214 | return 1; | 1149 | return true; |
1215 | } | 1150 | } |
1216 | 1151 | ||
1217 | struct in6_addr addr6; | 1152 | struct in6_addr addr6; |
@@ -1219,29 +1154,12 @@ int addr_parse_ip(const char *address, IP *to) | |||
1219 | if (inet_pton(AF_INET6, address, &addr6) == 1) { | 1154 | if (inet_pton(AF_INET6, address, &addr6) == 1) { |
1220 | to->family = net_family_ipv6; | 1155 | to->family = net_family_ipv6; |
1221 | get_ip6(&to->ip.v6, &addr6); | 1156 | get_ip6(&to->ip.v6, &addr6); |
1222 | return 1; | 1157 | return true; |
1223 | } | 1158 | } |
1224 | 1159 | ||
1225 | return 0; | 1160 | return false; |
1226 | } | 1161 | } |
1227 | 1162 | ||
1228 | /* | ||
1229 | * addr_resolve(): | ||
1230 | * uses getaddrinfo to resolve an address into an IP address | ||
1231 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | ||
1232 | * | ||
1233 | * input | ||
1234 | * address: a hostname (or something parseable to an IP address) | ||
1235 | * to: to.family MUST be initialized, either set to a specific IP version | ||
1236 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
1237 | * IP versions are acceptable | ||
1238 | * extra can be NULL and is only set in special circumstances, see returns | ||
1239 | * | ||
1240 | * returns in *to a valid IPAny (v4/v6), | ||
1241 | * prefers v6 if ip.family was AF_UNSPEC and both available | ||
1242 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is TOX_AF_INET6 | ||
1243 | * returns 0 on failure, TOX_ADDR_RESOLVE_* on success. | ||
1244 | */ | ||
1245 | int addr_resolve(const char *address, IP *to, IP *extra) | 1163 | int addr_resolve(const char *address, IP *to, IP *extra) |
1246 | { | 1164 | { |
1247 | if (!address || !to) { | 1165 | if (!address || !to) { |
@@ -1332,30 +1250,15 @@ int addr_resolve(const char *address, IP *to, IP *extra) | |||
1332 | return result; | 1250 | return result; |
1333 | } | 1251 | } |
1334 | 1252 | ||
1335 | /* | 1253 | bool addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) |
1336 | * addr_resolve_or_parse_ip | ||
1337 | * resolves string into an IP address | ||
1338 | * | ||
1339 | * address: a hostname (or something parseable to an IP address) | ||
1340 | * to: to.family MUST be initialized, either set to a specific IP version | ||
1341 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | ||
1342 | * IP versions are acceptable | ||
1343 | * extra can be NULL and is only set in special circumstances, see returns | ||
1344 | * | ||
1345 | * returns in *tro a matching address (IPv6 or IPv4) | ||
1346 | * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC | ||
1347 | * returns 1 on success | ||
1348 | * returns 0 on failure | ||
1349 | */ | ||
1350 | int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) | ||
1351 | { | 1254 | { |
1352 | if (!addr_resolve(address, to, extra)) { | 1255 | if (!addr_resolve(address, to, extra)) { |
1353 | if (!addr_parse_ip(address, to)) { | 1256 | if (!addr_parse_ip(address, to)) { |
1354 | return 0; | 1257 | return false; |
1355 | } | 1258 | } |
1356 | } | 1259 | } |
1357 | 1260 | ||
1358 | return 1; | 1261 | return true; |
1359 | } | 1262 | } |
1360 | 1263 | ||
1361 | int net_connect(Socket sock, IP_Port ip_port) | 1264 | int net_connect(Socket sock, IP_Port ip_port) |
@@ -1466,10 +1369,7 @@ void net_freeipport(IP_Port *ip_ports) | |||
1466 | free(ip_ports); | 1369 | free(ip_ports); |
1467 | } | 1370 | } |
1468 | 1371 | ||
1469 | /* return 1 on success | 1372 | bool bind_to_port(Socket sock, Family family, uint16_t port) |
1470 | * return 0 on failure | ||
1471 | */ | ||
1472 | int bind_to_port(Socket sock, Family family, uint16_t port) | ||
1473 | { | 1373 | { |
1474 | struct sockaddr_storage addr = {0}; | 1374 | struct sockaddr_storage addr = {0}; |
1475 | size_t addrsize; | 1375 | size_t addrsize; |
@@ -1487,7 +1387,7 @@ int bind_to_port(Socket sock, Family family, uint16_t port) | |||
1487 | addr6->sin6_family = AF_INET6; | 1387 | addr6->sin6_family = AF_INET6; |
1488 | addr6->sin6_port = net_htons(port); | 1388 | addr6->sin6_port = net_htons(port); |
1489 | } else { | 1389 | } else { |
1490 | return 0; | 1390 | return false; |
1491 | } | 1391 | } |
1492 | 1392 | ||
1493 | return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0; | 1393 | return bind(sock.socket, (struct sockaddr *)&addr, addrsize) == 0; |
diff --git a/toxcore/network.h b/toxcore/network.h index 08be6f10..6c5b6781 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -64,12 +64,12 @@ typedef struct Socket { | |||
64 | 64 | ||
65 | Socket net_socket(Family domain, int type, int protocol); | 65 | Socket net_socket(Family domain, int type, int protocol); |
66 | 66 | ||
67 | /* Check if socket is valid. | 67 | /** |
68 | * Check if socket is valid. | ||
68 | * | 69 | * |
69 | * return 1 if valid | 70 | * @return true if valid, false otherwise. |
70 | * return 0 if not valid | ||
71 | */ | 71 | */ |
72 | int sock_valid(Socket sock); | 72 | bool sock_valid(Socket sock); |
73 | 73 | ||
74 | extern const Socket net_invalid_socket; | 74 | extern const Socket net_invalid_socket; |
75 | 75 | ||
@@ -235,53 +235,49 @@ bool ipv6_ipv4_in_v6(IP6 a); | |||
235 | #define IP_NTOA_LEN 96 // TODO(irungentoo): magic number. Why not INET6_ADDRSTRLEN ? | 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); | 236 | const char *ip_ntoa(const IP *ip, char *ip_str, size_t length); |
237 | 237 | ||
238 | /* | 238 | /** |
239 | * ip_parse_addr | 239 | * Parses IP structure into an address string. |
240 | * parses IP structure into an address string | ||
241 | * | 240 | * |
242 | * input | 241 | * @param ip IP of TOX_AF_INET or TOX_AF_INET6 families. |
243 | * ip: ip of TOX_AF_INET or TOX_AF_INET6 families | 242 | * @param length length of the address buffer. |
244 | * length: length of the address buffer | 243 | * Must be at least TOX_INET_ADDRSTRLEN for TOX_AF_INET |
245 | * Must be at least TOX_INET_ADDRSTRLEN for TOX_AF_INET | 244 | * and TOX_INET6_ADDRSTRLEN for TOX_AF_INET6 |
246 | * and TOX_INET6_ADDRSTRLEN for TOX_AF_INET6 | ||
247 | * | 245 | * |
248 | * output | 246 | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
249 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
250 | * | 247 | * |
251 | * returns 1 on success, 0 on failure | 248 | * @return true on success, false on failure. |
252 | */ | 249 | */ |
253 | 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); |
254 | 251 | ||
255 | /* | 252 | /** |
256 | * addr_parse_ip | 253 | * Directly parses the input into an IP structure. |
257 | * directly parses the input into an IP structure | ||
258 | * tries IPv4 first, then IPv6 | ||
259 | * | 254 | * |
260 | * input | 255 | * Tries IPv4 first, then IPv6. |
261 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | ||
262 | * | 256 | * |
263 | * output | 257 | * @param address dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6). |
264 | * IP: family and the value is set on success | 258 | * @param to family and the value is set on success. |
265 | * | 259 | * |
266 | * returns 1 on success, 0 on failure | 260 | * @return true on success, false on failure. |
267 | */ | 261 | */ |
268 | int addr_parse_ip(const char *address, IP *to); | 262 | bool addr_parse_ip(const char *address, IP *to); |
269 | 263 | ||
270 | /* ip_equal | 264 | /** |
271 | * compares two IPAny structures | 265 | * Compares two IPAny structures. |
272 | * unset means unequal | ||
273 | * | 266 | * |
274 | * returns 0 when not equal or when uninitialized | 267 | * Unset means unequal. |
268 | * | ||
269 | * @return false when not equal or when uninitialized. | ||
275 | */ | 270 | */ |
276 | int ip_equal(const IP *a, const IP *b); | 271 | bool ip_equal(const IP *a, const IP *b); |
277 | 272 | ||
278 | /* ipport_equal | 273 | /** |
279 | * compares two IPAny_Port structures | 274 | * Compares two IPAny_Port structures. |
280 | * unset means unequal | 275 | * |
276 | * Unset means unequal. | ||
281 | * | 277 | * |
282 | * returns 0 when not equal or when uninitialized | 278 | * @return false when not equal or when uninitialized. |
283 | */ | 279 | */ |
284 | int ipport_equal(const IP_Port *a, const IP_Port *b); | 280 | bool ipport_equal(const IP_Port *a, const IP_Port *b); |
285 | 281 | ||
286 | /* nulls out ip */ | 282 | /* nulls out ip */ |
287 | void ip_reset(IP *ip); | 283 | void ip_reset(IP *ip); |
@@ -296,41 +292,40 @@ void ip_copy(IP *target, const IP *source); | |||
296 | /* copies an ip_port structure */ | 292 | /* copies an ip_port structure */ |
297 | void ipport_copy(IP_Port *target, const IP_Port *source); | 293 | void ipport_copy(IP_Port *target, const IP_Port *source); |
298 | 294 | ||
299 | /* | 295 | /** |
300 | * addr_resolve(): | 296 | * Uses getaddrinfo to resolve an address into an IP address. |
301 | * uses getaddrinfo to resolve an address into an IP address | ||
302 | * uses the first IPv4/IPv6 addresses returned by getaddrinfo | ||
303 | * | 297 | * |
304 | * input | 298 | * Uses the first IPv4/IPv6 addresses returned by getaddrinfo. |
305 | * address: a hostname (or something parseable to an IP address) | 299 | * |
306 | * to: to.family MUST be initialized, either set to a specific IP version | 300 | * @param 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 | ||
307 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both | 302 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both |
308 | * IP versions are acceptable | 303 | * IP versions are acceptable |
309 | * 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 |
310 | * | 305 | * |
311 | * returns in *to a valid IPAny (v4/v6), | 306 | * returns in *to a valid IPAny (v4/v6), |
312 | * prefers v6 if ip.family was TOX_AF_UNSPEC and both available | 307 | * prefers v6 if ip.family was TOX_AF_UNSPEC and both available |
313 | * returns in *extra an IPv4 address, if family was TOX_AF_UNSPEC and *to is TOX_AF_INET6 | 308 | * returns in *extra an IPv4 address, if family was TOX_AF_UNSPEC and *to is TOX_AF_INET6 |
314 | * returns 0 on failure | 309 | * |
310 | * @return 0 on failure, TOX_ADDR_RESOLVE_* on success. | ||
315 | */ | 311 | */ |
316 | int addr_resolve(const char *address, IP *to, IP *extra); | 312 | int addr_resolve(const char *address, IP *to, IP *extra); |
317 | 313 | ||
318 | /* | 314 | /** |
319 | * addr_resolve_or_parse_ip | 315 | * Resolves string into an IP address |
320 | * resolves string into an IP address | ||
321 | * | 316 | * |
322 | * address: a hostname (or something parseable to an IP address) | 317 | * @param address a hostname (or something parseable to an IP address) |
323 | * 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 |
324 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both | 319 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified TOX_AF_UNSPEC (= 0), if both |
325 | * IP versions are acceptable | 320 | * IP versions are acceptable |
326 | * 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 | ||
327 | * | 325 | * |
328 | * returns in *tro a matching address (IPv6 or IPv4) | 326 | * @return true on success, false on failure |
329 | * returns in *extra, if not NULL, an IPv4 address, if to->family was TOX_AF_UNSPEC | ||
330 | * returns 1 on success | ||
331 | * returns 0 on failure | ||
332 | */ | 327 | */ |
333 | 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); |
334 | 329 | ||
335 | /* 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. |
336 | * Packet data is put into data. | 331 | * Packet data is put into data. |
@@ -354,33 +349,33 @@ int networking_at_startup(void); | |||
354 | */ | 349 | */ |
355 | void kill_sock(Socket sock); | 350 | void kill_sock(Socket sock); |
356 | 351 | ||
357 | /* Set socket as nonblocking | 352 | /** |
353 | * Set socket as nonblocking | ||
358 | * | 354 | * |
359 | * return 1 on success | 355 | * @return true on success, false on failure. |
360 | * return 0 on failure | ||
361 | */ | 356 | */ |
362 | int set_socket_nonblock(Socket sock); | 357 | bool set_socket_nonblock(Socket sock); |
363 | 358 | ||
364 | /* Set socket to not emit SIGPIPE | 359 | /** |
360 | * Set socket to not emit SIGPIPE | ||
365 | * | 361 | * |
366 | * return 1 on success | 362 | * @return true on success, false on failure. |
367 | * return 0 on failure | ||
368 | */ | 363 | */ |
369 | int set_socket_nosigpipe(Socket sock); | 364 | bool set_socket_nosigpipe(Socket sock); |
370 | 365 | ||
371 | /* Enable SO_REUSEADDR on socket. | 366 | /** |
367 | * Enable SO_REUSEADDR on socket. | ||
372 | * | 368 | * |
373 | * return 1 on success | 369 | * @return true on success, false on failure. |
374 | * return 0 on failure | ||
375 | */ | 370 | */ |
376 | int set_socket_reuseaddr(Socket sock); | 371 | bool set_socket_reuseaddr(Socket sock); |
377 | 372 | ||
378 | /* Set socket to dual (IPv4 + IPv6 socket) | 373 | /** |
374 | * Set socket to dual (IPv4 + IPv6 socket) | ||
379 | * | 375 | * |
380 | * return 1 on success | 376 | * @return true on success, false on failure. |
381 | * return 0 on failure | ||
382 | */ | 377 | */ |
383 | int set_socket_dualstack(Socket sock); | 378 | bool set_socket_dualstack(Socket sock); |
384 | 379 | ||
385 | /* Basic network functions: */ | 380 | /* Basic network functions: */ |
386 | 381 | ||
@@ -413,10 +408,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type); | |||
413 | */ | 408 | */ |
414 | void net_freeipport(IP_Port *ip_ports); | 409 | void net_freeipport(IP_Port *ip_ports); |
415 | 410 | ||
416 | /* return 1 on success | 411 | /** |
417 | * return 0 on failure | 412 | * @return true on success, false on failure. |
418 | */ | 413 | */ |
419 | int bind_to_port(Socket sock, Family family, uint16_t port); | 414 | bool bind_to_port(Socket sock, Family family, uint16_t port); |
420 | 415 | ||
421 | /* Get the last networking error code. | 416 | /* Get the last networking error code. |
422 | * | 417 | * |