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