]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
db19f76ac76306b377cc0bd5bd00ec0d9f28b8f3
[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         int num_lost_users;                     /* Users lost in split */
87         int num_lost_servers;                   /* Servers lost in split */
88         time_t NextPing;                        /* Time when we are due to ping this server */
89         bool LastPingWasGood;                   /* Responded to last ping we sent? */
90         bool bursting;                          /* True if not finished bursting yet */
91         unsigned int keylength;                 /* Is this still used? */
92         std::string ModuleList;                 /* Module list of other server from CAPAB */
93         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
94         Module* Hook;                           /* I/O hooking module that we're attached to for this socket */
95         std::string ourchallenge;               /* Challenge sent for challenge/response */
96         std::string theirchallenge;             /* Challenge recv for challenge/response */
97         std::string OutboundPass;               /* Outbound password */
98
99  public:
100
101         /** Because most of the I/O gubbins are encapsulated within
102          * InspSocket, we just call the superclass constructor for
103          * most of the action, and append a few of our own values
104          * to it.
105          */
106         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, Module* HookMod = NULL);
107
108         /** Because most of the I/O gubbins are encapsulated within
109          * InspSocket, we just call the superclass constructor for
110          * most of the action, and append a few of our own values
111          * to it.
112          */
113         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);
114
115         /** When a listening socket gives us a new file descriptor,
116          * we must associate it with a socket without creating a new
117          * connection. This constructor is used for this purpose.
118          */
119         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
120
121         /** Get link state
122          */
123         ServerState GetLinkState();
124
125         /** Get challenge set in our CAPAB for challenge/response
126          */
127         const std::string& GetOurChallenge();
128
129         /** Get challenge set in our CAPAB for challenge/response
130          */
131         void SetOurChallenge(const std::string &c);
132
133         /** Get challenge set in their CAPAB for challenge/response
134          */
135         const std::string& GetTheirChallenge();
136
137         /** Get challenge set in their CAPAB for challenge/response
138          */
139         void SetTheirChallenge(const std::string &c);
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(InspSocketError e);
169
170         /** Handle socket disconnect event
171          */
172         virtual int OnDisconnect();
173
174         /** Recursively send the server tree with distances as hops.
175          * This is used during network burst to inform the other server
176          * (and any of ITS servers too) of what servers we know about.
177          * If at any point any of these servers already exist on the other
178          * end, our connection may be terminated. The hopcounts given
179          * by this function are relative, this doesn't matter so long as
180          * they are all >1, as all the remote servers re-calculate them
181          * to be relative too, with themselves as hop 0.
182          */
183         void SendServers(TreeServer* Current, TreeServer* s, int hops);
184
185         /** Returns my capabilities as a string
186          */
187         std::string MyCapabilities();
188
189         /** Send my capabilities to the remote side
190          */
191         void SendCapabilities();
192
193         /* Check a comma seperated list for an item */
194         bool HasItem(const std::string &list, const std::string &item);
195
196         /* Isolate and return the elements that are different between two comma seperated lists */
197         std::string ListDifference(const std::string &one, const std::string &two);
198
199         bool Capab(const std::deque<std::string> &params);
200
201         /** This function forces this server to quit, removing this server
202          * and any users on it (and servers and users below that, etc etc).
203          * It's very slow and pretty clunky, but luckily unless your network
204          * is having a REAL bad hair day, this function shouldnt be called
205          * too many times a month ;-)
206          */
207         void SquitServer(std::string &from, TreeServer* Current);
208
209         /** This is a wrapper function for SquitServer above, which
210          * does some validation first and passes on the SQUIT to all
211          * other remaining servers.
212          */
213         void Squit(TreeServer* Current, const std::string &reason);
214
215         /** FMODE command - server mode with timestamp checks */
216         bool ForceMode(const std::string &source, std::deque<std::string> &params);
217
218         /** FTOPIC command */
219         bool ForceTopic(const std::string &source, std::deque<std::string> &params);
220
221         /** FJOIN, similar to TS6 SJOIN, but not quite. */
222         bool ForceJoin(const std::string &source, std::deque<std::string> &params);
223
224         /** NICK command */
225         bool IntroduceClient(const std::string &source, std::deque<std::string> &params);
226
227         /** Send one or more FJOINs for a channel of users.
228          * If the length of a single line is more than 480-NICKMAX
229          * in length, it is split over multiple lines.
230          */
231         void SendFJoins(TreeServer* Current, chanrec* c);
232
233         /** Send G, Q, Z and E lines */
234         void SendXLines(TreeServer* Current);
235
236         /** Send channel modes and topics */
237         void SendChannelModes(TreeServer* Current);
238
239         /** send all users and their oper state/modes */
240         void SendUsers(TreeServer* Current);
241
242         /** This function is called when we want to send a netburst to a local
243          * server. There is a set order we must do this, because for example
244          * users require their servers to exist, and channels require their
245          * users to exist. You get the idea.
246          */
247         void DoBurst(TreeServer* s);
248
249         /** This function is called when we receive data from a remote
250          * server. We buffer the data in a std::string (it doesnt stay
251          * there for long), reading using InspSocket::Read() which can
252          * read up to 16 kilobytes in one operation.
253          *
254          * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
255          * THE SOCKET OBJECT FOR US.
256          */
257         virtual bool OnDataReady();
258
259         /** Send one or more complete lines down the socket
260          */
261         int WriteLine(std::string line);
262
263         /** Handle ERROR command */
264         bool Error(std::deque<std::string> &params);
265
266         /** remote MOTD. leet, huh? */
267         bool Motd(const std::string &prefix, std::deque<std::string> &params);
268
269         /** remote ADMIN. leet, huh? */
270         bool Admin(const std::string &prefix, std::deque<std::string> &params);
271
272         /** Remote MODULES */
273         bool Modules(const std::string &prefix, std::deque<std::string> &params);
274
275         bool Stats(const std::string &prefix, std::deque<std::string> &params);
276
277         /** Because the core won't let users or even SERVERS set +o,
278          * we use the OPERTYPE command to do this.
279          */
280         bool OperType(const std::string &prefix, std::deque<std::string> &params);
281
282         /** Because Andy insists that services-compatible servers must
283          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
284          */
285         bool ForceNick(const std::string &prefix, std::deque<std::string> &params);
286
287         bool OperQuit(const std::string &prefix, std::deque<std::string> &params);
288
289         /** Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
290          * then let that server do the dirty work (squit it!). Example:
291          * A -> B -> C -> D: oper on A squits D, A routes to B, B routes to C, C notices D connected locally, kills it. -- w00t
292          */
293         bool RemoteSquit(const std::string &prefix, std::deque<std::string> &params);
294
295         /** SVSJOIN
296          */
297         bool ServiceJoin(const std::string &prefix, std::deque<std::string> &params);
298
299         /** REHASH
300          */
301         bool RemoteRehash(const std::string &prefix, std::deque<std::string> &params);
302
303         /** KILL
304          */
305         bool RemoteKill(const std::string &prefix, std::deque<std::string> &params);
306
307         /** PONG
308          */
309         bool LocalPong(const std::string &prefix, std::deque<std::string> &params);
310
311         /** METADATA
312          */
313         bool MetaData(const std::string &prefix, std::deque<std::string> &params);
314
315         /** VERSION
316          */
317         bool ServerVersion(const std::string &prefix, std::deque<std::string> &params);
318
319         /** CHGHOST
320          */
321         bool ChangeHost(const std::string &prefix, std::deque<std::string> &params);
322
323         /** ADDLINE
324          */
325         bool AddLine(const std::string &prefix, std::deque<std::string> &params);
326
327         /** CHGNAME
328          */
329         bool ChangeName(const std::string &prefix, std::deque<std::string> &params);
330
331         /** WHOIS
332          */
333         bool Whois(const std::string &prefix, std::deque<std::string> &params);
334
335         /** PUSH
336          */
337         bool Push(const std::string &prefix, std::deque<std::string> &params);
338
339         /** SETTIME
340          */
341         bool HandleSetTime(const std::string &prefix, std::deque<std::string> &params);
342
343         /** TIME
344          */
345         bool Time(const std::string &prefix, std::deque<std::string> &params);
346
347         /** PING
348          */
349         bool LocalPing(const std::string &prefix, std::deque<std::string> &params);
350
351         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
352          * This does not update the timestamp of the target channel, this must be done seperately.
353          */
354         bool RemoveStatus(const std::string &prefix, std::deque<std::string> &params);
355
356         /** <- (remote) <- SERVER
357          */
358         bool RemoteServer(const std::string &prefix, std::deque<std::string> &params);
359
360         /** (local) -> SERVER
361          */
362         bool Outbound_Reply_Server(std::deque<std::string> &params);
363
364         /** (local) <- SERVER
365          */
366         bool Inbound_Server(std::deque<std::string> &params);
367
368         /** Handle netsplit
369          */
370         void Split(const std::string &line, std::deque<std::string> &n);
371
372         /** Process complete line from buffer
373          */
374         bool ProcessLine(std::string &line);
375
376         /** Get this server's name
377          */
378         virtual std::string GetName();
379
380         /** Handle socket timeout from connect()
381          */
382         virtual void OnTimeout();
383
384         /** Handle socket close event
385          */
386         virtual void OnClose();
387
388         /** Handle incoming connection event
389          */
390         virtual int OnIncomingConnection(int newsock, char* ip);
391 };
392
393 #endif
394