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