]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
c9e65b2142355a926fce7f8afa2a8099c0750649
[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                         userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
125         }
126         return time_to_die.size();
127 }
128
129 /** This method is used to add the structure to the
130  * hash_map for linear searches. It is only called
131  * by the constructors.
132  */
133 void TreeServer::AddHashEntry()
134 {
135         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
136         if (iter == Utils->serverlist.end())
137                 Utils->serverlist[this->ServerName.c_str()] = this;
138 }
139
140 /** This method removes the reference to this object
141  * from the hash_map which is used for linear searches.
142  * It is only called by the default destructor.
143  */
144 void TreeServer::DelHashEntry()
145 {
146         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
147         if (iter != Utils->serverlist.end())
148                 Utils->serverlist.erase(iter);
149 }
150
151 /** These accessors etc should be pretty self-
152  * explanitory.
153  */
154 TreeServer* TreeServer::GetRoute()
155 {
156         return Route;
157 }
158
159 std::string TreeServer::GetName()
160 {
161         return ServerName.c_str();
162 }
163
164 std::string TreeServer::GetDesc()
165 {
166         return ServerDesc;
167 }
168
169 std::string TreeServer::GetVersion()
170 {
171         return VersionString;
172 }
173
174 void TreeServer::SetNextPingTime(time_t t)
175 {
176         this->NextPing = t;
177         LastPingWasGood = false;
178 }
179
180 time_t TreeServer::NextPingTime()
181 {
182         return NextPing;
183 }
184
185 bool TreeServer::AnsweredLastPing()
186 {
187         return LastPingWasGood;
188 }
189
190 void TreeServer::SetPingFlag()
191 {
192         LastPingWasGood = true;
193 }
194
195 int TreeServer::GetUserCount()
196 {
197         return UserCount;
198 }
199
200 void TreeServer::AddUserCount()
201 {
202         UserCount++;
203 }
204
205 void TreeServer::DelUserCount()
206 {
207         UserCount--;
208 }
209
210 int TreeServer::GetOperCount()
211 {
212         return OperCount;
213 }
214
215 TreeSocket* TreeServer::GetSocket()
216 {
217         return Socket;
218 }
219
220 TreeServer* TreeServer::GetParent()
221 {
222         return Parent;
223 }
224
225 void TreeServer::SetVersion(const std::string &Version)
226 {
227         VersionString = Version;
228 }
229
230 unsigned int TreeServer::ChildCount()
231 {
232         return Children.size();
233 }
234
235 TreeServer* TreeServer::GetChild(unsigned int n)
236 {
237         if (n < Children.size())
238         {
239                 /* Make sure they  cant request
240                  * an out-of-range object. After
241                  * all we know what these programmer
242                  * types are like *grin*.
243                  */
244                 return Children[n];
245         }
246         else
247         {
248                 return NULL;
249         }
250 }
251
252 void TreeServer::AddChild(TreeServer* Child)
253 {
254         Children.push_back(Child);
255 }
256
257 bool TreeServer::DelChild(TreeServer* Child)
258 {
259         for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
260         {
261                 if (*a == Child)
262                 {
263                         Children.erase(a);
264                         return true;
265                 }
266         }
267         return false;
268 }
269
270 /** Removes child nodes of this node, and of that node, etc etc.
271  * This is used during netsplits to automatically tidy up the
272  * server tree. It is slow, we don't use it for much else.
273  */
274 bool TreeServer::Tidy()
275 {
276         bool stillchildren = true;
277         while (stillchildren)
278         {
279                 stillchildren = false;
280                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
281                 {
282                         TreeServer* s = (TreeServer*)*a;
283                         s->Tidy();
284                         Children.erase(a);
285                         DELETE(s);
286                         stillchildren = true;
287                         break;
288                 }
289         }
290         return true;
291 }
292
293 TreeServer::~TreeServer()
294 {
295         /* We'd better tidy up after ourselves, eh? */
296         this->DelHashEntry();
297 }
298
299