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 reference<Link> link; /* Link block used for this connection */
61 reference<Autoconnect> ac; /* Autoconnect used to cause this connection, if any */
62 std::string ModuleList; /* Required module list of other server from CAPAB */
63 std::string OptModuleList; /* Optional module list of other server from CAPAB */
64 std::string ChanModes;
65 std::string UserModes;
66 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
67 std::string ourchallenge; /* Challenge sent for challenge/response */
68 std::string theirchallenge; /* Challenge recv for challenge/response */
69 int capab_phase; /* Have sent CAPAB already */
70 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
71 bool auth_challenge; /* Did we auth using challenge/response */
74 /** Every SERVER connection inbound or outbound is represented by an object of
75 * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
76 * after setup, MyRoot will have been created as a child of Utils->TreeRoot
78 class TreeSocket : public BufferedSocket
80 SpanningTreeUtilities* Utils; /* Utility class */
81 std::string linkID; /* Description for this link */
82 ServerState LinkState; /* Link state */
83 CapabData* capab; /* Link setup data (held until burst is sent) */
84 TreeServer* MyRoot; /* The server we are talking to */
85 time_t NextPing; /* Time when we are due to ping this server */
86 bool LastPingWasGood; /* Responded to last ping we sent? */
87 int proto_version; /* Remote protocol version */
91 /** Because most of the I/O gubbins are encapsulated within
92 * BufferedSocket, we just call the superclass constructor for
93 * most of the action, and append a few of our own values
96 TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr);
98 /** When a listening socket gives us a new file descriptor,
99 * we must associate it with a socket without creating a new
100 * connection. This constructor is used for this purpose.
102 TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
106 ServerState GetLinkState();
108 /** Get challenge set in our CAPAB for challenge/response
110 const std::string& GetOurChallenge();
112 /** Get challenge set in our CAPAB for challenge/response
114 void SetOurChallenge(const std::string &c);
116 /** Get challenge set in their CAPAB for challenge/response
118 const std::string& GetTheirChallenge();
120 /** Get challenge set in their CAPAB for challenge/response
122 void SetTheirChallenge(const std::string &c);
124 /** Compare two passwords based on authentication scheme
126 bool ComparePass(const Link& link, const std::string &theirs);
128 /** Clean up information used only during server negotiation
130 void CleanNegotiationInfo();
137 /** Construct a password, optionally hashed with the other side's
140 std::string MakePass(const std::string &password, const std::string &challenge);
142 /** When an outbound connection finishes connecting, we receive
143 * this event, and must send our SERVER string to the other
144 * side. If the other side is happy, as outlined in the server
145 * to server docs on the inspircd.org site, the other side
146 * will then send back its own server string.
148 virtual void OnConnected();
150 /** Handle socket error event
152 virtual void OnError(BufferedSocketError e);
154 /** Sends an error to the remote server, and displays it locally to show
157 void SendError(const std::string &errormessage);
159 /** Recursively send the server tree with distances as hops.
160 * This is used during network burst to inform the other server
161 * (and any of ITS servers too) of what servers we know about.
162 * If at any point any of these servers already exist on the other
163 * end, our connection may be terminated. The hopcounts given
164 * by this function are relative, this doesn't matter so long as
165 * they are all >1, as all the remote servers re-calculate them
166 * to be relative too, with themselves as hop 0.
168 void SendServers(TreeServer* Current, TreeServer* s, int hops);
170 /** Returns module list as a string, filtered by filter
171 * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
173 std::string MyModules(int filter);
175 /** Send my capabilities to the remote side
177 void SendCapabilities(int phase);
179 /** Add modules to VF_COMMON list for backwards compatability */
180 void CompatAddModules(std::vector<std::string>& modlist);
182 /* Isolate and return the elements that are different between two lists */
183 void ListDifference(const std::string &one, const std::string &two, char sep,
184 std::string& mleft, std::string& mright);
186 bool Capab(const parameterlist ¶ms);
188 /** This function forces this server to quit, removing this server
189 * and any users on it (and servers and users below that, etc etc).
190 * It's very slow and pretty clunky, but luckily unless your network
191 * is having a REAL bad hair day, this function shouldnt be called
192 * too many times a month ;-)
194 void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
196 /** This is a wrapper function for SquitServer above, which
197 * does some validation first and passes on the SQUIT to all
198 * other remaining servers.
200 void Squit(TreeServer* Current, const std::string &reason);
202 /* Used on nick collision ... XXX ugly function HACK */
203 int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
205 /** Send one or more FJOINs for a channel of users.
206 * If the length of a single line is more than 480-NICKMAX
207 * in length, it is split over multiple lines.
209 void SendFJoins(TreeServer* Current, Channel* c);
211 /** Send G, Q, Z and E lines */
212 void SendXLines(TreeServer* Current);
214 /** Send channel modes and topics */
215 void SendChannelModes(TreeServer* Current);
217 /** send all users and their oper state/modes */
218 void SendUsers(TreeServer* Current);
220 /** This function is called when we want to send a netburst to a local
221 * server. There is a set order we must do this, because for example
222 * users require their servers to exist, and channels require their
223 * users to exist. You get the idea.
225 void DoBurst(TreeServer* s);
227 /** This function is called when we receive data from a remote
232 /** Send one or more complete lines down the socket
234 void WriteLine(std::string line);
236 /** Handle ERROR command */
237 void Error(parameterlist ¶ms);
240 bool Away(const std::string &prefix, parameterlist ¶ms);
242 /** SAVE to resolve nick collisions without killing */
243 bool ForceNick(const std::string &prefix, parameterlist ¶ms);
247 void Encap(User* who, parameterlist ¶ms);
251 bool OperQuit(const std::string &prefix, parameterlist ¶ms);
255 bool LocalPong(const std::string &prefix, parameterlist ¶ms);
259 bool ServerVersion(const std::string &prefix, parameterlist ¶ms);
263 bool AddLine(const std::string &prefix, parameterlist ¶ms);
267 bool DelLine(const std::string &prefix, parameterlist ¶ms);
271 bool Whois(const std::string &prefix, parameterlist ¶ms);
275 bool Push(const std::string &prefix, parameterlist ¶ms);
279 bool LocalPing(const std::string &prefix, parameterlist ¶ms);
281 /** <- (remote) <- SERVER
283 bool RemoteServer(const std::string &prefix, parameterlist ¶ms);
285 /** (local) -> SERVER
287 bool Outbound_Reply_Server(parameterlist ¶ms);
289 /** (local) <- SERVER
291 bool Inbound_Server(parameterlist ¶ms);
293 /** Handle IRC line split
295 void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist ¶ms);
297 /** Process complete line from buffer
299 void ProcessLine(std::string &line);
301 void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
303 /** Handle socket timeout from connect()
305 virtual void OnTimeout();
306 /** Handle server quit on close
308 virtual void Close();