From 86b6aeac997bc8c0560c05d51c1028fe3ff614a7 Mon Sep 17 00:00:00 2001 From: Thomas Debrunner Date: Thu, 15 Feb 2024 13:59:48 +0000 Subject: [PATCH] udp-server: add possibility to join multicast groups --- include/mav/UDPServer.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/mav/UDPServer.h b/include/mav/UDPServer.h index 70eaf17..9738bf4 100644 --- a/include/mav/UDPServer.h +++ b/include/mav/UDPServer.h @@ -76,6 +76,16 @@ namespace mav { } } + void joinMulticastGroup(const std::string& multicast_group, const std::string& local_address="") const { + struct ip_mreq mreq{}; + mreq.imr_multiaddr.s_addr = inet_addr(multicast_group.c_str()); + mreq.imr_interface.s_addr = local_address.empty() ? INADDR_ANY : inet_addr(local_address.c_str()); + if (setsockopt(_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { + ::close(_socket); + throw NetworkError("Could not join multicast group", errno); + } + } + void stop() const { _should_terminate.store(true); if (_socket >= 0) {