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"
26 #include "inspsocket.h"
30 * States which a socket may be in
32 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
35 * Error types which a socket may exhibit
37 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE, I_ERR_NOMOREFDS };
42 using irc::sockets::insp_sockaddr;
43 using irc::sockets::insp_inaddr;
44 using irc::sockets::insp_ntoa;
45 using irc::sockets::insp_aton;
47 /** Used to time out socket connections
49 class SocketTimeout : public InspTimer
53 InspIRCd* ServerInstance;
56 SocketTimeout(int fd, InspIRCd* Instance, InspSocket* thesock, long secs_from_now, time_t now) : InspTimer(secs_from_now, now), sock(thesock), ServerInstance(Instance), sfd(fd) { };
57 virtual void Tick(time_t now);
61 * InspSocket is an extendable socket class which modules
62 * can use for TCP socket support. It is fully integrated
63 * into InspIRCds socket loop and attaches its sockets to
64 * the core's instance of the SocketEngine class, meaning
65 * that any sockets you create have the same power and
66 * abilities as a socket created by the core itself.
67 * To use InspSocket, you must inherit a class from it,
68 * and use the InspSocket constructors to establish connections
71 class InspSocket : public EventHandler
79 SocketTimeout* Timeout;
81 unsigned long timeout_val;
83 std::deque<std::string> outbuffer;
86 * The hostname connected to
91 * The port connected to, or the port
92 * this socket is listening on
97 * The state for this socket, either
98 * listening, connecting, connected
101 InspSocketState state;
104 * The host being connected to,
110 * The host being connected to,
116 * This value is true if the
117 * socket has timed out.
122 * Socket input buffer, used by read(). The class which
123 * extends InspSocket is expected to implement an extendable
124 * buffer which can grow much larger than 64k,
125 * this buffer is just designed to be temporary storage.
131 * The IP address being connected
132 * to stored in string form for
133 * easy retrieval by accessors.
138 * Client sockaddr structure used
141 insp_sockaddr client;
144 * Server sockaddr structure used
147 insp_sockaddr server;
150 * Used by accept() to indicate the
151 * sizes of the sockaddr_in structures
155 /** Flushes the write buffer
157 bool FlushWriteBuffer();
159 /** Set the queue sizes
160 * This private method sets the operating system queue
161 * sizes for this socket to 65535 so that it can queue
162 * more information without application-level queueing
163 * which was required in older software.
165 void SetQueues(int nfd);
167 /** When the socket has been marked as closing, this flag
168 * will be set to true, then the next time the socket is
169 * examined, the socket is deleted and closed.
173 /** Set to true when we're waiting for a write event.
174 * If this is true and a write event comes in, we
175 * call the write instead of the read method.
177 bool WaitingForWriteEvent;
182 * The default constructor does nothing
183 * and should not be used.
185 InspSocket(InspIRCd* SI);
188 * This constructor is used to associate
189 * an existing connecting with an InspSocket
190 * class. The given file descriptor must be
191 * valid, and when initialized, the InspSocket
192 * will be set with the given IP address
193 * and placed in CONNECTED state.
195 InspSocket(InspIRCd* SI, int newfd, const char* ip);
198 * This constructor is used to create a new
199 * socket, either listening for connections, or an outbound connection to another host.
200 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
201 * connect. You must resolve your hostnames before passing them to InspSocket. To do so,
202 * you should use the nonblocking class 'Resolver'.
203 * @param ipaddr The IP to connect to, or bind to
204 * @param port The port number to connect to, or bind to
205 * @param listening true to listen on the given host:port pair, or false to connect to them
206 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
208 InspSocket(InspIRCd* SI, const std::string &ipaddr, int port, bool listening, unsigned long maxtime);
211 * This method is called when an outbound
212 * connection on your socket is completed.
213 * @return false to abort the connection, true to continue
215 virtual bool OnConnected();
218 * This method is called when an error occurs.
219 * A closed socket in itself is not an error,
220 * however errors also generate close events.
221 * @param e The error type which occured
223 virtual void OnError(InspSocketError e);
226 * When an established connection is terminated,
227 * the OnDisconnect method is triggered.
229 virtual int OnDisconnect();
232 * When there is data waiting to be read on a
233 * socket, the OnDataReady() method is called.
234 * Within this method, you *MUST* call the Read()
235 * method to read any pending data. At its lowest
236 * level, this event is signalled by the core via
237 * the socket engine. If you return false from this
238 * function, the core removes your socket from its
239 * list and erases it from the socket engine, then
240 * calls InspSocket::Close() and deletes it.
241 * @return false to close the socket
243 virtual bool OnDataReady();
245 virtual bool OnWriteReady();
248 * When an outbound connection fails, and the
249 * attempt times out, you will receive this event.
250 * The method will trigger once maxtime seconds are
251 * reached (as given in the constructor) just
252 * before the socket's descriptor is closed.
253 * A failed DNS lookup may cause this event if
254 * the DNS server is not responding, as well as
255 * a failed connect() call, because DNS lookups are
256 * nonblocking as implemented by this class.
258 virtual void OnTimeout();
261 * Whenever close() is called, OnClose() will be
262 * called first. Please note that this means
263 * OnClose will be called alongside OnError(),
264 * OnTimeout(), and Close(), and also when
265 * cancelling a listening socket by calling
266 * the destructor indirectly.
268 virtual void OnClose();
271 * Reads all pending bytes from the socket
272 * into a char* array which can be up to
273 * 16 kilobytes in length.
275 virtual char* Read();
278 * Returns the IP address associated with
279 * this connection, or an empty string if
280 * no IP address exists.
285 * Writes a std::string to the socket. No carriage
286 * returns or linefeeds are appended to the string.
287 * @param data The data to send
289 virtual int Write(const std::string &data);
292 * If your socket is a listening socket, when a new
293 * connection comes in on the socket this method will
294 * be called. Given the new file descriptor in the
295 * parameters, and the IP, it is recommended you copy
296 * them to a new instance of your socket class,
299 * MySocket* newsocket = new MySocket(newfd,ip);
301 * Once you have done this, you can then associate the
302 * new socket with the core using Server::AddSocket().
304 virtual int OnIncomingConnection(int newfd, char* ip);
307 * Changes the socket's state. The core uses this
308 * to change socket states, and you should not call
311 void SetState(InspSocketState s);
314 * Call this to receive the next write event
315 * that comes along for this fd to the OnWriteReady
321 * Returns the current socket state.
323 InspSocketState GetState();
326 * Only the core should call this function.
327 * When called, it is assumed the socket is ready
328 * to read data, and the method call routes the
329 * event to the various methods of InspSocket
330 * for you to handle. This can also cause the
331 * socket's state to change.
336 * This method returns the socket's file descriptor
337 * as assigned by the operating system, or -1
338 * if no descriptor has been assigned.
343 * This method causes the socket to close, and may
344 * also be triggered by other methods such as OnTimeout
347 virtual void Close();
350 * The destructor may implicitly call OnClose(), and
351 * will close() and shutdown() the file descriptor
352 * used for this socket.
354 virtual ~InspSocket();
357 * This method attempts to connect to a hostname.
358 * This only occurs on a non-listening socket. This
359 * method is asyncronous.
361 virtual bool DoConnect();
364 * This method marks the socket closed.
365 * The next time the core examines a socket marked
366 * as closed, the socket will be closed and the
371 void HandleEvent(EventType et, int errornum = 0);