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