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

A minimal RAII wrapper around a Linux AF_PACKET raw socket bound to an interface. More...

#include <util.h>

+ Inheritance diagram for profinet::EthernetSocket:
+ Collaboration diagram for profinet::EthernetSocket:

Public Member Functions

 EthernetSocket (const std::string &interface, std::uint16_t ethertype=0)
 Bind a raw socket to an interface.
 
 ~EthernetSocket ()
 Close the underlying socket.
 
 EthernetSocket (const EthernetSocket &)=delete
 Not copyable (owns a raw socket file descriptor).
 
EthernetSocketoperator= (const EthernetSocket &)=delete
 Not copyable (owns a raw socket file descriptor).
 
 EthernetSocket (EthernetSocket &&other) noexcept
 Move-construct, transferring ownership of the underlying socket.
 
EthernetSocketoperator= (EthernetSocket &&other) noexcept
 Move-assign, transferring ownership of the underlying socket.
 
void Send (const std::vector< std::uint8_t > &frame) const override
 Send a raw Ethernet frame.
 
void SetTimeout (std::chrono::milliseconds timeout) const override
 Set the receive timeout.
 
std::vector< std::uint8_t > Recv () const override
 Receive up to MAX_ETHERNET_FRAME bytes.
 
int Fd () const
 The underlying file descriptor.
 
- Public Member Functions inherited from profinet::IRawEthernetSocket
virtual ~IRawEthernetSocket ()=default
 Virtual destructor for polymorphic use.
 

Private Attributes

int fd = -1
 Underlying socket file descriptor, or -1 if closed/moved-from.
 

Detailed Description

A minimal RAII wrapper around a Linux AF_PACKET raw socket bound to an interface.

Mirrors util.py's ethernet_socket(). Uses ETH_P_ALL by default so VLAN-tagged responses (some devices, e.g. Siemens S7-1200, reply with an 802.1Q tag even to untagged requests) are still received.

Definition at line 195 of file util.h.

Constructor & Destructor Documentation

◆ EthernetSocket() [1/3]

profinet::EthernetSocket::EthernetSocket ( const std::string &  interface,
std::uint16_t  ethertype = 0 
)
explicit

Bind a raw socket to an interface.

Parameters
interfaceInterface name to bind to, e.g. "eth0".
ethertypeEtherType filter for received frames; 0 (default) means ETH_P_ALL.

Throws PermissionDeniedError if not running with sufficient privilege, or SocketError on other failures.

Definition at line 295 of file util.cpp.

References profinet::ETH_P_ALL, and fd.

◆ ~EthernetSocket()

profinet::EthernetSocket::~EthernetSocket ( )

Close the underlying socket.

Definition at line 338 of file util.cpp.

References fd.

◆ EthernetSocket() [2/3]

profinet::EthernetSocket::EthernetSocket ( const EthernetSocket )
delete

Not copyable (owns a raw socket file descriptor).

◆ EthernetSocket() [3/3]

profinet::EthernetSocket::EthernetSocket ( EthernetSocket &&  other)
noexcept

Move-construct, transferring ownership of the underlying socket.

Parameters
otherSocket to move from; left closed.

Definition at line 346 of file util.cpp.

Member Function Documentation

◆ Fd()

int profinet::EthernetSocket::Fd ( ) const
inline

The underlying file descriptor.

Returns
The raw socket file descriptor.

Definition at line 241 of file util.h.

References fd.

◆ operator=() [1/2]

EthernetSocket & profinet::EthernetSocket::operator= ( const EthernetSocket )
delete

Not copyable (owns a raw socket file descriptor).

◆ operator=() [2/2]

EthernetSocket & profinet::EthernetSocket::operator= ( EthernetSocket &&  other)
noexcept

Move-assign, transferring ownership of the underlying socket.

Parameters
otherSocket to move from; left closed.
Returns
Reference to this socket.

Definition at line 352 of file util.cpp.

◆ Recv()

std::vector< std::uint8_t > profinet::EthernetSocket::Recv ( ) const
overridevirtual

Receive up to MAX_ETHERNET_FRAME bytes.

Returns
The received frame, or an empty vector on timeout (mirrors Python's socket.timeout being caught and retried by the caller).

Implements profinet::IRawEthernetSocket.

Definition at line 386 of file util.cpp.

References fd, and profinet::MAX_ETHERNET_FRAME.

Referenced by profinet::dcp::ReadResponse(), and profinet::dcp::ReceiveHello().

+ Here is the caller graph for this function:

◆ Send()

void profinet::EthernetSocket::Send ( const std::vector< std::uint8_t > &  frame) const
overridevirtual

Send a raw Ethernet frame.

Parameters
frameComplete frame bytes (destination MAC .. payload, no FCS).

Implements profinet::IRawEthernetSocket.

Definition at line 366 of file util.cpp.

References fd.

Referenced by profinet::dcp::GetParam(), profinet::dcp::ResetToFactory(), profinet::dcp::SendDiscover(), profinet::dcp::SendHello(), profinet::dcp::SendRequest(), profinet::dcp::SetIp(), profinet::dcp::SetParam(), and profinet::dcp::SignalDevice().

+ Here is the caller graph for this function:

◆ SetTimeout()

void profinet::EthernetSocket::SetTimeout ( std::chrono::milliseconds  timeout) const
overridevirtual

Set the receive timeout.

Parameters
timeoutTimeout duration; a zero duration blocks forever.

Sets SO_RCVTIMEO. Marked const: this only changes a kernel-side socket option, not the object's own identity, which lets read-only call sites keep a const&.

Implements profinet::IRawEthernetSocket.

Definition at line 375 of file util.cpp.

References fd.

Referenced by profinet::dcp::ReadResponse(), and profinet::dcp::ReceiveHello().

+ Here is the caller graph for this function:

Member Data Documentation

◆ fd

int profinet::EthernetSocket::fd = -1
private

Underlying socket file descriptor, or -1 if closed/moved-from.

Definition at line 248 of file util.h.

Referenced by EthernetSocket(), ~EthernetSocket(), Fd(), Recv(), Send(), and SetTimeout().


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