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