my_gethwaddr.c 4.27 KB
Newer Older
unknown's avatar
unknown committed
1
/* Copyright (C) 2004 MySQL AB
unknown's avatar
unknown committed
2 3 4

   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
unknown's avatar
unknown committed
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7 8 9 10 11 12 13 14 15 16 17 18

   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
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/* get hardware address for an interface */
/* if there are many available, any non-zero one can be used */

19
#define DONT_DEFINE_VOID /* windows includes break if we do */
unknown's avatar
unknown committed
20 21 22
#include "mysys_priv.h"
#include <m_string.h>

unknown's avatar
unknown committed
23 24
#ifndef MAIN

25 26 27 28 29 30 31 32 33 34
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;
}

unknown's avatar
unknown committed
35
#if defined(__APPLE__) || defined(__FreeBSD__)
unknown's avatar
unknown committed
36 37 38 39 40 41 42 43 44
#include <net/ethernet.h>
#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;
45
  uchar  *buf, *next, *end, *addr;
unknown's avatar
unknown committed
46 47
  struct if_msghdr *ifm;
  struct sockaddr_dl *sdl;
48
  int res= 1, mib[6]= {CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
unknown's avatar
unknown committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  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)
    {
64
      sdl = (struct sockaddr_dl *)(ifm + 1);
unknown's avatar
unknown committed
65
      addr= (uchar *)LLADDR(sdl);
66
      res= memcpy_and_test(to, addr, ETHER_ADDR_LEN);
unknown's avatar
unknown committed
67 68 69 70 71 72 73
    }
  }

err:
  return res;
}

74
#elif defined(__linux__) || defined(__sun__)
unknown's avatar
unknown committed
75 76
#include <net/if.h>
#include <sys/ioctl.h>
77 78 79 80 81 82
#include <net/if_arp.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

#define ETHER_ADDR_LEN 6
unknown's avatar
unknown committed
83 84 85

my_bool my_gethwaddr(uchar *to)
{
86
  int fd, res= 1;
87 88 89 90 91
  struct ifreq ifr[32];
  struct ifconf ifc;

  ifc.ifc_req= ifr;
  ifc.ifc_len= sizeof(ifr);
unknown's avatar
unknown committed
92 93 94 95 96

  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0)
    goto err;

97
  if (ioctl(fd, SIOCGIFCONF, (char*)&ifc) >= 0)
98
  {
99 100
    uint i;
    for (i= 0; res && i < ifc.ifc_len / sizeof(ifr[0]); i++)
101
    {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#ifdef SIOCGIFHWADDR
      if (ioctl(fd, SIOCGIFHWADDR, &ifr[i]) >= 0)
        res= memcpy_and_test(to, (uchar *)&ifr[i].ifr_hwaddr.sa_data,
                             ETHER_ADDR_LEN);
#else
      /*
        A bug in OpenSolaris prevents non-root from getting a mac address:
        http://bugs.opensolaris.org/bugdatabase/view_bug.do?bug_id=4720634

        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
121
    }
122
  }
unknown's avatar
unknown committed
123 124 125 126 127 128

  close(fd);
err:
  return res;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#elif defined(_WIN32)
#include <winsock2.h>
#include <iphlpapi.h>

#define ETHER_ADDR_LEN 6

my_bool my_gethwaddr(uchar *to)
{
  my_bool res= 1;

  IP_ADAPTER_INFO *info= NULL;
  ULONG info_len= 0;

  if (GetAdaptersInfo(info, &info_len) != ERROR_BUFFER_OVERFLOW)
    goto err;

  info= alloca(info_len);

  if (GetAdaptersInfo(info, &info_len) != NO_ERROR)
    goto err;

  while (info && res)
  {
    if (info->Type == MIB_IF_TYPE_ETHERNET &&
        info->AddressLength == ETHER_ADDR_LEN)
154
    {
155
      res= memcpy_and_test(to, info->Address, ETHER_ADDR_LEN);
156 157
    }
    info = info->Next;
158 159 160 161 162 163 164
  }

err:
  return res;
}

#else   /* neither FreeBSD nor linux not Windows */
unknown's avatar
unknown committed
165 166 167 168 169 170 171
/* just fail */
my_bool my_gethwaddr(uchar *to __attribute__((unused)))
{
  return 1;
}
#endif

172
#else /* MAIN */
unknown's avatar
unknown committed
173 174 175 176 177 178 179 180 181 182
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);
  }
183
  for (i= 0; i < sizeof(mac); i++)
unknown's avatar
unknown committed
184 185 186 187 188 189 190 191 192
  {
    if (i) printf(":");
    printf("%02x", mac[i]);
  }
  printf("\n");
  return 0;
}
#endif