1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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"
20 #include "transport.h"
22 #include "m_spanningtree/utils.h"
23 #include "m_spanningtree/treeserver.h"
25 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
27 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)
48 VersionString.clear();
49 ServerUserCount = ServerOperCount = 0;
50 VersionString = ServerInstance->GetVersionString();
52 Socket = NULL; /* Fix by brain */
54 Warned = Hidden = DupError = false;
59 /** When we create a new server, we call this constructor to initialize it.
60 * This constructor initializes the server's Route and Parent, and sets up
61 * its ping counters so that it will be pinged one minute from now.
63 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
64 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
66 VersionString.clear();
67 ServerUserCount = ServerOperCount = 0;
68 this->SetNextPingTime(time(NULL) + Utils->PingFreq);
74 gettimeofday(&t, NULL);
75 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
76 this->StartBurst = ts;
77 Instance->Log(DEBUG, "Started bursting at time %lu", ts);
79 /* find the 'route' for this server (e.g. the one directly connected
80 * to the local server, which we can use to reach it)
82 * In the following example, consider we have just added a TreeServer
83 * class for server G on our network, of which we are server A.
84 * To route traffic to G (marked with a *) we must send the data to
85 * B (marked with a +) so this algorithm initializes the 'Route'
86 * value to point at whichever server traffic must be routed through
87 * to get here. If we were to try this algorithm with server B,
88 * the Route pointer would point at its own object ('this').
98 * We only run this algorithm when a server is created, as
99 * the routes remain constant while ever the server exists, and
100 * do not need to be re-calculated.
104 if (Route == Utils->TreeRoot)
110 while (this->Route->GetParent() != Utils->TreeRoot)
112 this->Route = Route->GetParent();
116 /* Because recursive code is slow and takes a lot of resources,
117 * we store two representations of the server tree. The first
118 * is a recursive structure where each server references its
119 * children and its parent, which is used for netbursts and
120 * netsplits to dump the whole dataset to the other server,
121 * and the second is used for very fast lookups when routing
122 * messages and is instead a hash_map, where each item can
123 * be referenced by its server name. The AddHashEntry()
124 * call below automatically inserts each TreeServer class
125 * into the hash_map as it is created. There is a similar
126 * maintainance call in the destructor to tidy up deleted
130 this->AddHashEntry();
135 std::string& TreeServer::GetID()
140 void TreeServer::SetID(const std::string &id)
142 ServerInstance->Log(DEBUG, "Setting SID to " + id);
144 server_hash::iterator iter = Utils->sidlist.find(sid);
145 if (iter == Utils->sidlist.end())
146 Utils->sidlist[sid] = this;
151 bool TreeServer::DuplicateID()
156 int TreeServer::QuitUsers(const std::string &reason)
158 const char* reason_s = reason.c_str();
159 std::vector<User*> time_to_die;
160 for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
162 if (!strcmp(n->second->server, this->ServerName.c_str()))
164 time_to_die.push_back(n->second);
167 for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
172 if (ServerInstance->Config->HideSplits)
173 User::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
175 User::QuitUser(ServerInstance, a, reason_s);
177 if (this->Utils->quiet_bursts)
178 ServerInstance->GlobalCulls.MakeSilent(a);
181 return time_to_die.size();
184 /** This method is used to add the structure to the
185 * hash_map for linear searches. It is only called
186 * by the constructors.
188 void TreeServer::AddHashEntry()
190 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
191 if (iter == Utils->serverlist.end())
192 Utils->serverlist[this->ServerName.c_str()] = this;
195 /** This method removes the reference to this object
196 * from the hash_map which is used for linear searches.
197 * It is only called by the default destructor.
199 void TreeServer::DelHashEntry()
201 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
202 if (iter != Utils->serverlist.end())
203 Utils->serverlist.erase(iter);
206 /** These accessors etc should be pretty self-
209 TreeServer* TreeServer::GetRoute()
214 std::string TreeServer::GetName()
216 return ServerName.c_str();
219 std::string TreeServer::GetDesc()
224 std::string TreeServer::GetVersion()
226 return VersionString;
229 void TreeServer::SetNextPingTime(time_t t)
232 LastPingWasGood = false;
235 time_t TreeServer::NextPingTime()
240 bool TreeServer::AnsweredLastPing()
242 return LastPingWasGood;
245 void TreeServer::SetPingFlag()
247 LastPingWasGood = true;
250 unsigned int TreeServer::GetUserCount()
252 return ServerUserCount;
255 void TreeServer::SetUserCount(int diff)
257 ServerUserCount += diff;
260 void TreeServer::SetOperCount(int diff)
262 ServerOperCount += diff;
265 unsigned int TreeServer::GetOperCount()
267 return ServerOperCount;
270 TreeSocket* TreeServer::GetSocket()
275 TreeServer* TreeServer::GetParent()
280 void TreeServer::SetVersion(const std::string &Version)
282 VersionString = Version;
285 unsigned int TreeServer::ChildCount()
287 return Children.size();
290 TreeServer* TreeServer::GetChild(unsigned int n)
292 if (n < Children.size())
294 /* Make sure they cant request
295 * an out-of-range object. After
296 * all we know what these programmer
297 * types are like *grin*.
307 void TreeServer::AddChild(TreeServer* Child)
309 Children.push_back(Child);
312 bool TreeServer::DelChild(TreeServer* Child)
314 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
325 /** Removes child nodes of this node, and of that node, etc etc.
326 * This is used during netsplits to automatically tidy up the
327 * server tree. It is slow, we don't use it for much else.
329 bool TreeServer::Tidy()
331 bool stillchildren = true;
332 while (stillchildren)
334 stillchildren = false;
335 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
337 TreeServer* s = (TreeServer*)*a;
341 stillchildren = true;
348 TreeServer::~TreeServer()
350 /* We'd better tidy up after ourselves, eh? */
351 this->DelHashEntry();
353 server_hash::iterator iter = Utils->sidlist.find(GetID());
354 if (iter != Utils->sidlist.end())
355 Utils->sidlist.erase(iter);