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