summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c158
1 files changed, 29 insertions, 129 deletions
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. 349bool sock_valid(Socket sock)
350 *
351 * return 1 if valid
352 * return 0 if not valid
353 */
354int 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 365bool set_socket_nonblock(Socket sock)
371 *
372 * return 1 on success
373 * return 0 on failure
374 */
375int 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 375bool set_socket_nosigpipe(Socket sock)
386 *
387 * return 1 on success
388 * return 0 on failure
389 */
390int 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. 385bool set_socket_reuseaddr(Socket sock)
401 *
402 * return 1 on success
403 * return 0 on failure
404 */
405int 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) 391bool set_socket_dualstack(Socket sock)
412 *
413 * return 1 on success
414 * return 0 on failure
415 */
416int 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 951bool 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 */
982int 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 993bool 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 */
1030int 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/* 1119bool 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 */
1171int 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/* 1138bool 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 */
1203int 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 */
1245int addr_resolve(const char *address, IP *to, IP *extra) 1163int 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/* 1253bool 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 */
1350int 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
1361int net_connect(Socket sock, IP_Port ip_port) 1264int 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 1372bool bind_to_port(Socket sock, Family family, uint16_t port)
1470 * return 0 on failure
1471 */
1472int 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;