Skip to content

Commit

Permalink
Merge pull request xbmc#25111 from CrystalP/fix-macdiscovery
Browse files Browse the repository at this point in the history
[Windows] Fix Discovery of MAC Address
  • Loading branch information
CrystalP authored May 4, 2024
2 parents 0db97ab + 3758c6f commit 0894898
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions xbmc/platform/win32/network/NetworkWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ bool CNetworkWin32::PingHost(const struct sockaddr& host, unsigned int timeout_m

bool CNetworkInterfaceWin32::GetHostMacAddress(unsigned long host, std::string& mac) const
{
struct sockaddr sockHost;
sockaddr sockHost{};
sockHost.sa_family = AF_INET;
reinterpret_cast<struct sockaddr_in&>(sockHost).sin_addr.S_un.S_addr = host;
return GetHostMacAddress(&sockHost, mac);
Expand All @@ -260,33 +260,38 @@ bool CNetworkInterfaceWin32::GetHostMacAddress(struct sockaddr* host, std::strin
if (GetBestInterfaceEx(host, &InterfaceIndex) != NO_ERROR)
return false;

NET_LUID luid = {};
NET_LUID luid{};
if (ConvertInterfaceIndexToLuid(InterfaceIndex, &luid) != NO_ERROR)
return false;

MIB_IPNET_ROW2 neighborIp = {};
MIB_IPNET_ROW2 neighborIp{};
neighborIp.InterfaceLuid = luid;
neighborIp.InterfaceIndex;
neighborIp.Address.si_family = host->sa_family;
switch (host->sa_family)
{
case AF_INET:
neighborIp.Address.Ipv4 = reinterpret_cast<const struct sockaddr_in&>(host);
memcpy(&neighborIp.Address.Ipv4, host, sizeof(sockaddr_in));
break;
case AF_INET6:
neighborIp.Address.Ipv6 = reinterpret_cast<const struct sockaddr_in6&>(host);
memcpy(&neighborIp.Address.Ipv6, host, sizeof(sockaddr_in6));
break;
default:
return false;
}

DWORD dwRetVal = ResolveIpNetEntry2(&neighborIp, nullptr);
DWORD dwRetVal = GetIpNetEntry2(&neighborIp);

if (dwRetVal != NO_ERROR)
{
CLog::LogF(LOGDEBUG, "Host not found in the cache (error {}), resolve the address.", dwRetVal);
dwRetVal = ResolveIpNetEntry2(&neighborIp, nullptr);
}

if (dwRetVal != NO_ERROR)
{
CLog::LogF(LOGERROR, "ResolveIpNetEntry2 failed with error ({})", dwRetVal);
return false;
}

if (neighborIp.PhysicalAddressLength < MAC_LENGTH)
{
CLog::LogF(LOGERROR,
Expand Down

0 comments on commit 0894898

Please sign in to comment.