]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
52496a4ef27781d8de89166a44c720cf3f2ce3e8
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treesocket.h
1 #ifndef __TREESOCKET_H__
2 #define __TREESOCKET_H__
3
4 #include "configreader.h"
5 #include "users.h"
6 #include "channels.h"
7 #include "modules.h"
8 #include "commands/cmd_whois.h"
9 #include "commands/cmd_stats.h"
10 #include "socket.h"
11 #include "inspircd.h"
12 #include "wildcard.h"
13 #include "xline.h"
14 #include "transport.h"
15
16 #include "m_spanningtree/utils.h"
17
18 /*
19  * The server list in InspIRCd is maintained as two structures
20  * which hold the data in different ways. Most of the time, we
21  * want to very quicky obtain three pieces of information:
22  *
23  * (1) The information on a server
24  * (2) The information on the server we must send data through
25  *     to actually REACH the server we're after
26  * (3) Potentially, the child/parent objects of this server
27  *
28  * The InspIRCd spanning protocol provides easy access to these
29  * by storing the data firstly in a recursive structure, where
30  * each item references its parent item, and a dynamic list
31  * of child items, and another structure which stores the items
32  * hashed, linearly. This means that if we want to find a server
33  * by name quickly, we can look it up in the hash, avoiding
34  * any O(n) lookups. If however, during a split or sync, we want
35  * to apply an operation to a server, and any of its child objects
36  * we can resort to recursion to walk the tree structure.
37  * Any socket can have one of five states at any one time.
38  * The LISTENER state indicates a socket which is listening
39  * for connections. It cannot receive data itself, only incoming
40  * sockets.
41  * The CONNECTING state indicates an outbound socket which is
42  * waiting to be writeable.
43  * The WAIT_AUTH_1 state indicates the socket is outbound and
44  * has successfully connected, but has not yet sent and received
45  * SERVER strings.
46  * The WAIT_AUTH_2 state indicates that the socket is inbound
47  * (allocated by a LISTENER) but has not yet sent and received
48  * SERVER strings.
49  * The CONNECTED state represents a fully authorized, fully
50  * connected server.
51  */
52 enum ServerState { LISTENER, CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
53
54 /** Every SERVER connection inbound or outbound is represented by
55  * an object of type TreeSocket.
56  * TreeSockets, being inherited from InspSocket, can be tied into
57  * the core socket engine, and we cn therefore receive activity events
58  * for them, just like activex objects on speed. (yes really, that
59  * is a technical term!) Each of these which relates to a locally
60  * connected server is assocated with it, by hooking it onto a
61  * TreeSocket class using its constructor. In this way, we can
62  * maintain a list of servers, some of which are directly connected,
63  * some of which are not.
64  */
65 class TreeSocket : public InspSocket
66 {
67         SpanningTreeUtilities* Utils;           /* Utility class */
68         std::string myhost;                     /* Canonical hostname */
69         std::string in_buffer;                  /* Input buffer */
70         ServerState LinkState;                  /* Link state */
71         std::string InboundServerName;          /* Server name sent to us by other side */
72         std::string InboundDescription;         /* Server description (GECOS) sent to us by the other side */
73         int num_lost_users;                     /* Users lost in split */
74         int num_lost_servers;                   /* Servers lost in split */
75         time_t NextPing;                        /* Time when we are due to ping this server */
76         bool LastPingWasGood;                   /* Responded to last ping we sent? */
77         bool bursting;                          /* True if not finished bursting yet */
78         unsigned int keylength;                 /* Is this still used? */
79         std::string ModuleList;                 /* Module list of other server from CAPAB */
80         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
81         Module* Hook;                           /* I/O hooking module that we're attached to for this socket */
82
83  public:
84
85         /** Because most of the I/O gubbins are encapsulated within
86          * InspSocket, we just call the superclass constructor for
87          * most of the action, and append a few of our own values
88          * to it.
89          */
90         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, std::string host, int port, bool listening, unsigned long maxtime, Module* HookMod = NULL);
91
92         /** Because most of the I/O gubbins are encapsulated within
93          * InspSocket, we just call the superclass constructor for
94          * most of the action, and append a few of our own values
95          * to it.
96          */
97         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);
98
99         /** When a listening socket gives us a new file descriptor,
100          * we must associate it with a socket without creating a new
101          * connection. This constructor is used for this purpose.
102          */
103         TreeSocket(SpanningTreeUtilities* Util, InspIRCd* SI, int newfd, char* ip, Module* HookMod = NULL);
104
105         /** Get link state
106          */
107         ServerState GetLinkState();
108
109         /** Return the module which we are hooking to for I/O encapsulation
110          */
111         Module* GetHook();
112
113         /** Destructor
114          */
115         ~TreeSocket();
116
117         /** When an outbound connection finishes connecting, we receive
118          * this event, and must send our SERVER string to the other
119          * side. If the other side is happy, as outlined in the server
120          * to server docs on the inspircd.org site, the other side
121          * will then send back its own server string.
122          */
123         virtual bool OnConnected();
124
125         /** Handle socket error event
126          */
127         virtual void OnError(InspSocketError e);
128
129         /** Handle socket disconnect event
130          */
131         virtual int OnDisconnect();
132
133         /** Recursively send the server tree with distances as hops.
134          * This is used during network burst to inform the other server
135          * (and any of ITS servers too) of what servers we know about.
136          * If at any point any of these servers already exist on the other
137          * end, our connection may be terminated. The hopcounts given
138          * by this function are relative, this doesn't matter so long as
139          * they are all >1, as all the remote servers re-calculate them
140          * to be relative too, with themselves as hop 0.
141          */
142         void SendServers(TreeServer* Current, TreeServer* s, int hops);
143
144         /** Returns my capabilities as a string
145          */
146         std::string MyCapabilities();
147
148         /** Send my capabilities to the remote side
149          */
150         void SendCapabilities();
151
152         /* Check a comma seperated list for an item */
153         bool HasItem(const std::string &list, const std::string &item);
154
155         /* Isolate and return the elements that are different between two comma seperated lists */
156         std::string ListDifference(const std::string &one, const std::string &two);
157
158         bool Capab(const std::deque<std::string> &params);
159
160         /** This function forces this server to quit, removing this server
161          * and any users on it (and servers and users below that, etc etc).
162          * It's very slow and pretty clunky, but luckily unless your network
163          * is having a REAL bad hair day, this function shouldnt be called
164          * too many times a month ;-)
165          */
166         void SquitServer(std::string &from, TreeServer* Current);
167
168         /** This is a wrapper function for SquitServer above, which
169          * does some validation first and passes on the SQUIT to all
170          * other remaining servers.
171          */
172         void Squit(TreeServer* Current, const std::string &reason);
173
174         /** FMODE command - server mode with timestamp checks */
175         bool ForceMode(const std::string &source, std::deque<std::string> &params);
176
177         /** FTOPIC command */
178         bool ForceTopic(const std::string &source, std::deque<std::string> &params);
179
180         /** FJOIN, similar to TS6 SJOIN, but not quite. */
181         bool ForceJoin(const std::string &source, std::deque<std::string> &params);
182
183         /** NICK command */
184         bool IntroduceClient(const std::string &source, std::deque<std::string> &params);
185
186         /** Send one or more FJOINs for a channel of users.
187          * If the length of a single line is more than 480-NICKMAX
188          * in length, it is split over multiple lines.
189          */
190         void SendFJoins(TreeServer* Current, chanrec* c);
191
192         /** Send G, Q, Z and E lines */
193         void SendXLines(TreeServer* Current);
194
195         /** Send channel modes and topics */
196         void SendChannelModes(TreeServer* Current);
197
198         /** send all users and their oper state/modes */
199         void SendUsers(TreeServer* Current);
200
201         /** This function is called when we want to send a netburst to a local
202          * server. There is a set order we must do this, because for example
203          * users require their servers to exist, and channels require their
204          * users to exist. You get the idea.
205          */
206         void DoBurst(TreeServer* s);
207
208         /** This function is called when we receive data from a remote
209          * server. We buffer the data in a std::string (it doesnt stay
210          * there for long), reading using InspSocket::Read() which can
211          * read up to 16 kilobytes in one operation.
212          *
213          * IF THIS FUNCTION RETURNS FALSE, THE CORE CLOSES AND DELETES
214          * THE SOCKET OBJECT FOR US.
215          */
216         virtual bool OnDataReady();
217
218         /** Send one or more complete lines down the socket
219          */
220         int WriteLine(std::string line);
221
222         /** Handle ERROR command */
223         bool Error(std::deque<std::string> &params);
224
225         /** remote MOTD. leet, huh? */
226         bool Motd(const std::string &prefix, std::deque<std::string> &params);
227
228         /** remote ADMIN. leet, huh? */
229         bool Admin(const std::string &prefix, std::deque<std::string> &params);
230
231         bool Stats(const std::string &prefix, std::deque<std::string> &params);
232
233         /** Because the core won't let users or even SERVERS set +o,
234          * we use the OPERTYPE command to do this.
235          */
236         bool OperType(const std::string &prefix, std::deque<std::string> &params);
237
238         /** Because Andy insists that services-compatible servers must
239          * implement SVSNICK and SVSJOIN, that's exactly what we do :p
240          */
241         bool ForceNick(const std::string &prefix, std::deque<std::string> &params);
242
243         /** Remote SQUIT (RSQUIT). Routing works similar to SVSNICK: Route it to the server that the target is connected to locally,
244          * then let that server do the dirty work (squit it!). Example:
245          * 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
246          */
247         bool RemoteSquit(const std::string &prefix, std::deque<std::string> &params);
248
249         /** SVSJOIN
250          */
251         bool ServiceJoin(const std::string &prefix, std::deque<std::string> &params);
252
253         /** REHASH
254          */
255         bool RemoteRehash(const std::string &prefix, std::deque<std::string> &params);
256
257         /** KILL
258          */
259         bool RemoteKill(const std::string &prefix, std::deque<std::string> &params);
260
261         /** PONG
262          */
263         bool LocalPong(const std::string &prefix, std::deque<std::string> &params);
264
265         /** METADATA
266          */
267         bool MetaData(const std::string &prefix, std::deque<std::string> &params);
268
269         /** VERSION
270          */
271         bool ServerVersion(const std::string &prefix, std::deque<std::string> &params);
272
273         /** CHGHOST
274          */
275         bool ChangeHost(const std::string &prefix, std::deque<std::string> &params);
276
277         /** ADDLINE
278          */
279         bool AddLine(const std::string &prefix, std::deque<std::string> &params);
280
281         /** CHGNAME
282          */
283         bool ChangeName(const std::string &prefix, std::deque<std::string> &params);
284
285         /** WHOIS
286          */
287         bool Whois(const std::string &prefix, std::deque<std::string> &params);
288
289         /** PUSH
290          */
291         bool Push(const std::string &prefix, std::deque<std::string> &params);
292
293         /** SETTIME
294          */
295         bool HandleSetTime(const std::string &prefix, std::deque<std::string> &params);
296
297         /** TIME
298          */
299         bool Time(const std::string &prefix, std::deque<std::string> &params);
300
301         /** PING
302          */
303         bool LocalPing(const std::string &prefix, std::deque<std::string> &params);
304
305         /** Remove all modes from a channel, including statusmodes (+qaovh etc), simplemodes, parameter modes.
306          * This does not update the timestamp of the target channel, this must be done seperately.
307          */
308         bool RemoveStatus(const std::string &prefix, std::deque<std::string> &params);
309
310         /** <- (remote) <- SERVER
311          */
312         bool RemoteServer(const std::string &prefix, std::deque<std::string> &params);
313
314         /** (local) -> SERVER
315          */
316         bool Outbound_Reply_Server(std::deque<std::string> &params);
317
318         /** (local) <- SERVER
319          */
320         bool Inbound_Server(std::deque<std::string> &params);
321
322         /** Handle netsplit
323          */
324         void Split(const std::string &line, std::deque<std::string> &n);
325
326         /** Process complete line from buffer
327          */
328         bool ProcessLine(std::string &line);
329
330         /** Get this server's name
331          */
332         virtual std::string GetName();
333
334         /** Handle socket timeout from connect()
335          */
336         virtual void OnTimeout();
337
338         /** Handle socket close event
339          */
340         virtual void OnClose();
341
342         /** Handle incoming connection event
343          */
344         virtual int OnIncomingConnection(int newsock, char* ip);
345 };
346
347 #endif
348