2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7 * Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 * States which a socket may be in
33 enum BufferedSocketState
35 /** Socket disconnected */
37 /** Socket connecting */
39 /** Socket fully connected */
41 /** Socket has an error */
46 * Error types which a socket may exhibit
48 enum BufferedSocketError
52 /** Socket was closed by peer */
54 /** Socket connect timed out */
56 /** Socket could not be created */
58 /** Socket could not connect (refused) */
60 /** Socket could not bind to local port/ip */
62 /** Socket could not write data */
64 /** No more file descriptors left to create socket! */
66 /** Some other error */
70 /* Required forward declarations */
73 /** Used to time out socket connections
75 class CoreExport SocketTimeout : public Timer
78 /** BufferedSocket the class is attached to
82 /** File descriptor of class this is attached to
87 /** Create a socket timeout class
88 * @param fd File descriptor of BufferedSocket
89 * @param thesock BufferedSocket to attach to
90 * @param secs_from_now Seconds from now to time out
91 * @param now The current time
93 SocketTimeout(int fd, BufferedSocket* thesock, long secs_from_now) : Timer(secs_from_now), sock(thesock), sfd(fd) { }
97 virtual bool Tick(time_t now);
101 * StreamSocket is a class that wraps a TCP socket and handles send
102 * and receive queues, including passing them to IO hooks
104 class CoreExport StreamSocket : public EventHandler
106 /** The IOHook that handles raw I/O for this socket, or NULL */
109 /** Private send queue. Note that individual strings may be shared
111 std::deque<std::string> sendq;
112 /** Length, in bytes, of the sendq */
114 /** Error - if nonempty, the socket is dead, and this is the reason. */
117 /** Check if the socket has an error set, if yes, call OnError
118 * @param err Error to pass to OnError()
120 void CheckError(BufferedSocketError err);
122 /** Read data from the socket into the recvq, if successful call OnDataReady()
129 StreamSocket() : iohook(NULL), sendq_len(0) {}
130 IOHook* GetIOHook() const;
131 void AddIOHook(IOHook* hook);
134 /** Flush the send queue
138 /** Called by the socket engine on a read event
140 void OnEventHandlerRead() CXX11_OVERRIDE;
142 /** Called by the socket engine on a write event
144 void OnEventHandlerWrite() CXX11_OVERRIDE;
146 /** Called by the socket engine on error
147 * @param errcode Error
149 void OnEventHandlerError(int errcode) CXX11_OVERRIDE;
151 /** Sets the error message for this socket. Once set, the socket is dead. */
152 void SetError(const std::string& err) { if (error.empty()) error = err; }
154 /** Gets the error message for this socket. */
155 const std::string& getError() const { return error; }
157 /** Called when new data is present in recvq */
158 virtual void OnDataReady() = 0;
159 /** Called when the socket gets an error from socket engine or IO hook */
160 virtual void OnError(BufferedSocketError e) = 0;
162 /** Send the given data out the socket, either now or when writes unblock
164 void WriteData(const std::string& data);
165 /** Convenience function: read a line from the socket
166 * @param line The line read
167 * @param delim The line delimiter
168 * @return true if a line was read
170 bool GetNextLine(std::string& line, char delim = '\n');
171 /** Useful for implementing sendq exceeded */
172 inline size_t getSendQSize() const { return sendq_len; }
175 * Close the socket, remove from socket engine, etc
177 virtual void Close();
178 /** This ensures that close is called prior to destructor */
179 virtual CullResult cull();
182 * BufferedSocket is an extendable socket class which modules
183 * can use for TCP socket support. It is fully integrated
184 * into InspIRCds socket loop and attaches its sockets to
185 * the core's instance of the SocketEngine class, meaning
186 * that all use is fully asynchronous.
188 * To use BufferedSocket, you must inherit a class from it.
190 class CoreExport BufferedSocket : public StreamSocket
193 /** Timeout object or NULL
195 SocketTimeout* Timeout;
198 * The state for this socket, either
199 * listening, connecting, connected
202 BufferedSocketState state;
206 * This constructor is used to associate
207 * an existing connecting with an BufferedSocket
208 * class. The given file descriptor must be
209 * valid, and when initialized, the BufferedSocket
210 * will be placed in CONNECTED state.
212 BufferedSocket(int newfd);
214 /** Begin connection to the given address
215 * This will create a socket, register with socket engine, and start the asynchronous
216 * connection process. If an error is detected at this point (such as out of file descriptors),
217 * OnError will be called; otherwise, the state will become CONNECTING.
218 * @param ipaddr Address to connect to
219 * @param aport Port to connect on
220 * @param maxtime Time to wait for connection
221 * @param connectbindip Address to bind to (if NULL, no bind will be done)
223 void DoConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip);
225 /** This method is called when an outbound connection on your socket is
228 virtual void OnConnected();
230 /** When there is data waiting to be read on a socket, the OnDataReady()
233 virtual void OnDataReady() = 0;
236 * When an outbound connection fails, and the attempt times out, you
237 * will receive this event. The method will trigger once maxtime
238 * seconds are reached (as given in the constructor) just before the
239 * socket's descriptor is closed. A failed DNS lookup may cause this
240 * event if the DNS server is not responding, as well as a failed
241 * connect() call, because DNS lookups are nonblocking as implemented by
244 virtual void OnTimeout();
246 virtual ~BufferedSocket();
248 void OnEventHandlerWrite() CXX11_OVERRIDE;
249 BufferedSocketError BeginConnect(const irc::sockets::sockaddrs& dest, const irc::sockets::sockaddrs& bind, unsigned long timeout);
250 BufferedSocketError BeginConnect(const std::string &ipaddr, int aport, unsigned long maxtime, const std::string &connectbindip);
253 inline IOHook* StreamSocket::GetIOHook() const { return iohook; }
254 inline void StreamSocket::AddIOHook(IOHook* hook) { iohook = hook; }
255 inline void StreamSocket::DelIOHook() { iohook = NULL; }