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 #endif
} }
} // namespace ix
// //
// mingw does not have inet_ntop and inet_pton, which were taken as is from the musl C library. // mingw does not have inet_ntop, 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) 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; const unsigned char* a = (const unsigned char*) a0;
int i, j, max, best; int i, j, max, best;
char buf[100]; char buf[100];
@ -190,6 +190,9 @@ const char* inet_ntop(int af, const void* a0, char* s, socklen_t l)
} }
errno = ENOSPC; errno = ENOSPC;
return 0; return 0;
#else
return ::inet_ntop(af, a0, s, l);
#endif
} }
static int hexval(unsigned c) static int hexval(unsigned c)
@ -200,8 +203,14 @@ static int hexval(unsigned c)
return -1; return -1;
} }
//
// 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) 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]; uint16_t ip[8];
unsigned char* a = (unsigned char*) a0; unsigned char* a = (unsigned char*) a0;
int i, j, v, d, brk = -1, need_v4 = 0; 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; if (need_v4 && inet_pton(AF_INET, (const char*) s, a - 4) <= 0) return 0;
return 1; return 1;
#else
return ::inet_pton(af, s, a0);
#endif
} }
#endif // defined(_WIN32) && defined(__GNUC__)
} // namespace ix

View File

@ -53,12 +53,6 @@ struct pollfd
#include <unistd.h> #include <unistd.h>
#endif #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 namespace ix
{ {
#ifdef _WIN32 #ifdef _WIN32
@ -71,4 +65,7 @@ namespace ix
bool uninitNetSystem(); bool uninitNetSystem();
int poll(struct pollfd* fds, nfds_t nfds, int timeout); 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 } // namespace ix

View File

@ -104,7 +104,7 @@ namespace ix
server.sin_family = _addressFamily; server.sin_family = _addressFamily;
server.sin_port = htons(_port); 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; std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton " ss << "SocketServer::listen() error calling inet_pton "
@ -133,7 +133,7 @@ namespace ix
server.sin6_family = _addressFamily; server.sin6_family = _addressFamily;
server.sin6_port = htons(_port); 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; std::stringstream ss;
ss << "SocketServer::listen() error calling inet_pton " ss << "SocketServer::listen() error calling inet_pton "
@ -338,7 +338,7 @@ namespace ix
if (_addressFamily == AF_INET) if (_addressFamily == AF_INET)
{ {
char remoteIp4[INET_ADDRSTRLEN]; 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(); int err = Socket::getErrno();
std::stringstream ss; std::stringstream ss;
@ -357,7 +357,7 @@ namespace ix
else // AF_INET6 else // AF_INET6
{ {
char remoteIp6[INET6_ADDRSTRLEN]; 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(); int err = Socket::getErrno();
std::stringstream ss; std::stringstream ss;

View File

@ -906,7 +906,7 @@ namespace ix
// code which display correct results // code which display correct results
char str[INET_ADDRSTRLEN]; 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); spdlog::info("host: {} ip: {}", hostname, str);