Skip to content

Commit

Permalink
fix(udp): enforce that at most one multicast group is joined
Browse files Browse the repository at this point in the history
Signed-off-by: Max SCHMELLER <[email protected]>
  • Loading branch information
mojomex committed Dec 23, 2024
1 parent d962945 commit 95fb8ea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion nebula_hw_interfaces/test/common/test_udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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()));
Expand Down

0 comments on commit 95fb8ea

Please sign in to comment.