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