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 91b0b16 commit ded282a
Showing 1 changed file with 76 additions and 20 deletions.
96 changes: 76 additions & 20 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,83 @@ 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;
MAC_Address macAddr = {0};
struct ifreq ifr = {0};

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/DEV9/AdapterUtils.cpp#L253

Variable 'ifr' is assigned a value that is never used.
/*
* BSD 4.4 defines the size of an ifreq to be
* max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
* However, under earlier systems, sa_len isn't present, so the size is
* just sizeof(struct ifreq)
*/
#ifdef HAVE_SA_LEN
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#define ifreq_size(i) max(sizeof(struct ifreq), \
sizeof((i).ifr_name) + (i).ifr_addr.sa_len)
#else
#define ifreq_size(i) sizeof(struct ifreq)
#endif /* HAVE_SA_LEN*/

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 283 in pcsx2/DEV9/AdapterUtils.cpp

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/DEV9/AdapterUtils.cpp#L283

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

#if defined(SIOCGIFHWADDR)
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)
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)
for (int i = 0; i < ifc.ifc_len; i += ifreq_size(*ifrp))
{
struct ifreq* ifrp = ((struct ifreq*)(ifc.ifc_buf + i));

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

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pcsx2/DEV9/AdapterUtils.cpp#L305

C-style pointer casting
if (strcmp(ifrp->ifr_name, adapter->ifa_name) != 0)
continue;

int fd = socket(AF_INET, SOCK_DGRAM, 0);
int ret = ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);
strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);

if (ret == 0)
return *(MAC_Address*)ifr.ifr_hwaddr.sa_data;
struct sockaddr_dl sdlp;
memcpy(&sdlp, &ifrp->ifr_addr, sizeof(sdlp));
if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
continue;

// We have a valid MAC address.
memcpy(&macAddr, &sdlp->sdl_data[sdlp->sdl_nlen], 6);
close(sd);
return macAddr;
}
#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 +432,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 ded282a

Please sign in to comment.