]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Remove unneeded Extensible inheritance and remove "age" field from classbase
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __TREESOCKET_H__
15 #define __TREESOCKET_H__
16
17 #include "commands/cmd_whois.h"
18 #include "commands/cmd_stats.h"
19 #include "socket.h"
20 #include "inspircd.h"
21 #include "xline.h"
22 #include "../transport.h"
23
24 #include "utils.h"
25 #include "handshaketimer.h"
26
27 /*
28  * The server list in InspIRCd is maintained as two structures
29  * which hold the data in different ways. Most of the time, we
30  * want to very quicky obtain three pieces of information:
31  *
32  * (1) The information on a server
33  * (2) The information on the server we must send data through
34  *     to actually REACH the server we're after
35  * (3) Potentially, the child/parent objects of this server
36  *
37  * The InspIRCd spanning protocol provides easy access to these
38  * by storing the data firstly in a recursive structure, where
39  * each item references its parent item, and a dynamic list
40  * of child items, and another structure which stores the items
41  * hashed, linearly. This means that if we want to find a server
42  * by name quickly, we can look it up in the hash, avoiding
43  * any O(n) lookups. If however, during a split or sync, we want
44  * to apply an operation to a server, and any of its child objects
45  * we can resort to recursion to walk the tree structure.
46  * Any socket can have one of five states at any one time.
47  *
48  * CONNECTING:  indicates an outbound socket which is
49  *                                                      waiting to be writeable.
50  * WAIT_AUTH_1: indicates the socket is outbound and
51  *                                                      has successfully connected, but has not
52  *                                                      yet sent and received SERVER strings.
53  * WAIT_AUTH_2: indicates that the socket is inbound
54  *                                                      but has not yet sent and received
55  *                                                      SERVER strings.
56  * CONNECTED:           represents a fully authorized, fully
57  *                                                      connected server.
58  */
59 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
60
61 /** Every SERVER connection inbound or outbound is represented by
62  * an object of type TreeSocket.
63  * TreeSockets, being inherited from BufferedSocket, can be tied into
64  * the core socket engine, and we cn therefore receive activity events
65  * for them, just like activex objects on speed. (yes really, that
66  * is a technical term!) Each of these which relates to a locally
67  * connected server is assocated with it, by hooking it onto a
68  * TreeSocket class using its constructor. In this way, we can
69  * maintain a list of servers, some of which are directly connected,
70  * some of which are not.
71  */
72 class TreeSocket : public BufferedSocket
73 {
74         SpanningTreeUtilities* Utils;           /* Utility class */
75         std::string myhost;                     /* Canonical hostname */
76         std::string in_buffer;                  /* Input buffer */
77         ServerState LinkState;                  /* Link state */
78         std::string InboundServerName;          /* Server name sent to us by other side */
79         std::string InboundDescription;         /* Server description (GECOS) sent to us by the other side */
80         std::string InboundSID;                 /* Server ID sent to us by the other side */
81         int num_lost_users;                     /* Users lost in split */
82         int num_lost_servers;                   /* Servers lost in split */
83         time_t NextPing;                        /* Time when we are due to ping this server */
84         bool LastPingWasGood;                   /* Responded to last ping we sent? */
85         std::string ModuleList;                 /* Module list of other server from CAPAB */
86         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
87         Module* Hook;                           /* I/O hooking module that we're attached to for this socket */
88         std::string ourchallenge;               /* Challenge sent for challenge/response */
89         std::string theirchallenge;             /* Challenge recv for challenge/response */
90         std::string OutboundPass;               /* Outbound password */
91         bool sentcapab;                         /* Have sent CAPAB already */
92         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
93         bool auth_challenge;                    /* Did we auth using challenge/response */
94  public:
95         HandshakeTimer* hstimer;                /* Handshake timer, needed to work around I/O hook buffering */
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, InspIRCd* SI, std::string host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Module* HookMod = NULL);
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, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
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         /** Return the module which we are hooking to for I/O encapsulation
140          */
141         Module* GetHook();
142
143         /** Destructor
144          */
145         ~TreeSocket();
146
147         /** Generate random string used for challenge-response auth
148          */
149         std::string RandString(unsigned int length);
150
151         /** Construct a password, optionally hashed with the other side's
152          * challenge string
153          */
154         std::string MakePass(const std::string &password, const std::string &challenge);
155
156         /** When an outbound connection finishes connecting, we receive
157          * this event, and must send our SERVER string to the other
158          * side. If the other side is happy, as outlined in the server
159          * to server docs on the inspircd.org site, the other side
160          * will then send back its own server string.
161          */
162         virtual bool OnConnected();
163
164         /** Handle socket error event
165          */
166         virtual void OnError(BufferedSocketError e);
167
168         /** Sends an error to the remote server, and displays it locally to show
169          * that it was sent.
170          */
171         void SendError(const std::string &errormessage);
172
173         /** Handle socket disconnect event
174          */
175         virtual int OnDisconnect();
176
177         /** Recursively send the server tree with distances as hops.
178          * This is used during network burst to inform the other server
179          * (and any of ITS servers too) of what servers we know about.
180          * If at any point any of these servers already exist on the other
181          * end, our connection may be terminated. The hopcounts given
182          * by this function are relative, this doesn't matter so long as
183          * they are all >1, as all the remote servers re-calculate them
184          * to be relative too, with themselves as hop 0.
185          */
186         void SendServers(TreeServer* Current, TreeServer* s, int hops);
187
188         /** Returns my capabilities as a string
189          */
190         std::string MyCapabilities();
191
192         /** Send my capabilities to the remote side
193          */
194         void SendCapabilities();
195
196         /* Check a comma seperated list for an item */
197         bool HasItem(const std::string &list, const std::string &item);
198
199         /* Isolate and return the elements that are different between two comma seperated lists */
200         std::string ListDifference(const std::string &one, const std::string &two);
201
202         bool Capab(const parameterlist &params);
203
204         /** This function forces this server to quit, removing this server
205          * and any users on it (and servers and users below that, etc etc).
206          * It's very slow and pretty clunky, but luckily unless your network
207          * is having a REAL bad hair day, this function shouldnt be called
208          * too many times a month ;-)
209          */
210         void SquitServer(std::string &from, TreeServer* Current);
211
212         /** This is a wrapper function for SquitServer above, which
213          * does some validation first and passes on the SQUIT to all
214          * other remaining servers.
215          */
216         void Squit(TreeServer* Current, const std::string &reason);
217
218         /** FMODE command - server mode with timestamp checks */
219         bool ForceMode(const std::string &source, parameterlist &params);
220
221         /** FTOPIC command */
222         bool ForceTopic(const std::string &source, parameterlist &params);
223
224         /** FJOIN, similar to TS6 SJOIN, but not quite. */
225         bool ForceJoin(const std::string &source, parameterlist &params);
226
227         /* Used on nick collision ... XXX ugly function HACK */
228         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
229
230         /** UID command */
231         bool ParseUID(const std::string &source, parameterlist &params);
232
233         /** Send one or more FJOINs for a channel of users.
234          * If the length of a single line is more than 480-NICKMAX
235          * in length, it is split over multiple lines.
236          */
237         void SendFJoins(TreeServer* Current, Channel* c);
238
239         /** Send G, Q, Z and E lines */
240         void SendXLines(TreeServer* Current);
241
242         /** Send channel modes and topics */
243         void SendChannelModes(TreeServer* Current);
244
245         /** send all users and their oper state/modes */
246         void SendUsers(TreeServer* Current);
247
248         /** This function is called when we want to send a netburst to a local
249          * server. There is a set order we must do this, because for example
250          * users require their servers to exist, and channels require their
251          * users to exist. You get the idea.
252          */
253         void DoBurst(TreeServer* s);
254
255         /** This function is called when we receive data from a remote
256          * server. We buffer the data in a std::string (it doesnt stay
257          * there for long), reading using BufferedSocket::Read() which can
258          * read up to 16 kilobytes in one operation.
259          *
260          * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
261          * THE SOCKET OBJECT FOR US.
262          */
263         virtual bool OnDataReady();
264
265         /** Send one or more complete lines down the socket
266          */
267         void WriteLine(std::string line);
268
269         /** Handle ERROR command */
270         bool Error(parameterlist &params);
271
272         /** remote MOTD. leet, huh? */
273         bool Motd(const std::string &prefix, parameterlist &params);
274
275         /** remote ADMIN. leet, huh? */
276         bool Admin(const std::string &prefix, parameterlist &params);
277
278         /** Remote MODULES */
279         bool Modules(const std::string &prefix, parameterlist &params);
280
281         bool Stats(const std::string &prefix, parameterlist &params);
282
283         /** Because the core won't let users or even SERVERS set +o,
284          * we use the OPERTYPE command to do this.
285          */
286         bool OperType(const std::string &prefix, parameterlist &params);
287
288         /** Because Andy insists that services-compatible servers must
289          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
290          */
291         bool ForceNick(const std::string &prefix, parameterlist &params);
292
293         /** PRIVMSG or NOTICE with server origin ONLY
294          */
295         bool ServerMessage(const std::string &messagetype, const std::string &prefix, parameterlist &params, const std::string &sourceserv);
296
297         /** ENCAP command
298          */
299         bool Encap(const std::string &prefix, parameterlist &params);
300
301         /** OPERQUIT command
302          */
303         bool OperQuit(const std::string &prefix, parameterlist &params);
304
305         /** SVSJOIN
306          */
307         bool ServiceJoin(const std::string &prefix, parameterlist &params);
308
309         /** SVSPART
310          */
311         bool ServicePart(const std::string &prefix, parameterlist &params);
312
313         /** KILL
314          */
315         bool RemoteKill(const std::string &prefix, parameterlist &params);
316
317         /** PONG
318          */
319         bool LocalPong(const std::string &prefix, parameterlist &params);
320
321         /** METADATA
322          */
323         bool MetaData(const std::string &prefix, parameterlist &params);
324
325         /** VERSION
326          */
327         bool ServerVersion(const std::string &prefix, parameterlist &params);
328
329         /** CHGHOST
330          */
331         bool ChangeHost(const std::string &prefix, parameterlist &params);
332
333         /** ADDLINE
334          */
335         bool AddLine(const std::string &prefix, parameterlist &params);
336
337         /** DELLINE
338          */
339         bool DelLine(const std::string &prefix, parameterlist &params);
340
341         /** CHGNAME
342          */
343         bool ChangeName(const std::string &prefix, parameterlist &params);
344
345         /** WHOIS
346          */
347         bool Whois(const std::string &prefix, parameterlist &params);
348
349         /** PUSH
350          */
351         bool Push(const std::string &prefix, parameterlist &params);
352
353         /** TIME
354          */
355         bool Time(const std::string &prefix, parameterlist &params);
356
357         /** PING
358          */
359         bool LocalPing(const std::string &prefix, parameterlist &params);
360
361         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
362          * This does not update the timestamp of the target channel, this must be done seperately.
363          */
364         bool RemoveStatus(const std::string &prefix, parameterlist &params);
365
366         /** <- (remote) <- SERVER
367          */
368         bool RemoteServer(const std::string &prefix, parameterlist &params);
369
370         /** (local) -> SERVER
371          */
372         bool Outbound_Reply_Server(parameterlist &params);
373
374         /** (local) <- SERVER
375          */
376         bool Inbound_Server(parameterlist &params);
377
378         /** Handle netsplit
379          */
380         void Split(const std::string &line, parameterlist &n);
381
382         /** Process complete line from buffer
383          */
384         bool ProcessLine(std::string &line);
385
386         /** Get this server's name
387          */
388         virtual std::string GetName();
389
390         /** Handle socket timeout from connect()
391          */
392         virtual void OnTimeout();
393
394         /** Handle socket close event
395          */
396         virtual void OnClose();
397 };
398
399 /* Used to validate the value lengths of multiple parameters for a command */
400 struct cmd_validation
401 {
402         const char* item;
403         size_t param;
404         size_t length;
405 };
406
407 /* Used to validate the length values in CAPAB CAPABILITIES */
408 struct cap_validation
409 {
410         const char* reason;
411         const char* key;
412         size_t size;
413 };
414
415 #endif
416