]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Fix REMOVE maxparams
[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 "socket.h"
18 #include "inspircd.h"
19 #include "xline.h"
20 #include "../transport.h"
21
22 #include "utils.h"
23 #include "handshaketimer.h"
24
25 /*
26  * The server list in InspIRCd is maintained as two structures
27  * which hold the data in different ways. Most of the time, we
28  * want to very quicky obtain three pieces of information:
29  *
30  * (1) The information on a server
31  * (2) The information on the server we must send data through
32  *     to actually REACH the server we're after
33  * (3) Potentially, the child/parent objects of this server
34  *
35  * The InspIRCd spanning protocol provides easy access to these
36  * by storing the data firstly in a recursive structure, where
37  * each item references its parent item, and a dynamic list
38  * of child items, and another structure which stores the items
39  * hashed, linearly. This means that if we want to find a server
40  * by name quickly, we can look it up in the hash, avoiding
41  * any O(n) lookups. If however, during a split or sync, we want
42  * to apply an operation to a server, and any of its child objects
43  * we can resort to recursion to walk the tree structure.
44  * Any socket can have one of five states at any one time.
45  *
46  * CONNECTING:  indicates an outbound socket which is
47  *                                                      waiting to be writeable.
48  * WAIT_AUTH_1: indicates the socket is outbound and
49  *                                                      has successfully connected, but has not
50  *                                                      yet sent and received SERVER strings.
51  * WAIT_AUTH_2: indicates that the socket is inbound
52  *                                                      but has not yet sent and received
53  *                                                      SERVER strings.
54  * CONNECTED:           represents a fully authorized, fully
55  *                                                      connected server.
56  */
57 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
58
59 /** Every SERVER connection inbound or outbound is represented by
60  * an object of type TreeSocket.
61  * TreeSockets, being inherited from BufferedSocket, can be tied into
62  * the core socket engine, and we cn therefore receive activity events
63  * for them, just like activex objects on speed. (yes really, that
64  * is a technical term!) Each of these which relates to a locally
65  * connected server is assocated with it, by hooking it onto a
66  * TreeSocket class using its constructor. In this way, we can
67  * maintain a list of servers, some of which are directly connected,
68  * some of which are not.
69  */
70 class TreeSocket : public BufferedSocket
71 {
72         SpanningTreeUtilities* Utils;           /* Utility class */
73         std::string myhost;                     /* Canonical hostname */
74         std::string in_buffer;                  /* Input buffer */
75         ServerState LinkState;                  /* Link state */
76         std::string InboundServerName;          /* Server name sent to us by other side */
77         std::string InboundDescription;         /* Server description (GECOS) sent to us by the other side */
78         std::string InboundSID;                 /* Server ID sent to us by the other side */
79         int num_lost_users;                     /* Users lost in split */
80         int num_lost_servers;                   /* Servers lost in split */
81         time_t NextPing;                        /* Time when we are due to ping this server */
82         bool LastPingWasGood;                   /* Responded to last ping we sent? */
83         std::string ModuleList;                 /* Required module list of other server from CAPAB */
84         std::string OptModuleList;              /* Optional module list of other server from CAPAB */
85         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
86         Module* Hook;                           /* I/O hooking module that we're attached to for this socket */
87         std::string ourchallenge;               /* Challenge sent for challenge/response */
88         std::string theirchallenge;             /* Challenge recv for challenge/response */
89         std::string OutboundPass;               /* Outbound password */
90         int capab_phase;                        /* Have sent CAPAB already */
91         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
92         bool auth_challenge;                    /* Did we auth using challenge/response */
93         int proto_version;                      /* Remote protocol version */
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 module list as a string, filtered by filter
189          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
190          */
191         std::string MyModules(int filter);
192
193         /** Send my capabilities to the remote side
194          */
195         void SendCapabilities(int phase);
196
197         /** Add modules to VF_COMMON list for backwards compatability */
198         void CompatAddModules(std::vector<std::string>& modlist);
199
200         /* Check a comma seperated list for an item */
201         bool HasItem(const std::string &list, const std::string &item);
202
203         /* Isolate and return the elements that are different between two comma seperated lists */
204         std::string ListDifference(const std::string &one, const std::string &two);
205
206         bool Capab(const parameterlist &params);
207
208         /** This function forces this server to quit, removing this server
209          * and any users on it (and servers and users below that, etc etc).
210          * It's very slow and pretty clunky, but luckily unless your network
211          * is having a REAL bad hair day, this function shouldnt be called
212          * too many times a month ;-)
213          */
214         void SquitServer(std::string &from, TreeServer* Current);
215
216         /** This is a wrapper function for SquitServer above, which
217          * does some validation first and passes on the SQUIT to all
218          * other remaining servers.
219          */
220         void Squit(TreeServer* Current, const std::string &reason);
221
222         /** FMODE command - server mode with timestamp checks */
223         bool ForceMode(const std::string &source, parameterlist &params);
224
225         /** FTOPIC command */
226         bool ForceTopic(const std::string &source, parameterlist &params);
227
228         /** FJOIN, similar to TS6 SJOIN, but not quite. */
229         bool ForceJoin(const std::string &source, parameterlist &params);
230
231         /* Used on nick collision ... XXX ugly function HACK */
232         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
233
234         /** UID command */
235         bool ParseUID(const std::string &source, parameterlist &params);
236
237         /** Send one or more FJOINs for a channel of users.
238          * If the length of a single line is more than 480-NICKMAX
239          * in length, it is split over multiple lines.
240          */
241         void SendFJoins(TreeServer* Current, Channel* c);
242
243         /** Send G, Q, Z and E lines */
244         void SendXLines(TreeServer* Current);
245
246         /** Send channel modes and topics */
247         void SendChannelModes(TreeServer* Current);
248
249         /** send all users and their oper state/modes */
250         void SendUsers(TreeServer* Current);
251
252         /** This function is called when we want to send a netburst to a local
253          * server. There is a set order we must do this, because for example
254          * users require their servers to exist, and channels require their
255          * users to exist. You get the idea.
256          */
257         void DoBurst(TreeServer* s);
258
259         /** This function is called when we receive data from a remote
260          * server. We buffer the data in a std::string (it doesnt stay
261          * there for long), reading using BufferedSocket::Read() which can
262          * read up to 16 kilobytes in one operation.
263          *
264          * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
265          * THE SOCKET OBJECT FOR US.
266          */
267         virtual bool OnDataReady();
268
269         /** Send one or more complete lines down the socket
270          */
271         void WriteLine(std::string line);
272
273         /** Handle ERROR command */
274         bool Error(parameterlist &params);
275
276         /** remote MOTD. leet, huh? */
277         bool Motd(const std::string &prefix, parameterlist &params);
278
279         /** remote ADMIN. leet, huh? */
280         bool Admin(const std::string &prefix, parameterlist &params);
281
282         /** Remote MODULES */
283         bool Modules(const std::string &prefix, parameterlist &params);
284
285         bool Stats(const std::string &prefix, parameterlist &params);
286
287         /** Because the core won't let users or even SERVERS set +o,
288          * we use the OPERTYPE command to do this.
289          */
290         bool OperType(const std::string &prefix, parameterlist &params);
291
292         /** Remote AWAY */
293         bool Away(const std::string &prefix, parameterlist &params);
294
295         /** Because Andy insists that services-compatible servers must
296          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
297          */
298         bool SVSNick(const std::string &prefix, parameterlist &params);
299
300         /** SAVE to resolve nick collisions without killing */
301         bool ForceNick(const std::string &prefix, parameterlist &params);
302
303         /** PRIVMSG or NOTICE with server origin ONLY
304          */
305         bool ServerMessage(const std::string &messagetype, const std::string &prefix, parameterlist &params, const std::string &sourceserv);
306
307         /** ENCAP command
308          */
309         bool Encap(const std::string &prefix, parameterlist &params);
310
311         /** OPERQUIT command
312          */
313         bool OperQuit(const std::string &prefix, parameterlist &params);
314
315         /** SVSJOIN
316          */
317         bool ServiceJoin(const std::string &prefix, parameterlist &params);
318
319         /** SVSPART
320          */
321         bool ServicePart(const std::string &prefix, parameterlist &params);
322
323         /** KILL
324          */
325         bool RemoteKill(const std::string &prefix, parameterlist &params);
326
327         /** PONG
328          */
329         bool LocalPong(const std::string &prefix, parameterlist &params);
330
331         /** METADATA
332          */
333         bool MetaData(const std::string &prefix, parameterlist &params);
334
335         /** VERSION
336          */
337         bool ServerVersion(const std::string &prefix, parameterlist &params);
338
339         /** CHGHOST
340          */
341         bool ChangeHost(const std::string &prefix, parameterlist &params);
342
343         /** ADDLINE
344          */
345         bool AddLine(const std::string &prefix, parameterlist &params);
346
347         /** DELLINE
348          */
349         bool DelLine(const std::string &prefix, parameterlist &params);
350
351         /** CHGNAME
352          */
353         bool ChangeName(const std::string &prefix, parameterlist &params);
354
355         /** FIDENT */
356         bool ChangeIdent(const std::string &prefix, parameterlist &params);
357
358         /** WHOIS
359          */
360         bool Whois(const std::string &prefix, parameterlist &params);
361
362         /** PUSH
363          */
364         bool Push(const std::string &prefix, parameterlist &params);
365
366         /** TIME
367          */
368         bool Time(const std::string &prefix, parameterlist &params);
369
370         /** PING
371          */
372         bool LocalPing(const std::string &prefix, parameterlist &params);
373
374         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
375          * This does not update the timestamp of the target channel, this must be done seperately.
376          */
377         bool RemoveStatus(const std::string &prefix, parameterlist &params);
378
379         /** <- (remote) <- SERVER
380          */
381         bool RemoteServer(const std::string &prefix, parameterlist &params);
382
383         /** (local) -> SERVER
384          */
385         bool Outbound_Reply_Server(parameterlist &params);
386
387         /** (local) <- SERVER
388          */
389         bool Inbound_Server(parameterlist &params);
390
391         /** Handle netsplit
392          */
393         void Split(const std::string &line, parameterlist &n);
394
395         /** Process complete line from buffer
396          */
397         bool ProcessLine(std::string &line);
398
399         /** Get this server's name
400          */
401         virtual std::string GetName();
402
403         /** Handle socket timeout from connect()
404          */
405         virtual void OnTimeout();
406
407         /** Handle socket close event
408          */
409         virtual void OnClose();
410 };
411
412 /* Used to validate the value lengths of multiple parameters for a command */
413 struct cmd_validation
414 {
415         const char* item;
416         size_t param;
417         size_t length;
418 };
419
420 /* Used to validate the length values in CAPAB CAPABILITIES */
421 struct cap_validation
422 {
423         const char* reason;
424         const char* key;
425         size_t size;
426 };
427
428 #endif
429