]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
Use ERR_YOUREBANNEDCREEP instead of NOTICE when a user is banned.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #include "inspircd.h"
24 #include "xline.h"
25 #include "main.h"
26 #include "modules/spanningtree.h"
27
28 #include "utils.h"
29 #include "treeserver.h"
30
31 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
32  * represents our own server. Therefore, it has no route, no parent, and
33  * no socket associated with it. Its version string is our own local version.
34  */
35 TreeServer::TreeServer()
36         : Server(ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc)
37         , Parent(NULL), Route(NULL)
38         , VersionString(ServerInstance->GetVersionString())
39         , fullversion(ServerInstance->GetVersionString(true))
40         , Socket(NULL), sid(ServerInstance->Config->GetSID()), behind_bursting(0), isdead(false)
41         , ServerUser(ServerInstance->FakeClient)
42         , age(ServerInstance->Time()), Warned(false), UserCount(ServerInstance->Users.GetLocalUsers().size())
43         , OperCount(0), rtt(0), StartBurst(0), Hidden(false)
44 {
45         AddHashEntry();
46 }
47
48 /** When we create a new server, we call this constructor to initialize it.
49  * This constructor initializes the server's Route and Parent, and sets up
50  * its ping counters so that it will be pinged one minute from now.
51  */
52 TreeServer::TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide)
53         : Server(Name, Desc)
54         , Parent(Above), Socket(Sock), sid(id), behind_bursting(Parent->behind_bursting), isdead(false)
55         , ServerUser(new FakeUser(id, this))
56         , age(ServerInstance->Time()), Warned(false), UserCount(0), OperCount(0), rtt(0), StartBurst(0), Hidden(Hide)
57 {
58         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "New server %s behind_bursting %u", GetName().c_str(), behind_bursting);
59         CheckULine();
60         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
61         SetPingFlag();
62
63         /* find the 'route' for this server (e.g. the one directly connected
64          * to the local server, which we can use to reach it)
65          *
66          * In the following example, consider we have just added a TreeServer
67          * class for server G on our network, of which we are server A.
68          * To route traffic to G (marked with a *) we must send the data to
69          * B (marked with a +) so this algorithm initializes the 'Route'
70          * value to point at whichever server traffic must be routed through
71          * to get here. If we were to try this algorithm with server B,
72          * the Route pointer would point at its own object ('this').
73          *
74          *            A
75          *           / \
76          *        + B   C
77          *         / \   \
78          *        D   E   F
79          *       /         \
80          *    * G           H
81          *
82          * We only run this algorithm when a server is created, as
83          * the routes remain constant while ever the server exists, and
84          * do not need to be re-calculated.
85          */
86
87         Route = Above;
88         if (Route == Utils->TreeRoot)
89         {
90                 Route = this;
91         }
92         else
93         {
94                 while (this->Route->GetParent() != Utils->TreeRoot)
95                 {
96                         this->Route = Route->GetParent();
97                 }
98         }
99
100         /* Because recursive code is slow and takes a lot of resources,
101          * we store two representations of the server tree. The first
102          * is a recursive structure where each server references its
103          * children and its parent, which is used for netbursts and
104          * netsplits to dump the whole dataset to the other server,
105          * and the second is used for very fast lookups when routing
106          * messages and is instead a hash_map, where each item can
107          * be referenced by its server name. The AddHashEntry()
108          * call below automatically inserts each TreeServer class
109          * into the hash_map as it is created. There is a similar
110          * maintainance call in the destructor to tidy up deleted
111          * servers.
112          */
113
114         this->AddHashEntry();
115         Parent->AddChild(this);
116 }
117
118 void TreeServer::BeginBurst(unsigned long startms)
119 {
120         behind_bursting++;
121
122         unsigned long now = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
123         // If the start time is in the future (clocks are not synced) then use current time
124         if ((!startms) || (startms > now))
125                 startms = now;
126         this->StartBurst = startms;
127         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Server %s started bursting at time %lu behind_bursting %u", sid.c_str(), startms, behind_bursting);
128 }
129
130 const std::string& TreeServer::GetID()
131 {
132         return sid;
133 }
134
135 void TreeServer::FinishBurstInternal()
136 {
137         // Check is needed because 1202 protocol servers don't send the bursting state of a server, so servers
138         // introduced during a netburst may later send ENDBURST which would normally decrease this counter
139         if (behind_bursting > 0)
140                 behind_bursting--;
141         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "FinishBurstInternal() %s behind_bursting %u", GetName().c_str(), behind_bursting);
142
143         if (!IsBehindBursting())
144         {
145                 SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
146                 SetPingFlag();
147         }
148         for (ChildServers::const_iterator i = Children.begin(); i != Children.end(); ++i)
149         {
150                 TreeServer* child = *i;
151                 child->FinishBurstInternal();
152         }
153 }
154
155 void TreeServer::FinishBurst()
156 {
157         ServerInstance->XLines->ApplyLines();
158         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
159         unsigned long bursttime = ts - this->StartBurst;
160         ServerInstance->SNO->WriteToSnoMask(Parent == Utils->TreeRoot ? 'l' : 'L', "Received end of netburst from \2%s\2 (burst time: %lu %s)",
161                 GetName().c_str(), (bursttime > 10000 ? bursttime / 1000 : bursttime), (bursttime > 10000 ? "secs" : "msecs"));
162         AddServerEvent(Utils->Creator, GetName());
163
164         StartBurst = 0;
165         FinishBurstInternal();
166 }
167
168 void TreeServer::SQuitChild(TreeServer* server, const std::string& reason)
169 {
170         DelServerEvent(Utils->Creator, server->GetName());
171         DelChild(server);
172
173         if (IsRoot())
174         {
175                 // Server split from us, generate a SQUIT message and broadcast it
176                 ServerInstance->SNO->WriteGlobalSno('l', "Server \002" + server->GetName() + "\002 split: " + reason);
177                 CmdBuilder("SQUIT").push(server->GetID()).push_last(reason).Broadcast();
178         }
179         else
180         {
181                 ServerInstance->SNO->WriteToSnoMask('L', "Server \002" + server->GetName() + "\002 split from server \002" + GetName() + "\002 with reason: " + reason);
182         }
183
184         unsigned int num_lost_servers = 0;
185         server->SQuitInternal(num_lost_servers);
186
187         const std::string quitreason = GetName() + " " + server->GetName();
188         unsigned int num_lost_users = QuitUsers(quitreason);
189
190         ServerInstance->SNO->WriteToSnoMask(IsRoot() ? 'l' : 'L', "Netsplit complete, lost \002%u\002 user%s on \002%u\002 server%s.",
191                 num_lost_users, num_lost_users != 1 ? "s" : "", num_lost_servers, num_lost_servers != 1 ? "s" : "");
192
193         // No-op if the socket is already closed (i.e. it called us)
194         if (server->IsLocal())
195                 server->GetSocket()->Close();
196
197         // Add the server to the cull list, the servers behind it are handled by cull() and the destructor
198         ServerInstance->GlobalCulls.AddItem(server);
199 }
200
201 void TreeServer::SQuitInternal(unsigned int& num_lost_servers)
202 {
203         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Server %s lost in split", GetName().c_str());
204
205         for (ChildServers::const_iterator i = Children.begin(); i != Children.end(); ++i)
206         {
207                 TreeServer* server = *i;
208                 server->SQuitInternal(num_lost_servers);
209         }
210
211         // Mark server as dead
212         isdead = true;
213         num_lost_servers++;
214         RemoveHash();
215 }
216
217 unsigned int TreeServer::QuitUsers(const std::string& reason)
218 {
219         std::string publicreason = ServerInstance->Config->HideSplits ? "*.net *.split" : reason;
220
221         const user_hash& users = ServerInstance->Users->GetUsers();
222         unsigned int original_size = users.size();
223         for (user_hash::const_iterator i = users.begin(); i != users.end(); )
224         {
225                 User* user = i->second;
226                 // Increment the iterator now because QuitUser() removes the user from the container
227                 ++i;
228                 TreeServer* server = TreeServer::Get(user);
229                 if (server->IsDead())
230                         ServerInstance->Users->QuitUser(user, publicreason, &reason);
231         }
232         return original_size - users.size();
233 }
234
235 void TreeServer::CheckULine()
236 {
237         uline = silentuline = false;
238
239         ConfigTagList tags = ServerInstance->Config->ConfTags("uline");
240         for (ConfigIter i = tags.first; i != tags.second; ++i)
241         {
242                 ConfigTag* tag = i->second;
243                 std::string server = tag->getString("server");
244                 if (!strcasecmp(server.c_str(), GetName().c_str()))
245                 {
246                         if (this->IsRoot())
247                         {
248                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Servers should not uline themselves (at " + tag->getTagLocation() + ")");
249                                 return;
250                         }
251
252                         uline = true;
253                         silentuline = tag->getBool("silent");
254                         break;
255                 }
256         }
257 }
258
259 /** This method is used to add the structure to the
260  * hash_map for linear searches. It is only called
261  * by the constructors.
262  */
263 void TreeServer::AddHashEntry()
264 {
265         Utils->serverlist[GetName()] = this;
266         Utils->sidlist[sid] = this;
267 }
268
269 /** These accessors etc should be pretty self-
270  * explanitory.
271  */
272 TreeServer* TreeServer::GetRoute()
273 {
274         return Route;
275 }
276
277 const std::string& TreeServer::GetVersion()
278 {
279         return VersionString;
280 }
281
282 void TreeServer::SetNextPingTime(time_t t)
283 {
284         this->NextPing = t;
285         LastPingWasGood = false;
286 }
287
288 time_t TreeServer::NextPingTime()
289 {
290         return NextPing;
291 }
292
293 bool TreeServer::AnsweredLastPing()
294 {
295         return LastPingWasGood;
296 }
297
298 void TreeServer::SetPingFlag()
299 {
300         LastPingWasGood = true;
301 }
302
303 TreeSocket* TreeServer::GetSocket()
304 {
305         return Socket;
306 }
307
308 TreeServer* TreeServer::GetParent()
309 {
310         return Parent;
311 }
312
313 void TreeServer::SetVersion(const std::string &Version)
314 {
315         VersionString = Version;
316 }
317
318 void TreeServer::AddChild(TreeServer* Child)
319 {
320         Children.push_back(Child);
321 }
322
323 bool TreeServer::DelChild(TreeServer* Child)
324 {
325         return stdalgo::erase(Children, Child);
326 }
327
328 CullResult TreeServer::cull()
329 {
330         // Recursively cull all servers that are under us in the tree
331         for (ChildServers::const_iterator i = Children.begin(); i != Children.end(); ++i)
332         {
333                 TreeServer* server = *i;
334                 server->cull();
335         }
336
337         if (!IsRoot())
338                 ServerUser->cull();
339         return classbase::cull();
340 }
341
342 TreeServer::~TreeServer()
343 {
344         // Recursively delete all servers that are under us in the tree first
345         for (ChildServers::const_iterator i = Children.begin(); i != Children.end(); ++i)
346                 delete *i;
347
348         // Delete server user unless it's us
349         if (!IsRoot())
350                 delete ServerUser;
351 }
352
353 void TreeServer::RemoveHash()
354 {
355         // XXX: Erase server from UserManager::uuidlist now, to allow sid reuse in the current main loop
356         // iteration, before the cull list is applied
357         ServerInstance->Users->uuidlist.erase(sid);
358
359         Utils->sidlist.erase(sid);
360         Utils->serverlist.erase(GetName());
361 }