From 723079310f757e48b496e314bcd94166467c8aba Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Wed, 26 Jul 2017 13:09:21 -0700 Subject: [PATCH] Disable IP_MULTICAST_ALL when setting up UDP multicast This avoids interference caused when two sockets are bound to the same port in different multicast groups. Fixes #241 Change-Id: Iba1b3300e3850f1f268886b7a49f5a3315d95b80 --- packager/file/udp_file.cc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packager/file/udp_file.cc b/packager/file/udp_file.cc index ef1aa16157..6bb9445cd6 100644 --- a/packager/file/udp_file.cc +++ b/packager/file/udp_file.cc @@ -7,16 +7,26 @@ #include "packager/file/udp_file.h" #if defined(OS_WIN) + #include #include #define close closesocket + #else + #include #include #include #include #include #define INVALID_SOCKET -1 + +// IP_MULTICAST_ALL has been supported since kernel version 2.6.31 but we may be +// building on a machine that is older than that. +#ifndef IP_MULTICAST_ALL +#define IP_MULTICAST_ALL 49 +#endif + #endif // defined(OS_WIN) #include @@ -216,6 +226,19 @@ bool UdpFile::Open() { LOG(ERROR) << "Failed to join multicast group."; return false; } + +#if defined(__linux__) + // Disable IP_MULTICAST_ALL to avoid interference caused when two sockets + // are bound to the same port but joined to different multicast groups. + const int optval_zero = 0; + if (setsockopt(new_socket.get(), IPPROTO_IP, IP_MULTICAST_ALL, + reinterpret_cast(&optval_zero), + sizeof(optval_zero)) < 0 && + errno != ENOPROTOOPT) { + LOG(ERROR) << "Failed to disable IP_MULTICAST_ALL option."; + return false; + } +#endif // #if defined(__linux__) } // Set timeout if needed.