diff options
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/network.c | 81 | ||||
-rw-r--r-- | toxcore/network.h | 6 |
2 files changed, 55 insertions, 32 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index f504401e..31da833c 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -235,6 +235,38 @@ void kill_networking(Networking_Core *net) | |||
235 | return; | 235 | return; |
236 | } | 236 | } |
237 | 237 | ||
238 | /* ipany_ntoa | ||
239 | * converts ip into a string | ||
240 | * uses a static buffer, so mustn't used multiple times in the same output | ||
241 | */ | ||
242 | /* there would be INET6_ADDRSTRLEN, but it might be too short for the error message */ | ||
243 | static char addresstext[96]; | ||
244 | const char *ipany_ntoa(IPAny *ip) | ||
245 | { | ||
246 | if (ip) { | ||
247 | if (ip->family == AF_INET) { | ||
248 | addresstext[0] = 0; | ||
249 | struct in_addr *addr = (struct in_addr *)&ip->ip4; | ||
250 | inet_ntop(ip->family, addr, addresstext, sizeof(addresstext)); | ||
251 | } | ||
252 | else if (ip->family == AF_INET6) { | ||
253 | addresstext[0] = '['; | ||
254 | struct in6_addr *addr = (struct in6_addr *)&ip->ip6; | ||
255 | inet_ntop(ip->family, addr, &addresstext[1], sizeof(addresstext) - 3); | ||
256 | size_t len = strlen(addresstext); | ||
257 | addresstext[len] = ']'; | ||
258 | addresstext[len + 1] = 0; | ||
259 | } | ||
260 | else | ||
261 | snprintf(addresstext, sizeof(addresstext), "(IP invalid, family %u)", ip->family); | ||
262 | } | ||
263 | else | ||
264 | snprintf(addresstext, sizeof(addresstext), "(IP invalid: NULL)"); | ||
265 | |||
266 | addresstext[INET6_ADDRSTRLEN + 2] = 0; | ||
267 | return addresstext; | ||
268 | }; | ||
269 | |||
238 | /* | 270 | /* |
239 | * addr_parse_ip | 271 | * addr_parse_ip |
240 | * directly parses the input into an IP structure | 272 | * directly parses the input into an IP structure |
@@ -252,16 +284,14 @@ void kill_networking(Networking_Core *net) | |||
252 | int addr_parse_ip(const char *address, IPAny *to) | 284 | int addr_parse_ip(const char *address, IPAny *to) |
253 | { | 285 | { |
254 | struct in_addr addr4; | 286 | struct in_addr addr4; |
255 | if (1 == inet_pton(AF_INET, address, &addr4)) | 287 | if (1 == inet_pton(AF_INET, address, &addr4)) { |
256 | { | ||
257 | to->family = AF_INET; | 288 | to->family = AF_INET; |
258 | to->ip4.in_addr = addr4; | 289 | to->ip4.in_addr = addr4; |
259 | return 1; | 290 | return 1; |
260 | }; | 291 | }; |
261 | 292 | ||
262 | struct in6_addr addr6; | 293 | struct in6_addr addr6; |
263 | if (1 == inet_pton(AF_INET6, address, &addr6)) | 294 | if (1 == inet_pton(AF_INET6, address, &addr6)) { |
264 | { | ||
265 | to->family = AF_INET6; | 295 | to->family = AF_INET6; |
266 | to->ip6 = addr6; | 296 | to->ip6 = addr6; |
267 | return 1; | 297 | return 1; |
@@ -324,24 +354,18 @@ int addr_resolve(const char *address, IPAny *ip) | |||
324 | memset(&ip6, 0, sizeof(ip6)); | 354 | memset(&ip6, 0, sizeof(ip6)); |
325 | 355 | ||
326 | walker = server; | 356 | walker = server; |
327 | while (walker && (rc != 3)) | 357 | while (walker && (rc != 3)) { |
328 | { | 358 | if (ip->family != AF_UNSPEC) { |
329 | if (ip->family != AF_UNSPEC) | ||
330 | { | ||
331 | if (walker->ai_family == ip->family) { | 359 | if (walker->ai_family == ip->family) { |
332 | if (ip->family == AF_INET) | 360 | if (ip->family == AF_INET) { |
333 | { | 361 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) { |
334 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) | ||
335 | { | ||
336 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; | 362 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; |
337 | ip->ip4.in_addr = addr->sin_addr; | 363 | ip->ip4.in_addr = addr->sin_addr; |
338 | rc = 3; | 364 | rc = 3; |
339 | } | 365 | } |
340 | } | 366 | } |
341 | else if (ip->family == AF_INET6) | 367 | else if (ip->family == AF_INET6) { |
342 | { | 368 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { |
343 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) | ||
344 | { | ||
345 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; | 369 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; |
346 | ip->ip6 = addr->sin6_addr; | 370 | ip->ip6 = addr->sin6_addr; |
347 | rc = 3; | 371 | rc = 3; |
@@ -349,21 +373,16 @@ int addr_resolve(const char *address, IPAny *ip) | |||
349 | } | 373 | } |
350 | } | 374 | } |
351 | } | 375 | } |
352 | else | 376 | else { |
353 | { | 377 | if (walker->ai_family == AF_INET) { |
354 | if (walker->ai_family == AF_INET) | 378 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) { |
355 | { | ||
356 | if (walker->ai_addrlen == sizeof(struct sockaddr_in)) | ||
357 | { | ||
358 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; | 379 | struct sockaddr_in *addr = (struct sockaddr_in *)walker->ai_addr; |
359 | ip4.in_addr = addr->sin_addr; | 380 | ip4.in_addr = addr->sin_addr; |
360 | rc |= 1; | 381 | rc |= 1; |
361 | } | 382 | } |
362 | } | 383 | } |
363 | else if (walker->ai_family == AF_INET6) | 384 | else if (walker->ai_family == AF_INET6) { |
364 | { | 385 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) { |
365 | if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) | ||
366 | { | ||
367 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; | 386 | struct sockaddr_in6 *addr = (struct sockaddr_in6 *)walker->ai_addr; |
368 | ip6 = addr->sin6_addr; | 387 | ip6 = addr->sin6_addr; |
369 | rc |= 2; | 388 | rc |= 2; |
@@ -374,14 +393,12 @@ int addr_resolve(const char *address, IPAny *ip) | |||
374 | walker = walker->ai_next; | 393 | walker = walker->ai_next; |
375 | } | 394 | } |
376 | 395 | ||
377 | if (ip->family == AF_UNSPEC) | 396 | if (ip->family == AF_UNSPEC) { |
378 | { | 397 | if (rc & 2) { |
379 | if (rc & 2) | ||
380 | { | ||
381 | ip->family = AF_INET6; | 398 | ip->family = AF_INET6; |
382 | ip->ip6 = ip6; | 399 | ip->ip6 = ip6; |
383 | } else if (rc & 1) | 400 | } |
384 | { | 401 | else if (rc & 1) { |
385 | ip->family = AF_INET; | 402 | ip->family = AF_INET; |
386 | ip->ip4 = ip4; | 403 | ip->ip4 = ip4; |
387 | } | 404 | } |
diff --git a/toxcore/network.h b/toxcore/network.h index 2a0b5560..15ca68e3 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -99,6 +99,12 @@ typedef struct { | |||
99 | }; | 99 | }; |
100 | } IPAny; | 100 | } IPAny; |
101 | 101 | ||
102 | /* ipany_ntoa | ||
103 | * converts ip into a string | ||
104 | * uses a static buffer, so mustn't used multiple times in the same output | ||
105 | */ | ||
106 | const char *ipany_ntoa(IPAny *ip); | ||
107 | |||
102 | typedef union { | 108 | typedef union { |
103 | struct { | 109 | struct { |
104 | IP4 ip; | 110 | IP4 ip; |