summaryrefslogtreecommitdiff
path: root/modules/commands/os_session.cpp
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2013-04-07 23:46:44 -0500
committerAdam <Adam@anope.org>2013-04-07 23:46:44 -0500
commitfb7fef7a849342ab8463743497e781c5c3e6ae88 (patch)
tree5d230a68b6eed70c7b4f718410dd62fea779654c /modules/commands/os_session.cpp
parent36602224b8b1a11326a224779d16bcb12f0ed532 (diff)
Optimizations of much of the more commonly used code
Diffstat (limited to 'modules/commands/os_session.cpp')
-rw-r--r--modules/commands/os_session.cpp174
1 files changed, 87 insertions, 87 deletions
diff --git a/modules/commands/os_session.cpp b/modules/commands/os_session.cpp
index 1edda00d6..31846c9db 100644
--- a/modules/commands/os_session.cpp
+++ b/modules/commands/os_session.cpp
@@ -61,11 +61,6 @@ class MySessionService : public SessionService
return this->Exceptions;
}
- void AddSession(Session *s) anope_override
- {
- this->Sessions[s->addr] = s;
- }
-
void DelSession(Session *s) anope_override
{
this->Sessions.erase(s->addr);
@@ -80,6 +75,18 @@ class MySessionService : public SessionService
return NULL;
}
+ SessionMap::iterator FindSessionIterator(const Anope::string &ip)
+ {
+ cidr c(ip, ip.find(':') != Anope::string::npos ? Config->SessionIPv6CIDR : Config->SessionIPv4CIDR);
+ return this->Sessions.find(c);
+ }
+
+ Session* &FindOrCreateSession(const Anope::string &ip)
+ {
+ cidr c(ip, ip.find(':') != Anope::string::npos ? Config->SessionIPv6CIDR : Config->SessionIPv4CIDR);
+ return this->Sessions[c];
+ }
+
SessionMap &GetSessions() anope_override
{
return this->Sessions;
@@ -611,124 +618,117 @@ class OSSession : public Module
CommandOSException commandosexception;
ServiceReference<XLineManager> akills;
- void AddSession(User *u, bool exempt)
+ public:
+ OSSession(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
+ exception_type("Exception", Exception::Unserialize), ss(this), commandossession(this), commandosexception(this), akills("XLineManager", "xlinemanager/sgline")
{
- Session *session;
- try
- {
- session = this->ss.FindSession(u->ip);
- }
- catch (const SocketException &)
- {
+ this->SetAuthor("Anope");
+ this->SetPermanent(true);
+
+ Implementation i[] = { I_OnUserConnect, I_OnPreUserLogoff };
+ ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
+ ModuleManager::SetPriority(this, PRIORITY_FIRST);
+ }
+
+ void OnUserConnect(User *u, bool &exempt) anope_override
+ {
+ if (u->Quitting() || !Config->LimitSessions || exempt || !u->server || u->server->IsULined())
return;
- }
- if (session)
+ try
{
- bool kill = false;
- if (Config->DefSessionLimit && session->count >= Config->DefSessionLimit)
+ Session* &session = this->ss.FindOrCreateSession(u->ip);
+
+ if (session)
{
- kill = true;
- Exception *exception = this->ss.FindException(u);
- if (exception)
+ bool kill = false;
+ if (Config->DefSessionLimit && session->count >= Config->DefSessionLimit)
{
- kill = false;
- if (exception->limit && session->count >= exception->limit)
- kill = true;
+ kill = true;
+ Exception *exception = this->ss.FindException(u);
+ if (exception)
+ {
+ kill = false;
+ if (exception->limit && session->count >= exception->limit)
+ kill = true;
+ }
}
- }
- /* Previously on IRCds that send a QUIT (InspIRCD) when a user is killed, the session for a host was
- * decremented in do_quit, which caused problems and fixed here
- *
- * Now, we create the user struture before calling this to fix some user tracking issues,
- * so we must increment this here no matter what because it will either be
- * decremented in do_kill or in do_quit - Adam
- */
- ++session->count;
+ /* Previously on IRCds that send a QUIT (InspIRCD) when a user is killed, the session for a host was
+ * decremented in do_quit, which caused problems and fixed here
+ *
+ * Now, we create the user struture before calling this to fix some user tracking issues,
+ * so we must increment this here no matter what because it will either be
+ * decremented in do_kill or in do_quit - Adam
+ */
+ ++session->count;
- if (kill && !exempt)
- {
- if (OperServ)
+ if (kill && !exempt)
{
- if (!Config->SessionLimitExceeded.empty())
- u->SendMessage(OperServ, Config->SessionLimitExceeded.c_str(), u->ip.c_str());
- if (!Config->SessionLimitDetailsLoc.empty())
- u->SendMessage(OperServ, "%s", Config->SessionLimitDetailsLoc.c_str());
- }
+ if (OperServ)
+ {
+ if (!Config->SessionLimitExceeded.empty())
+ u->SendMessage(OperServ, Config->SessionLimitExceeded.c_str(), u->ip.c_str());
+ if (!Config->SessionLimitDetailsLoc.empty())
+ u->SendMessage(OperServ, "%s", Config->SessionLimitDetailsLoc.c_str());
+ }
- ++session->hits;
- if (Config->MaxSessionKill && session->hits >= Config->MaxSessionKill && akills)
- {
- const Anope::string &akillmask = "*@" + u->ip;
- XLine *x = new XLine(akillmask, Config->OperServ, Anope::CurTime + Config->SessionAutoKillExpiry, "Session limit exceeded", XLineManager::GenerateUID());
- akills->AddXLine(x);
- akills->Send(NULL, x);
- Log(OperServ, "akill/session") << "Added a temporary AKILL for \002" << akillmask << "\002 due to excessive connections";
- }
- else
- {
- u->Kill(Config->OperServ, "Session limit exceeded");
- u = NULL; /* No guarentee u still exists */
+ ++session->hits;
+ if (Config->MaxSessionKill && session->hits >= Config->MaxSessionKill && akills)
+ {
+ const Anope::string &akillmask = "*@" + u->ip;
+ XLine *x = new XLine(akillmask, Config->OperServ, Anope::CurTime + Config->SessionAutoKillExpiry, "Session limit exceeded", XLineManager::GenerateUID());
+ akills->AddXLine(x);
+ akills->Send(NULL, x);
+ Log(OperServ, "akill/session") << "Added a temporary AKILL for \002" << akillmask << "\002 due to excessive connections";
+ }
+ else
+ {
+ u->Kill(Config->OperServ, "Session limit exceeded");
+ }
}
}
+ else
+ {
+ session = new Session(u->ip, u->ip.find(':') != Anope::string::npos ? Config->SessionIPv6CIDR : Config->SessionIPv4CIDR);
+ }
}
- else
- {
- session = new Session(u->ip, u->ip.find(':') != Anope::string::npos ? Config->SessionIPv6CIDR : Config->SessionIPv4CIDR);
- this->ss.AddSession(session);
- }
+ catch (const SocketException &) { }
}
- void DelSession(User *u)
+ void OnPreUserLogoff(User *u) anope_override
{
- Session *session;
+ if (!Config->LimitSessions || !u->server || u->server->IsULined())
+ return;
+
+ SessionService::SessionMap::iterator sit;
try
{
- session = this->ss.FindSession(u->ip);
+ sit = this->ss.FindSessionIterator(u->ip);
}
catch (const SocketException &)
{
return;
}
- if (!session)
+
+ SessionService::SessionMap &sessions = this->ss.GetSessions();
+
+ if (sit == sessions.end())
{
Log(LOG_DEBUG) << "Tried to delete non-existant session: " << u->ip;
return;
}
+ Session *session = sit->second;
+
if (session->count > 1)
{
--session->count;
return;
}
- this->ss.DelSession(session);
delete session;
- }
-
- public:
- OSSession(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
- exception_type("Exception", Exception::Unserialize), ss(this), commandossession(this), commandosexception(this), akills("XLineManager", "xlinemanager/sgline")
- {
- this->SetAuthor("Anope");
- this->SetPermanent(true);
-
- Implementation i[] = { I_OnUserConnect, I_OnPreUserLogoff };
- ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
- ModuleManager::SetPriority(this, PRIORITY_FIRST);
- }
-
- void OnUserConnect(User *user, bool &exempt) anope_override
- {
- if (!user->Quitting() && Config->LimitSessions)
- this->AddSession(user, exempt);
- }
-
- void OnPreUserLogoff(User *u) anope_override
- {
- if (Config->LimitSessions && (!u->server || !u->server->IsULined()))
- this->DelSession(u);
+ sessions.erase(sit);
}
};