diff options
Diffstat (limited to 'src/network/core/address.cpp')
-rw-r--r-- | src/network/core/address.cpp | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index e61024111..0db0705d9 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -14,12 +14,9 @@ const char *NetworkAddress::GetHostname() { - if (this->hostname == NULL) { + if (StrEmpty(this->hostname)) { assert(this->address_length != 0); - - char buf[NETWORK_HOSTNAME_LENGTH] = { '\0' }; - getnameinfo((struct sockaddr *)&this->address, this->address_length, buf, sizeof(buf), NULL, 0, NI_NUMERICHOST); - this->hostname = strdup(buf); + getnameinfo((struct sockaddr *)&this->address, this->address_length, this->hostname, sizeof(this->hostname), NULL, 0, NI_NUMERICHOST); } return this->hostname; } @@ -59,9 +56,15 @@ void NetworkAddress::SetPort(uint16 port) const char *NetworkAddress::GetAddressAsString() { /* 6 = for the : and 5 for the decimal port number */ - static char buf[NETWORK_HOSTNAME_LENGTH + 6]; + static char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7]; - seprintf(buf, lastof(buf), "%s:%d", this->GetHostname(), this->GetPort()); + char family; + switch (this->address.ss_family) { + case AF_INET: family = '4'; break; + case AF_INET6: family = '6'; break; + default: family = '?'; break; + } + seprintf(buf, lastof(buf), "%s:%d (IPv%c)", this->GetHostname(), this->GetPort(), family); return buf; } @@ -156,6 +159,10 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, LoopProc fun char port_name[6]; seprintf(port_name, lastof(port_name), "%u", this->GetPort()); + if (this->address_length == 0 && StrEmpty(this->hostname)) { + strecpy(this->hostname, this->address.ss_family == AF_INET ? "0.0.0.0" : "::", lastof(this->hostname)); + } + int e = getaddrinfo(this->GetHostname(), port_name, &hints, &ai); if (e != 0) { DEBUG(net, 0, "getaddrinfo(%s, %s) failed: %s", this->GetHostname(), port_name, FS2OTTD(gai_strerror(e))); |