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