7#ifndef WIN32_LEAN_AND_MEAN
8#define WIN32_LEAN_AND_MEAN
19#include <asio/error_code.hpp>
20#include <asio/io_context.hpp>
21#include <asio/steady_timer.hpp>
35#include <asio/windows/object_handle.hpp>
37#include <asio/posix/stream_descriptor.hpp>
81 void Send(
const std::vector<std::uint8_t>&
frame);
86 void SetTimeout(std::chrono::milliseconds newTimeout);
92 [[nodiscard]] std::vector<std::uint8_t>
Recv();
161 void HandlePacketReady(
const std::shared_ptr<ReceiveOperation>& operation,
const asio::error_code&
error);
167 void HandleTimeout(
const std::shared_ptr<ReceiveOperation>& operation,
const asio::error_code&
error);
172 void RearmPacketWait(
const std::shared_ptr<ReceiveOperation>& operation);
178 void FailOperation(
const std::shared_ptr<ReceiveOperation>& operation, std::exception_ptr
error);
189 void CompleteHandler(
const std::shared_ptr<ReceiveOperation>& operation);
219 std::chrono::milliseconds
timeout{std::chrono::milliseconds::zero()};
Asio-backed platform-independent raw Ethernet transport.
Private packet-capture backend for complete Ethernet frames.
void HandleTimeout(const std::shared_ptr< ReceiveOperation > &operation, const asio::error_code &error)
Handle application receive timeout.
void SetTimeout(std::chrono::milliseconds newTimeout)
Configure the receive timeout.
EthernetPacketBackend & operator=(EthernetPacketBackend &&)=delete
~EthernetPacketBackend()
Destroy the packet backend.
void RearmPacketWait(const std::shared_ptr< ReceiveOperation > &operation)
Rearm packet readiness for another asynchronous operation.
std::uint16_t ethertype
Configured Ethernet EtherType filter.
std::mutex captureMutex
Serializes libpcap operations.
void CancelPacketWait(const std::shared_ptr< ReceiveOperation > &operation, asio::error_code &error)
Cancel the current packet readiness operation.
void FailOperation(const std::shared_ptr< ReceiveOperation > &operation, std::exception_ptr error)
Mark a receive operation as failed.
std::vector< std::uint8_t > Recv()
Receive a complete Ethernet frame.
bool MatchesEtherType(const std::vector< std::uint8_t > &frame) const
Check whether an Ethernet frame matches the configured EtherType.
void OpenCapture(const std::string &interface)
Initialize the libpcap packet capture handle.
void Send(const std::vector< std::uint8_t > &frame)
Send one complete Ethernet frame.
bool usePolling
True when packet readiness must use timer polling.
int CaptureNextFrame(std::vector< std::uint8_t > &frame)
Read one packet from libpcap without blocking.
std::mutex receiveMutex
Prevents multiple simultaneous Recv() operations.
pcap_t * capture
libpcap capture handle.
asio::io_context & ioContext
Asio execution context.
void CompleteHandler(const std::shared_ptr< ReceiveOperation > &operation)
Complete one asynchronous handler.
EthernetPacketBackend & operator=(const EthernetPacketBackend &)=delete
void HandlePacketReady(const std::shared_ptr< ReceiveOperation > &operation, const asio::error_code &error)
Handle packet-source readiness.
EthernetPacketBackend(EthernetPacketBackend &&)=delete
EthernetPacketBackend(const EthernetPacketBackend &)=delete
void InitializePacketWait()
Connect the packet source to the Asio readiness mechanism.
asio::posix::stream_descriptor packetWait
Asio wrapper around the duplicated libpcap selectable descriptor.
std::chrono::milliseconds timeout
Current receive timeout.
std::mutex timeoutMutex
Protects access to the receive timeout.
void AsyncWaitForPacket(const std::shared_ptr< ReceiveOperation > &operation)
Start waiting until a packet source becomes readable.
Shared fixture for EthernetPacketBackend tests.
Receive operation state shared by all asynchronous handlers.
asio::steady_timer timeoutTimer
Application-level receive timeout.
ReceiveOperation(asio::io_context &ioContext)
Construct a receive operation with timers on the supplied context.
std::atomic< State > state
Completion state shared between asynchronous handlers.
std::mutex resultMutex
Protects frame and error completion data.
std::exception_ptr error
Exception produced by a failed receive.
std::promise< std::vector< std::uint8_t > > completion
Wait used by the synchronous Recv() API.
asio::steady_timer pollTimer
Polling fallback timer for non-selectable packet sources.
std::atomic< std::size_t > pendingHandlers
Number of outstanding asynchronous handlers.
std::vector< std::uint8_t > frame
Frame received by the packet handler.