(svn r15968) -Codechange: do not allocate a buffer for NetworkAddresses so passing it around is easier.

This commit is contained in:
rubidium
2009-04-07 19:04:37 +00:00
parent 22d9306889
commit fdddfd6269
2 changed files with 36 additions and 39 deletions

View File

@@ -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)));