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"
29 * States which a socket may be in
31 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
34 * Error types which a socket may exhibit
36 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE, I_ERR_NOMOREFDS };
41 using irc::sockets::insp_sockaddr;
42 using irc::sockets::insp_inaddr;
43 using irc::sockets::insp_ntoa;
44 using irc::sockets::insp_aton;
47 * InspSocket is an extendable socket class which modules
48 * can use for TCP socket support. It is fully integrated
49 * into InspIRCds socket loop and attaches its sockets to
50 * the core's instance of the SocketEngine class, meaning
51 * that any sockets you create have the same power and
52 * abilities as a socket created by the core itself.
53 * To use InspSocket, you must inherit a class from it,
54 * and use the InspSocket constructors to establish connections
57 class InspSocket : public EventHandler
62 std::deque<std::string> outbuffer;
65 * The hostname connected to
70 * The port connected to, or the port
71 * this socket is listening on
76 * The state for this socket, either
77 * listening, connecting, connected
80 InspSocketState state;
83 * The host being connected to,
89 * The host being connected to,
95 * When this time is reached,
96 * the socket times out if it is
97 * in the CONNECTING state
102 * This value is true if the
103 * socket has timed out.
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.
117 * The IP address being connected
118 * to stored in string form for
119 * easy retrieval by accessors.
124 * Client sockaddr structure used
127 insp_sockaddr client;
130 * Server sockaddr structure used
133 insp_sockaddr server;
136 * Used by accept() to indicate the
137 * sizes of the sockaddr_in structures
141 /** Flushes the write buffer
143 bool FlushWriteBuffer();
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.
151 void SetQueues(int nfd);
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.
159 /** Set to true when we're waiting for a write event.
160 * If this is true and a write event comes in, we
161 * call the write instead of the read method.
163 bool WaitingForWriteEvent;
168 * The default constructor does nothing
169 * and should not be used.
171 InspSocket(InspIRCd* SI);
174 * This constructor is used to associate
175 * an existing connecting with an InspSocket
176 * class. The given file descriptor must be
177 * valid, and when initialized, the InspSocket
178 * will be set with the given IP address
179 * and placed in CONNECTED state.
181 InspSocket(InspIRCd* SI, int newfd, const char* ip);
184 * This constructor is used to create a new
185 * socket, either listening for connections, or an outbound connection to another host.
186 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
187 * connect. You must resolve your hostnames before passing them to InspSocket. To do so,
188 * you should use the nonblocking class 'Resolver'.
189 * @param ipaddr The IP to connect to, or bind to
190 * @param port The port number to connect to, or bind to
191 * @param listening true to listen on the given host:port pair, or false to connect to them
192 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
194 InspSocket(InspIRCd* SI, const std::string &ipaddr, int port, bool listening, unsigned long maxtime);
197 * This method is called when an outbound
198 * connection on your socket is completed.
199 * @return false to abort the connection, true to continue
201 virtual bool OnConnected();
204 * This method is called when an error occurs.
205 * A closed socket in itself is not an error,
206 * however errors also generate close events.
207 * @param e The error type which occured
209 virtual void OnError(InspSocketError e);
212 * When an established connection is terminated,
213 * the OnDisconnect method is triggered.
215 virtual int OnDisconnect();
218 * When there is data waiting to be read on a
219 * socket, the OnDataReady() method is called.
220 * Within this method, you *MUST* call the Read()
221 * method to read any pending data. At its lowest
222 * level, this event is signalled by the core via
223 * the socket engine. If you return false from this
224 * function, the core removes your socket from its
225 * list and erases it from the socket engine, then
226 * calls InspSocket::Close() and deletes it.
227 * @return false to close the socket
229 virtual bool OnDataReady();
231 virtual bool OnWriteReady();
234 * When an outbound connection fails, and the
235 * attempt times out, you will receive this event.
236 * The method will trigger once maxtime seconds are
237 * reached (as given in the constructor) just
238 * before the socket's descriptor is closed.
239 * A failed DNS lookup may cause this event if
240 * the DNS server is not responding, as well as
241 * a failed connect() call, because DNS lookups are
242 * nonblocking as implemented by this class.
244 virtual void OnTimeout();
247 * Whenever close() is called, OnClose() will be
248 * called first. Please note that this means
249 * OnClose will be called alongside OnError(),
250 * OnTimeout(), and Close(), and also when
251 * cancelling a listening socket by calling
252 * the destructor indirectly.
254 virtual void OnClose();
257 * Reads all pending bytes from the socket
258 * into a char* array which can be up to
259 * 16 kilobytes in length.
261 virtual char* Read();
264 * Returns the IP address associated with
265 * this connection, or an empty string if
266 * no IP address exists.
271 * This function checks if the socket has
272 * timed out yet, given the current time
274 * @return true if timed out, false if not timed out
276 bool Timeout(time_t current);
279 * Writes a std::string to the socket. No carriage
280 * returns or linefeeds are appended to the string.
281 * @param data The data to send
283 virtual int Write(const std::string &data);
286 * If your socket is a listening socket, when a new
287 * connection comes in on the socket this method will
288 * be called. Given the new file descriptor in the
289 * parameters, and the IP, it is recommended you copy
290 * them to a new instance of your socket class,
293 * MySocket* newsocket = new MySocket(newfd,ip);
295 * Once you have done this, you can then associate the
296 * new socket with the core using Server::AddSocket().
298 virtual int OnIncomingConnection(int newfd, char* ip);
301 * Changes the socket's state. The core uses this
302 * to change socket states, and you should not call
305 void SetState(InspSocketState s);
308 * Call this to receive the next write event
309 * that comes along for this fd to the OnWriteReady
315 * Returns the current socket state.
317 InspSocketState GetState();
320 * Only the core should call this function.
321 * When called, it is assumed the socket is ready
322 * to read data, and the method call routes the
323 * event to the various methods of InspSocket
324 * for you to handle. This can also cause the
325 * socket's state to change.
330 * This method returns the socket's file descriptor
331 * as assigned by the operating system, or -1
332 * if no descriptor has been assigned.
337 * This method causes the socket to close, and may
338 * also be triggered by other methods such as OnTimeout
341 virtual void Close();
344 * The destructor may implicitly call OnClose(), and
345 * will close() and shutdown() the file descriptor
346 * used for this socket.
348 virtual ~InspSocket();
351 * This method attempts to connect to a hostname.
352 * This only occurs on a non-listening socket. This
353 * method is asyncronous.
355 virtual bool DoConnect();
358 * This method marks the socket closed.
359 * The next time the core examines a socket marked
360 * as closed, the socket will be closed and the
365 void HandleEvent(EventType et);