]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
Merge pull request #53 from SaberUK/clang-analyze
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 "main.h"
18 #include "../spanningtree.h"
19
20 #include "utils.h"
21 #include "treeserver.h"
22
23 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
24
25 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
26  * represents our own server. Therefore, it has no route, no parent, and
27  * no socket associated with it. Its version string is our own local version.
28  */
29 TreeServer::TreeServer(SpanningTreeUtilities* Util, std::string Name, std::string Desc, const std::string &id)
30         : ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util), ServerUser(ServerInstance->FakeClient)
31 {
32         age = ServerInstance->Time();
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, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
51         : Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), ServerUser(new FakeUser(id, Name)), Hidden(Hide)
52 {
53         age = ServerInstance->Time();
54         bursting = true;
55         VersionString.clear();
56         ServerUserCount = ServerOperCount = 0;
57         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
58         SetPingFlag();
59         Warned = false;
60         rtt = 0;
61
62         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
63         this->StartBurst = ts;
64         ServerInstance->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         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
131         SetPingFlag();
132         for(unsigned int q=0; q < ChildCount(); q++)
133         {
134                 TreeServer* child = GetChild(q);
135                 child->FinishBurstInternal();
136         }
137 }
138
139 void TreeServer::FinishBurst()
140 {
141         FinishBurstInternal();
142         ServerInstance->XLines->ApplyLines();
143         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
144         unsigned long bursttime = ts - this->StartBurst;
145         ServerInstance->SNO->WriteToSnoMask(Parent == Utils->TreeRoot ? 'l' : '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         AddServerEvent(Utils->Creator, ServerName.c_str());
148 }
149
150 void TreeServer::SetID(const std::string &id)
151 {
152         ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
153         sid = id;
154         Utils->sidlist[sid] = this;
155 }
156
157 int TreeServer::QuitUsers(const std::string &reason)
158 {
159         const char* reason_s = reason.c_str();
160         std::vector<User*> time_to_die;
161         for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
162         {
163                 if (n->second->server == ServerName)
164                 {
165                         time_to_die.push_back(n->second);
166                 }
167         }
168         for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
169         {
170                 User* a = (User*)*n;
171                 if (!IS_LOCAL(a))
172                 {
173                         if (this->Utils->quiet_bursts)
174                                 a->quietquit = true;
175
176                         if (ServerInstance->Config->HideSplits)
177                                 ServerInstance->Users->QuitUser(a, "*.net *.split", reason_s);
178                         else
179                                 ServerInstance->Users->QuitUser(a, reason_s);
180                 }
181         }
182         return time_to_die.size();
183 }
184
185 /** This method is used to add the structure to the
186  * hash_map for linear searches. It is only called
187  * by the constructors.
188  */
189 void TreeServer::AddHashEntry()
190 {
191         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
192         if (iter == Utils->serverlist.end())
193                 Utils->serverlist[this->ServerName.c_str()] = this;
194 }
195
196 /** This method removes the reference to this object
197  * from the hash_map which is used for linear searches.
198  * It is only called by the default destructor.
199  */
200 void TreeServer::DelHashEntry()
201 {
202         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
203         if (iter != Utils->serverlist.end())
204                 Utils->serverlist.erase(iter);
205 }
206
207 /** These accessors etc should be pretty self-
208  * explanitory.
209  */
210 TreeServer* TreeServer::GetRoute()
211 {
212         return Route;
213 }
214
215 std::string TreeServer::GetName()
216 {
217         return ServerName.c_str();
218 }
219
220 std::string TreeServer::GetDesc()
221 {
222         return ServerDesc;
223 }
224
225 std::string TreeServer::GetVersion()
226 {
227         return VersionString;
228 }
229
230 void TreeServer::SetNextPingTime(time_t t)
231 {
232         this->NextPing = t;
233         LastPingWasGood = false;
234 }
235
236 time_t TreeServer::NextPingTime()
237 {
238         return NextPing;
239 }
240
241 bool TreeServer::AnsweredLastPing()
242 {
243         return LastPingWasGood;
244 }
245
246 void TreeServer::SetPingFlag()
247 {
248         LastPingWasGood = true;
249 }
250
251 unsigned int TreeServer::GetUserCount()
252 {
253         return ServerUserCount;
254 }
255
256 void TreeServer::SetUserCount(int diff)
257 {
258         ServerUserCount += diff;
259 }
260
261 void TreeServer::SetOperCount(int diff)
262 {
263         ServerOperCount += diff;
264 }
265
266 unsigned int TreeServer::GetOperCount()
267 {
268         return ServerOperCount;
269 }
270
271 TreeSocket* TreeServer::GetSocket()
272 {
273         return Socket;
274 }
275
276 TreeServer* TreeServer::GetParent()
277 {
278         return Parent;
279 }
280
281 void TreeServer::SetVersion(const std::string &Version)
282 {
283         VersionString = Version;
284 }
285
286 unsigned int TreeServer::ChildCount()
287 {
288         return Children.size();
289 }
290
291 TreeServer* TreeServer::GetChild(unsigned int n)
292 {
293         if (n < Children.size())
294         {
295                 /* Make sure they  cant request
296                  * an out-of-range object. After
297                  * all we know what these programmer
298                  * types are like *grin*.
299                  */
300                 return Children[n];
301         }
302         else
303         {
304                 return NULL;
305         }
306 }
307
308 void TreeServer::AddChild(TreeServer* Child)
309 {
310         Children.push_back(Child);
311 }
312
313 bool TreeServer::DelChild(TreeServer* Child)
314 {
315         for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
316         {
317                 if (*a == Child)
318                 {
319                         Children.erase(a);
320                         return true;
321                 }
322         }
323         return false;
324 }
325
326 /** Removes child nodes of this node, and of that node, etc etc.
327  * This is used during netsplits to automatically tidy up the
328  * server tree. It is slow, we don't use it for much else.
329  */
330 bool TreeServer::Tidy()
331 {
332         while (1)
333         {
334                 std::vector<TreeServer*>::iterator a = Children.begin();
335                 if (a == Children.end())
336                         return true;
337                 TreeServer* s = *a;
338                 s->Tidy();
339                 s->cull();
340                 Children.erase(a);
341                 delete s;
342         }
343 }
344
345 CullResult TreeServer::cull()
346 {
347         if (ServerUser != ServerInstance->FakeClient)
348                 ServerUser->cull();
349         return classbase::cull();
350 }
351
352 TreeServer::~TreeServer()
353 {
354         /* We'd better tidy up after ourselves, eh? */
355         this->DelHashEntry();
356         if (ServerUser != ServerInstance->FakeClient)
357                 delete ServerUser;
358
359         server_hash::iterator iter = Utils->sidlist.find(GetID());
360         if (iter != Utils->sidlist.end())
361                 Utils->sidlist.erase(iter);
362 }