]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
9a661416a76889753d67afef4657d7a661520d5c
[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()), ServerUser(ServerInstance->FakeClient)
41         , age(ServerInstance->Time()), Warned(false), UserCount(ServerInstance->Users.GetLocalUsers().size())
42         , OperCount(0), rtt(0), StartBurst(0), Hidden(false)
43 {
44         AddHashEntry();
45 }
46
47 /** When we create a new server, we call this constructor to initialize it.
48  * This constructor initializes the server's Route and Parent, and sets up
49  * its ping counters so that it will be pinged one minute from now.
50  */
51 TreeServer::TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide)
52         : Server(Name, Desc)
53         , Parent(Above), Socket(Sock), sid(id), ServerUser(new FakeUser(id, this))
54         , age(ServerInstance->Time()), Warned(false), UserCount(0), OperCount(0), rtt(0), Hidden(Hide)
55 {
56         CheckULine();
57         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
58         SetPingFlag();
59
60         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
61         this->StartBurst = ts;
62         ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Server %s started bursting at time %lu", sid.c_str(), ts);
63
64         /* find the 'route' for this server (e.g. the one directly connected
65          * to the local server, which we can use to reach it)
66          *
67          * In the following example, consider we have just added a TreeServer
68          * class for server G on our network, of which we are server A.
69          * To route traffic to G (marked with a *) we must send the data to
70          * B (marked with a +) so this algorithm initializes the 'Route'
71          * value to point at whichever server traffic must be routed through
72          * to get here. If we were to try this algorithm with server B,
73          * the Route pointer would point at its own object ('this').
74          *
75          *            A
76          *           / \
77          *        + B   C
78          *         / \   \
79          *        D   E   F
80          *       /         \
81          *    * G           H
82          *
83          * We only run this algorithm when a server is created, as
84          * the routes remain constant while ever the server exists, and
85          * do not need to be re-calculated.
86          */
87
88         Route = Above;
89         if (Route == Utils->TreeRoot)
90         {
91                 Route = this;
92         }
93         else
94         {
95                 while (this->Route->GetParent() != Utils->TreeRoot)
96                 {
97                         this->Route = Route->GetParent();
98                 }
99         }
100
101         /* Because recursive code is slow and takes a lot of resources,
102          * we store two representations of the server tree. The first
103          * is a recursive structure where each server references its
104          * children and its parent, which is used for netbursts and
105          * netsplits to dump the whole dataset to the other server,
106          * and the second is used for very fast lookups when routing
107          * messages and is instead a hash_map, where each item can
108          * be referenced by its server name. The AddHashEntry()
109          * call below automatically inserts each TreeServer class
110          * into the hash_map as it is created. There is a similar
111          * maintainance call in the destructor to tidy up deleted
112          * servers.
113          */
114
115         this->AddHashEntry();
116         Parent->AddChild(this);
117 }
118
119 const std::string& TreeServer::GetID()
120 {
121         return sid;
122 }
123
124 void TreeServer::FinishBurstInternal()
125 {
126         StartBurst = 0;
127         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
128         SetPingFlag();
129         for (ChildServers::const_iterator i = Children.begin(); i != Children.end(); ++i)
130         {
131                 TreeServer* child = *i;
132                 child->FinishBurstInternal();
133         }
134 }
135
136 void TreeServer::FinishBurst()
137 {
138         ServerInstance->XLines->ApplyLines();
139         long ts = ServerInstance->Time() * 1000 + (ServerInstance->Time_ns() / 1000000);
140         unsigned long bursttime = ts - this->StartBurst;
141         ServerInstance->SNO->WriteToSnoMask(Parent == Utils->TreeRoot ? 'l' : 'L', "Received end of netburst from \2%s\2 (burst time: %lu %s)",
142                 GetName().c_str(), (bursttime > 10000 ? bursttime / 1000 : bursttime), (bursttime > 10000 ? "secs" : "msecs"));
143         AddServerEvent(Utils->Creator, GetName());
144
145         FinishBurstInternal();
146 }
147
148 int TreeServer::QuitUsers(const std::string &reason)
149 {
150         std::string publicreason = ServerInstance->Config->HideSplits ? "*.net *.split" : reason;
151
152         const user_hash& users = ServerInstance->Users->GetUsers();
153         unsigned int original_size = users.size();
154         for (user_hash::const_iterator i = users.begin(); i != users.end(); )
155         {
156                 User* user = i->second;
157                 // Increment the iterator now because QuitUser() removes the user from the container
158                 ++i;
159                 if (user->server == this)
160                         ServerInstance->Users->QuitUser(user, publicreason, &reason);
161         }
162         return original_size - users.size();
163 }
164
165 void TreeServer::CheckULine()
166 {
167         uline = silentuline = false;
168
169         ConfigTagList tags = ServerInstance->Config->ConfTags("uline");
170         for (ConfigIter i = tags.first; i != tags.second; ++i)
171         {
172                 ConfigTag* tag = i->second;
173                 std::string server = tag->getString("server");
174                 if (!strcasecmp(server.c_str(), GetName().c_str()))
175                 {
176                         if (this->IsRoot())
177                         {
178                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Servers should not uline themselves (at " + tag->getTagLocation() + ")");
179                                 return;
180                         }
181
182                         uline = true;
183                         silentuline = tag->getBool("silent");
184                         break;
185                 }
186         }
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         Utils->serverlist[GetName()] = this;
196         Utils->sidlist[sid] = this;
197 }
198
199 /** These accessors etc should be pretty self-
200  * explanitory.
201  */
202 TreeServer* TreeServer::GetRoute()
203 {
204         return Route;
205 }
206
207 const std::string& TreeServer::GetVersion()
208 {
209         return VersionString;
210 }
211
212 void TreeServer::SetNextPingTime(time_t t)
213 {
214         this->NextPing = t;
215         LastPingWasGood = false;
216 }
217
218 time_t TreeServer::NextPingTime()
219 {
220         return NextPing;
221 }
222
223 bool TreeServer::AnsweredLastPing()
224 {
225         return LastPingWasGood;
226 }
227
228 void TreeServer::SetPingFlag()
229 {
230         LastPingWasGood = true;
231 }
232
233 TreeSocket* TreeServer::GetSocket()
234 {
235         return Socket;
236 }
237
238 TreeServer* TreeServer::GetParent()
239 {
240         return Parent;
241 }
242
243 void TreeServer::SetVersion(const std::string &Version)
244 {
245         VersionString = Version;
246 }
247
248 void TreeServer::AddChild(TreeServer* Child)
249 {
250         Children.push_back(Child);
251 }
252
253 bool TreeServer::DelChild(TreeServer* Child)
254 {
255         std::vector<TreeServer*>::iterator it = std::find(Children.begin(), Children.end(), Child);
256         if (it != Children.end())
257         {
258                 Children.erase(it);
259                 return true;
260         }
261         return false;
262 }
263
264 /** Removes child nodes of this node, and of that node, etc etc.
265  * This is used during netsplits to automatically tidy up the
266  * server tree. It is slow, we don't use it for much else.
267  */
268 void TreeServer::Tidy()
269 {
270         while (1)
271         {
272                 std::vector<TreeServer*>::iterator a = Children.begin();
273                 if (a == Children.end())
274                         return;
275                 TreeServer* s = *a;
276                 s->Tidy();
277                 s->cull();
278                 Children.erase(a);
279                 delete s;
280         }
281 }
282
283 CullResult TreeServer::cull()
284 {
285         if (!IsRoot())
286                 ServerUser->cull();
287         return classbase::cull();
288 }
289
290 TreeServer::~TreeServer()
291 {
292         /* We'd better tidy up after ourselves, eh? */
293         if (!IsRoot())
294                 delete ServerUser;
295
296         Utils->sidlist.erase(sid);
297         Utils->serverlist.erase(GetName());
298 }