From 8a654e78ab44596833f06416e9d40bc10ac6c575 Mon Sep 17 00:00:00 2001 From: "Robin E. R. Davies" Date: Sat, 23 Nov 2024 12:24:02 -0500 Subject: [PATCH] Ubuntu IP interface names. --- src/Ipv6Helpers.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ src/Ipv6Helpers.hpp | 1 + 2 files changed, 42 insertions(+) diff --git a/src/Ipv6Helpers.cpp b/src/Ipv6Helpers.cpp index 04b56b6a..4dcb4188 100644 --- a/src/Ipv6Helpers.cpp +++ b/src/Ipv6Helpers.cpp @@ -267,6 +267,46 @@ bool pipedal::IsOnLocalSubnet(const std::string &fromAddress) return result; } + +static bool isEthernetAddress(const char*ifName) +{ + // either ethN (classic), + if (strncmp(ifName,"eth",3) == 0) return true; + // or "enpNNsNN" (ubuntu) + return (ifName[0] == 'e' && ifName[1] == 'n' && ifName[2] == 'p'); +} + +std::vector pipedal::GetEthernetIpv4Addresses() { + std::vector result; + + struct ifaddrs *ifap = nullptr; + if (getifaddrs(&ifap) != 0) + return result; + + for (ifaddrs *p = ifap; p != nullptr; p = p->ifa_next) + { + if (isEthernetAddress(p->ifa_name)) + { + if (p->ifa_addr->sa_family == AF_INET && p->ifa_addr != nullptr && p->ifa_netmask != nullptr) + uint32_t netmask = htonl(((sockaddr_in *)(p->ifa_netmask))->sin_addr.s_addr); + uint32_t ifAddr = htonl(((sockaddr_in *)(p->ifa_addr))->sin_addr.s_addr); + { + if (ifAddr & 0xFF) { // has an actual bound IP address. + std::string name = SS( + ((ifAddr >> 24) & 0xFF) << + '.' << ((ifAddr >> 16) & 0xFF) << + '.' << ((ifAddr >> 8) & 0xFF) << + '.' << ((ifAddr) & 0xFF) + ); + result.push_back(std::move(name)); + } + } + } + } + freeifaddrs(ifap); + return result; +} + static std::string GetInterfaceForIp4Address(uint32_t ipv4Address) { struct ifaddrs *ifap = nullptr; @@ -618,6 +658,7 @@ std::string pipedal::GetInterfaceIpv4Address(const std::string& interfaceName) } + // std::string getNonLinkLocalAddress(const std::string&address, const std::string&interface) // { // if (!interface.empty()) diff --git a/src/Ipv6Helpers.hpp b/src/Ipv6Helpers.hpp index 17e56408..b041f732 100644 --- a/src/Ipv6Helpers.hpp +++ b/src/Ipv6Helpers.hpp @@ -27,6 +27,7 @@ namespace pipedal { std::string GetInterfaceIpv4Address(const std::string& interfaceName); + std::vector GetEthernetIpv4Addresses(); // bool IsLinkLocalAddress(const std::string &fromAddress);