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