PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
util.cpp File Reference

Implements common PROFINET utility, address, and socket functions. More...

#include "profinet/util.h"
#include <arpa/inet.h>
#include <fcntl.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <ranges>
#include "profinet/exceptions.h"
#include <linux/if_packet.h>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <utility>
+ Include dependency graph for util.cpp:

Go to the source code of this file.

Namespaces

namespace  profinet
 

Functions

std::string profinet::Hex4 (std::uint16_t value)
 Format a 16-bit unsigned integer as a 4-digit hexadecimal string.
 
std::string profinet::Hex2 (std::uint8_t value)
 Format an 8-bit unsigned integer as a 2-digit hexadecimal string.
 
std::vector< std::uint8_t > profinet::ToVec (const std::string &input)
 Convert a string into a vector of raw bytes.
 
std::string profinet::HexFallback (const char *widthPrefix, std::uint32_t value, int digitWidth)
 Format a hexadecimal string with a custom prefix and dynamic width.
 
std::size_t profinet::SkipVlanTags (std::span< const std::uint8_t > frame) noexcept
 Return the byte offset of the real EtherType in a raw Ethernet frame.
 
std::string profinet::ToHex (const std::uint8_t *data, std::size_t len)
 Hex-encode a byte buffer.
 
std::string profinet::ToHex (const std::vector< std::uint8_t > &data)
 Hex-encode a byte buffer.
 
MacAddress profinet::String2Mac (const std::string &macStr)
 Parse a colon-separated MAC address string.
 
std::string profinet::Mac2String (const MacAddress &mac)
 Format a MAC address as a colon-separated string.
 
std::string profinet::Mac2String (const std::uint8_t *macBytes, std::size_t len)
 Format a MAC address as a colon-separated string.
 
std::string profinet::String2Ip (std::span< const std::uint8_t > ipBytes)
 Format raw IPv4 address bytes as a dotted string.
 
std::string profinet::String2Ip (const std::uint8_t *ipBytes, std::size_t len)
 Format raw IPv4 address bytes as a dotted string.
 
std::array< std::uint8_t, 4 > profinet::Ip2String (const std::string &ipStr)
 Parse a dotted-decimal IPv4 address string.
 
std::string profinet::DecodeBytes (const std::uint8_t *data, std::size_t len)
 Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
 
std::string profinet::DecodeBytes (const std::vector< std::uint8_t > &data)
 Decode bytes to a UTF-8 string, stripping trailing NUL bytes.
 
MacAddress profinet::GetMac (const std::string &ifname)
 Get the MAC address of a network interface.
 
std::filesystem::path profinet::GetExecutableDirectory ()
 Retrieves the absolute directory path of the currently running executable.
 

Detailed Description

Implements common PROFINET utility, address, and socket functions.

Provides the utility functionality declared in util.h for byte conversion, network address handling, Ethernet frame processing, and Linux socket operations used by the PROFINET controller.

The implementation covers:

  • Hexadecimal formatting and byte-vector conversion.
  • Ethernet frame VLAN tag detection and offset calculation.
  • Conversion of binary data to hexadecimal strings.
  • Conversion between textual and binary MAC addresses.
  • Conversion between textual and binary IPv4 addresses.
  • Decoding zero-terminated or zero-padded byte fields.
  • Retrieval of the MAC address associated with a Linux network interface.
  • Linux AF_PACKET raw Ethernet socket creation and management.
  • Raw Ethernet frame transmission and reception.
  • Ethernet socket receive timeouts.
  • Connected IPv4 UDP socket creation and management.
  • UDP datagram transmission and reception with timeout handling.
  • Move-only ownership of underlying socket file descriptors.
  • Resolution of the running executable's directory through /proc/self/exe.

Socket operations use the Linux AF_PACKET and IPv4 socket APIs and translate operating-system failures into the project's PROFINET socket exception types. Raw Ethernet sockets require the appropriate operating system privileges.

Definition in file util.cpp.