]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
64f01e83ce4c7fcb984684f61ee88af86e28b427
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "wildcard.h"
19 #include "xline.h"
20 #include "transport.h"
21
22 #include "m_spanningtree/utils.h"
23 #include "m_spanningtree/treeserver.h"
24
25 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
26
27 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, const std::string &id) : ServerInstance(Instance), Utils(Util)
28 {
29         Parent = NULL;
30         ServerName.clear();
31         ServerDesc.clear();
32         VersionString.clear();
33         UserCount = OperCount = 0;
34         rtt = LastPing = 0;
35         Warned = Hidden = DupError = false;
36         VersionString = ServerInstance->GetVersionString();
37         SetID(id);
38 }
39
40 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
41  * represents our own server. Therefore, it has no route, no parent, and
42  * no socket associated with it. Its version string is our own local version.
43  */
44 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
45                                                 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
46 {
47         Parent = NULL;
48         VersionString.clear();
49         UserCount = ServerInstance->UserCount();
50         OperCount = ServerInstance->OperCount();
51         VersionString = ServerInstance->GetVersionString();
52         Route = NULL;
53         Socket = NULL; /* Fix by brain */
54         rtt = LastPing = 0;
55         Warned = Hidden = DupError = false;
56         AddHashEntry();
57         SetID(id);
58 }
59
60 /** When we create a new server, we call this constructor to initialize it.
61  * This constructor initializes the server's Route and Parent, and sets up
62  * its ping counters so that it will be pinged one minute from now.
63  */
64 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
65         : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
66 {
67         VersionString.clear();
68         UserCount = OperCount = 0;
69         this->SetNextPingTime(time(NULL) + Utils->PingFreq);
70         this->SetPingFlag();
71         DupError = false;
72         rtt = LastPing = 0;
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)
75          *
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').
83          *
84          *            A
85          *           / \
86          *        + B   C
87          *         / \   \
88          *        D   E   F
89          *       /         \
90          *    * G           H
91          *
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.
95          */
96
97         Route = Above;
98         if (Route == Utils->TreeRoot)
99         {
100                 Route = this;
101         }
102         else
103         {
104                 while (this->Route->GetParent() != Utils->TreeRoot)
105                 {
106                         this->Route = Route->GetParent();
107                 }
108         }
109
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
121          * servers.
122          */
123
124         this->AddHashEntry();
125
126         SetID(id);
127 }
128
129 std::string& TreeServer::GetID()
130 {
131         return sid;
132 }
133
134 void TreeServer::SetID(const std::string &id)
135 {
136         ServerInstance->Log(DEBUG, "Setting SID to " + id);
137         sid = id;
138         server_hash::iterator iter = Utils->sidlist.find(sid);
139         if (iter == Utils->sidlist.end())
140                 Utils->sidlist[sid] = this;
141         else
142                 DupError = true;
143 }
144
145 bool TreeServer::DuplicateID()
146 {
147         return DupError;
148 }
149
150 int TreeServer::QuitUsers(const std::string &reason)
151 {
152         const char* reason_s = reason.c_str();
153         std::vector<User*> time_to_die;
154         for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
155         {
156                 if (!strcmp(n->second->server, this->ServerName.c_str()))
157                 {
158                         time_to_die.push_back(n->second);
159                 }
160         }
161         for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
162         {
163                 User* a = (User*)*n;
164                 if (!IS_LOCAL(a))
165                 {
166                         if (ServerInstance->Config->HideSplits)
167                                 User::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
168                         else
169                                 User::QuitUser(ServerInstance, a, reason_s);
170
171                         if (this->Utils->quiet_bursts)
172                                 ServerInstance->GlobalCulls.MakeSilent(a);
173                 }
174         }
175         return time_to_die.size();
176 }
177
178 /** This method is used to add the structure to the
179  * hash_map for linear searches. It is only called
180  * by the constructors.
181  */
182 void TreeServer::AddHashEntry()
183 {
184         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
185         if (iter == Utils->serverlist.end())
186                 Utils->serverlist[this->ServerName.c_str()] = this;
187 }
188
189 /** This method removes the reference to this object
190  * from the hash_map which is used for linear searches.
191  * It is only called by the default destructor.
192  */
193 void TreeServer::DelHashEntry()
194 {
195         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
196         if (iter != Utils->serverlist.end())
197                 Utils->serverlist.erase(iter);
198 }
199
200 /** These accessors etc should be pretty self-
201  * explanitory.
202  */
203 TreeServer* TreeServer::GetRoute()
204 {
205         return Route;
206 }
207
208 std::string TreeServer::GetName()
209 {
210         return ServerName.c_str();
211 }
212
213 std::string TreeServer::GetDesc()
214 {
215         return ServerDesc;
216 }
217
218 std::string TreeServer::GetVersion()
219 {
220         return VersionString;
221 }
222
223 void TreeServer::SetNextPingTime(time_t t)
224 {
225         this->NextPing = t;
226         LastPingWasGood = false;
227 }
228
229 time_t TreeServer::NextPingTime()
230 {
231         return NextPing;
232 }
233
234 bool TreeServer::AnsweredLastPing()
235 {
236         return LastPingWasGood;
237 }
238
239 void TreeServer::SetPingFlag()
240 {
241         LastPingWasGood = true;
242 }
243
244 int TreeServer::GetUserCount()
245 {
246         return UserCount;
247 }
248
249 void TreeServer::AddUserCount()
250 {
251         UserCount++;
252 }
253
254 void TreeServer::DelUserCount()
255 {
256         UserCount--;
257 }
258
259 int TreeServer::GetOperCount()
260 {
261         return OperCount;
262 }
263
264 TreeSocket* TreeServer::GetSocket()
265 {
266         return Socket;
267 }
268
269 TreeServer* TreeServer::GetParent()
270 {
271         return Parent;
272 }
273
274 void TreeServer::SetVersion(const std::string &Version)
275 {
276         VersionString = Version;
277 }
278
279 unsigned int TreeServer::ChildCount()
280 {
281         return Children.size();
282 }
283
284 TreeServer* TreeServer::GetChild(unsigned int n)
285 {
286         if (n < Children.size())
287         {
288                 /* Make sure they  cant request
289                  * an out-of-range object. After
290                  * all we know what these programmer
291                  * types are like *grin*.
292                  */
293                 return Children[n];
294         }
295         else
296         {
297                 return NULL;
298         }
299 }
300
301 void TreeServer::AddChild(TreeServer* Child)
302 {
303         Children.push_back(Child);
304 }
305
306 bool TreeServer::DelChild(TreeServer* Child)
307 {
308         for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
309         {
310                 if (*a == Child)
311                 {
312                         Children.erase(a);
313                         return true;
314                 }
315         }
316         return false;
317 }
318
319 /** Removes child nodes of this node, and of that node, etc etc.
320  * This is used during netsplits to automatically tidy up the
321  * server tree. It is slow, we don't use it for much else.
322  */
323 bool TreeServer::Tidy()
324 {
325         bool stillchildren = true;
326         while (stillchildren)
327         {
328                 stillchildren = false;
329                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
330                 {
331                         TreeServer* s = (TreeServer*)*a;
332                         s->Tidy();
333                         Children.erase(a);
334                         delete s;
335                         stillchildren = true;
336                         break;
337                 }
338         }
339         return true;
340 }
341
342 TreeServer::~TreeServer()
343 {
344         /* We'd better tidy up after ourselves, eh? */
345         this->DelHashEntry();
346
347         server_hash::iterator iter = Utils->sidlist.find(GetID());
348         if (iter != Utils->sidlist.end())
349                 Utils->sidlist.erase(iter);
350 }
351
352