1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
19 #include "../transport.h"
22 #include "treeserver.h"
24 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
26 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
27 * represents our own server. Therefore, it has no route, no parent, and
28 * no socket associated with it. Its version string is our own local version.
30 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
31 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
33 age = Instance->Time();
36 VersionString.clear();
37 ServerUserCount = ServerOperCount = 0;
38 VersionString = ServerInstance->GetVersionString();
40 Socket = NULL; /* Fix by brain */
42 Warned = Hidden = false;
47 /** When we create a new server, we call this constructor to initialize it.
48 * This constructor initializes the server's Route and Parent, and sets up
49 * its ping counters so that it will be pinged one minute from now.
51 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
52 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
54 age = Instance->Time();
56 VersionString.clear();
57 ServerUserCount = ServerOperCount = 0;
58 SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
64 gettimeofday(&t, NULL);
65 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
66 this->StartBurst = ts;
67 Instance->Logs->Log("m_spanningtree",DEBUG, "Started bursting at time %lu", ts);
69 /* find the 'route' for this server (e.g. the one directly connected
70 * to the local server, which we can use to reach it)
72 * In the following example, consider we have just added a TreeServer
73 * class for server G on our network, of which we are server A.
74 * To route traffic to G (marked with a *) we must send the data to
75 * B (marked with a +) so this algorithm initializes the 'Route'
76 * value to point at whichever server traffic must be routed through
77 * to get here. If we were to try this algorithm with server B,
78 * the Route pointer would point at its own object ('this').
88 * We only run this algorithm when a server is created, as
89 * the routes remain constant while ever the server exists, and
90 * do not need to be re-calculated.
94 if (Route == Utils->TreeRoot)
100 while (this->Route->GetParent() != Utils->TreeRoot)
102 this->Route = Route->GetParent();
106 /* Because recursive code is slow and takes a lot of resources,
107 * we store two representations of the server tree. The first
108 * is a recursive structure where each server references its
109 * children and its parent, which is used for netbursts and
110 * netsplits to dump the whole dataset to the other server,
111 * and the second is used for very fast lookups when routing
112 * messages and is instead a hash_map, where each item can
113 * be referenced by its server name. The AddHashEntry()
114 * call below automatically inserts each TreeServer class
115 * into the hash_map as it is created. There is a similar
116 * maintainance call in the destructor to tidy up deleted
120 this->AddHashEntry();
125 std::string& TreeServer::GetID()
130 void TreeServer::FinishBurstInternal()
132 this->bursting = false;
133 SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
135 for(unsigned int q=0; q < ChildCount(); q++)
137 TreeServer* child = GetChild(q);
138 child->FinishBurstInternal();
142 void TreeServer::FinishBurst()
144 FinishBurstInternal();
145 ServerInstance->XLines->ApplyLines();
147 gettimeofday(&t, NULL);
148 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
149 unsigned long bursttime = ts - this->StartBurst;
150 ServerInstance->SNO->WriteToSnoMask('l', "Received end of netburst from \2%s\2 (burst time: %lu %s)",
151 ServerName.c_str(), (bursttime > 10000 ? bursttime / 1000 : bursttime), (bursttime > 10000 ? "secs" : "msecs"));
152 Event rmode((char*)ServerName.c_str(), (Module*)Utils->Creator, "new_server");
153 rmode.Send(ServerInstance);
156 void TreeServer::SetID(const std::string &id)
158 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
160 Utils->sidlist[sid] = this;
163 int TreeServer::QuitUsers(const std::string &reason)
165 const char* reason_s = reason.c_str();
166 std::vector<User*> time_to_die;
167 for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
169 if (!strcmp(n->second->server, this->ServerName.c_str()))
171 time_to_die.push_back(n->second);
174 for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
179 if (ServerInstance->Config->HideSplits)
180 ServerInstance->Users->QuitUser(a, "*.net *.split", reason_s);
182 ServerInstance->Users->QuitUser(a, reason_s);
184 if (this->Utils->quiet_bursts)
188 return time_to_die.size();
191 /** This method is used to add the structure to the
192 * hash_map for linear searches. It is only called
193 * by the constructors.
195 void TreeServer::AddHashEntry()
197 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
198 if (iter == Utils->serverlist.end())
199 Utils->serverlist[this->ServerName.c_str()] = this;
202 /** This method removes the reference to this object
203 * from the hash_map which is used for linear searches.
204 * It is only called by the default destructor.
206 void TreeServer::DelHashEntry()
208 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
209 if (iter != Utils->serverlist.end())
210 Utils->serverlist.erase(iter);
213 /** These accessors etc should be pretty self-
216 TreeServer* TreeServer::GetRoute()
221 std::string TreeServer::GetName()
223 return ServerName.c_str();
226 std::string TreeServer::GetDesc()
231 std::string TreeServer::GetVersion()
233 return VersionString;
236 void TreeServer::SetNextPingTime(time_t t)
239 LastPingWasGood = false;
242 time_t TreeServer::NextPingTime()
247 bool TreeServer::AnsweredLastPing()
249 return LastPingWasGood;
252 void TreeServer::SetPingFlag()
254 LastPingWasGood = true;
257 unsigned int TreeServer::GetUserCount()
259 return ServerUserCount;
262 void TreeServer::SetUserCount(int diff)
264 ServerUserCount += diff;
267 void TreeServer::SetOperCount(int diff)
269 ServerOperCount += diff;
272 unsigned int TreeServer::GetOperCount()
274 return ServerOperCount;
277 TreeSocket* TreeServer::GetSocket()
282 TreeServer* TreeServer::GetParent()
287 void TreeServer::SetVersion(const std::string &Version)
289 VersionString = Version;
292 unsigned int TreeServer::ChildCount()
294 return Children.size();
297 TreeServer* TreeServer::GetChild(unsigned int n)
299 if (n < Children.size())
301 /* Make sure they cant request
302 * an out-of-range object. After
303 * all we know what these programmer
304 * types are like *grin*.
314 void TreeServer::AddChild(TreeServer* Child)
316 Children.push_back(Child);
319 bool TreeServer::DelChild(TreeServer* Child)
321 for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
332 /** Removes child nodes of this node, and of that node, etc etc.
333 * This is used during netsplits to automatically tidy up the
334 * server tree. It is slow, we don't use it for much else.
336 bool TreeServer::Tidy()
338 bool stillchildren = true;
339 while (stillchildren)
341 stillchildren = false;
342 for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
344 TreeServer* s = (TreeServer*)*a;
348 stillchildren = true;
355 TreeServer::~TreeServer()
357 /* We'd better tidy up after ourselves, eh? */
358 this->DelHashEntry();
360 server_hash::iterator iter = Utils->sidlist.find(GetID());
361 if (iter != Utils->sidlist.end())
362 Utils->sidlist.erase(iter);