1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 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 "configreader.h"
19 #include "commands/cmd_whois.h"
20 #include "commands/cmd_stats.h"
24 #include "transport.h"
26 #include "m_spanningtree/utils.h"
27 #include "m_spanningtree/treeserver.h"
29 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
31 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &id) : ServerInstance(Instance), Utils(Util)
36 VersionString.clear();
37 UserCount = OperCount = 0;
40 VersionString = ServerInstance->GetVersionString();
44 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
45 * represents our own server. Therefore, it has no route, no parent, and
46 * no socket associated with it. Its version string is our own local version.
48 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
49 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
52 VersionString.clear();
53 UserCount = ServerInstance->UserCount();
54 OperCount = ServerInstance->OperCount();
55 VersionString = ServerInstance->GetVersionString();
57 Socket = NULL; /* Fix by brain */
64 /** When we create a new server, we call this constructor to initialize it.
65 * This constructor initializes the server's Route and Parent, and sets up
66 * its ping counters so that it will be pinged one minute from now.
68 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
69 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
71 VersionString.clear();
72 UserCount = OperCount = 0;
73 this->SetNextPingTime(time(NULL) + Utils->PingFreq);
76 /* find the 'route' for this server (e.g. the one directly connected
77 * to the local server, which we can use to reach it)
79 * In the following example, consider we have just added a TreeServer
80 * class for server G on our network, of which we are server A.
81 * To route traffic to G (marked with a *) we must send the data to
82 * B (marked with a +) so this algorithm initializes the 'Route'
83 * value to point at whichever server traffic must be routed through
84 * to get here. If we were to try this algorithm with server B,
85 * the Route pointer would point at its own object ('this').
95 * We only run this algorithm when a server is created, as
96 * the routes remain constant while ever the server exists, and
97 * do not need to be re-calculated.
101 if (Route == Utils->TreeRoot)
107 while (this->Route->GetParent() != Utils->TreeRoot)
109 this->Route = Route->GetParent();
113 /* Because recursive code is slow and takes a lot of resources,
114 * we store two representations of the server tree. The first
115 * is a recursive structure where each server references its
116 * children and its parent, which is used for netbursts and
117 * netsplits to dump the whole dataset to the other server,
118 * and the second is used for very fast lookups when routing
119 * messages and is instead a hash_map, where each item can
120 * be referenced by its server name. The AddHashEntry()
121 * call below automatically inserts each TreeServer class
122 * into the hash_map as it is created. There is a similar
123 * maintainance call in the destructor to tidy up deleted
127 this->AddHashEntry();
132 std::string& TreeServer::GetID()
137 void TreeServer::SetID(const std::string &id)
140 server_hash::iterator iter = Utils->sidlist.find(sid);
141 if (iter == Utils->sidlist.end())
142 Utils->sidlist[sid] = this;
144 throw CoreException("Server ID '"+id+"' already exists!");
147 int TreeServer::QuitUsers(const std::string &reason)
149 const char* reason_s = reason.c_str();
150 std::vector<userrec*> time_to_die;
151 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
153 if (!strcmp(n->second->server, this->ServerName.c_str()))
155 time_to_die.push_back(n->second);
158 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
160 userrec* a = (userrec*)*n;
163 if (ServerInstance->Config->HideSplits)
164 userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
166 userrec::QuitUser(ServerInstance, a, reason_s);
168 if (this->Utils->quiet_bursts)
169 ServerInstance->GlobalCulls.MakeSilent(a);
172 return time_to_die.size();
175 /** This method is used to add the structure to the
176 * hash_map for linear searches. It is only called
177 * by the constructors.
179 void TreeServer::AddHashEntry()
181 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
182 if (iter == Utils->serverlist.end())
183 Utils->serverlist[this->ServerName.c_str()] = this;
186 /** This method removes the reference to this object
187 * from the hash_map which is used for linear searches.
188 * It is only called by the default destructor.
190 void TreeServer::DelHashEntry()
192 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
193 if (iter != Utils->serverlist.end())
194 Utils->serverlist.erase(iter);
197 /** These accessors etc should be pretty self-
200 TreeServer* TreeServer::GetRoute()
205 std::string TreeServer::GetName()
207 return ServerName.c_str();
210 std::string TreeServer::GetDesc()
215 std::string TreeServer::GetVersion()
217 return VersionString;
220 void TreeServer::SetNextPingTime(time_t t)
223 LastPingWasGood = false;
226 time_t TreeServer::NextPingTime()
231 bool TreeServer::AnsweredLastPing()
233 return LastPingWasGood;
236 void TreeServer::SetPingFlag()
238 LastPingWasGood = true;
241 int TreeServer::GetUserCount()
246 void TreeServer::AddUserCount()
251 void TreeServer::DelUserCount()
256 int TreeServer::GetOperCount()
261 TreeSocket* TreeServer::GetSocket()
266 TreeServer* TreeServer::GetParent()
271 void TreeServer::SetVersion(const std::string &Version)
273 VersionString = Version;
276 unsigned int TreeServer::ChildCount()
278 return Children.size();
281 TreeServer* TreeServer::GetChild(unsigned int n)
283 if (n < Children.size())
285 /* Make sure they cant request
286 * an out-of-range object. After
287 * all we know what these programmer
288 * types are like *grin*.
298 void TreeServer::AddChild(TreeServer* Child)
300 Children.push_back(Child);
303 bool TreeServer::DelChild(TreeServer* Child)
305 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
316 /** Removes child nodes of this node, and of that node, etc etc.
317 * This is used during netsplits to automatically tidy up the
318 * server tree. It is slow, we don't use it for much else.
320 bool TreeServer::Tidy()
322 bool stillchildren = true;
323 while (stillchildren)
325 stillchildren = false;
326 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
328 TreeServer* s = (TreeServer*)*a;
332 stillchildren = true;
339 TreeServer::~TreeServer()
341 /* We'd better tidy up after ourselves, eh? */
342 this->DelHashEntry();
344 server_hash::iterator iter = Utils->sidlist.find(GetID());
345 if (iter != Utils->sidlist.end())
346 Utils->sidlist.erase(iter);