use inet_ntop and inet_pton musl implementations on all platforms

This commit is contained in:
Benjamin Sergeant 2021-03-23 20:53:19 -07:00
parent 449c5fa138
commit 24c2eae3d7
4 changed files with 148 additions and 138 deletions

View File

@ -124,14 +124,14 @@ namespace ix
#endif
}
} // namespace ix
//
// mingw does not have inet_ntop and inet_pton, which were taken as is from the musl C library.
//
#if defined(_WIN32) && defined(__GNUC__)
const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
{
//
// mingw does not have inet_ntop, which were taken as is from the musl C library.
//
const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
{
// wrapper enabled on all platforms for now
// #if defined(_WIN32) && defined(__GNUC__)
#if 1
const unsigned char* a = (const unsigned char*) a0;
int i, j, max, best;
char buf[100];
@ -190,18 +190,27 @@ const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
}
errno = ENOSPC;
return 0;
}
#else
return ::inet_ntop(af, a0, s, l);
#endif
}
static int hexval(unsigned c)
{
static int hexval(unsigned c)
{
if (c - '0' < 10) return c - '0';
c |= 32;
if (c - 'a' < 6) return c - 'a' + 10;
return -1;
}
}
int inet_pton(int af, const char* s, void* a0)
{
//
// mingw does not have inet_pton, which were taken as is from the musl C library.
//
int inet_pton(int af, const char* s, void* a0)
{
// wrapper enabled on all platforms for now
// #if defined(_WIN32) && defined(__GNUC__)
#if 1
uint16_t ip[8];
unsigned char* a = (unsigned char*) a0;
int i, j, v, d, brk = -1, need_v4 = 0;
@ -266,5 +275,9 @@ int inet_pton(int af, const char* s, void* a0)
}
if (need_v4 && inet_pton(AF_INET, (const char*) s, a - 4) <= 0) return 0;
return 1;
}
#endif // defined(_WIN32) && defined(__GNUC__)
#else
return ::inet_pton(af, s, a0);
#endif
}
} // namespace ix

View File

@ -53,12 +53,6 @@ struct pollfd
#include <unistd.h>
#endif
// mingw does not have those
#if defined(_WIN32) && defined(__GNUC__)
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
namespace ix
{
#ifdef _WIN32
@ -71,4 +65,7 @@ namespace ix
bool uninitNetSystem();
int poll(struct pollfd* fds, nfds_t nfds, int timeout);
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
int inet_pton(int af, const char* src, void* dst);
} // namespace ix

View File

@ -104,7 +104,7 @@ namespace ix
server.sin_family = _addressFamily;
server.sin_port = htons(_port);
if (inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
if (ix::inet_pton(_addressFamily, _host.c_str(), &server.sin_addr.s_addr) <= 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton "
@ -133,7 +133,7 @@ namespace ix
server.sin6_family = _addressFamily;
server.sin6_port = htons(_port);
if (inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
if (ix::inet_pton(_addressFamily, _host.c_str(), &server.sin6_addr) <= 0)
{
std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton "
@ -338,7 +338,7 @@ namespace ix
if (_addressFamily == AF_INET)
{
char remoteIp4[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
if (ix::inet_ntop(AF_INET, &client.sin_addr, remoteIp4, INET_ADDRSTRLEN) == nullptr)
{
int err = Socket::getErrno();
std::stringstream ss;
@ -357,7 +357,7 @@ namespace ix
else // AF_INET6
{
char remoteIp6[INET6_ADDRSTRLEN];
if (inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) == nullptr)
if (ix::inet_ntop(AF_INET6, &client.sin_addr, remoteIp6, INET6_ADDRSTRLEN) == nullptr)
{
int err = Socket::getErrno();
std::stringstream ss;

View File

@ -906,7 +906,7 @@ namespace ix
// code which display correct results
char str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
ix::inet_ntop(AF_INET, &addr, str, INET_ADDRSTRLEN);
spdlog::info("host: {} ip: {}", hostname, str);