1 /* 2 * InspIRCd -- Internet Relay Chat Daemon 3 * 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> 12 * 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. 16 * 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 20 * details. 21 * 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/>. 24 */ 25 26 27 #pragma once 28 29 #include "treesocket.h" 30 #include "pingtimer.h" 31 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. 45 */ 46 class TreeServer : public Server 47 { 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 */ 52 53 /** Full version string including patch version and other info 54 */ 55 std::string fullversion; 56 std::string rawversion; 57 58 TreeSocket* Socket; /* Socket used to communicate with this server */ 59 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 63 * finishes burst. 64 */ 65 unsigned int behind_bursting; 66 67 /** True if this server has been lost in a split and is awaiting destruction 68 */ 69 bool isdead; 70 71 /** Timer handling PINGing the server and killing it on timeout 72 */ 73 PingTimer pingtimer; 74 75 /** This method is used to add this TreeServer to the 76 * hash maps. It is only called by the constructors. 77 */ 78 void AddHashEntry(); 79 80 /** Used by SQuit logic to recursively remove servers 81 */ 82 void SQuitInternal(unsigned int& num_lost_servers, bool error); 83 84 /** Remove the reference to this server from the hash maps 85 */ 86 void RemoveHash(); 87 88 public: 89 typedef std::vector<TreeServer*> ChildServers; 90 FakeUser* const ServerUser; /* User representing this server */ 91 const time_t age; 92 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? */ 95 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. 99 */ 100 TreeServer(); 101 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. 105 */ 106 TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide); 107 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. 112 */ 113 void SQuitChild(TreeServer* server, const std::string& reason, bool error = false); 114 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. 118 */ 119 void SQuit(const std::string& reason, bool error = false) 120 { 121 GetParent()->SQuitChild(this, reason, error); 122 } 123 124 static unsigned int QuitUsers(const std::string& reason); 125 126 /** Get route. 127 * The 'route' is defined as the locally- 128 * connected server which can be used to reach this server. 129 */ GetRoute()130 TreeServer* GetRoute() const { return Route; } 131 132 /** Returns true if this server is the tree root (i.e.: us) 133 */ IsRoot()134 bool IsRoot() const { return (this->Parent == NULL); } 135 136 /** Returns true if this server is locally connected 137 */ IsLocal()138 bool IsLocal() const { return (this->Route == this); } 139 140 /** Returns true if the server is awaiting destruction 141 * @return True if the server is waiting to be culled and deleted, false otherwise 142 */ IsDead()143 bool IsDead() const { return isdead; } 144 145 /** Get server version string 146 */ GetVersion()147 const std::string& GetVersion() const { return VersionString; } 148 149 /** Get the full version string of this server 150 * @return The full version string of this server, including patch version and other info 151 */ GetFullVersion()152 const std::string& GetFullVersion() const { return fullversion; } 153 154 /** Get the raw version string of this server 155 */ GetRawVersion()156 const std::string& GetRawVersion() const { return rawversion; } 157 158 /** Round trip time of last ping 159 */ 160 unsigned long rtt; 161 162 /** When we received BURST from this server, used to calculate total burst time at ENDBURST. 163 */ 164 uint64_t StartBurst; 165 166 /** True if this server is hidden 167 */ 168 bool Hidden; 169 170 /** Get the TreeSocket pointer for local servers. 171 * For remote servers, this returns NULL. 172 */ GetSocket()173 TreeSocket* GetSocket() const { return Socket; } 174 175 /** Get the parent server. 176 * For the root node, this returns NULL. 177 */ GetParent()178 TreeServer* GetParent() const { return Parent; } 179 180 /** Set the server version string 181 */ SetVersion(const std::string & verstr)182 void SetVersion(const std::string& verstr) { VersionString = verstr; } 183 184 /** Set the full version string 185 * @param verstr The version string to set 186 */ SetFullVersion(const std::string & verstr)187 void SetFullVersion(const std::string& verstr) { fullversion = verstr; } 188 189 /** Set the raw version string 190 */ SetRawVersion(const std::string & verstr)191 void SetRawVersion(const std::string& verstr) { rawversion = verstr; } 192 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 196 */ SetDesc(const std::string & descstr)197 void SetDesc(const std::string& descstr) { description = descstr; } 198 199 /** Return all child servers 200 */ GetChildren()201 const ChildServers& GetChildren() const { return Children; } 202 203 /** Marks a server as having finished bursting and performs appropriate actions. 204 */ 205 void FinishBurst(); 206 /** Recursive call for child servers */ 207 void FinishBurstInternal(); 208 209 /** (Re)check the uline state of this server 210 */ 211 void CheckULine(); 212 213 /** Get the bursting state of this server 214 * @return True if this server is bursting, false if it isn't 215 */ IsBursting()216 bool IsBursting() const { return (StartBurst != 0); } 217 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 221 */ IsBehindBursting()222 bool IsBehindBursting() const { return (behind_bursting != 0); } 223 224 /** Set the bursting state of the server 225 * @param startms Time the server started bursting, if 0 or omitted, use current time 226 */ 227 void BeginBurst(uint64_t startms = 0); 228 229 /** Register a PONG from the server 230 */ OnPong()231 void OnPong() { pingtimer.OnPong(); } 232 233 CullResult cull() CXX11_OVERRIDE; 234 235 /** Destructor, deletes ServerUser unless IsRoot() 236 */ 237 ~TreeServer(); 238 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. 242 */ Get(User * user)243 static TreeServer* Get(User* user) 244 { 245 return static_cast<TreeServer*>(user->server); 246 } 247 }; 248