11#include " udp_server.h"
2+ #include " api_config.h"
23#include " socket_utils.h"
34#include " stream_info_impl.h"
45#include " util/strfuns.hpp"
@@ -34,11 +35,10 @@ udp_server::udp_server(stream_info_impl_p info, asio::io_context &io, udp protoc
3435 (void *)this );
3536}
3637
37- udp_server::udp_server (stream_info_impl_p info, asio::io_context &io, const std::string & address,
38+ udp_server::udp_server (stream_info_impl_p info, asio::io_context &io, ip:: address addr ,
3839 uint16_t port, int ttl, const std::string &listen_address)
3940 : info_(std::move(info)), io_(io), socket_(std::make_shared<udp_socket>(io)),
4041 time_services_enabled_(false ) {
41- ip::address addr = ip::make_address (address);
4242 bool is_broadcast = addr == ip::address_v4::broadcast ();
4343
4444 // set up the endpoint where we listen (note: this is not yet the multicast address)
@@ -65,16 +65,28 @@ udp_server::udp_server(stream_info_impl_p info, asio::io_context &io, const std:
6565 // bind to the listen endpoint
6666 socket_->bind (listen_endpoint);
6767
68- // join the multicast group, if any
68+ // join the multicast groups
6969 if (addr.is_multicast () && !is_broadcast) {
70- if (addr.is_v4 ())
71- socket_->set_option (
72- ip::multicast::join_group (addr.to_v4 (), listen_endpoint.address ().to_v4 ()));
73- else
74- socket_->set_option (ip::multicast::join_group (addr));
70+ bool joined_anywhere = false ;
71+ asio::error_code err;
72+ for (auto &if_ : api_config::get_instance ()->multicast_interfaces ) {
73+ DLOG_F (
74+ INFO, " Joining %s to %s" , if_.addr .to_string ().c_str (), addr.to_string ().c_str ());
75+ if (addr.is_v4 () && if_.addr .is_v4 ())
76+ socket_->set_option (ip::multicast::join_group (addr.to_v4 (), if_.addr .to_v4 ()), err);
77+ else if (addr.is_v6 () && if_.addr .is_v6 ())
78+ socket_->set_option (
79+ ip::multicast::join_group (addr.to_v6 (), if_.addr .to_v6 ().scope_id ()), err);
80+ if (err)
81+ LOG_F (WARNING, " Could not bind multicast responder for %s to interface %s (%s)" ,
82+ addr.to_string ().c_str (), if_.addr .to_string ().c_str (), err.message ().c_str ());
83+ else
84+ joined_anywhere = true ;
85+ }
86+ if (!joined_anywhere) throw std::runtime_error (" Could not join any multicast group" );
7587 }
7688 LOG_F (2 , " %s: Started multicast udp server at %s port %d (addr %p)" ,
77- this ->info_ ->name ().c_str (), address .c_str (), port, (void *)this );
89+ this ->info_ ->name ().c_str (), addr. to_string () .c_str (), port, (void *)this );
7890}
7991
8092// === externally issued asynchronous commands ===
0 commit comments