diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/inet.c | 50 | ||||
| -rw-r--r-- | src/inet.h | 6 |
2 files changed, 56 insertions, 0 deletions
| @@ -507,4 +507,54 @@ int inet_aton(const char *cp, struct in_addr *inp) | |||
| 507 | } | 507 | } |
| 508 | #endif | 508 | #endif |
| 509 | 509 | ||
| 510 | // inet_ntop/inet_pton for MinGW from http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html | ||
| 510 | 511 | ||
| 512 | #ifdef INET_PTON | ||
| 513 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) | ||
| 514 | { | ||
| 515 | if (af == AF_INET) | ||
| 516 | { | ||
| 517 | struct sockaddr_in in; | ||
| 518 | memset(&in, 0, sizeof(in)); | ||
| 519 | in.sin_family = AF_INET; | ||
| 520 | memcpy(&in.sin_addr, src, sizeof(struct in_addr)); | ||
| 521 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST); | ||
| 522 | return dst; | ||
| 523 | } | ||
| 524 | else if (af == AF_INET6) | ||
| 525 | { | ||
| 526 | struct sockaddr_in6 in; | ||
| 527 | memset(&in, 0, sizeof(in)); | ||
| 528 | in.sin6_family = AF_INET6; | ||
| 529 | memcpy(&in.sin6_addr, src, sizeof(struct in_addr6)); | ||
| 530 | getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST); | ||
| 531 | return dst; | ||
| 532 | } | ||
| 533 | return NULL; | ||
| 534 | } | ||
| 535 | |||
| 536 | int inet_pton(int af, const char *src, void *dst) | ||
| 537 | { | ||
| 538 | struct addrinfo hints, *res, *ressave; | ||
| 539 | |||
| 540 | memset(&hints, 0, sizeof(struct addrinfo)); | ||
| 541 | hints.ai_family = af; | ||
| 542 | |||
| 543 | if (getaddrinfo(src, NULL, &hints, &res) != 0) | ||
| 544 | { | ||
| 545 | return -1; | ||
| 546 | } | ||
| 547 | |||
| 548 | ressave = res; | ||
| 549 | |||
| 550 | while (res) | ||
| 551 | { | ||
| 552 | memcpy(dst, res->ai_addr, res->ai_addrlen); | ||
| 553 | res = res->ai_next; | ||
| 554 | } | ||
| 555 | |||
| 556 | freeaddrinfo(ressave); | ||
| 557 | return 0; | ||
| 558 | } | ||
| 559 | |||
| 560 | #endif | ||
| @@ -20,6 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | #ifdef _WIN32 | 21 | #ifdef _WIN32 |
| 22 | #define INET_ATON | 22 | #define INET_ATON |
| 23 | #define INET_PTON | ||
| 23 | #endif | 24 | #endif |
| 24 | 25 | ||
| 25 | int inet_open(lua_State *L); | 26 | int inet_open(lua_State *L); |
| @@ -42,4 +43,9 @@ int inet_optsocktype(lua_State* L, int narg, const char* def); | |||
| 42 | int inet_aton(const char *cp, struct in_addr *inp); | 43 | int inet_aton(const char *cp, struct in_addr *inp); |
| 43 | #endif | 44 | #endif |
| 44 | 45 | ||
| 46 | #ifdef INET_PTON | ||
| 47 | const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt); | ||
| 48 | int inet_pton(int af, const char *src, void *dst); | ||
| 49 | #endif | ||
| 50 | |||
| 45 | #endif /* INET_H */ | 51 | #endif /* INET_H */ |
