1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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>
27 * States which a socket may be in
29 enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
32 * Error types which a socket may exhibit
34 enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND };
37 * InspSocket is an extendable socket class which modules
38 * can use for TCP socket support. It is fully integrated
39 * into InspIRCds socket loop and attaches its sockets to
40 * the core's instance of the SocketEngine class, meaning
41 * that any sockets you create have the same power and
42 * abilities as a socket created by the core itself.
43 * To use InspSocket, you must inherit a class from it,
44 * and use the InspSocket constructors to establish connections
52 * The file descriptor of this socket
57 * The hostname connected to
62 * The port connected to, or the port
63 * this socket is listening on
68 * The state for this socket, either
69 * listening, connecting, connected
72 InspSocketState state;
75 * The host being connected to,
81 * The host being connected to,
87 * When this time is reached,
88 * the socket times out if it is
89 * in the CONNECTING state
94 * This value is true if the
95 * socket has timed out.
100 * Socket input buffer, used by read(). The class which
101 * extends InspSocket is expected to implement an extendable
102 * buffer which can grow much larger than 16k,
103 * this buffer is just designed to be temporary storage.
109 * The IP address being connected
110 * to stored in string form for
111 * easy retrieval by accessors.
116 * Client sockaddr structure used
122 * Server sockaddr structure used
128 * Used by accept() to indicate the
129 * sizes of the sockaddr_in structures
136 * The default constructor does nothing
137 * and should not be used.
142 * This constructor is used to associate
143 * an existing connecting with an InspSocket
144 * class. The given file descriptor must be
145 * valid, and when initialized, the InspSocket
146 * will be set with the given IP address
147 * and placed in CONNECTED state.
149 InspSocket(int newfd, char* ip);
152 * This constructor is used to create a new
153 * socket, either listening for connections,
154 * or an outbound connection to another host.
155 * @param host The hostname to connect to, or bind to
156 * @param port The port number to connect to, or bind to
157 * @param listening true to listen on the given host:port pair, or false to connect to them
158 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
160 InspSocket(std::string host, int port, bool listening, unsigned long maxtime);
163 * This method is called when an outbound
164 * connection on your socket is completed.
165 * @return false to abort the connection, true to continue
167 virtual bool OnConnected();
170 * This method is called when an error occurs.
171 * A closed socket in itself is not an error,
172 * however errors also generate close events.
173 * @param e The error type which occured
175 virtual void OnError(InspSocketError e);
178 * When an established connection is terminated,
179 * the OnDisconnect method is triggered.
181 virtual int OnDisconnect();
184 * When there is data waiting to be read on a
185 * socket, the OnDataReady() method is called.
186 * Within this method, you *MUST* call the Read()
187 * method to read any pending data. At its lowest
188 * level, this event is signalled by the core via
189 * the socket engine. If you return false from this
190 * function, the core removes your socket from its
191 * list and erases it from the socket engine, then
192 * calls InspSocket::Close() and deletes it.
193 * @return false to close the socket
195 virtual bool OnDataReady();
198 * When an outbound connection fails, and the
199 * attempt times out, you will receive this event.
200 * The mthod will trigger once maxtime secons are
201 * reached (as given in the constructor) just
202 * before the socket's descriptor is closed.
204 virtual void OnTimeout();
207 * Whenever close() is called, OnClose() will be
208 * called first. Please note that this means
209 * OnClose will be called alongside OnError(),
210 * OnTimeout(), and Close(), and also when
211 * cancelling a listening socket by calling
212 * the destructor indirectly.
214 virtual void OnClose();
217 * Reads all pending bytes from the socket
218 * into a char* array which can be up to
219 * 16 kilobytes in length.
221 virtual char* Read();
224 * Returns the IP address associated with
225 * this connection, or an empty string if
226 * no IP address exists.
231 * This function checks if the socket has
232 * timed out yet, given the current time
234 * @return true if timed out, false if not timed out
236 bool Timeout(time_t current);
239 * Writes a std::string to the socket. No carriage
240 * returns or linefeeds are appended to the string.
241 * @param data The data to send
243 virtual int Write(std::string data);
246 * If your socket is a listening socket, when a new
247 * connection comes in on the socket this method will
248 * be called. Given the new file descriptor in the
249 * parameters, and the IP, it is recommended you copy
250 * them to a new instance of your socket class,
253 * MySocket* newsocket = new MySocket(newfd,ip);
255 * Once you have done this, you can then associate the
256 * new socket with the core using Server::AddSocket().
258 virtual int OnIncomingConnection(int newfd, char* ip);
261 * Changes the socket's state. The core uses this
262 * to change socket states, and you should not call
265 void SetState(InspSocketState s);
268 * Returns the current socket state.
270 InspSocketState GetState();
273 * Only the core should call this function.
274 * When called, it is assumed the socket is ready
275 * to read data, and the method call routes the
276 * event to the various methods of InspSocket
277 * for you to handle. This can also cause the
278 * socket's state to change.
283 * This method returns the socket's file descriptor
284 * as assigned by the operating system, or -1
285 * if no descriptor has been assigned.
290 * This method causes the socket to close, and may
291 * also be triggered by other methods such as OnTimeout
294 virtual void Close();
297 * The destructor may implicitly call OnClose(), and
298 * will close() and shutdown() the file descriptor
299 * used for this socket.
301 virtual ~InspSocket();