67using MacAddress = std::array<std::uint8_t, macAddressLength>;
76std::string
Hex4(std::uint16_t value);
81std::string
Hex2(std::uint8_t value);
86std::vector<std::uint8_t>
ToVec(
const std::string& input);
93std::string
HexFallback(
const char* widthPrefix, std::uint32_t value,
int digitWidth);
109std::size_t
SkipVlanTags(std::span<const std::uint8_t> frame)
noexcept;
115std::string
ToHex(
const std::uint8_t* data, std::size_t len);
120std::string
ToHex(
const std::vector<std::uint8_t>& data);
137std::string
Mac2String(
const std::uint8_t* macBytes, std::size_t len);
143std::string
String2Ip(std::span<const std::uint8_t> ipBytes);
150std::string
String2Ip(
const std::uint8_t* ipBytes, std::size_t len);
156std::array<std::uint8_t, 4>
Ip2String(
const std::string& ipStr);
162std::string
DecodeBytes(
const std::uint8_t* data, std::size_t len);
167std::string
DecodeBytes(
const std::vector<std::uint8_t>& data);
203 explicit EthernetSocket(
const std::string& interface, std::uint16_t ethertype = 0);
225 void Send(
const std::vector<std::uint8_t>& frame)
const override;
232 void SetTimeout(std::chrono::milliseconds timeout)
const override;
237 [[nodiscard]] std::vector<std::uint8_t>
Recv()
const override;
241 [[nodiscard]]
int Fd()
const
266 UdpSocket(
const std::string& host, std::uint16_t port,
267 std::chrono::duration<double> timeout = std::chrono::duration<double>(
defaultTimeout));
289 void Send(
const std::vector<std::uint8_t>& data)
const;
298 [[nodiscard]]
int Fd()
const
324 :
dieAfter(std::chrono::steady_clock::now() +
325 std::chrono::duration_cast<std::chrono::steady_clock::duration>(seconds))
333 return std::chrono::steady_clock::now() >
dieAfter;
338 [[nodiscard]] std::chrono::duration<double>
Remaining()
const
340 auto rem =
dieAfter - std::chrono::steady_clock::now();
341 auto secs = std::chrono::duration<double>(rem);
342 return secs.count() > 0 ? secs : std::chrono::duration<double>(0);
Abstraction for sending and receiving raw Ethernet frames.
A minimal RAII wrapper around a Linux AF_PACKET raw socket bound to an interface.
int fd
Underlying socket file descriptor, or -1 if closed/moved-from.
EthernetSocket & operator=(const EthernetSocket &)=delete
Not copyable (owns a raw socket file descriptor).
void SetTimeout(std::chrono::milliseconds timeout) const override
Set the receive timeout.
int Fd() const
The underlying file descriptor.
void Send(const std::vector< std::uint8_t > &frame) const override
Send a raw Ethernet frame.
std::vector< std::uint8_t > Recv() const override
Receive up to MAX_ETHERNET_FRAME bytes.
~EthernetSocket()
Close the underlying socket.
EthernetSocket(const EthernetSocket &)=delete
Not copyable (owns a raw socket file descriptor).
Abstract interface for raw Ethernet frame transport.
Time-limited-operation helper, mirroring util.py's MaxTimeout context manager.
MaxTimeout(std::chrono::duration< double > seconds)
Start a countdown of the given duration.
std::chrono::steady_clock::time_point dieAfter
Time point at which TimedOut() will start returning true.
bool TimedOut() const
Whether the countdown has elapsed.
std::chrono::duration< double > Remaining() const
Time left before the countdown elapses.
A connected UDP socket with a timeout.
~UdpSocket()
Close the underlying socket.
std::vector< std::uint8_t > Recv(std::size_t maxLen=defaultReceiveBufferSize) const
Receive a UDP datagram from the connected peer.
void Send(const std::vector< std::uint8_t > &data) const
Send a UDP datagram to the connected peer.
int fd
Underlying socket file descriptor, or -1 if closed/moved-from.
static constexpr int defaultReceiveBufferSize
Default receive buffer size.
static constexpr double defaultTimeout
Default Timeout.
UdpSocket & operator=(const UdpSocket &)=delete
Not copyable (owns a socket file descriptor).
UdpSocket(const UdpSocket &)=delete
Not copyable (owns a socket file descriptor).
int Fd() const
The underlying file descriptor.
std::string Hex2(std::uint8_t value)
Format an 8-bit unsigned integer as a 2-digit hexadecimal string.
constexpr std::uint16_t VLAN_ETHERTYPE
EtherType value identifying an 802.1Q VLAN tag (0x8100).
constexpr int uuidLenght
Constant lenght of a UUID.
std::vector< std::uint8_t > ToVec(const std::string &input)
Convert a string into a vector of raw bytes.
std::string String2Ip(std::span< const std::uint8_t > ipBytes)
Format raw IPv4 address bytes as a dotted string.
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
std::array< std::uint8_t, 4 > Ip2String(const std::string &ipStr)
Parse a dotted-decimal IPv4 address string.
std::filesystem::path GetExecutableDirectory()
Retrieves the absolute directory path of the currently running executable.
constexpr std::uint16_t IP_ETHERTYPE
EtherType value identifying IP frames (0x8000).
MacAddress String2Mac(const std::string &macStr)
Parse a colon-separated MAC address string.
constexpr std::size_t MAX_ETHERNET_FRAME
Maximum PROFINET Ethernet frame size in bytes (including VLAN tag headroom).
std::size_t SkipVlanTags(std::span< const std::uint8_t > frame) noexcept
Return the byte offset of the real EtherType in a raw Ethernet frame.
MacAddress GetMac(const std::string &ifname)
Get the MAC address of a network interface.
static constexpr int OneOctetShift
The bit-shift distance required to move data across a single octet.
static constexpr int ThreeOctetsShift
The bit-shift distance required to move data across three octets.
constexpr int macAddressLength
Constant lenght of a mac address.
static constexpr int TwoOctetsShift
The bit-shift distance required to move data across two octets.
std::string ToHex(const std::uint8_t *data, std::size_t len)
Hex-encode a byte buffer.
constexpr std::uint16_t PROFINET_ETHERTYPE
EtherType value identifying PROFINET frames (0x8892).
std::string Hex4(std::uint16_t value)
Format a 16-bit unsigned integer as a 4-digit hexadecimal string.
static constexpr std::uint8_t LowByteMask
Bitmask used to isolate the lowest significant byte (8 bits) of a larger integer.
std::string HexFallback(const char *widthPrefix, std::uint32_t value, int digitWidth)
Format a hexadecimal string with a custom prefix and dynamic width.
constexpr std::size_t RECEIVE_BUFFER_LENGTH
Receive buffer length.
constexpr int ETH_P_ALL
Linux AF_PACKET protocol value matching every EtherType.
std::string Mac2String(const MacAddress &mac)
Format a MAC address as a colon-separated string.
std::string DecodeBytes(const std::uint8_t *data, std::size_t len)
Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
static constexpr int BitsPerOctet