]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/inspsocket.h
(1) remove CleanAndResolve.
[user/henk/code/inspircd.git] / include / inspsocket.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __INSP_SOCKET_H__
18 #define __INSP_SOCKET_H__
19
20 #include <sstream>
21 #include <string>
22 #include <deque>
23 #include "dns.h"
24 #include "inspircd_config.h"
25 #include "socket.h"
26
27 /**
28  * States which a socket may be in
29  */
30 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
31
32 /**
33  * Error types which a socket may exhibit
34  */
35 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE };
36
37 class InspSocket;
38
39 /**
40  * InspSocket is an extendable socket class which modules
41  * can use for TCP socket support. It is fully integrated
42  * into InspIRCds socket loop and attaches its sockets to
43  * the core's instance of the SocketEngine class, meaning
44  * that any sockets you create have the same power and
45  * abilities as a socket created by the core itself.
46  * To use InspSocket, you must inherit a class from it,
47  * and use the InspSocket constructors to establish connections
48  * and bindings.
49  */
50 class InspSocket : public Extensible
51 {
52  public:
53
54         std::deque<std::string> outbuffer;
55
56         /**
57          * The file descriptor of this socket
58          */
59         int fd;
60
61         /**
62          * The hostname connected to
63          */
64         char host[MAXBUF];
65
66         /**
67          * The port connected to, or the port
68          * this socket is listening on
69          */
70         int port;
71
72         /**
73          * The state for this socket, either
74          * listening, connecting, connected
75          * or error.
76          */
77         InspSocketState state;
78
79         /**
80          * The host being connected to,
81          * in sockaddr form
82          */
83         insp_sockaddr addr;
84
85         /** 
86          * The host being connected to,
87          * in in_addr form
88          */
89         insp_inaddr addy;
90
91         /**
92          * When this time is reached,
93          * the socket times out if it is
94          * in the CONNECTING state
95          */
96         time_t timeout_end;
97
98         /**
99          * This value is true if the
100          * socket has timed out.
101          */
102         bool timeout;
103         
104         /**
105          * Socket input buffer, used by read(). The class which
106          * extends InspSocket is expected to implement an extendable
107          * buffer which can grow much larger than 64k,
108          * this buffer is just designed to be temporary storage.
109          * space.
110          */
111         char ibuf[65535];
112
113         /**
114          * The IP address being connected
115          * to stored in string form for
116          * easy retrieval by accessors.
117          */
118         char IP[MAXBUF];
119
120         /**
121          * Client sockaddr structure used
122          * by accept()
123          */
124         insp_sockaddr client;
125
126         /**
127          * Server sockaddr structure used
128          * by accept()
129          */
130         insp_sockaddr server;
131
132         /**
133          * Used by accept() to indicate the
134          * sizes of the sockaddr_in structures
135          */
136         socklen_t length;
137
138         /** Flushes the write buffer
139          */
140         bool FlushWriteBuffer();
141
142         /** Set the queue sizes
143          * This private method sets the operating system queue
144          * sizes for this socket to 65535 so that it can queue
145          * more information without application-level queueing
146          * which was required in older software.
147          */
148         void SetQueues(int nfd);
149
150         /** When the socket has been marked as closing, this flag
151          * will be set to true, then the next time the socket is
152          * examined, the socket is deleted and closed.
153          */
154         bool ClosePending;
155
156         /** Set to true when we're waiting for a write event.
157          * If this is true and a write event comes in, we
158          * call the write instead of the read method.
159          */
160         bool WaitingForWriteEvent;
161
162         bool BindAddr();
163
164         /**
165          * The default constructor does nothing
166          * and should not be used.
167          */
168         InspSocket();
169
170         /**
171          * This constructor is used to associate
172          * an existing connecting with an InspSocket
173          * class. The given file descriptor must be
174          * valid, and when initialized, the InspSocket
175          * will be set with the given IP address
176          * and placed in CONNECTED state.
177          */
178         InspSocket(int newfd, const char* ip);
179
180         /**
181          * This constructor is used to create a new
182          * socket, either listening for connections, or an outbound connection to another host.
183          * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
184          * connect. You must resolve your hostnames before passing them to InspSocket. To do so,
185          * you should use the nonblocking class 'Resolver'.
186          * @param ipaddr The IP to connect to, or bind to
187          * @param port The port number to connect to, or bind to
188          * @param listening true to listen on the given host:port pair, or false to connect to them
189          * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
190          */
191         InspSocket(const std::string &ipaddr, int port, bool listening, unsigned long maxtime);
192
193         /**
194          * This method is called when an outbound
195          * connection on your socket is completed.
196          * @return false to abort the connection, true to continue
197          */
198         virtual bool OnConnected();
199
200         /**
201          * This method is called when an error occurs.
202          * A closed socket in itself is not an error,
203          * however errors also generate close events.
204          * @param e The error type which occured
205          */
206         virtual void OnError(InspSocketError e);
207
208         /**
209          * When an established connection is terminated,
210          * the OnDisconnect method is triggered.
211          */
212         virtual int OnDisconnect();
213
214         /**
215          * When there is data waiting to be read on a
216          * socket, the OnDataReady() method is called.
217          * Within this method, you *MUST* call the Read()
218          * method to read any pending data. At its lowest
219          * level, this event is signalled by the core via
220          * the socket engine. If you return false from this
221          * function, the core removes your socket from its
222          * list and erases it from the socket engine, then
223          * calls InspSocket::Close() and deletes it.
224          * @return false to close the socket
225          */
226         virtual bool OnDataReady();
227
228         virtual bool OnWriteReady();
229
230         /**
231          * When an outbound connection fails, and the
232          * attempt times out, you will receive this event.
233          * The method will trigger once maxtime seconds are
234          * reached (as given in the constructor) just
235          * before the socket's descriptor is closed.
236          * A failed DNS lookup may cause this event if
237          * the DNS server is not responding, as well as
238          * a failed connect() call, because DNS lookups are
239          * nonblocking as implemented by this class.
240          */
241         virtual void OnTimeout();
242
243         /**
244          * Whenever close() is called, OnClose() will be
245          * called first. Please note that this means
246          * OnClose will be called alongside OnError(),
247          * OnTimeout(), and Close(), and also when
248          * cancelling a listening socket by calling
249          * the destructor indirectly.
250          */
251         virtual void OnClose();
252
253         /**
254          * Reads all pending bytes from the socket
255          * into a char* array which can be up to
256          * 16 kilobytes in length.
257          */
258         virtual char* Read();
259
260         /**
261          * Returns the IP address associated with
262          * this connection, or an empty string if
263          * no IP address exists.
264          */
265         std::string GetIP();
266
267         /**
268          * This function checks if the socket has
269          * timed out yet, given the current time
270          * in the parameter.
271          * @return true if timed out, false if not timed out
272          */
273         bool Timeout(time_t current);
274
275         /**
276          * Writes a std::string to the socket. No carriage
277          * returns or linefeeds are appended to the string.
278          * @param data The data to send
279          */
280         virtual int Write(const std::string &data);
281
282         /**
283          * If your socket is a listening socket, when a new
284          * connection comes in on the socket this method will
285          * be called. Given the new file descriptor in the
286          * parameters, and the IP, it is recommended you copy
287          * them to a new instance of your socket class,
288          * e.g.:
289          *
290          * MySocket* newsocket = new MySocket(newfd,ip);
291          *
292          * Once you have done this, you can then associate the
293          * new socket with the core using Server::AddSocket().
294          */
295         virtual int OnIncomingConnection(int newfd, char* ip);
296
297         /**
298          * Changes the socket's state. The core uses this
299          * to change socket states, and you should not call
300          * it directly.
301          */
302         void SetState(InspSocketState s);
303
304         /**
305          * Call this to receive the next write event
306          * that comes along for this fd to the OnWriteReady
307          * method.
308          */
309         void WantWrite();
310
311         /**
312          * Returns the current socket state.
313          */
314         InspSocketState GetState();
315
316         /**
317          * Only the core should call this function.
318          * When called, it is assumed the socket is ready
319          * to read data, and the method call routes the
320          * event to the various methods of InspSocket
321          * for you to handle. This can also cause the
322          * socket's state to change.
323          */
324         bool Poll();
325
326         /**
327          * This method returns the socket's file descriptor
328          * as assigned by the operating system, or -1
329          * if no descriptor has been assigned.
330          */
331         int GetFd();
332
333         /**
334          * This method causes the socket to close, and may
335          * also be triggered by other methods such as OnTimeout
336          * and OnError.
337          */
338         virtual void Close();
339
340         /**
341          * The destructor may implicitly call OnClose(), and
342          * will close() and shutdown() the file descriptor
343          * used for this socket.
344          */
345         virtual ~InspSocket();
346
347         /**
348          * This method attempts to connect to a hostname.
349          * This only occurs on a non-listening socket. This
350          * method is asyncronous.
351          */
352         virtual bool DoConnect();
353
354         /**
355          * This method marks the socket closed.
356          * The next time the core examines a socket marked
357          * as closed, the socket will be closed and the 
358          * memory reclaimed.
359          */
360         void MarkAsClosed();
361 };
362
363 #endif