2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5 * Copyright (C) 2013, 2017, 2019 Sadie Powell <sadie@witchery.services>
6 * Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
7 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
10 * Copyright (C) 2007, 2010 Craig Edwards <brain@inspircd.org>
11 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
13 * This file is part of InspIRCd. InspIRCd is free software: you can
14 * redistribute it and/or modify it under the terms of the GNU General Public
15 * License as published by the Free Software Foundation, version 2.
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #include "treesocket.h"
30 #include "pingtimer.h"
32 /** Each server in the tree is represented by one class of
33 * type TreeServer. A locally connected TreeServer can
34 * have a class of type TreeSocket associated with it, for
35 * remote servers, the TreeSocket entry will be NULL.
36 * Each server also maintains a pointer to its parent
37 * (NULL if this server is ours, at the top of the tree)
38 * and a pointer to its "Route" (see the comments in the
39 * constructors below), and also a dynamic list of pointers
40 * to its children which can be iterated recursively
41 * if required. Creating or deleting objects of type
42 i* TreeServer automatically maintains the hash_map of
43 * TreeServer items, deleting and inserting them as they
44 * are created and destroyed.
46 class TreeServer : public Server
48 TreeServer* Parent; /* Parent entry */
49 TreeServer* Route; /* Route entry */
50 std::vector<TreeServer*> Children; /* List of child objects */
51 std::string VersionString; /* Version string or empty string */
53 /** Full version string including patch version and other info
55 std::string fullversion;
56 std::string rawversion;
58 TreeSocket* Socket; /* Socket used to communicate with this server */
60 /** Counter counting how many servers are bursting in front of this server, including
61 * this server. Set to parents' value on construction then it is increased if the
62 * server itself starts bursting. Decreased when a server on the path to this server
65 unsigned int behind_bursting;
67 /** True if this server has been lost in a split and is awaiting destruction
71 /** Timer handling PINGing the server and killing it on timeout
75 /** This method is used to add this TreeServer to the
76 * hash maps. It is only called by the constructors.
80 /** Used by SQuit logic to recursively remove servers
82 void SQuitInternal(unsigned int& num_lost_servers, bool error);
84 /** Remove the reference to this server from the hash maps
89 typedef std::vector<TreeServer*> ChildServers;
90 FakeUser* const ServerUser; /* User representing this server */
93 unsigned int UserCount; /* How many users are on this server? [note: doesn't care about +i] */
94 unsigned int OperCount; /* How many opers are on this server? */
96 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
97 * represents our own server. Therefore, it has no route, no parent, and
98 * no socket associated with it. Its version string is our own local version.
102 /** When we create a new server, we call this constructor to initialize it.
103 * This constructor initializes the server's Route and Parent, and sets up
104 * its ping counters so that it will be pinged one minute from now.
106 TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide);
108 /** SQuit a server connected to this server, removing the given server and all servers behind it
109 * @param server Server to squit, must be directly below this server
110 * @param reason Reason for quitting the server, sent to opers and other servers
111 * @param error Whether the server is being squit because of an error.
113 void SQuitChild(TreeServer* server, const std::string& reason, bool error = false);
115 /** SQuit this server, removing this server and all servers behind it
116 * @param reason Reason for quitting the server, sent to opers and other servers
117 * @param error Whether the server is being squit because of an error.
119 void SQuit(const std::string& reason, bool error = false)
121 GetParent()->SQuitChild(this, reason, error);
124 static unsigned int QuitUsers(const std::string& reason);
127 * The 'route' is defined as the locally-
128 * connected server which can be used to reach this server.
130 TreeServer* GetRoute() const { return Route; }
132 /** Returns true if this server is the tree root (i.e.: us)
134 bool IsRoot() const { return (this->Parent == NULL); }
136 /** Returns true if this server is locally connected
138 bool IsLocal() const { return (this->Route == this); }
140 /** Returns true if the server is awaiting destruction
141 * @return True if the server is waiting to be culled and deleted, false otherwise
143 bool IsDead() const { return isdead; }
145 /** Get server version string
147 const std::string& GetVersion() const { return VersionString; }
149 /** Get the full version string of this server
150 * @return The full version string of this server, including patch version and other info
152 const std::string& GetFullVersion() const { return fullversion; }
154 /** Get the raw version string of this server
156 const std::string& GetRawVersion() const { return rawversion; }
158 /** Round trip time of last ping
162 /** When we received BURST from this server, used to calculate total burst time at ENDBURST.
166 /** True if this server is hidden
170 /** Get the TreeSocket pointer for local servers.
171 * For remote servers, this returns NULL.
173 TreeSocket* GetSocket() const { return Socket; }
175 /** Get the parent server.
176 * For the root node, this returns NULL.
178 TreeServer* GetParent() const { return Parent; }
180 /** Set the server version string
182 void SetVersion(const std::string& verstr) { VersionString = verstr; }
184 /** Set the full version string
185 * @param verstr The version string to set
187 void SetFullVersion(const std::string& verstr) { fullversion = verstr; }
189 /** Set the raw version string
191 void SetRawVersion(const std::string& verstr) { rawversion = verstr; }
193 /** Sets the description of this server. Called when the description of a remote server changes
194 * and we are notified about it.
195 * @param descstr The description to set
197 void SetDesc(const std::string& descstr) { description = descstr; }
199 /** Return all child servers
201 const ChildServers& GetChildren() const { return Children; }
203 /** Marks a server as having finished bursting and performs appropriate actions.
206 /** Recursive call for child servers */
207 void FinishBurstInternal();
209 /** (Re)check the uline state of this server
213 /** Get the bursting state of this server
214 * @return True if this server is bursting, false if it isn't
216 bool IsBursting() const { return (StartBurst != 0); }
218 /** Check whether this server is behind a bursting server or is itself bursting.
219 * This can tell whether a user is on a part of the network that is still bursting.
220 * @return True if this server is bursting or is behind a server that is bursting, false if it isn't
222 bool IsBehindBursting() const { return (behind_bursting != 0); }
224 /** Set the bursting state of the server
225 * @param startms Time the server started bursting, if 0 or omitted, use current time
227 void BeginBurst(uint64_t startms = 0);
229 /** Register a PONG from the server
231 void OnPong() { pingtimer.OnPong(); }
233 CullResult cull() CXX11_OVERRIDE;
235 /** Destructor, deletes ServerUser unless IsRoot()
239 /** Returns the TreeServer the given user is connected to
240 * @param user The user whose server to return
241 * @return The TreeServer this user is connected to.
243 static TreeServer* Get(User* user)
245 return static_cast<TreeServer*>(user->server);