PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
profinet::AsioEthernetSocket::EthernetPacketBackend Class Referencefinal

Private packet-capture backend for complete Ethernet frames. More...

#include <ethernetPacketBackend.h>

+ Collaboration diagram for profinet::AsioEthernetSocket::EthernetPacketBackend:

Classes

struct  ReceiveOperation
 Receive operation state shared by all asynchronous handlers. More...
 

Public Member Functions

 EthernetPacketBackend (asio::io_context &ioContext, const std::string &interface, std::uint16_t ethertype)
 Construct the packet backend.
 
 ~EthernetPacketBackend ()
 Destroy the packet backend.
 
 EthernetPacketBackend (const EthernetPacketBackend &)=delete
 
EthernetPacketBackend & operator= (const EthernetPacketBackend &)=delete
 
 EthernetPacketBackend (EthernetPacketBackend &&)=delete
 
EthernetPacketBackend & operator= (EthernetPacketBackend &&)=delete
 
void Send (const std::vector< std::uint8_t > &frame)
 Send one complete Ethernet frame.
 
void SetTimeout (std::chrono::milliseconds newTimeout)
 Configure the receive timeout.
 
std::vector< std::uint8_t > Recv ()
 Receive a complete Ethernet frame.
 

Private Member Functions

void OpenCapture (const std::string &interface)
 Initialize the libpcap packet capture handle.
 
void InitializePacketWait ()
 Connect the packet source to the Asio readiness mechanism.
 
void AsyncWaitForPacket (const std::shared_ptr< ReceiveOperation > &operation)
 Start waiting until a packet source becomes readable.
 
void HandlePacketReady (const std::shared_ptr< ReceiveOperation > &operation, const asio::error_code &error)
 Handle packet-source readiness.
 
void HandleTimeout (const std::shared_ptr< ReceiveOperation > &operation, const asio::error_code &error)
 Handle application receive timeout.
 
void RearmPacketWait (const std::shared_ptr< ReceiveOperation > &operation)
 Rearm packet readiness for another asynchronous operation.
 
void FailOperation (const std::shared_ptr< ReceiveOperation > &operation, std::exception_ptr error)
 Mark a receive operation as failed.
 
void CancelPacketWait (const std::shared_ptr< ReceiveOperation > &operation, asio::error_code &error)
 Cancel the current packet readiness operation.
 
void CompleteHandler (const std::shared_ptr< ReceiveOperation > &operation)
 Complete one asynchronous handler.
 
int CaptureNextFrame (std::vector< std::uint8_t > &frame)
 Read one packet from libpcap without blocking.
 
bool MatchesEtherType (const std::vector< std::uint8_t > &frame) const
 Check whether an Ethernet frame matches the configured EtherType.
 

Private Attributes

asio::io_context & ioContext
 Asio execution context.
 
pcap_t * capture {nullptr}
 libpcap capture handle.
 
std::uint16_t ethertype {0U}
 Configured Ethernet EtherType filter.
 
std::chrono::milliseconds timeout {std::chrono::milliseconds::zero()}
 Current receive timeout.
 
std::mutex timeoutMutex
 Protects access to the receive timeout.
 
std::mutex captureMutex
 Serializes libpcap operations.
 
std::mutex receiveMutex
 Prevents multiple simultaneous Recv() operations.
 
bool usePolling {false}
 True when packet readiness must use timer polling.
 
asio::posix::stream_descriptor packetWait
 Asio wrapper around the duplicated libpcap selectable descriptor.
 

Friends

class EthernetPacketBackendTestFixture
 Grants backend tests access to the private packet backend.
 

Detailed Description

Private packet-capture backend for complete Ethernet frames.

Owns the libpcap packet capture handle and integrates its packet-ready notification mechanism with Asio.

Linux uses the selectable descriptor returned by pcap_get_selectable_fd(). Windows uses the event returned by pcap_getevent(). If the platform cannot expose a waitable packet source, a short Asio timer is used as a fallback.

This class is intentionally private to AsioEthernetSocket. No protocol implementation should depend on libpcap or operating-system handles.

Definition at line 55 of file ethernetPacketBackend.h.

Constructor & Destructor Documentation

◆ EthernetPacketBackend() [1/3]

profinet::AsioEthernetSocket::EthernetPacketBackend::EthernetPacketBackend ( asio::io_context &  ioContext,
const std::string &  interface,
std::uint16_t  ethertype 
)

Construct the packet backend.

Parameters
ioContextAsio execution context used for packet readiness.
interfaceNetwork interface or packet-capture device name.
ethertypeOptional EtherType filter. Zero accepts all frames.
Exceptions
SocketErrorWhen packet capture cannot be initialized.
PermissionDeniedErrorWhen packet access is denied.

Definition at line 86 of file ethernetPacketBackend.cpp.

References InitializePacketWait(), and OpenCapture().

+ Here is the call graph for this function:

◆ ~EthernetPacketBackend()

profinet::AsioEthernetSocket::EthernetPacketBackend::~EthernetPacketBackend ( )

Destroy the packet backend.

Definition at line 100 of file ethernetPacketBackend.cpp.

References error.

◆ EthernetPacketBackend() [2/3]

profinet::AsioEthernetSocket::EthernetPacketBackend::EthernetPacketBackend ( const EthernetPacketBackend &  )
delete

◆ EthernetPacketBackend() [3/3]

profinet::AsioEthernetSocket::EthernetPacketBackend::EthernetPacketBackend ( EthernetPacketBackend &&  )
delete

Member Function Documentation

◆ AsyncWaitForPacket()

void profinet::AsioEthernetSocket::EthernetPacketBackend::AsyncWaitForPacket ( const std::shared_ptr< ReceiveOperation > &  operation)
private

Start waiting until a packet source becomes readable.

Parameters
operationActive receive operation.

Definition at line 47 of file ethernetPacketBackendLinux.cpp.

References error.

◆ CancelPacketWait()

void profinet::AsioEthernetSocket::EthernetPacketBackend::CancelPacketWait ( const std::shared_ptr< ReceiveOperation > &  operation,
asio::error_code &  error 
)
private

Cancel the current packet readiness operation.

Parameters
operationActive receive operation.
errorReceives an Asio error when cancellation fails.

Definition at line 67 of file ethernetPacketBackendLinux.cpp.

References error.

◆ CaptureNextFrame()

int profinet::AsioEthernetSocket::EthernetPacketBackend::CaptureNextFrame ( std::vector< std::uint8_t > &  frame)
private

Read one packet from libpcap without blocking.

Parameters
frameReceives the captured packet bytes.
Returns
libpcap result code from pcap_next_ex().

Definition at line 453 of file ethernetPacketBackend.cpp.

References frame.

◆ CompleteHandler()

void profinet::AsioEthernetSocket::EthernetPacketBackend::CompleteHandler ( const std::shared_ptr< ReceiveOperation > &  operation)
private

Complete one asynchronous handler.

Parameters
operationActive receive operation.

Definition at line 420 of file ethernetPacketBackend.cpp.

References error, and frame.

◆ FailOperation()

void profinet::AsioEthernetSocket::EthernetPacketBackend::FailOperation ( const std::shared_ptr< ReceiveOperation > &  operation,
std::exception_ptr  error 
)
private

Mark a receive operation as failed.

Parameters
operationActive receive operation.
errorException describing the failure.

Definition at line 400 of file ethernetPacketBackend.cpp.

References error.

◆ HandlePacketReady()

void profinet::AsioEthernetSocket::EthernetPacketBackend::HandlePacketReady ( const std::shared_ptr< ReceiveOperation > &  operation,
const asio::error_code &  error 
)
private

Handle packet-source readiness.

Parameters
operationActive receive operation.
errorAsio operation result.

Definition at line 277 of file ethernetPacketBackend.cpp.

References error, and frame.

◆ HandleTimeout()

void profinet::AsioEthernetSocket::EthernetPacketBackend::HandleTimeout ( const std::shared_ptr< ReceiveOperation > &  operation,
const asio::error_code &  error 
)
private

Handle application receive timeout.

Parameters
operationActive receive operation.
errorAsio timer result.

Definition at line 356 of file ethernetPacketBackend.cpp.

References error.

◆ InitializePacketWait()

void profinet::AsioEthernetSocket::EthernetPacketBackend::InitializePacketWait ( )
private

Connect the packet source to the Asio readiness mechanism.

Exceptions
SocketErrorWhen the platform wait mechanism cannot be created.

Definition at line 15 of file ethernetPacketBackendLinux.cpp.

References capture, error, packetWait, and usePolling.

Referenced by EthernetPacketBackend().

+ Here is the caller graph for this function:

◆ MatchesEtherType()

bool profinet::AsioEthernetSocket::EthernetPacketBackend::MatchesEtherType ( const std::vector< std::uint8_t > &  frame) const
private

Check whether an Ethernet frame matches the configured EtherType.

Parameters
frameComplete Ethernet frame.
Returns
True when the configured EtherType matches.

Definition at line 475 of file ethernetPacketBackend.cpp.

References frame, and profinet::SkipVlanTags().

Referenced by profinet::EthernetPacketBackendTestFixture::MatchesEtherType().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ OpenCapture()

void profinet::AsioEthernetSocket::EthernetPacketBackend::OpenCapture ( const std::string &  interface)
private

Initialize the libpcap packet capture handle.

Parameters
interfaceRequested network interface.

Definition at line 116 of file ethernetPacketBackend.cpp.

References profinet::MAX_ETHERNET_FRAME.

Referenced by EthernetPacketBackend().

+ Here is the caller graph for this function:

◆ operator=() [1/2]

EthernetPacketBackend & profinet::AsioEthernetSocket::EthernetPacketBackend::operator= ( const EthernetPacketBackend &  )
delete

◆ operator=() [2/2]

EthernetPacketBackend & profinet::AsioEthernetSocket::EthernetPacketBackend::operator= ( EthernetPacketBackend &&  )
delete

◆ RearmPacketWait()

void profinet::AsioEthernetSocket::EthernetPacketBackend::RearmPacketWait ( const std::shared_ptr< ReceiveOperation > &  operation)
private

Rearm packet readiness for another asynchronous operation.

Parameters
operationActive receive operation.

Definition at line 372 of file ethernetPacketBackend.cpp.

◆ Recv()

std::vector< std::uint8_t > profinet::AsioEthernetSocket::EthernetPacketBackend::Recv ( )

Receive a complete Ethernet frame.

Returns
Received frame, or an empty vector on timeout.
Exceptions
SocketErrorWhen capture fails.

Definition at line 243 of file ethernetPacketBackend.cpp.

References completion, error, and profinet::AsioEthernetSocket::ioContext.

◆ Send()

void profinet::AsioEthernetSocket::EthernetPacketBackend::Send ( const std::vector< std::uint8_t > &  frame)

Send one complete Ethernet frame.

Parameters
frameComplete Ethernet frame.
Exceptions
SocketErrorWhen libpcap cannot inject the frame.

Definition at line 206 of file ethernetPacketBackend.cpp.

References frame.

◆ SetTimeout()

void profinet::AsioEthernetSocket::EthernetPacketBackend::SetTimeout ( std::chrono::milliseconds  newTimeout)

Configure the receive timeout.

Parameters
newTimeoutTimeout duration. Zero means no timeout.

Definition at line 233 of file ethernetPacketBackend.cpp.

Friends And Related Symbol Documentation

◆ EthernetPacketBackendTestFixture

friend class EthernetPacketBackendTestFixture
friend

Grants backend tests access to the private packet backend.

The backend remains an implementation detail of AsioEthernetSocket and is not exposed as part of the production transport API.

Definition at line 201 of file ethernetPacketBackend.h.

Member Data Documentation

◆ capture

pcap_t* profinet::AsioEthernetSocket::EthernetPacketBackend::capture {nullptr}
private

libpcap capture handle.

Definition at line 213 of file ethernetPacketBackend.h.

Referenced by InitializePacketWait().

◆ captureMutex

std::mutex profinet::AsioEthernetSocket::EthernetPacketBackend::captureMutex
private

Serializes libpcap operations.

Definition at line 225 of file ethernetPacketBackend.h.

◆ ethertype

std::uint16_t profinet::AsioEthernetSocket::EthernetPacketBackend::ethertype {0U}
private

Configured Ethernet EtherType filter.

Definition at line 216 of file ethernetPacketBackend.h.

◆ ioContext

asio::io_context& profinet::AsioEthernetSocket::EthernetPacketBackend::ioContext
private

Asio execution context.

Definition at line 210 of file ethernetPacketBackend.h.

◆ packetWait

asio::posix::stream_descriptor profinet::AsioEthernetSocket::EthernetPacketBackend::packetWait
private

Asio wrapper around the duplicated libpcap selectable descriptor.

Definition at line 238 of file ethernetPacketBackend.h.

Referenced by InitializePacketWait().

◆ receiveMutex

std::mutex profinet::AsioEthernetSocket::EthernetPacketBackend::receiveMutex
private

Prevents multiple simultaneous Recv() operations.

Definition at line 228 of file ethernetPacketBackend.h.

◆ timeout

std::chrono::milliseconds profinet::AsioEthernetSocket::EthernetPacketBackend::timeout {std::chrono::milliseconds::zero()}
private

Current receive timeout.

Definition at line 219 of file ethernetPacketBackend.h.

◆ timeoutMutex

std::mutex profinet::AsioEthernetSocket::EthernetPacketBackend::timeoutMutex
private

Protects access to the receive timeout.

Definition at line 222 of file ethernetPacketBackend.h.

◆ usePolling

bool profinet::AsioEthernetSocket::EthernetPacketBackend::usePolling {false}
private

True when packet readiness must use timer polling.

Definition at line 231 of file ethernetPacketBackend.h.

Referenced by InitializePacketWait().


The documentation for this class was generated from the following files: