1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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 * ---------------------------------------------------
18 #include "../spanningtree.h"
21 #include "treeserver.h"
23 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
25 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
26 * represents our own server. Therefore, it has no route, no parent, and
27 * no socket associated with it. Its version string is our own local version.
29 TreeServer::TreeServer(SpanningTreeUtilities* Util, std::string Name, std::string Desc, const std::string &id)
30 : ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util), ServerUser(ServerInstance->FakeClient)
32 age = ServerInstance->Time();
35 VersionString.clear();
36 ServerUserCount = ServerOperCount = 0;
37 VersionString = ServerInstance->GetVersionString();
39 Socket = NULL; /* Fix by brain */
41 Warned = Hidden = false;
46 /** When we create a new server, we call this constructor to initialize it.
47 * This constructor initializes the server's Route and Parent, and sets up
48 * its ping counters so that it will be pinged one minute from now.
50 TreeServer::TreeServer(SpanningTreeUtilities* Util, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
51 : Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), ServerUser(new FakeUser(id, Name)), Hidden(Hide)
53 age = ServerInstance->Time();
55 VersionString.clear();
56 ServerUserCount = ServerOperCount = 0;
57 SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
62 long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
63 this->StartBurst = ts;
64 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Started bursting at time %lu", ts);
66 /* find the 'route' for this server (e.g. the one directly connected
67 * to the local server, which we can use to reach it)
69 * In the following example, consider we have just added a TreeServer
70 * class for server G on our network, of which we are server A.
71 * To route traffic to G (marked with a *) we must send the data to
72 * B (marked with a +) so this algorithm initializes the 'Route'
73 * value to point at whichever server traffic must be routed through
74 * to get here. If we were to try this algorithm with server B,
75 * the Route pointer would point at its own object ('this').
85 * We only run this algorithm when a server is created, as
86 * the routes remain constant while ever the server exists, and
87 * do not need to be re-calculated.
91 if (Route == Utils->TreeRoot)
97 while (this->Route->GetParent() != Utils->TreeRoot)
99 this->Route = Route->GetParent();
103 /* Because recursive code is slow and takes a lot of resources,
104 * we store two representations of the server tree. The first
105 * is a recursive structure where each server references its
106 * children and its parent, which is used for netbursts and
107 * netsplits to dump the whole dataset to the other server,
108 * and the second is used for very fast lookups when routing
109 * messages and is instead a hash_map, where each item can
110 * be referenced by its server name. The AddHashEntry()
111 * call below automatically inserts each TreeServer class
112 * into the hash_map as it is created. There is a similar
113 * maintainance call in the destructor to tidy up deleted
117 this->AddHashEntry();
122 std::string& TreeServer::GetID()
127 void TreeServer::FinishBurstInternal()
129 this->bursting = false;
130 SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
132 for(unsigned int q=0; q < ChildCount(); q++)
134 TreeServer* child = GetChild(q);
135 child->FinishBurstInternal();
139 void TreeServer::FinishBurst()
141 FinishBurstInternal();
142 ServerInstance->XLines->ApplyLines();
143 long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
144 unsigned long bursttime = ts - this->StartBurst;
145 ServerInstance->SNO->WriteToSnoMask(Parent == Utils->TreeRoot ? 'l' : 'L', "Received end of netburst from \2%s\2 (burst time: %lu %s)",
146 ServerName.c_str(), (bursttime > 10000 ? bursttime / 1000 : bursttime), (bursttime > 10000 ? "secs" : "msecs"));
147 AddServerEvent(Utils->Creator, ServerName.c_str());
150 void TreeServer::SetID(const std::string &id)
152 ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
154 Utils->sidlist[sid] = this;
157 int TreeServer::QuitUsers(const std::string &reason)
159 const char* reason_s = reason.c_str();
160 std::vector<User*> time_to_die;
161 for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
163 if (n->second->server == ServerName)
165 time_to_die.push_back(n->second);
168 for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
173 if (this->Utils->quiet_bursts)
176 if (ServerInstance->Config->HideSplits)
177 ServerInstance->Users->QuitUser(a, "*.net *.split", reason_s);
179 ServerInstance->Users->QuitUser(a, reason_s);
182 return time_to_die.size();
185 /** This method is used to add the structure to the
186 * hash_map for linear searches. It is only called
187 * by the constructors.
189 void TreeServer::AddHashEntry()
191 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
192 if (iter == Utils->serverlist.end())
193 Utils->serverlist[this->ServerName.c_str()] = this;
196 /** This method removes the reference to this object
197 * from the hash_map which is used for linear searches.
198 * It is only called by the default destructor.
200 void TreeServer::DelHashEntry()
202 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
203 if (iter != Utils->serverlist.end())
204 Utils->serverlist.erase(iter);
207 /** These accessors etc should be pretty self-
210 TreeServer* TreeServer::GetRoute()
215 std::string TreeServer::GetName()
217 return ServerName.c_str();
220 std::string TreeServer::GetDesc()
225 std::string TreeServer::GetVersion()
227 return VersionString;
230 void TreeServer::SetNextPingTime(time_t t)
233 LastPingWasGood = false;
236 time_t TreeServer::NextPingTime()
241 bool TreeServer::AnsweredLastPing()
243 return LastPingWasGood;
246 void TreeServer::SetPingFlag()
248 LastPingWasGood = true;
251 unsigned int TreeServer::GetUserCount()
253 return ServerUserCount;
256 void TreeServer::SetUserCount(int diff)
258 ServerUserCount += diff;
261 void TreeServer::SetOperCount(int diff)
263 ServerOperCount += diff;
266 unsigned int TreeServer::GetOperCount()
268 return ServerOperCount;
271 TreeSocket* TreeServer::GetSocket()
276 TreeServer* TreeServer::GetParent()
281 void TreeServer::SetVersion(const std::string &Version)
283 VersionString = Version;
286 unsigned int TreeServer::ChildCount()
288 return Children.size();
291 TreeServer* TreeServer::GetChild(unsigned int n)
293 if (n < Children.size())
295 /* Make sure they cant request
296 * an out-of-range object. After
297 * all we know what these programmer
298 * types are like *grin*.
308 void TreeServer::AddChild(TreeServer* Child)
310 Children.push_back(Child);
313 bool TreeServer::DelChild(TreeServer* Child)
315 for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
326 /** Removes child nodes of this node, and of that node, etc etc.
327 * This is used during netsplits to automatically tidy up the
328 * server tree. It is slow, we don't use it for much else.
330 bool TreeServer::Tidy()
334 std::vector<TreeServer*>::iterator a = Children.begin();
335 if (a == Children.end())
345 CullResult TreeServer::cull()
347 if (ServerUser != ServerInstance->FakeClient)
349 return classbase::cull();
352 TreeServer::~TreeServer()
354 /* We'd better tidy up after ourselves, eh? */
355 this->DelHashEntry();
356 if (ServerUser != ServerInstance->FakeClient)
359 server_hash::iterator iter = Utils->sidlist.find(GetID());
360 if (iter != Utils->sidlist.end())
361 Utils->sidlist.erase(iter);