Skip to content

Commit

Permalink
implemented multicast in npcap
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold committed Apr 19, 2024
1 parent f09df3c commit 76a1e33
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
3 changes: 2 additions & 1 deletion samples/ecaludp_perftool/src/receiver_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "receiver_async.h"

#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -62,7 +63,7 @@ void ReceiverAsync::start()
catch (const std::exception& e)
{
std::cerr << "Error creating socket: " << e.what() << '\n';
exit(1);
std::exit(1);
}

auto endpoint = asio::ip::udp::endpoint(asio::ip::make_address(parameters_.ip), parameters_.port);
Expand Down
6 changes: 4 additions & 2 deletions samples/ecaludp_perftool/src/receiver_npcap_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@

#include "receiver_npcap_async.h"

#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <mutex>
#include <thread>

#include <asio.hpp>

#include "ecaludp/socket.h"
#include "receiver.h"
#include "receiver_parameters.h"
#include "socket_builder_npcap.h"

ReceiverNpcapAsync::ReceiverNpcapAsync(const ReceiverParameters& parameters)
Expand All @@ -50,7 +52,7 @@ void ReceiverNpcapAsync::start()
catch (const std::exception& e)
{
std::cerr << "Error creating socket: " << e.what()<< '\n';
exit(1);
std::exit(1);
}

receive_message();
Expand Down
5 changes: 4 additions & 1 deletion samples/ecaludp_perftool/src/receiver_npcap_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "receiver_npcap_sync.h"

#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
#include <mutex>
Expand All @@ -25,6 +27,7 @@

#include "ecaludp/socket_npcap.h"
#include "receiver.h"
#include "receiver_parameters.h"
#include "socket_builder_npcap.h"

ReceiverNpcapSync::ReceiverNpcapSync(const ReceiverParameters& parameters)
Expand Down Expand Up @@ -56,7 +59,7 @@ void ReceiverNpcapSync::receive_loop()
catch (const std::exception& e)
{
std::cerr << "Error creating socket: " << e.what()<< '\n';
exit(1);
std::exit(1);
}

asio::ip::udp::endpoint destination(asio::ip::address::from_string(parameters_.ip), parameters_.port);
Expand Down
3 changes: 2 additions & 1 deletion samples/ecaludp_perftool/src/receiver_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "receiver_sync.h"

#include <cstdlib>
#include <exception>
#include <iostream>
#include <memory>
Expand Down Expand Up @@ -60,7 +61,7 @@ void ReceiverSync::receive_loop()
catch (const std::exception& e)
{
std::cerr << "Error creating socket: " << e.what() << '\n';
exit(1);
std::exit(1);
}

asio::ip::udp::endpoint destination;
Expand Down
31 changes: 30 additions & 1 deletion samples/ecaludp_perftool/src/socket_builder_npcap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ namespace SocketBuilderNpcap
}
}

// only v4 is supported right now
if (!ip_address.is_v4())
{
throw std::runtime_error("Only IPv4 is supported");
}

// Set receive buffer size
if (parameters.buffer_size > 0)
{
Expand All @@ -51,9 +57,32 @@ namespace SocketBuilderNpcap
}
}

const asio::ip::udp::endpoint destination(ip_address, parameters.port);

if (ip_address.is_multicast())
{
socket->set_multicast_loopback_enabled(true);

// "Bind" multicast address
{
const asio::ip::udp::endpoint bind_endpoint = asio::ip::udp::endpoint(asio::ip::address_v4(), parameters.port);
const bool success = socket->bind(bind_endpoint);
if (!success)
{
throw std::runtime_error("Failed to bind socket");
}
}

{
const bool success = socket->join_multicast_group(ip_address.to_v4());
if (!success)
{
throw std::runtime_error("Failed to join multicast group");
}
}
}
else
{
const asio::ip::udp::endpoint destination(ip_address, parameters.port);
const bool success = socket->bind(destination);
if (!success)
{
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/udpcap

0 comments on commit 76a1e33

Please sign in to comment.