Added inet_pton/inet_ntop for MinGW on Windows; compiles with Lua52.
This commit is contained in:
parent
d548a78e55
commit
5a58786a39
3 changed files with 65 additions and 0 deletions
50
src/inet.c
50
src/inet.c
|
@ -507,4 +507,54 @@ int inet_aton(const char *cp, struct in_addr *inp)
|
|||
}
|
||||
#endif
|
||||
|
||||
// inet_ntop/inet_pton for MinGW from http://mingw-users.1079350.n2.nabble.com/IPv6-getaddrinfo-amp-inet-ntop-td5891996.html
|
||||
|
||||
#ifdef INET_PTON
|
||||
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt)
|
||||
{
|
||||
if (af == AF_INET)
|
||||
{
|
||||
struct sockaddr_in in;
|
||||
memset(&in, 0, sizeof(in));
|
||||
in.sin_family = AF_INET;
|
||||
memcpy(&in.sin_addr, src, sizeof(struct in_addr));
|
||||
getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
||||
return dst;
|
||||
}
|
||||
else if (af == AF_INET6)
|
||||
{
|
||||
struct sockaddr_in6 in;
|
||||
memset(&in, 0, sizeof(in));
|
||||
in.sin6_family = AF_INET6;
|
||||
memcpy(&in.sin6_addr, src, sizeof(struct in_addr6));
|
||||
getnameinfo((struct sockaddr *)&in, sizeof(struct sockaddr_in6), dst, cnt, NULL, 0, NI_NUMERICHOST);
|
||||
return dst;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
struct addrinfo hints, *res, *ressave;
|
||||
|
||||
memset(&hints, 0, sizeof(struct addrinfo));
|
||||
hints.ai_family = af;
|
||||
|
||||
if (getaddrinfo(src, NULL, &hints, &res) != 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
ressave = res;
|
||||
|
||||
while (res)
|
||||
{
|
||||
memcpy(dst, res->ai_addr, res->ai_addrlen);
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
freeaddrinfo(ressave);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue