1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #ifndef __INSP_SOCKET_H__
18 #define __INSP_SOCKET_H__
24 #include "inspircd_config.h"
28 * States which a socket may be in
30 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
33 * Error types which a socket may exhibit
35 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE, I_ERR_NOMOREFDS };
40 using irc::sockets::insp_sockaddr;
41 using irc::sockets::insp_inaddr;
42 using irc::sockets::insp_ntoa;
43 using irc::sockets::insp_aton;
46 * InspSocket is an extendable socket class which modules
47 * can use for TCP socket support. It is fully integrated
48 * into InspIRCds socket loop and attaches its sockets to
49 * the core's instance of the SocketEngine class, meaning
50 * that any sockets you create have the same power and
51 * abilities as a socket created by the core itself.
52 * To use InspSocket, you must inherit a class from it,
53 * and use the InspSocket constructors to establish connections
56 class InspSocket : public Extensible
61 std::deque<std::string> outbuffer;
64 * The file descriptor of this socket
69 * The hostname connected to
74 * The port connected to, or the port
75 * this socket is listening on
80 * The state for this socket, either
81 * listening, connecting, connected
84 InspSocketState state;
87 * The host being connected to,
93 * The host being connected to,
99 * When this time is reached,
100 * the socket times out if it is
101 * in the CONNECTING state
106 * This value is true if the
107 * socket has timed out.
112 * Socket input buffer, used by read(). The class which
113 * extends InspSocket is expected to implement an extendable
114 * buffer which can grow much larger than 64k,
115 * this buffer is just designed to be temporary storage.
121 * The IP address being connected
122 * to stored in string form for
123 * easy retrieval by accessors.
128 * Client sockaddr structure used
131 insp_sockaddr client;
134 * Server sockaddr structure used
137 insp_sockaddr server;
140 * Used by accept() to indicate the
141 * sizes of the sockaddr_in structures
145 /** Flushes the write buffer
147 bool FlushWriteBuffer();
149 /** Set the queue sizes
150 * This private method sets the operating system queue
151 * sizes for this socket to 65535 so that it can queue
152 * more information without application-level queueing
153 * which was required in older software.
155 void SetQueues(int nfd);
157 /** When the socket has been marked as closing, this flag
158 * will be set to true, then the next time the socket is
159 * examined, the socket is deleted and closed.
163 /** Set to true when we're waiting for a write event.
164 * If this is true and a write event comes in, we
165 * call the write instead of the read method.
167 bool WaitingForWriteEvent;
172 * The default constructor does nothing
173 * and should not be used.
175 InspSocket(InspIRCd* SI);
178 * This constructor is used to associate
179 * an existing connecting with an InspSocket
180 * class. The given file descriptor must be
181 * valid, and when initialized, the InspSocket
182 * will be set with the given IP address
183 * and placed in CONNECTED state.
185 InspSocket(InspIRCd* SI, int newfd, const char* ip);
188 * This constructor is used to create a new
189 * socket, either listening for connections, or an outbound connection to another host.
190 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
191 * connect. You must resolve your hostnames before passing them to InspSocket. To do so,
192 * you should use the nonblocking class 'Resolver'.
193 * @param ipaddr The IP to connect to, or bind to
194 * @param port The port number to connect to, or bind to
195 * @param listening true to listen on the given host:port pair, or false to connect to them
196 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
198 InspSocket(InspIRCd* SI, const std::string &ipaddr, int port, bool listening, unsigned long maxtime);
201 * This method is called when an outbound
202 * connection on your socket is completed.
203 * @return false to abort the connection, true to continue
205 virtual bool OnConnected();
208 * This method is called when an error occurs.
209 * A closed socket in itself is not an error,
210 * however errors also generate close events.
211 * @param e The error type which occured
213 virtual void OnError(InspSocketError e);
216 * When an established connection is terminated,
217 * the OnDisconnect method is triggered.
219 virtual int OnDisconnect();
222 * When there is data waiting to be read on a
223 * socket, the OnDataReady() method is called.
224 * Within this method, you *MUST* call the Read()
225 * method to read any pending data. At its lowest
226 * level, this event is signalled by the core via
227 * the socket engine. If you return false from this
228 * function, the core removes your socket from its
229 * list and erases it from the socket engine, then
230 * calls InspSocket::Close() and deletes it.
231 * @return false to close the socket
233 virtual bool OnDataReady();
235 virtual bool OnWriteReady();
238 * When an outbound connection fails, and the
239 * attempt times out, you will receive this event.
240 * The method will trigger once maxtime seconds are
241 * reached (as given in the constructor) just
242 * before the socket's descriptor is closed.
243 * A failed DNS lookup may cause this event if
244 * the DNS server is not responding, as well as
245 * a failed connect() call, because DNS lookups are
246 * nonblocking as implemented by this class.
248 virtual void OnTimeout();
251 * Whenever close() is called, OnClose() will be
252 * called first. Please note that this means
253 * OnClose will be called alongside OnError(),
254 * OnTimeout(), and Close(), and also when
255 * cancelling a listening socket by calling
256 * the destructor indirectly.
258 virtual void OnClose();
261 * Reads all pending bytes from the socket
262 * into a char* array which can be up to
263 * 16 kilobytes in length.
265 virtual char* Read();
268 * Returns the IP address associated with
269 * this connection, or an empty string if
270 * no IP address exists.
275 * This function checks if the socket has
276 * timed out yet, given the current time
278 * @return true if timed out, false if not timed out
280 bool Timeout(time_t current);
283 * Writes a std::string to the socket. No carriage
284 * returns or linefeeds are appended to the string.
285 * @param data The data to send
287 virtual int Write(const std::string &data);
290 * If your socket is a listening socket, when a new
291 * connection comes in on the socket this method will
292 * be called. Given the new file descriptor in the
293 * parameters, and the IP, it is recommended you copy
294 * them to a new instance of your socket class,
297 * MySocket* newsocket = new MySocket(newfd,ip);
299 * Once you have done this, you can then associate the
300 * new socket with the core using Server::AddSocket().
302 virtual int OnIncomingConnection(int newfd, char* ip);
305 * Changes the socket's state. The core uses this
306 * to change socket states, and you should not call
309 void SetState(InspSocketState s);
312 * Call this to receive the next write event
313 * that comes along for this fd to the OnWriteReady
319 * Returns the current socket state.
321 InspSocketState GetState();
324 * Only the core should call this function.
325 * When called, it is assumed the socket is ready
326 * to read data, and the method call routes the
327 * event to the various methods of InspSocket
328 * for you to handle. This can also cause the
329 * socket's state to change.
334 * This method returns the socket's file descriptor
335 * as assigned by the operating system, or -1
336 * if no descriptor has been assigned.
341 * This method causes the socket to close, and may
342 * also be triggered by other methods such as OnTimeout
345 virtual void Close();
348 * The destructor may implicitly call OnClose(), and
349 * will close() and shutdown() the file descriptor
350 * used for this socket.
352 virtual ~InspSocket();
355 * This method attempts to connect to a hostname.
356 * This only occurs on a non-listening socket. This
357 * method is asyncronous.
359 virtual bool DoConnect();
362 * This method marks the socket closed.
363 * The next time the core examines a socket marked
364 * as closed, the socket will be closed and the