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