Commit fc8db5e9 authored by Vladislav Vaintroub's avatar Vladislav Vaintroub

Downport

WL#1624 -determine MAC addresses on Windows.
Contribution  by Chris Runyan
parent 716099e0
......@@ -101,7 +101,97 @@ err:
return res;
}
#else /* FreeBSD elif linux */
#elif defined(__WIN__)
#include <iphlpapi.h>
/*
The following typedef is for dynamically loading
iphlpapi.dll / GetAdaptersAddresses. Dynamic loading is
used because GetAdaptersAddresses is not available on Windows 2000
which MySQL still supports. Static linking would cause an unresolved export.
*/
typedef DWORD (WINAPI *pfnGetAdaptersAddresses)(IN ULONG Family,
IN DWORD Flags,IN PVOID Reserved,
OUT PIP_ADAPTER_ADDRESSES pAdapterAddresses,
IN OUT PULONG pOutBufLen);
/*
my_gethwaddr - Windows version
@brief Retrieve MAC address from network hardware
@param[out] to MAC address exactly six bytes
@return Operation status
@retval 0 OK
@retval <>0 FAILED
*/
my_bool my_gethwaddr(uchar *to)
{
PIP_ADAPTER_ADDRESSES pAdapterAddresses;
PIP_ADAPTER_ADDRESSES pCurrAddresses;
IP_ADAPTER_ADDRESSES adapterAddresses;
ULONG address_len;
my_bool return_val= 1;
static pfnGetAdaptersAddresses fnGetAdaptersAddresses=
(pfnGetAdaptersAddresses)-1;
if(fnGetAdaptersAddresses == (pfnGetAdaptersAddresses)-1)
{
/* Get the function from the DLL */
fnGetAdaptersAddresses= (pfnGetAdaptersAddresses)
GetProcAddress(LoadLibrary("iphlpapi.dll"),
"GetAdaptersAddresses");
}
if (!fnGetAdaptersAddresses)
return 1; /* failed to get function */
address_len= sizeof (IP_ADAPTER_ADDRESSES);
/* Get the required size for the address data. */
if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, &adapterAddresses, &address_len)
== ERROR_BUFFER_OVERFLOW)
{
pAdapterAddresses= my_malloc(address_len, 0);
if (!pAdapterAddresses)
return 1; /* error, alloc failed */
}
else
pAdapterAddresses= &adapterAddresses; /* one is enough don't alloc */
/* Get the hardware info. */
if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, pAdapterAddresses, &address_len)
== NO_ERROR)
{
pCurrAddresses= pAdapterAddresses;
while (pCurrAddresses)
{
/* Look for ethernet cards. */
if (pCurrAddresses->IfType == IF_TYPE_ETHERNET_CSMACD)
{
/* check for a good address */
if (pCurrAddresses->PhysicalAddressLength < 6)
continue; /* bad address */
/* save 6 bytes of the address in the 'to' parameter */
memcpy(to, pCurrAddresses->PhysicalAddress, 6);
/* Network card found, we're done. */
return_val= 0;
break;
}
pCurrAddresses= pCurrAddresses->Next;
}
}
/* Clean up memory allocation. */
if (pAdapterAddresses != &adapterAddresses)
my_free(pAdapterAddresses, 0);
return return_val;
}
#else /* __FreeBSD__ || __linux__ || __WIN__ */
/* just fail */
my_bool my_gethwaddr(uchar *to __attribute__((unused)))
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment