diff options
author | iphydf <iphydf@users.noreply.github.com> | 2018-02-27 01:07:46 +0000 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2018-05-20 19:35:28 +0000 |
commit | 21675ce0d2581597b0e0a727ab4cf6cfb796a037 (patch) | |
tree | e1ac4ca63c5cc2b2dc78fcd5b20a8202eab98d9d /toxcore/network.c | |
parent | 4f6ab0708c85f3e3da7726f6caecc381c6d21370 (diff) |
Finish @Diadlo's network Family abstraction.
The Family stuff in toxcore is a big mess. I'm sure I saw a bunch of bugs
on the way, but I'm not verifying that code now, so the bugs stay.
Diffstat (limited to 'toxcore/network.c')
-rw-r--r-- | toxcore/network.c | 204 |
1 files changed, 143 insertions, 61 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index f357d887..e7e754c5 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -196,7 +196,7 @@ static int make_socktype(int type); | |||
196 | 196 | ||
197 | static int make_family(Family tox_family) | 197 | static int make_family(Family tox_family) |
198 | { | 198 | { |
199 | switch (tox_family) { | 199 | switch (tox_family.value) { |
200 | case TOX_AF_INET: | 200 | case TOX_AF_INET: |
201 | return AF_INET; | 201 | return AF_INET; |
202 | 202 | ||
@@ -207,24 +207,24 @@ static int make_family(Family tox_family) | |||
207 | return AF_UNSPEC; | 207 | return AF_UNSPEC; |
208 | 208 | ||
209 | default: | 209 | default: |
210 | return tox_family; | 210 | return tox_family.value; |
211 | } | 211 | } |
212 | } | 212 | } |
213 | 213 | ||
214 | static Family make_tox_family(int family) | 214 | static const Family *make_tox_family(int family) |
215 | { | 215 | { |
216 | switch (family) { | 216 | switch (family) { |
217 | case AF_INET: | 217 | case AF_INET: |
218 | return TOX_AF_INET; | 218 | return &net_family_ipv4; |
219 | 219 | ||
220 | case AF_INET6: | 220 | case AF_INET6: |
221 | return TOX_AF_INET6; | 221 | return &net_family_ipv6; |
222 | 222 | ||
223 | case AF_UNSPEC: | 223 | case AF_UNSPEC: |
224 | return TOX_AF_UNSPEC; | 224 | return &net_family_unspec; |
225 | 225 | ||
226 | default: | 226 | default: |
227 | return family; | 227 | return nullptr; |
228 | } | 228 | } |
229 | } | 229 | } |
230 | 230 | ||
@@ -281,6 +281,61 @@ const Socket net_invalid_socket = { | |||
281 | #endif | 281 | #endif |
282 | }; | 282 | }; |
283 | 283 | ||
284 | const Family net_family_unspec = {TOX_AF_UNSPEC}; | ||
285 | const Family net_family_ipv4 = {TOX_AF_INET}; | ||
286 | const Family net_family_ipv6 = {TOX_AF_INET6}; | ||
287 | const Family net_family_tcp_family = {TCP_FAMILY}; | ||
288 | const Family net_family_tcp_onion = {TCP_ONION_FAMILY}; | ||
289 | const Family net_family_tcp_ipv4 = {TCP_INET}; | ||
290 | const Family net_family_tcp_ipv6 = {TCP_INET6}; | ||
291 | const Family net_family_tox_tcp_ipv4 = {TOX_TCP_INET}; | ||
292 | const Family net_family_tox_tcp_ipv6 = {TOX_TCP_INET6}; | ||
293 | |||
294 | bool net_family_is_unspec(Family family) | ||
295 | { | ||
296 | return family.value == net_family_unspec.value; | ||
297 | } | ||
298 | |||
299 | bool net_family_is_ipv4(Family family) | ||
300 | { | ||
301 | return family.value == net_family_ipv4.value; | ||
302 | } | ||
303 | |||
304 | bool net_family_is_ipv6(Family family) | ||
305 | { | ||
306 | return family.value == net_family_ipv6.value; | ||
307 | } | ||
308 | |||
309 | bool net_family_is_tcp_family(Family family) | ||
310 | { | ||
311 | return family.value == net_family_tcp_family.value; | ||
312 | } | ||
313 | |||
314 | bool net_family_is_tcp_onion(Family family) | ||
315 | { | ||
316 | return family.value == net_family_tcp_onion.value; | ||
317 | } | ||
318 | |||
319 | bool net_family_is_tcp_ipv4(Family family) | ||
320 | { | ||
321 | return family.value == net_family_tcp_ipv4.value; | ||
322 | } | ||
323 | |||
324 | bool net_family_is_tcp_ipv6(Family family) | ||
325 | { | ||
326 | return family.value == net_family_tcp_ipv6.value; | ||
327 | } | ||
328 | |||
329 | bool net_family_is_tox_tcp_ipv4(Family family) | ||
330 | { | ||
331 | return family.value == net_family_tox_tcp_ipv4.value; | ||
332 | } | ||
333 | |||
334 | bool net_family_is_tox_tcp_ipv6(Family family) | ||
335 | { | ||
336 | return family.value == net_family_tox_tcp_ipv6.value; | ||
337 | } | ||
338 | |||
284 | /* Check if socket is valid. | 339 | /* Check if socket is valid. |
285 | * | 340 | * |
286 | * return 1 if valid | 341 | * return 1 if valid |
@@ -496,20 +551,19 @@ uint16_t net_port(const Networking_Core *net) | |||
496 | */ | 551 | */ |
497 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) | 552 | int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint16_t length) |
498 | { | 553 | { |
499 | if (net->family == TOX_AF_UNSPEC) { /* Socket not initialized */ | 554 | if (net_family_is_unspec(net->family)) { /* Socket not initialized */ |
555 | LOGGER_ERROR(net->log, "attempted to send message of length %u on uninitialised socket", (unsigned)length); | ||
500 | return -1; | 556 | return -1; |
501 | } | 557 | } |
502 | 558 | ||
503 | /* socket TOX_AF_INET, but target IP NOT: can't send */ | 559 | /* socket TOX_AF_INET, but target IP NOT: can't send */ |
504 | if ((net->family == TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET)) { | 560 | if (net_family_is_ipv4(net->family) && !net_family_is_ipv4(ip_port.ip.family)) { |
561 | LOGGER_ERROR(net->log, "attempted to send message with network family %d (probably IPv6) on IPv4 socket", | ||
562 | ip_port.ip.family.value); | ||
505 | return -1; | 563 | return -1; |
506 | } | 564 | } |
507 | 565 | ||
508 | struct sockaddr_storage addr; | 566 | if (net_family_is_ipv4(ip_port.ip.family) && net_family_is_ipv6(net->family)) { |
509 | |||
510 | size_t addrsize = 0; | ||
511 | |||
512 | if (ip_port.ip.family == TOX_AF_INET && net->family == TOX_AF_INET6) { | ||
513 | /* must convert to IPV4-in-IPV6 address */ | 567 | /* must convert to IPV4-in-IPV6 address */ |
514 | IP6 ip6; | 568 | IP6 ip6; |
515 | 569 | ||
@@ -520,29 +574,33 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
520 | ip6.uint32[2] = net_htonl(0xFFFF); | 574 | ip6.uint32[2] = net_htonl(0xFFFF); |
521 | ip6.uint32[3] = ip_port.ip.ip.v4.uint32; | 575 | ip6.uint32[3] = ip_port.ip.ip.v4.uint32; |
522 | 576 | ||
523 | ip_port.ip.family = TOX_AF_INET6; | 577 | ip_port.ip.family = net_family_ipv6; |
524 | ip_port.ip.ip.v6 = ip6; | 578 | ip_port.ip.ip.v6 = ip6; |
525 | } | 579 | } |
526 | 580 | ||
527 | if (ip_port.ip.family == TOX_AF_INET) { | 581 | struct sockaddr_storage addr; |
528 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 582 | |
583 | size_t addrsize; | ||
584 | |||
585 | if (net_family_is_ipv4(ip_port.ip.family)) { | ||
586 | struct sockaddr_in *const addr4 = (struct sockaddr_in *)&addr; | ||
529 | 587 | ||
530 | addrsize = sizeof(struct sockaddr_in); | 588 | addrsize = sizeof(struct sockaddr_in); |
531 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); | ||
532 | addr4->sin_family = AF_INET; | 589 | addr4->sin_family = AF_INET; |
533 | addr4->sin_port = ip_port.port; | 590 | addr4->sin_port = ip_port.port; |
534 | } else if (ip_port.ip.family == TOX_AF_INET6) { | 591 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); |
535 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 592 | } else if (net_family_is_ipv6(ip_port.ip.family)) { |
593 | struct sockaddr_in6 *const addr6 = (struct sockaddr_in6 *)&addr; | ||
536 | 594 | ||
537 | addrsize = sizeof(struct sockaddr_in6); | 595 | addrsize = sizeof(struct sockaddr_in6); |
538 | fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); | ||
539 | addr6->sin6_family = AF_INET6; | 596 | addr6->sin6_family = AF_INET6; |
540 | addr6->sin6_port = ip_port.port; | 597 | addr6->sin6_port = ip_port.port; |
598 | fill_addr6(ip_port.ip.ip.v6, &addr6->sin6_addr); | ||
541 | 599 | ||
542 | addr6->sin6_flowinfo = 0; | 600 | addr6->sin6_flowinfo = 0; |
543 | addr6->sin6_scope_id = 0; | 601 | addr6->sin6_scope_id = 0; |
544 | } else { | 602 | } else { |
545 | LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family); | 603 | LOGGER_WARNING(net->log, "unknown address type: %d", ip_port.ip.family.value); |
546 | return -1; | 604 | return -1; |
547 | } | 605 | } |
548 | 606 | ||
@@ -587,17 +645,31 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da | |||
587 | if (addr.ss_family == AF_INET) { | 645 | if (addr.ss_family == AF_INET) { |
588 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; | 646 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; |
589 | 647 | ||
590 | ip_port->ip.family = make_tox_family(addr_in->sin_family); | 648 | const Family *const family = make_tox_family(addr_in->sin_family); |
649 | assert(family != nullptr); | ||
650 | |||
651 | if (family == nullptr) { | ||
652 | return -1; | ||
653 | } | ||
654 | |||
655 | ip_port->ip.family = *family; | ||
591 | get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); | 656 | get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr); |
592 | ip_port->port = addr_in->sin_port; | 657 | ip_port->port = addr_in->sin_port; |
593 | } else if (addr.ss_family == AF_INET6) { | 658 | } else if (addr.ss_family == AF_INET6) { |
594 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; | 659 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; |
595 | ip_port->ip.family = make_tox_family(addr_in6->sin6_family); | 660 | const Family *const family = make_tox_family(addr_in6->sin6_family); |
661 | assert(family != nullptr); | ||
662 | |||
663 | if (family == nullptr) { | ||
664 | return -1; | ||
665 | } | ||
666 | |||
667 | ip_port->ip.family = *family; | ||
596 | get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); | 668 | get_ip6(&ip_port->ip.ip.v6, &addr_in6->sin6_addr); |
597 | ip_port->port = addr_in6->sin6_port; | 669 | ip_port->port = addr_in6->sin6_port; |
598 | 670 | ||
599 | if (IPV6_IPV4_IN_V6(ip_port->ip.ip.v6)) { | 671 | if (IPV6_IPV4_IN_V6(ip_port->ip.ip.v6)) { |
600 | ip_port->ip.family = TOX_AF_INET; | 672 | ip_port->ip.family = net_family_ipv4; |
601 | ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; | 673 | ip_port->ip.ip.v4.uint32 = ip_port->ip.ip.v6.uint32[3]; |
602 | } | 674 | } |
603 | } else { | 675 | } else { |
@@ -617,7 +689,8 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl | |||
617 | 689 | ||
618 | void networking_poll(Networking_Core *net, void *userdata) | 690 | void networking_poll(Networking_Core *net, void *userdata) |
619 | { | 691 | { |
620 | if (net->family == 0) { /* Socket not initialized */ | 692 | if (net_family_is_unspec(net->family)) { |
693 | /* Socket not initialized */ | ||
621 | return; | 694 | return; |
622 | } | 695 | } |
623 | 696 | ||
@@ -732,8 +805,8 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
732 | } | 805 | } |
733 | 806 | ||
734 | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ | 807 | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ |
735 | if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) { | 808 | if (!net_family_is_ipv4(ip.family) && !net_family_is_ipv6(ip.family)) { |
736 | LOGGER_ERROR(log, "Invalid address family: %u", ip.family); | 809 | LOGGER_ERROR(log, "invalid address family: %u\n", ip.family.value); |
737 | return nullptr; | 810 | return nullptr; |
738 | } | 811 | } |
739 | 812 | ||
@@ -809,7 +882,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
809 | 882 | ||
810 | memset(&addr, 0, sizeof(struct sockaddr_storage)); | 883 | memset(&addr, 0, sizeof(struct sockaddr_storage)); |
811 | 884 | ||
812 | if (temp->family == TOX_AF_INET) { | 885 | if (net_family_is_ipv4(temp->family)) { |
813 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 886 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
814 | 887 | ||
815 | addrsize = sizeof(struct sockaddr_in); | 888 | addrsize = sizeof(struct sockaddr_in); |
@@ -818,7 +891,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
818 | fill_addr4(ip.ip.v4, &addr4->sin_addr); | 891 | fill_addr4(ip.ip.v4, &addr4->sin_addr); |
819 | 892 | ||
820 | portptr = &addr4->sin_port; | 893 | portptr = &addr4->sin_port; |
821 | } else if (temp->family == TOX_AF_INET6) { | 894 | } else if (net_family_is_ipv6(temp->family)) { |
822 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 895 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
823 | 896 | ||
824 | addrsize = sizeof(struct sockaddr_in6); | 897 | addrsize = sizeof(struct sockaddr_in6); |
@@ -835,8 +908,8 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
835 | return nullptr; | 908 | return nullptr; |
836 | } | 909 | } |
837 | 910 | ||
838 | if (ip.family == TOX_AF_INET6) { | 911 | if (net_family_is_ipv6(ip.family)) { |
839 | int is_dualstack = set_socket_dualstack(temp->sock); | 912 | const int is_dualstack = set_socket_dualstack(temp->sock); |
840 | LOGGER_DEBUG(log, "Dual-stack socket: %s", | 913 | LOGGER_DEBUG(log, "Dual-stack socket: %s", |
841 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); | 914 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); |
842 | /* multicast local nodes */ | 915 | /* multicast local nodes */ |
@@ -944,7 +1017,8 @@ void kill_networking(Networking_Core *net) | |||
944 | return; | 1017 | return; |
945 | } | 1018 | } |
946 | 1019 | ||
947 | if (net->family != 0) { /* Socket not initialized */ | 1020 | if (!net_family_is_unspec(net->family)) { |
1021 | /* Socket is initialized, so we close it. */ | ||
948 | kill_sock(net->sock); | 1022 | kill_sock(net->sock); |
949 | } | 1023 | } |
950 | 1024 | ||
@@ -965,8 +1039,8 @@ int ip_equal(const IP *a, const IP *b) | |||
965 | } | 1039 | } |
966 | 1040 | ||
967 | /* same family */ | 1041 | /* same family */ |
968 | if (a->family == b->family) { | 1042 | if (a->family.value == b->family.value) { |
969 | if (a->family == TOX_AF_INET || a->family == TCP_INET) { | 1043 | if (net_family_is_ipv4(a->family) || net_family_is_tcp_ipv4(a->family)) { |
970 | struct in_addr addr_a; | 1044 | struct in_addr addr_a; |
971 | struct in_addr addr_b; | 1045 | struct in_addr addr_b; |
972 | fill_addr4(a->ip.v4, &addr_a); | 1046 | fill_addr4(a->ip.v4, &addr_a); |
@@ -974,7 +1048,7 @@ int ip_equal(const IP *a, const IP *b) | |||
974 | return addr_a.s_addr == addr_b.s_addr; | 1048 | return addr_a.s_addr == addr_b.s_addr; |
975 | } | 1049 | } |
976 | 1050 | ||
977 | if (a->family == TOX_AF_INET6 || a->family == TCP_INET6) { | 1051 | if (net_family_is_ipv6(a->family) || net_family_is_tcp_ipv6(a->family)) { |
978 | return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && | 1052 | return a->ip.v6.uint64[0] == b->ip.v6.uint64[0] && |
979 | a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; | 1053 | a->ip.v6.uint64[1] == b->ip.v6.uint64[1]; |
980 | } | 1054 | } |
@@ -983,13 +1057,13 @@ int ip_equal(const IP *a, const IP *b) | |||
983 | } | 1057 | } |
984 | 1058 | ||
985 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ | 1059 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ |
986 | if ((a->family == TOX_AF_INET) && (b->family == TOX_AF_INET6)) { | 1060 | if (net_family_is_ipv4(a->family) && net_family_is_ipv6(b->family)) { |
987 | if (IPV6_IPV4_IN_V6(b->ip.v6)) { | 1061 | if (IPV6_IPV4_IN_V6(b->ip.v6)) { |
988 | struct in_addr addr_a; | 1062 | struct in_addr addr_a; |
989 | fill_addr4(a->ip.v4, &addr_a); | 1063 | fill_addr4(a->ip.v4, &addr_a); |
990 | return addr_a.s_addr == b->ip.v6.uint32[3]; | 1064 | return addr_a.s_addr == b->ip.v6.uint32[3]; |
991 | } | 1065 | } |
992 | } else if ((a->family == TOX_AF_INET6) && (b->family == TOX_AF_INET)) { | 1066 | } else if (net_family_is_ipv6(a->family) && net_family_is_ipv4(b->family)) { |
993 | if (IPV6_IPV4_IN_V6(a->ip.v6)) { | 1067 | if (IPV6_IPV4_IN_V6(a->ip.v6)) { |
994 | struct in_addr addr_b; | 1068 | struct in_addr addr_b; |
995 | fill_addr4(b->ip.v4, &addr_b); | 1069 | fill_addr4(b->ip.v4, &addr_b); |
@@ -1037,28 +1111,28 @@ void ip_init(IP *ip, bool ipv6enabled) | |||
1037 | } | 1111 | } |
1038 | 1112 | ||
1039 | memset(ip, 0, sizeof(IP)); | 1113 | memset(ip, 0, sizeof(IP)); |
1040 | ip->family = ipv6enabled ? TOX_AF_INET6 : TOX_AF_INET; | 1114 | ip->family = ipv6enabled ? net_family_ipv6 : net_family_ipv4; |
1041 | } | 1115 | } |
1042 | 1116 | ||
1043 | /* checks if ip is valid */ | 1117 | /* checks if ip is valid */ |
1044 | int ip_isset(const IP *ip) | 1118 | bool ip_isset(const IP *ip) |
1045 | { | 1119 | { |
1046 | if (!ip) { | 1120 | if (!ip) { |
1047 | return 0; | 1121 | return false; |
1048 | } | 1122 | } |
1049 | 1123 | ||
1050 | return (ip->family != 0); | 1124 | return !net_family_is_unspec(ip->family); |
1051 | } | 1125 | } |
1052 | 1126 | ||
1053 | /* checks if ip is valid */ | 1127 | /* checks if ip is valid */ |
1054 | int ipport_isset(const IP_Port *ipport) | 1128 | bool ipport_isset(const IP_Port *ipport) |
1055 | { | 1129 | { |
1056 | if (!ipport) { | 1130 | if (!ipport) { |
1057 | return 0; | 1131 | return true; |
1058 | } | 1132 | } |
1059 | 1133 | ||
1060 | if (!ipport->port) { | 1134 | if (!ipport->port) { |
1061 | return 0; | 1135 | return true; |
1062 | } | 1136 | } |
1063 | 1137 | ||
1064 | return ip_isset(&ipport->ip); | 1138 | return ip_isset(&ipport->ip); |
@@ -1103,14 +1177,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
1103 | if (ip) { | 1177 | if (ip) { |
1104 | const int family = make_family(ip->family); | 1178 | const int family = make_family(ip->family); |
1105 | 1179 | ||
1106 | if (ip->family == TOX_AF_INET) { | 1180 | if (net_family_is_ipv4(ip->family)) { |
1107 | /* returns standard quad-dotted notation */ | 1181 | /* returns standard quad-dotted notation */ |
1108 | struct in_addr addr; | 1182 | struct in_addr addr; |
1109 | fill_addr4(ip->ip.v4, &addr); | 1183 | fill_addr4(ip->ip.v4, &addr); |
1110 | 1184 | ||
1111 | ip_str[0] = 0; | 1185 | ip_str[0] = 0; |
1112 | inet_ntop(family, &addr, ip_str, length); | 1186 | inet_ntop(family, &addr, ip_str, length); |
1113 | } else if (ip->family == TOX_AF_INET6) { | 1187 | } else if (net_family_is_ipv6(ip->family)) { |
1114 | /* returns hex-groups enclosed into square brackets */ | 1188 | /* returns hex-groups enclosed into square brackets */ |
1115 | struct in6_addr addr; | 1189 | struct in6_addr addr; |
1116 | fill_addr6(ip->ip.v6, &addr); | 1190 | fill_addr6(ip->ip.v6, &addr); |
@@ -1121,7 +1195,7 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
1121 | ip_str[len] = ']'; | 1195 | ip_str[len] = ']'; |
1122 | ip_str[len + 1] = 0; | 1196 | ip_str[len + 1] = 0; |
1123 | } else { | 1197 | } else { |
1124 | snprintf(ip_str, length, "(IP invalid, family %u)", ip->family); | 1198 | snprintf(ip_str, length, "(IP invalid, family %u)", ip->family.value); |
1125 | } | 1199 | } |
1126 | } else { | 1200 | } else { |
1127 | snprintf(ip_str, length, "(IP invalid: NULL)"); | 1201 | snprintf(ip_str, length, "(IP invalid: NULL)"); |
@@ -1153,14 +1227,14 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) | |||
1153 | return 0; | 1227 | return 0; |
1154 | } | 1228 | } |
1155 | 1229 | ||
1156 | if (ip->family == TOX_AF_INET) { | 1230 | if (net_family_is_ipv4(ip->family)) { |
1157 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; | 1231 | const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; |
1158 | return inet_ntop(ip->family, addr, address, length) != nullptr; | 1232 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; |
1159 | } | 1233 | } |
1160 | 1234 | ||
1161 | if (ip->family == TOX_AF_INET6) { | 1235 | if (net_family_is_ipv6(ip->family)) { |
1162 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; | 1236 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; |
1163 | return inet_ntop(ip->family, addr, address, length) != nullptr; | 1237 | return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; |
1164 | } | 1238 | } |
1165 | 1239 | ||
1166 | return 0; | 1240 | return 0; |
@@ -1188,7 +1262,7 @@ int addr_parse_ip(const char *address, IP *to) | |||
1188 | struct in_addr addr4; | 1262 | struct in_addr addr4; |
1189 | 1263 | ||
1190 | if (inet_pton(AF_INET, address, &addr4) == 1) { | 1264 | if (inet_pton(AF_INET, address, &addr4) == 1) { |
1191 | to->family = TOX_AF_INET; | 1265 | to->family = net_family_ipv4; |
1192 | get_ip4(&to->ip.v4, &addr4); | 1266 | get_ip4(&to->ip.v4, &addr4); |
1193 | return 1; | 1267 | return 1; |
1194 | } | 1268 | } |
@@ -1196,7 +1270,7 @@ int addr_parse_ip(const char *address, IP *to) | |||
1196 | struct in6_addr addr6; | 1270 | struct in6_addr addr6; |
1197 | 1271 | ||
1198 | if (inet_pton(AF_INET6, address, &addr6) == 1) { | 1272 | if (inet_pton(AF_INET6, address, &addr6) == 1) { |
1199 | to->family = TOX_AF_INET6; | 1273 | to->family = net_family_ipv6; |
1200 | get_ip6(&to->ip.v6, &addr6); | 1274 | get_ip6(&to->ip.v6, &addr6); |
1201 | return 1; | 1275 | return 1; |
1202 | } | 1276 | } |
@@ -1228,7 +1302,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) | |||
1228 | } | 1302 | } |
1229 | 1303 | ||
1230 | Family tox_family = to->family; | 1304 | Family tox_family = to->family; |
1231 | Family family = make_family(tox_family); | 1305 | int family = make_family(tox_family); |
1232 | 1306 | ||
1233 | struct addrinfo *server = nullptr; | 1307 | struct addrinfo *server = nullptr; |
1234 | struct addrinfo *walker = nullptr; | 1308 | struct addrinfo *walker = nullptr; |
@@ -1342,14 +1416,14 @@ int net_connect(Socket sock, IP_Port ip_port) | |||
1342 | struct sockaddr_storage addr = {0}; | 1416 | struct sockaddr_storage addr = {0}; |
1343 | size_t addrsize; | 1417 | size_t addrsize; |
1344 | 1418 | ||
1345 | if (ip_port.ip.family == TOX_AF_INET) { | 1419 | if (net_family_is_ipv4(ip_port.ip.family)) { |
1346 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 1420 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
1347 | 1421 | ||
1348 | addrsize = sizeof(struct sockaddr_in); | 1422 | addrsize = sizeof(struct sockaddr_in); |
1349 | addr4->sin_family = AF_INET; | 1423 | addr4->sin_family = AF_INET; |
1350 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); | 1424 | fill_addr4(ip_port.ip.ip.v4, &addr4->sin_addr); |
1351 | addr4->sin_port = ip_port.port; | 1425 | addr4->sin_port = ip_port.port; |
1352 | } else if (ip_port.ip.family == TOX_AF_INET6) { | 1426 | } else if (net_family_is_ipv6(ip_port.ip.family)) { |
1353 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 1427 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
1354 | 1428 | ||
1355 | addrsize = sizeof(struct sockaddr_in6); | 1429 | addrsize = sizeof(struct sockaddr_in6); |
@@ -1422,7 +1496,15 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type) | |||
1422 | continue; | 1496 | continue; |
1423 | } | 1497 | } |
1424 | 1498 | ||
1425 | ip_port->ip.family = make_tox_family(cur->ai_family); | 1499 | const Family *const family = make_tox_family(cur->ai_family); |
1500 | assert(family != nullptr); | ||
1501 | |||
1502 | if (family == nullptr) { | ||
1503 | freeaddrinfo(infos); | ||
1504 | return -1; | ||
1505 | } | ||
1506 | |||
1507 | ip_port->ip.family = *family; | ||
1426 | 1508 | ||
1427 | ip_port++; | 1509 | ip_port++; |
1428 | } | 1510 | } |
@@ -1440,18 +1522,18 @@ void net_freeipport(IP_Port *ip_ports) | |||
1440 | /* return 1 on success | 1522 | /* return 1 on success |
1441 | * return 0 on failure | 1523 | * return 0 on failure |
1442 | */ | 1524 | */ |
1443 | int bind_to_port(Socket sock, int family, uint16_t port) | 1525 | int bind_to_port(Socket sock, Family family, uint16_t port) |
1444 | { | 1526 | { |
1445 | struct sockaddr_storage addr = {0}; | 1527 | struct sockaddr_storage addr = {0}; |
1446 | size_t addrsize; | 1528 | size_t addrsize; |
1447 | 1529 | ||
1448 | if (family == TOX_AF_INET) { | 1530 | if (net_family_is_ipv4(family)) { |
1449 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 1531 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
1450 | 1532 | ||
1451 | addrsize = sizeof(struct sockaddr_in); | 1533 | addrsize = sizeof(struct sockaddr_in); |
1452 | addr4->sin_family = AF_INET; | 1534 | addr4->sin_family = AF_INET; |
1453 | addr4->sin_port = net_htons(port); | 1535 | addr4->sin_port = net_htons(port); |
1454 | } else if (family == TOX_AF_INET6) { | 1536 | } else if (net_family_is_ipv6(family)) { |
1455 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 1537 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
1456 | 1538 | ||
1457 | addrsize = sizeof(struct sockaddr_in6); | 1539 | addrsize = sizeof(struct sockaddr_in6); |