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>
@@ -28,11 +29,10 @@ udp_server::udp_server(const stream_info_impl_p &info, asio::io_context &io, udp
2829 (void *)this );
2930}
3031
31- udp_server::udp_server (const stream_info_impl_p &info, asio::io_context &io,
32- const std::string &address, uint16_t port, int ttl, const std::string &listen_address)
32+ udp_server::udp_server (const stream_info_impl_p &info, asio::io_context &io, ip::address addr,
33+ uint16_t port, int ttl, const std::string &listen_address)
3334 : info_(info), io_(io), socket_(std::make_shared<udp::socket>(io)),
3435 time_services_enabled_(false ) {
35- ip::address addr = ip::make_address (address);
3636 bool is_broadcast = addr == ip::address_v4::broadcast ();
3737
3838 // set up the endpoint where we listen (note: this is not yet the multicast address)
@@ -59,16 +59,28 @@ udp_server::udp_server(const stream_info_impl_p &info, asio::io_context &io,
5959 // bind to the listen endpoint
6060 socket_->bind (listen_endpoint);
6161
62- // join the multicast group, if any
62+ // join the multicast groups
6363 if (addr.is_multicast () && !is_broadcast) {
64- if (addr.is_v4 ())
65- socket_->set_option (
66- ip::multicast::join_group (addr.to_v4 (), listen_endpoint.address ().to_v4 ()));
67- else
68- socket_->set_option (ip::multicast::join_group (addr));
64+ bool joined_anywhere = false ;
65+ lslboost::system::error_code err;
66+ for (auto &if_ : api_config::get_instance ()->multicast_interfaces ) {
67+ DLOG_F (INFO, " Joining %s to %s" , if_.addr .to_string ().c_str (), addr.to_string ().c_str ());
68+ if (addr.is_v4 () && if_.addr .is_v4 ())
69+ socket_->set_option (
70+ ip::multicast::join_group (addr.to_v4 (), if_.addr .to_v4 ()), err);
71+ else if (addr.is_v6 () && if_.addr .is_v6 ())
72+ socket_->set_option (
73+ ip::multicast::join_group (addr.to_v6 (), if_.addr .to_v6 ().scope_id ()), err);
74+ if (err)
75+ LOG_F (WARNING, " Could not bind multicast responder for %s to interface %s (%s)" ,
76+ addr.to_string ().c_str (), if_.addr .to_string ().c_str (), err.message ().c_str ());
77+ else
78+ joined_anywhere = true ;
79+ }
80+ if (!joined_anywhere) throw std::runtime_error (" Could not join any multicast group" );
6981 }
7082 LOG_F (2 , " %s: Started multicast udp server at %s port %d (addr %p)" ,
71- this ->info_ ->name ().c_str (), address .c_str (), port, (void *)this );
83+ this ->info_ ->name ().c_str (), addr. to_string () .c_str (), port, (void *)this );
7284}
7385
7486// === externally issued asynchronous commands ===
0 commit comments