summaryrefslogtreecommitdiff
path: root/src/network/core/address.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-04-07 19:04:37 +0000
committerrubidium <rubidium@openttd.org>2009-04-07 19:04:37 +0000
commitfdddfd6269ec4f5c770c288f05927ab8b0a60142 (patch)
treeb25fdd92d40db23425b5054e4630acdadcdd27c4 /src/network/core/address.cpp
parent22d93068890b758ac7d54256fdc24e0c3169504e (diff)
downloadopenttd-fdddfd6269ec4f5c770c288f05927ab8b0a60142.tar.xz
(svn r15968) -Codechange: do not allocate a buffer for NetworkAddresses so passing it around is easier.
Diffstat (limited to 'src/network/core/address.cpp')
-rw-r--r--src/network/core/address.cpp21
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)));