1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/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"
21 #include "m_spanningtree/utils.h"
22 #include "m_spanningtree/treeserver.h"
24 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
26 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &id) : ServerInstance(Instance), Utils(Util)
32 VersionString.clear();
33 ServerUserCount = ServerOperCount = 0;
35 Warned = Hidden = DupError = false;
36 VersionString = ServerInstance->GetVersionString();
40 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
41 * represents our own server. Therefore, it has no route, no parent, and
42 * no socket associated with it. Its version string is our own local version.
44 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
45 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
49 VersionString.clear();
50 ServerUserCount = ServerOperCount = 0;
51 VersionString = ServerInstance->GetVersionString();
53 Socket = NULL; /* Fix by brain */
55 Warned = Hidden = DupError = false;
60 /** When we create a new server, we call this constructor to initialize it.
61 * This constructor initializes the server's Route and Parent, and sets up
62 * its ping counters so that it will be pinged one minute from now.
64 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
65 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
68 VersionString.clear();
69 ServerUserCount = ServerOperCount = 0;
70 this->SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
72 Warned = DupError = false;
76 gettimeofday(&t, NULL);
77 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
78 this->StartBurst = ts;
79 Instance->Logs->Log("m_spanningtree",DEBUG, "Started bursting at time %lu", ts);
81 /* find the 'route' for this server (e.g. the one directly connected
82 * to the local server, which we can use to reach it)
84 * In the following example, consider we have just added a TreeServer
85 * class for server G on our network, of which we are server A.
86 * To route traffic to G (marked with a *) we must send the data to
87 * B (marked with a +) so this algorithm initializes the 'Route'
88 * value to point at whichever server traffic must be routed through
89 * to get here. If we were to try this algorithm with server B,
90 * the Route pointer would point at its own object ('this').
100 * We only run this algorithm when a server is created, as
101 * the routes remain constant while ever the server exists, and
102 * do not need to be re-calculated.
106 if (Route == Utils->TreeRoot)
112 while (this->Route->GetParent() != Utils->TreeRoot)
114 this->Route = Route->GetParent();
118 /* Because recursive code is slow and takes a lot of resources,
119 * we store two representations of the server tree. The first
120 * is a recursive structure where each server references its
121 * children and its parent, which is used for netbursts and
122 * netsplits to dump the whole dataset to the other server,
123 * and the second is used for very fast lookups when routing
124 * messages and is instead a hash_map, where each item can
125 * be referenced by its server name. The AddHashEntry()
126 * call below automatically inserts each TreeServer class
127 * into the hash_map as it is created. There is a similar
128 * maintainance call in the destructor to tidy up deleted
132 this->AddHashEntry();
137 std::string& TreeServer::GetID()
142 void TreeServer::FinishBurst()
144 this->bursting = false;
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)", ServerName.c_str(), (bursttime > 1000 ? bursttime / 1000 : bursttime), (bursttime > 1000 ? "secs" : "msecs"));
151 Event rmode((char*)ServerName.c_str(), (Module*)Utils->Creator, "new_server");
152 rmode.Send(ServerInstance);
155 void TreeServer::SetID(const std::string &id)
157 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
159 server_hash::iterator iter = Utils->sidlist.find(sid);
160 if (iter == Utils->sidlist.end())
161 Utils->sidlist[sid] = this;
166 bool TreeServer::DuplicateID()
171 int TreeServer::QuitUsers(const std::string &reason)
173 const char* reason_s = reason.c_str();
174 std::vector<User*> time_to_die;
175 for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
177 if (!strcmp(n->second->server, this->ServerName.c_str()))
179 time_to_die.push_back(n->second);
182 for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
187 if (ServerInstance->Config->HideSplits)
188 ServerInstance->Users->QuitUser(a, "*.net *.split", reason_s);
190 ServerInstance->Users->QuitUser(a, reason_s);
192 if (this->Utils->quiet_bursts)
193 ServerInstance->GlobalCulls.MakeSilent(a);
196 return time_to_die.size();
199 /** This method is used to add the structure to the
200 * hash_map for linear searches. It is only called
201 * by the constructors.
203 void TreeServer::AddHashEntry()
205 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
206 if (iter == Utils->serverlist.end())
207 Utils->serverlist[this->ServerName.c_str()] = this;
210 /** This method removes the reference to this object
211 * from the hash_map which is used for linear searches.
212 * It is only called by the default destructor.
214 void TreeServer::DelHashEntry()
216 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
217 if (iter != Utils->serverlist.end())
218 Utils->serverlist.erase(iter);
221 /** These accessors etc should be pretty self-
224 TreeServer* TreeServer::GetRoute()
229 std::string TreeServer::GetName()
231 return ServerName.c_str();
234 std::string TreeServer::GetDesc()
239 std::string TreeServer::GetVersion()
241 return VersionString;
244 void TreeServer::SetNextPingTime(time_t t)
247 LastPingWasGood = false;
250 time_t TreeServer::NextPingTime()
255 bool TreeServer::AnsweredLastPing()
257 return LastPingWasGood;
260 void TreeServer::SetPingFlag()
262 LastPingWasGood = true;
265 unsigned int TreeServer::GetUserCount()
267 return ServerUserCount;
270 void TreeServer::SetUserCount(int diff)
272 ServerUserCount += diff;
275 void TreeServer::SetOperCount(int diff)
277 ServerOperCount += diff;
280 unsigned int TreeServer::GetOperCount()
282 return ServerOperCount;
285 TreeSocket* TreeServer::GetSocket()
290 TreeServer* TreeServer::GetParent()
295 void TreeServer::SetVersion(const std::string &Version)
297 VersionString = Version;
300 unsigned int TreeServer::ChildCount()
302 return Children.size();
305 TreeServer* TreeServer::GetChild(unsigned int n)
307 if (n < Children.size())
309 /* Make sure they cant request
310 * an out-of-range object. After
311 * all we know what these programmer
312 * types are like *grin*.
322 void TreeServer::AddChild(TreeServer* Child)
324 Children.push_back(Child);
327 bool TreeServer::DelChild(TreeServer* Child)
329 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
340 /** Removes child nodes of this node, and of that node, etc etc.
341 * This is used during netsplits to automatically tidy up the
342 * server tree. It is slow, we don't use it for much else.
344 bool TreeServer::Tidy()
346 bool stillchildren = true;
347 while (stillchildren)
349 stillchildren = false;
350 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
352 TreeServer* s = (TreeServer*)*a;
356 stillchildren = true;
363 TreeServer::~TreeServer()
365 /* We'd better tidy up after ourselves, eh? */
366 this->DelHashEntry();
368 server_hash::iterator iter = Utils->sidlist.find(GetID());
369 if (iter != Utils->sidlist.end())
370 Utils->sidlist.erase(iter);