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.