1
0
mirror of https://github.com/anope/anope.git synced 2026-06-12 19:14:47 +02:00

Speed up akill xline checks

Cache xline nick, user, host, etc instead of rebuilding it everytime its
requested. Store users ip in sockaddr form and not string form to
prevent having to rebuild sockaddrs when checking xlines.

Also do not try to convert empty config values in Config::Get as this
can be rather common if a non string configuration value is not set, and
the cost of the ConvertException is great.
This commit is contained in:
Adam
2014-05-20 21:10:49 -04:00
parent 20ce170024
commit 866f3f32ab
20 changed files with 115 additions and 112 deletions
+7 -8
View File
@@ -51,14 +51,13 @@ namespace Configuration
template<typename T> T Get(const Anope::string &tag, const Anope::string &def) const
{
const Anope::string &value = this->Get<const Anope::string>(tag, def);
try
{
return convertTo<T>(value);
}
catch (const ConvertException &)
{
return T();
}
if (!value.empty())
try
{
return convertTo<T>(value);
}
catch (const ConvertException &) { }
return T();
}
bool Set(const Anope::string &tag, const Anope::string &value);
+1 -1
View File
@@ -7,7 +7,7 @@ struct Session
unsigned count; /* Number of clients with this host */
unsigned hits; /* Number of subsequent kills for a host */
Session(const Anope::string &ip, int len) : addr(ip, len), count(1), hits(0) { }
Session(const sockaddrs &ip, int len) : addr(ip, len), count(1), hits(0) { }
};
struct Exception : Serializable
+2 -3
View File
@@ -59,7 +59,7 @@ union CoreExport sockaddrs
/** Check if this sockaddr has data in it
*/
bool operator()() const;
bool valid() const;
/** Compares with sockaddr with another. Compares address type, port, and address
* @return true if they are the same
@@ -82,8 +82,6 @@ union CoreExport sockaddrs
* @throws A socket exception if given an invalid structure
*/
void ntop(int type, const void *src);
bool valid() const;
};
class CoreExport cidr
@@ -94,6 +92,7 @@ class CoreExport cidr
public:
cidr(const Anope::string &ip);
cidr(const Anope::string &ip, unsigned char len);
cidr(const sockaddrs &ip, unsigned char len);
Anope::string mask() const;
bool match(const sockaddrs &other);
bool valid() const;
+2 -1
View File
@@ -20,6 +20,7 @@
#include "serialize.h"
#include "commands.h"
#include "account.h"
#include "sockets.h"
typedef Anope::hash_map<User *> user_map;
@@ -71,7 +72,7 @@ class CoreExport User : public virtual Base, public Extensible, public CommandRe
/* SSL Fingerprint */
Anope::string fingerprint;
/* User's IP */
Anope::string ip;
sockaddrs ip;
/* Server user is connected to */
Server *server;
/* When the user signed on. Set on connect and never modified. */
+8 -5
View File
@@ -12,12 +12,15 @@
#include "serialize.h"
#include "service.h"
#include "sockets.h"
/* An Xline, eg, anything added with operserv/akill, or any of the operserv/sxline commands */
class CoreExport XLine : public Serializable
{
void InitRegex();
void Init();
Anope::string nick, user, host, real;
public:
cidr *c;
Anope::string mask;
Regex *regex;
Anope::string by;
@@ -32,10 +35,10 @@ class CoreExport XLine : public Serializable
XLine(const Anope::string &mask, const Anope::string &by, const time_t expires, const Anope::string &reason, const Anope::string &uid = "");
~XLine();
Anope::string GetNick() const;
Anope::string GetUser() const;
Anope::string GetHost() const;
Anope::string GetReal() const;
const Anope::string &GetNick() const;
const Anope::string &GetUser() const;
const Anope::string &GetHost() const;
const Anope::string &GetReal() const;
Anope::string GetReason() const;
+2 -2
View File
@@ -502,7 +502,7 @@ class OSDefcon : public Module
if (DConfig.sessionlimit <= 0 || !session_service)
return;
Session *session = session_service->FindSession(u->ip);
Session *session = session_service->FindSession(u->ip.addr());
Exception *exception = session_service->FindException(u);
if (DConfig.Check(DEFCON_REDUCE_SESSION) && !exception)
@@ -511,7 +511,7 @@ class OSDefcon : public Module
{
if (!DConfig.sle_reason.empty())
{
Anope::string message = DConfig.sle_reason.replace_all_cs("%IP%", u->ip);
Anope::string message = DConfig.sle_reason.replace_all_cs("%IP%", u->ip.addr());
u->SendMessage(OperServ, message);
}
if (!DConfig.sle_detailsloc.empty())
+1 -1
View File
@@ -180,7 +180,7 @@ class CommandOSUserList : public Command
if (!pattern.empty())
{
Anope::string mask = u2->nick + "!" + u2->GetIdent() + "@" + u2->GetDisplayedHost(), mask2 = u2->nick + "!" + u2->GetIdent() + "@" + u2->host, mask3 = u2->nick + "!" + u2->GetIdent() + "@" + (!u2->ip.empty() ? u2->ip : u2->host);
Anope::string mask = u2->nick + "!" + u2->GetIdent() + "@" + u2->GetDisplayedHost(), mask2 = u2->nick + "!" + u2->GetIdent() + "@" + u2->host, mask3 = u2->nick + "!" + u2->GetIdent() + "@" + u2->ip.addr();
if (!Anope::Match(mask, pattern) && !Anope::Match(mask2, pattern) && !Anope::Match(mask3, pattern))
continue;
if (!modes.empty())
+7 -7
View File
@@ -64,10 +64,10 @@ class MySessionService : public SessionService
for (std::vector<Exception *>::const_iterator it = this->Exceptions->begin(), it_end = this->Exceptions->end(); it != it_end; ++it)
{
Exception *e = *it;
if (Anope::Match(u->host, e->mask) || Anope::Match(u->ip, e->mask))
if (Anope::Match(u->host, e->mask) || Anope::Match(u->ip.addr(), e->mask))
return e;
if (cidr(e->mask).match(sockaddrs(u->ip)))
if (cidr(e->mask).match(u->ip))
return e;
}
return NULL;
@@ -109,9 +109,9 @@ class MySessionService : public SessionService
return NULL;
}
SessionMap::iterator FindSessionIterator(const Anope::string &ip)
SessionMap::iterator FindSessionIterator(const sockaddrs &ip)
{
cidr c(ip, ip.find(':') != Anope::string::npos ? ipv6_cidr : ipv4_cidr);
cidr c(ip, ip.ipv6() ? ipv6_cidr : ipv4_cidr);
if (!c.valid())
return this->Sessions.end();
return this->Sessions.find(c);
@@ -668,7 +668,7 @@ class OSSession : public Module
if (u->Quitting() || !session_limit || exempt || !u->server || u->server->IsULined())
return;
cidr u_ip(u->ip, u->ip.find(':') != Anope::string::npos ? ipv6_cidr : ipv4_cidr);
cidr u_ip(u->ip, u->ip.ipv6() ? ipv6_cidr : ipv4_cidr);
if (!u_ip.valid())
return;
@@ -705,7 +705,7 @@ class OSSession : public Module
{
if (!sle_reason.empty())
{
Anope::string message = sle_reason.replace_all_cs("%IP%", u->ip);
Anope::string message = sle_reason.replace_all_cs("%IP%", u->ip.addr());
u->SendMessage(OperServ, message);
}
if (!sle_detailsloc.empty())
@@ -729,7 +729,7 @@ class OSSession : public Module
}
else
{
session = new Session(u->ip, u->ip.find(':') != Anope::string::npos ? ipv6_cidr : ipv4_cidr);
session = new Session(u->ip, u->ip.ipv6() ? ipv6_cidr : ipv4_cidr);
}
}
+1 -1
View File
@@ -122,7 +122,7 @@ class ModuleSQLAuthentication : public Module
if (u)
{
q.SetValue("n", u->nick);
q.SetValue("i", u->ip);
q.SetValue("i", u->ip.addr());
}
else
{
+1 -1
View File
@@ -135,7 +135,7 @@ class ModuleSQLOper : public Module
SQL::Query q(this->query);
q.SetValue("a", u->Account()->display);
q.SetValue("i", u->ip);
q.SetValue("i", u->ip.addr());
this->SQL->Run(new SQLOperResult(this, u), q);
+1 -1
View File
@@ -95,7 +95,7 @@ void IRC2SQL::OnUserConnect(User *u, bool &exempt)
query.SetValue("vhost", u->vhost);
query.SetValue("chost", u->chost);
query.SetValue("realname", u->realname);
query.SetValue("ip", u->ip);
query.SetValue("ip", u->ip.addr());
query.SetValue("ident", u->GetIdent());
query.SetValue("vident", u->GetVIdent());
query.SetValue("secure", u->HasMode("SSL") || u->HasExt("ssl") ? "Y" : "N");
+9 -10
View File
@@ -54,18 +54,18 @@ class DNSBLResolver : public Request
record_reason = this->blacklist.replies[result];
}
Anope::string reason = this->blacklist.reason;
Anope::string reason = this->blacklist.reason, addr = user->ip.addr();
reason = reason.replace_all_cs("%n", user->nick);
reason = reason.replace_all_cs("%u", user->GetIdent());
reason = reason.replace_all_cs("%g", user->realname);
reason = reason.replace_all_cs("%h", user->host);
reason = reason.replace_all_cs("%i", user->ip);
reason = reason.replace_all_cs("%i", addr);
reason = reason.replace_all_cs("%r", record_reason);
reason = reason.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
BotInfo *OperServ = Config->GetClient("OperServ");
Log(creator, "dnsbl", OperServ) << user->GetMask() << " (" << user->ip << ") appears in " << this->blacklist.name;
XLine *x = new XLine("*@" + user->ip, OperServ ? OperServ->nick : "m_dnsbl", Anope::CurTime + this->blacklist.bantime, reason, XLineManager::GenerateUID());
Log(creator, "dnsbl", OperServ) << user->GetMask() << " (" << addr << ") appears in " << this->blacklist.name;
XLine *x = new XLine("*@" + addr, OperServ ? OperServ->nick : "m_dnsbl", Anope::CurTime + this->blacklist.bantime, reason, XLineManager::GenerateUID());
if (this->add_to_akill && akills)
{
akills->AddXLine(x);
@@ -130,22 +130,21 @@ class ModuleDNSBL : public Module
return;
/* At this time we only support IPv4 */
sockaddrs user_ip;
user_ip.pton(AF_INET, user->ip);
if (!user_ip.valid())
if (!user->ip.valid() || user->ip.sa.sa_family != AF_INET)
/* User doesn't have a valid IPv4 IP (ipv6/spoof/etc) */
return;
const unsigned long &ip = user_ip.sa4.sin_addr.s_addr;
const unsigned long &ip = user->ip.sa4.sin_addr.s_addr;
unsigned long reverse_ip = (ip << 24) | ((ip & 0xFF00) << 8) | ((ip & 0xFF0000) >> 8) | (ip >> 24);
user_ip.sa4.sin_addr.s_addr = reverse_ip;
sockaddrs reverse = user->ip;
reverse.sa4.sin_addr.s_addr = reverse_ip;
for (unsigned i = 0; i < this->blacklists.size(); ++i)
{
const Blacklist &b = this->blacklists[i];
Anope::string dnsbl_host = user_ip.addr() + "." + b.name;
Anope::string dnsbl_host = reverse.addr() + "." + b.name;
DNSBLResolver *res = NULL;
try
{
+2 -4
View File
@@ -334,9 +334,7 @@ class ModuleProxyScan : public Module
return;
/* At this time we only support IPv4 */
sockaddrs user_ip;
user_ip.pton(AF_INET, user->ip);
if (!user_ip.valid())
if (!user->ip.valid() || user->ip.sa.sa_family != AF_INET)
/* User doesn't have a valid IPv4 IP (ipv6/spoof/etc) */
return;
@@ -364,7 +362,7 @@ class ModuleProxyScan : public Module
con = new SOCKS5ProxyConnect(p, p.ports[k]);
else
continue;
con->Connect(user->ip, p.ports[k]);
con->Connect(user->ip.addr(), p.ports[k]);
}
catch (const SocketException &ex)
{
+1 -2
View File
@@ -212,8 +212,7 @@ class MyXMLRPCEvent : public XMLRPCEvent
request.reply("vhost", iface->Sanitize(u->vhost));
if (!u->chost.empty())
request.reply("chost", iface->Sanitize(u->chost));
if (!u->ip.empty())
request.reply("ip", u->ip);
request.reply("ip", u->ip.addr());
request.reply("timestamp", stringify(u->timestamp));
request.reply("signon", stringify(u->signon));
if (u->Account())
+3 -6
View File
@@ -53,13 +53,10 @@ class SGLineManager : public XLineManager
if (!x->GetReal().empty() && !Anope::Match(u->realname, x->GetReal()))
return false;
if (x->GetHost().find('/') != Anope::string::npos)
{
if (cidr(x->GetHost()).match(sockaddrs(u->ip)))
return true;
}
if (x->c && x->c->match(u->ip))
return true;
if (x->GetHost().empty() || Anope::Match(u->host, x->GetHost()) || Anope::Match(u->ip, x->GetHost()))
if (x->GetHost().empty() || Anope::Match(u->host, x->GetHost()) || Anope::Match(u->ip.addr(), x->GetHost()))
return true;
return false;
-2
View File
@@ -158,8 +158,6 @@ void Join::SJoin(MessageSource &source, const Anope::string &chan, time_t ts, co
if (c->CheckDelete())
delete c;
else
c->CheckModes();
}
}
}
+1 -1
View File
@@ -867,7 +867,7 @@ bool Entry::Matches(User *u, bool full) const
}
}
else if (!this->host.empty() && !Anope::Match(u->GetDisplayedHost(), this->host) && !Anope::Match(u->GetCloakedHost(), this->host) &&
(!full || (!Anope::Match(u->host, this->host) && !Anope::Match(u->ip, this->host))))
(!full || (!Anope::Match(u->host, this->host) && !Anope::Match(u->ip.addr(), this->host))))
ret = false;
if (!this->real.empty() && !Anope::Match(u->realname, this->real))
+8 -7
View File
@@ -96,9 +96,9 @@ bool sockaddrs::ipv6() const
return sa.sa_family == AF_INET6;
}
bool sockaddrs::operator()() const
bool sockaddrs::valid() const
{
return valid();
return size() != 0;
}
bool sockaddrs::operator==(const sockaddrs &other) const
@@ -180,11 +180,6 @@ void sockaddrs::ntop(int type, const void *src)
this->clear();
}
bool sockaddrs::valid() const
{
return size() != 0;
}
cidr::cidr(const Anope::string &ip)
{
bool ipv6 = ip.find(':') != Anope::string::npos;
@@ -221,6 +216,12 @@ cidr::cidr(const Anope::string &ip, unsigned char len)
this->cidr_len = len;
}
cidr::cidr(const sockaddrs &a, unsigned char len) : addr(a)
{
this->cidr_ip = a.addr();
this->cidr_len = len;
}
Anope::string cidr::mask() const
{
if ((this->addr.ipv6() && this->cidr_len == 128) || (!this->addr.ipv6() && this->cidr_len == 32))
+2 -3
View File
@@ -33,7 +33,7 @@ time_t MaxUserTime = 0;
std::list<User *> User::quitting_users;
User::User(const Anope::string &snick, const Anope::string &sident, const Anope::string &shost, const Anope::string &svhost, const Anope::string &sip, Server *sserver, const Anope::string &srealname, time_t ts, const Anope::string &smodes, const Anope::string &suid, NickCore *account)
User::User(const Anope::string &snick, const Anope::string &sident, const Anope::string &shost, const Anope::string &svhost, const Anope::string &uip, Server *sserver, const Anope::string &srealname, time_t ts, const Anope::string &smodes, const Anope::string &suid, NickCore *account) : ip(uip)
{
if (snick.empty() || sident.empty() || shost.empty())
throw CoreException("Bad args passed to User::User");
@@ -49,7 +49,6 @@ User::User(const Anope::string &snick, const Anope::string &sident, const Anope:
this->host = shost;
this->vhost = svhost;
this->chost = svhost;
this->ip = sip;
this->server = sserver;
this->realname = srealname;
this->timestamp = this->signon = ts;
@@ -72,7 +71,7 @@ User::User(const Anope::string &snick, const Anope::string &sident, const Anope:
{
++sserver->users;
if (server->IsSynced())
Log(this, "connect") << (!vhost.empty() && vhost != host ? "(" + vhost + ") " : "") << "(" << srealname << ") " << (!sip.empty() && sip != host ? "[" + sip + "] " : "") << "connected to the network (" << sserver->GetName() << ")";
Log(this, "connect") << (!vhost.empty() && vhost != host ? "(" + vhost + ") " : "") << "(" << srealname << ") " << (!uip.empty() && uip != host ? "[" + uip + "] " : "") << "connected to the network (" << sserver->GetName() << ")";
}
if (UserListByNick.size() > MaxUserCount)
+56 -46
View File
@@ -24,7 +24,7 @@
std::list<XLineManager *> XLineManager::XLineManagers;
Serialize::Checker<std::multimap<Anope::string, XLine *, ci::less> > XLineManager::XLinesByUID("XLine");
void XLine::InitRegex()
void XLine::Init()
{
if (this->mask.length() >= 2 && this->mask[0] == '/' && this->mask[this->mask.length() - 1] == '/' && !Config->GetBlock("options")->Get<const Anope::string>("regexengine").empty())
{
@@ -43,82 +43,92 @@ void XLine::InitRegex()
}
}
}
size_t nick_t = this->mask.find('!');
if (nick_t != Anope::string::npos)
nick = this->mask.substr(0, nick_t);
size_t user_t = this->mask.find('!'), host_t = this->mask.find('@');
if (host_t != Anope::string::npos)
{
if (user_t != Anope::string::npos && host_t > user_t)
user = this->mask.substr(user_t + 1, host_t - user_t - 1);
else
user = this->mask.substr(0, host_t);
}
size_t real_t = this->mask.find('#');
if (host_t != Anope::string::npos)
{
if (real_t != Anope::string::npos && real_t > host_t)
host = this->mask.substr(host_t + 1, real_t - host_t - 1);
else
host = this->mask.substr(host_t + 1);
}
else
{
if (real_t != Anope::string::npos)
host = this->mask.substr(0, real_t);
else
host = this->mask;
}
if (real_t != Anope::string::npos)
real = this->mask.substr(real_t + 1);
if (host.find('/') != Anope::string::npos)
{
c = new cidr(host);
if (!c->valid())
{
delete c;
c = NULL;
}
}
}
XLine::XLine(const Anope::string &ma, const Anope::string &r, const Anope::string &uid) : Serializable("XLine"), mask(ma), by(Me->GetName()), created(0), expires(0), reason(r), id(uid)
{
regex = NULL;
manager = NULL;
c = NULL;
this->InitRegex();
this->Init();
}
XLine::XLine(const Anope::string &ma, const Anope::string &b, const time_t ex, const Anope::string &r, const Anope::string &uid) : Serializable("XLine"), mask(ma), by(b), created(Anope::CurTime), expires(ex), reason(r), id(uid)
{
regex = NULL;
manager = NULL;
c = NULL;
this->InitRegex();
this->Init();
}
XLine::~XLine()
{
delete regex;
delete c;
}
Anope::string XLine::GetNick() const
const Anope::string &XLine::GetNick() const
{
size_t nick_t = this->mask.find('!');
if (nick_t == Anope::string::npos)
return "";
return this->mask.substr(0, nick_t);
return nick;
}
Anope::string XLine::GetUser() const
const Anope::string &XLine::GetUser() const
{
size_t user_t = this->mask.find('!'), host_t = this->mask.find('@');
if (host_t != Anope::string::npos)
{
if (user_t != Anope::string::npos && host_t > user_t)
return this->mask.substr(user_t + 1, host_t - user_t - 1);
else
return this->mask.substr(0, host_t);
}
else
return "";
return user;
}
Anope::string XLine::GetHost() const
const Anope::string &XLine::GetHost() const
{
size_t host_t = this->mask.find('@'), real_t = this->mask.find('#');
if (host_t != Anope::string::npos)
{
if (real_t != Anope::string::npos && real_t > host_t)
return this->mask.substr(host_t + 1, real_t - host_t - 1);
else
return this->mask.substr(host_t + 1);
}
else
{
if (real_t != Anope::string::npos)
return this->mask.substr(0, real_t);
else
return this->mask;
}
return host;
}
Anope::string XLine::GetReal() const
const Anope::string &XLine::GetReal() const
{
size_t real_t = this->mask.find('#');
if (real_t != Anope::string::npos)
return this->mask.substr(real_t + 1);
else
return "";
return real;
}
Anope::string XLine::GetReason() const