11#include " udp_server.h"
2+ #include " api_config.h"
23#include " socket_utils.h"
34#include " stream_info_impl.h"
45#include < boost/asio/ip/address.hpp>
@@ -29,11 +30,10 @@ udp_server::udp_server(const stream_info_impl_p &info, asio::io_context &io, udp
2930 (void *)this );
3031}
3132
32- udp_server::udp_server (const stream_info_impl_p &info, asio::io_context &io,
33- const std::string &address, uint16_t port, int ttl, const std::string &listen_address)
33+ udp_server::udp_server (const stream_info_impl_p &info, asio::io_context &io, ip::address addr,
34+ uint16_t port, int ttl, const std::string &listen_address)
3435 : info_(info), io_(io), socket_(std::make_shared<udp::socket>(io)),
3536 time_services_enabled_(false ) {
36- ip::address addr = ip::make_address (address);
3737 bool is_broadcast = addr == ip::address_v4::broadcast ();
3838
3939 // set up the endpoint where we listen (note: this is not yet the multicast address)
@@ -60,16 +60,28 @@ udp_server::udp_server(const stream_info_impl_p &info, asio::io_context &io,
6060 // bind to the listen endpoint
6161 socket_->bind (listen_endpoint);
6262
63- // join the multicast group, if any
63+ // join the multicast groups
6464 if (addr.is_multicast () && !is_broadcast) {
65- if (addr.is_v4 ())
66- socket_->set_option (
67- ip::multicast::join_group (addr.to_v4 (), listen_endpoint.address ().to_v4 ()));
68- else
69- socket_->set_option (ip::multicast::join_group (addr));
65+ bool joined_anywhere = false ;
66+ lslboost::system::error_code err;
67+ for (auto &if_ : api_config::get_instance ()->multicast_interfaces ) {
68+ DLOG_F (INFO, " Joining %s to %s" , if_.addr .to_string ().c_str (), addr.to_string ().c_str ());
69+ if (addr.is_v4 () && if_.addr .is_v4 ())
70+ socket_->set_option (
71+ ip::multicast::join_group (addr.to_v4 (), if_.addr .to_v4 ()), err);
72+ else if (addr.is_v6 () && if_.addr .is_v6 ())
73+ socket_->set_option (
74+ ip::multicast::join_group (addr.to_v6 (), if_.addr .to_v6 ().scope_id ()), err);
75+ if (err)
76+ LOG_F (WARNING, " Could not bind multicast responder for %s to interface %s (%s)" ,
77+ addr.to_string ().c_str (), if_.addr .to_string ().c_str (), err.message ().c_str ());
78+ else
79+ joined_anywhere = true ;
80+ }
81+ if (!joined_anywhere) throw std::runtime_error (" Could not join any multicast group" );
7082 }
7183 LOG_F (2 , " %s: Started multicast udp server at %s port %d (addr %p)" ,
72- this ->info_ ->name ().c_str (), address .c_str (), port, (void *)this );
84+ this ->info_ ->name ().c_str (), addr. to_string () .c_str (), port, (void *)this );
7385}
7486
7587// === externally issued asynchronous commands ===
0 commit comments