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