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