From add4a1e8a0323f7c02ec0d8c2a1de7f816a6aaf3 Mon Sep 17 00:00:00 2001 From: Cthulhu-throwaway <96153783+Cthulhu-throwaway@users.noreply.github.com> Date: Thu, 4 Aug 2022 21:23:35 -0300 Subject: [PATCH] (Network) Define inet_ntop and inet_pton for older Windows versions (#14284) --- libretro-common/include/net/net_compat.h | 101 +++++++++++++---------- libretro-common/net/net_compat.c | 85 ++++++++++++++++++- 2 files changed, 142 insertions(+), 44 deletions(-) diff --git a/libretro-common/include/net/net_compat.h b/libretro-common/include/net/net_compat.h index 13e1b2394c..4a0a7ff800 100644 --- a/libretro-common/include/net/net_compat.h +++ b/libretro-common/include/net/net_compat.h @@ -36,8 +36,6 @@ #include -RETRO_BEGIN_DECLS - #if defined(_WIN32) && !defined(_XBOX) #define WIN32_LEAN_AND_MEAN @@ -61,19 +59,6 @@ RETRO_BEGIN_DECLS #define socklen_t unsigned int -struct hostent -{ - char *h_name; - char **h_aliases; - int h_addrtype; - int h_length; - char **h_addr_list; - char *h_addr; - char *h_end; -}; - -struct hostent *gethostbyname(const char *name); - #elif defined(VITA) #include #include @@ -140,31 +125,6 @@ struct hostent *gethostbyname(const char *name); #define inet_ntop sceNetInetNtop #define inet_pton sceNetInetPton -struct pollfd -{ - int fd; - unsigned events; - unsigned revents; - unsigned __pad; /* Align to 64-bits boundary */ -}; - -struct hostent -{ - char *h_name; - char **h_aliases; - int h_addrtype; - int h_length; - char **h_addr_list; - char *h_addr; - char *h_end; -}; - -char *inet_ntoa(struct in_addr in); -int inet_aton(const char *cp, struct in_addr *inp); -uint32_t inet_addr(const char *cp); - -struct hostent *gethostbyname(const char *name); - #elif defined(GEKKO) #include @@ -186,9 +146,6 @@ struct hostent *gethostbyname(const char *name); #define select(a,b,c,d,e) net_select(a,b,c,d,e) #define gethostbyname(a) net_gethostbyname(a) -const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); -int inet_pton(int af, const char *src, void *dst); - #else #include #include @@ -245,6 +202,8 @@ int inet_pton(int af, const char *src, void *dst); #define NET_POLL_HAS_EVENT(sockev, sockfds) ((sockfds)->revents & (sockev)) #endif +RETRO_BEGIN_DECLS + /* Compatibility layer for legacy or incomplete BSD socket implementations. * Only for IPv4. Mostly useful for the consoles which do not support * anything reasonably modern on the socket API side of things. */ @@ -309,6 +268,40 @@ struct addrinfo #endif +#if defined(_XBOX) +struct hostent +{ + char *h_name; + char **h_aliases; + int h_addrtype; + int h_length; + char **h_addr_list; + char *h_addr; + char *h_end; +}; + +#elif defined(VITA) +struct pollfd +{ + int fd; + unsigned events; + unsigned revents; + unsigned __pad; /* Align to 64-bits boundary */ +}; + +struct hostent +{ + char *h_name; + char **h_aliases; + int h_addrtype; + int h_length; + char **h_addr_list; + char *h_addr; + char *h_end; +}; + +#endif + static INLINE bool isagain(int val) { #if defined(_WIN32) @@ -339,6 +332,28 @@ static INLINE bool isinprogress(int val) #endif } +#if defined(_WIN32) && !defined(_XBOX) +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); +int inet_pton(int af, const char *src, void *dst); +#endif + +#elif defined(_XBOX) +struct hostent *gethostbyname(const char *name); + +#elif defined(VITA) +char *inet_ntoa(struct in_addr in); +int inet_aton(const char *cp, struct in_addr *inp); +uint32_t inet_addr(const char *cp); + +struct hostent *gethostbyname(const char *name); + +#elif defined(GEKKO) +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size); +int inet_pton(int af, const char *src, void *dst); + +#endif + int getaddrinfo_retro(const char *node, const char *service, struct addrinfo *hints, struct addrinfo **res); diff --git a/libretro-common/net/net_compat.c b/libretro-common/net/net_compat.c index 7d23bb26e7..b5ea880ad7 100644 --- a/libretro-common/net/net_compat.c +++ b/libretro-common/net/net_compat.c @@ -28,7 +28,90 @@ #include -#if defined(_XBOX) +#if defined(_WIN32) && !defined(_XBOX) +#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 +const char *inet_ntop(int af, const void *src, char *dst, socklen_t size) +{ + struct sockaddr_storage addr; + + switch (af) + { + case AF_INET: + memcpy(&((struct sockaddr_in*)&addr)->sin_addr, src, + sizeof(struct in_addr)); + break; +#ifdef HAVE_INET6 + case AF_INET6: + memcpy(&((struct sockaddr_in6*)&addr)->sin6_addr, src, + sizeof(struct in6_addr)); + break; +#endif + default: + return NULL; + } + + addr.ss_family = af; + if (getnameinfo((struct sockaddr*)&addr, sizeof(addr), dst, size, NULL, 0, + NI_NUMERICHOST)) + return NULL; + + return dst; +} + +int inet_pton(int af, const char *src, void *dst) +{ + struct addrinfo *addr = NULL; + struct addrinfo hints = {0}; + + switch (af) + { + case AF_INET: +#ifdef HAVE_INET6 + case AF_INET6: +#endif + break; + default: + return -1; + } + + hints.ai_family = af; + hints.ai_flags = AI_NUMERICHOST; + switch (getaddrinfo(src, NULL, &hints, &addr)) + { + case 0: + break; + case EAI_NONAME: + return 0; + default: + return -1; + } + + if (!addr) + return -1; + + switch (af) + { + case AF_INET: + memcpy(dst, &((struct sockaddr_in*)addr->ai_addr)->sin_addr, + sizeof(struct in_addr)); + break; +#ifdef HAVE_INET6 + case AF_INET6: + memcpy(dst, &((struct sockaddr_in6*)addr->ai_addr)->sin6_addr, + sizeof(struct in6_addr)); + break; +#endif + default: + break; + } + + freeaddrinfo(addr); + + return 1; +} +#endif + +#elif defined(_XBOX) struct hostent *gethostbyname(const char *name) { static struct in_addr addr = {0};