14#include <sys/socket.h>
29MacAddress GetLinuxInterfaceMac(
const std::string& interface)
31 if (interface.empty())
33 throw SocketError(
"Interface name cannot be empty");
36 if (interface.size() >= IFNAMSIZ)
38 throw SocketError(
"Interface name is too long: '" + interface +
"'");
41 const int socketFd = ::socket(AF_INET, SOCK_DGRAM, 0);
45 throw SocketError(
"Failed to open socket while reading MAC address for '" + interface +
"': " +
46 std::strerror(errno));
50 std::strncpy(request.ifr_name, interface.c_str(), IFNAMSIZ - 1);
52 if (::ioctl(socketFd, SIOCGIFHWADDR, &request) < 0)
54 const int error = errno;
57 throw SocketError(
"Failed to get MAC address for '" + interface +
"': " +
58 std::strerror(
error));
65 std::span<const std::uint8_t>(
66 reinterpret_cast<const std::uint8_t*
>(request.ifr_hwaddr.sa_data),
76 return GetLinuxInterfaceMac(interface);
81 static constexpr char kProcessPath[] =
"/proc/self/exe";
85 return std::filesystem::read_symlink(kProcessPath).parent_path();
87 catch (
const std::filesystem::filesystem_error&
error)
89 throw std::runtime_error(
"Failed to resolve executable path from /proc/self/exe: " +
90 std::string(
error.what()));
Declares exceptions and error types used by the PROFINET IO controller stack.
MacAddress GetInterfaceMac(const std::string &interface)
Get the MAC address assigned to a network interface.
std::array< std::uint8_t, macAddressLength > MacAddress
A 6-byte Ethernet MAC address.
std::filesystem::path GetExecutableDirectory()
Retrieves the absolute directory path of the currently running executable.
constexpr int macAddressLength
Constant lenght of a mac address.
Declares Linux Ethernet, addressing, timing, and utility helpers used by the PROFINET stack.