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