make use of getnameinfo() optional (default: true)

This commit is contained in:
UdjinM6 2016-06-02 01:31:39 +03:00
parent 9b9d105c23
commit 51696ac512
2 changed files with 19 additions and 16 deletions

View File

@ -867,17 +867,20 @@ enum Network CNetAddr::GetNetwork() const
return NET_IPV6;
}
std::string CNetAddr::ToStringIP() const
std::string CNetAddr::ToStringIP(bool fUseGetnameinfo) const
{
if (IsTor())
return EncodeBase32(&ip[6], 10) + ".onion";
CService serv(*this, 0);
struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr);
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
char name[1025] = "";
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
return std::string(name);
if (fUseGetnameinfo)
{
CService serv(*this, 0);
struct sockaddr_storage sockaddr;
socklen_t socklen = sizeof(sockaddr);
if (serv.GetSockAddr((struct sockaddr*)&sockaddr, &socklen)) {
char name[1025] = "";
if (!getnameinfo((const struct sockaddr*)&sockaddr, socklen, name, sizeof(name), NULL, 0, NI_NUMERICHOST))
return std::string(name);
}
}
if (IsIPv4())
return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
@ -1214,18 +1217,18 @@ std::string CService::ToStringPort() const
return strprintf("%u", port);
}
std::string CService::ToStringIPPort() const
std::string CService::ToStringIPPort(bool fUseGetnameinfo) const
{
if (IsIPv4() || IsTor()) {
return ToStringIP() + ":" + ToStringPort();
return ToStringIP(fUseGetnameinfo) + ":" + ToStringPort();
} else {
return "[" + ToStringIP() + "]:" + ToStringPort();
return "[" + ToStringIP(fUseGetnameinfo) + "]:" + ToStringPort();
}
}
std::string CService::ToString() const
std::string CService::ToString(bool fUseGetnameinfo) const
{
return ToStringIPPort();
return ToStringIPPort(fUseGetnameinfo);
}
void CService::SetPort(unsigned short portIn)

View File

@ -82,7 +82,7 @@ class CNetAddr
bool IsMulticast() const;
enum Network GetNetwork() const;
std::string ToString() const;
std::string ToStringIP() const;
std::string ToStringIP(bool fUseGetnameinfo = true) const;
unsigned int GetByte(int n) const;
uint64_t GetHash() const;
bool GetInAddr(struct in_addr* pipv4Addr) const;
@ -166,9 +166,9 @@ class CService : public CNetAddr
friend bool operator!=(const CService& a, const CService& b);
friend bool operator<(const CService& a, const CService& b);
std::vector<unsigned char> GetKey() const;
std::string ToString() const;
std::string ToString(bool fUseGetnameinfo = true) const;
std::string ToStringPort() const;
std::string ToStringIPPort() const;
std::string ToStringIPPort(bool fUseGetnameinfo = true) const;
CService(const struct in6_addr& ipv6Addr, unsigned short port);
CService(const struct sockaddr_in6& addr);