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