2011-06-30 17:37:13 +02:00
|
|
|
/*
|
2011-11-21 18:13:14 +01:00
|
|
|
Copyright (c) 2004, 2010, Oracle and/or its affiliates
|
2011-11-22 18:04:38 +01:00
|
|
|
Copyright (c) 2011, Monty Program Ab
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2006-12-23 20:17:15 +01:00
|
|
|
the Free Software Foundation; version 2 of the License.
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2019-05-11 20:29:06 +02:00
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA */
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
/* get hardware address for an interface */
|
|
|
|
/* if there are many available, any non-zero one can be used */
|
|
|
|
|
|
|
|
#include "mysys_priv.h"
|
|
|
|
#include <m_string.h>
|
|
|
|
|
2007-08-09 14:56:57 +02:00
|
|
|
#ifndef MAIN
|
|
|
|
|
2022-10-27 02:30:47 +02:00
|
|
|
#if defined(_AIX) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__linux__) || defined(__sun) || defined(_WIN32)
|
2011-10-04 15:07:55 +02:00
|
|
|
static my_bool memcpy_and_test(uchar *to, uchar *from, uint len)
|
|
|
|
{
|
|
|
|
uint i, res= 1;
|
|
|
|
|
|
|
|
for (i= 0; i < len; i++)
|
|
|
|
if ((*to++= *from++))
|
|
|
|
res= 0;
|
|
|
|
return res;
|
|
|
|
}
|
2022-02-02 15:41:13 +01:00
|
|
|
#endif
|
2004-02-27 20:30:08 +01:00
|
|
|
|
2022-10-27 01:52:17 +02:00
|
|
|
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <net/if_arp.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#else
|
2004-02-27 20:30:08 +01:00
|
|
|
#include <net/ethernet.h>
|
2022-10-27 01:52:17 +02:00
|
|
|
#endif
|
2004-02-27 20:30:08 +01:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
|
|
|
|
my_bool my_gethwaddr(uchar *to)
|
|
|
|
{
|
|
|
|
size_t len;
|
2011-10-04 15:07:55 +02:00
|
|
|
uchar *buf, *next, *end, *addr;
|
2004-02-27 20:30:08 +01:00
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct sockaddr_dl *sdl;
|
2011-10-04 15:07:55 +02:00
|
|
|
int res= 1, mib[6]= {CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
|
|
|
|
goto err;
|
|
|
|
if (!(buf = alloca(len)))
|
|
|
|
goto err;
|
|
|
|
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
end = buf + len;
|
|
|
|
|
|
|
|
for (next = buf ; res && next < end ; next += ifm->ifm_msglen)
|
|
|
|
{
|
|
|
|
ifm = (struct if_msghdr *)next;
|
|
|
|
if (ifm->ifm_type == RTM_IFINFO)
|
|
|
|
{
|
2011-10-04 15:07:55 +02:00
|
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
2011-10-18 21:50:17 +02:00
|
|
|
addr= (uchar *)LLADDR(sdl);
|
2011-10-04 15:07:55 +02:00
|
|
|
res= memcpy_and_test(to, addr, ETHER_ADDR_LEN);
|
2004-02-27 20:30:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:47:09 +02:00
|
|
|
#elif defined(_AIX) || defined(__linux__) || defined(__sun)
|
2004-02-27 20:30:08 +01:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/ioctl.h>
|
2011-10-04 15:07:55 +02:00
|
|
|
#include <net/if_arp.h>
|
|
|
|
#ifdef HAVE_SYS_SOCKIO_H
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ETHER_ADDR_LEN 6
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
my_bool my_gethwaddr(uchar *to)
|
|
|
|
{
|
2010-10-20 15:40:04 +02:00
|
|
|
int fd, res= 1;
|
2022-04-05 07:47:09 +02:00
|
|
|
#ifdef _AIX
|
|
|
|
struct ifhwaddr_req ifr[32];
|
|
|
|
#else
|
2011-10-04 15:07:55 +02:00
|
|
|
struct ifreq ifr[32];
|
2022-04-05 07:47:09 +02:00
|
|
|
#endif
|
2011-10-04 15:07:55 +02:00
|
|
|
struct ifconf ifc;
|
2013-03-25 10:13:42 +01:00
|
|
|
DBUG_ENTER("my_gethwaddr");
|
2011-10-04 15:07:55 +02:00
|
|
|
|
2022-04-05 07:47:09 +02:00
|
|
|
ifc.ifc_req= (struct ifreq *) ifr;
|
2011-10-04 15:07:55 +02:00
|
|
|
ifc.ifc_len= sizeof(ifr);
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
fd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd < 0)
|
2013-03-25 10:13:42 +01:00
|
|
|
{
|
|
|
|
DBUG_PRINT("error", ("socket() call failed with %d", errno));
|
2004-02-27 20:30:08 +01:00
|
|
|
goto err;
|
2013-03-25 10:13:42 +01:00
|
|
|
}
|
2004-02-27 20:30:08 +01:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
if (ioctl(fd, SIOCGIFCONF, (char*)&ifc) >= 0)
|
2010-10-20 15:40:04 +02:00
|
|
|
{
|
2011-10-04 15:07:55 +02:00
|
|
|
uint i;
|
|
|
|
for (i= 0; res && i < ifc.ifc_len / sizeof(ifr[0]); i++)
|
2010-10-20 15:40:04 +02:00
|
|
|
{
|
2022-11-12 14:14:23 +01:00
|
|
|
#if defined(_AIX) || defined(__linux__)
|
2022-08-31 05:00:16 +02:00
|
|
|
#if defined(__linux__)
|
2022-04-05 07:47:09 +02:00
|
|
|
#define HWADDR_DATA ifr[i].ifr_hwaddr.sa_data
|
|
|
|
#else
|
|
|
|
#define HWADDR_DATA ifr[i].ifr_hwaddr
|
|
|
|
#endif
|
2011-10-04 15:07:55 +02:00
|
|
|
if (ioctl(fd, SIOCGIFHWADDR, &ifr[i]) >= 0)
|
2022-04-05 07:47:09 +02:00
|
|
|
res= memcpy_and_test(to, (uchar *)&HWADDR_DATA,
|
2011-10-04 15:07:55 +02:00
|
|
|
ETHER_ADDR_LEN);
|
|
|
|
#else
|
|
|
|
/*
|
2013-03-25 10:13:42 +01:00
|
|
|
A bug in OpenSolaris used to prevent non-root from getting a mac
|
|
|
|
address: {no url. Oracle killed the old OpenSolaris bug database}
|
2011-10-04 15:07:55 +02:00
|
|
|
|
|
|
|
Thus, we'll use an alternative method and extract the address from the
|
|
|
|
arp table.
|
|
|
|
*/
|
|
|
|
struct arpreq arpr;
|
|
|
|
arpr.arp_pa= ifr[i].ifr_addr;
|
|
|
|
|
|
|
|
if (ioctl(fd, SIOCGARP, (char*)&arpr) >= 0)
|
|
|
|
res= memcpy_and_test(to, (uchar *)&arpr.arp_ha.sa_data,
|
|
|
|
ETHER_ADDR_LEN);
|
|
|
|
#endif
|
2010-10-20 15:40:04 +02:00
|
|
|
}
|
2011-10-04 15:07:55 +02:00
|
|
|
}
|
2004-02-27 20:30:08 +01:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
err:
|
2013-03-25 10:13:42 +01:00
|
|
|
DBUG_RETURN(res);
|
2004-02-27 20:30:08 +01:00
|
|
|
}
|
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
#elif defined(_WIN32)
|
|
|
|
#include <winsock2.h>
|
2009-09-11 22:42:39 +02:00
|
|
|
#include <iphlpapi.h>
|
2014-02-27 12:00:16 +01:00
|
|
|
#pragma comment(lib, "iphlpapi.lib")
|
2009-09-11 22:42:39 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
#define ETHER_ADDR_LEN 6
|
2009-09-11 22:42:39 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
my_bool my_gethwaddr(uchar *to)
|
|
|
|
{
|
|
|
|
my_bool res= 1;
|
2009-09-11 22:42:39 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
IP_ADAPTER_INFO *info= NULL;
|
|
|
|
ULONG info_len= 0;
|
2010-05-31 17:29:54 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
if (GetAdaptersInfo(info, &info_len) != ERROR_BUFFER_OVERFLOW)
|
|
|
|
goto err;
|
2010-05-31 17:29:54 +02:00
|
|
|
|
2011-11-23 19:29:39 +01:00
|
|
|
info= (IP_ADAPTER_INFO *)alloca(info_len);
|
2009-09-11 22:42:39 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
if (GetAdaptersInfo(info, &info_len) != NO_ERROR)
|
|
|
|
goto err;
|
2009-09-11 22:42:39 +02:00
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
while (info && res)
|
2009-09-11 22:42:39 +02:00
|
|
|
{
|
2011-10-04 15:07:55 +02:00
|
|
|
if (info->Type == MIB_IF_TYPE_ETHERNET &&
|
|
|
|
info->AddressLength == ETHER_ADDR_LEN)
|
2009-09-11 22:42:39 +02:00
|
|
|
{
|
2011-10-04 15:07:55 +02:00
|
|
|
res= memcpy_and_test(to, info->Address, ETHER_ADDR_LEN);
|
2010-05-31 17:29:54 +02:00
|
|
|
}
|
2011-10-19 20:51:01 +02:00
|
|
|
info = info->Next;
|
2009-09-11 22:42:39 +02:00
|
|
|
}
|
|
|
|
|
2011-10-04 15:07:55 +02:00
|
|
|
err:
|
|
|
|
return res;
|
2009-09-11 22:42:39 +02:00
|
|
|
}
|
|
|
|
|
2011-11-22 18:04:38 +01:00
|
|
|
#else /* unsupported system */
|
2004-02-27 20:30:08 +01:00
|
|
|
/* just fail */
|
|
|
|
my_bool my_gethwaddr(uchar *to __attribute__((unused)))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-20 00:28:42 +02:00
|
|
|
#else /* MAIN */
|
2004-02-27 20:30:08 +01:00
|
|
|
int main(int argc __attribute__((unused)),char **argv)
|
|
|
|
{
|
|
|
|
uchar mac[6];
|
|
|
|
uint i;
|
|
|
|
MY_INIT(argv[0]);
|
|
|
|
if (my_gethwaddr(mac))
|
|
|
|
{
|
|
|
|
printf("my_gethwaddr failed with errno %d\n", errno);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-10-04 15:07:55 +02:00
|
|
|
for (i= 0; i < sizeof(mac); i++)
|
2004-02-27 20:30:08 +01:00
|
|
|
{
|
|
|
|
if (i) printf(":");
|
|
|
|
printf("%02x", mac[i]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|