]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Tidy up source files:
[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 "socket.h"
26 #include "inspircd.h"
27 #include "xline.h"
28
29 #include "utils.h"
30
31 /*
32  * The server list in InspIRCd is maintained as two structures
33  * which hold the data in different ways. Most of the time, we
34  * want to very quicky obtain three pieces of information:
35  *
36  * (1) The information on a server
37  * (2) The information on the server we must send data through
38  *     to actually REACH the server we're after
39  * (3) Potentially, the child/parent objects of this server
40  *
41  * The InspIRCd spanning protocol provides easy access to these
42  * by storing the data firstly in a recursive structure, where
43  * each item references its parent item, and a dynamic list
44  * of child items, and another structure which stores the items
45  * hashed, linearly. This means that if we want to find a server
46  * by name quickly, we can look it up in the hash, avoiding
47  * any O(n) lookups. If however, during a split or sync, we want
48  * to apply an operation to a server, and any of its child objects
49  * we can resort to recursion to walk the tree structure.
50  * Any socket can have one of five states at any one time.
51  *
52  * CONNECTING:  indicates an outbound socket which is
53  *                                                      waiting to be writeable.
54  * WAIT_AUTH_1: indicates the socket is outbound and
55  *                                                      has successfully connected, but has not
56  *                                                      yet sent and received SERVER strings.
57  * WAIT_AUTH_2: indicates that the socket is inbound
58  *                                                      but has not yet sent and received
59  *                                                      SERVER strings.
60  * CONNECTED:   represents a fully authorized, fully
61  *                                                      connected server.
62  * DYING:       represents a server that has had an error.
63  */
64 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
65
66 struct CapabData
67 {
68         reference<Link> link;                   /* Link block used for this connection */
69         reference<Autoconnect> ac;              /* Autoconnect used to cause this connection, if any */
70         std::string ModuleList;                 /* Required module list of other server from CAPAB */
71         std::string OptModuleList;              /* Optional module list of other server from CAPAB */
72         std::string ChanModes;
73         std::string UserModes;
74         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
75         std::string ourchallenge;               /* Challenge sent for challenge/response */
76         std::string theirchallenge;             /* Challenge recv for challenge/response */
77         int capab_phase;                        /* Have sent CAPAB already */
78         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
79         bool auth_challenge;                    /* Did we auth using challenge/response */
80 };
81
82 /** Every SERVER connection inbound or outbound is represented by an object of
83  * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
84  * after setup, MyRoot will have been created as a child of Utils->TreeRoot
85  */
86 class TreeSocket : public BufferedSocket
87 {
88         SpanningTreeUtilities* Utils;           /* Utility class */
89         std::string linkID;                     /* Description for this link */
90         ServerState LinkState;                  /* Link state */
91         CapabData* capab;                       /* Link setup data (held until burst is sent) */
92         TreeServer* MyRoot;                     /* The server we are talking to */
93         time_t NextPing;                        /* Time when we are due to ping this server */
94         bool LastPingWasGood;                   /* Responded to last ping we sent? */
95         int proto_version;                      /* Remote protocol version */
96         bool ConnectionFailureShown; /* Set to true if a connection failure message was shown */
97  public:
98         time_t age;
99
100         /** Because most of the I/O gubbins are encapsulated within
101          * BufferedSocket, we just call the superclass constructor for
102          * most of the action, and append a few of our own values
103          * to it.
104          */
105         TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr);
106
107         /** When a listening socket gives us a new file descriptor,
108          * we must associate it with a socket without creating a new
109          * connection. This constructor is used for this purpose.
110          */
111         TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
112
113         /** Get link state
114          */
115         ServerState GetLinkState();
116
117         /** Get challenge set in our CAPAB for challenge/response
118          */
119         const std::string& GetOurChallenge();
120
121         /** Get challenge set in our CAPAB for challenge/response
122          */
123         void SetOurChallenge(const std::string &c);
124
125         /** Get challenge set in their CAPAB for challenge/response
126          */
127         const std::string& GetTheirChallenge();
128
129         /** Get challenge set in their CAPAB for challenge/response
130          */
131         void SetTheirChallenge(const std::string &c);
132
133         /** Compare two passwords based on authentication scheme
134          */
135         bool ComparePass(const Link& link, const std::string &theirs);
136
137         /** Clean up information used only during server negotiation
138          */
139         void CleanNegotiationInfo();
140
141         CullResult cull();
142         /** Destructor
143          */
144         ~TreeSocket();
145
146         /** Construct a password, optionally hashed with the other side's
147          * challenge string
148          */
149         std::string MakePass(const std::string &password, const std::string &challenge);
150
151         /** When an outbound connection finishes connecting, we receive
152          * this event, and must send our SERVER string to the other
153          * side. If the other side is happy, as outlined in the server
154          * to server docs on the inspircd.org site, the other side
155          * will then send back its own server string.
156          */
157         virtual void OnConnected();
158
159         /** Handle socket error event
160          */
161         virtual void OnError(BufferedSocketError e);
162
163         /** Sends an error to the remote server, and displays it locally to show
164          * that it was sent.
165          */
166         void SendError(const std::string &errormessage);
167
168         /** Recursively send the server tree with distances as hops.
169          * This is used during network burst to inform the other server
170          * (and any of ITS servers too) of what servers we know about.
171          * If at any point any of these servers already exist on the other
172          * end, our connection may be terminated. The hopcounts given
173          * by this function are relative, this doesn't matter so long as
174          * they are all >1, as all the remote servers re-calculate them
175          * to be relative too, with themselves as hop 0.
176          */
177         void SendServers(TreeServer* Current, TreeServer* s, int hops);
178
179         /** Returns module list as a string, filtered by filter
180          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
181          */
182         std::string MyModules(int filter);
183
184         /** Send my capabilities to the remote side
185          */
186         void SendCapabilities(int phase);
187
188         /* Isolate and return the elements that are different between two lists */
189         void ListDifference(const std::string &one, const std::string &two, char sep,
190                 std::string& mleft, std::string& mright);
191
192         bool Capab(const parameterlist &params);
193
194         /** This function forces this server to quit, removing this server
195          * and any users on it (and servers and users below that, etc etc).
196          * It's very slow and pretty clunky, but luckily unless your network
197          * is having a REAL bad hair day, this function shouldnt be called
198          * too many times a month ;-)
199          */
200         void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
201
202         /** This is a wrapper function for SquitServer above, which
203          * does some validation first and passes on the SQUIT to all
204          * other remaining servers.
205          */
206         void Squit(TreeServer* Current, const std::string &reason);
207
208         /* Used on nick collision ... XXX ugly function HACK */
209         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
210
211         /** Send one or more FJOINs for a channel of users.
212          * If the length of a single line is more than 480-NICKMAX
213          * in length, it is split over multiple lines.
214          */
215         void SendFJoins(Channel* c);
216
217         /** Send G, Q, Z and E lines */
218         void SendXLines();
219
220         /** Send channel modes and topics */
221         void SendChannelModes();
222
223         /** send all users and their oper state/modes */
224         void SendUsers();
225
226         /** This function is called when we want to send a netburst to a local
227          * server. There is a set order we must do this, because for example
228          * users require their servers to exist, and channels require their
229          * users to exist. You get the idea.
230          */
231         void DoBurst(TreeServer* s);
232
233         /** This function is called when we receive data from a remote
234          * server.
235          */
236         void OnDataReady();
237
238         /** Send one or more complete lines down the socket
239          */
240         void WriteLine(std::string line);
241
242         /** Handle ERROR command */
243         void Error(parameterlist &params);
244
245         /** Remote AWAY */
246         bool Away(const std::string &prefix, parameterlist &params);
247
248         /** SAVE to resolve nick collisions without killing */
249         bool ForceNick(const std::string &prefix, parameterlist &params);
250
251         /** ENCAP command
252          */
253         void Encap(User* who, parameterlist &params);
254
255         /** OPERQUIT command
256          */
257         bool OperQuit(const std::string &prefix, parameterlist &params);
258
259         /** PONG
260          */
261         bool LocalPong(const std::string &prefix, parameterlist &params);
262
263         /** VERSION
264          */
265         bool ServerVersion(const std::string &prefix, parameterlist &params);
266
267         /** ADDLINE
268          */
269         bool AddLine(const std::string &prefix, parameterlist &params);
270
271         /** DELLINE
272          */
273         bool DelLine(const std::string &prefix, parameterlist &params);
274
275         /** WHOIS
276          */
277         bool Whois(const std::string &prefix, parameterlist &params);
278
279         /** PUSH
280          */
281         bool Push(const std::string &prefix, parameterlist &params);
282
283         /** PING
284          */
285         bool LocalPing(const std::string &prefix, parameterlist &params);
286
287         /** <- (remote) <- SERVER
288          */
289         bool RemoteServer(const std::string &prefix, parameterlist &params);
290
291         /** (local) -> SERVER
292          */
293         bool Outbound_Reply_Server(parameterlist &params);
294
295         /** (local) <- SERVER
296          */
297         bool Inbound_Server(parameterlist &params);
298
299         /** Handle IRC line split
300          */
301         void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist &params);
302
303         /** Process complete line from buffer
304          */
305         void ProcessLine(std::string &line);
306
307         void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
308
309         /** Handle socket timeout from connect()
310          */
311         virtual void OnTimeout();
312         /** Handle server quit on close
313          */
314         virtual void Close();
315
316         /** Returns true if this server was introduced to the rest of the network
317          */
318         bool Introduced();
319 };