]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treesocket.h
Fix some of the include guard names (requested by SaberUK)
[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 M_SPANNINGTREE_TREESOCKET_H
15 #define M_SPANNINGTREE_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  * DYING:       represents a server that has had an error.
55  */
56 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED, DYING };
57
58 struct CapabData
59 {
60         reference<Link> link;                   /* Link block used for this connection */
61         reference<Autoconnect> ac;              /* Autoconnect used to cause this connection, if any */
62         std::string ModuleList;                 /* Required module list of other server from CAPAB */
63         std::string OptModuleList;              /* Optional module list of other server from CAPAB */
64         std::string ChanModes;
65         std::string UserModes;
66         std::map<std::string,std::string> CapKeys;      /* CAPAB keys from other server */
67         std::string ourchallenge;               /* Challenge sent for challenge/response */
68         std::string theirchallenge;             /* Challenge recv for challenge/response */
69         int capab_phase;                        /* Have sent CAPAB already */
70         bool auth_fingerprint;                  /* Did we auth using SSL fingerprint */
71         bool auth_challenge;                    /* Did we auth using challenge/response */
72 };
73
74 /** Every SERVER connection inbound or outbound is represented by an object of
75  * type TreeSocket. During setup, the object can be found in Utils->timeoutlist;
76  * after setup, MyRoot will have been created as a child of Utils->TreeRoot
77  */
78 class TreeSocket : public BufferedSocket
79 {
80         SpanningTreeUtilities* Utils;           /* Utility class */
81         std::string linkID;                     /* Description for this link */
82         ServerState LinkState;                  /* Link state */
83         CapabData* capab;                       /* Link setup data (held until burst is sent) */
84         TreeServer* MyRoot;                     /* The server we are talking to */
85         time_t NextPing;                        /* Time when we are due to ping this server */
86         bool LastPingWasGood;                   /* Responded to last ping we sent? */
87         int proto_version;                      /* Remote protocol version */
88  public:
89         time_t age;
90
91         /** Because most of the I/O gubbins are encapsulated within
92          * BufferedSocket, we just call the superclass constructor for
93          * most of the action, and append a few of our own values
94          * to it.
95          */
96         TreeSocket(SpanningTreeUtilities* Util, Link* link, Autoconnect* myac, const std::string& ipaddr);
97
98         /** When a listening socket gives us a new file descriptor,
99          * we must associate it with a socket without creating a new
100          * connection. This constructor is used for this purpose.
101          */
102         TreeSocket(SpanningTreeUtilities* Util, int newfd, ListenSocket* via, irc::sockets::sockaddrs* client, irc::sockets::sockaddrs* server);
103
104         /** Get link state
105          */
106         ServerState GetLinkState();
107
108         /** Get challenge set in our CAPAB for challenge/response
109          */
110         const std::string& GetOurChallenge();
111
112         /** Get challenge set in our CAPAB for challenge/response
113          */
114         void SetOurChallenge(const std::string &c);
115
116         /** Get challenge set in their CAPAB for challenge/response
117          */
118         const std::string& GetTheirChallenge();
119
120         /** Get challenge set in their CAPAB for challenge/response
121          */
122         void SetTheirChallenge(const std::string &c);
123
124         /** Compare two passwords based on authentication scheme
125          */
126         bool ComparePass(const Link& link, const std::string &theirs);
127
128         /** Clean up information used only during server negotiation
129          */
130         void CleanNegotiationInfo();
131
132         CullResult cull();
133         /** Destructor
134          */
135         ~TreeSocket();
136
137         /** Construct a password, optionally hashed with the other side's
138          * challenge string
139          */
140         std::string MakePass(const std::string &password, const std::string &challenge);
141
142         /** When an outbound connection finishes connecting, we receive
143          * this event, and must send our SERVER string to the other
144          * side. If the other side is happy, as outlined in the server
145          * to server docs on the inspircd.org site, the other side
146          * will then send back its own server string.
147          */
148         virtual void OnConnected();
149
150         /** Handle socket error event
151          */
152         virtual void OnError(BufferedSocketError e);
153
154         /** Sends an error to the remote server, and displays it locally to show
155          * that it was sent.
156          */
157         void SendError(const std::string &errormessage);
158
159         /** Recursively send the server tree with distances as hops.
160          * This is used during network burst to inform the other server
161          * (and any of ITS servers too) of what servers we know about.
162          * If at any point any of these servers already exist on the other
163          * end, our connection may be terminated. The hopcounts given
164          * by this function are relative, this doesn't matter so long as
165          * they are all >1, as all the remote servers re-calculate them
166          * to be relative too, with themselves as hop 0.
167          */
168         void SendServers(TreeServer* Current, TreeServer* s, int hops);
169
170         /** Returns module list as a string, filtered by filter
171          * @param filter a module version bitmask, such as VF_COMMON or VF_OPTCOMMON
172          */
173         std::string MyModules(int filter);
174
175         /** Send my capabilities to the remote side
176          */
177         void SendCapabilities(int phase);
178
179         /** Add modules to VF_COMMON list for backwards compatability */
180         void CompatAddModules(std::vector<std::string>& modlist);
181
182         /* Isolate and return the elements that are different between two lists */
183         void ListDifference(const std::string &one, const std::string &two, char sep,
184                 std::string& mleft, std::string& mright);
185
186         bool Capab(const parameterlist &params);
187
188         /** This function forces this server to quit, removing this server
189          * and any users on it (and servers and users below that, etc etc).
190          * It's very slow and pretty clunky, but luckily unless your network
191          * is having a REAL bad hair day, this function shouldnt be called
192          * too many times a month ;-)
193          */
194         void SquitServer(std::string &from, TreeServer* Current, int& num_lost_servers, int& num_lost_users);
195
196         /** This is a wrapper function for SquitServer above, which
197          * does some validation first and passes on the SQUIT to all
198          * other remaining servers.
199          */
200         void Squit(TreeServer* Current, const std::string &reason);
201
202         /* Used on nick collision ... XXX ugly function HACK */
203         int DoCollision(User *u, time_t remotets, const std::string &remoteident, const std::string &remoteip, const std::string &remoteuid);
204
205         /** Send one or more FJOINs for a channel of users.
206          * If the length of a single line is more than 480-NICKMAX
207          * in length, it is split over multiple lines.
208          */
209         void SendFJoins(TreeServer* Current, Channel* c);
210
211         /** Send G, Q, Z and E lines */
212         void SendXLines(TreeServer* Current);
213
214         /** Send channel modes and topics */
215         void SendChannelModes(TreeServer* Current);
216
217         /** send all users and their oper state/modes */
218         void SendUsers(TreeServer* Current);
219
220         /** This function is called when we want to send a netburst to a local
221          * server. There is a set order we must do this, because for example
222          * users require their servers to exist, and channels require their
223          * users to exist. You get the idea.
224          */
225         void DoBurst(TreeServer* s);
226
227         /** This function is called when we receive data from a remote
228          * server.
229          */
230         void OnDataReady();
231
232         /** Send one or more complete lines down the socket
233          */
234         void WriteLine(std::string line);
235
236         /** Handle ERROR command */
237         void Error(parameterlist &params);
238
239         /** Remote AWAY */
240         bool Away(const std::string &prefix, parameterlist &params);
241
242         /** SAVE to resolve nick collisions without killing */
243         bool ForceNick(const std::string &prefix, parameterlist &params);
244
245         /** ENCAP command
246          */
247         void Encap(User* who, parameterlist &params);
248
249         /** OPERQUIT command
250          */
251         bool OperQuit(const std::string &prefix, parameterlist &params);
252
253         /** PONG
254          */
255         bool LocalPong(const std::string &prefix, parameterlist &params);
256
257         /** VERSION
258          */
259         bool ServerVersion(const std::string &prefix, parameterlist &params);
260
261         /** ADDLINE
262          */
263         bool AddLine(const std::string &prefix, parameterlist &params);
264
265         /** DELLINE
266          */
267         bool DelLine(const std::string &prefix, parameterlist &params);
268
269         /** WHOIS
270          */
271         bool Whois(const std::string &prefix, parameterlist &params);
272
273         /** PUSH
274          */
275         bool Push(const std::string &prefix, parameterlist &params);
276
277         /** PING
278          */
279         bool LocalPing(const std::string &prefix, parameterlist &params);
280
281         /** <- (remote) <- SERVER
282          */
283         bool RemoteServer(const std::string &prefix, parameterlist &params);
284
285         /** (local) -> SERVER
286          */
287         bool Outbound_Reply_Server(parameterlist &params);
288
289         /** (local) <- SERVER
290          */
291         bool Inbound_Server(parameterlist &params);
292
293         /** Handle IRC line split
294          */
295         void Split(const std::string &line, std::string& prefix, std::string& command, parameterlist &params);
296
297         /** Process complete line from buffer
298          */
299         void ProcessLine(std::string &line);
300
301         void ProcessConnectedLine(std::string& prefix, std::string& command, parameterlist& params);
302
303         /** Handle socket timeout from connect()
304          */
305         virtual void OnTimeout();
306         /** Handle server quit on close
307          */
308         virtual void Close();
309 };
310
311 #endif
312