summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium42 <rubidium@openttd.org>2021-06-13 21:29:40 +0200
committerrubidium42 <rubidium42@users.noreply.github.com>2021-06-15 06:13:00 +0200
commita91e29b656d91b1f1c6a906ee8e81ddd716723aa (patch)
tree88b58e7616a1e79c63993f1e9b2a3454946dd9da
parent36705f1dc0d7c5436780d8915c147d9f55eaa191 (diff)
downloadopenttd-a91e29b656d91b1f1c6a906ee8e81ddd716723aa.tar.xz
Codechange: [Network] Let IsInNetmask use std::string
-rw-r--r--src/network/core/address.cpp11
-rw-r--r--src/network/core/address.h2
-rw-r--r--src/network/core/tcp_listen.h2
3 files changed, 7 insertions, 8 deletions
diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp
index 478dc1b2c..ba9ae69fc 100644
--- a/src/network/core/address.cpp
+++ b/src/network/core/address.cpp
@@ -143,7 +143,7 @@ bool NetworkAddress::IsFamily(int family)
* @note netmask without /n assumes all bits need to match.
* @return true if this IP is within the netmask.
*/
-bool NetworkAddress::IsInNetmask(const char *netmask)
+bool NetworkAddress::IsInNetmask(const std::string &netmask)
{
/* Resolve it if we didn't do it already */
if (!this->IsResolved()) this->GetAddress();
@@ -153,16 +153,15 @@ bool NetworkAddress::IsInNetmask(const char *netmask)
NetworkAddress mask_address;
/* Check for CIDR separator */
- const char *chr_cidr = strchr(netmask, '/');
- if (chr_cidr != nullptr) {
- int tmp_cidr = atoi(chr_cidr + 1);
+ auto cidr_separator_location = netmask.find('/');
+ if (cidr_separator_location != std::string::npos) {
+ int tmp_cidr = atoi(netmask.substr(cidr_separator_location + 1).c_str());
/* Invalid CIDR, treat as single host */
if (tmp_cidr > 0 && tmp_cidr < cidr) cidr = tmp_cidr;
/* Remove the / so that NetworkAddress works on the IP portion */
- std::string ip_str(netmask, chr_cidr - netmask);
- mask_address = NetworkAddress(ip_str.c_str(), 0, this->address.ss_family);
+ mask_address = NetworkAddress(netmask.substr(0, cidr_separator_location), 0, this->address.ss_family);
} else {
mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
}
diff --git a/src/network/core/address.h b/src/network/core/address.h
index eac21c76a..46d0acef1 100644
--- a/src/network/core/address.h
+++ b/src/network/core/address.h
@@ -116,7 +116,7 @@ public:
}
bool IsFamily(int family);
- bool IsInNetmask(const char *netmask);
+ bool IsInNetmask(const std::string &netmask);
/**
* Compare the address of this class with the address of another.
diff --git a/src/network/core/tcp_listen.h b/src/network/core/tcp_listen.h
index 1cde4a179..03945e230 100644
--- a/src/network/core/tcp_listen.h
+++ b/src/network/core/tcp_listen.h
@@ -56,7 +56,7 @@ public:
/* Check if the client is banned */
bool banned = false;
for (const auto &entry : _network_ban_list) {
- banned = address.IsInNetmask(entry.c_str());
+ banned = address.IsInNetmask(entry);
if (banned) {
Packet p(Tban_packet);
p.PrepareToSend();