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)
33 VersionString.clear();
34 ServerUserCount = ServerOperCount = 0;
36 Warned = Hidden = DupError = false;
37 VersionString = ServerInstance->GetVersionString();
41 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
42 * represents our own server. Therefore, it has no route, no parent, and
43 * no socket associated with it. Its version string is our own local version.
45 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
46 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
50 VersionString.clear();
51 ServerUserCount = ServerOperCount = 0;
52 VersionString = ServerInstance->GetVersionString();
54 Socket = NULL; /* Fix by brain */
56 Warned = Hidden = DupError = false;
61 /** When we create a new server, we call this constructor to initialize it.
62 * This constructor initializes the server's Route and Parent, and sets up
63 * its ping counters so that it will be pinged one minute from now.
65 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
66 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
69 VersionString.clear();
70 ServerUserCount = ServerOperCount = 0;
71 this->SetNextPingTime(time(NULL) + Utils->PingFreq);
77 gettimeofday(&t, NULL);
78 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
79 this->StartBurst = ts;
80 Instance->Logs->Log("m_spanningtree",DEBUG, "Started bursting at time %lu", ts);
82 /* find the 'route' for this server (e.g. the one directly connected
83 * to the local server, which we can use to reach it)
85 * In the following example, consider we have just added a TreeServer
86 * class for server G on our network, of which we are server A.
87 * To route traffic to G (marked with a *) we must send the data to
88 * B (marked with a +) so this algorithm initializes the 'Route'
89 * value to point at whichever server traffic must be routed through
90 * to get here. If we were to try this algorithm with server B,
91 * the Route pointer would point at its own object ('this').
101 * We only run this algorithm when a server is created, as
102 * the routes remain constant while ever the server exists, and
103 * do not need to be re-calculated.
107 if (Route == Utils->TreeRoot)
113 while (this->Route->GetParent() != Utils->TreeRoot)
115 this->Route = Route->GetParent();
119 /* Because recursive code is slow and takes a lot of resources,
120 * we store two representations of the server tree. The first
121 * is a recursive structure where each server references its
122 * children and its parent, which is used for netbursts and
123 * netsplits to dump the whole dataset to the other server,
124 * and the second is used for very fast lookups when routing
125 * messages and is instead a hash_map, where each item can
126 * be referenced by its server name. The AddHashEntry()
127 * call below automatically inserts each TreeServer class
128 * into the hash_map as it is created. There is a similar
129 * maintainance call in the destructor to tidy up deleted
133 this->AddHashEntry();
138 std::string& TreeServer::GetID()
143 void TreeServer::FinishBurst()
145 this->bursting = false;
146 ServerInstance->XLines->ApplyLines();
148 gettimeofday(&t, NULL);
149 long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
150 unsigned long bursttime = ts - this->StartBurst;
151 ServerInstance->SNO->WriteToSnoMask('l', "Received end of netburst from \2%s\2 (burst time: %lu %s)", ServerName.c_str(),
152 (bursttime > 1000 ? bursttime / 1000 : bursttime), (bursttime > 1000 ? "secs" : "msecs"));
153 Event rmode((char*)ServerName.c_str(), (Module*)Utils->Creator, "new_server");
154 rmode.Send(ServerInstance);
157 void TreeServer::SetID(const std::string &id)
159 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
161 server_hash::iterator iter = Utils->sidlist.find(sid);
162 if (iter == Utils->sidlist.end())
163 Utils->sidlist[sid] = this;
168 bool TreeServer::DuplicateID()
173 int TreeServer::QuitUsers(const std::string &reason)
175 const char* reason_s = reason.c_str();
176 std::vector<User*> time_to_die;
177 for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
179 if (!strcmp(n->second->server, this->ServerName.c_str()))
181 time_to_die.push_back(n->second);
184 for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
189 if (ServerInstance->Config->HideSplits)
190 User::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
192 User::QuitUser(ServerInstance, a, reason_s);
194 if (this->Utils->quiet_bursts)
195 ServerInstance->GlobalCulls.MakeSilent(a);
198 return time_to_die.size();
201 /** This method is used to add the structure to the
202 * hash_map for linear searches. It is only called
203 * by the constructors.
205 void TreeServer::AddHashEntry()
207 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
208 if (iter == Utils->serverlist.end())
209 Utils->serverlist[this->ServerName.c_str()] = this;
212 /** This method removes the reference to this object
213 * from the hash_map which is used for linear searches.
214 * It is only called by the default destructor.
216 void TreeServer::DelHashEntry()
218 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
219 if (iter != Utils->serverlist.end())
220 Utils->serverlist.erase(iter);
223 /** These accessors etc should be pretty self-
226 TreeServer* TreeServer::GetRoute()
231 std::string TreeServer::GetName()
233 return ServerName.c_str();
236 std::string TreeServer::GetDesc()
241 std::string TreeServer::GetVersion()
243 return VersionString;
246 void TreeServer::SetNextPingTime(time_t t)
249 LastPingWasGood = false;
252 time_t TreeServer::NextPingTime()
257 bool TreeServer::AnsweredLastPing()
259 return LastPingWasGood;
262 void TreeServer::SetPingFlag()
264 LastPingWasGood = true;
267 unsigned int TreeServer::GetUserCount()
269 return ServerUserCount;
272 void TreeServer::SetUserCount(int diff)
274 ServerUserCount += diff;
277 void TreeServer::SetOperCount(int diff)
279 ServerOperCount += diff;
282 unsigned int TreeServer::GetOperCount()
284 return ServerOperCount;
287 TreeSocket* TreeServer::GetSocket()
292 TreeServer* TreeServer::GetParent()
297 void TreeServer::SetVersion(const std::string &Version)
299 VersionString = Version;
302 unsigned int TreeServer::ChildCount()
304 return Children.size();
307 TreeServer* TreeServer::GetChild(unsigned int n)
309 if (n < Children.size())
311 /* Make sure they cant request
312 * an out-of-range object. After
313 * all we know what these programmer
314 * types are like *grin*.
324 void TreeServer::AddChild(TreeServer* Child)
326 Children.push_back(Child);
329 bool TreeServer::DelChild(TreeServer* Child)
331 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
342 /** Removes child nodes of this node, and of that node, etc etc.
343 * This is used during netsplits to automatically tidy up the
344 * server tree. It is slow, we don't use it for much else.
346 bool TreeServer::Tidy()
348 bool stillchildren = true;
349 while (stillchildren)
351 stillchildren = false;
352 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
354 TreeServer* s = (TreeServer*)*a;
358 stillchildren = true;
365 TreeServer::~TreeServer()
367 /* We'd better tidy up after ourselves, eh? */
368 this->DelHashEntry();
370 server_hash::iterator iter = Utils->sidlist.find(GetID());
371 if (iter != Utils->sidlist.end())
372 Utils->sidlist.erase(iter);