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__
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
29 * States which a socket may be in
31 enum InspSocketState { I_DISCONNECTED, I_RESOLVING, 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 };
39 * InspSocket is an extendable socket class which modules
40 * can use for TCP socket support. It is fully integrated
41 * into InspIRCds socket loop and attaches its sockets to
42 * the core's instance of the SocketEngine class, meaning
43 * that any sockets you create have the same power and
44 * abilities as a socket created by the core itself.
45 * To use InspSocket, you must inherit a class from it,
46 * and use the InspSocket constructors to establish connections
53 std::deque<std::string> outbuffer;
56 * The file descriptor of this socket
61 * The resolver for this socket
66 * The hostname connected to
71 * The port connected to, or the port
72 * this socket is listening on
77 * The state for this socket, either
78 * listening, connecting, connected
81 InspSocketState state;
84 * The host being connected to,
90 * The host being connected to,
96 * When this time is reached,
97 * the socket times out if it is
98 * in the CONNECTING state
103 * This value is true if the
104 * socket has timed out.
109 * Socket input buffer, used by read(). The class which
110 * extends InspSocket is expected to implement an extendable
111 * buffer which can grow much larger than 64k,
112 * this buffer is just designed to be temporary storage.
118 * The IP address being connected
119 * to stored in string form for
120 * easy retrieval by accessors.
125 * Client sockaddr structure used
131 * Server sockaddr structure used
137 * Used by accept() to indicate the
138 * sizes of the sockaddr_in structures
142 /** Flushes the write buffer
144 bool FlushWriteBuffer();
146 /** Set the queue sizes
147 * This private method sets the operating system queue
148 * sizes for this socket to 65535 so that it can queue
149 * more information without application-level queueing
150 * which was required in older software.
152 void SetQueues(int nfd);
154 /** When the socket has been marked as closing, this flag
155 * will be set to true, then the next time the socket is
156 * examined, the socket is deleted and closed.
163 * The default constructor does nothing
164 * and should not be used.
169 * This constructor is used to associate
170 * an existing connecting with an InspSocket
171 * class. The given file descriptor must be
172 * valid, and when initialized, the InspSocket
173 * will be set with the given IP address
174 * and placed in CONNECTED state.
176 InspSocket(int newfd, char* ip);
179 * This constructor is used to create a new
180 * socket, either listening for connections, or an outbound connection to another host.
181 * Note that if you specify a hostname in the 'host' parameter, then there will be an extra
182 * step involved (a nonblocking DNS lookup) which will cause your connection to be established
183 * slower than if it was an IP. Therefore, use an IP address where it is available instead.
184 * @param host The hostname to connect to, or bind to
185 * @param port The port number to connect to, or bind to
186 * @param listening true to listen on the given host:port pair, or false to connect to them
187 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
189 InspSocket(const std::string &host, int port, bool listening, unsigned long maxtime);
192 * This method is called when an outbound
193 * connection on your socket is completed.
194 * @return false to abort the connection, true to continue
196 virtual bool OnConnected();
199 * This method is called when an error occurs.
200 * A closed socket in itself is not an error,
201 * however errors also generate close events.
202 * @param e The error type which occured
204 virtual void OnError(InspSocketError e);
207 * When an established connection is terminated,
208 * the OnDisconnect method is triggered.
210 virtual int OnDisconnect();
213 * When there is data waiting to be read on a
214 * socket, the OnDataReady() method is called.
215 * Within this method, you *MUST* call the Read()
216 * method to read any pending data. At its lowest
217 * level, this event is signalled by the core via
218 * the socket engine. If you return false from this
219 * function, the core removes your socket from its
220 * list and erases it from the socket engine, then
221 * calls InspSocket::Close() and deletes it.
222 * @return false to close the socket
224 virtual bool OnDataReady();
227 * When an outbound connection fails, and the
228 * attempt times out, you will receive this event.
229 * The method will trigger once maxtime seconds are
230 * reached (as given in the constructor) just
231 * before the socket's descriptor is closed.
232 * A failed DNS lookup may cause this event if
233 * the DNS server is not responding, as well as
234 * a failed connect() call, because DNS lookups are
235 * nonblocking as implemented by this class.
237 virtual void OnTimeout();
240 * Whenever close() is called, OnClose() will be
241 * called first. Please note that this means
242 * OnClose will be called alongside OnError(),
243 * OnTimeout(), and Close(), and also when
244 * cancelling a listening socket by calling
245 * the destructor indirectly.
247 virtual void OnClose();
250 * Reads all pending bytes from the socket
251 * into a char* array which can be up to
252 * 16 kilobytes in length.
254 virtual char* Read();
257 * Returns the IP address associated with
258 * this connection, or an empty string if
259 * no IP address exists.
264 * This function checks if the socket has
265 * timed out yet, given the current time
267 * @return true if timed out, false if not timed out
269 bool Timeout(time_t current);
272 * Writes a std::string to the socket. No carriage
273 * returns or linefeeds are appended to the string.
274 * @param data The data to send
276 virtual int Write(const std::string &data);
279 * If your socket is a listening socket, when a new
280 * connection comes in on the socket this method will
281 * be called. Given the new file descriptor in the
282 * parameters, and the IP, it is recommended you copy
283 * them to a new instance of your socket class,
286 * MySocket* newsocket = new MySocket(newfd,ip);
288 * Once you have done this, you can then associate the
289 * new socket with the core using Server::AddSocket().
291 virtual int OnIncomingConnection(int newfd, char* ip);
294 * Changes the socket's state. The core uses this
295 * to change socket states, and you should not call
298 void SetState(InspSocketState s);
301 * Returns the current socket state.
303 InspSocketState GetState();
306 * Only the core should call this function.
307 * When called, it is assumed the socket is ready
308 * to read data, and the method call routes the
309 * event to the various methods of InspSocket
310 * for you to handle. This can also cause the
311 * socket's state to change.
316 * This method returns the socket's file descriptor
317 * as assigned by the operating system, or -1
318 * if no descriptor has been assigned.
323 * This method causes the socket to close, and may
324 * also be triggered by other methods such as OnTimeout
327 virtual void Close();
330 * The destructor may implicitly call OnClose(), and
331 * will close() and shutdown() the file descriptor
332 * used for this socket.
334 virtual ~InspSocket();
337 * This method attempts to resolve the hostname,
338 * if a hostname is given and not an IP,
339 * before a connection can occur. This method is
342 virtual bool DoResolve();
345 * This method attempts to connect to a hostname.
346 * This only occurs on a non-listening socket. This
347 * method is asyncronous.
349 virtual bool DoConnect();
352 * This method marks the socket closed.
353 * The next time the core examines a socket marked
354 * as closed, the socket will be closed and the