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__
20 #include "../transport.h"
23 #include "handshaketimer.h"
26 * The server list in InspIRCd is maintained as two structures
27 * which hold the data in different ways. Most of the time, we
28 * want to very quicky obtain three pieces of information:
30 * (1) The information on a server
31 * (2) The information on the server we must send data through
32 * to actually REACH the server we're after
33 * (3) Potentially, the child/parent objects of this server
35 * The InspIRCd spanning protocol provides easy access to these
36 * by storing the data firstly in a recursive structure, where
37 * each item references its parent item, and a dynamic list
38 * of child items, and another structure which stores the items
39 * hashed, linearly. This means that if we want to find a server
40 * by name quickly, we can look it up in the hash, avoiding
41 * any O(n) lookups. If however, during a split or sync, we want
42 * to apply an operation to a server, and any of its child objects
43 * we can resort to recursion to walk the tree structure.
44 * Any socket can have one of five states at any one time.
46 * CONNECTING: indicates an outbound socket which is
47 * waiting to be writeable.
48 * WAIT_AUTH_1: indicates the socket is outbound and
49 * has successfully connected, but has not
50 * yet sent and received SERVER strings.
51 * WAIT_AUTH_2: indicates that the socket is inbound
52 * but has not yet sent and received
54 * CONNECTED: represents a fully authorized, fully
57 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
59 /** Every SERVER connection inbound or outbound is represented by
60 * an object of type TreeSocket.
61 * TreeSockets, being inherited from BufferedSocket, can be tied into
62 * the core socket engine, and we cn therefore receive activity events
63 * for them, just like activex objects on speed. (yes really, that
64 * is a technical term!) Each of these which relates to a locally
65 * connected server is assocated with it, by hooking it onto a
66 * TreeSocket class using its constructor. In this way, we can
67 * maintain a list of servers, some of which are directly connected,
68 * some of which are not.
70 class TreeSocket : public BufferedSocket
72 SpanningTreeUtilities* Utils; /* Utility class */
73 std::string myhost; /* Canonical hostname */
74 ServerState LinkState; /* Link state */
75 std::string InboundServerName; /* Server name sent to us by other side */
76 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
77 std::string InboundSID; /* Server ID sent to us by the other side */
78 int num_lost_users; /* Users lost in split */
79 int num_lost_servers; /* Servers lost in split */
80 time_t NextPing; /* Time when we are due to ping this server */
81 bool LastPingWasGood; /* Responded to last ping we sent? */
83 std::string ModuleList; /* Required module list of other server from CAPAB */
84 std::string OptModuleList; /* Optional module list of other server from CAPAB */
85 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
86 std::string ourchallenge; /* Challenge sent for challenge/response */
87 std::string theirchallenge; /* Challenge recv for challenge/response */
88 std::string OutboundPass; /* Outbound password */
89 int capab_phase; /* Have sent CAPAB already */
90 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
91 bool auth_challenge; /* Did we auth using challenge/response */
92 int proto_version; /* Remote protocol version */
94 HandshakeTimer* hstimer; /* Handshake timer, needed to work around I/O hook buffering */
95 Autoconnect* myautoconnect; /* Autoconnect used to cause this connection, if any */
98 /** Because most of the I/O gubbins are encapsulated within
99 * BufferedSocket, we just call the superclass constructor for
100 * most of the action, and append a few of our own values
103 TreeSocket(SpanningTreeUtilities* Util, std::string host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, Module* HookMod = NULL);
105 /** When a listening socket gives us a new file descriptor,
106 * we must associate it with a socket without creating a new
107 * connection. This constructor is used for this purpose.
109 TreeSocket(SpanningTreeUtilities* Util, int newfd, char* ip, Autoconnect* myac, Module* HookMod = NULL);
113 ServerState GetLinkState();
115 /** Get challenge set in our CAPAB for challenge/response
117 const std::string& GetOurChallenge();
119 /** Get challenge set in our CAPAB for challenge/response
121 void SetOurChallenge(const std::string &c);
123 /** Get challenge set in their CAPAB for challenge/response
125 const std::string& GetTheirChallenge();
127 /** Get challenge set in their CAPAB for challenge/response
129 void SetTheirChallenge(const std::string &c);
131 /** Compare two passwords based on authentication scheme
133 bool ComparePass(const Link& link, const std::string &theirs);
135 /** Clean up information used only during server negotiation
137 void CleanNegotiationInfo();
143 /** Generate random string used for challenge-response auth
145 std::string RandString(unsigned int length);
147 /** Construct a password, optionally hashed with the other side's
150 std::string MakePass(const std::string &password, const std::string &challenge);
152 /** When an outbound connection finishes connecting, we receive
153 * this event, and must send our SERVER string to the other
154 * side. If the other side is happy, as outlined in the server
155 * to server docs on the inspircd.org site, the other side
156 * will then send back its own server string.
158 virtual void OnConnected();
160 /** Handle socket error event
162 virtual void OnError(BufferedSocketError e);
164 /** Sends an error to the remote server, and displays it locally to show
167 void SendError(const std::string &errormessage);
169 /** Recursively send the server tree with distances as hops.
170 * This is used during network burst to inform the other server
171 * (and any of ITS servers too) of what servers we know about.
172 * If at any point any of these servers already exist on the other
173 * end, our connection may be terminated. The hopcounts given
174 * by this function are relative, this doesn't matter so long as
175 * they are all >1, as all the remote servers re-calculate them
176 * to be relative too, with themselves as hop 0.
178 void SendServers(TreeServer* Current, TreeServer* s, int hops);
180 /** Returns module list as a string, filtered by filter
181 * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
183 std::string MyModules(int filter);
185 /** Send my capabilities to the remote side
187 void SendCapabilities(int phase);
189 /** Add modules to VF_COMMON list for backwards compatability */
190 void CompatAddModules(std::vector<std::string>& modlist);
192 /* Check a comma seperated list for an item */
193 bool HasItem(const std::string &list, const std::string &item);
195 /* Isolate and return the elements that are different between two comma seperated lists */
196 std::string ListDifference(const std::string &one, const std::string &two);
198 bool Capab(const parameterlist ¶ms);
200 /** This function forces this server to quit, removing this server
201 * and any users on it (and servers and users below that, etc etc).
202 * It's very slow and pretty clunky, but luckily unless your network
203 * is having a REAL bad hair day, this function shouldnt be called
204 * too many times a month ;-)
206 void SquitServer(std::string &from, TreeServer* Current);
208 /** This is a wrapper function for SquitServer above, which
209 * does some validation first and passes on the SQUIT to all
210 * other remaining servers.
212 void Squit(TreeServer* Current, const std::string &reason);
214 /** FMODE command - server mode with timestamp checks */
215 bool ForceMode(const std::string &source, parameterlist ¶ms);
217 /** FTOPIC command */
218 bool ForceTopic(const std::string &source, parameterlist ¶ms);
220 /** FJOIN, similar to TS6 SJOIN, but not quite. */
221 bool ForceJoin(const std::string &source, parameterlist ¶ms);
223 /* Used on nick collision ... XXX ugly function HACK */
224 int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
227 bool ParseUID(const std::string &source, parameterlist ¶ms);
229 /** Send one or more FJOINs for a channel of users.
230 * If the length of a single line is more than 480-NICKMAX
231 * in length, it is split over multiple lines.
233 void SendFJoins(TreeServer* Current, Channel* c);
235 /** Send G, Q, Z and E lines */
236 void SendXLines(TreeServer* Current);
238 /** Send channel modes and topics */
239 void SendChannelModes(TreeServer* Current);
241 /** send all users and their oper state/modes */
242 void SendUsers(TreeServer* Current);
244 /** This function is called when we want to send a netburst to a local
245 * server. There is a set order we must do this, because for example
246 * users require their servers to exist, and channels require their
247 * users to exist. You get the idea.
249 void DoBurst(TreeServer* s);
251 /** This function is called when we receive data from a remote
256 /** Send one or more complete lines down the socket
258 void WriteLine(std::string line);
260 /** Handle ERROR command */
261 bool Error(parameterlist ¶ms);
263 /** remote MOTD. leet, huh? */
264 bool Motd(const std::string &prefix, parameterlist ¶ms);
266 /** remote ADMIN. leet, huh? */
267 bool Admin(const std::string &prefix, parameterlist ¶ms);
269 /** Remote MODULES */
270 bool Modules(const std::string &prefix, parameterlist ¶ms);
272 bool Stats(const std::string &prefix, parameterlist ¶ms);
274 /** Because the core won't let users or even SERVERS set +o,
275 * we use the OPERTYPE command to do this.
277 bool OperType(const std::string &prefix, parameterlist ¶ms);
280 bool Away(const std::string &prefix, parameterlist ¶ms);
282 /** Because Andy insists that services-compatible servers must
283 * implement SVSNICK and SVSJOIN, that's exactly what we do :p
285 bool SVSNick(const std::string &prefix, parameterlist ¶ms);
287 /** SAVE to resolve nick collisions without killing */
288 bool ForceNick(const std::string &prefix, parameterlist ¶ms);
290 /** PRIVMSG or NOTICE with server origin ONLY
292 bool ServerMessage(const std::string &messagetype, const std::string &prefix, parameterlist ¶ms, const std::string &sourceserv);
296 bool Encap(const std::string &prefix, parameterlist ¶ms);
300 bool OperQuit(const std::string &prefix, parameterlist ¶ms);
304 bool ServiceJoin(const std::string &prefix, parameterlist ¶ms);
308 bool ServicePart(const std::string &prefix, parameterlist ¶ms);
312 bool RemoteKill(const std::string &prefix, parameterlist ¶ms);
316 bool LocalPong(const std::string &prefix, parameterlist ¶ms);
320 bool MetaData(const std::string &prefix, parameterlist ¶ms);
324 bool ServerVersion(const std::string &prefix, parameterlist ¶ms);
328 bool ChangeHost(const std::string &prefix, parameterlist ¶ms);
332 bool AddLine(const std::string &prefix, parameterlist ¶ms);
336 bool DelLine(const std::string &prefix, parameterlist ¶ms);
340 bool ChangeName(const std::string &prefix, parameterlist ¶ms);
343 bool ChangeIdent(const std::string &prefix, parameterlist ¶ms);
347 bool Whois(const std::string &prefix, parameterlist ¶ms);
351 bool Push(const std::string &prefix, parameterlist ¶ms);
355 bool Time(const std::string &prefix, parameterlist ¶ms);
359 bool LocalPing(const std::string &prefix, parameterlist ¶ms);
361 /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
362 * This does not update the timestamp of the target channel, this must be done seperately.
364 bool RemoveStatus(const std::string &prefix, parameterlist ¶ms);
366 /** <- (remote) <- SERVER
368 bool RemoteServer(const std::string &prefix, parameterlist ¶ms);
370 /** (local) -> SERVER
372 bool Outbound_Reply_Server(parameterlist ¶ms);
374 /** (local) <- SERVER
376 bool Inbound_Server(parameterlist ¶ms);
380 void Split(const std::string &line, parameterlist &n);
382 /** Process complete line from buffer
384 bool ProcessLine(std::string &line);
386 /** Get this server's name
388 virtual std::string GetName();
390 /** Handle socket timeout from connect()
392 virtual void OnTimeout();
393 /** Handle server quit on close
395 virtual void Close();
398 /* Used to validate the value lengths of multiple parameters for a command */
399 struct cmd_validation
406 /* Used to validate the length values in CAPAB CAPABILITIES */
407 struct cap_validation