Skip to content

Commit

Permalink
DEV9: Improve logic for getting MacAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtrujy committed Apr 11, 2024
1 parent 3b5c027 commit 5b496f3
Showing 1 changed file with 60 additions and 21 deletions.
81 changes: 60 additions & 21 deletions pcsx2/DEV9/AdapterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@
#include "common/StringUtil.h"

#ifdef __POSIX__
#include <unistd.h>
#include <vector>
#include <fstream>
#include <net/if.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#ifdef __linux__
#include <unistd.h>
#include <sys/ioctl.h>
#endif
#include <string.h>

#if defined(__FreeBSD__) || (__APPLE__)
#include <sys/types.h>
#include <net/if_dl.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/sockio.h>
#include <net/route.h>
#include <net/if_var.h>
#endif
#endif

Expand Down Expand Up @@ -244,29 +246,66 @@ std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)

return std::nullopt;
}
#elif defined(__POSIX__)
#ifdef __linux__
#else
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
{
struct ifreq ifr;
strcpy(ifr.ifr_name, adapter->ifa_name);
MAC_Address macAddr = {0};
int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sd < 0)
{
Console.Error("DEV9: Failed to open socket");
return std::nullopt;
}

char buf[1024] = {0};
struct ifconf ifc;
memset(buf, 0, sizeof(buf));
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;

if (ioctl(sd, SIOCGIFCONF, (char*)&ifc) < 0)

Check notice on line 266 in pcsx2/DEV9/AdapterUtils.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/DEV9/AdapterUtils.cpp#L266

C-style pointer casting
{
close(sd);
Console.Error("DEV9: Failed to get interface configuration");
return std::nullopt;
}

int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
#if defined(SIOCGIFHWADDR)
struct ifreq ifr = {0};
strcpy(ifr.ifr_name, adapter->ifa_name);
if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
return std::nullopt;
memcpy(&macAddr, &ifr.ifr_hwaddr.sa_data, 6);
return macAddr;
#elif defined(SIOCGENADDR)
struct ifreq ifr = {0};
strcpy(ifr.ifr_name, adapter->ifa_name);
if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
return std::nullopt;
memcpy(&macAddr, &ifr.ifr_enaddr, 6);
return macAddr;
#elif defined(AF_LINK)
struct ifaddrs *tempifs, *po;
getifaddrs(&tempifs);
for(po = tempifs; po != NULL; po = po->ifa_next)
{
if (strcasecmp(po->ifa_name, adapter->ifa_name))
continue;

if (ret == 0)
return *(MAC_Address*)ifr.ifr_hwaddr.sa_data;
if(po->ifa_addr->sa_family != AF_LINK)
continue;

// We have a valid MAC address.
memcpy(&macAddr, LLADDR((struct sockaddr_dl *)po->ifa_addr), 6);

Check notice on line 299 in pcsx2/DEV9/AdapterUtils.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/DEV9/AdapterUtils.cpp#L299

C-style pointer casting
close(sd);
return macAddr;
}
Console.Error("DEV9: Failed to get MAC address for adapter using AF_LINK");
#endif
close(sd);
Console.Error("DEV9: Unsupported OS, can't get network features");
return std::nullopt;
}
#else
std::optional<MAC_Address> AdapterUtils::GetAdapterMAC(Adapter* adapter)
{
Console.Error("DEV9: Unsupported OS, can't get MAC address");
return std::nullopt;
}
#endif
#endif

// AdapterIP.
Expand Down Expand Up @@ -376,7 +415,7 @@ std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
}
return collection;
}
#elif defined(__FreeBSD__) || (__APPLE__)
#elif defined(__FreeBSD__) || defined(__APPLE__)
std::vector<IP_Address> AdapterUtils::GetGateways(Adapter* adapter)
{
if (adapter == nullptr)
Expand Down

0 comments on commit 5b496f3

Please sign in to comment.