(Network) Define inet_ntop and inet_pton for older Windows versions (#14284)

This commit is contained in:
Cthulhu-throwaway 2022-08-04 21:23:35 -03:00 committed by GitHub
parent 9c7b96b9d0
commit add4a1e8a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 142 additions and 44 deletions

View File

@ -36,8 +36,6 @@
#include <retro_common_api.h>
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 <psp2/net/net.h>
#include <psp2/net/netctl.h>
@ -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 <network.h>
@ -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 <sys/types.h>
#include <sys/socket.h>
@ -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);

View File

@ -28,7 +28,90 @@
#include <net/net_compat.h>
#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};