Skip to content

Commit e4b22a0

Browse files
Fixed issue that caused asio code to throw exceptions, when e.g. unable to join a multicast group.
1 parent d6ad890 commit e4b22a0

File tree

2 files changed

+70
-9
lines changed

2 files changed

+70
-9
lines changed

tests/udpcap_test/src/udpcap_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ TEST(udpcap, DelayedPackageReceiveMultiplePackages)
422422
}
423423

424424
// Wait some time for the receive thread to finish
425-
received_messages.wait_for([num_packages_to_send](int value) { return value >= num_packages_to_send; }, receive_delay * num_packages_to_send + std::chrono::milliseconds(1000));
425+
received_messages.wait_for([num_packages_to_send](int value) { return value >= num_packages_to_send; }, receive_delay * num_packages_to_send + std::chrono::milliseconds(2000));
426426

427427
// Check if the received message counter is equal to the sent messages
428428
ASSERT_EQ(received_messages.get(), num_packages_to_send);

udpcap/src/udpcap_socket_private.cpp

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -842,25 +842,71 @@ namespace Udpcap
842842

843843
// create socket
844844
const asio::ip::udp::endpoint listen_endpoint(asio::ip::make_address("0.0.0.0"), kickstart_port);
845-
kickstart_socket.open(listen_endpoint.protocol());
845+
{
846+
asio::error_code ec;
847+
kickstart_socket.open(listen_endpoint.protocol(), ec);
848+
if (ec)
849+
{
850+
LOG_DEBUG("Failed to open kickstart socket: " + ec.message());
851+
return;
852+
}
853+
}
846854

847855
// set socket reuse
848-
kickstart_socket.set_option(asio::ip::udp::socket::reuse_address(true));
856+
{
857+
asio::error_code ec;
858+
kickstart_socket.set_option(asio::ip::udp::socket::reuse_address(true), ec);
859+
if (ec)
860+
{
861+
LOG_DEBUG("Failed to set socket reuse of kickstart socket: " + ec.message());
862+
return;
863+
}
864+
}
849865

850866
// bind socket
851-
kickstart_socket.bind(listen_endpoint);
867+
{
868+
asio::error_code ec;
869+
kickstart_socket.bind(listen_endpoint, ec);
870+
if (ec)
871+
{
872+
LOG_DEBUG("Failed to bind kickstart socket: " + ec.message());
873+
return;
874+
}
875+
}
852876

853877
// multicast loopback
854-
kickstart_socket.set_option(asio::ip::multicast::enable_loopback(true));
878+
{
879+
asio::error_code ec;
880+
kickstart_socket.set_option(asio::ip::multicast::enable_loopback(true), ec);
881+
if (ec)
882+
{
883+
LOG_DEBUG("Failed to set multicast loopback of kickstart socket: " + ec.message());
884+
return;
885+
}
886+
}
855887

856888
// multicast ttl
857-
kickstart_socket.set_option(asio::ip::multicast::hops(0));
889+
{
890+
asio::error_code ec;
891+
kickstart_socket.set_option(asio::ip::multicast::hops(0), ec);
892+
if (ec)
893+
{
894+
LOG_DEBUG("Failed to set multicast ttl of kickstart socket: " + ec.message());
895+
return;
896+
}
897+
}
858898

859899
// Join all multicast groups
860900
for (const auto& multicast_group : multicast_groups_)
861901
{
862902
const asio::ip::address asio_mc_group = asio::ip::make_address(multicast_group.toString());
863-
kickstart_socket.set_option(asio::ip::multicast::join_group(asio_mc_group));
903+
904+
asio::error_code ec;
905+
kickstart_socket.set_option(asio::ip::multicast::join_group(asio_mc_group), ec);
906+
if (ec)
907+
{
908+
LOG_DEBUG("Failed to join multicast group " + multicast_group.toString() + " with kickstart socket: " + ec.message());
909+
}
864910
}
865911

866912
// Send data to all multicast groups
@@ -869,11 +915,26 @@ namespace Udpcap
869915
LOG_DEBUG(std::string("Sending loopback kickstart packet to ") + multicast_group.toString() + ":" + std::to_string(kickstart_port));
870916
const asio::ip::address asio_mc_group = asio::ip::make_address(multicast_group.toString());
871917
const asio::ip::udp::endpoint send_endpoint(asio_mc_group, kickstart_port);
872-
kickstart_socket.send_to(asio::buffer(static_cast<void*>(nullptr), 0), send_endpoint, 0);
918+
919+
{
920+
asio::error_code ec;
921+
kickstart_socket.send_to(asio::buffer(static_cast<void*>(nullptr), 0), send_endpoint, 0, ec);
922+
if (ec)
923+
{
924+
LOG_DEBUG("Failed to send kickstart packet to " + multicast_group.toString() + ":" + std::to_string(kickstart_port) + ": " + ec.message());
925+
}
926+
}
873927
}
874928

875929
// Close the socket
876-
kickstart_socket.close();
930+
{
931+
asio::error_code ec;
932+
kickstart_socket.close();
933+
if (ec)
934+
{
935+
LOG_DEBUG("Failed to close kickstart socket: " + ec.message());
936+
}
937+
}
877938
}
878939

879940
void UdpcapSocketPrivate::PacketHandlerRawPtr(unsigned char* param, const struct pcap_pkthdr* header, const unsigned char* pkt_data)

0 commit comments

Comments
 (0)