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>
28 * States which a socket may be in
30 enum InspSocketState { I_DISCONNECTED, I_RESOLVING, 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 };
38 * InspSocket is an extendable socket class which modules
39 * can use for TCP socket support. It is fully integrated
40 * into InspIRCds socket loop and attaches its sockets to
41 * the core's instance of the SocketEngine class, meaning
42 * that any sockets you create have the same power and
43 * abilities as a socket created by the core itself.
44 * To use InspSocket, you must inherit a class from it,
45 * and use the InspSocket constructors to establish connections
53 * The file descriptor of this socket
58 * The resolver for this socket
63 * The hostname connected to
68 * The port connected to, or the port
69 * this socket is listening on
74 * The state for this socket, either
75 * listening, connecting, connected
78 InspSocketState state;
81 * The host being connected to,
87 * The host being connected to,
93 * When this time is reached,
94 * the socket times out if it is
95 * in the CONNECTING state
100 * This value is true if the
101 * socket has timed out.
106 * Socket input buffer, used by read(). The class which
107 * extends InspSocket is expected to implement an extendable
108 * buffer which can grow much larger than 64k,
109 * this buffer is just designed to be temporary storage.
115 * The IP address being connected
116 * to stored in string form for
117 * easy retrieval by accessors.
122 * Client sockaddr structure used
128 * Server sockaddr structure used
134 * Used by accept() to indicate the
135 * sizes of the sockaddr_in structures
139 /** Flushes the write buffer
141 bool FlushWriteBuffer();
143 void SetQueues(int nfd);
150 * The default constructor does nothing
151 * and should not be used.
156 * This constructor is used to associate
157 * an existing connecting with an InspSocket
158 * class. The given file descriptor must be
159 * valid, and when initialized, the InspSocket
160 * will be set with the given IP address
161 * and placed in CONNECTED state.
163 InspSocket(int newfd, char* ip);
166 * This constructor is used to create a new
167 * socket, either listening for connections, or an outbound connection to another host.
168 * Note that if you specify a hostname in the 'host' parameter, then there will be an extra
169 * step involved (a nonblocking DNS lookup) which will cause your connection to be established
170 * slower than if it was an IP. Therefore, use an IP address where it is available instead.
171 * @param host The hostname to connect to, or bind to
172 * @param port The port number to connect to, or bind to
173 * @param listening true to listen on the given host:port pair, or false to connect to them
174 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
176 InspSocket(const std::string &host, int port, bool listening, unsigned long maxtime);
179 * This method is called when an outbound
180 * connection on your socket is completed.
181 * @return false to abort the connection, true to continue
183 virtual bool OnConnected();
186 * This method is called when an error occurs.
187 * A closed socket in itself is not an error,
188 * however errors also generate close events.
189 * @param e The error type which occured
191 virtual void OnError(InspSocketError e);
194 * When an established connection is terminated,
195 * the OnDisconnect method is triggered.
197 virtual int OnDisconnect();
200 * When there is data waiting to be read on a
201 * socket, the OnDataReady() method is called.
202 * Within this method, you *MUST* call the Read()
203 * method to read any pending data. At its lowest
204 * level, this event is signalled by the core via
205 * the socket engine. If you return false from this
206 * function, the core removes your socket from its
207 * list and erases it from the socket engine, then
208 * calls InspSocket::Close() and deletes it.
209 * @return false to close the socket
211 virtual bool OnDataReady();
214 * When an outbound connection fails, and the
215 * attempt times out, you will receive this event.
216 * The method will trigger once maxtime seconds are
217 * reached (as given in the constructor) just
218 * before the socket's descriptor is closed.
219 * A failed DNS lookup may cause this event if
220 * the DNS server is not responding, as well as
221 * a failed connect() call, because DNS lookups are
222 * nonblocking as implemented by this class.
224 virtual void OnTimeout();
227 * Whenever close() is called, OnClose() will be
228 * called first. Please note that this means
229 * OnClose will be called alongside OnError(),
230 * OnTimeout(), and Close(), and also when
231 * cancelling a listening socket by calling
232 * the destructor indirectly.
234 virtual void OnClose();
237 * Reads all pending bytes from the socket
238 * into a char* array which can be up to
239 * 16 kilobytes in length.
241 virtual char* Read();
244 * Returns the IP address associated with
245 * this connection, or an empty string if
246 * no IP address exists.
251 * This function checks if the socket has
252 * timed out yet, given the current time
254 * @return true if timed out, false if not timed out
256 bool Timeout(time_t current);
259 * Writes a std::string to the socket. No carriage
260 * returns or linefeeds are appended to the string.
261 * @param data The data to send
263 virtual int Write(const std::string &data);
266 * If your socket is a listening socket, when a new
267 * connection comes in on the socket this method will
268 * be called. Given the new file descriptor in the
269 * parameters, and the IP, it is recommended you copy
270 * them to a new instance of your socket class,
273 * MySocket* newsocket = new MySocket(newfd,ip);
275 * Once you have done this, you can then associate the
276 * new socket with the core using Server::AddSocket().
278 virtual int OnIncomingConnection(int newfd, char* ip);
281 * Changes the socket's state. The core uses this
282 * to change socket states, and you should not call
285 void SetState(InspSocketState s);
288 * Returns the current socket state.
290 InspSocketState GetState();
293 * Only the core should call this function.
294 * When called, it is assumed the socket is ready
295 * to read data, and the method call routes the
296 * event to the various methods of InspSocket
297 * for you to handle. This can also cause the
298 * socket's state to change.
303 * This method returns the socket's file descriptor
304 * as assigned by the operating system, or -1
305 * if no descriptor has been assigned.
310 * This method causes the socket to close, and may
311 * also be triggered by other methods such as OnTimeout
314 virtual void Close();
317 * The destructor may implicitly call OnClose(), and
318 * will close() and shutdown() the file descriptor
319 * used for this socket.
321 virtual ~InspSocket();
323 virtual bool DoResolve();
324 virtual bool DoConnect();