|
PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
|
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. | |
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.
| profinet::AsioEthernetSocket::EthernetPacketBackend::EthernetPacketBackend | ( | asio::io_context & | ioContext, |
| const std::string & | interface, | ||
| std::uint16_t | ethertype | ||
| ) |
Construct the packet backend.
| ioContext | Asio execution context used for packet readiness. |
| interface | Network interface or packet-capture device name. |
| ethertype | Optional EtherType filter. Zero accepts all frames. |
| SocketError | When packet capture cannot be initialized. |
| PermissionDeniedError | When packet access is denied. |
Definition at line 86 of file ethernetPacketBackend.cpp.
References InitializePacketWait(), and OpenCapture().
Here is the call graph for this function:| profinet::AsioEthernetSocket::EthernetPacketBackend::~EthernetPacketBackend | ( | ) |
Destroy the packet backend.
Definition at line 100 of file ethernetPacketBackend.cpp.
References error.
|
delete |
|
delete |
|
private |
Start waiting until a packet source becomes readable.
| operation | Active receive operation. |
Definition at line 47 of file ethernetPacketBackendLinux.cpp.
References error.
|
private |
Cancel the current packet readiness operation.
| operation | Active receive operation. |
| error | Receives an Asio error when cancellation fails. |
Definition at line 67 of file ethernetPacketBackendLinux.cpp.
References error.
|
private |
Read one packet from libpcap without blocking.
| frame | Receives the captured packet bytes. |
Definition at line 453 of file ethernetPacketBackend.cpp.
References frame.
|
private |
Complete one asynchronous handler.
| operation | Active receive operation. |
Definition at line 420 of file ethernetPacketBackend.cpp.
|
private |
Mark a receive operation as failed.
| operation | Active receive operation. |
| error | Exception describing the failure. |
Definition at line 400 of file ethernetPacketBackend.cpp.
References error.
|
private |
Handle packet-source readiness.
| operation | Active receive operation. |
| error | Asio operation result. |
Definition at line 277 of file ethernetPacketBackend.cpp.
|
private |
Handle application receive timeout.
| operation | Active receive operation. |
| error | Asio timer result. |
Definition at line 356 of file ethernetPacketBackend.cpp.
References error.
|
private |
Connect the packet source to the Asio readiness mechanism.
| SocketError | When 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:
|
private |
Check whether an Ethernet frame matches the configured EtherType.
| frame | Complete Ethernet frame. |
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:
|
private |
Initialize the libpcap packet capture handle.
| interface | Requested 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:
|
delete |
|
delete |
|
private |
Rearm packet readiness for another asynchronous operation.
| operation | Active receive operation. |
Definition at line 372 of file ethernetPacketBackend.cpp.
| std::vector< std::uint8_t > profinet::AsioEthernetSocket::EthernetPacketBackend::Recv | ( | ) |
Receive a complete Ethernet frame.
| SocketError | When capture fails. |
Definition at line 243 of file ethernetPacketBackend.cpp.
References completion, error, and profinet::AsioEthernetSocket::ioContext.
| void profinet::AsioEthernetSocket::EthernetPacketBackend::Send | ( | const std::vector< std::uint8_t > & | frame | ) |
Send one complete Ethernet frame.
| frame | Complete Ethernet frame. |
| SocketError | When libpcap cannot inject the frame. |
Definition at line 206 of file ethernetPacketBackend.cpp.
References frame.
| void profinet::AsioEthernetSocket::EthernetPacketBackend::SetTimeout | ( | std::chrono::milliseconds | newTimeout | ) |
Configure the receive timeout.
| newTimeout | Timeout duration. Zero means no timeout. |
Definition at line 233 of file ethernetPacketBackend.cpp.
|
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.
|
private |
libpcap capture handle.
Definition at line 213 of file ethernetPacketBackend.h.
Referenced by InitializePacketWait().
|
private |
Serializes libpcap operations.
Definition at line 225 of file ethernetPacketBackend.h.
|
private |
Configured Ethernet EtherType filter.
Definition at line 216 of file ethernetPacketBackend.h.
|
private |
Asio execution context.
Definition at line 210 of file ethernetPacketBackend.h.
|
private |
Asio wrapper around the duplicated libpcap selectable descriptor.
Definition at line 238 of file ethernetPacketBackend.h.
Referenced by InitializePacketWait().
|
private |
Prevents multiple simultaneous Recv() operations.
Definition at line 228 of file ethernetPacketBackend.h.
|
private |
Current receive timeout.
Definition at line 219 of file ethernetPacketBackend.h.
|
private |
Protects access to the receive timeout.
Definition at line 222 of file ethernetPacketBackend.h.
|
private |
True when packet readiness must use timer polling.
Definition at line 231 of file ethernetPacketBackend.h.
Referenced by InitializePacketWait().