From 95fb8ea9ff7e967a25c295d9de8ceeea54f1a45a Mon Sep 17 00:00:00 2001 From: Max SCHMELLER Date: Mon, 23 Dec 2024 14:19:44 +0900 Subject: [PATCH] fix(udp): enforce that at most one multicast group is joined Signed-off-by: Max SCHMELLER --- .../nebula_hw_interfaces_common/connections/udp.hpp | 2 ++ nebula_hw_interfaces/test/common/test_udp.cpp | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/nebula_hw_interfaces/include/nebula_hw_interfaces/nebula_hw_interfaces_common/connections/udp.hpp b/nebula_hw_interfaces/include/nebula_hw_interfaces/nebula_hw_interfaces_common/connections/udp.hpp index dd9f34618..e9296d19d 100644 --- a/nebula_hw_interfaces/include/nebula_hw_interfaces/nebula_hw_interfaces_common/connections/udp.hpp +++ b/nebula_hw_interfaces/include/nebula_hw_interfaces/nebula_hw_interfaces_common/connections/udp.hpp @@ -214,6 +214,8 @@ class UdpSocket */ Builder && join_multicast_group(const std::string & group_ip) { + if (config_.multicast_ip) + throw UsageError("Only one multicast group can be joined by this socket"); ip_mreq mreq{parse_ip_or_throw(group_ip), config_.host.ip}; sock_fd_.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq).value_or_throw(); diff --git a/nebula_hw_interfaces/test/common/test_udp.cpp b/nebula_hw_interfaces/test/common/test_udp.cpp index 415bc8017..ea0aa29c3 100644 --- a/nebula_hw_interfaces/test/common/test_udp.cpp +++ b/nebula_hw_interfaces/test/common/test_udp.cpp @@ -27,6 +27,7 @@ static const char localhost_ip[] = "127.0.0.1"; static const char broadcast_ip[] = "255.255.255.255"; static const char any_ip[] = "0.0.0.0"; static const char multicast_group[] = "230.1.2.3"; +static const char multicast_group2[] = "230.4.5.6"; static const char sender_ip[] = "192.168.201"; static const uint16_t sender_port = 7373; @@ -94,7 +95,6 @@ TEST(test_udp, test_correct_usage_is_enforced) ASSERT_NO_THROW(UdpSocket::Builder(localhost_ip, host_port) .set_polling_interval(20) .set_socket_buffer_size(3000) - .join_multicast_group(multicast_group) .set_mtu(1600) .limit_to_sender(sender_ip, sender_port) .set_polling_interval(20) @@ -103,6 +103,13 @@ TEST(test_udp, test_correct_usage_is_enforced) .limit_to_sender(sender_ip, sender_port) .bind()); + // Only one multicast group can be joined + ASSERT_THROW( + UdpSocket::Builder(localhost_ip, host_port) + .join_multicast_group(multicast_group) + .join_multicast_group(multicast_group2), + UsageError); + // Pre-existing subscriptions shall be gracefully unsubscribed when a new subscription is created ASSERT_NO_THROW( UdpSocket::Builder(localhost_ip, host_port).bind().subscribe(empty_cb()).subscribe(empty_cb()));