]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Merge pull request #495 from SaberUK/master+fix-libcpp
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
8  *
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.
12  *
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
16  * details.
17  *
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/>.
20  */
21
22
23 #pragma once
24
25 #include "inspircd.h"
26
27 #include "utils.h"
28
29 /*
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:
33  *
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
38  *
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.
49  *
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
57  *                                                      SERVER strings.
58  * CONNECTED:   represents a fully authorized, fully
59  *                                                      connected server.
60  * DYING:       represents a server that has had an error.
61  */
62 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
63
64 struct CapabData
65 {
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 */
78 };
79
80 /** Every SERVER connection inbound or outbound is represented by an object of
81  * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
82  * after setup, MyRoot will have been created as a child of Utils->TreeRoot
83  */
84 class TreeSocket : public BufferedSocket
85 {
86         SpanningTreeUtilities* Utils;           /* Utility class */
87         std::string linkID;                     /* Description for this link */
88         ServerState LinkState;                  /* Link state */
89         CapabData* capab;                       /* Link setup data (held until burst is sent) */
90         TreeServer* MyRoot;                     /* The server we are talking to */
91         time_t NextPing;                        /* Time when we are due to ping this server */
92         bool LastPingWasGood;                   /* Responded to last ping we sent? */
93         int proto_version;                      /* Remote protocol version */
94         bool ConnectionFailureShown; /* Set to true if a connection failure message was shown */
95  public:
96         time_t age;
97
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
101          * to it.
102          */
103         TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr);
104
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.
108          */
109         TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
110
111         /** Get link state
112          */
113         ServerState GetLinkState();
114
115         /** Get challenge set in our CAPAB for challenge/response
116          */
117         const std::string& GetOurChallenge();
118
119         /** Get challenge set in our CAPAB for challenge/response
120          */
121         void SetOurChallenge(const std::string &c);
122
123         /** Get challenge set in their CAPAB for challenge/response
124          */
125         const std::string& GetTheirChallenge();
126
127         /** Get challenge set in their CAPAB for challenge/response
128          */
129         void SetTheirChallenge(const std::string &c);
130
131         /** Compare two passwords based on authentication scheme
132          */
133         bool ComparePass(const Link& link, const std::string &theirs);
134
135         /** Clean up information used only during server negotiation
136          */
137         void CleanNegotiationInfo();
138
139         CullResult cull();
140         /** Destructor
141          */
142         ~TreeSocket();
143
144         /** Construct a password, optionally hashed with the other side's
145          * challenge string
146          */
147         std::string MakePass(const std::string &password, const std::string &challenge);
148
149         /** When an outbound connection finishes connecting, we receive
150          * this event, and must send our SERVER string to the other
151          * side. If the other side is happy, as outlined in the server
152          * to server docs on the inspircd.org site, the other side
153          * will then send back its own server string.
154          */
155         virtual void OnConnected();
156
157         /** Handle socket error event
158          */
159         virtual void OnError(BufferedSocketError e);
160
161         /** Sends an error to the remote server, and displays it locally to show
162          * that it was sent.
163          */
164         void SendError(const std::string &errormessage);
165
166         /** Recursively send the server tree with distances as hops.
167          * This is used during network burst to inform the other server
168          * (and any of ITS servers too) of what servers we know about.
169          */
170         void SendServers(TreeServer* Current, TreeServer* s);
171
172         /** Returns module list as a string, filtered by filter
173          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
174          */
175         std::string MyModules(int filter);
176
177         /** Send my capabilities to the remote side
178          */
179         void SendCapabilities(int phase);
180
181         /* Isolate and return the elements that are different between two lists */
182         void ListDifference(const std::string &one, const std::string &two, char sep,
183                 std::string& mleft, std::string& mright);
184
185         bool Capab(const parameterlist &params);
186
187         /** This function forces this server to quit, removing this server
188          * and any users on it (and servers and users below that, etc etc).
189          * It's very slow and pretty clunky, but luckily unless your network
190          * is having a REAL bad hair day, this function shouldnt be called
191          * too many times a month ;-)
192          */
193         void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
194
195         /** This is a wrapper function for SquitServer above, which
196          * does some validation first and passes on the SQUIT to all
197          * other remaining servers.
198          */
199         void Squit(TreeServer* Current, const std::string &reason);
200
201         /* Used on nick collision ... XXX ugly function HACK */
202         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
203
204         /** Send one or more FJOINs for a channel of users.
205          * If the length of a single line is more than 480-NICKMAX
206          * in length, it is split over multiple lines.
207          */
208         void SendFJoins(Channel* c);
209
210         /** Send G, Q, Z and E lines */
211         void SendXLines();
212
213         /** Send all known information about a channel */
214         void SyncChannel(Channel* chan);
215
216         /** send all users and their oper state/modes */
217         void SendUsers();
218
219         /** This function is called when we want to send a netburst to a local
220          * server. There is a set order we must do this, because for example
221          * users require their servers to exist, and channels require their
222          * users to exist. You get the idea.
223          */
224         void DoBurst(TreeServer* s);
225
226         /** This function is called when we receive data from a remote
227          * server.
228          */
229         void OnDataReady();
230
231         /** Send one or more complete lines down the socket
232          */
233         void WriteLine(std::string line);
234
235         /** Handle ERROR command */
236         void Error(parameterlist &params);
237
238         /** Remote AWAY */
239         bool Away(const std::string &prefix, parameterlist &params);
240
241         /** SAVE to resolve nick collisions without killing */
242         bool ForceNick(const std::string &prefix, parameterlist &params);
243
244         /** ENCAP command
245          */
246         void Encap(User* who, parameterlist &params);
247
248         /** OPERQUIT command
249          */
250         bool OperQuit(const std::string &prefix, parameterlist &params);
251
252         /** PONG
253          */
254         bool LocalPong(const std::string &prefix, parameterlist &params);
255
256         /** VERSION
257          */
258         bool ServerVersion(const std::string &prefix, parameterlist &params);
259
260         /** ADDLINE
261          */
262         bool AddLine(const std::string &prefix, parameterlist &params);
263
264         /** DELLINE
265          */
266         bool DelLine(const std::string &prefix, parameterlist &params);
267
268         /** WHOIS
269          */
270         bool Whois(const std::string &prefix, parameterlist &params);
271
272         /** PUSH
273          */
274         bool Push(const std::string &prefix, parameterlist &params);
275
276         /** PING
277          */
278         bool LocalPing(const std::string &prefix, parameterlist &params);
279
280         /** <- (remote) <- SERVER
281          */
282         bool RemoteServer(const std::string &prefix, parameterlist &params);
283
284         /** (local) -> SERVER
285          */
286         bool Outbound_Reply_Server(parameterlist &params);
287
288         /** (local) <- SERVER
289          */
290         bool Inbound_Server(parameterlist &params);
291
292         /** Handle IRC line split
293          */
294         void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist &params);
295
296         /** Process complete line from buffer
297          */
298         void ProcessLine(std::string &line);
299
300         void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
301
302         /** Handle socket timeout from connect()
303          */
304         virtual void OnTimeout();
305         /** Handle server quit on close
306          */
307         virtual void Close();
308
309         /** Returns true if this server was introduced to the rest of the network
310          */
311         bool Introduced();
312
313         /** Fixes messages coming from old servers so the new command handlers understand them
314          */
315         bool PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params);
316 };