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