summaryrefslogtreecommitdiff
path: root/toxcore
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore')
-rw-r--r--toxcore/network.c62
1 files changed, 48 insertions, 14 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 67122ea3..d6d5bfd4 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -83,6 +83,26 @@
83 83
84#define TOX_EWOULDBLOCK EWOULDBLOCK 84#define TOX_EWOULDBLOCK EWOULDBLOCK
85 85
86static const char *inet_ntop4(int family, const struct in_addr *addr, char *buf, size_t bufsize)
87{
88 return inet_ntop(family, addr, buf, bufsize);
89}
90
91static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize)
92{
93 return inet_ntop(family, addr, buf, bufsize);
94}
95
96static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf)
97{
98 return inet_pton(family, addrString, addrbuf);
99}
100
101static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf)
102{
103 return inet_pton(family, addrString, addrbuf);
104}
105
86#else 106#else
87#ifndef IPV6_V6ONLY 107#ifndef IPV6_V6ONLY
88#define IPV6_V6ONLY 27 108#define IPV6_V6ONLY 27
@@ -90,14 +110,14 @@
90 110
91#define TOX_EWOULDBLOCK WSAEWOULDBLOCK 111#define TOX_EWOULDBLOCK WSAEWOULDBLOCK
92 112
93static const char *inet_ntop(int family, const void *addr, char *buf, size_t bufsize) 113static const char *inet_ntop4(int family, const struct in_addr *addr, char *buf, size_t bufsize)
94{ 114{
95 if (family == AF_INET) { 115 if (family == AF_INET) {
96 struct sockaddr_in saddr; 116 struct sockaddr_in saddr;
97 memset(&saddr, 0, sizeof(saddr)); 117 memset(&saddr, 0, sizeof(saddr));
98 118
99 saddr.sin_family = AF_INET; 119 saddr.sin_family = AF_INET;
100 saddr.sin_addr = *(const struct in_addr *)addr; 120 saddr.sin_addr = *addr;
101 121
102 DWORD len = bufsize; 122 DWORD len = bufsize;
103 123
@@ -106,12 +126,19 @@ static const char *inet_ntop(int family, const void *addr, char *buf, size_t buf
106 } 126 }
107 127
108 return buf; 128 return buf;
109 } else if (family == AF_INET6) { 129 }
130
131 return nullptr;
132}
133
134static const char *inet_ntop6(int family, const struct in6_addr *addr, char *buf, size_t bufsize)
135{
136 if (family == AF_INET6) {
110 struct sockaddr_in6 saddr; 137 struct sockaddr_in6 saddr;
111 memset(&saddr, 0, sizeof(saddr)); 138 memset(&saddr, 0, sizeof(saddr));
112 139
113 saddr.sin6_family = AF_INET6; 140 saddr.sin6_family = AF_INET6;
114 saddr.sin6_addr = *(const struct in6_addr *)addr; 141 saddr.sin6_addr = *addr;
115 142
116 DWORD len = bufsize; 143 DWORD len = bufsize;
117 144
@@ -125,7 +152,7 @@ static const char *inet_ntop(int family, const void *addr, char *buf, size_t buf
125 return nullptr; 152 return nullptr;
126} 153}
127 154
128static int inet_pton(int family, const char *addrString, void *addrbuf) 155static int inet_pton4(int family, const char *addrString, struct in_addr *addrbuf)
129{ 156{
130 if (family == AF_INET) { 157 if (family == AF_INET) {
131 struct sockaddr_in saddr; 158 struct sockaddr_in saddr;
@@ -137,10 +164,17 @@ static int inet_pton(int family, const char *addrString, void *addrbuf)
137 return 0; 164 return 0;
138 } 165 }
139 166
140 *(struct in_addr *)addrbuf = saddr.sin_addr; 167 *addrbuf = saddr.sin_addr;
141 168
142 return 1; 169 return 1;
143 } else if (family == AF_INET6) { 170 }
171
172 return 0;
173}
174
175static int inet_pton6(int family, const char *addrString, struct in6_addr *addrbuf)
176{
177 if (family == AF_INET6) {
144 struct sockaddr_in6 saddr; 178 struct sockaddr_in6 saddr;
145 memset(&saddr, 0, sizeof(saddr)); 179 memset(&saddr, 0, sizeof(saddr));
146 180
@@ -150,7 +184,7 @@ static int inet_pton(int family, const char *addrString, void *addrbuf)
150 return 0; 184 return 0;
151 } 185 }
152 186
153 *(struct in6_addr *)addrbuf = saddr.sin6_addr; 187 *addrbuf = saddr.sin6_addr;
154 188
155 return 1; 189 return 1;
156 } 190 }
@@ -1085,14 +1119,14 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length)
1085 fill_addr4(ip->ip.v4, &addr); 1119 fill_addr4(ip->ip.v4, &addr);
1086 1120
1087 ip_str[0] = 0; 1121 ip_str[0] = 0;
1088 inet_ntop(family, &addr, ip_str, length); 1122 inet_ntop4(family, &addr, ip_str, length);
1089 } else if (net_family_is_ipv6(ip->family)) { 1123 } else if (net_family_is_ipv6(ip->family)) {
1090 /* returns hex-groups enclosed into square brackets */ 1124 /* returns hex-groups enclosed into square brackets */
1091 struct in6_addr addr; 1125 struct in6_addr addr;
1092 fill_addr6(ip->ip.v6, &addr); 1126 fill_addr6(ip->ip.v6, &addr);
1093 1127
1094 ip_str[0] = '['; 1128 ip_str[0] = '[';
1095 inet_ntop(family, &addr, &ip_str[1], length - 3); 1129 inet_ntop6(family, &addr, &ip_str[1], length - 3);
1096 size_t len = strlen(ip_str); 1130 size_t len = strlen(ip_str);
1097 ip_str[len] = ']'; 1131 ip_str[len] = ']';
1098 ip_str[len + 1] = 0; 1132 ip_str[len + 1] = 0;
@@ -1116,12 +1150,12 @@ bool ip_parse_addr(const IP *ip, char *address, size_t length)
1116 1150
1117 if (net_family_is_ipv4(ip->family)) { 1151 if (net_family_is_ipv4(ip->family)) {
1118 const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4; 1152 const struct in_addr *addr = (const struct in_addr *)&ip->ip.v4;
1119 return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; 1153 return inet_ntop4(make_family(ip->family), addr, address, length) != nullptr;
1120 } 1154 }
1121 1155
1122 if (net_family_is_ipv6(ip->family)) { 1156 if (net_family_is_ipv6(ip->family)) {
1123 const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6; 1157 const struct in6_addr *addr = (const struct in6_addr *)&ip->ip.v6;
1124 return inet_ntop(make_family(ip->family), addr, address, length) != nullptr; 1158 return inet_ntop6(make_family(ip->family), addr, address, length) != nullptr;
1125 } 1159 }
1126 1160
1127 return false; 1161 return false;
@@ -1135,7 +1169,7 @@ bool addr_parse_ip(const char *address, IP *to)
1135 1169
1136 struct in_addr addr4; 1170 struct in_addr addr4;
1137 1171
1138 if (inet_pton(AF_INET, address, &addr4) == 1) { 1172 if (inet_pton4(AF_INET, address, &addr4) == 1) {
1139 to->family = net_family_ipv4; 1173 to->family = net_family_ipv4;
1140 get_ip4(&to->ip.v4, &addr4); 1174 get_ip4(&to->ip.v4, &addr4);
1141 return true; 1175 return true;
@@ -1143,7 +1177,7 @@ bool addr_parse_ip(const char *address, IP *to)
1143 1177
1144 struct in6_addr addr6; 1178 struct in6_addr addr6;
1145 1179
1146 if (inet_pton(AF_INET6, address, &addr6) == 1) { 1180 if (inet_pton6(AF_INET6, address, &addr6) == 1) {
1147 to->family = net_family_ipv6; 1181 to->family = net_family_ipv6;
1148 get_ip6(&to->ip.v6, &addr6); 1182 get_ip6(&to->ip.v6, &addr6);
1149 return true; 1183 return true;