From a50815efeced443121a2c3ae9c872d0b33b4e7e6 Mon Sep 17 00:00:00 2001
From: Sebastian Woelke <Sebastian.Woelke@posteo.de>
Date: Fri, 7 Nov 2014 22:43:41 +0100
Subject: [PATCH] Added IP_MULTICAST_ALL option for tester.

---
 mcproxy/include/utils/mc_socket.hpp |  8 +++-
 mcproxy/src/tester/tester.cpp       |  9 ++++-
 mcproxy/src/utils/mc_socket.cpp     | 58 +++++++++++++++++++++++------
 3 files changed, 60 insertions(+), 15 deletions(-)

diff --git a/mcproxy/include/utils/mc_socket.hpp b/mcproxy/include/utils/mc_socket.hpp
index 6f253eb..dee287d 100644
--- a/mcproxy/include/utils/mc_socket.hpp
+++ b/mcproxy/include/utils/mc_socket.hpp
@@ -151,7 +151,13 @@ public:
      * @brief allow the reuse of binded ports 
      * @return Return true on success.
      */
-    bool set_reuse_port() const;
+    bool set_reuse_port(bool enable) const;
+
+    /**
+     * @brief 
+     * @return Return true on success.
+     */
+    bool set_multicast_all(bool enable) const;
 
     /**
      * @brief Enable or disable multicast loopback.
diff --git a/mcproxy/src/tester/tester.cpp b/mcproxy/src/tester/tester.cpp
index 2d95a4c..17f0933 100644
--- a/mcproxy/src/tester/tester.cpp
+++ b/mcproxy/src/tester/tester.cpp
@@ -500,12 +500,17 @@ void tester::receive_data(const std::unique_ptr<const mc_socket>& ms, int port,
         }
     }
 
-    if (!ms->set_reuse_port()) {
+    if (!ms->set_reuse_port(true)) {
         std::cout << "failed to set socket option reuse port" << std::endl;
         exit(0);
     }
 
-    if (!ms->bind_udp_socket(gaddr, port)) {
+    if (!ms->set_multicast_all(false)) {
+        std::cout << "failed to set socket option multicast all" << std::endl;
+        exit(0);
+    }
+
+    if (!ms->bind_udp_socket(addr_storage(gaddr.get_addr_family()), port)) { //bind to any address
         std::cout << "failed to bind port " << port << " and address "<< gaddr << " to socket" << std::endl;
         exit(0);
     }
diff --git a/mcproxy/src/utils/mc_socket.cpp b/mcproxy/src/utils/mc_socket.cpp
index c885420..d2af9e6 100644
--- a/mcproxy/src/utils/mc_socket.cpp
+++ b/mcproxy/src/utils/mc_socket.cpp
@@ -196,7 +196,7 @@ bool mc_socket::bind_udp_socket(const addr_storage& addr, in_port_t port) const
     }
 }
 
-bool mc_socket::set_reuse_port() const
+bool mc_socket::set_multicast_all(bool enable) const
 {
     HC_LOG_TRACE("");
 
@@ -206,7 +206,45 @@ bool mc_socket::set_reuse_port() const
     }
 
     int rc;
-    int option = 1;
+    int mall_arg;
+    int level;
+
+    int option = enable;
+
+    if (m_addrFamily == AF_INET) {
+        level = IPPROTO_IP;
+        mall_arg = IP_MULTICAST_ALL;
+    } else if (m_addrFamily == AF_INET6) {
+        level = IPPROTO_IPV6;
+        //mall_arg = IP6_MULTICAST_ALL;
+        HC_LOG_ERROR("option for IPv6 not available");
+        return true; 
+    } else {
+        HC_LOG_ERROR("wrong address family");
+        return false;
+    }
+
+    rc = setsockopt(m_sock, level, mall_arg, &option, sizeof(option));
+
+    if (rc == -1) {
+        HC_LOG_ERROR("failed to set multicast_all(on/off)! Error: " << strerror(errno) << " errno: " << errno);
+        return false;
+    } else {
+        return true;
+    }
+}
+
+bool mc_socket::set_reuse_port(bool enable) const
+{
+    HC_LOG_TRACE("");
+
+    if (!is_udp_valid()) {
+        HC_LOG_ERROR("udp_socket invalid");
+        return false;
+    }
+
+    int rc;
+    int option = enable;
 
     rc = setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));
 
@@ -232,12 +270,7 @@ bool mc_socket::set_loop_back(bool enable) const
     int level;
 
     //u_char loop;
-    int loop;
-    if (enable == true) {
-        loop = 1;
-    } else {
-        loop = 0;
-    }
+    int loop = enable;
 
     if (m_addrFamily == AF_INET) {
         level = IPPROTO_IP;
@@ -253,7 +286,7 @@ bool mc_socket::set_loop_back(bool enable) const
     rc = setsockopt(m_sock, level, loopArg, &loop, sizeof(loop));
 
     if (rc == -1) {
-        HC_LOG_ERROR("failed to setLoopBack(on/off)! Error: " << strerror(errno) << " errno: " << errno);
+        HC_LOG_ERROR("failed to set_loop_back(on/off)! Error: " << strerror(errno) << " errno: " << errno);
         return false;
     } else {
         return true;
@@ -945,9 +978,10 @@ void mc_socket::test_all()
 }
 #endif /* TESTER */
 
-void mc_socket::close_socket() const{
+void mc_socket::close_socket() const
+{
     HC_LOG_TRACE("");
-    
+
     if (is_udp_valid() && m_own_socket) {
         close(m_sock);
     }
@@ -956,6 +990,6 @@ void mc_socket::close_socket() const{
 mc_socket::~mc_socket()
 {
     HC_LOG_TRACE("");
-    
+
     close_socket();
 }
-- 
GitLab