From 395df606c0d81293c16bf0c0d0026351b1556340 Mon Sep 17 00:00:00 2001 From: kervala Date: Sun, 14 Feb 2016 19:58:42 +0100 Subject: [PATCH] Fixed: Windows XP doesn't support inet_pton and inet_ntop --- code/nel/src/net/inet_address.cpp | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/code/nel/src/net/inet_address.cpp b/code/nel/src/net/inet_address.cpp index 45ff0c20e..79224b1ee 100644 --- a/code/nel/src/net/inet_address.cpp +++ b/code/nel/src/net/inet_address.cpp @@ -30,6 +30,84 @@ # include // for Windows 2000 compatibility # include + +#if !defined(NTDDI_VISTA) || (NTDDI_VERSION < NTDDI_VISTA) + +// inet_pton and inet_ntop not defined in winsock DLL before Vista + +// taken from http://stackoverflow.com/questions/13731243/what-is-the-windows-xp-equivalent-of-inet-pton-or-inetpton +int inet_pton(int af, const char *src, void *dst) +{ + struct sockaddr_storage ss; + int size = sizeof(ss); + char src_copy[INET6_ADDRSTRLEN+1]; + + ZeroMemory(&ss, sizeof(ss)); + // stupid non-const API + strncpy (src_copy, src, INET6_ADDRSTRLEN+1); + src_copy[INET6_ADDRSTRLEN] = 0; + + if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0) + { + switch(af) + { + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; + } + } + + return 0; +} + +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) +{ + struct sockaddr_storage ss; + unsigned long s = size; + + ZeroMemory(&ss, sizeof(ss)); + ss.ss_family = af; + + switch(af) + { + case AF_INET: + ((struct sockaddr_in *)&ss)->sin_addr = *(struct in_addr *)src; + break; + + case AF_INET6: + ((struct sockaddr_in6 *)&ss)->sin6_addr = *(struct in6_addr *)src; + break; + + default: + return NULL; + } + + // cannot directly use &size because of strict aliasing rules + return WSAAddressToString((struct sockaddr *)&ss, sizeof(ss), NULL, dst, &s) == 0 ? dst : NULL; +} + +BOOLEAN IN6_IS_ADDR_UNSPECIFIED(CONST IN6_ADDR *a) +{ + // + // We can't use the in6addr_any variable, since that would + // require existing callers to link with a specific library. + // + return (BOOLEAN)((a->s6_words[0] == 0) && + (a->s6_words[1] == 0) && + (a->s6_words[2] == 0) && + (a->s6_words[3] == 0) && + (a->s6_words[4] == 0) && + (a->s6_words[5] == 0) && + (a->s6_words[6] == 0) && + (a->s6_words[7] == 0)); +} + +#endif + #elif defined NL_OS_UNIX # include # include