aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Sadie Powell2022-12-18 17:51:35 +0000
committerGravatar Sadie Powell2022-12-18 18:47:28 +0000
commit2ddb62319803f6a3f3246a52b4e009d9ae1daaed (patch)
tree96a5d23c8a38f3282f3e52346cb706d55e58658f
parentWe actually want ADDRESS_FAMILY not SOCKET_ADDRESS for sa_family_t. (diff)
Use in_port_t instead of int/unsigned int/long.
-rw-r--r--include/socket.h5
-rw-r--r--include/users.h2
-rw-r--r--src/coremods/core_who.cpp7
-rw-r--r--src/listensocket.cpp2
-rw-r--r--src/modules/m_gateway.cpp4
-rw-r--r--src/modules/m_hostchange.cpp7
-rw-r--r--src/modules/m_ircv3_sts.cpp8
-rw-r--r--src/socket.cpp11
-rw-r--r--src/users.cpp9
9 files changed, 33 insertions, 22 deletions
diff --git a/include/socket.h b/include/socket.h
index 47b503a0c..f306a7706 100644
--- a/include/socket.h
+++ b/include/socket.h
@@ -35,6 +35,7 @@
# include <sys/un.h>
#else
# include <afunix.h>
+typedef USHORT in_port_t;
typedef ADDRESS_FAMILY sa_family_t;
#endif
@@ -83,7 +84,7 @@ namespace irc
* @param port A TCP port.
* @return True if the IP/port was stored in this socket address; otherwise, false.
*/
- bool from_ip_port(const std::string& addr, int port);
+ bool from_ip_port(const std::string& addr, in_port_t port);
/** Store a UNIX socket path in this socket address.
* @param path A path to a UNIX socket.
@@ -95,7 +96,7 @@ namespace irc
bool is_local() const;
/** Returns the TCP port number of the socket address or 0 if not relevant to this family. */
- int port() const;
+ in_port_t port() const;
/** Returns the size of the structure for use in networking syscalls. */
socklen_t sa_size() const;
diff --git a/include/users.h b/include/users.h
index 4150e40c7..17c8d5e2a 100644
--- a/include/users.h
+++ b/include/users.h
@@ -76,7 +76,7 @@ public:
std::string passwordhash;
/** If non-empty then the server ports which a user has to be connecting on. */
- insp::flat_set<int> ports;
+ insp::flat_set<in_port_t> ports;
/** The type of class this. */
Type type;
diff --git a/src/coremods/core_who.cpp b/src/coremods/core_who.cpp
index ac872209b..a34fc654f 100644
--- a/src/coremods/core_who.cpp
+++ b/src/coremods/core_who.cpp
@@ -352,10 +352,11 @@ bool CommandWho::MatchUser(LocalUser* source, User* user, WhoData& data)
if (source_can_see_target && lu)
{
irc::portparser portrange(data.matchtext, false);
- long port;
- while ((port = portrange.GetToken()))
+ while (long port = portrange.GetToken())
{
- if (port == lu->server_sa.port())
+ if (port >= std::numeric_limits<in_port_t>::min()
+ && port <= std::numeric_limits<in_port_t>::max()
+ && static_cast<in_port_t>(port) == lu->server_sa.port())
{
match = true;
break;
diff --git a/src/listensocket.cpp b/src/listensocket.cpp
index ac5698402..f25d233bc 100644
--- a/src/listensocket.cpp
+++ b/src/listensocket.cpp
@@ -187,7 +187,7 @@ void ListenSocket::OnEventHandlerRead()
if (!memcmp(prefix4in6, &client.in6.sin6_addr, 12))
{
// recreate as a sockaddr_in using the IPv4 IP
- uint16_t sport = client.in6.sin6_port;
+ in_port_t sport = client.in6.sin6_port;
client.in4.sin_family = AF_INET;
client.in4.sin_port = sport;
memcpy(&client.in4.sin_addr.s_addr, client.in6.sin6_addr.s6_addr + 12, sizeof(uint32_t));
diff --git a/src/modules/m_gateway.cpp b/src/modules/m_gateway.cpp
index a43e3ee9b..4e3bdc843 100644
--- a/src/modules/m_gateway.cpp
+++ b/src/modules/m_gateway.cpp
@@ -480,7 +480,7 @@ public:
if (cport != flags->end())
{
// If we can't parse the port then just give up.
- uint16_t port = ConvToNum<uint16_t>(cport->second);
+ in_port_t port = ConvToNum<in_port_t>(cport->second);
if (port)
{
switch (user->client_sa.family())
@@ -506,7 +506,7 @@ public:
if (sport != flags->end())
{
// If we can't parse the port then just give up.
- uint16_t port = ConvToNum<uint16_t>(sport->second);
+ in_port_t port = ConvToNum<in_port_t>(sport->second);
if (port)
{
switch (user->server_sa.family())
diff --git a/src/modules/m_hostchange.cpp b/src/modules/m_hostchange.cpp
index fda8e7e67..bd3e2b36b 100644
--- a/src/modules/m_hostchange.cpp
+++ b/src/modules/m_hostchange.cpp
@@ -47,7 +47,7 @@ private:
std::string host;
std::string klass;
std::string mask;
- insp::flat_set<long> ports;
+ insp::flat_set<in_port_t> ports;
std::string prefix;
std::string suffix;
@@ -62,7 +62,10 @@ private:
{
irc::portparser portrange(portlist, false);
while (long port = portrange.GetToken())
- ports.insert(port);
+ {
+ if (port > std::numeric_limits<in_port_t>::min() && port <= std::numeric_limits<in_port_t>::max())
+ ports.insert(static_cast<in_port_t>(port));
+ }
}
}
diff --git a/src/modules/m_ircv3_sts.cpp b/src/modules/m_ircv3_sts.cpp
index e199b7f34..93004f3f4 100644
--- a/src/modules/m_ircv3_sts.cpp
+++ b/src/modules/m_ircv3_sts.cpp
@@ -88,7 +88,7 @@ public:
// TODO: Send duration=0 when STS vanishes.
}
- void SetPolicy(const std::string& newhost, unsigned long duration, unsigned int port, bool preload)
+ void SetPolicy(const std::string& newhost, unsigned long duration, in_port_t port, bool preload)
{
// To enforce an STS upgrade policy, servers MUST send this key to insecurely connected clients. Servers
// MAY send this key to securely connected clients, but it will be ignored.
@@ -140,7 +140,7 @@ private:
STSCap cap;
// The IRCv3 STS specification requires that the server is listening using TLS using a valid certificate.
- static bool HasValidSSLPort(unsigned int port)
+ static bool HasValidSSLPort(in_port_t port)
{
for (const auto& ls : ServerInstance->ports)
{
@@ -149,7 +149,7 @@ private:
return true;
// Is this listener on the right port?
- unsigned int saport = ls->bind_sa.port();
+ in_port_t saport = ls->bind_sa.port();
if (saport != port)
continue;
@@ -181,7 +181,7 @@ public:
if (host.empty())
throw ModuleException(this, "<sts:host> must contain a hostname, at " + tag->source.str());
- unsigned int port = static_cast<unsigned int>(tag->getUInt("port", 0, 0, UINT16_MAX));
+ in_port_t port = static_cast<in_port_t>(tag->getUInt("port", 0, 0, UINT16_MAX));
if (!HasValidSSLPort(port))
throw ModuleException(this, "<sts:port> must be a TLS port, at " + tag->source.str());
diff --git a/src/socket.cpp b/src/socket.cpp
index 77c8d1edc..0ede146fd 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -86,10 +86,13 @@ size_t InspIRCd::BindPorts(FailedPortList& failed_ports)
address.empty() ? "*" : address.c_str(), tag->source.str().c_str());
irc::portparser portrange(portlist, false);
- for (int port; (port = static_cast<int>(portrange.GetToken())); )
+ while (long port = portrange.GetToken())
{
+ if (port <= std::numeric_limits<in_port_t>::min() || port > std::numeric_limits<in_port_t>::max())
+ continue;
+
irc::sockets::sockaddrs bindspec(false);
- if (!bindspec.from_ip_port(address, port))
+ if (!bindspec.from_ip_port(address, static_cast<in_port_t>(port)))
continue;
if (!BindPort(tag, bindspec, old_ports))
@@ -160,7 +163,7 @@ irc::sockets::sockaddrs::sockaddrs(bool initialize)
memset(this, 0, sizeof(*this));
}
-bool irc::sockets::sockaddrs::from_ip_port(const std::string& addr, int port)
+bool irc::sockets::sockaddrs::from_ip_port(const std::string& addr, in_port_t port)
{
if (addr.empty() || addr == "*")
{
@@ -237,7 +240,7 @@ bool irc::sockets::sockaddrs::is_local() const
return false;
}
-int irc::sockets::sockaddrs::port() const
+in_port_t irc::sockets::sockaddrs::port() const
{
switch (family())
{
diff --git a/src/users.cpp b/src/users.cpp
index 1e95d3310..3ab397a49 100644
--- a/src/users.cpp
+++ b/src/users.cpp
@@ -523,7 +523,7 @@ void LocalUser::FullConnect()
FOREACH_MOD(OnPostConnect, (this));
- ServerInstance->SNO.WriteToSnoMask('c', "Client connecting on port %d (class %s): %s (%s) [%s\x0F]",
+ ServerInstance->SNO.WriteToSnoMask('c', "Client connecting on port %hu (class %s): %s (%s) [%s\x0F]",
this->server_sa.port(), this->GetClass()->name.c_str(), GetFullRealHost().c_str(),
this->GetIPString().c_str(), this->GetRealName().c_str());
ServerInstance->Logs.Debug("BANCACHE", "BanCache: Adding NEGATIVE hit for " + this->GetIPString());
@@ -1091,7 +1091,7 @@ void LocalUser::SetClass(const std::string& explicit_name)
/* if it requires a port and our port doesn't match, fail */
if (!c->ports.empty() && !c->ports.count(this->server_sa.port()))
{
- ServerInstance->Logs.Debug("CONNECTCLASS", "The %s connect class is not suitable as the connection port (%d) is not any of %s",
+ ServerInstance->Logs.Debug("CONNECTCLASS", "The %s connect class is not suitable as the connection port (%hu) is not any of %s",
c->GetName().c_str(), this->server_sa.port(), stdalgo::string::join(c->ports).c_str());
continue;
}
@@ -1206,7 +1206,10 @@ void ConnectClass::Configure(const std::string& classname, std::shared_ptr<Confi
irc::portparser portrange(tag->getString("port"), false);
while (long port = portrange.GetToken())
- ports.insert(static_cast<int>(port));
+ {
+ if (port > std::numeric_limits<in_port_t>::min() && port <= std::numeric_limits<in_port_t>::max())
+ ports.insert(static_cast<in_port_t>(port));
+ }
commandrate = tag->getUInt("commandrate", commandrate, 1);
fakelag = tag->getBool("fakelag", fakelag);