1 #include "configreader.h"
5 #include "commands/cmd_whois.h"
6 #include "commands/cmd_stats.h"
11 #include "transport.h"
13 #include "m_spanningtree/utils.h"
14 #include "m_spanningtree/treeserver.h"
16 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
18 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance) : ServerInstance(Instance), Utils(Util)
24 UserCount = OperCount = 0;
27 VersionString = ServerInstance->GetVersionString();
30 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
31 * represents our own server. Therefore, it has no route, no parent, and
32 * no socket associated with it. Its version string is our own local version.
34 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc) : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
38 UserCount = ServerInstance->UserCount();
39 OperCount = ServerInstance->OperCount();
40 VersionString = ServerInstance->GetVersionString();
42 Socket = NULL; /* Fix by brain */
48 /** When we create a new server, we call this constructor to initialize it.
49 * This constructor initializes the server's Route and Parent, and sets up
50 * its ping counters so that it will be pinged one minute from now.
52 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock, bool Hide)
53 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
56 UserCount = OperCount = 0;
57 this->SetNextPingTime(time(NULL) + 60);
60 /* find the 'route' for this server (e.g. the one directly connected
61 * to the local server, which we can use to reach it)
63 * In the following example, consider we have just added a TreeServer
64 * class for server G on our network, of which we are server A.
65 * To route traffic to G (marked with a *) we must send the data to
66 * B (marked with a +) so this algorithm initializes the 'Route'
67 * value to point at whichever server traffic must be routed through
68 * to get here. If we were to try this algorithm with server B,
69 * the Route pointer would point at its own object ('this').
79 * We only run this algorithm when a server is created, as
80 * the routes remain constant while ever the server exists, and
81 * do not need to be re-calculated.
85 if (Route == Utils->TreeRoot)
91 while (this->Route->GetParent() != Utils->TreeRoot)
93 this->Route = Route->GetParent();
97 /* Because recursive code is slow and takes a lot of resources,
98 * we store two representations of the server tree. The first
99 * is a recursive structure where each server references its
100 * children and its parent, which is used for netbursts and
101 * netsplits to dump the whole dataset to the other server,
102 * and the second is used for very fast lookups when routing
103 * messages and is instead a hash_map, where each item can
104 * be referenced by its server name. The AddHashEntry()
105 * call below automatically inserts each TreeServer class
106 * into the hash_map as it is created. There is a similar
107 * maintainance call in the destructor to tidy up deleted
111 this->AddHashEntry();
114 int TreeServer::QuitUsers(const std::string &reason)
116 const char* reason_s = reason.c_str();
117 std::vector<userrec*> time_to_die;
118 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
120 if (!strcmp(n->second->server, this->ServerName.c_str()))
122 time_to_die.push_back(n->second);
125 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
127 userrec* a = (userrec*)*n;
130 if (ServerInstance->Config->HideSplits)
131 userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
133 userrec::QuitUser(ServerInstance, a, reason_s);
136 return time_to_die.size();
139 /** This method is used to add the structure to the
140 * hash_map for linear searches. It is only called
141 * by the constructors.
143 void TreeServer::AddHashEntry()
145 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
146 if (iter == Utils->serverlist.end())
147 Utils->serverlist[this->ServerName.c_str()] = this;
150 /** This method removes the reference to this object
151 * from the hash_map which is used for linear searches.
152 * It is only called by the default destructor.
154 void TreeServer::DelHashEntry()
156 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
157 if (iter != Utils->serverlist.end())
158 Utils->serverlist.erase(iter);
161 /** These accessors etc should be pretty self-
164 TreeServer* TreeServer::GetRoute()
169 std::string TreeServer::GetName()
171 return ServerName.c_str();
174 std::string TreeServer::GetDesc()
179 std::string TreeServer::GetVersion()
181 return VersionString;
184 void TreeServer::SetNextPingTime(time_t t)
187 LastPingWasGood = false;
190 time_t TreeServer::NextPingTime()
195 bool TreeServer::AnsweredLastPing()
197 return LastPingWasGood;
200 void TreeServer::SetPingFlag()
202 LastPingWasGood = true;
205 int TreeServer::GetUserCount()
210 void TreeServer::AddUserCount()
215 void TreeServer::DelUserCount()
220 int TreeServer::GetOperCount()
225 TreeSocket* TreeServer::GetSocket()
230 TreeServer* TreeServer::GetParent()
235 void TreeServer::SetVersion(const std::string &Version)
237 VersionString = Version;
240 unsigned int TreeServer::ChildCount()
242 return Children.size();
245 TreeServer* TreeServer::GetChild(unsigned int n)
247 if (n < Children.size())
249 /* Make sure they cant request
250 * an out-of-range object. After
251 * all we know what these programmer
252 * types are like *grin*.
262 void TreeServer::AddChild(TreeServer* Child)
264 Children.push_back(Child);
267 bool TreeServer::DelChild(TreeServer* Child)
269 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
280 /** Removes child nodes of this node, and of that node, etc etc.
281 * This is used during netsplits to automatically tidy up the
282 * server tree. It is slow, we don't use it for much else.
284 bool TreeServer::Tidy()
286 bool stillchildren = true;
287 while (stillchildren)
289 stillchildren = false;
290 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
292 TreeServer* s = (TreeServer*)*a;
296 stillchildren = true;
303 TreeServer::~TreeServer()
305 /* We'd better tidy up after ourselves, eh? */
306 this->DelHashEntry();