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 "commands/cmd_whois.h"
18 #include "commands/cmd_stats.h"
23 #include "transport.h"
25 #include "m_spanningtree/utils.h"
28 * The server list in InspIRCd is maintained as two structures
29 * which hold the data in different ways. Most of the time, we
30 * want to very quicky obtain three pieces of information:
32 * (1) The information on a server
33 * (2) The information on the server we must send data through
34 * to actually REACH the server we're after
35 * (3) Potentially, the child/parent objects of this server
37 * The InspIRCd spanning protocol provides easy access to these
38 * by storing the data firstly in a recursive structure, where
39 * each item references its parent item, and a dynamic list
40 * of child items, and another structure which stores the items
41 * hashed, linearly. This means that if we want to find a server
42 * by name quickly, we can look it up in the hash, avoiding
43 * any O(n) lookups. If however, during a split or sync, we want
44 * to apply an operation to a server, and any of its child objects
45 * we can resort to recursion to walk the tree structure.
46 * Any socket can have one of five states at any one time.
47 * The LISTENER state indicates a socket which is listening
48 * for connections. It cannot receive data itself, only incoming
50 * The CONNECTING state indicates an outbound socket which is
51 * waiting to be writeable.
52 * The WAIT_AUTH_1 state indicates the socket is outbound and
53 * has successfully connected, but has not yet sent and received
55 * The WAIT_AUTH_2 state indicates that the socket is inbound
56 * (allocated by a LISTENER) but has not yet sent and received
58 * The CONNECTED state represents a fully authorized, fully
61 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
63 /** Every SERVER connection inbound or outbound is represented by
64 * an object of type TreeSocket.
65 * TreeSockets, being inherited from InspSocket, can be tied into
66 * the core socket engine, and we cn therefore receive activity events
67 * for them, just like activex objects on speed. (yes really, that
68 * is a technical term!) Each of these which relates to a locally
69 * connected server is assocated with it, by hooking it onto a
70 * TreeSocket class using its constructor. In this way, we can
71 * maintain a list of servers, some of which are directly connected,
72 * some of which are not.
74 class TreeSocket : public InspSocket
76 SpanningTreeUtilities* Utils; /* Utility class */
77 std::string myhost; /* Canonical hostname */
78 std::string in_buffer; /* Input buffer */
79 ServerState LinkState; /* Link state */
80 std::string InboundServerName; /* Server name sent to us by other side */
81 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
82 std::string InboundSID; /* Server ID sent to us by the other side */
83 int num_lost_users; /* Users lost in split */
84 int num_lost_servers; /* Servers lost in split */
85 time_t NextPing; /* Time when we are due to ping this server */
86 bool LastPingWasGood; /* Responded to last ping we sent? */
87 bool bursting; /* True if not finished bursting yet */
88 unsigned int keylength; /* Is this still used? */
89 std::string ModuleList; /* Module list of other server from CAPAB */
90 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
91 Module* Hook; /* I/O hooking module that we're attached to for this socket */
92 std::string ourchallenge; /* Challenge sent for challenge/response */
93 std::string theirchallenge; /* Challenge recv for challenge/response */
94 std::string OutboundPass; /* Outbound password */
95 bool sentcapab; /* Have sent CAPAB already */
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 /** Get challenge set in our CAPAB for challenge/response
124 const std::string& GetOurChallenge();
126 /** Get challenge set in our CAPAB for challenge/response
128 void SetOurChallenge(const std::string &c);
130 /** Get challenge set in their CAPAB for challenge/response
132 const std::string& GetTheirChallenge();
134 /** Get challenge set in their CAPAB for challenge/response
136 void SetTheirChallenge(const std::string &c);
138 /** Compare two passwords based on authentication scheme
140 bool ComparePass(const std::string &ours, const std::string &theirs);
142 /** Return the module which we are hooking to for I/O encapsulation
150 /** Generate random string used for challenge-response auth
152 std::string RandString(unsigned int length);
154 /** Construct a password, optionally hashed with the other side's
157 std::string MakePass(const std::string &password, const std::string &challenge);
159 /** When an outbound connection finishes connecting, we receive
160 * this event, and must send our SERVER string to the other
161 * side. If the other side is happy, as outlined in the server
162 * to server docs on the inspircd.org site, the other side
163 * will then send back its own server string.
165 virtual bool OnConnected();
167 /** Handle socket error event
169 virtual void OnError(InspSocketError e);
171 /** Sends an error to the remote server, and displays it locally to show
174 void SendError(const std::string &errormessage);
176 /** Handle socket disconnect event
178 virtual int OnDisconnect();
180 /** Recursively send the server tree with distances as hops.
181 * This is used during network burst to inform the other server
182 * (and any of ITS servers too) of what servers we know about.
183 * If at any point any of these servers already exist on the other
184 * end, our connection may be terminated. The hopcounts given
185 * by this function are relative, this doesn't matter so long as
186 * they are all >1, as all the remote servers re-calculate them
187 * to be relative too, with themselves as hop 0.
189 void SendServers(TreeServer* Current, TreeServer* s, int hops);
191 /** Returns my capabilities as a string
193 std::string MyCapabilities();
195 /** Send my capabilities to the remote side
197 void SendCapabilities();
199 /* Check a comma seperated list for an item */
200 bool HasItem(const std::string &list, const std::string &item);
202 /* Isolate and return the elements that are different between two comma seperated lists */
203 std::string ListDifference(const std::string &one, const std::string &two);
205 bool Capab(const std::deque<std::string> ¶ms);
207 /** This function forces this server to quit, removing this server
208 * and any users on it (and servers and users below that, etc etc).
209 * It's very slow and pretty clunky, but luckily unless your network
210 * is having a REAL bad hair day, this function shouldnt be called
211 * too many times a month ;-)
213 void SquitServer(std::string &from, TreeServer* Current);
215 /** This is a wrapper function for SquitServer above, which
216 * does some validation first and passes on the SQUIT to all
217 * other remaining servers.
219 void Squit(TreeServer* Current, const std::string &reason);
221 /** FMODE command - server mode with timestamp checks */
222 bool ForceMode(const std::string &source, std::deque<std::string> ¶ms);
224 /** FTOPIC command */
225 bool ForceTopic(const std::string &source, std::deque<std::string> ¶ms);
227 /** FJOIN, similar to TS6 SJOIN, but not quite. */
228 bool ForceJoin(const std::string &source, std::deque<std::string> ¶ms);
230 /* Used on nick collision ... XXX ugly function HACK */
231 int DoCollision(User *u, time_t remotets, const char *remoteident, const char *remoteip, const char *remoteuid);
234 bool ParseUID(const std::string &source, std::deque<std::string> ¶ms);
236 /** Send one or more FJOINs for a channel of users.
237 * If the length of a single line is more than 480-NICKMAX
238 * in length, it is split over multiple lines.
240 void SendFJoins(TreeServer* Current, Channel* c);
242 /** Send G, Q, Z and E lines */
243 void SendXLines(TreeServer* Current);
245 /** Send channel modes and topics */
246 void SendChannelModes(TreeServer* Current);
248 /** send all users and their oper state/modes */
249 void SendUsers(TreeServer* Current);
251 /** This function is called when we want to send a netburst to a local
252 * server. There is a set order we must do this, because for example
253 * users require their servers to exist, and channels require their
254 * users to exist. You get the idea.
256 void DoBurst(TreeServer* s);
258 /** This function is called when we receive data from a remote
259 * server. We buffer the data in a std::string (it doesnt stay
260 * there for long), reading using InspSocket::Read() which can
261 * read up to 16 kilobytes in one operation.
263 * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
264 * THE SOCKET OBJECT FOR US.
266 virtual bool OnDataReady();
268 /** Send one or more complete lines down the socket
270 int WriteLine(std::string line);
272 /** Handle ERROR command */
273 bool Error(std::deque<std::string> ¶ms);
275 /** remote MOTD. leet, huh? */
276 bool Motd(const std::string &prefix, std::deque<std::string> ¶ms);
278 /** remote ADMIN. leet, huh? */
279 bool Admin(const std::string &prefix, std::deque<std::string> ¶ms);
281 /** Remote MODULES */
282 bool Modules(const std::string &prefix, std::deque<std::string> ¶ms);
284 bool Stats(const std::string &prefix, std::deque<std::string> ¶ms);
286 /** Because the core won't let users or even SERVERS set +o,
287 * we use the OPERTYPE command to do this.
289 bool OperType(const std::string &prefix, std::deque<std::string> ¶ms);
291 /** Because Andy insists that services-compatible servers must
292 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
294 bool ForceNick(const std::string &prefix, std::deque<std::string> ¶ms);
296 bool OperQuit(const std::string &prefix, std::deque<std::string> ¶ms);
300 bool ServiceJoin(const std::string &prefix, std::deque<std::string> ¶ms);
304 bool ServicePart(const std::string &prefix, std::deque<std::string> ¶ms);
308 bool RemoteRehash(const std::string &prefix, std::deque<std::string> ¶ms);
312 bool RemoteKill(const std::string &prefix, std::deque<std::string> ¶ms);
316 bool LocalPong(const std::string &prefix, std::deque<std::string> ¶ms);
320 bool MetaData(const std::string &prefix, std::deque<std::string> ¶ms);
324 bool ServerVersion(const std::string &prefix, std::deque<std::string> ¶ms);
328 bool ChangeHost(const std::string &prefix, std::deque<std::string> ¶ms);
332 bool AddLine(const std::string &prefix, std::deque<std::string> ¶ms);
336 bool ChangeName(const std::string &prefix, std::deque<std::string> ¶ms);
340 bool Whois(const std::string &prefix, std::deque<std::string> ¶ms);
344 bool Push(const std::string &prefix, std::deque<std::string> ¶ms);
348 bool HandleSetTime(const std::string &prefix, std::deque<std::string> ¶ms);
352 bool Time(const std::string &prefix, std::deque<std::string> ¶ms);
356 bool LocalPing(const std::string &prefix, std::deque<std::string> ¶ms);
358 /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
359 * This does not update the timestamp of the target channel, this must be done seperately.
361 bool RemoveStatus(const std::string &prefix, std::deque<std::string> ¶ms);
363 /** <- (remote) <- SERVER
365 bool RemoteServer(const std::string &prefix, std::deque<std::string> ¶ms);
367 /** (local) -> SERVER
369 bool Outbound_Reply_Server(std::deque<std::string> ¶ms);
371 /** (local) <- SERVER
373 bool Inbound_Server(std::deque<std::string> ¶ms);
377 void Split(const std::string &line, std::deque<std::string> &n);
379 /** Process complete line from buffer
381 bool ProcessLine(std::string &line);
383 /** Get this server's name
385 virtual std::string GetName();
387 /** Handle socket timeout from connect()
389 virtual void OnTimeout();
391 /** Handle socket close event
393 virtual void OnClose();
395 /** Handle incoming connection event
397 virtual int OnIncomingConnection(int newsock, char* ip);
400 /* Used to validate the value lengths of multiple parameters for a command */
401 struct cmd_validation
408 /* Used to validate the length values in CAPAB CAPABILITIES */
409 struct cap_validation