]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Fix use of commasepstream on now space-separated items
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 struct CapabData
58 {
59         std::string ModuleList;                 /* Required module list of other server from CAPAB */
60         std::string OptModuleList;              /* Optional module list of other server from CAPAB */
61         std::string ChanModes;
62         std::string UserModes;
63         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
64         std::string ourchallenge;               /* Challenge sent for challenge/response */
65         std::string theirchallenge;             /* Challenge recv for challenge/response */
66         std::string OutboundPass;               /* Outbound password */
67         int capab_phase;                        /* Have sent CAPAB already */
68         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
69         bool auth_challenge;                    /* Did we auth using challenge/response */
70 };
71
72 /** Every SERVER connection inbound or outbound is represented by
73  * an object of type TreeSocket.
74  * TreeSockets, being inherited from BufferedSocket, can be tied into
75  * the core socket engine, and we cn therefore receive activity events
76  * for them, just like activex objects on speed. (yes really, that
77  * is a technical term!) Each of these which relates to a locally
78  * connected server is assocated with it, by hooking it onto a
79  * TreeSocket class using its constructor. In this way, we can
80  * maintain a list of servers, some of which are directly connected,
81  * some of which are not.
82  */
83 class TreeSocket : public BufferedSocket
84 {
85         SpanningTreeUtilities* Utils;           /* Utility class */
86         std::string myhost;                     /* Canonical hostname */
87         ServerState LinkState;                  /* Link state */
88         std::string InboundServerName;          /* Server name sent to us by other side */
89         std::string InboundDescription;         /* Server description (GECOS) sent to us by the other side */
90         std::string InboundSID;                 /* Server ID sent to us by the other side */
91         std::string IP;
92         CapabData* capab;
93         int num_lost_users;                     /* Users lost in split */
94         int num_lost_servers;                   /* Servers lost in split */
95         time_t NextPing;                        /* Time when we are due to ping this server */
96         bool LastPingWasGood;                   /* Responded to last ping we sent? */
97         int proto_version;                      /* Remote protocol version */
98  public:
99         reference<Autoconnect> myautoconnect;           /* Autoconnect used to cause this connection, if any */
100         time_t age;
101
102         /** Because most of the I/O gubbins are encapsulated within
103          * BufferedSocket, 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, const std::string& host, int port, unsigned long maxtime, const std::string &ServerName, const std::string &bindto, Autoconnect* myac, const std::string& Hook);
108
109         /** When a listening socket gives us a new file descriptor,
110          * we must associate it with a socket without creating a new
111          * connection. This constructor is used for this purpose.
112          */
113         TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
114
115         /** Get link state
116          */
117         ServerState GetLinkState();
118
119         /** Get challenge set in our CAPAB for challenge/response
120          */
121         const std::string& GetOurChallenge();
122
123         /** Get challenge set in our CAPAB for challenge/response
124          */
125         void SetOurChallenge(const std::string &c);
126
127         /** Get challenge set in their CAPAB for challenge/response
128          */
129         const std::string& GetTheirChallenge();
130
131         /** Get challenge set in their CAPAB for challenge/response
132          */
133         void SetTheirChallenge(const std::string &c);
134
135         /** Compare two passwords based on authentication scheme
136          */
137         bool ComparePass(const Link& link, const std::string &theirs);
138
139         /** Clean up information used only during server negotiation
140          */
141         void CleanNegotiationInfo();
142
143         CullResult cull();
144         /** Destructor
145          */
146         ~TreeSocket();
147
148         /** Generate random string used for challenge-response auth
149          */
150         std::string RandString(unsigned int length);
151
152         /** Construct a password, optionally hashed with the other side's
153          * challenge string
154          */
155         std::string MakePass(const std::string &password, const std::string &challenge);
156
157         /** When an outbound connection finishes connecting, we receive
158          * this event, and must send our SERVER string to the other
159          * side. If the other side is happy, as outlined in the server
160          * to server docs on the inspircd.org site, the other side
161          * will then send back its own server string.
162          */
163         virtual void OnConnected();
164
165         /** Handle socket error event
166          */
167         virtual void OnError(BufferedSocketError e);
168
169         /** Sends an error to the remote server, and displays it locally to show
170          * that it was sent.
171          */
172         void SendError(const std::string &errormessage);
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 module list as a string, filtered by filter
186          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
187          */
188         std::string MyModules(int filter);
189
190         /** Send my capabilities to the remote side
191          */
192         void SendCapabilities(int phase);
193
194         /** Add modules to VF_COMMON list for backwards compatability */
195         void CompatAddModules(std::vector<std::string>& modlist);
196
197         /* Isolate and return the elements that are different between two lists */
198         void ListDifference(const std::string &one, const std::string &two, char sep,
199                 std::string& mleft, std::string& mright);
200
201         bool Capab(const parameterlist &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         void ForceMode(User* who, parameterlist &params);
219
220         /** FTOPIC command */
221         bool ForceTopic(const std::string &source, parameterlist &params);
222
223         /** FJOIN, similar to TS6 SJOIN, but not quite. */
224         void ForceJoin(User* who, parameterlist &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, parameterlist &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.
256          */
257         void OnDataReady();
258
259         /** Send one or more complete lines down the socket
260          */
261         void WriteLine(std::string line);
262
263         /** Handle ERROR command */
264         void Error(parameterlist &params);
265
266         /** remote MOTD. */
267         bool Motd(const std::string &prefix, parameterlist &params);
268
269         /** remote ADMIN. */
270         bool Admin(const std::string &prefix, parameterlist &params);
271
272         bool Stats(const std::string &prefix, parameterlist &params);
273
274         /** Because the core won't let users or even SERVERS set +o,
275          * we use the OPERTYPE command to do this.
276          */
277         bool OperType(const std::string &prefix, parameterlist &params);
278
279         /** Remote AWAY */
280         bool Away(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         void Encap(User* who, parameterlist &params);
288
289         /** OPERQUIT command
290          */
291         bool OperQuit(const std::string &prefix, parameterlist &params);
292
293         /** KILL
294          */
295         bool RemoteKill(const std::string &prefix, parameterlist &params);
296
297         /** PONG
298          */
299         bool LocalPong(const std::string &prefix, parameterlist &params);
300
301         /** METADATA
302          */
303         bool MetaData(const std::string &prefix, parameterlist &params);
304
305         /** VERSION
306          */
307         bool ServerVersion(const std::string &prefix, parameterlist &params);
308
309         /** CHGHOST
310          */
311         bool ChangeHost(const std::string &prefix, parameterlist &params);
312
313         /** ADDLINE
314          */
315         bool AddLine(const std::string &prefix, parameterlist &params);
316
317         /** DELLINE
318          */
319         bool DelLine(const std::string &prefix, parameterlist &params);
320
321         /** CHGNAME
322          */
323         bool ChangeName(const std::string &prefix, parameterlist &params);
324
325         /** FIDENT */
326         bool ChangeIdent(const std::string &prefix, parameterlist &params);
327
328         /** WHOIS
329          */
330         bool Whois(const std::string &prefix, parameterlist &params);
331
332         /** PUSH
333          */
334         bool Push(const std::string &prefix, parameterlist &params);
335
336         /** TIME
337          */
338         bool Time(const std::string &prefix, parameterlist &params);
339
340         /** PING
341          */
342         bool LocalPing(const std::string &prefix, parameterlist &params);
343
344         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
345          * This does not update the timestamp of the target channel, this must be done seperately.
346          */
347         void RemoveStatus(User* source, parameterlist &params);
348
349         /** <- (remote) <- SERVER
350          */
351         bool RemoteServer(const std::string &prefix, parameterlist &params);
352
353         /** (local) -> SERVER
354          */
355         bool Outbound_Reply_Server(parameterlist &params);
356
357         /** (local) <- SERVER
358          */
359         bool Inbound_Server(parameterlist &params);
360
361         /** Handle IRC line split
362          */
363         void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist &params);
364
365         /** Process complete line from buffer
366          */
367         void ProcessLine(std::string &line);
368
369         void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
370
371         /** Get this server's name
372          */
373         virtual std::string GetName();
374
375         /** Handle socket timeout from connect()
376          */
377         virtual void OnTimeout();
378         /** Handle server quit on close
379          */
380         virtual void Close();
381 };
382
383 /* Used to validate the value lengths of multiple parameters for a command */
384 struct cmd_validation
385 {
386         const char* item;
387         size_t param;
388         size_t length;
389 };
390
391 /* Used to validate the length values in CAPAB CAPABILITIES */
392 struct cap_validation
393 {
394         const char* reason;
395         const char* key;
396         size_t size;
397 };
398
399 #endif
400