mirror of https://github.com/OpenTTD/OpenTTD
Codechange: [Network] Simplify formatting of network addresses to string
parent
9e32c618f9
commit
36705f1dc0
|
@ -71,26 +71,17 @@ void NetworkAddress::SetPort(uint16 port)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the address as a string, e.g. 127.0.0.1:12345.
|
* Helper to get the formatting string of an address for a given family.
|
||||||
* @param buffer the buffer to write to
|
* @param family The family to get the address format for.
|
||||||
* @param last the last element in the buffer
|
* @param with_family Whether to add the familty to the address (e.g. IPv4).
|
||||||
* @param with_family whether to add the family (e.g. IPvX).
|
* @return The format string for the address.
|
||||||
*/
|
*/
|
||||||
void NetworkAddress::GetAddressAsString(char *buffer, const char *last, bool with_family)
|
static const char *GetAddressFormatString(uint16 family, bool with_family)
|
||||||
{
|
{
|
||||||
if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "[", last);
|
switch (family) {
|
||||||
buffer = strecpy(buffer, this->GetHostname(), last);
|
case AF_INET: return with_family ? "{}:{} (IPv4)" : "{}:{}";
|
||||||
if (this->GetAddress()->ss_family == AF_INET6) buffer = strecpy(buffer, "]", last);
|
case AF_INET6: return with_family ? "[{}]:{} (IPv6)" : "[{}]:{}";
|
||||||
buffer += seprintf(buffer, last, ":%d", this->GetPort());
|
default: return with_family ? "{}:{} (IPv?)" : "{}:{}";
|
||||||
|
|
||||||
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(buffer, last, " (IPv%c)", family);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,10 +92,7 @@ void NetworkAddress::GetAddressAsString(char *buffer, const char *last, bool wit
|
||||||
*/
|
*/
|
||||||
std::string NetworkAddress::GetAddressAsString(bool with_family)
|
std::string NetworkAddress::GetAddressAsString(bool with_family)
|
||||||
{
|
{
|
||||||
/* 7 extra are for with_family, which adds " (IPvX)". */
|
return fmt::format(GetAddressFormatString(this->GetAddress()->ss_family, with_family), this->GetHostname(), this->GetPort());
|
||||||
char buf[NETWORK_HOSTNAME_PORT_LENGTH + 7];
|
|
||||||
this->GetAddressAsString(buf, lastof(buf), with_family);
|
|
||||||
return buf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -89,7 +89,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *GetHostname();
|
const char *GetHostname();
|
||||||
void GetAddressAsString(char *buffer, const char *last, bool with_family = true);
|
|
||||||
std::string GetAddressAsString(bool with_family = true);
|
std::string GetAddressAsString(bool with_family = true);
|
||||||
const sockaddr_storage *GetAddress();
|
const sockaddr_storage *GetAddress();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue