Skip to content

Commit 0c5166a

Browse files
committed
Add getaddrinfo_compat for workaround of an iOS 9 bug
1 parent 285e4e7 commit 0c5166a

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

src/network/unix_ipv6.cpp

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,72 @@
1515
// along with this program; if not, write to the Free Software
1616
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

18+
#ifdef WIN32
19+
#include "ws2tcpip.h"
20+
#else
21+
#include <arpa/inet.h>
22+
#include <err.h>
23+
#include <netdb.h>
24+
#include <sys/socket.h>
25+
#include <sys/sysctl.h>
26+
#include <stdlib.h>
27+
#endif
28+
29+
// ----------------------------------------------------------------------------
30+
/** Workaround of a bug in iOS 9 where port number is not written. */
31+
extern "C" int getaddrinfo_compat(const char* hostname,
32+
const char* servname,
33+
const struct addrinfo* hints,
34+
struct addrinfo** res)
35+
{
36+
#ifdef IOS_STK
37+
int err;
38+
int numericPort;
39+
40+
// If we're given a service name and it's a numeric string,
41+
// set `numericPort` to that, otherwise it ends up as 0.
42+
numericPort = servname != NULL ? atoi(servname) : 0;
43+
44+
// Call `getaddrinfo` with our input parameters.
45+
err = getaddrinfo(hostname, servname, hints, res);
46+
47+
// Post-process the results of `getaddrinfo` to work around
48+
if ((err == 0) && (numericPort != 0))
49+
{
50+
for (const struct addrinfo* addr = *res; addr != NULL;
51+
addr = addr->ai_next)
52+
{
53+
in_port_t* portPtr;
54+
switch (addr->ai_family)
55+
{
56+
case AF_INET:
57+
{
58+
portPtr = &((struct sockaddr_in*)addr->ai_addr)->sin_port;
59+
}
60+
break;
61+
case AF_INET6:
62+
{
63+
portPtr = &((struct sockaddr_in6*)addr->ai_addr)->sin6_port;
64+
}
65+
break;
66+
default:
67+
{
68+
portPtr = NULL;
69+
}
70+
break;
71+
}
72+
if ((portPtr != NULL) && (*portPtr == 0))
73+
{
74+
*portPtr = htons(numericPort);
75+
}
76+
}
77+
}
78+
return err;
79+
#else
80+
return getaddrinfo(hostname, servname, hints, res);
81+
#endif
82+
}
83+
1884
#ifndef ENABLE_IPV6
1985
#include "network/unix_ipv6.hpp"
2086
// ----------------------------------------------------------------------------
@@ -37,13 +103,6 @@ std::string getIPV6ReadableFromMappedAddress(const ENetAddress* ea)
37103
#include "utils/log.hpp"
38104
#include "utils/types.hpp"
39105

40-
#include <arpa/inet.h>
41-
#include <err.h>
42-
#include <netdb.h>
43-
#include <stdlib.h>
44-
#include <sys/socket.h>
45-
#include <sys/sysctl.h>
46-
47106
#include <algorithm>
48107
#include <utility>
49108
#include <vector>

src/network/unix_ipv6.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ void unixInitialize();
2727
void removeMappedAddress(const ENetAddress* ea);
2828
void getIPV6FromMappedAddress(const ENetAddress* ea, struct sockaddr_in6* in6);
2929
void getMappedFromIPV6(const struct sockaddr_in6* in6, ENetAddress* ea);
30+
void addMappedAddress(const ENetAddress* ea, const struct sockaddr_in6* in6);
31+
int getaddrinfo_compat(const char* hostname, const char* servname,
32+
const struct addrinfo* hints, struct addrinfo** res);
3033
#ifdef __cplusplus
3134
}
3235
#endif
3336
std::string getIPV6ReadableFromMappedAddress(const ENetAddress* ea);
37+
std::string getIPV6ReadableFromIn6(const struct sockaddr_in6* in);

0 commit comments

Comments
 (0)