]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Make classbase and refcountbase uncopyable; expand comments on their indended uses
[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 "socket.h"
18 #include "inspircd.h"
19 #include "xline.h"
20
21 #include "utils.h"
22
23 /*
24  * The server list in InspIRCd is maintained as two structures
25  * which hold the data in different ways. Most of the time, we
26  * want to very quicky obtain three pieces of information:
27  *
28  * (1) The information on a server
29  * (2) The information on the server we must send data through
30  *     to actually REACH the server we're after
31  * (3) Potentially, the child/parent objects of this server
32  *
33  * The InspIRCd spanning protocol provides easy access to these
34  * by storing the data firstly in a recursive structure, where
35  * each item references its parent item, and a dynamic list
36  * of child items, and another structure which stores the items
37  * hashed, linearly. This means that if we want to find a server
38  * by name quickly, we can look it up in the hash, avoiding
39  * any O(n) lookups. If however, during a split or sync, we want
40  * to apply an operation to a server, and any of its child objects
41  * we can resort to recursion to walk the tree structure.
42  * Any socket can have one of five states at any one time.
43  *
44  * CONNECTING:  indicates an outbound socket which is
45  *                                                      waiting to be writeable.
46  * WAIT_AUTH_1: indicates the socket is outbound and
47  *                                                      has successfully connected, but has not
48  *                                                      yet sent and received SERVER strings.
49  * WAIT_AUTH_2: indicates that the socket is inbound
50  *                                                      but has not yet sent and received
51  *                                                      SERVER strings.
52  * CONNECTED:           represents a fully authorized, fully
53  *                                                      connected server.
54  */
55 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
56
57 /** Every SERVER connection inbound or outbound is represented by
58  * an object of type TreeSocket.
59  * TreeSockets, being inherited from BufferedSocket, can be tied into
60  * the core socket engine, and we cn therefore receive activity events
61  * for them, just like activex objects on speed. (yes really, that
62  * is a technical term!) Each of these which relates to a locally
63  * connected server is assocated with it, by hooking it onto a
64  * TreeSocket class using its constructor. In this way, we can
65  * maintain a list of servers, some of which are directly connected,
66  * some of which are not.
67  */
68 class TreeSocket : public BufferedSocket
69 {
70         SpanningTreeUtilities* Utils;           /* Utility class */
71         std::string myhost;                     /* Canonical hostname */
72         ServerState LinkState;                  /* Link state */
73         std::string InboundServerName;          /* Server name sent to us by other side */
74         std::string InboundDescription;         /* Server description (GECOS) sent to us by the other side */
75         std::string InboundSID;                 /* Server ID sent to us by the other side */
76         int num_lost_users;                     /* Users lost in split */
77         int num_lost_servers;                   /* Servers lost in split */
78         time_t NextPing;                        /* Time when we are due to ping this server */
79         bool LastPingWasGood;                   /* Responded to last ping we sent? */
80         std::string IP;
81         std::string ModuleList;                 /* Required module list of other server from CAPAB */
82         std::string OptModuleList;              /* Optional module list of other server from CAPAB */
83         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
84         std::string ourchallenge;               /* Challenge sent for challenge/response */
85         std::string theirchallenge;             /* Challenge recv for challenge/response */
86         std::string OutboundPass;               /* Outbound password */
87         int capab_phase;                        /* Have sent CAPAB already */
88         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
89         bool auth_challenge;                    /* Did we auth using challenge/response */
90         int proto_version;                      /* Remote protocol version */
91  public:
92         reference<Autoconnect> myautoconnect;           /* Autoconnect used to cause this connection, if any */
93         time_t age;
94
95         /** Because most of the I/O gubbins are encapsulated within
96          * BufferedSocket, we just call the superclass constructor for
97          * most of the action, and append a few of our own values
98          * to it.
99          */
100         TreeSocket(SpanningTreeUtilities* Util, const std::string& host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, const std::string& Hook);
101
102         /** When a listening socket gives us a new file descriptor,
103          * we must associate it with a socket without creating a new
104          * connection. This constructor is used for this purpose.
105          */
106         TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocketBase* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
107
108         /** Get link state
109          */
110         ServerState GetLinkState();
111
112         /** Get challenge set in our CAPAB for challenge/response
113          */
114         const std::string& GetOurChallenge();
115
116         /** Get challenge set in our CAPAB for challenge/response
117          */
118         void SetOurChallenge(const std::string &c);
119
120         /** Get challenge set in their CAPAB for challenge/response
121          */
122         const std::string& GetTheirChallenge();
123
124         /** Get challenge set in their CAPAB for challenge/response
125          */
126         void SetTheirChallenge(const std::string &c);
127
128         /** Compare two passwords based on authentication scheme
129          */
130         bool ComparePass(const Link& link, const std::string &theirs);
131
132         /** Clean up information used only during server negotiation
133          */
134         void CleanNegotiationInfo();
135
136         CullResult cull();
137         /** Destructor
138          */
139         ~TreeSocket();
140
141         /** Generate random string used for challenge-response auth
142          */
143         std::string RandString(unsigned int length);
144
145         /** Construct a password, optionally hashed with the other side's
146          * challenge string
147          */
148         std::string MakePass(const std::string &password, const std::string &challenge);
149
150         /** When an outbound connection finishes connecting, we receive
151          * this event, and must send our SERVER string to the other
152          * side. If the other side is happy, as outlined in the server
153          * to server docs on the inspircd.org site, the other side
154          * will then send back its own server string.
155          */
156         virtual void OnConnected();
157
158         /** Handle socket error event
159          */
160         virtual void OnError(BufferedSocketError e);
161
162         /** Sends an error to the remote server, and displays it locally to show
163          * that it was sent.
164          */
165         void SendError(const std::string &errormessage);
166
167         /** Recursively send the server tree with distances as hops.
168          * This is used during network burst to inform the other server
169          * (and any of ITS servers too) of what servers we know about.
170          * If at any point any of these servers already exist on the other
171          * end, our connection may be terminated. The hopcounts given
172          * by this function are relative, this doesn't matter so long as
173          * they are all >1, as all the remote servers re-calculate them
174          * to be relative too, with themselves as hop 0.
175          */
176         void SendServers(TreeServer* Current, TreeServer* s, int hops);
177
178         /** Returns module list as a string, filtered by filter
179          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
180          */
181         std::string MyModules(int filter);
182
183         /** Send my capabilities to the remote side
184          */
185         void SendCapabilities(int phase);
186
187         /** Add modules to VF_COMMON list for backwards compatability */
188         void CompatAddModules(std::vector<std::string>& modlist);
189
190         /* Check a comma seperated list for an item */
191         bool HasItem(const std::string &list, const std::string &item);
192
193         /* Isolate and return the elements that are different between two comma seperated lists */
194         std::string ListDifference(const std::string &one, const std::string &two);
195
196         bool Capab(const parameterlist &params);
197
198         /** This function forces this server to quit, removing this server
199          * and any users on it (and servers and users below that, etc etc).
200          * It's very slow and pretty clunky, but luckily unless your network
201          * is having a REAL bad hair day, this function shouldnt be called
202          * too many times a month ;-)
203          */
204         void SquitServer(std::string &from, TreeServer* Current);
205
206         /** This is a wrapper function for SquitServer above, which
207          * does some validation first and passes on the SQUIT to all
208          * other remaining servers.
209          */
210         void Squit(TreeServer* Current, const std::string &reason);
211
212         /** FMODE command - server mode with timestamp checks */
213         bool ForceMode(const std::string &source, parameterlist &params);
214
215         /** FTOPIC command */
216         bool ForceTopic(const std::string &source, parameterlist &params);
217
218         /** FJOIN, similar to TS6 SJOIN, but not quite. */
219         bool ForceJoin(const std::string &source, parameterlist &params);
220
221         /* Used on nick collision ... XXX ugly function HACK */
222         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
223
224         /** UID command */
225         bool ParseUID(const std::string &source, parameterlist &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, Channel* 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.
251          */
252         void OnDataReady();
253
254         /** Send one or more complete lines down the socket
255          */
256         void WriteLine(std::string line);
257
258         /** Handle ERROR command */
259         bool Error(parameterlist &params);
260
261         /** remote MOTD. */
262         bool Motd(const std::string &prefix, parameterlist &params);
263
264         /** remote ADMIN. */
265         bool Admin(const std::string &prefix, parameterlist &params);
266
267         bool Stats(const std::string &prefix, parameterlist &params);
268
269         /** Because the core won't let users or even SERVERS set +o,
270          * we use the OPERTYPE command to do this.
271          */
272         bool OperType(const std::string &prefix, parameterlist &params);
273
274         /** Remote AWAY */
275         bool Away(const std::string &prefix, parameterlist &params);
276
277         /** Because Andy insists that services-compatible servers must
278          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
279          */
280         bool SVSNick(const std::string &prefix, parameterlist &params);
281
282         /** SAVE to resolve nick collisions without killing */
283         bool ForceNick(const std::string &prefix, parameterlist &params);
284
285         /** ENCAP command
286          */
287         bool Encap(const std::string &prefix, parameterlist &params);
288
289         /** OPERQUIT command
290          */
291         bool OperQuit(const std::string &prefix, parameterlist &params);
292
293         /** SVSJOIN
294          */
295         bool ServiceJoin(const std::string &prefix, parameterlist &params);
296
297         /** SVSPART
298          */
299         bool ServicePart(const std::string &prefix, parameterlist &params);
300
301         /** KILL
302          */
303         bool RemoteKill(const std::string &prefix, parameterlist &params);
304
305         /** PONG
306          */
307         bool LocalPong(const std::string &prefix, parameterlist &params);
308
309         /** METADATA
310          */
311         bool MetaData(const std::string &prefix, parameterlist &params);
312
313         /** VERSION
314          */
315         bool ServerVersion(const std::string &prefix, parameterlist &params);
316
317         /** CHGHOST
318          */
319         bool ChangeHost(const std::string &prefix, parameterlist &params);
320
321         /** ADDLINE
322          */
323         bool AddLine(const std::string &prefix, parameterlist &params);
324
325         /** DELLINE
326          */
327         bool DelLine(const std::string &prefix, parameterlist &params);
328
329         /** CHGNAME
330          */
331         bool ChangeName(const std::string &prefix, parameterlist &params);
332
333         /** FIDENT */
334         bool ChangeIdent(const std::string &prefix, parameterlist &params);
335
336         /** WHOIS
337          */
338         bool Whois(const std::string &prefix, parameterlist &params);
339
340         /** PUSH
341          */
342         bool Push(const std::string &prefix, parameterlist &params);
343
344         /** TIME
345          */
346         bool Time(const std::string &prefix, parameterlist &params);
347
348         /** PING
349          */
350         bool LocalPing(const std::string &prefix, parameterlist &params);
351
352         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
353          * This does not update the timestamp of the target channel, this must be done seperately.
354          */
355         bool RemoveStatus(const std::string &prefix, parameterlist &params);
356
357         /** <- (remote) <- SERVER
358          */
359         bool RemoteServer(const std::string &prefix, parameterlist &params);
360
361         /** (local) -> SERVER
362          */
363         bool Outbound_Reply_Server(parameterlist &params);
364
365         /** (local) <- SERVER
366          */
367         bool Inbound_Server(parameterlist &params);
368
369         /** Handle IRC line split
370          */
371         void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist &params);
372
373         /** Process complete line from buffer
374          */
375         void ProcessLine(std::string &line);
376
377         void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
378
379         /** Get this server's name
380          */
381         virtual std::string GetName();
382
383         /** Handle socket timeout from connect()
384          */
385         virtual void OnTimeout();
386         /** Handle server quit on close
387          */
388         virtual void Close();
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