1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #ifndef __INSP_SOCKET_H__
15 #define __INSP_SOCKET_H__
20 * States which a socket may be in
22 enum BufferedSocketState
24 /** Socket disconnected */
26 /** Socket connecting */
28 /** Socket fully connected */
30 /** Socket has an error */
35 * Error types which a socket may exhibit
37 enum BufferedSocketError
39 /** Socket connect timed out */
41 /** Socket could not be created */
43 /** Socket could not connect (refused) */
45 /** Socket could not bind to local port/ip */
47 /** Socket could not reslve host (depreciated) */
49 /** Socket could not write data */
51 /** No more file descriptors left to create socket! */
55 /* Required forward declarations */
59 /** Used to time out socket connections
61 class CoreExport SocketTimeout : public Timer
64 /** BufferedSocket the class is attached to
68 /** Server instance creating the timeout class
70 InspIRCd* ServerInstance;
72 /** File descriptor of class this is attached to
77 /** Create a socket timeout class
78 * @param fd File descriptor of BufferedSocket
79 * @pram Instance server instance to attach to
80 * @param thesock BufferedSocket to attach to
81 * @param secs_from_now Seconds from now to time out
82 * @param now The current time
84 SocketTimeout(int fd, InspIRCd* Instance, BufferedSocket* thesock, long secs_from_now, time_t now) : Timer(secs_from_now, now), sock(thesock), ServerInstance(Instance), sfd(fd) { };
88 virtual void Tick(time_t now);
92 * BufferedSocket is an extendable socket class which modules
93 * can use for TCP socket support. It is fully integrated
94 * into InspIRCds socket loop and attaches its sockets to
95 * the core's instance of the SocketEngine class, meaning
96 * that all use is fully asynchronous.
98 * To use BufferedSocket, you must inherit a class from it.
100 class CoreExport BufferedSocket : public EventHandler
108 /** Instance we were created by
110 InspIRCd* ServerInstance;
112 /** Timeout class or NULL
114 SocketTimeout* Timeout;
116 /** Socket output buffer (binary safe)
118 std::deque<std::string> outbuffer;
120 /** The hostname connected to
124 /** The port connected to
129 * The state for this socket, either
130 * listening, connecting, connected
133 BufferedSocketState state;
136 * The IP address being connected
137 * to stored in string form for
138 * easy retrieval by accessors.
143 * Used by accept() to indicate the
144 * sizes of the sockaddr_in structures
148 /** Flushes the write buffer
149 * @returns true if the writing failed, false if it was successful
151 bool FlushWriteBuffer();
153 /** Set the queue sizes
154 * This private method sets the operating system queue
155 * sizes for this socket to 65535 so that it can queue
156 * more information without application-level queueing
157 * which was required in older software.
161 /** When the socket has been marked as closing, this flag
162 * will be set to true, then the next time the socket is
163 * examined, the socket is deleted and closed.
169 * @param ip IP to bind to
170 * @return True is the binding succeeded
172 bool BindAddr(const std::string &ip);
175 * The default constructor does nothing
176 * and should not be used.
178 BufferedSocket(InspIRCd* SI);
181 * This constructor is used to associate
182 * an existing connecting with an BufferedSocket
183 * class. The given file descriptor must be
184 * valid, and when initialized, the BufferedSocket
185 * will be set with the given IP address
186 * and placed in CONNECTED state.
188 BufferedSocket(InspIRCd* SI, int newfd, const char* ip);
191 * This constructor is used to create a new outbound connection to another host.
192 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
193 * connect. You must resolve your hostnames before passing them to BufferedSocket. To do so,
194 * you should use the nonblocking class 'Resolver'.
195 * @param ipaddr The IP to connect to, or bind to
196 * @param port The port number to connect to
197 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
198 * @param connectbindip When creating an outbound connection, the IP to bind the connection to. If not defined, the port is not bound.
199 * @return On exit, GetState() returns I_ERROR if an error occured, and errno can be used to read the socket error.
201 BufferedSocket(InspIRCd* SI, const std::string &ipaddr, int port, unsigned long maxtime, const std::string &connectbindip = "");
204 * This method is called when an outbound
205 * connection on your socket is completed.
206 * @return false to abort the connection, true to continue
208 virtual bool OnConnected();
211 * This method is called when an error occurs.
212 * A closed socket in itself is not an error,
213 * however errors also generate close events.
214 * @param e The error type which occured
216 virtual void OnError(BufferedSocketError e);
219 * When an established connection is terminated,
220 * the OnDisconnect method is triggered.
222 virtual int OnDisconnect();
225 * When there is data waiting to be read on a
226 * socket, the OnDataReady() method is called.
227 * Within this method, you *MUST* call the Read()
228 * method to read any pending data. At its lowest
229 * level, this event is signalled by the core via
230 * the socket engine. If you return false from this
231 * function, the core removes your socket from its
232 * list and erases it from the socket engine, then
233 * calls BufferedSocket::Close() and deletes it.
234 * @return false to close the socket
236 virtual bool OnDataReady();
239 * When it is ok to write to the socket, and a
240 * write event was requested, this method is
243 * Within this method you should call
244 * write() or send() etc, to send data to the
245 * other end of the socket.
247 * Further write events will not be triggered
248 * unless you call SocketEngine::WantWrite().
250 * The default behaviour of this method is to
251 * flush the write buffer, respecting the IO
254 * XXX: this used to be virtual, ask us if you need it to be so.
255 * @return false to close the socket
260 * When an outbound connection fails, and the
261 * attempt times out, you will receive this event.
262 * The method will trigger once maxtime seconds are
263 * reached (as given in the constructor) just
264 * before the socket's descriptor is closed.
265 * A failed DNS lookup may cause this event if
266 * the DNS server is not responding, as well as
267 * a failed connect() call, because DNS lookups are
268 * nonblocking as implemented by this class.
270 virtual void OnTimeout();
273 * Whenever close() is called, OnClose() will be
274 * called first. Please note that this means
275 * OnClose will be called alongside OnError(),
276 * OnTimeout(), and Close().
278 virtual void OnClose();
281 * Reads all pending bytes from the socket
282 * into a char* array which can be up to
283 * 16 kilobytes in length.
285 virtual const char* Read();
288 * Returns the IP address associated with
289 * this connection, or an empty string if
290 * no IP address exists.
295 * Writes a std::string to the socket. No carriage
296 * returns or linefeeds are appended to the string.
297 * @param data The data to send
299 virtual void Write(const std::string &data);
302 * Changes the socket's state. The core uses this
303 * to change socket states, and you should not call
306 void SetState(BufferedSocketState s);
309 * Returns the current socket state.
311 BufferedSocketState GetState();
313 /** Mark a socket as being connected and call appropriate events.
315 bool InternalMarkConnected();
318 * This method causes the socket to close, and may
319 * also be triggered by other methods such as OnTimeout
322 virtual void Close();
325 * The destructor may implicitly call OnClose(), and
326 * will close() and shutdown() the file descriptor
327 * used for this socket.
329 virtual ~BufferedSocket();
332 * This method attempts to connect to a hostname.
333 * This method is asyncronous.
334 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
336 virtual bool DoConnect(unsigned long maxtime);
338 /** Handle event from EventHandler parent class
340 void HandleEvent(EventType et, int errornum = 0);
342 /** Returns true if this socket is readable