From 2ddb62319803f6a3f3246a52b4e009d9ae1daaed Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 18 Dec 2022 17:51:35 +0000 Subject: Use in_port_t instead of int/unsigned int/long. --- include/socket.h | 5 +++-- include/users.h | 2 +- src/coremods/core_who.cpp | 7 ++++--- src/listensocket.cpp | 2 +- src/modules/m_gateway.cpp | 4 ++-- src/modules/m_hostchange.cpp | 7 +++++-- src/modules/m_ircv3_sts.cpp | 8 ++++---- src/socket.cpp | 11 +++++++---- src/users.cpp | 9 ++++++--- 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 #else # include +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 ports; + insp::flat_set 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::min() + && port <= std::numeric_limits::max() + && static_cast(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(cport->second); + in_port_t port = ConvToNum(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(sport->second); + in_port_t port = ConvToNum(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 ports; + insp::flat_set 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::min() && port <= std::numeric_limits::max()) + ports.insert(static_cast(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, " must contain a hostname, at " + tag->source.str()); - unsigned int port = static_cast(tag->getUInt("port", 0, 0, UINT16_MAX)); + in_port_t port = static_cast(tag->getUInt("port", 0, 0, UINT16_MAX)); if (!HasValidSSLPort(port)) throw ModuleException(this, " 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(portrange.GetToken())); ) + while (long port = portrange.GetToken()) { + if (port <= std::numeric_limits::min() || port > std::numeric_limits::max()) + continue; + irc::sockets::sockaddrs bindspec(false); - if (!bindspec.from_ip_port(address, port)) + if (!bindspec.from_ip_port(address, static_cast(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_ptrgetString("port"), false); while (long port = portrange.GetToken()) - ports.insert(static_cast(port)); + { + if (port > std::numeric_limits::min() && port <= std::numeric_limits::max()) + ports.insert(static_cast(port)); + } commandrate = tag->getUInt("commandrate", commandrate, 1); fakelag = tag->getBool("fakelag", fakelag); -- cgit v1.3.1-10-gc9f91