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