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 };
40 * InspSocket is an extendable socket class which modules
41 * can use for TCP socket support. It is fully integrated
42 * into InspIRCds socket loop and attaches its sockets to
43 * the core's instance of the SocketEngine class, meaning
44 * that any sockets you create have the same power and
45 * abilities as a socket created by the core itself.
46 * To use InspSocket, you must inherit a class from it,
47 * and use the InspSocket constructors to establish connections
50 class InspSocket : public Extensible
54 std::deque<std::string> outbuffer;
57 * The file descriptor of this socket
62 * The hostname connected to
67 * The port connected to, or the port
68 * this socket is listening on
73 * The state for this socket, either
74 * listening, connecting, connected
77 InspSocketState state;
80 * The host being connected to,
86 * The host being connected to,
92 * When this time is reached,
93 * the socket times out if it is
94 * in the CONNECTING state
99 * This value is true if the
100 * socket has timed out.
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.
114 * The IP address being connected
115 * to stored in string form for
116 * easy retrieval by accessors.
121 * Client sockaddr structure used
124 insp_sockaddr client;
127 * Server sockaddr structure used
130 insp_sockaddr server;
133 * Used by accept() to indicate the
134 * sizes of the sockaddr_in structures
138 /** Flushes the write buffer
140 bool FlushWriteBuffer();
142 /** Set the queue sizes
143 * This private method sets the operating system queue
144 * sizes for this socket to 65535 so that it can queue
145 * more information without application-level queueing
146 * which was required in older software.
148 void SetQueues(int nfd);
150 /** When the socket has been marked as closing, this flag
151 * will be set to true, then the next time the socket is
152 * examined, the socket is deleted and closed.
156 /** Set to true when we're waiting for a write event.
157 * If this is true and a write event comes in, we
158 * call the write instead of the read method.
160 bool WaitingForWriteEvent;
165 * The default constructor does nothing
166 * and should not be used.
171 * This constructor is used to associate
172 * an existing connecting with an InspSocket
173 * class. The given file descriptor must be
174 * valid, and when initialized, the InspSocket
175 * will be set with the given IP address
176 * and placed in CONNECTED state.
178 InspSocket(int newfd, const char* ip);
181 * This constructor is used to create a new
182 * socket, either listening for connections, or an outbound connection to another host.
183 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
184 * connect. You must resolve your hostnames before passing them to InspSocket. To do so,
185 * you should use the nonblocking class 'Resolver'.
186 * @param ipaddr The IP to connect to, or bind to
187 * @param port The port number to connect to, or bind to
188 * @param listening true to listen on the given host:port pair, or false to connect to them
189 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
191 InspSocket(const std::string &ipaddr, int port, bool listening, unsigned long maxtime);
194 * This method is called when an outbound
195 * connection on your socket is completed.
196 * @return false to abort the connection, true to continue
198 virtual bool OnConnected();
201 * This method is called when an error occurs.
202 * A closed socket in itself is not an error,
203 * however errors also generate close events.
204 * @param e The error type which occured
206 virtual void OnError(InspSocketError e);
209 * When an established connection is terminated,
210 * the OnDisconnect method is triggered.
212 virtual int OnDisconnect();
215 * When there is data waiting to be read on a
216 * socket, the OnDataReady() method is called.
217 * Within this method, you *MUST* call the Read()
218 * method to read any pending data. At its lowest
219 * level, this event is signalled by the core via
220 * the socket engine. If you return false from this
221 * function, the core removes your socket from its
222 * list and erases it from the socket engine, then
223 * calls InspSocket::Close() and deletes it.
224 * @return false to close the socket
226 virtual bool OnDataReady();
228 virtual bool OnWriteReady();
231 * When an outbound connection fails, and the
232 * attempt times out, you will receive this event.
233 * The method will trigger once maxtime seconds are
234 * reached (as given in the constructor) just
235 * before the socket's descriptor is closed.
236 * A failed DNS lookup may cause this event if
237 * the DNS server is not responding, as well as
238 * a failed connect() call, because DNS lookups are
239 * nonblocking as implemented by this class.
241 virtual void OnTimeout();
244 * Whenever close() is called, OnClose() will be
245 * called first. Please note that this means
246 * OnClose will be called alongside OnError(),
247 * OnTimeout(), and Close(), and also when
248 * cancelling a listening socket by calling
249 * the destructor indirectly.
251 virtual void OnClose();
254 * Reads all pending bytes from the socket
255 * into a char* array which can be up to
256 * 16 kilobytes in length.
258 virtual char* Read();
261 * Returns the IP address associated with
262 * this connection, or an empty string if
263 * no IP address exists.
268 * This function checks if the socket has
269 * timed out yet, given the current time
271 * @return true if timed out, false if not timed out
273 bool Timeout(time_t current);
276 * Writes a std::string to the socket. No carriage
277 * returns or linefeeds are appended to the string.
278 * @param data The data to send
280 virtual int Write(const std::string &data);
283 * If your socket is a listening socket, when a new
284 * connection comes in on the socket this method will
285 * be called. Given the new file descriptor in the
286 * parameters, and the IP, it is recommended you copy
287 * them to a new instance of your socket class,
290 * MySocket* newsocket = new MySocket(newfd,ip);
292 * Once you have done this, you can then associate the
293 * new socket with the core using Server::AddSocket().
295 virtual int OnIncomingConnection(int newfd, char* ip);
298 * Changes the socket's state. The core uses this
299 * to change socket states, and you should not call
302 void SetState(InspSocketState s);
305 * Call this to receive the next write event
306 * that comes along for this fd to the OnWriteReady
312 * Returns the current socket state.
314 InspSocketState GetState();
317 * Only the core should call this function.
318 * When called, it is assumed the socket is ready
319 * to read data, and the method call routes the
320 * event to the various methods of InspSocket
321 * for you to handle. This can also cause the
322 * socket's state to change.
327 * This method returns the socket's file descriptor
328 * as assigned by the operating system, or -1
329 * if no descriptor has been assigned.
334 * This method causes the socket to close, and may
335 * also be triggered by other methods such as OnTimeout
338 virtual void Close();
341 * The destructor may implicitly call OnClose(), and
342 * will close() and shutdown() the file descriptor
343 * used for this socket.
345 virtual ~InspSocket();
348 * This method attempts to connect to a hostname.
349 * This only occurs on a non-listening socket. This
350 * method is asyncronous.
352 virtual bool DoConnect();
355 * This method marks the socket closed.
356 * The next time the core examines a socket marked
357 * as closed, the socket will be closed and the