1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/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"
22 #include "../transport.h"
25 #include "handshaketimer.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.
48 * CONNECTING: indicates an outbound socket which is
49 * waiting to be writeable.
50 * WAIT_AUTH_1: indicates the socket is outbound and
51 * has successfully connected, but has not
52 * yet sent and received SERVER strings.
53 * WAIT_AUTH_2: indicates that the socket is inbound
54 * but has not yet sent and received
56 * CONNECTED: represents a fully authorized, fully
59 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
61 /** Every SERVER connection inbound or outbound is represented by
62 * an object of type TreeSocket.
63 * TreeSockets, being inherited from BufferedSocket, can be tied into
64 * the core socket engine, and we cn therefore receive activity events
65 * for them, just like activex objects on speed. (yes really, that
66 * is a technical term!) Each of these which relates to a locally
67 * connected server is assocated with it, by hooking it onto a
68 * TreeSocket class using its constructor. In this way, we can
69 * maintain a list of servers, some of which are directly connected,
70 * some of which are not.
72 class TreeSocket : public BufferedSocket
74 SpanningTreeUtilities* Utils; /* Utility class */
75 std::string myhost; /* Canonical hostname */
76 std::string in_buffer; /* Input buffer */
77 ServerState LinkState; /* Link state */
78 std::string InboundServerName; /* Server name sent to us by other side */
79 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
80 std::string InboundSID; /* Server ID sent to us by the other side */
81 int num_lost_users; /* Users lost in split */
82 int num_lost_servers; /* Servers lost in split */
83 time_t NextPing; /* Time when we are due to ping this server */
84 bool LastPingWasGood; /* Responded to last ping we sent? */
85 std::string ModuleList; /* Required module list of other server from CAPAB */
86 std::string OptModuleList; /* Optional module list of other server from CAPAB */
87 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
88 Module* Hook; /* I/O hooking module that we're attached to for this socket */
89 std::string ourchallenge; /* Challenge sent for challenge/response */
90 std::string theirchallenge; /* Challenge recv for challenge/response */
91 std::string OutboundPass; /* Outbound password */
92 int capab_phase; /* Have sent CAPAB already */
93 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
94 bool auth_challenge; /* Did we auth using challenge/response */
95 int proto_version; /* Remote protocol version */
97 HandshakeTimer* hstimer; /* Handshake timer, needed to work around I/O hook buffering */
100 /** Because most of the I/O gubbins are encapsulated within
101 * BufferedSocket, we just call the superclass constructor for
102 * most of the action, and append a few of our own values
105 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Module* HookMod = NULL);
107 /** When a listening socket gives us a new file descriptor,
108 * we must associate it with a socket without creating a new
109 * connection. This constructor is used for this purpose.
111 TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
115 ServerState GetLinkState();
117 /** Get challenge set in our CAPAB for challenge/response
119 const std::string& GetOurChallenge();
121 /** Get challenge set in our CAPAB for challenge/response
123 void SetOurChallenge(const std::string &c);
125 /** Get challenge set in their CAPAB for challenge/response
127 const std::string& GetTheirChallenge();
129 /** Get challenge set in their CAPAB for challenge/response
131 void SetTheirChallenge(const std::string &c);
133 /** Compare two passwords based on authentication scheme
135 bool ComparePass(const Link& link, const std::string &theirs);
137 /** Clean up information used only during server negotiation
139 void CleanNegotiationInfo();
141 /** Return the module which we are hooking to for I/O encapsulation
149 /** Generate random string used for challenge-response auth
151 std::string RandString(unsigned int length);
153 /** Construct a password, optionally hashed with the other side's
156 std::string MakePass(const std::string &password, const std::string &challenge);
158 /** When an outbound connection finishes connecting, we receive
159 * this event, and must send our SERVER string to the other
160 * side. If the other side is happy, as outlined in the server
161 * to server docs on the inspircd.org site, the other side
162 * will then send back its own server string.
164 virtual bool OnConnected();
166 /** Handle socket error event
168 virtual void OnError(BufferedSocketError e);
170 /** Sends an error to the remote server, and displays it locally to show
173 void SendError(const std::string &errormessage);
175 /** Handle socket disconnect event
177 virtual int OnDisconnect();
179 /** Recursively send the server tree with distances as hops.
180 * This is used during network burst to inform the other server
181 * (and any of ITS servers too) of what servers we know about.
182 * If at any point any of these servers already exist on the other
183 * end, our connection may be terminated. The hopcounts given
184 * by this function are relative, this doesn't matter so long as
185 * they are all >1, as all the remote servers re-calculate them
186 * to be relative too, with themselves as hop 0.
188 void SendServers(TreeServer* Current, TreeServer* s, int hops);
190 /** Returns module list as a string, filtered by filter
191 * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
193 std::string MyModules(int filter);
195 /** Send my capabilities to the remote side
197 void SendCapabilities(int phase);
199 /** Add modules to VF_COMMON list for backwards compatability */
200 void CompatAddModules(std::vector<std::string>& modlist);
202 /* Check a comma seperated list for an item */
203 bool HasItem(const std::string &list, const std::string &item);
205 /* Isolate and return the elements that are different between two comma seperated lists */
206 std::string ListDifference(const std::string &one, const std::string &two);
208 bool Capab(const parameterlist ¶ms);
210 /** This function forces this server to quit, removing this server
211 * and any users on it (and servers and users below that, etc etc).
212 * It's very slow and pretty clunky, but luckily unless your network
213 * is having a REAL bad hair day, this function shouldnt be called
214 * too many times a month ;-)
216 void SquitServer(std::string &from, TreeServer* Current);
218 /** This is a wrapper function for SquitServer above, which
219 * does some validation first and passes on the SQUIT to all
220 * other remaining servers.
222 void Squit(TreeServer* Current, const std::string &reason);
224 /** FMODE command - server mode with timestamp checks */
225 bool ForceMode(const std::string &source, parameterlist ¶ms);
227 /** FTOPIC command */
228 bool ForceTopic(const std::string &source, parameterlist ¶ms);
230 /** FJOIN, similar to TS6 SJOIN, but not quite. */
231 bool ForceJoin(const std::string &source, parameterlist ¶ms);
233 /* Used on nick collision ... XXX ugly function HACK */
234 int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
237 bool ParseUID(const std::string &source, parameterlist ¶ms);
239 /** Send one or more FJOINs for a channel of users.
240 * If the length of a single line is more than 480-NICKMAX
241 * in length, it is split over multiple lines.
243 void SendFJoins(TreeServer* Current, Channel* c);
245 /** Send G, Q, Z and E lines */
246 void SendXLines(TreeServer* Current);
248 /** Send channel modes and topics */
249 void SendChannelModes(TreeServer* Current);
251 /** send all users and their oper state/modes */
252 void SendUsers(TreeServer* Current);
254 /** This function is called when we want to send a netburst to a local
255 * server. There is a set order we must do this, because for example
256 * users require their servers to exist, and channels require their
257 * users to exist. You get the idea.
259 void DoBurst(TreeServer* s);
261 /** This function is called when we receive data from a remote
262 * server. We buffer the data in a std::string (it doesnt stay
263 * there for long), reading using BufferedSocket::Read() which can
264 * read up to 16 kilobytes in one operation.
266 * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
267 * THE SOCKET OBJECT FOR US.
269 virtual bool OnDataReady();
271 /** Send one or more complete lines down the socket
273 void WriteLine(std::string line);
275 /** Handle ERROR command */
276 bool Error(parameterlist ¶ms);
278 /** remote MOTD. leet, huh? */
279 bool Motd(const std::string &prefix, parameterlist ¶ms);
281 /** remote ADMIN. leet, huh? */
282 bool Admin(const std::string &prefix, parameterlist ¶ms);
284 /** Remote MODULES */
285 bool Modules(const std::string &prefix, parameterlist ¶ms);
287 bool Stats(const std::string &prefix, parameterlist ¶ms);
289 /** Because the core won't let users or even SERVERS set +o,
290 * we use the OPERTYPE command to do this.
292 bool OperType(const std::string &prefix, parameterlist ¶ms);
294 /** Because Andy insists that services-compatible servers must
295 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
297 bool SVSNick(const std::string &prefix, parameterlist ¶ms);
299 /** SAVE to resolve nick collisions without killing */
300 bool ForceNick(const std::string &prefix, parameterlist ¶ms);
302 /** PRIVMSG or NOTICE with server origin ONLY
304 bool ServerMessage(const std::string &messagetype, const std::string &prefix, parameterlist ¶ms, const std::string &sourceserv);
308 bool Encap(const std::string &prefix, parameterlist ¶ms);
312 bool OperQuit(const std::string &prefix, parameterlist ¶ms);
316 bool ServiceJoin(const std::string &prefix, parameterlist ¶ms);
320 bool ServicePart(const std::string &prefix, parameterlist ¶ms);
324 bool RemoteKill(const std::string &prefix, parameterlist ¶ms);
328 bool LocalPong(const std::string &prefix, parameterlist ¶ms);
332 bool MetaData(const std::string &prefix, parameterlist ¶ms);
336 bool ServerVersion(const std::string &prefix, parameterlist ¶ms);
340 bool ChangeHost(const std::string &prefix, parameterlist ¶ms);
344 bool AddLine(const std::string &prefix, parameterlist ¶ms);
348 bool DelLine(const std::string &prefix, parameterlist ¶ms);
352 bool ChangeName(const std::string &prefix, parameterlist ¶ms);
355 bool ChangeIdent(const std::string &prefix, parameterlist ¶ms);
359 bool Whois(const std::string &prefix, parameterlist ¶ms);
363 bool Push(const std::string &prefix, parameterlist ¶ms);
367 bool Time(const std::string &prefix, parameterlist ¶ms);
371 bool LocalPing(const std::string &prefix, parameterlist ¶ms);
373 /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
374 * This does not update the timestamp of the target channel, this must be done seperately.
376 bool RemoveStatus(const std::string &prefix, parameterlist ¶ms);
378 /** <- (remote) <- SERVER
380 bool RemoteServer(const std::string &prefix, parameterlist ¶ms);
382 /** (local) -> SERVER
384 bool Outbound_Reply_Server(parameterlist ¶ms);
386 /** (local) <- SERVER
388 bool Inbound_Server(parameterlist ¶ms);
392 void Split(const std::string &line, parameterlist &n);
394 /** Process complete line from buffer
396 bool ProcessLine(std::string &line);
398 /** Get this server's name
400 virtual std::string GetName();
402 /** Handle socket timeout from connect()
404 virtual void OnTimeout();
406 /** Handle socket close event
408 virtual void OnClose();
411 /* Used to validate the value lengths of multiple parameters for a command */
412 struct cmd_validation
419 /* Used to validate the length values in CAPAB CAPABILITIES */
420 struct cap_validation