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/>.
30 * The server list in InspIRCd is maintained as two structures
31 * which hold the data in different ways. Most of the time, we
32 * want to very quicky obtain three pieces of information:
34 * (1) The information on a server
35 * (2) The information on the server we must send data through
36 * to actually REACH the server we're after
37 * (3) Potentially, the child/parent objects of this server
39 * The InspIRCd spanning protocol provides easy access to these
40 * by storing the data firstly in a recursive structure, where
41 * each item references its parent item, and a dynamic list
42 * of child items, and another structure which stores the items
43 * hashed, linearly. This means that if we want to find a server
44 * by name quickly, we can look it up in the hash, avoiding
45 * any O(n) lookups. If however, during a split or sync, we want
46 * to apply an operation to a server, and any of its child objects
47 * we can resort to recursion to walk the tree structure.
48 * Any socket can have one of five states at any one time.
50 * CONNECTING: indicates an outbound socket which is
51 * waiting to be writeable.
52 * WAIT_AUTH_1: indicates the socket is outbound and
53 * has successfully connected, but has not
54 * yet sent and received SERVER strings.
55 * WAIT_AUTH_2: indicates that the socket is inbound
56 * but has not yet sent and received
58 * CONNECTED: represents a fully authorized, fully
60 * DYING: represents a server that has had an error.
62 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
66 reference<Link> link; /* Link block used for this connection */
67 reference<Autoconnect> ac; /* Autoconnect used to cause this connection, if any */
68 std::string ModuleList; /* Required module list of other server from CAPAB */
69 std::string OptModuleList; /* Optional module list of other server from CAPAB */
70 std::string ChanModes;
71 std::string UserModes;
72 std::map<std::string,std::string> CapKeys; /* CAPAB keys from other server */
73 std::string ourchallenge; /* Challenge sent for challenge/response */
74 std::string theirchallenge; /* Challenge recv for challenge/response */
75 int capab_phase; /* Have sent CAPAB already */
76 bool auth_fingerprint; /* Did we auth using SSL fingerprint */
77 bool auth_challenge; /* Did we auth using challenge/response */
79 // Data saved from incoming SERVER command, for later use when our credentials have been accepted by the other party
80 std::string description;
86 /** Every SERVER connection inbound or outbound is represented by an object of
87 * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
88 * after setup, MyRoot will have been created as a child of Utils->TreeRoot
90 class TreeSocket : public BufferedSocket
94 std::string linkID; /* Description for this link */
95 ServerState LinkState; /* Link state */
96 CapabData* capab; /* Link setup data (held until burst is sent) */
97 TreeServer* MyRoot; /* The server we are talking to */
98 time_t NextPing; /* Time when we are due to ping this server */
99 bool LastPingWasGood; /* Responded to last ping we sent? */
100 int proto_version; /* Remote protocol version */
101 bool ConnectionFailureShown; /* Set to true if a connection failure message was shown */
103 /** Checks if the given servername and sid are both free
105 bool CheckDuplicate(const std::string& servername, const std::string& sid);
107 /** Send all ListModeBase modes set on the channel
109 void SendListModes(Channel* chan);
111 /** Send all known information about a channel */
112 void SyncChannel(Channel* chan, BurstState& bs);
114 /** Send all users and their oper state, away state and metadata */
115 void SendUsers(BurstState& bs);
120 /** Because most of the I/O gubbins are encapsulated within
121 * BufferedSocket, we just call the superclass constructor for
122 * most of the action, and append a few of our own values
125 TreeSocket(Link* link, Autoconnect* myac, const std::string& ipaddr);
127 /** When a listening socket gives us a new file descriptor,
128 * we must associate it with a socket without creating a new
129 * connection. This constructor is used for this purpose.
131 TreeSocket(int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
135 ServerState GetLinkState();
137 /** Get challenge set in our CAPAB for challenge/response
139 const std::string& GetOurChallenge();
141 /** Get challenge set in our CAPAB for challenge/response
143 void SetOurChallenge(const std::string &c);
145 /** Get challenge set in their CAPAB for challenge/response
147 const std::string& GetTheirChallenge();
149 /** Get challenge set in their CAPAB for challenge/response
151 void SetTheirChallenge(const std::string &c);
153 /** Compare two passwords based on authentication scheme
155 bool ComparePass(const Link& link, const std::string &theirs);
157 /** Clean up information used only during server negotiation
159 void CleanNegotiationInfo();
166 /** Construct a password, optionally hashed with the other side's
169 std::string MakePass(const std::string &password, const std::string &challenge);
171 /** When an outbound connection finishes connecting, we receive
172 * this event, and must send our SERVER string to the other
173 * side. If the other side is happy, as outlined in the server
174 * to server docs on the inspircd.org site, the other side
175 * will then send back its own server string.
179 /** Handle socket error event
181 void OnError(BufferedSocketError e) CXX11_OVERRIDE;
183 /** Sends an error to the remote server, and displays it locally to show
186 void SendError(const std::string &errormessage);
188 /** Recursively send the server tree with distances as hops.
189 * This is used during network burst to inform the other server
190 * (and any of ITS servers too) of what servers we know about.
192 void SendServers(TreeServer* Current, TreeServer* s);
194 /** Returns module list as a string, filtered by filter
195 * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
197 std::string MyModules(int filter);
199 /** Send my capabilities to the remote side
201 void SendCapabilities(int phase);
203 /* Isolate and return the elements that are different between two lists */
204 void ListDifference(const std::string &one, const std::string &two, char sep,
205 std::string& mleft, std::string& mright);
207 bool Capab(const parameterlist ¶ms);
209 /** This function forces this server to quit, removing this server
210 * and any users on it (and servers and users below that, etc etc).
211 * It's very slow and pretty clunky, but luckily unless your network
212 * is having a REAL bad hair day, this function shouldnt be called
213 * too many times a month ;-)
215 void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
217 /** This is a wrapper function for SquitServer above, which
218 * does some validation first and passes on the SQUIT to all
219 * other remaining servers.
221 void Squit(TreeServer* Current, const std::string &reason);
223 /** Send one or more FJOINs for a channel of users.
224 * If the length of a single line is more than 480-NICKMAX
225 * in length, it is split over multiple lines.
227 void SendFJoins(Channel* c);
229 /** Send G, Q, Z and E lines */
232 /** Send all known information about a channel */
233 void SyncChannel(Channel* chan);
235 /** This function is called when we want to send a netburst to a local
236 * server. There is a set order we must do this, because for example
237 * users require their servers to exist, and channels require their
238 * users to exist. You get the idea.
240 void DoBurst(TreeServer* s);
242 /** This function is called when we receive data from a remote
247 /** Send one or more complete lines down the socket
249 void WriteLine(const std::string& line);
251 /** Handle ERROR command */
252 void Error(parameterlist ¶ms);
254 /** (local) -> SERVER
256 bool Outbound_Reply_Server(parameterlist ¶ms);
258 /** (local) <- SERVER
260 bool Inbound_Server(parameterlist ¶ms);
262 /** Handle IRC line split
264 void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist ¶ms);
266 /** Process complete line from buffer
268 void ProcessLine(std::string &line);
270 void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
272 /** Handle socket timeout from connect()
275 /** Handle server quit on close
279 /** Returns true if this server was introduced to the rest of the network
283 /** Fixes messages coming from old servers so the new command handlers understand them
285 bool PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params);