summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-11-11 15:17:40 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-11-11 15:17:40 +0000
commit69c3a32784b8c638ea10c269c3f63ede86b8aaa0 (patch)
treeee065fbe0bbeaf3121ff359e2cca3671b426de08 /src
parent0b230aa96b81b7a6426e48a509a19699556cf190 (diff)
Add irc::portparser, a class to parse port ranges in the form "6660,6661,6662-6669,7000".
Needs testing, watch next few commits. git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@5690 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src')
-rw-r--r--src/hashcomp.cpp50
-rw-r--r--src/modules/extra/m_ssl_gnutls.cpp47
-rw-r--r--src/modules/extra/m_ssl_openssl.cpp47
-rw-r--r--src/modules/m_spanningtree.cpp70
-rw-r--r--src/socket.cpp59
5 files changed, 91 insertions, 182 deletions
diff --git a/src/hashcomp.cpp b/src/hashcomp.cpp
index 4b3200512..b26fd0841 100644
--- a/src/hashcomp.cpp
+++ b/src/hashcomp.cpp
@@ -386,3 +386,53 @@ std::string& irc::stringjoiner::GetJoined()
return joined;
}
+irc::portparser::portparser(const std::string &source) : in_range(0), range_begin(0), range_end(0)
+{
+ sep = new irc::commasepstream(source);
+}
+
+irc::portparser::~portparser()
+{
+ delete sep;
+}
+
+long irc::portparser::GetToken()
+{
+ if (in_range > 0)
+ {
+ in_range++;
+ if (in_range <= range_end)
+ return in_range;
+ else
+ in_range = 0;
+ }
+
+ std::string x = sep->GetToken();
+
+ if (x == "")
+ return 0;
+
+ std::string::size_type dash = x.rfind('-');
+ if (dash != std::string::npos)
+ {
+ std::string sbegin = x.substr(0, dash);
+ std::string send = x.substr(dash+1, x.length());
+ long range_begin = atoi(sbegin.c_str());
+ long range_end = atoi(send.c_str());
+
+ if ((range_begin > 0) && (range_end > 0) && (range_begin < 65536) && (range_end < 65536) && (range_begin < range_end))
+ {
+ in_range = range_begin;
+ return in_range;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else
+ {
+ return atoi(x.c_str());
+ }
+}
+
diff --git a/src/modules/extra/m_ssl_gnutls.cpp b/src/modules/extra/m_ssl_gnutls.cpp
index fafb646e1..863b503cf 100644
--- a/src/modules/extra/m_ssl_gnutls.cpp
+++ b/src/modules/extra/m_ssl_gnutls.cpp
@@ -118,51 +118,18 @@ class ModuleSSLGnuTLS : public Module
{
// Get the port we're meant to be listening on with SSL
std::string port = Conf->ReadValue("bind", "port", i);
- irc::commasepstream portrange(port);
- std::string portno = "*";
- while ((portno = portrange.GetToken()) != "")
+ irc::portparser portrange(port);
+ long portno = -1;
+ while ((portno = portrange.GetToken()))
{
- std::string::size_type dash = portno.rfind('-');
- if (dash != std::string::npos)
+ if (ServerInstance->Config->AddIOHook(portno, this))
{
- std::string sbegin = portno.substr(0, dash);
- std::string send = portno.substr(dash+1, portno.length());
- long begin = atoi(sbegin.c_str());
- long end = atoi(send.c_str());
- if ((begin < 0) || (end < 0) || (begin > 65535) || (end > 65535) || (begin >= end))
- {
- ServerInstance->Log(DEFAULT,"WARNING: Port range \"%d-%d\" discarded. begin >= end, or begin/end out of range.", begin, end);
- }
- else
- {
- for (int portval = begin; portval <= end; ++portval)
- {
- if (ServerInstance->Config->AddIOHook(portval, this))
- {
- listenports.push_back(portval);
- ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %d", portval);
- }
- else
- {
- ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?",
- portval);
- }
- }
- }
+ listenports.push_back(portno);
+ ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %d", portno);
}
else
{
- if (ServerInstance->Config->AddIOHook(atoi(portno.c_str()), this))
- {
- // We keep a record of which ports we're listening on with SSL
- listenports.push_back(atoi(portno.c_str()));
-
- ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %s", portno.c_str());
- }
- else
- {
- ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %s, maybe you have another ssl or similar module loaded?", portno.c_str());
- }
+ ServerInstance->Log(DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?", portno);
}
}
}
diff --git a/src/modules/extra/m_ssl_openssl.cpp b/src/modules/extra/m_ssl_openssl.cpp
index 2acd78d0e..d1521ad4d 100644
--- a/src/modules/extra/m_ssl_openssl.cpp
+++ b/src/modules/extra/m_ssl_openssl.cpp
@@ -143,51 +143,18 @@ class ModuleSSLOpenSSL : public Module
{
// Get the port we're meant to be listening on with SSL
std::string port = Conf->ReadValue("bind", "port", i);
- irc::commasepstream portrange(port);
- std::string portno = "*";
- while ((portno = portrange.GetToken()) != "")
+ irc::portparser portrange(port);
+ long portno = -1;
+ while ((portno = portrange.GetToken()))
{
- std::string::size_type dash = portno.rfind('-');
- if (dash != std::string::npos)
+ if (ServerInstance->Config->AddIOHook(portno, this))
{
- std::string sbegin = portno.substr(0, dash);
- std::string send = portno.substr(dash+1, portno.length());
- long begin = atoi(sbegin.c_str());
- long end = atoi(send.c_str());
- if ((begin < 0) || (end < 0) || (begin > 65535) || (end > 65535) || (begin >= end))
- {
- ServerInstance->Log(DEFAULT,"WARNING: Port range \"%d-%d\" discarded. begin >= end, or begin/end out of range.", begin, end);
- }
- else
- {
- for (int portval = begin; portval <= end; ++portval)
- {
- if (ServerInstance->Config->AddIOHook(portval, this))
- {
- listenports.push_back(portval);
- ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %d", portval);
- }
- else
- {
- ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?",
- portval);
- }
- }
- }
+ listenports.push_back(portno);
+ ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %d", portno);
}
else
{
- if (ServerInstance->Config->AddIOHook(atoi(portno.c_str()), this))
- {
- // We keep a record of which ports we're listening on with SSL
- listenports.push_back(atoi(portno.c_str()));
-
- ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: Enabling SSL for port %s", portno.c_str());
- }
- else
- {
- ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: FAILED to enable SSL on port %s, maybe you have another ssl or similar module loaded?", portno.c_str());
- }
+ ServerInstance->Log(DEFAULT, "m_ssl_openssl.so: FAILED to enable SSL on port %d, maybe you have another ssl or similar module loaded?", portno);
}
}
}
diff --git a/src/modules/m_spanningtree.cpp b/src/modules/m_spanningtree.cpp
index 7f854809b..20c81bd95 100644
--- a/src/modules/m_spanningtree.cpp
+++ b/src/modules/m_spanningtree.cpp
@@ -3874,67 +3874,27 @@ void SpanningTreeUtilities::ReadConfiguration(bool rebind)
std::string Port = Conf->ReadValue("bind","port",j);
if (Type == "servers")
{
- irc::commasepstream portrange(Port);
- std::string portno = "*";
- while ((portno = portrange.GetToken()) != "")
+ irc::portparser portrange(Port);
+ int portno = -1;
+ while ((portno = portrange.GetToken()))
{
- std::string::size_type dash = portno.rfind('-');
- if (dash != std::string::npos)
- {
- std::string sbegin = portno.substr(0, dash);
- std::string send = portno.substr(dash+1, portno.length());
- long begin = atoi(sbegin.c_str());
- long end = atoi(send.c_str());
+ ServerInstance->Log(DEBUG,"m_spanningtree: Binding server port %s:%d", IP.c_str(), portno);
+ if (IP == "*")
+ IP = "";
- if ((begin < 0) || (end < 0) || (begin > 65535) || (end > 65535) || (begin >= end))
- {
- ServerInstance->Log(DEFAULT,"WARNING: Port range \"%d-%d\" discarded. begin >= end, or begin/end out of range.", begin, end);
- }
- else
- {
- for (long port = begin; port <= end; ++port)
- {
- ServerInstance->Log(DEBUG,"m_spanningtree: Binding server port %s:%d (part of range %s)", IP.c_str(), port, portno.c_str());
- if (IP == "*")
- IP = "";
-
- TreeSocket* listener = new TreeSocket(this, ServerInstance, IP.c_str(), port, true, 10);
- if (listener->GetState() == I_LISTENING)
- {
- ServerInstance->Log(DEFAULT,"m_spanningtree: Binding server port %s:%d successful!", IP.c_str(), port);
- Bindings.push_back(listener);
- }
- else
- {
- ServerInstance->Log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %s:%d (forms part of range '%s')",IP.c_str(),
- port, portno.c_str());
- listener->Close();
- DELETE(listener);
- }
- ServerInstance->Log(DEBUG,"Done with this binding");
- }
- }
+ TreeSocket* listener = new TreeSocket(this, ServerInstance, IP.c_str(), portno, true, 10);
+ if (listener->GetState() == I_LISTENING)
+ {
+ ServerInstance->Log(DEFAULT,"m_spanningtree: Binding server port %s:%d successful!", IP.c_str(), portno);
+ Bindings.push_back(listener);
}
else
{
- ServerInstance->Log(DEBUG,"m_spanningtree: Binding server port %s:%s (single port %s)", IP.c_str(), portno.c_str(), portno.c_str());
- if (IP == "*")
- IP = "";
-
- TreeSocket* listener = new TreeSocket(this, ServerInstance, IP.c_str(),atoi(portno.c_str()),true,10);
- if (listener->GetState() == I_LISTENING)
- {
- ServerInstance->Log(DEFAULT,"m_spanningtree: Binding server port %s:%s successful!", IP.c_str(), portno.c_str());
- Bindings.push_back(listener);
- }
- else
- {
- ServerInstance->Log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %s:%s",IP.c_str(), portno.c_str());
- listener->Close();
- DELETE(listener);
- }
- ServerInstance->Log(DEBUG,"Done with this binding");
+ ServerInstance->Log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %s:%d",IP.c_str(), portno);
+ listener->Close();
+ DELETE(listener);
}
+ ServerInstance->Log(DEBUG,"Done with this binding");
}
}
}
diff --git a/src/socket.cpp b/src/socket.cpp
index 560541f4d..b68de7159 100644
--- a/src/socket.cpp
+++ b/src/socket.cpp
@@ -409,55 +409,20 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
if ((!*Type) || (!strcmp(Type,"clients")))
{
- irc::commasepstream portrange(configToken);
- std::string portno = "*";
- while ((portno = portrange.GetToken()) != "")
+ irc::portparser portrange(configToken);
+ long portno = -1;
+ while (portno = portrange.GetToken())
{
- std::string::size_type dash = portno.rfind('-');
- if (dash != std::string::npos)
+ if (!HasPort(portno, Addr))
{
- this->Log(DEBUG,"Port range, %s", portno.c_str());
- /* Range of ports */
- std::string sbegin = portno.substr(0, dash);
- std::string send = portno.substr(dash+1, portno.length());
- long begin = atoi(sbegin.c_str());
- long end = atoi(send.c_str());
- if ((begin < 0) || (end < 0) || (begin > 65535) || (end > 65535) || (begin >= end))
- {
- this->Log(DEFAULT,"WARNING: Port range \"%d-%d\" discarded. begin >= end, or begin/end out of range.", begin, end);
- }
- else
- {
- for (int portval = begin; portval <= end; ++portval)
- {
- if (!HasPort(portval, Addr))
- {
- ports_found++;
- Config->ports[clientportcount+InitialPortCount] = portval;
- if (*Addr == '*')
- *Addr = 0;
-
- strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
- clientportcount++;
- this->Log(DEBUG,"NEW binding %s:%d [%s] from config (part of port range %s)",Addr, portval, Type, portno.c_str());
- }
- }
- }
- }
- else
- {
- this->Log(DEBUG,"Single port, %s", portno.c_str());
- /* Single port */
- if (!HasPort(atoi(portno.c_str()), Addr))
- {
- ports_found++;
- Config->ports[clientportcount+InitialPortCount] = atoi(portno.c_str());
- if (*Addr == '*')
- *Addr = 0;
- strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
- clientportcount++;
- this->Log(DEBUG,"NEW binding %s:%s [%s] from config (single port)",Addr, portno.c_str(), Type);
- }
+ ports_found++;
+ Config->ports[clientportcount+InitialPortCount] = portno;
+ if (*Addr == '*')
+ *Addr = 0;
+
+ strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
+ clientportcount++;
+ this->Log(DEBUG,"NEW binding %s:%d [%s] from config",Addr, portno, Type);
}
}
}