[GH-ISSUE #85] Virtual interfaces cause mac address detection to fail #62

Closed
opened 2026-10-15 12:55:44 +00:00 by bsakaguchi · 0 comments
Owner

Originally created by @Azelphur on GitHub (Jul 14, 2024).
Original GitHub issue: https://github.com/games-on-whales/wolf/issues/85

It seems that if you are using virtual interfaces (ie enp1s0f0:0) then get_mac_address fails, because it tries to open /sys/class/net/enp1s0f0:0/address, when it should instead open /sys/class/net/enp1s0f0

For context:

$ ip addr show enp1s0f0
3: enp1s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 48:df:37:12:99:58 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.11/24 brd 192.168.1.255 scope global dynamic enp1s0f0:0
       valid_lft 55649sec preferred_lft 44849sec
    inet 192.168.1.9/24 brd 192.168.1.255 scope global secondary enp1s0f0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::4adf:37ff:fe12:9958/64 scope link proto kernel_ll 
       valid_lft forever preferred_lft forever

I modified the function

std::string get_mac_address(std::string_view local_ip) {
  ifaddrs *ifaddrptr = nullptr;
  getifaddrs(&ifaddrptr);
  std::unique_ptr<ifaddrs, decltype(&freeifaddrs)> ifAddrStruct = {ifaddrptr, ::freeifaddrs};

  for (auto ifa = ifAddrStruct.get(); ifa != NULL; ifa = ifa->ifa_next) {
    if (ifa->ifa_addr) {
      logs::log(logs::warning, "local_ip = {}, get_ip_address(ifa) = {}", local_ip, get_ip_address(ifa));
    }
    if (ifa->ifa_addr && local_ip == get_ip_address(ifa)) {
      logs::log(logs::warning, "trying to open /sys/class/net/{}/address", ifa->ifa_name);
      std::ifstream mac_file(fmt::format("/sys/class/net/{}/address", ifa->ifa_name));
      if (mac_file.is_open()) {
        std::string mac_address;
        std::getline(mac_file, mac_address);
        return mac_address;
      }
    }
  }

  logs::log(logs::warning,
            "Unable to get mac address of ip address: {}, you can override this by settings the env variables "
            "WOLF_INTERNAL_MAC or WOLF_INTERNAL_IP",
            local_ip);

  return "00:00:00:00:00:00";
}

and I get

wolf-1  | 17:02:16.012470817 WARN  | local_ip = 192.168.1.9, get_ip_address(ifa) = 192.168.1.9
wolf-1  | 17:02:16.012475265 WARN  | trying to open /sys/class/net/enp1s0f0:1/address

which doesn't exist, however, /sys/class/net/enp1s0f0/address does exist.

Originally created by @Azelphur on GitHub (Jul 14, 2024). Original GitHub issue: https://github.com/games-on-whales/wolf/issues/85 It seems that if you are using virtual interfaces (ie enp1s0f0:0) then get_mac_address fails, because it tries to open /sys/class/net/enp1s0f0:0/address, when it should instead open `/sys/class/net/enp1s0f0` For context: ```cpp $ ip addr show enp1s0f0 3: enp1s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 48:df:37:12:99:58 brd ff:ff:ff:ff:ff:ff inet 192.168.1.11/24 brd 192.168.1.255 scope global dynamic enp1s0f0:0 valid_lft 55649sec preferred_lft 44849sec inet 192.168.1.9/24 brd 192.168.1.255 scope global secondary enp1s0f0:1 valid_lft forever preferred_lft forever inet6 fe80::4adf:37ff:fe12:9958/64 scope link proto kernel_ll valid_lft forever preferred_lft forever ``` I modified the function ``` std::string get_mac_address(std::string_view local_ip) { ifaddrs *ifaddrptr = nullptr; getifaddrs(&ifaddrptr); std::unique_ptr<ifaddrs, decltype(&freeifaddrs)> ifAddrStruct = {ifaddrptr, ::freeifaddrs}; for (auto ifa = ifAddrStruct.get(); ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr) { logs::log(logs::warning, "local_ip = {}, get_ip_address(ifa) = {}", local_ip, get_ip_address(ifa)); } if (ifa->ifa_addr && local_ip == get_ip_address(ifa)) { logs::log(logs::warning, "trying to open /sys/class/net/{}/address", ifa->ifa_name); std::ifstream mac_file(fmt::format("/sys/class/net/{}/address", ifa->ifa_name)); if (mac_file.is_open()) { std::string mac_address; std::getline(mac_file, mac_address); return mac_address; } } } logs::log(logs::warning, "Unable to get mac address of ip address: {}, you can override this by settings the env variables " "WOLF_INTERNAL_MAC or WOLF_INTERNAL_IP", local_ip); return "00:00:00:00:00:00"; } ``` and I get ``` wolf-1 | 17:02:16.012470817 WARN | local_ip = 192.168.1.9, get_ip_address(ifa) = 192.168.1.9 wolf-1 | 17:02:16.012475265 WARN | trying to open /sys/class/net/enp1s0f0:1/address ``` which doesn't exist, however, `/sys/class/net/enp1s0f0/address` does exist.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
games-on-whales/wolf#62
No description provided.