1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __TREESOCKET_H__
15 #define __TREESOCKET_H__
17 #include "configreader.h"
21 #include "commands/cmd_whois.h"
22 #include "commands/cmd_stats.h"
27 #include "transport.h"
29 #include "m_spanningtree/utils.h"
32 * The server list in InspIRCd is maintained as two structures
33 * which hold the data in different ways. Most of the time, we
34 * want to very quicky obtain three pieces of information:
36 * (1) The information on a server
37 * (2) The information on the server we must send data through
38 * to actually REACH the server we're after
39 * (3) Potentially, the child/parent objects of this server
41 * The InspIRCd spanning protocol provides easy access to these
42 * by storing the data firstly in a recursive structure, where
43 * each item references its parent item, and a dynamic list
44 * of child items, and another structure which stores the items
45 * hashed, linearly. This means that if we want to find a server
46 * by name quickly, we can look it up in the hash, avoiding
47 * any O(n) lookups. If however, during a split or sync, we want
48 * to apply an operation to a server, and any of its child objects
49 * we can resort to recursion to walk the tree structure.
50 * Any socket can have one of five states at any one time.
51 * The LISTENER state indicates a socket which is listening
52 * for connections. It cannot receive data itself, only incoming
54 * The CONNECTING state indicates an outbound socket which is
55 * waiting to be writeable.
56 * The WAIT_AUTH_1 state indicates the socket is outbound and
57 * has successfully connected, but has not yet sent and received
59 * The WAIT_AUTH_2 state indicates that the socket is inbound
60 * (allocated by a LISTENER) but has not yet sent and received
62 * The CONNECTED state represents a fully authorized, fully
65 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
67 /** Every SERVER connection inbound or outbound is represented by
68 * an object of type TreeSocket.
69 * TreeSockets, being inherited from InspSocket, can be tied into
70 * the core socket engine, and we cn therefore receive activity events
71 * for them, just like activex objects on speed. (yes really, that
72 * is a technical term!) Each of these which relates to a locally
73 * connected server is assocated with it, by hooking it onto a
74 * TreeSocket class using its constructor. In this way, we can
75 * maintain a list of servers, some of which are directly connected,
76 * some of which are not.
78 class TreeSocket : public InspSocket
80 SpanningTreeUtilities* Utils; /* Utility class */
81 std::string myhost; /* Canonical hostname */
82 std::string in_buffer; /* Input buffer */
83 ServerState LinkState; /* Link state */
84 std::string InboundServerName; /* Server name sent to us by other side */
85 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
86 int num_lost_users; /* Users lost in split */
87 int num_lost_servers; /* Servers lost in split */
88 time_t NextPing; /* Time when we are due to ping this server */
89 bool LastPingWasGood; /* Responded to last ping we sent? */
90 bool bursting; /* True if not finished bursting yet */
91 unsigned int keylength; /* Is this still used? */
92 std::string ModuleList; /* Module list of other server from CAPAB */
93 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
94 Module* Hook; /* I/O hooking module that we're attached to for this socket */
98 /** Because most of the I/O gubbins are encapsulated within
99 * InspSocket, we just call the superclass constructor for
100 * most of the action, and append a few of our own values
103 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, Module* HookMod = NULL);
105 /** Because most of the I/O gubbins are encapsulated within
106 * InspSocket, we just call the superclass constructor for
107 * most of the action, and append a few of our own values
110 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Module* HookMod = NULL);
112 /** When a listening socket gives us a new file descriptor,
113 * we must associate it with a socket without creating a new
114 * connection. This constructor is used for this purpose.
116 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
120 ServerState GetLinkState();
122 /** Return the module which we are hooking to for I/O encapsulation
130 /** When an outbound connection finishes connecting, we receive
131 * this event, and must send our SERVER string to the other
132 * side. If the other side is happy, as outlined in the server
133 * to server docs on the inspircd.org site, the other side
134 * will then send back its own server string.
136 virtual bool OnConnected();
138 /** Handle socket error event
140 virtual void OnError(InspSocketError e);
142 /** Handle socket disconnect event
144 virtual int OnDisconnect();
146 /** Recursively send the server tree with distances as hops.
147 * This is used during network burst to inform the other server
148 * (and any of ITS servers too) of what servers we know about.
149 * If at any point any of these servers already exist on the other
150 * end, our connection may be terminated. The hopcounts given
151 * by this function are relative, this doesn't matter so long as
152 * they are all >1, as all the remote servers re-calculate them
153 * to be relative too, with themselves as hop 0.
155 void SendServers(TreeServer* Current, TreeServer* s, int hops);
157 /** Returns my capabilities as a string
159 std::string MyCapabilities();
161 /** Send my capabilities to the remote side
163 void SendCapabilities();
165 /* Check a comma seperated list for an item */
166 bool HasItem(const std::string &list, const std::string &item);
168 /* Isolate and return the elements that are different between two comma seperated lists */
169 std::string ListDifference(const std::string &one, const std::string &two);
171 bool Capab(const std::deque<std::string> ¶ms);
173 /** This function forces this server to quit, removing this server
174 * and any users on it (and servers and users below that, etc etc).
175 * It's very slow and pretty clunky, but luckily unless your network
176 * is having a REAL bad hair day, this function shouldnt be called
177 * too many times a month ;-)
179 void SquitServer(std::string &from, TreeServer* Current);
181 /** This is a wrapper function for SquitServer above, which
182 * does some validation first and passes on the SQUIT to all
183 * other remaining servers.
185 void Squit(TreeServer* Current, const std::string &reason);
187 /** FMODE command - server mode with timestamp checks */
188 bool ForceMode(const std::string &source, std::deque<std::string> ¶ms);
190 /** FTOPIC command */
191 bool ForceTopic(const std::string &source, std::deque<std::string> ¶ms);
193 /** FJOIN, similar to TS6 SJOIN, but not quite. */
194 bool ForceJoin(const std::string &source, std::deque<std::string> ¶ms);
197 bool IntroduceClient(const std::string &source, std::deque<std::string> ¶ms);
199 /** Send one or more FJOINs for a channel of users.
200 * If the length of a single line is more than 480-NICKMAX
201 * in length, it is split over multiple lines.
203 void SendFJoins(TreeServer* Current, chanrec* c);
205 /** Send G, Q, Z and E lines */
206 void SendXLines(TreeServer* Current);
208 /** Send channel modes and topics */
209 void SendChannelModes(TreeServer* Current);
211 /** send all users and their oper state/modes */
212 void SendUsers(TreeServer* Current);
214 /** This function is called when we want to send a netburst to a local
215 * server. There is a set order we must do this, because for example
216 * users require their servers to exist, and channels require their
217 * users to exist. You get the idea.
219 void DoBurst(TreeServer* s);
221 /** This function is called when we receive data from a remote
222 * server. We buffer the data in a std::string (it doesnt stay
223 * there for long), reading using InspSocket::Read() which can
224 * read up to 16 kilobytes in one operation.
226 * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
227 * THE SOCKET OBJECT FOR US.
229 virtual bool OnDataReady();
231 /** Send one or more complete lines down the socket
233 int WriteLine(std::string line);
235 /** Handle ERROR command */
236 bool Error(std::deque<std::string> ¶ms);
238 /** remote MOTD. leet, huh? */
239 bool Motd(const std::string &prefix, std::deque<std::string> ¶ms);
241 /** remote ADMIN. leet, huh? */
242 bool Admin(const std::string &prefix, std::deque<std::string> ¶ms);
244 /** Remote MODULES */
245 bool Modules(const std::string &prefix, std::deque<std::string> ¶ms);
247 bool Stats(const std::string &prefix, std::deque<std::string> ¶ms);
249 /** Because the core won't let users or even SERVERS set +o,
250 * we use the OPERTYPE command to do this.
252 bool OperType(const std::string &prefix, std::deque<std::string> ¶ms);
254 /** Because Andy insists that services-compatible servers must
255 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
257 bool ForceNick(const std::string &prefix, std::deque<std::string> ¶ms);
259 bool OperQuit(const std::string &prefix, std::deque<std::string> ¶ms);
261 /** Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
262 * then let that server do the dirty work (squit it!). Example:
263 * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
265 bool RemoteSquit(const std::string &prefix, std::deque<std::string> ¶ms);
269 bool ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms);
273 bool RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms);
277 bool RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms);
281 bool LocalPong(const std::string &prefix, std::deque<std::string> ¶ms);
285 bool MetaData(const std::string &prefix, std::deque<std::string> ¶ms);
289 bool ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms);
293 bool ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms);
297 bool AddLine(const std::string &prefix, std::deque<std::string> ¶ms);
301 bool ChangeName(const std::string &prefix, std::deque<std::string> ¶ms);
305 bool Whois(const std::string &prefix, std::deque<std::string> ¶ms);
309 bool Push(const std::string &prefix, std::deque<std::string> ¶ms);
313 bool HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms);
317 bool Time(const std::string &prefix, std::deque<std::string> ¶ms);
321 bool LocalPing(const std::string &prefix, std::deque<std::string> ¶ms);
323 /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
324 * This does not update the timestamp of the target channel, this must be done seperately.
326 bool RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms);
328 /** <- (remote) <- SERVER
330 bool RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms);
332 /** (local) -> SERVER
334 bool Outbound_Reply_Server(std::deque<std::string> ¶ms);
336 /** (local) <- SERVER
338 bool Inbound_Server(std::deque<std::string> ¶ms);
342 void Split(const std::string &line, std::deque<std::string> &n);
344 /** Process complete line from buffer
346 bool ProcessLine(std::string &line);
348 /** Get this server's name
350 virtual std::string GetName();
352 /** Handle socket timeout from connect()
354 virtual void OnTimeout();
356 /** Handle socket close event
358 virtual void OnClose();
360 /** Handle incoming connection event
362 virtual int OnIncomingConnection(int newsock, char* ip);