+ /** Convert an address-port pair into a binary sockaddr
+ * @param addr The IP address, IPv4 or IPv6
+ * @param port The port, 0 for unspecified
+ * @param sa The structure to place the result in. Will be zeroed prior to conversion
+ * @return true if the conversion was successful, false if not.
+ */
+ CoreExport bool aptosa(const std::string& addr, int port, irc::sockets::sockaddrs* sa);
+ /** Convert a binary sockaddr to an address-port pair
+ * @param sa The structure to convert
+ * @param addr the IP address
+ * @param port the port
+ * @return true if the conversion was successful, false if unknown address family
+ */
+ CoreExport bool satoap(const irc::sockets::sockaddrs* sa, std::string& addr, int &port);
+ /** Convert a binary sockaddr to a user-readable string.
+ * This means IPv6 addresses are written as [::1]:6667, and *:6668 is used for 0.0.0.0:6668
+ * @param sa The structure to convert
+ * @return The string; "<unknown>" if not a valid address
+ */
+ CoreExport std::string satouser(const irc::sockets::sockaddrs* sa);
+ }
+}
+
+/** This class handles incoming connections on client ports.
+ * It will create a new User for every valid connection
+ * and assign it a file descriptor.
+ */
+class CoreExport ListenSocketBase : public EventHandler
+{
+ protected:
+ /** Raw address socket is bound to */
+ std::string bind_addr;
+ /** Human-readable address/port socket is bound to */
+ std::string bind_desc;
+
+ /** The client address if the most recently connected client.
+ * Should only be used when accepting a new client.
+ */
+ static irc::sockets::sockaddrs client;
+ /** The server address used by the most recently connected client.
+ * This may differ from the bind address by having a nonzero address,
+ * if the port is wildcard bound, or being IPv4 on a 6to4 IPv6 port.
+ * The address family will always match that of "client"
+ */
+ static irc::sockets::sockaddrs server;
+
+ public:
+ /** Socket type (client/server) */
+ const std::string type;
+ /** Socket hook (plain/gnutls/openssl/zip) */
+ const std::string hook;
+ /** Port socket is bound to */
+ const int bind_port;
+ /** Create a new listening socket
+ */
+ ListenSocketBase(int port, const std::string &addr, const std::string &type, const std::string &hook);
+ /** Handle an I/O event
+ */
+ void HandleEvent(EventType et, int errornum = 0);
+ /** Close the socket
+ */
+ ~ListenSocketBase();
+
+ /** Get IP address socket is bound to
+ */
+ const std::string &GetIP() { return bind_addr; }
+
+ const std::string &GetBindDesc() { return bind_desc; }
+
+ /** Handles sockets internals crap of a connection, convenience wrapper really
+ */
+ void AcceptInternal();
+
+ /** Called when a new connection has successfully been accepted on this listener.
+ * @param fd The file descriptor of the new connection
+ */
+ virtual void OnAcceptReady(int fd) = 0;
+};
+
+class CoreExport ClientListenSocket : public ListenSocketBase
+{
+ virtual void OnAcceptReady(int fd);
+ public:
+ ClientListenSocket(int port, const std::string &addr, const std::string &Type, const std::string &Hook)
+ : ListenSocketBase(port, addr, Type, Hook) { }
+};