From 57285ee3b5570e870252deabc7637644bdadba23 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 2 Jul 2021 14:07:14 +0200 Subject: [PATCH] Fix building for Switch and PS4 --- Source/ThirdParty/enet/enet.h | 136 ++++++++++++++++++++++++++++++---- 1 file changed, 121 insertions(+), 15 deletions(-) diff --git a/Source/ThirdParty/enet/enet.h b/Source/ThirdParty/enet/enet.h index 3e63c9545..73aa72cf0 100644 --- a/Source/ThirdParty/enet/enet.h +++ b/Source/ThirdParty/enet/enet.h @@ -139,14 +139,64 @@ #define ENET_SOCKETSET_CHECK(sockset, socket) FD_ISSET(socket, &(sockset)) #else #include - #include - #include #include + #if PLATFORM_PS4 + #define ENET_IPV6 0 + #include + in_addr in4addr_any = { 0 }; + struct addrinfo { + int ai_flags; + int ai_family; + int ai_socktype; + int ai_protocol; + socklen_t ai_addrlen; + struct sockaddr *ai_addr; + char *ai_canonname; + struct addrinfo *ai_next; + }; + struct pollfd { + int fd; + short int events; + short int revents; + }; + #define AI_NUMERICHOST 0 + #define NI_NAMEREQD 4 + #define EAI_NONAME 8 + #define SOMAXCONN 4096 + #define POLLIN 0x0001 + #define POLLOUT 0x0004 +#ifdef __cplusplus +extern "C" { +#endif + extern int getaddrinfo(const char* node, const char* service, const struct addrinfo* hints, struct addrinfo** res); + extern void freeaddrinfo(struct addrinfo* res); + extern int getnameinfo(const struct sockaddr *addr, socklen_t addrlen, char *host, socklen_t hostlen, char *serv, socklen_t servlen, int flags); + extern int poll(struct pollfd *fds, int nfds, int timeout); +#ifdef __cplusplus +} +#endif + #elif PLATFORM_SWITCH + #define ENET_IPV6 0 #include + #include + in_addr in4addr_any = { 0 }; +#ifdef __cplusplus +extern "C" { +#endif + extern ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); + extern ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); +#ifdef __cplusplus +} +#endif + #else + #include + #include + #include + #endif + #include #include #include #include - #include #include #include #include @@ -210,7 +260,10 @@ #define ENET_MAX(x, y) ((x) > (y) ? (x) : (y)) #define ENET_MIN(x, y) ((x) < (y) ? (x) : (y)) +#ifndef ENET_IPV6 #define ENET_IPV6 1 +#endif +#if ENET_IPV6 const struct in6_addr enet_v4_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }}}; const struct in6_addr enet_v4_noaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}; const struct in6_addr enet_v4_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01 }}}; @@ -218,6 +271,16 @@ const struct in6_addr enet_v6_anyaddr = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 const struct in6_addr enet_v6_noaddr = {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}; const struct in6_addr enet_v6_localhost = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}}; #define ENET_HOST_ANY in6addr_any +#define ENET_IPV_FAMILY AF_INET6 +#else +#define in6_addr in_addr +#define sockaddr_in6 sockaddr_in +#define sin6_family sin_family +#define sin6_port sin_port +#define sin6_addr sin_addr +#define ENET_HOST_ANY in4addr_any +#define ENET_IPV_FAMILY AF_INET +#endif #define ENET_HOST_BROADCAST 0xFFFFFFFFU #define ENET_PORT_ANY 0 @@ -5008,6 +5071,7 @@ extern "C" { void enet_inaddr_map4to6(struct in_addr in, struct in6_addr *out) { +#if ENET_IPV6 if (in.s_addr == 0x00000000) { /* 0.0.0.0 */ *out = enet_v6_anyaddr; } else if (in.s_addr == 0xFFFFFFFF) { /* 255.255.255.255 */ @@ -5021,14 +5085,21 @@ extern "C" { out->s6_addr[14] = ((uint8_t *)&in.s_addr)[2]; out->s6_addr[15] = ((uint8_t *)&in.s_addr)[3]; } +#else + *out = in; +#endif } void enet_inaddr_map6to4(const struct in6_addr *in, struct in_addr *out) { +#if ENET_IPV6 memset(out, 0, sizeof(struct in_addr)); ((uint8_t *)&out->s_addr)[0] = in->s6_addr[12]; ((uint8_t *)&out->s_addr)[1] = in->s6_addr[13]; ((uint8_t *)&out->s_addr)[2] = in->s6_addr[14]; ((uint8_t *)&out->s_addr)[3] = in->s6_addr[15]; +#else + *out = *in; +#endif } int enet_in6addr_lookup_host(const char *name, bool nodns, struct in6_addr *out) { @@ -5060,7 +5131,9 @@ extern "C" { } return 0; - } else if (result->ai_family == AF_INET6 || (result->ai_family == AF_UNSPEC && result->ai_addrlen == sizeof(struct sockaddr_in6))) { + } +#if ENET_IPV6 + else if (result->ai_family == AF_INET6 || (result->ai_family == AF_UNSPEC && result->ai_addrlen == sizeof(struct sockaddr_in6))) { memcpy(out, &((struct sockaddr_in6*)result->ai_addr)->sin6_addr, sizeof(struct in6_addr)); if (resultList != NULL) { @@ -5069,6 +5142,7 @@ extern "C" { return 0; } +#endif } } @@ -5088,6 +5162,7 @@ extern "C" { } int enet_address_get_host_ip_new(const ENetAddress *address, char *name, size_t nameLength) { +#if ENET_IPV6 if (IN6_IS_ADDR_V4MAPPED(&address->host)) { struct in_addr buf; enet_inaddr_map6to4(&address->host, &buf); @@ -5101,6 +5176,11 @@ extern "C" { return -1; } } +#else + if (inet_ntop(AF_INET, &address->host, name, nameLength) == NULL) { + return -1; + } +#endif return 0; } /* enet_address_get_host_ip_new */ @@ -5112,10 +5192,12 @@ extern "C" { int err; - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16 (address->port); sin.sin6_addr = address->host; +#if ENET_IPV6 sin.sin6_scope_id = address->sin6_scope_id; +#endif err = getnameinfo((struct sockaddr *) &sin, sizeof(sin), name, nameLength, NULL, 0, NI_NAMEREQD); if (!err) { @@ -5354,15 +5436,20 @@ extern "C" { if (result->ai_family == AF_INET) { struct sockaddr_in * sin = (struct sockaddr_in *) result->ai_addr; +#if ENET_IPV6 ((uint32_t *)&address->host.s6_addr)[0] = 0; ((uint32_t *)&address->host.s6_addr)[1] = 0; ((uint32_t *)&address->host.s6_addr)[2] = htonl(0xffff); ((uint32_t *)&address->host.s6_addr)[3] = sin->sin_addr.s_addr; +#else + address->host.s_addr = sin->sin_addr.s_addr; +#endif freeaddrinfo(resultList); return 0; } +#if ENET_IPV6 else if(result->ai_family == AF_INET6) { struct sockaddr_in6 * sin = (struct sockaddr_in6 *)result->ai_addr; @@ -5373,6 +5460,7 @@ extern "C" { return 0; } +#endif } } @@ -5396,10 +5484,12 @@ extern "C" { struct sockaddr_in6 sin; int err; memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16 (address->port); sin.sin6_addr = address->host; +#if ENET_IPV6 sin.sin6_scope_id = address->sin6_scope_id; +#endif err = getnameinfo((struct sockaddr *) &sin, sizeof(sin), name, nameLength, NULL, 0, NI_NAMEREQD); if (!err) { if (name != NULL && nameLength > 0 && !memchr(name, '\0', nameLength)) { @@ -5416,16 +5506,20 @@ extern "C" { int enet_socket_bind(ENetSocket socket, const ENetAddress *address) { struct sockaddr_in6 sin; memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; if (address != NULL) { sin.sin6_port = ENET_HOST_TO_NET_16(address->port); sin.sin6_addr = address->host; +#if ENET_IPV6 sin.sin6_scope_id = address->sin6_scope_id; +#endif } else { sin.sin6_port = 0; sin.sin6_addr = ENET_HOST_ANY; +#if ENET_IPV6 sin.sin6_scope_id = 0; +#endif } return bind(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in6)); @@ -5441,7 +5535,9 @@ extern "C" { address->host = sin.sin6_addr; address->port = ENET_NET_TO_HOST_16(sin.sin6_port); +#if ENET_IPV6 address->sin6_scope_id = sin.sin6_scope_id; +#endif return 0; } @@ -5498,9 +5594,11 @@ extern "C" { result = setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char *)&value, sizeof(int)); break; +#if ENET_IPV6 case ENET_SOCKOPT_IPV6_V6ONLY: result = setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&value, sizeof(int)); break; +#endif default: break; @@ -5530,10 +5628,12 @@ extern "C" { memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16(address->port); sin.sin6_addr = address->host; +#if ENET_IPV6 sin.sin6_scope_id = address->sin6_scope_id; +#endif result = connect(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in6)); if (result == -1 && errno == EINPROGRESS) { @@ -5557,7 +5657,9 @@ extern "C" { if (address != NULL) { address->host = sin.sin6_addr; address->port = ENET_NET_TO_HOST_16 (sin.sin6_port); +#if ENET_IPV6 address->sin6_scope_id = sin.sin6_scope_id; +#endif } return result; @@ -5583,10 +5685,12 @@ extern "C" { if (address != NULL) { memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16(address->port); sin.sin6_addr = address->host; +#if ENET_IPV6 sin.sin6_scope_id = address->sin6_scope_id; +#endif msgHdr.msg_name = &sin; msgHdr.msg_namelen = sizeof(struct sockaddr_in6); @@ -5640,7 +5744,9 @@ extern "C" { if (address != NULL) { address->host = sin.sin6_addr; address->port = ENET_NET_TO_HOST_16(sin.sin6_port); +#if ENET_IPV6 address->sin6_scope_id = sin.sin6_scope_id; +#endif } return recvLength; @@ -5773,7 +5879,7 @@ extern "C" { hostEntry = gethostbyname(name); if (hostEntry == NULL || hostEntry->h_addrtype != AF_INET) { - if (!inet_pton(AF_INET6, name, &address->host)) { + if (!inet_pton(ENET_IPV_FAMILY, name, &address->host)) { return -1; } @@ -5789,7 +5895,7 @@ extern "C" { } int enet_address_get_host_ip_old(const ENetAddress *address, char *name, size_t nameLength) { - if (inet_ntop(AF_INET6, (PVOID)&address->host, name, nameLength) == NULL) { + if (inet_ntop(ENET_IPV_FAMILY, (PVOID)&address->host, name, nameLength) == NULL) { return -1; } @@ -5800,7 +5906,7 @@ extern "C" { struct in6_addr in; struct hostent *hostEntry = NULL; in = address->host; - hostEntry = gethostbyaddr((char *)&in, sizeof(struct in6_addr), AF_INET6); + hostEntry = gethostbyaddr((char *)&in, sizeof(struct in6_addr), ENET_IPV_FAMILY); if (hostEntry == NULL) { return enet_address_get_host_ip(address, name, nameLength); } else { @@ -5816,7 +5922,7 @@ extern "C" { int enet_socket_bind(ENetSocket socket, const ENetAddress *address) { struct sockaddr_in6 sin; memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; if (address != NULL) { sin.sin6_port = ENET_HOST_TO_NET_16 (address->port); @@ -5923,7 +6029,7 @@ extern "C" { memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16(address->port); sin.sin6_addr = address->host; sin.sin6_scope_id = address->sin6_scope_id; @@ -5973,7 +6079,7 @@ extern "C" { if (address != NULL) { memset(&sin, 0, sizeof(struct sockaddr_in6)); - sin.sin6_family = AF_INET6; + sin.sin6_family = ENET_IPV_FAMILY; sin.sin6_port = ENET_HOST_TO_NET_16(address->port); sin.sin6_addr = address->host; sin.sin6_scope_id = address->sin6_scope_id;