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