]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
de9c78fef42bd48d9c62e066f1f411493321dd67
[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
97         /** Because most of the I/O gubbins are encapsulated within
98          * BufferedSocket, we just call the superclass constructor for
99          * most of the action, and append a few of our own values
100          * to it.
101          */
102         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Module* HookMod = NULL);
103
104         /** When a listening socket gives us a new file descriptor,
105          * we must associate it with a socket without creating a new
106          * connection. This constructor is used for this purpose.
107          */
108         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
109
110         /** Get link state
111          */
112         ServerState GetLinkState();
113
114         /** Get challenge set in our CAPAB for challenge/response
115          */
116         const std::string& GetOurChallenge();
117
118         /** Get challenge set in our CAPAB for challenge/response
119          */
120         void SetOurChallenge(const std::string &c);
121
122         /** Get challenge set in their CAPAB for challenge/response
123          */
124         const std::string& GetTheirChallenge();
125
126         /** Get challenge set in their CAPAB for challenge/response
127          */
128         void SetTheirChallenge(const std::string &c);
129
130         /** Compare two passwords based on authentication scheme
131          */
132         bool ComparePass(const Link& link, const std::string &theirs);
133
134         /** Clean up information used only during server negotiation
135          */
136         void CleanNegotiationInfo();
137
138         /** Return the module which we are hooking to for I/O encapsulation
139          */
140         Module* GetHook();
141
142         /** Destructor
143          */
144         ~TreeSocket();
145
146         /** Generate random string used for challenge-response auth
147          */
148         std::string RandString(unsigned int length);
149
150         /** Construct a password, optionally hashed with the other side's
151          * challenge string
152          */
153         std::string MakePass(const std::string &password, const std::string &challenge);
154
155         /** When an outbound connection finishes connecting, we receive
156          * this event, and must send our SERVER string to the other
157          * side. If the other side is happy, as outlined in the server
158          * to server docs on the inspircd.org site, the other side
159          * will then send back its own server string.
160          */
161         virtual bool OnConnected();
162
163         /** Handle socket error event
164          */
165         virtual void OnError(BufferedSocketError e);
166
167         /** Sends an error to the remote server, and displays it locally to show
168          * that it was sent.
169          */
170         void SendError(const std::string &errormessage);
171
172         /** Handle socket disconnect event
173          */
174         virtual int OnDisconnect();
175
176         /** Recursively send the server tree with distances as hops.
177          * This is used during network burst to inform the other server
178          * (and any of ITS servers too) of what servers we know about.
179          * If at any point any of these servers already exist on the other
180          * end, our connection may be terminated. The hopcounts given
181          * by this function are relative, this doesn't matter so long as
182          * they are all >1, as all the remote servers re-calculate them
183          * to be relative too, with themselves as hop 0.
184          */
185         void SendServers(TreeServer* Current, TreeServer* s, int hops);
186
187         /** Returns my capabilities as a string
188          */
189         std::string MyCapabilities();
190
191         /** Send my capabilities to the remote side
192          */
193         void SendCapabilities();
194
195         /* Check a comma seperated list for an item */
196         bool HasItem(const std::string &list, const std::string &item);
197
198         /* Isolate and return the elements that are different between two comma seperated lists */
199         std::string ListDifference(const std::string &one, const std::string &two);
200
201         bool Capab(const std::deque<std::string> &params);
202
203         /** This function forces this server to quit, removing this server
204          * and any users on it (and servers and users below that, etc etc).
205          * It's very slow and pretty clunky, but luckily unless your network
206          * is having a REAL bad hair day, this function shouldnt be called
207          * too many times a month ;-)
208          */
209         void SquitServer(std::string &from, TreeServer* Current);
210
211         /** This is a wrapper function for SquitServer above, which
212          * does some validation first and passes on the SQUIT to all
213          * other remaining servers.
214          */
215         void Squit(TreeServer* Current, const std::string &reason);
216
217         /** FMODE command - server mode with timestamp checks */
218         bool ForceMode(const std::string &source, std::deque<std::string> &params);
219
220         /** FTOPIC command */
221         bool ForceTopic(const std::string &source, std::deque<std::string> &params);
222
223         /** FJOIN, similar to TS6 SJOIN, but not quite. */
224         bool ForceJoin(const std::string &source, std::deque<std::string> &params);
225
226         /* Used on nick collision ... XXX ugly function HACK */
227         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
228
229         /** UID command */
230         bool ParseUID(const std::string &source, std::deque<std::string> &params);
231
232         /** Send one or more FJOINs for a channel of users.
233          * If the length of a single line is more than 480-NICKMAX
234          * in length, it is split over multiple lines.
235          */
236         void SendFJoins(TreeServer* Current, Channel* c);
237
238         /** Send G, Q, Z and E lines */
239         void SendXLines(TreeServer* Current);
240
241         /** Send channel modes and topics */
242         void SendChannelModes(TreeServer* Current);
243
244         /** send all users and their oper state/modes */
245         void SendUsers(TreeServer* Current);
246
247         /** This function is called when we want to send a netburst to a local
248          * server. There is a set order we must do this, because for example
249          * users require their servers to exist, and channels require their
250          * users to exist. You get the idea.
251          */
252         void DoBurst(TreeServer* s);
253
254         /** This function is called when we receive data from a remote
255          * server. We buffer the data in a std::string (it doesnt stay
256          * there for long), reading using BufferedSocket::Read() which can
257          * read up to 16 kilobytes in one operation.
258          *
259          * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
260          * THE SOCKET OBJECT FOR US.
261          */
262         virtual bool OnDataReady();
263
264         /** Send one or more complete lines down the socket
265          */
266         void WriteLine(std::string line);
267
268         /** Handle ERROR command */
269         bool Error(std::deque<std::string> &params);
270
271         /** remote MOTD. leet, huh? */
272         bool Motd(const std::string &prefix, std::deque<std::string> &params);
273
274         /** remote ADMIN. leet, huh? */
275         bool Admin(const std::string &prefix, std::deque<std::string> &params);
276
277         /** Remote MODULES */
278         bool Modules(const std::string &prefix, std::deque<std::string> &params);
279
280         bool Stats(const std::string &prefix, std::deque<std::string> &params);
281
282         /** Because the core won't let users or even SERVERS set +o,
283          * we use the OPERTYPE command to do this.
284          */
285         bool OperType(const std::string &prefix, std::deque<std::string> &params);
286
287         /** Because Andy insists that services-compatible servers must
288          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
289          */
290         bool ForceNick(const std::string &prefix, std::deque<std::string> &params);
291
292         /** PRIVMSG or NOTICE with server origin ONLY
293          */
294         bool ServerMessage(const std::string &messagetype, const std::string &prefix, std::deque<std::string> &params, const std::string &sourceserv);
295
296         /** ENCAP command
297          */
298         bool Encap(const std::string &prefix, std::deque<std::string> &params);
299
300         /** OPERQUIT command
301          */
302         bool OperQuit(const std::string &prefix, std::deque<std::string> &params);
303
304         /** SVSJOIN
305          */
306         bool ServiceJoin(const std::string &prefix, std::deque<std::string> &params);
307
308         /** SVSPART
309          */
310         bool ServicePart(const std::string &prefix, std::deque<std::string> &params);
311
312         /** KILL
313          */
314         bool RemoteKill(const std::string &prefix, std::deque<std::string> &params);
315
316         /** PONG
317          */
318         bool LocalPong(const std::string &prefix, std::deque<std::string> &params);
319
320         /** METADATA
321          */
322         bool MetaData(const std::string &prefix, std::deque<std::string> &params);
323
324         /** VERSION
325          */
326         bool ServerVersion(const std::string &prefix, std::deque<std::string> &params);
327
328         /** CHGHOST
329          */
330         bool ChangeHost(const std::string &prefix, std::deque<std::string> &params);
331
332         /** ADDLINE
333          */
334         bool AddLine(const std::string &prefix, std::deque<std::string> &params);
335
336         /** DELLINE
337          */
338         bool DelLine(const std::string &prefix, std::deque<std::string> &params);
339
340         /** CHGNAME
341          */
342         bool ChangeName(const std::string &prefix, std::deque<std::string> &params);
343
344         /** WHOIS
345          */
346         bool Whois(const std::string &prefix, std::deque<std::string> &params);
347
348         /** PUSH
349          */
350         bool Push(const std::string &prefix, std::deque<std::string> &params);
351
352         /** TIME
353          */
354         bool Time(const std::string &prefix, std::deque<std::string> &params);
355
356         /** PING
357          */
358         bool LocalPing(const std::string &prefix, std::deque<std::string> &params);
359
360         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
361          * This does not update the timestamp of the target channel, this must be done seperately.
362          */
363         bool RemoveStatus(const std::string &prefix, std::deque<std::string> &params);
364
365         /** <- (remote) <- SERVER
366          */
367         bool RemoteServer(const std::string &prefix, std::deque<std::string> &params);
368
369         /** (local) -> SERVER
370          */
371         bool Outbound_Reply_Server(std::deque<std::string> &params);
372
373         /** (local) <- SERVER
374          */
375         bool Inbound_Server(std::deque<std::string> &params);
376
377         /** Handle netsplit
378          */
379         void Split(const std::string &line, std::deque<std::string> &n);
380
381         /** Process complete line from buffer
382          */
383         bool ProcessLine(std::string &line);
384
385         /** Get this server's name
386          */
387         virtual std::string GetName();
388
389         /** Handle socket timeout from connect()
390          */
391         virtual void OnTimeout();
392
393         /** Handle socket close event
394          */
395         virtual void OnClose();
396 };
397
398 /* Used to validate the value lengths of multiple parameters for a command */
399 struct cmd_validation
400 {
401         const char* item;
402         size_t param;
403         size_t length;
404 };
405
406 /* Used to validate the length values in CAPAB CAPABILITIES */
407 struct cap_validation
408 {
409         const char* reason;
410         const char* key;
411         size_t size;
412 };
413
414 #endif
415