use getaddrinfo() AI_PASSIVE flag, to remove "::" and "0.0.0.0"

This commit is contained in:
SASANO Takayoshi
2020-04-05 07:05:33 +09:00
parent 15b5011ebf
commit 88bbb0cd0f
2 changed files with 22 additions and 25 deletions

View File

@@ -61,15 +61,24 @@ CUDPSocket::~CUDPSocket()
} }
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length) int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length)
{
struct addrinfo hints;
::memset(&hints, 0, sizeof(hints));
return lookup(hostname, port, addr, address_length, hints);
}
int CUDPSocket::lookup(const std::string& hostname, unsigned int port, sockaddr_storage &addr, unsigned int &address_length, struct addrinfo &hints)
{ {
int err; int err;
std::string portstr = std::to_string(port); std::string portstr = std::to_string(port);
struct addrinfo hints, *res; struct addrinfo *res;
::memset(&hints, 0, sizeof(struct addrinfo)); /* port is always digits, no needs to lookup service */
hints.ai_flags = AI_NUMERICSERV; hints.ai_flags |= AI_NUMERICSERV;
err = getaddrinfo(hostname.c_str(), portstr.c_str(), &hints, &res); err = getaddrinfo(hostname.empty() ? NULL : hostname.c_str(), portstr.c_str(), &hints, &res);
if (err) { if (err) {
sockaddr_in *paddr = (sockaddr_in *)&addr; sockaddr_in *paddr = (sockaddr_in *)&addr;
::memset(paddr, 0, address_length = sizeof(sockaddr_in)); ::memset(paddr, 0, address_length = sizeof(sockaddr_in));
@@ -117,37 +126,24 @@ bool CUDPSocket::isnone(const sockaddr_storage &addr)
(in->sin_addr.s_addr == htonl(INADDR_NONE)) ); (in->sin_addr.s_addr == htonl(INADDR_NONE)) );
} }
bool CUDPSocket::open(const unsigned int af) bool CUDPSocket::open()
{ {
switch (af) { return open(AF_UNSPEC);
case AF_INET6:
m_address = "::";
break;
case AF_INET:
m_address = "0.0.0.0";
break;
default:
LogWarning("unknown address family - %d", af);
break;
}
return open();
} }
bool CUDPSocket::open() bool CUDPSocket::open(const unsigned int af)
{ {
int err; int err;
sockaddr_storage addr; sockaddr_storage addr;
unsigned int addrlen; unsigned int addrlen;
struct addrinfo hints;
/* m_address should be defined */ ::memset(&hints, 0, sizeof(hints));
if (m_address.empty()) { hints.ai_flags = AI_PASSIVE;
LogError("The local address is undefined"); hints.ai_family = af;
return false;
}
/* to determine protocol family, call lookup() first. */ /* to determine protocol family, call lookup() first. */
err = lookup(m_address.c_str(), m_port, addr, addrlen); err = lookup(m_address, m_port, addr, addrlen, hints);
if (err) { if (err) {
LogError("The local address is invalid - %s", m_address.c_str()); LogError("The local address is invalid - %s", m_address.c_str());
return false; return false;

View File

@@ -50,6 +50,7 @@ public:
void close(); void close();
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length); static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length);
static int lookup(const std::string& hostName, unsigned int port, sockaddr_storage &address, unsigned int &address_length, struct addrinfo &hints);
static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2); static bool match(const sockaddr_storage &addr1, const sockaddr_storage &addr2);
static bool isnone(const sockaddr_storage &addr); static bool isnone(const sockaddr_storage &addr);