summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-06-13 21:05:15 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-06-15 06:13:00 +0200
commit667301e3ecc39ecd6bce286fafe25247d774642f (patch)
tree1ce0a0f24272e5ac8ff7cf0ce58b4d0c07e18034
parenta91e29b656d91b1f1c6a906ee8e81ddd716723aa (diff)
downloadopenttd-667301e3ecc39ecd6bce286fafe25247d774642f.tar.xz
Codechange: [Network] Make hostname/client IP return strings instead of a C-string
-rw-r--r--src/network/core/address.cpp4
-rw-r--r--src/network/core/address.h2
-rw-r--r--src/network/core/tcp_connect.cpp2
-rw-r--r--src/network/network_server.cpp4
-rw-r--r--src/network/network_server.h2
5 files changed, 7 insertions, 7 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index ba9ae69fc..0c16a2c3c 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -19,7 +19,7 @@
* IPv4 dotted representation is given.
* @return the hostname
*/
-const char *NetworkAddress::GetHostname()
+const std::string &NetworkAddress::GetHostname()
{
if (this->hostname.empty() && this->address.ss_family != AF_UNSPEC) {
assert(this->address_length != 0);
@@ -27,7 +27,7 @@ const char *NetworkAddress::GetHostname()
getnameinfo((struct sockaddr *)&this->address, this->address_length, buffer, sizeof(buffer), nullptr, 0, NI_NUMERICHOST);
this->hostname = buffer;
}
- return this->hostname.c_str();
+ return this->hostname;
}
/**
diff --git a/src/network/core/address.h b/src/network/core/address.h
index 46d0acef1..af10a2756 100644
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -88,7 +88,7 @@ public:
this->SetPort(port);
}
- const char *GetHostname();
+ const std::string &GetHostname();
std::string GetAddressAsString(bool with_family = true);
const sockaddr_storage *GetAddress();
diff --git a/src/network/core/tcp_connect.cpp b/src/network/core/tcp_connect.cpp
index 5538416f6..f251f0288 100644
--- a/src/network/core/tcp_connect.cpp
+++ b/src/network/core/tcp_connect.cpp
@@ -183,7 +183,7 @@ void TCPConnecter::Resolve()
auto start = std::chrono::steady_clock::now();
addrinfo *ai;
- int error = getaddrinfo(address.GetHostname(), port_name, &hints, &ai);
+ int error = getaddrinfo(address.GetHostname().c_str(), port_name, &hints, &ai);
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::seconds>(end - start);
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index 81d4500a2..9868c905e 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -1924,7 +1924,7 @@ void NetworkServerDailyLoop()
* Get the IP address/hostname of the connected client.
* @return The IP address.
*/
-const char *ServerNetworkGameSocketHandler::GetClientIP()
+const std::string &ServerNetworkGameSocketHandler::GetClientIP()
{
return this->client_address.GetHostname();
}
@@ -2094,7 +2094,7 @@ uint NetworkServerKickOrBanIP(const std::string &ip, bool ban, const std::string
for (NetworkClientSocket *cs : NetworkClientSocket::Iterate()) {
if (cs->client_id == CLIENT_ID_SERVER) continue;
if (cs->client_id == _redirect_console_to_client) continue;
- if (cs->client_address.IsInNetmask(ip.c_str())) {
+ if (cs->client_address.IsInNetmask(ip)) {
NetworkServerKickClient(cs->client_id, reason);
n++;
}
diff --git a/src/network/network_server.h b/src/network/network_server.h
index 7a3fa3eac..fe15e2a54 100644
--- a/src/network/network_server.h
+++ b/src/network/network_server.h
@@ -115,7 +115,7 @@ public:
return "server";
}
- const char *GetClientIP();
+ const std::string &GetClientIP();
static ServerNetworkGameSocketHandler *GetByClientID(ClientID client_id);
};