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 * ---------------------------------------------------
14 #include "configreader.h"
18 #include "commands/cmd_whois.h"
19 #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) : ServerInstance(Instance), Utils(Util)
37 UserCount = OperCount = 0;
40 VersionString = ServerInstance->GetVersionString();
43 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
44 * represents our own server. Therefore, it has no route, no parent, and
45 * no socket associated with it. Its version string is our own local version.
47 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc) : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
51 UserCount = ServerInstance->UserCount();
52 OperCount = ServerInstance->OperCount();
53 VersionString = ServerInstance->GetVersionString();
55 Socket = NULL; /* Fix by brain */
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, TreeServer* Above, TreeSocket* Sock, bool Hide)
66 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
69 UserCount = OperCount = 0;
70 this->SetNextPingTime(time(NULL) + 60);
73 /* find the 'route' for this server (e.g. the one directly connected
74 * to the local server, which we can use to reach it)
76 * In the following example, consider we have just added a TreeServer
77 * class for server G on our network, of which we are server A.
78 * To route traffic to G (marked with a *) we must send the data to
79 * B (marked with a +) so this algorithm initializes the 'Route'
80 * value to point at whichever server traffic must be routed through
81 * to get here. If we were to try this algorithm with server B,
82 * the Route pointer would point at its own object ('this').
92 * We only run this algorithm when a server is created, as
93 * the routes remain constant while ever the server exists, and
94 * do not need to be re-calculated.
98 if (Route == Utils->TreeRoot)
104 while (this->Route->GetParent() != Utils->TreeRoot)
106 this->Route = Route->GetParent();
110 /* Because recursive code is slow and takes a lot of resources,
111 * we store two representations of the server tree. The first
112 * is a recursive structure where each server references its
113 * children and its parent, which is used for netbursts and
114 * netsplits to dump the whole dataset to the other server,
115 * and the second is used for very fast lookups when routing
116 * messages and is instead a hash_map, where each item can
117 * be referenced by its server name. The AddHashEntry()
118 * call below automatically inserts each TreeServer class
119 * into the hash_map as it is created. There is a similar
120 * maintainance call in the destructor to tidy up deleted
124 this->AddHashEntry();
127 int TreeServer::QuitUsers(const std::string &reason)
129 const char* reason_s = reason.c_str();
130 std::vector<userrec*> time_to_die;
131 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
133 if (!strcmp(n->second->server, this->ServerName.c_str()))
135 time_to_die.push_back(n->second);
138 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
140 userrec* a = (userrec*)*n;
143 if (ServerInstance->Config->HideSplits)
144 userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
146 userrec::QuitUser(ServerInstance, a, reason_s);
149 return time_to_die.size();
152 /** This method is used to add the structure to the
153 * hash_map for linear searches. It is only called
154 * by the constructors.
156 void TreeServer::AddHashEntry()
158 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
159 if (iter == Utils->serverlist.end())
160 Utils->serverlist[this->ServerName.c_str()] = this;
163 /** This method removes the reference to this object
164 * from the hash_map which is used for linear searches.
165 * It is only called by the default destructor.
167 void TreeServer::DelHashEntry()
169 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
170 if (iter != Utils->serverlist.end())
171 Utils->serverlist.erase(iter);
174 /** These accessors etc should be pretty self-
177 TreeServer* TreeServer::GetRoute()
182 std::string TreeServer::GetName()
184 return ServerName.c_str();
187 std::string TreeServer::GetDesc()
192 std::string TreeServer::GetVersion()
194 return VersionString;
197 void TreeServer::SetNextPingTime(time_t t)
200 LastPingWasGood = false;
203 time_t TreeServer::NextPingTime()
208 bool TreeServer::AnsweredLastPing()
210 return LastPingWasGood;
213 void TreeServer::SetPingFlag()
215 LastPingWasGood = true;
218 int TreeServer::GetUserCount()
223 void TreeServer::AddUserCount()
228 void TreeServer::DelUserCount()
233 int TreeServer::GetOperCount()
238 TreeSocket* TreeServer::GetSocket()
243 TreeServer* TreeServer::GetParent()
248 void TreeServer::SetVersion(const std::string &Version)
250 VersionString = Version;
253 unsigned int TreeServer::ChildCount()
255 return Children.size();
258 TreeServer* TreeServer::GetChild(unsigned int n)
260 if (n < Children.size())
262 /* Make sure they cant request
263 * an out-of-range object. After
264 * all we know what these programmer
265 * types are like *grin*.
275 void TreeServer::AddChild(TreeServer* Child)
277 Children.push_back(Child);
280 bool TreeServer::DelChild(TreeServer* Child)
282 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
293 /** Removes child nodes of this node, and of that node, etc etc.
294 * This is used during netsplits to automatically tidy up the
295 * server tree. It is slow, we don't use it for much else.
297 bool TreeServer::Tidy()
299 bool stillchildren = true;
300 while (stillchildren)
302 stillchildren = false;
303 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
305 TreeServer* s = (TreeServer*)*a;
309 stillchildren = true;
316 TreeServer::~TreeServer()
318 /* We'd better tidy up after ourselves, eh? */
319 this->DelHashEntry();