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