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