Fix android crash for inet_ntop
This commit is contained in:
parent
055ed23e10
commit
3d0ef44014
@ -26,7 +26,6 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
WINSOCK_API_LINKAGE PCSTR WSAAPI inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize);
|
||||
WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
|
||||
}
|
||||
|
||||
@ -48,6 +47,116 @@ extern "C"
|
||||
|
||||
#include <string>
|
||||
|
||||
// ============================================================================
|
||||
// Android STK seems to crash when using inet_ntop so we copy it from linux
|
||||
#define NS_IN6ADDRSZ 16
|
||||
#define NS_INT16SZ 2
|
||||
static const char *
|
||||
stk_inet_ntop4(const u_char *src, char *dst, socklen_t size)
|
||||
{
|
||||
static const char fmt[] = "%u.%u.%u.%u";
|
||||
char tmp[sizeof "255.255.255.255"];
|
||||
|
||||
if (sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= (int)size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return strcpy(dst, tmp);
|
||||
}
|
||||
|
||||
static const char *
|
||||
stk_inet_ntop6(const uint8_t *src, char *dst, socklen_t size)
|
||||
{
|
||||
/*
|
||||
* Note that int32_t and int16_t need only be "at least" large enough
|
||||
* to contain a value of the specified size. On some systems, like
|
||||
* Crays, there is no such thing as an integer variable with 16 bits.
|
||||
* Keep this in mind if you think this function should have been coded
|
||||
* to use pointer overlays. All the world's not a VAX.
|
||||
*/
|
||||
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
|
||||
struct { int base, len; } best, cur;
|
||||
uint32_t words[NS_IN6ADDRSZ / NS_INT16SZ];
|
||||
int i;
|
||||
/*
|
||||
* Preprocess:
|
||||
* Copy the input (bytewise) array into a wordwise array.
|
||||
* Find the longest run of 0x00's in src[] for :: shorthanding.
|
||||
*/
|
||||
memset(words, '\0', sizeof words);
|
||||
for (i = 0; i < NS_IN6ADDRSZ; i += 2)
|
||||
words[i / 2] = (src[i] << 8) | src[i + 1];
|
||||
best.base = -1;
|
||||
cur.base = -1;
|
||||
best.len = 0;
|
||||
cur.len = 0;
|
||||
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
|
||||
{
|
||||
if (words[i] == 0)
|
||||
{
|
||||
if (cur.base == -1)
|
||||
cur.base = i, cur.len = 1;
|
||||
else
|
||||
cur.len++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cur.base != -1)
|
||||
{
|
||||
if (best.base == -1 || cur.len > best.len)
|
||||
best = cur;
|
||||
cur.base = -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cur.base != -1)
|
||||
{
|
||||
if (best.base == -1 || cur.len > best.len)
|
||||
best = cur;
|
||||
}
|
||||
if (best.base != -1 && best.len < 2)
|
||||
best.base = -1;
|
||||
/*
|
||||
* Format the result.
|
||||
*/
|
||||
tp = tmp;
|
||||
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
|
||||
{
|
||||
/* Are we inside the best run of 0x00's? */
|
||||
if (best.base != -1 && i >= best.base && i < (best.base + best.len))
|
||||
{
|
||||
if (i == best.base)
|
||||
*tp++ = ':';
|
||||
continue;
|
||||
}
|
||||
/* Are we following an initial run of 0x00s or any real hex? */
|
||||
if (i != 0)
|
||||
*tp++ = ':';
|
||||
/* Is this address an encapsulated IPv4? */
|
||||
if (i == 6 && best.base == 0 &&
|
||||
(best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
|
||||
{
|
||||
if (!stk_inet_ntop4(src + 12, tp, sizeof tmp - (tp - tmp)))
|
||||
return (NULL);
|
||||
tp += strlen(tp);
|
||||
break;
|
||||
}
|
||||
tp += sprintf(tp, "%x", words[i]);
|
||||
}
|
||||
/* Was it a trailing run of 0x00's? */
|
||||
if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
|
||||
*tp++ = ':';
|
||||
*tp++ = '\0';
|
||||
/*
|
||||
* Check for overflow, copy, and we're done.
|
||||
*/
|
||||
if ((socklen_t)(tp - tmp) > size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return strcpy(dst, tmp);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
bool isIPv4MappedAddress(const struct sockaddr_in6* in6)
|
||||
{
|
||||
@ -73,12 +182,12 @@ bool isIPv4MappedAddress(const struct sockaddr_in6* in6)
|
||||
// ----------------------------------------------------------------------------
|
||||
std::string getIPV6ReadableFromIn6(const struct sockaddr_in6* in)
|
||||
{
|
||||
std::string result;
|
||||
char ipv6[INET6_ADDRSTRLEN] = {};
|
||||
struct in6_addr ipv6_addr = in->sin6_addr;
|
||||
inet_ntop(AF_INET6, &ipv6_addr, ipv6, INET6_ADDRSTRLEN);
|
||||
result = ipv6;
|
||||
return result;
|
||||
std::string ipv6;
|
||||
ipv6.resize(INET6_ADDRSTRLEN, 0);
|
||||
stk_inet_ntop6(in->sin6_addr.s6_addr, &ipv6[0], INET6_ADDRSTRLEN);
|
||||
size_t len = strlen(ipv6.c_str());
|
||||
ipv6.resize(len);
|
||||
return ipv6;
|
||||
} // getIPV6ReadableFromIn6
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
Loading…
x
Reference in New Issue
Block a user