1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 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__
24 * The server list in InspIRCd is maintained as two structures
25 * which hold the data in different ways. Most of the time, we
26 * want to very quicky obtain three pieces of information:
28 * (1) The information on a server
29 * (2) The information on the server we must send data through
30 * to actually REACH the server we're after
31 * (3) Potentially, the child/parent objects of this server
33 * The InspIRCd spanning protocol provides easy access to these
34 * by storing the data firstly in a recursive structure, where
35 * each item references its parent item, and a dynamic list
36 * of child items, and another structure which stores the items
37 * hashed, linearly. This means that if we want to find a server
38 * by name quickly, we can look it up in the hash, avoiding
39 * any O(n) lookups. If however, during a split or sync, we want
40 * to apply an operation to a server, and any of its child objects
41 * we can resort to recursion to walk the tree structure.
42 * Any socket can have one of five states at any one time.
44 * CONNECTING: indicates an outbound socket which is
45 * waiting to be writeable.
46 * WAIT_AUTH_1: indicates the socket is outbound and
47 * has successfully connected, but has not
48 * yet sent and received SERVER strings.
49 * WAIT_AUTH_2: indicates that the socket is inbound
50 * but has not yet sent and received
52 * CONNECTED: represents a fully authorized, fully
54 * DYING: represents a server that has had an error.
56 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
60 std::string ModuleList; /* Required module list of other server from CAPAB */
61 std::string OptModuleList; /* Optional module list of other server from CAPAB */
62 std::string ChanModes;
63 std::string UserModes;
64 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
65 std::string ourchallenge; /* Challenge sent for challenge/response */
66 std::string theirchallenge; /* Challenge recv for challenge/response */
67 std::string OutboundPass; /* Outbound password */
68 int capab_phase; /* Have sent CAPAB already */
69 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
70 bool auth_challenge; /* Did we auth using challenge/response */
73 /** Every SERVER connection inbound or outbound is represented by
74 * an object of type TreeSocket.
75 * TreeSockets, being inherited from BufferedSocket, can be tied into
76 * the core socket engine, and we cn therefore receive activity events
77 * for them, just like activex objects on speed. (yes really, that
78 * is a technical term!) Each of these which relates to a locally
79 * connected server is assocated with it, by hooking it onto a
80 * TreeSocket class using its constructor. In this way, we can
81 * maintain a list of servers, some of which are directly connected,
82 * some of which are not.
84 class TreeSocket : public BufferedSocket
86 SpanningTreeUtilities* Utils; /* Utility class */
87 std::string myhost; /* Canonical hostname */
88 ServerState LinkState; /* Link state */
89 std::string InboundServerName; /* Server name sent to us by other side */
90 std::string InboundDescription; /* Server description (GECOS) sent to us by the other side */
91 std::string InboundSID; /* Server ID sent to us by the other side */
94 int num_lost_users; /* Users lost in split */
95 int num_lost_servers; /* Servers lost in split */
96 time_t NextPing; /* Time when we are due to ping this server */
97 bool LastPingWasGood; /* Responded to last ping we sent? */
98 int proto_version; /* Remote protocol version */
100 reference<Autoconnect> myautoconnect; /* Autoconnect used to cause this connection, if any */
103 /** Because most of the I/O gubbins are encapsulated within
104 * BufferedSocket, we just call the superclass constructor for
105 * most of the action, and append a few of our own values
108 TreeSocket(SpanningTreeUtilities* Util, const std::string& host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, const std::string& Hook);
110 /** When a listening socket gives us a new file descriptor,
111 * we must associate it with a socket without creating a new
112 * connection. This constructor is used for this purpose.
114 TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
118 ServerState GetLinkState();
120 /** Get challenge set in our CAPAB for challenge/response
122 const std::string& GetOurChallenge();
124 /** Get challenge set in our CAPAB for challenge/response
126 void SetOurChallenge(const std::string &c);
128 /** Get challenge set in their CAPAB for challenge/response
130 const std::string& GetTheirChallenge();
132 /** Get challenge set in their CAPAB for challenge/response
134 void SetTheirChallenge(const std::string &c);
136 /** Compare two passwords based on authentication scheme
138 bool ComparePass(const Link& link, const std::string &theirs);
140 /** Clean up information used only during server negotiation
142 void CleanNegotiationInfo();
149 /** Construct a password, optionally hashed with the other side's
152 std::string MakePass(const std::string &password, const std::string &challenge);
154 /** When an outbound connection finishes connecting, we receive
155 * this event, and must send our SERVER string to the other
156 * side. If the other side is happy, as outlined in the server
157 * to server docs on the inspircd.org site, the other side
158 * will then send back its own server string.
160 virtual void OnConnected();
162 /** Handle socket error event
164 virtual void OnError(BufferedSocketError e);
166 /** Sends an error to the remote server, and displays it locally to show
169 void SendError(const std::string &errormessage);
171 /** Recursively send the server tree with distances as hops.
172 * This is used during network burst to inform the other server
173 * (and any of ITS servers too) of what servers we know about.
174 * If at any point any of these servers already exist on the other
175 * end, our connection may be terminated. The hopcounts given
176 * by this function are relative, this doesn't matter so long as
177 * they are all >1, as all the remote servers re-calculate them
178 * to be relative too, with themselves as hop 0.
180 void SendServers(TreeServer* Current, TreeServer* s, int hops);
182 /** Returns module list as a string, filtered by filter
183 * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
185 std::string MyModules(int filter);
187 /** Send my capabilities to the remote side
189 void SendCapabilities(int phase);
191 /** Add modules to VF_COMMON list for backwards compatability */
192 void CompatAddModules(std::vector<std::string>& modlist);
194 /* Isolate and return the elements that are different between two lists */
195 void ListDifference(const std::string &one, const std::string &two, char sep,
196 std::string& mleft, std::string& mright);
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 /* Used on nick collision ... XXX ugly function HACK */
215 int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
217 /** Send one or more FJOINs for a channel of users.
218 * If the length of a single line is more than 480-NICKMAX
219 * in length, it is split over multiple lines.
221 void SendFJoins(TreeServer* Current, Channel* c);
223 /** Send G, Q, Z and E lines */
224 void SendXLines(TreeServer* Current);
226 /** Send channel modes and topics */
227 void SendChannelModes(TreeServer* Current);
229 /** send all users and their oper state/modes */
230 void SendUsers(TreeServer* Current);
232 /** This function is called when we want to send a netburst to a local
233 * server. There is a set order we must do this, because for example
234 * users require their servers to exist, and channels require their
235 * users to exist. You get the idea.
237 void DoBurst(TreeServer* s);
239 /** This function is called when we receive data from a remote
244 /** Send one or more complete lines down the socket
246 void WriteLine(std::string line);
248 /** Handle ERROR command */
249 void Error(parameterlist ¶ms);
252 bool Away(const std::string &prefix, parameterlist ¶ms);
254 /** SAVE to resolve nick collisions without killing */
255 bool ForceNick(const std::string &prefix, parameterlist ¶ms);
259 void Encap(User* who, parameterlist ¶ms);
263 bool OperQuit(const std::string &prefix, parameterlist ¶ms);
267 bool LocalPong(const std::string &prefix, parameterlist ¶ms);
271 bool ServerVersion(const std::string &prefix, parameterlist ¶ms);
275 bool AddLine(const std::string &prefix, parameterlist ¶ms);
279 bool DelLine(const std::string &prefix, parameterlist ¶ms);
283 bool Whois(const std::string &prefix, parameterlist ¶ms);
287 bool Push(const std::string &prefix, parameterlist ¶ms);
291 bool LocalPing(const std::string &prefix, parameterlist ¶ms);
293 /** <- (remote) <- SERVER
295 bool RemoteServer(const std::string &prefix, parameterlist ¶ms);
297 /** (local) -> SERVER
299 bool Outbound_Reply_Server(parameterlist ¶ms);
301 /** (local) <- SERVER
303 bool Inbound_Server(parameterlist ¶ms);
305 /** Handle IRC line split
307 void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist ¶ms);
309 /** Process complete line from buffer
311 void ProcessLine(std::string &line);
313 void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
315 /** Get this server's name
317 virtual std::string GetName();
319 /** Handle socket timeout from connect()
321 virtual void OnTimeout();
322 /** Handle server quit on close
324 virtual void Close();
327 /* Used to validate the value lengths of multiple parameters for a command */
328 struct cmd_validation
335 /* Used to validate the length values in CAPAB CAPABILITIES */
336 struct cap_validation