1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 using irc::sockets::insp_sockaddr;
60 using irc::sockets::insp_inaddr;
61 using irc::sockets::insp_ntoa;
62 using irc::sockets::insp_aton;
64 /** Used to time out socket connections
66 class CoreExport SocketTimeout : public Timer
69 /** BufferedSocket the class is attached to
72 /** Server instance creating the timeout class
74 InspIRCd* ServerInstance;
75 /** File descriptor of class this is attached to
79 /** Create a socket timeout class
80 * @param fd File descriptor of BufferedSocket
81 * @pram Instance server instance to attach to
82 * @param thesock BufferedSocket to attach to
83 * @param secs_from_now Seconds from now to time out
84 * @param now The current time
86 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) { };
89 virtual void Tick(time_t now);
93 * BufferedSocket is an extendable socket class which modules
94 * can use for TCP socket support. It is fully integrated
95 * into InspIRCds socket loop and attaches its sockets to
96 * the core's instance of the SocketEngine class, meaning
97 * that any sockets you create have the same power and
98 * abilities as a socket created by the core itself.
99 * To use BufferedSocket, you must inherit a class from it,
100 * and use the BufferedSocket constructors to establish connections
103 class CoreExport BufferedSocket : public EventHandler
111 /** Instance we were created by
115 /** Timeout class or NULL
117 SocketTimeout* Timeout;
121 unsigned long timeout_val;
123 /** Socket output buffer (binary safe)
125 std::deque<std::string> outbuffer;
127 /** The hostname connected to
131 /** The port connected to
136 * The state for this socket, either
137 * listening, connecting, connected
140 BufferedSocketState state;
143 * This value is true if the
144 * socket has timed out.
149 * The IP address being connected
150 * to stored in string form for
151 * easy retrieval by accessors.
156 * Used by accept() to indicate the
157 * sizes of the sockaddr_in structures
161 /** Flushes the write buffer
162 * @returns true if the writing failed, false if it was successful
164 bool FlushWriteBuffer();
166 /** Set the queue sizes
167 * This private method sets the operating system queue
168 * sizes for this socket to 65535 so that it can queue
169 * more information without application-level queueing
170 * which was required in older software.
172 void SetQueues(int nfd);
174 /** When the socket has been marked as closing, this flag
175 * will be set to true, then the next time the socket is
176 * examined, the socket is deleted and closed.
182 * @param ip IP to bind to
183 * @return True is the binding succeeded
185 bool BindAddr(const std::string &ip);
188 * The default constructor does nothing
189 * and should not be used.
191 BufferedSocket(InspIRCd* SI);
194 * This constructor is used to associate
195 * an existing connecting with an BufferedSocket
196 * class. The given file descriptor must be
197 * valid, and when initialized, the BufferedSocket
198 * will be set with the given IP address
199 * and placed in CONNECTED state.
201 BufferedSocket(InspIRCd* SI, int newfd, const char* ip);
204 * This constructor is used to create a new outbound connection to another host.
205 * Note that if you specify a hostname in the 'ipaddr' parameter, this class will not
206 * connect. You must resolve your hostnames before passing them to BufferedSocket. To do so,
207 * you should use the nonblocking class 'Resolver'.
208 * @param ipaddr The IP to connect to, or bind to
209 * @param port The port number to connect to
210 * @param maxtime Number of seconds to wait, if connecting, before the connection times out and an OnTimeout() event is generated
211 * @param connectbindip When creating an outbound connection, the IP to bind the connection to. If not defined, the port is not bound.
212 * @return On exit, GetState() returns I_ERROR if an error occured, and errno can be used to read the socket error.
214 BufferedSocket(InspIRCd* SI, const std::string &ipaddr, int port, unsigned long maxtime, const std::string &connectbindip = "");
217 * This method is called when an outbound
218 * connection on your socket is completed.
219 * @return false to abort the connection, true to continue
221 virtual bool OnConnected();
224 * This method is called when an error occurs.
225 * A closed socket in itself is not an error,
226 * however errors also generate close events.
227 * @param e The error type which occured
229 virtual void OnError(BufferedSocketError e);
232 * When an established connection is terminated,
233 * the OnDisconnect method is triggered.
235 virtual int OnDisconnect();
238 * When there is data waiting to be read on a
239 * socket, the OnDataReady() method is called.
240 * Within this method, you *MUST* call the Read()
241 * method to read any pending data. At its lowest
242 * level, this event is signalled by the core via
243 * the socket engine. If you return false from this
244 * function, the core removes your socket from its
245 * list and erases it from the socket engine, then
246 * calls BufferedSocket::Close() and deletes it.
247 * @return false to close the socket
249 virtual bool OnDataReady();
252 * When it is ok to write to the socket, and a
253 * write event was requested, this method is
256 * Within this method you should call
257 * write() or send() etc, to send data to the
258 * other end of the socket.
260 * Further write events will not be triggered
261 * unless you call WantWrite().
263 * The default behaviour of this method is to
264 * flush the write buffer, respecting the IO
267 * XXX: this used to be virtual, ask us if you need it to be so.
268 * @return false to close the socket
273 * When an outbound connection fails, and the
274 * attempt times out, you will receive this event.
275 * The method will trigger once maxtime seconds are
276 * reached (as given in the constructor) just
277 * before the socket's descriptor is closed.
278 * A failed DNS lookup may cause this event if
279 * the DNS server is not responding, as well as
280 * a failed connect() call, because DNS lookups are
281 * nonblocking as implemented by this class.
283 virtual void OnTimeout();
286 * Whenever close() is called, OnClose() will be
287 * called first. Please note that this means
288 * OnClose will be called alongside OnError(),
289 * OnTimeout(), and Close().
291 virtual void OnClose();
294 * Reads all pending bytes from the socket
295 * into a char* array which can be up to
296 * 16 kilobytes in length.
298 virtual const char* Read();
301 * Returns the IP address associated with
302 * this connection, or an empty string if
303 * no IP address exists.
308 * Writes a std::string to the socket. No carriage
309 * returns or linefeeds are appended to the string.
310 * @param data The data to send
312 virtual void Write(const std::string &data);
315 * Changes the socket's state. The core uses this
316 * to change socket states, and you should not call
319 void SetState(BufferedSocketState s);
322 * Call this to receive the next write event
323 * that comes along for this fd to the OnWriteReady
329 * Returns the current socket state.
331 BufferedSocketState GetState();
334 * Only the core should call this function.
335 * When called, it is assumed the socket is ready
336 * to read data, and the method call routes the
337 * event to the various methods of BufferedSocket
338 * for you to handle. This can also cause the
339 * socket's state to change.
344 * This method causes the socket to close, and may
345 * also be triggered by other methods such as OnTimeout
348 virtual void Close();
351 * The destructor may implicitly call OnClose(), and
352 * will close() and shutdown() the file descriptor
353 * used for this socket.
355 virtual ~BufferedSocket();
358 * This method attempts to connect to a hostname.
359 * This method is asyncronous.
361 virtual bool DoConnect();
363 /** Handle event from EventHandler parent class
365 void HandleEvent(EventType et, int errornum = 0);
367 /** Returns true if this socket is readable