From 03c55a5b314a8281270e461b16c2954aaa762ec7 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 14 Nov 2019 12:37:08 -0600 Subject: Qt UI: Factor MAC address into base interface list --- ui/qt5/horizonwizard.cc | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'ui/qt5/horizonwizard.cc') diff --git a/ui/qt5/horizonwizard.cc b/ui/qt5/horizonwizard.cc index dda631f..67ed193 100644 --- a/ui/qt5/horizonwizard.cc +++ b/ui/qt5/horizonwizard.cc @@ -21,6 +21,9 @@ #ifdef HAS_INSTALL_ENV # include +# include /* ifreq */ +# include /* ioctl */ +# include /* close */ #endif /* HAS_INSTALL_ENV */ #include "intropage.hh" @@ -63,13 +66,13 @@ static std::map help_id_map = { #ifdef HAS_INSTALL_ENV -std::map probe_ifaces(void) { +std::map probe_ifaces(void) { struct udev *udev; struct udev_enumerate *if_list; struct udev_list_entry *first, *candidate; struct udev_device *device = nullptr; - std::map ifaces; + std::map ifaces; udev = udev_new(); if(udev == nullptr) { @@ -90,6 +93,7 @@ std::map probe_ifaces(void) { udev_list_entry_foreach(candidate, first) { const char *syspath = udev_list_entry_get_name(candidate); const char *devtype, *cifname; + QString mac; if(device != nullptr) udev_device_unref(device); device = udev_device_new_from_syspath(udev, syspath); @@ -109,18 +113,40 @@ std::map probe_ifaces(void) { continue; } + /* Retrieving the index is always valid, and is not even privileged. */ + struct ifreq request; + int my_sock = ::socket(AF_INET, SOCK_STREAM, 0); + if(my_sock != -1) { + memset(&request, 0, sizeof(request)); + memcpy(&request.ifr_name, cifname, strlen(cifname)); + errno = 0; + if(ioctl(my_sock, SIOCGIFHWADDR, &request) != -1) { + char *buf; + asprintf(&buf, "%02X:%02X:%02X:%02X:%02X:%02X", + request.ifr_ifru.ifru_hwaddr.sa_data[0], + request.ifr_ifru.ifru_hwaddr.sa_data[1], + request.ifr_ifru.ifru_hwaddr.sa_data[2], + request.ifr_ifru.ifru_hwaddr.sa_data[3], + request.ifr_ifru.ifru_hwaddr.sa_data[4], + request.ifr_ifru.ifru_hwaddr.sa_data[5]); + mac = QString(buf); + free(buf); + } + ::close(my_sock); + } + std::string ifname(cifname); if(strstr(devtype, "wlan")) { - ifaces.insert({ifname, HorizonWizard::Wireless}); + ifaces.insert({ifname, {HorizonWizard::Wireless, mac}}); } else if(strstr(devtype, "bond")) { - ifaces.insert({ifname, HorizonWizard::Bonded}); + ifaces.insert({ifname, {HorizonWizard::Bonded, mac}}); } else if(strstr(syspath, "/virtual/")) { /* Skip lo, tuntap, etc */ continue; } else if(strstr(devtype, "1")) { - ifaces.insert({ifname, HorizonWizard::Ethernet}); + ifaces.insert({ifname, {HorizonWizard::Ethernet, mac}}); } else { - ifaces.insert({ifname, HorizonWizard::Unknown}); + ifaces.insert({ifname, {HorizonWizard::Unknown, mac}}); } } -- cgit v1.2.3-60-g2f50