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