changeset 233:f8094e852dd5

Irccd: unify TransportServerIp(v6)
author David Demelier <markand@malikania.fr>
date Thu, 11 Aug 2016 12:05:26 +0200
parents 5563a2415e34
children 3c631fb06ccf
files lib/irccd/config.cpp lib/irccd/transport-server.cpp lib/irccd/transport-server.hpp
diffstat 3 files changed, 39 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/config.cpp	Wed Aug 10 10:50:41 2016 +0200
+++ b/lib/irccd/config.cpp	Thu Aug 11 12:05:26 2016 +0200
@@ -158,29 +158,23 @@
         address = it->value();
 
     // Domain
-    bool ipv6 = true;
-    bool ipv4 = true;
+    std::uint8_t mode = TransportServerIp::v4;
 
     if ((it = sc.find("domain")) != sc.end()) {
-        ipv6 = false;
-        ipv4 = false;
+        mode = 0;
 
         for (const auto &v : *it) {
             if (v == "ipv4")
-                ipv4 = true;
+                mode |= TransportServerIp::v4;
             if (v == "ipv6")
-                ipv6 = true;
+                mode |= TransportServerIp::v6;
         }
     }
 
-    if (ipv6)
-        transport = std::make_shared<TransportServerIpv6>(move(address), port, !ipv4);
-    else if (ipv4)
-        transport = std::make_shared<TransportServerIp>(move(address), port);
-    else
+    if (mode == 0)
         throw std::invalid_argument("transport: domain must at least have ipv4 or ipv6");
 
-    return transport;
+    return std::make_shared<TransportServerIp>(address, port, mode);
 }
 
 std::shared_ptr<TransportServer> loadTransportUnix(const ini::Section &sc)
--- a/lib/irccd/transport-server.cpp	Wed Aug 10 10:50:41 2016 +0200
+++ b/lib/irccd/transport-server.cpp	Thu Aug 11 12:05:26 2016 +0200
@@ -34,39 +34,30 @@
  * ------------------------------------------------------------------
  */
 
-TransportServerIpv6::TransportServerIpv6(const std::string &address, std::uint16_t port, bool ipv6only)
-    : TransportServer(net::TcpSocket(AF_INET6, 0))
+TransportServerIp::TransportServerIp(const std::string &address,
+                                     std::uint16_t port,
+                                     std::uint8_t mode)
+    : TransportServer(net::TcpSocket((mode & v6) ? AF_INET6 : AF_INET, 0))
 {
+    assert((mode & v6) || (mode & v4));
+
     m_socket.set(net::option::SockReuseAddress(true));
 
-    // Disable or enable IPv4 when using IPv6.
-    if (ipv6only)
-        m_socket.set(net::option::Ipv6Only(true));
-
-    if (address == "*")
-        m_socket.bind(net::ipv6::any(port));
-    else
-        m_socket.bind(net::ipv6::pton(address, port));
-
-    m_socket.listen();
-
-    log::info() << "transport: listening on " << address << ", port " << port << std::endl;
-}
+    if (mode & v6) {
+        if (address == "*")
+            m_socket.bind(net::ipv6::any(port));
+        else
+            m_socket.bind(net::ipv6::pton(address, port));
 
-/*
- * TransportServerIp
- * ------------------------------------------------------------------
- */
-
-TransportServerIp::TransportServerIp(const std::string &address, std::uint16_t port)
-    : TransportServer(net::TcpSocket(AF_INET, 0))
-{
-    m_socket.set(net::option::SockReuseAddress(true));
-
-    if (address == "*")
-        m_socket.bind(net::ipv4::any(port));
-    else
-        m_socket.bind(net::ipv4::pton(address, port));
+        // Disable or enable IPv4 when using IPv6.
+        if (!(mode & v4))
+            m_socket.set(net::option::Ipv6Only(true));
+    } else {
+        if (address == "*")
+            m_socket.bind(net::ipv4::any(port));
+        else
+            m_socket.bind(net::ipv4::pton(address, port));
+    }
 
     m_socket.listen();
 
--- a/lib/irccd/transport-server.hpp	Wed Aug 10 10:50:41 2016 +0200
+++ b/lib/irccd/transport-server.hpp	Thu Aug 11 12:05:26 2016 +0200
@@ -102,27 +102,23 @@
 class TransportServerIp : public TransportServer {
 public:
     /**
+     * \brief Domain to use.
+     */
+    enum Mode {
+        v4 = (1 << 0),      //!< IPv6
+        v6 = (1 << 1)       //!< IPv4
+    };
+
+    /**
      * Constructor.
-     *
+     * \pre mode > 0
      * \param address the address (* for any)
      * \param port the port number
+     * \param mode the domains to use (can be OR'ed)
      */
-    IRCCD_EXPORT TransportServerIp(const std::string &address, std::uint16_t port);
-};
-
-/**
- * \brief Create IPv6 transport.
- */
-class TransportServerIpv6 : public TransportServer {
-public:
-    /**
-     * Constructor.
-     *
-     * \param address the address (* for any)
-     * \param port the port number
-     * \param ipv6only set to true to disable ipv4 completely
-     */
-    IRCCD_EXPORT TransportServerIpv6(const std::string &address, std::uint16_t port, bool ipv6only = true);
+    IRCCD_EXPORT TransportServerIp(const std::string &address,
+                                   std::uint16_t port,
+                                   std::uint8_t mode = v4);
 };
 
 #if !defined(IRCCD_SYSTEM_WINDOWS)