]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
Fix burst time indicator: set startburst in treeserver's constructor, BURST in treeso...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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         ServerUserCount = ServerOperCount = 0;
34         StartBurst = rtt = 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         ServerUserCount = ServerOperCount = 0;
50         VersionString = ServerInstance->GetVersionString();
51         Route = NULL;
52         Socket = NULL; /* Fix by brain */
53         StartBurst = rtt = 0;
54         Warned = Hidden = DupError = false;
55         AddHashEntry();
56         SetID(id);
57 }
58
59 /** When we create a new server, we call this constructor to initialize it.
60  * This constructor initializes the server's Route and Parent, and sets up
61  * its ping counters so that it will be pinged one minute from now.
62  */
63 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
64         : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
65 {
66         VersionString.clear();
67         ServerUserCount = ServerOperCount = 0;
68         this->SetNextPingTime(time(NULL) + Utils->PingFreq);
69         this->SetPingFlag();
70         DupError = false;
71         StartBurst = rtt = 0;
72
73         timeval t;
74         gettimeofday(&t, NULL);
75         long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
76         this->StartBurst = ts;
77         Instance->Log(DEBUG, "Started bursting at time %lu", ts);
78
79         /* find the 'route' for this server (e.g. the one directly connected
80          * to the local server, which we can use to reach it)
81          *
82          * In the following example, consider we have just added a TreeServer
83          * class for server G on our network, of which we are server A.
84          * To route traffic to G (marked with a *) we must send the data to
85          * B (marked with a +) so this algorithm initializes the 'Route'
86          * value to point at whichever server traffic must be routed through
87          * to get here. If we were to try this algorithm with server B,
88          * the Route pointer would point at its own object ('this').
89          *
90          *            A
91          *           / \
92          *        + B   C
93          *         / \   \
94          *        D   E   F
95          *       /         \
96          *    * G           H
97          *
98          * We only run this algorithm when a server is created, as
99          * the routes remain constant while ever the server exists, and
100          * do not need to be re-calculated.
101          */
102
103         Route = Above;
104         if (Route == Utils->TreeRoot)
105         {
106                 Route = this;
107         }
108         else
109         {
110                 while (this->Route->GetParent() != Utils->TreeRoot)
111                 {
112                         this->Route = Route->GetParent();
113                 }
114         }
115
116         /* Because recursive code is slow and takes a lot of resources,
117          * we store two representations of the server tree. The first
118          * is a recursive structure where each server references its
119          * children and its parent, which is used for netbursts and
120          * netsplits to dump the whole dataset to the other server,
121          * and the second is used for very fast lookups when routing
122          * messages and is instead a hash_map, where each item can
123          * be referenced by its server name. The AddHashEntry()
124          * call below automatically inserts each TreeServer class
125          * into the hash_map as it is created. There is a similar
126          * maintainance call in the destructor to tidy up deleted
127          * servers.
128          */
129
130         this->AddHashEntry();
131
132         SetID(id);
133 }
134
135 std::string& TreeServer::GetID()
136 {
137         return sid;
138 }
139
140 void TreeServer::SetID(const std::string &id)
141 {
142         ServerInstance->Log(DEBUG, "Setting SID to " + id);
143         sid = id;
144         server_hash::iterator iter = Utils->sidlist.find(sid);
145         if (iter == Utils->sidlist.end())
146                 Utils->sidlist[sid] = this;
147         else
148                 DupError = true;
149 }
150
151 bool TreeServer::DuplicateID()
152 {
153         return DupError;
154 }
155
156 int TreeServer::QuitUsers(const std::string &reason)
157 {
158         const char* reason_s = reason.c_str();
159         std::vector<User*> time_to_die;
160         for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
161         {
162                 if (!strcmp(n->second->server, this->ServerName.c_str()))
163                 {
164                         time_to_die.push_back(n->second);
165                 }
166         }
167         for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
168         {
169                 User* a = (User*)*n;
170                 if (!IS_LOCAL(a))
171                 {
172                         if (ServerInstance->Config->HideSplits)
173                                 User::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
174                         else
175                                 User::QuitUser(ServerInstance, a, reason_s);
176
177                         if (this->Utils->quiet_bursts)
178                                 ServerInstance->GlobalCulls.MakeSilent(a);
179                 }
180         }
181         return time_to_die.size();
182 }
183
184 /** This method is used to add the structure to the
185  * hash_map for linear searches. It is only called
186  * by the constructors.
187  */
188 void TreeServer::AddHashEntry()
189 {
190         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
191         if (iter == Utils->serverlist.end())
192                 Utils->serverlist[this->ServerName.c_str()] = this;
193 }
194
195 /** This method removes the reference to this object
196  * from the hash_map which is used for linear searches.
197  * It is only called by the default destructor.
198  */
199 void TreeServer::DelHashEntry()
200 {
201         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
202         if (iter != Utils->serverlist.end())
203                 Utils->serverlist.erase(iter);
204 }
205
206 /** These accessors etc should be pretty self-
207  * explanitory.
208  */
209 TreeServer* TreeServer::GetRoute()
210 {
211         return Route;
212 }
213
214 std::string TreeServer::GetName()
215 {
216         return ServerName.c_str();
217 }
218
219 std::string TreeServer::GetDesc()
220 {
221         return ServerDesc;
222 }
223
224 std::string TreeServer::GetVersion()
225 {
226         return VersionString;
227 }
228
229 void TreeServer::SetNextPingTime(time_t t)
230 {
231         this->NextPing = t;
232         LastPingWasGood = false;
233 }
234
235 time_t TreeServer::NextPingTime()
236 {
237         return NextPing;
238 }
239
240 bool TreeServer::AnsweredLastPing()
241 {
242         return LastPingWasGood;
243 }
244
245 void TreeServer::SetPingFlag()
246 {
247         LastPingWasGood = true;
248 }
249
250 unsigned int TreeServer::GetUserCount()
251 {
252         return ServerUserCount;
253 }
254
255 void TreeServer::SetUserCount(int diff)
256 {
257         ServerUserCount += diff;
258 }
259
260 void TreeServer::SetOperCount(int diff)
261 {
262         ServerOperCount += diff;
263 }
264
265 unsigned int TreeServer::GetOperCount()
266 {
267         return ServerOperCount;
268 }
269
270 TreeSocket* TreeServer::GetSocket()
271 {
272         return Socket;
273 }
274
275 TreeServer* TreeServer::GetParent()
276 {
277         return Parent;
278 }
279
280 void TreeServer::SetVersion(const std::string &Version)
281 {
282         VersionString = Version;
283 }
284
285 unsigned int TreeServer::ChildCount()
286 {
287         return Children.size();
288 }
289
290 TreeServer* TreeServer::GetChild(unsigned int n)
291 {
292         if (n < Children.size())
293         {
294                 /* Make sure they  cant request
295                  * an out-of-range object. After
296                  * all we know what these programmer
297                  * types are like *grin*.
298                  */
299                 return Children[n];
300         }
301         else
302         {
303                 return NULL;
304         }
305 }
306
307 void TreeServer::AddChild(TreeServer* Child)
308 {
309         Children.push_back(Child);
310 }
311
312 bool TreeServer::DelChild(TreeServer* Child)
313 {
314         for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
315         {
316                 if (*a == Child)
317                 {
318                         Children.erase(a);
319                         return true;
320                 }
321         }
322         return false;
323 }
324
325 /** Removes child nodes of this node, and of that node, etc etc.
326  * This is used during netsplits to automatically tidy up the
327  * server tree. It is slow, we don't use it for much else.
328  */
329 bool TreeServer::Tidy()
330 {
331         bool stillchildren = true;
332         while (stillchildren)
333         {
334                 stillchildren = false;
335                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
336                 {
337                         TreeServer* s = (TreeServer*)*a;
338                         s->Tidy();
339                         Children.erase(a);
340                         delete s;
341                         stillchildren = true;
342                         break;
343                 }
344         }
345         return true;
346 }
347
348 TreeServer::~TreeServer()
349 {
350         /* We'd better tidy up after ourselves, eh? */
351         this->DelHashEntry();
352
353         server_hash::iterator iter = Utils->sidlist.find(GetID());
354         if (iter != Utils->sidlist.end())
355                 Utils->sidlist.erase(iter);
356 }
357
358