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