2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
9 * This file is part of InspIRCd. InspIRCd is free software: you can
10 * redistribute it and/or modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation, version 2.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef M_SPANNINGTREE_TREESOCKET_H
24 #define M_SPANNINGTREE_TREESOCKET_H
33 * The server list in InspIRCd is maintained as two structures
34 * which hold the data in different ways. Most of the time, we
35 * want to very quicky obtain three pieces of information:
37 * (1) The information on a server
38 * (2) The information on the server we must send data through
39 * to actually REACH the server we're after
40 * (3) Potentially, the child/parent objects of this server
42 * The InspIRCd spanning protocol provides easy access to these
43 * by storing the data firstly in a recursive structure, where
44 * each item references its parent item, and a dynamic list
45 * of child items, and another structure which stores the items
46 * hashed, linearly. This means that if we want to find a server
47 * by name quickly, we can look it up in the hash, avoiding
48 * any O(n) lookups. If however, during a split or sync, we want
49 * to apply an operation to a server, and any of its child objects
50 * we can resort to recursion to walk the tree structure.
51 * Any socket can have one of five states at any one time.
53 * CONNECTING: indicates an outbound socket which is
54 * waiting to be writeable.
55 * WAIT_AUTH_1: indicates the socket is outbound and
56 * has successfully connected, but has not
57 * yet sent and received SERVER strings.
58 * WAIT_AUTH_2: indicates that the socket is inbound
59 * but has not yet sent and received
61 * CONNECTED: represents a fully authorized, fully
63 * DYING: represents a server that has had an error.
65 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
69 reference<Link> link; /* Link block used for this connection */
70 reference<Autoconnect> ac; /* Autoconnect used to cause this connection, if any */
71 std::string ModuleList; /* Required module list of other server from CAPAB */
72 std::string OptModuleList; /* Optional module list of other server from CAPAB */
73 std::string ChanModes;
74 std::string UserModes;
75 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
76 std::string ourchallenge; /* Challenge sent for challenge/response */
77 std::string theirchallenge; /* Challenge recv for challenge/response */
78 int capab_phase; /* Have sent CAPAB already */
79 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
80 bool auth_challenge; /* Did we auth using challenge/response */
83 /** Every SERVER connection inbound or outbound is represented by an object of
84 * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
85 * after setup, MyRoot will have been created as a child of Utils->TreeRoot
87 class TreeSocket : public BufferedSocket
89 SpanningTreeUtilities* Utils; /* Utility class */
90 std::string linkID; /* Description for this link */
91 ServerState LinkState; /* Link state */
92 CapabData* capab; /* Link setup data (held until burst is sent) */
93 TreeServer* MyRoot; /* The server we are talking to */
94 time_t NextPing; /* Time when we are due to ping this server */
95 bool LastPingWasGood; /* Responded to last ping we sent? */
96 int proto_version; /* Remote protocol version */
97 bool ConnectionFailureShown; /* Set to true if a connection failure message was shown */
101 /** Because most of the I/O gubbins are encapsulated within
102 * BufferedSocket, we just call the superclass constructor for
103 * most of the action, and append a few of our own values
106 TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr);
108 /** When a listening socket gives us a new file descriptor,
109 * we must associate it with a socket without creating a new
110 * connection. This constructor is used for this purpose.
112 TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
116 ServerState GetLinkState();
118 /** Get challenge set in our CAPAB for challenge/response
120 const std::string& GetOurChallenge();
122 /** Get challenge set in our CAPAB for challenge/response
124 void SetOurChallenge(const std::string &c);
126 /** Get challenge set in their CAPAB for challenge/response
128 const std::string& GetTheirChallenge();
130 /** Get challenge set in their CAPAB for challenge/response
132 void SetTheirChallenge(const std::string &c);
134 /** Compare two passwords based on authentication scheme
136 bool ComparePass(const Link& link, const std::string &theirs);
138 /** Clean up information used only during server negotiation
140 void CleanNegotiationInfo();
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 /* Isolate and return the elements that are different between two lists */
190 void ListDifference(const std::string &one, const std::string &two, char sep,
191 std::string& mleft, std::string& mright);
193 bool Capab(const parameterlist ¶ms);
195 /** This function forces this server to quit, removing this server
196 * and any users on it (and servers and users below that, etc etc).
197 * It's very slow and pretty clunky, but luckily unless your network
198 * is having a REAL bad hair day, this function shouldnt be called
199 * too many times a month ;-)
201 void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
203 /** This is a wrapper function for SquitServer above, which
204 * does some validation first and passes on the SQUIT to all
205 * other remaining servers.
207 void Squit(TreeServer* Current, const std::string &reason);
209 /* Used on nick collision ... XXX ugly function HACK */
210 int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
212 /** Send one or more FJOINs for a channel of users.
213 * If the length of a single line is more than 480-NICKMAX
214 * in length, it is split over multiple lines.
216 void SendFJoins(Channel* c);
218 /** Send G, Q, Z and E lines */
221 /** Send channel modes and topics */
222 void SendChannelModes();
224 /** send all users and their oper state/modes */
227 /** This function is called when we want to send a netburst to a local
228 * server. There is a set order we must do this, because for example
229 * users require their servers to exist, and channels require their
230 * users to exist. You get the idea.
232 void DoBurst(TreeServer* s);
234 /** This function is called when we receive data from a remote
239 /** Send one or more complete lines down the socket
241 void WriteLine(std::string line);
243 /** Handle ERROR command */
244 void Error(parameterlist ¶ms);
247 bool Away(const std::string &prefix, parameterlist ¶ms);
249 /** SAVE to resolve nick collisions without killing */
250 bool ForceNick(const std::string &prefix, parameterlist ¶ms);
254 void Encap(User* who, parameterlist ¶ms);
258 bool OperQuit(const std::string &prefix, parameterlist ¶ms);
262 bool LocalPong(const std::string &prefix, parameterlist ¶ms);
266 bool ServerVersion(const std::string &prefix, parameterlist ¶ms);
270 bool AddLine(const std::string &prefix, parameterlist ¶ms);
274 bool DelLine(const std::string &prefix, parameterlist ¶ms);
278 bool Whois(const std::string &prefix, parameterlist ¶ms);
282 bool Push(const std::string &prefix, parameterlist ¶ms);
286 bool LocalPing(const std::string &prefix, parameterlist ¶ms);
288 /** <- (remote) <- SERVER
290 bool RemoteServer(const std::string &prefix, parameterlist ¶ms);
292 /** (local) -> SERVER
294 bool Outbound_Reply_Server(parameterlist ¶ms);
296 /** (local) <- SERVER
298 bool Inbound_Server(parameterlist ¶ms);
300 /** Handle IRC line split
302 void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist ¶ms);
304 /** Process complete line from buffer
306 void ProcessLine(std::string &line);
308 void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
310 /** Handle socket timeout from connect()
312 virtual void OnTimeout();
313 /** Handle server quit on close
315 virtual void Close();
317 /** Returns true if this server was introduced to the rest of the network