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