diff options
author | Diadlo <polsha3@gmail.com> | 2017-03-02 23:15:51 +0300 |
---|---|---|
committer | Diadlo <polsha3@gmail.com> | 2017-08-24 20:09:08 +0300 |
commit | 66b8a7685e8fdecd6104f01f840f5d792ce1e041 (patch) | |
tree | 7c65281aefeb21c55ba422ff7d9587675eeb7070 /toxcore/network.c | |
parent | 8f19c926c0d5231ede61a831050664103e54000e (diff) |
AF_INET -> TOX_AF_INET
Diffstat (limited to 'toxcore/network.c')
-rw-r--r-- | toxcore/network.c | 121 |
1 files changed, 74 insertions, 47 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index edf6f11a..45805244 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -70,7 +70,7 @@ | |||
70 | 70 | ||
71 | static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) | 71 | static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) |
72 | { | 72 | { |
73 | if (family == AF_INET) { | 73 | if (family == TOX_AF_INET) { |
74 | struct sockaddr_in saddr; | 74 | struct sockaddr_in saddr; |
75 | memset(&saddr, 0, sizeof(saddr)); | 75 | memset(&saddr, 0, sizeof(saddr)); |
76 | 76 | ||
@@ -84,7 +84,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t | |||
84 | } | 84 | } |
85 | 85 | ||
86 | return buf; | 86 | return buf; |
87 | } else if (family == AF_INET6) { | 87 | } else if (family == TOX_AF_INET6) { |
88 | struct sockaddr_in6 saddr; | 88 | struct sockaddr_in6 saddr; |
89 | memset(&saddr, 0, sizeof(saddr)); | 89 | memset(&saddr, 0, sizeof(saddr)); |
90 | 90 | ||
@@ -105,7 +105,7 @@ static const char *inet_ntop(Family family, const void *addr, char *buf, size_t | |||
105 | 105 | ||
106 | static int inet_pton(Family family, const char *addrString, void *addrbuf) | 106 | static int inet_pton(Family family, const char *addrString, void *addrbuf) |
107 | { | 107 | { |
108 | if (family == AF_INET) { | 108 | if (family == TOX_AF_INET) { |
109 | struct sockaddr_in saddr; | 109 | struct sockaddr_in saddr; |
110 | memset(&saddr, 0, sizeof(saddr)); | 110 | memset(&saddr, 0, sizeof(saddr)); |
111 | 111 | ||
@@ -118,7 +118,7 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) | |||
118 | *(struct in_addr *)addrbuf = saddr.sin_addr; | 118 | *(struct in_addr *)addrbuf = saddr.sin_addr; |
119 | 119 | ||
120 | return 1; | 120 | return 1; |
121 | } else if (family == AF_INET6) { | 121 | } else if (family == TOX_AF_INET6) { |
122 | struct sockaddr_in6 saddr; | 122 | struct sockaddr_in6 saddr; |
123 | memset(&saddr, 0, sizeof(saddr)); | 123 | memset(&saddr, 0, sizeof(saddr)); |
124 | 124 | ||
@@ -140,7 +140,8 @@ static int inet_pton(Family family, const char *addrString, void *addrbuf) | |||
140 | 140 | ||
141 | static int make_proto(int proto); | 141 | static int make_proto(int proto); |
142 | static int make_socktype(int type); | 142 | static int make_socktype(int type); |
143 | static int make_family(int family); | 143 | static int make_family(int tox_family); |
144 | static int make_tox_family(int family); | ||
144 | 145 | ||
145 | /* Check if socket is valid. | 146 | /* Check if socket is valid. |
146 | * | 147 | * |
@@ -366,8 +367,8 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
366 | return -1; | 367 | return -1; |
367 | } | 368 | } |
368 | 369 | ||
369 | /* socket AF_INET, but target IP NOT: can't send */ | 370 | /* socket TOX_AF_INET, but target IP NOT: can't send */ |
370 | if ((net->family == AF_INET) && (ip_port.ip.family != AF_INET)) { | 371 | if ((net->family == TOX_AF_INET) && (ip_port.ip.family != TOX_AF_INET)) { |
371 | return -1; | 372 | return -1; |
372 | } | 373 | } |
373 | 374 | ||
@@ -375,8 +376,8 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
375 | 376 | ||
376 | size_t addrsize = 0; | 377 | size_t addrsize = 0; |
377 | 378 | ||
378 | if (ip_port.ip.family == AF_INET) { | 379 | if (ip_port.ip.family == TOX_AF_INET) { |
379 | if (net->family == AF_INET6) { | 380 | if (net->family == TOX_AF_INET6) { |
380 | /* must convert to IPV4-in-IPV6 address */ | 381 | /* must convert to IPV4-in-IPV6 address */ |
381 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 382 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
382 | 383 | ||
@@ -404,7 +405,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 | |||
404 | fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); | 405 | fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); |
405 | addr4->sin_port = ip_port.port; | 406 | addr4->sin_port = ip_port.port; |
406 | } | 407 | } |
407 | } else if (ip_port.ip.family == AF_INET6) { | 408 | } else if (ip_port.ip.family == TOX_AF_INET6) { |
408 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 409 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
409 | 410 | ||
410 | addrsize = sizeof(struct sockaddr_in6); | 411 | addrsize = sizeof(struct sockaddr_in6); |
@@ -457,17 +458,17 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da | |||
457 | if (addr.ss_family == AF_INET) { | 458 | if (addr.ss_family == AF_INET) { |
458 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; | 459 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; |
459 | 460 | ||
460 | ip_port->ip.family = addr_in->sin_family; | 461 | ip_port->ip.family = make_tox_family(addr_in->sin_family); |
461 | get_ip4(&ip_port->ip.ip4, &addr_in->sin_addr); | 462 | get_ip4(&ip_port->ip.ip4, &addr_in->sin_addr); |
462 | ip_port->port = addr_in->sin_port; | 463 | ip_port->port = addr_in->sin_port; |
463 | } else if (addr.ss_family == AF_INET6) { | 464 | } else if (addr.ss_family == AF_INET6) { |
464 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; | 465 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr; |
465 | ip_port->ip.family = addr_in6->sin6_family; | 466 | ip_port->ip.family = make_tox_family(addr_in6->sin6_family); |
466 | get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr); | 467 | get_ip6(&ip_port->ip.ip6, &addr_in6->sin6_addr); |
467 | ip_port->port = addr_in6->sin6_port; | 468 | ip_port->port = addr_in6->sin6_port; |
468 | 469 | ||
469 | if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { | 470 | if (IPV6_IPV4_IN_V6(ip_port->ip.ip6)) { |
470 | ip_port->ip.family = AF_INET; | 471 | ip_port->ip.family = TOX_AF_INET; |
471 | ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; | 472 | ip_port->ip.ip4.uint32 = ip_port->ip.ip6.uint32[3]; |
472 | } | 473 | } |
473 | } else { | 474 | } else { |
@@ -602,7 +603,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
602 | } | 603 | } |
603 | 604 | ||
604 | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ | 605 | /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ |
605 | if (ip.family != AF_INET && ip.family != AF_INET6) { | 606 | if (ip.family != TOX_AF_INET && ip.family != TOX_AF_INET6) { |
606 | LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family); | 607 | LOGGER_ERROR(log, "Invalid address family: %u\n", ip.family); |
607 | return NULL; | 608 | return NULL; |
608 | } | 609 | } |
@@ -674,7 +675,9 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
674 | struct sockaddr_storage addr; | 675 | struct sockaddr_storage addr; |
675 | size_t addrsize; | 676 | size_t addrsize; |
676 | 677 | ||
677 | if (temp->family == AF_INET) { | 678 | memset(&addr, 0, sizeof(struct sockaddr_storage)); |
679 | |||
680 | if (temp->family == TOX_AF_INET) { | ||
678 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 681 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
679 | 682 | ||
680 | addrsize = sizeof(struct sockaddr_in); | 683 | addrsize = sizeof(struct sockaddr_in); |
@@ -683,7 +686,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
683 | fill_addr4(ip.ip4, &addr4->sin_addr); | 686 | fill_addr4(ip.ip4, &addr4->sin_addr); |
684 | 687 | ||
685 | portptr = &addr4->sin_port; | 688 | portptr = &addr4->sin_port; |
686 | } else if (temp->family == AF_INET6) { | 689 | } else if (temp->family == TOX_AF_INET6) { |
687 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 690 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
688 | 691 | ||
689 | addrsize = sizeof(struct sockaddr_in6); | 692 | addrsize = sizeof(struct sockaddr_in6); |
@@ -700,7 +703,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 | |||
700 | return NULL; | 703 | return NULL; |
701 | } | 704 | } |
702 | 705 | ||
703 | if (ip.family == AF_INET6) { | 706 | if (ip.family == TOX_AF_INET6) { |
704 | int is_dualstack = set_socket_dualstack(temp->sock); | 707 | int is_dualstack = set_socket_dualstack(temp->sock); |
705 | LOGGER_DEBUG(log, "Dual-stack socket: %s", | 708 | LOGGER_DEBUG(log, "Dual-stack socket: %s", |
706 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); | 709 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses"); |
@@ -812,7 +815,7 @@ int ip_equal(const IP *a, const IP *b) | |||
812 | 815 | ||
813 | /* same family */ | 816 | /* same family */ |
814 | if (a->family == b->family) { | 817 | if (a->family == b->family) { |
815 | if (a->family == AF_INET || a->family == TCP_INET) { | 818 | if (a->family == TOX_AF_INET || a->family == TCP_INET) { |
816 | struct in_addr addr_a; | 819 | struct in_addr addr_a; |
817 | struct in_addr addr_b; | 820 | struct in_addr addr_b; |
818 | fill_addr4(a->ip4, &addr_a); | 821 | fill_addr4(a->ip4, &addr_a); |
@@ -820,7 +823,7 @@ int ip_equal(const IP *a, const IP *b) | |||
820 | return addr_a.s_addr == addr_b.s_addr; | 823 | return addr_a.s_addr == addr_b.s_addr; |
821 | } | 824 | } |
822 | 825 | ||
823 | if (a->family == AF_INET6 || a->family == TCP_INET6) { | 826 | if (a->family == TOX_AF_INET6 || a->family == TCP_INET6) { |
824 | return a->ip6.uint64[0] == b->ip6.uint64[0] && | 827 | return a->ip6.uint64[0] == b->ip6.uint64[0] && |
825 | a->ip6.uint64[1] == b->ip6.uint64[1]; | 828 | a->ip6.uint64[1] == b->ip6.uint64[1]; |
826 | } | 829 | } |
@@ -829,13 +832,13 @@ int ip_equal(const IP *a, const IP *b) | |||
829 | } | 832 | } |
830 | 833 | ||
831 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ | 834 | /* different family: check on the IPv6 one if it is the IPv4 one embedded */ |
832 | if ((a->family == AF_INET) && (b->family == AF_INET6)) { | 835 | if ((a->family == TOX_AF_INET) && (b->family == TOX_AF_INET6)) { |
833 | if (IPV6_IPV4_IN_V6(b->ip6)) { | 836 | if (IPV6_IPV4_IN_V6(b->ip6)) { |
834 | struct in_addr addr_a; | 837 | struct in_addr addr_a; |
835 | fill_addr4(a->ip4, &addr_a); | 838 | fill_addr4(a->ip4, &addr_a); |
836 | return addr_a.s_addr == b->ip6.uint32[3]; | 839 | return addr_a.s_addr == b->ip6.uint32[3]; |
837 | } | 840 | } |
838 | } else if ((a->family == AF_INET6) && (b->family == AF_INET)) { | 841 | } else if ((a->family == TOX_AF_INET6) && (b->family == TOX_AF_INET)) { |
839 | if (IPV6_IPV4_IN_V6(a->ip6)) { | 842 | if (IPV6_IPV4_IN_V6(a->ip6)) { |
840 | struct in_addr addr_b; | 843 | struct in_addr addr_b; |
841 | fill_addr4(b->ip4, &addr_b); | 844 | fill_addr4(b->ip4, &addr_b); |
@@ -883,7 +886,7 @@ void ip_init(IP *ip, uint8_t ipv6enabled) | |||
883 | } | 886 | } |
884 | 887 | ||
885 | memset(ip, 0, sizeof(IP)); | 888 | memset(ip, 0, sizeof(IP)); |
886 | ip->family = ipv6enabled ? AF_INET6 : AF_INET; | 889 | ip->family = ipv6enabled ? TOX_AF_INET6 : TOX_AF_INET; |
887 | } | 890 | } |
888 | 891 | ||
889 | /* checks if ip is valid */ | 892 | /* checks if ip is valid */ |
@@ -947,20 +950,22 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
947 | } | 950 | } |
948 | 951 | ||
949 | if (ip) { | 952 | if (ip) { |
950 | if (ip->family == AF_INET) { | 953 | const int family = make_family(ip->family); |
954 | |||
955 | if (ip->family == TOX_AF_INET) { | ||
951 | /* returns standard quad-dotted notation */ | 956 | /* returns standard quad-dotted notation */ |
952 | struct in_addr addr; | 957 | struct in_addr addr; |
953 | fill_addr4(ip->ip4, &addr); | 958 | fill_addr4(ip->ip4, &addr); |
954 | 959 | ||
955 | ip_str[0] = 0; | 960 | ip_str[0] = 0; |
956 | inet_ntop(ip->family, &addr, ip_str, length); | 961 | inet_ntop(family, &addr, ip_str, length); |
957 | } else if (ip->family == AF_INET6) { | 962 | } else if (ip->family == TOX_AF_INET6) { |
958 | /* returns hex-groups enclosed into square brackets */ | 963 | /* returns hex-groups enclosed into square brackets */ |
959 | struct in6_addr addr; | 964 | struct in6_addr addr; |
960 | fill_addr6(ip->ip6, &addr); | 965 | fill_addr6(ip->ip6, &addr); |
961 | 966 | ||
962 | ip_str[0] = '['; | 967 | ip_str[0] = '['; |
963 | inet_ntop(ip->family, &addr, &ip_str[1], length - 3); | 968 | inet_ntop(family, &addr, &ip_str[1], length - 3); |
964 | size_t len = strlen(ip_str); | 969 | size_t len = strlen(ip_str); |
965 | ip_str[len] = ']'; | 970 | ip_str[len] = ']'; |
966 | ip_str[len + 1] = 0; | 971 | ip_str[len + 1] = 0; |
@@ -981,10 +986,10 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) | |||
981 | * parses IP structure into an address string | 986 | * parses IP structure into an address string |
982 | * | 987 | * |
983 | * input | 988 | * input |
984 | * ip: ip of AF_INET or AF_INET6 families | 989 | * ip: ip of TOX_AF_INET or TOX_AF_INET6 families |
985 | * length: length of the address buffer | 990 | * length: length of the address buffer |
986 | * Must be at least INET_ADDRSTRLEN for AF_INET | 991 | * Must be at least INET_ADDRSTRLEN for TOX_AF_INET |
987 | * and INET6_ADDRSTRLEN for AF_INET6 | 992 | * and INET6_ADDRSTRLEN for TOX_AF_INET6 |
988 | * | 993 | * |
989 | * output | 994 | * output |
990 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) | 995 | * address: dotted notation (IPv4: quad, IPv6: 16) or colon notation (IPv6) |
@@ -997,12 +1002,12 @@ int ip_parse_addr(const IP *ip, char *address, size_t length) | |||
997 | return 0; | 1002 | return 0; |
998 | } | 1003 | } |
999 | 1004 | ||
1000 | if (ip->family == AF_INET) { | 1005 | if (ip->family == TOX_AF_INET) { |
1001 | const struct in_addr *addr = (const struct in_addr *)&ip->ip4; | 1006 | const struct in_addr *addr = (const struct in_addr *)&ip->ip4; |
1002 | return inet_ntop(ip->family, addr, address, length) != NULL; | 1007 | return inet_ntop(ip->family, addr, address, length) != NULL; |
1003 | } | 1008 | } |
1004 | 1009 | ||
1005 | if (ip->family == AF_INET6) { | 1010 | if (ip->family == TOX_AF_INET6) { |
1006 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; | 1011 | const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; |
1007 | return inet_ntop(ip->family, addr, address, length) != NULL; | 1012 | return inet_ntop(ip->family, addr, address, length) != NULL; |
1008 | } | 1013 | } |
@@ -1031,16 +1036,16 @@ int addr_parse_ip(const char *address, IP *to) | |||
1031 | 1036 | ||
1032 | struct in_addr addr4; | 1037 | struct in_addr addr4; |
1033 | 1038 | ||
1034 | if (1 == inet_pton(AF_INET, address, &addr4)) { | 1039 | if (inet_pton(AF_INET, address, &addr4) == 1) { |
1035 | to->family = AF_INET; | 1040 | to->family = TOX_AF_INET; |
1036 | get_ip4(&to->ip4, &addr4); | 1041 | get_ip4(&to->ip4, &addr4); |
1037 | return 1; | 1042 | return 1; |
1038 | } | 1043 | } |
1039 | 1044 | ||
1040 | struct in6_addr addr6; | 1045 | struct in6_addr addr6; |
1041 | 1046 | ||
1042 | if (1 == inet_pton(AF_INET6, address, &addr6)) { | 1047 | if (inet_pton(AF_INET6, address, &addr6) == 1) { |
1043 | to->family = AF_INET6; | 1048 | to->family = TOX_AF_INET6; |
1044 | get_ip6(&to->ip6, &addr6); | 1049 | get_ip6(&to->ip6, &addr6); |
1045 | return 1; | 1050 | return 1; |
1046 | } | 1051 | } |
@@ -1056,13 +1061,13 @@ int addr_parse_ip(const char *address, IP *to) | |||
1056 | * input | 1061 | * input |
1057 | * address: a hostname (or something parseable to an IP address) | 1062 | * address: a hostname (or something parseable to an IP address) |
1058 | * to: to.family MUST be initialized, either set to a specific IP version | 1063 | * to: to.family MUST be initialized, either set to a specific IP version |
1059 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | 1064 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both |
1060 | * IP versions are acceptable | 1065 | * IP versions are acceptable |
1061 | * extra can be NULL and is only set in special circumstances, see returns | 1066 | * extra can be NULL and is only set in special circumstances, see returns |
1062 | * | 1067 | * |
1063 | * returns in *to a valid IPAny (v4/v6), | 1068 | * returns in *to a valid IPAny (v4/v6), |
1064 | * prefers v6 if ip.family was AF_UNSPEC and both available | 1069 | * prefers v6 if ip.family was AF_UNSPEC and both available |
1065 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6 | 1070 | * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is TOX_AF_INET6 |
1066 | * returns 0 on failure, TOX_ADDR_RESOLVE_* on success. | 1071 | * returns 0 on failure, TOX_ADDR_RESOLVE_* on success. |
1067 | */ | 1072 | */ |
1068 | int addr_resolve(const char *address, IP *to, IP *extra) | 1073 | int addr_resolve(const char *address, IP *to, IP *extra) |
@@ -1071,7 +1076,8 @@ int addr_resolve(const char *address, IP *to, IP *extra) | |||
1071 | return 0; | 1076 | return 0; |
1072 | } | 1077 | } |
1073 | 1078 | ||
1074 | Family family = to->family; | 1079 | Family tox_family = to->family; |
1080 | Family family = make_family(tox_family); | ||
1075 | 1081 | ||
1076 | struct addrinfo *server = NULL; | 1082 | struct addrinfo *server = NULL; |
1077 | struct addrinfo *walker = NULL; | 1083 | struct addrinfo *walker = NULL; |
@@ -1160,7 +1166,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) | |||
1160 | * | 1166 | * |
1161 | * address: a hostname (or something parseable to an IP address) | 1167 | * address: a hostname (or something parseable to an IP address) |
1162 | * to: to.family MUST be initialized, either set to a specific IP version | 1168 | * to: to.family MUST be initialized, either set to a specific IP version |
1163 | * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both | 1169 | * (TOX_AF_INET/TOX_AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both |
1164 | * IP versions are acceptable | 1170 | * IP versions are acceptable |
1165 | * extra can be NULL and is only set in special circumstances, see returns | 1171 | * extra can be NULL and is only set in special circumstances, see returns |
1166 | * | 1172 | * |
@@ -1185,14 +1191,14 @@ int net_connect(Socket sock, IP_Port ip_port) | |||
1185 | struct sockaddr_storage addr = {0}; | 1191 | struct sockaddr_storage addr = {0}; |
1186 | size_t addrsize; | 1192 | size_t addrsize; |
1187 | 1193 | ||
1188 | if (ip_port.ip.family == AF_INET) { | 1194 | if (ip_port.ip.family == TOX_AF_INET) { |
1189 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 1195 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
1190 | 1196 | ||
1191 | addrsize = sizeof(struct sockaddr_in); | 1197 | addrsize = sizeof(struct sockaddr_in); |
1192 | addr4->sin_family = AF_INET; | 1198 | addr4->sin_family = AF_INET; |
1193 | fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); | 1199 | fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); |
1194 | addr4->sin_port = ip_port.port; | 1200 | addr4->sin_port = ip_port.port; |
1195 | } else if (ip_port.ip.family == AF_INET6) { | 1201 | } else if (ip_port.ip.family == TOX_AF_INET6) { |
1196 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 1202 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
1197 | 1203 | ||
1198 | addrsize = sizeof(struct sockaddr_in6); | 1204 | addrsize = sizeof(struct sockaddr_in6); |
@@ -1206,7 +1212,7 @@ int net_connect(Socket sock, IP_Port ip_port) | |||
1206 | return connect(sock, (struct sockaddr *)&addr, addrsize); | 1212 | return connect(sock, (struct sockaddr *)&addr, addrsize); |
1207 | } | 1213 | } |
1208 | 1214 | ||
1209 | int32_t net_getipport(const char *node, IP_Port **res, int type) | 1215 | int32_t net_getipport(const char *node, IP_Port **res, int tox_type) |
1210 | { | 1216 | { |
1211 | struct addrinfo *infos; | 1217 | struct addrinfo *infos; |
1212 | int ret = getaddrinfo(node, NULL, NULL, &infos); | 1218 | int ret = getaddrinfo(node, NULL, NULL, &infos); |
@@ -1218,6 +1224,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int type) | |||
1218 | 1224 | ||
1219 | // Used to avoid malloc parameter overflow | 1225 | // Used to avoid malloc parameter overflow |
1220 | const size_t MAX_COUNT = MIN(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); | 1226 | const size_t MAX_COUNT = MIN(SIZE_MAX, INT32_MAX) / sizeof(IP_Port); |
1227 | int type = make_socktype(tox_type); | ||
1221 | struct addrinfo *cur; | 1228 | struct addrinfo *cur; |
1222 | int32_t count = 0; | 1229 | int32_t count = 0; |
1223 | 1230 | ||
@@ -1262,7 +1269,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int type) | |||
1262 | continue; | 1269 | continue; |
1263 | } | 1270 | } |
1264 | 1271 | ||
1265 | ip_port->ip.family = cur->ai_family; | 1272 | ip_port->ip.family = make_tox_family(cur->ai_family); |
1266 | 1273 | ||
1267 | ip_port++; | 1274 | ip_port++; |
1268 | } | 1275 | } |
@@ -1285,13 +1292,13 @@ int bind_to_port(Socket sock, int family, uint16_t port) | |||
1285 | struct sockaddr_storage addr = {0}; | 1292 | struct sockaddr_storage addr = {0}; |
1286 | size_t addrsize; | 1293 | size_t addrsize; |
1287 | 1294 | ||
1288 | if (family == AF_INET) { | 1295 | if (family == TOX_AF_INET) { |
1289 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; | 1296 | struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; |
1290 | 1297 | ||
1291 | addrsize = sizeof(struct sockaddr_in); | 1298 | addrsize = sizeof(struct sockaddr_in); |
1292 | addr4->sin_family = AF_INET; | 1299 | addr4->sin_family = AF_INET; |
1293 | addr4->sin_port = net_htons(port); | 1300 | addr4->sin_port = net_htons(port); |
1294 | } else if (family == AF_INET6) { | 1301 | } else if (family == TOX_AF_INET6) { |
1295 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; | 1302 | struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; |
1296 | 1303 | ||
1297 | addrsize = sizeof(struct sockaddr_in6); | 1304 | addrsize = sizeof(struct sockaddr_in6); |
@@ -1304,17 +1311,37 @@ int bind_to_port(Socket sock, int family, uint16_t port) | |||
1304 | return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); | 1311 | return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); |
1305 | } | 1312 | } |
1306 | 1313 | ||
1307 | static int make_family(int family) | 1314 | static int make_tox_family(int family) |
1308 | { | 1315 | { |
1309 | switch (family) { | 1316 | switch (family) { |
1317 | case AF_INET: | ||
1318 | return TOX_AF_INET; | ||
1319 | |||
1320 | case AF_INET6: | ||
1321 | return TOX_AF_INET6; | ||
1322 | |||
1323 | case AF_UNSPEC: | ||
1324 | return TOX_AF_UNSPEC; | ||
1325 | |||
1326 | default: | ||
1327 | return family; | ||
1328 | } | ||
1329 | } | ||
1330 | |||
1331 | static int make_family(int tox_family) | ||
1332 | { | ||
1333 | switch (tox_family) { | ||
1310 | case TOX_AF_INET: | 1334 | case TOX_AF_INET: |
1311 | return AF_INET; | 1335 | return AF_INET; |
1312 | 1336 | ||
1313 | case TOX_AF_INET6: | 1337 | case TOX_AF_INET6: |
1314 | return AF_INET6; | 1338 | return AF_INET6; |
1315 | 1339 | ||
1340 | case TOX_AF_UNSPEC: | ||
1341 | return AF_UNSPEC; | ||
1342 | |||
1316 | default: | 1343 | default: |
1317 | return family; | 1344 | return tox_family; |
1318 | } | 1345 | } |
1319 | } | 1346 | } |
1320 | 1347 | ||