|
|
/* $Id$ */
|
|
|
|
|
|
/** @file core/address.cpp Implementation of the address. */
|
|
|
|
|
|
#include "../../stdafx.h"
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
|
#include "address.h"
|
|
|
#include "config.h"
|
|
|
#include "host.h"
|
|
|
#include "../../string_func.h"
|
|
|
#include "../../debug.h"
|
|
|
|
|
|
const char *NetworkAddress::GetHostname()
|
|
|
{
|
|
|
if (StrEmpty(this->hostname)) {
|
|
|
assert(this->address_length != 0);
|
|
|
char *buf = this->hostname;
|
|
|
if (this->address.ss_family == AF_INET6) buf = strecpy(buf, "[", lastof(this->hostname));
|
|
|
getnameinfo((struct sockaddr *)&this->address, this->address_length, buf, lastof(this->hostname) - buf, NULL, 0, NI_NUMERICHOST);
|
|
|
if (this->address.ss_family == AF_INET6) strecat(buf, "]", lastof(this->hostname));
|
|
|
getnameinfo((struct sockaddr *)&this->address, this->address_length, this->hostname, sizeof(this->hostname), NULL, 0, NI_NUMERICHOST);
|
|
|
}
|
|
|
return this->hostname;
|
|
|
}
|
|
|
|
|
|
uint16 NetworkAddress::GetPort() const
|
|
|
{
|
|
|
switch (this->address.ss_family) {
|
|
|
case AF_UNSPEC:
|
|
|
case AF_INET:
|
|
|
return ntohs(((struct sockaddr_in *)&this->address)->sin_port);
|
|
|
|
|
|
case AF_INET6:
|
|
|
return ntohs(((struct sockaddr_in6 *)&this->address)->sin6_port);
|
|
|
|
|
|
default:
|
|
|
NOT_REACHED();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void NetworkAddress::SetPort(uint16 port)
|
|
|
{
|
|
|
switch (this->address.ss_family) {
|
|
|
case AF_UNSPEC:
|
|
|
case AF_INET:
|
|
|
((struct sockaddr_in*)&this->address)->sin_port = htons(port);
|
|
|
break;
|
|
|
|
|
|
case AF_INET6:
|
|
|
((struct sockaddr_in6*)&this->address)->sin6_port = htons(port);
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
NOT_REACHED();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
const char *NetworkAddress::GetAddressAsString(bool with_family)
|
|
|
{
|
|
|
/* 6 = for the : and 5 for the decimal port number */
|
|
|
static char buf[NETWORK_HOSTNAME_LENGTH + 6 + 7];
|
|
|
char *p = buf;
|
|
|
if (this->address.ss_family == AF_INET6) p = strecpy(p, "[", lastof(buf));
|
|
|
p = strecpy(p, this->GetHostname(), lastof(buf));
|
|
|
if (this->address.ss_family == AF_INET6) p = strecpy(p, "]", lastof(buf));
|
|
|
p += seprintf(p, lastof(buf), ":%d", this->GetPort());
|
|
|
|
|
|
if (with_family) {
|
|
|
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);
|
|
|
} else {
|
|
|
seprintf(buf, lastof(buf), "%s:%d", this->GetHostname(), this->GetPort());
|
|
|
seprintf(p, lastof(buf), " (IPv%c)", family);
|
|
|
}
|
|
|
return buf;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Helper function to resolve without opening a socket.
|
|
|
* @param runp information about the socket to try not
|
|
|
* @return the opened socket or INVALID_SOCKET
|
|
|
*/
|
|
|
static SOCKET ResolveLoopProc(addrinfo *runp)
|
|
|
{
|
|
|
/* We just want the first 'entry', so return a valid socket. */
|
|
|
return !INVALID_SOCKET;
|
|
|
}
|
|
|
|
|
|
const sockaddr_storage *NetworkAddress::GetAddress()
|
|
|
{
|
|
|
if (!this->IsResolved()) {
|
|
|
/* Here we try to resolve a network address. We use SOCK_STREAM as
|
|
|
* socket type because some stupid OSes, like Solaris, cannot be
|
|
|
* bothered to implement the specifications and allow '0' as value
|
|
|
* that means "don't care whether it is SOCK_STREAM or SOCK_DGRAM".
|
|
|
*/
|
|
|
this->Resolve(this->address.ss_family, SOCK_STREAM, AI_ADDRCONFIG, NULL, ResolveLoopProc);
|