~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_gethwaddr.cc

  • Committer: Brian Aker
  • Date: 2008-12-05 17:55:00 UTC
  • Revision ID: brian@tangent.org-20081205175500-g3ja7dl0vfgjtrob
Random cleanup, removed dead mysys call for hw address creation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2004 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
/* get hardware address for an interface */
17
 
/* if there are many available, any non-zero one can be used */
18
 
 
19
 
#include "mysys_priv.h"
20
 
#include <mystrings/m_string.h>
21
 
 
22
 
#ifndef MAIN
23
 
 
24
 
#if defined(__FreeBSD__) || defined(__linux__)
25
 
static bool memcpy_and_test(unsigned char *to, unsigned char *from, uint32_t len)
26
 
{
27
 
  uint32_t i, res=1;
28
 
 
29
 
  for (i=0; i < len; i++)
30
 
    if ((*to++= *from++))
31
 
      res=0;
32
 
  return res;
33
 
}
34
 
#endif   /* FreeBSD || linux */
35
 
 
36
 
#ifdef __FreeBSD__
37
 
 
38
 
#include <net/ethernet.h>
39
 
#include <sys/sysctl.h>
40
 
#include <net/route.h>
41
 
#include <net/if.h>
42
 
#include <net/if_dl.h>
43
 
 
44
 
bool my_gethwaddr(unsigned char *to)
45
 
{
46
 
  size_t len;
47
 
  unsigned char  *buf, *next, *end, *addr;
48
 
  struct if_msghdr *ifm;
49
 
  struct sockaddr_dl *sdl;
50
 
  int i, res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
51
 
 
52
 
  if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
53
 
    goto err;
54
 
  if (!(buf = alloca(len)))
55
 
    goto err;
56
 
  if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
57
 
    goto err;
58
 
 
59
 
  end = buf + len;
60
 
 
61
 
  for (next = buf ; res && next < end ; next += ifm->ifm_msglen)
62
 
  {
63
 
    ifm = (struct if_msghdr *)next;
64
 
    if (ifm->ifm_type == RTM_IFINFO)
65
 
    {
66
 
      sdl = (struct sockaddr_dl *)(ifm + 1);
67
 
      addr=LLADDR(sdl);
68
 
      res=memcpy_and_test(to, addr, ETHER_ADDR_LEN);
69
 
    }
70
 
  }
71
 
 
72
 
err:
73
 
  return res;
74
 
}
75
 
 
76
 
#elif defined(__linux__)
77
 
 
78
 
#include <net/if.h>
79
 
#include <sys/ioctl.h>
80
 
#include <net/ethernet.h>
81
 
 
82
 
bool my_gethwaddr(unsigned char *to)
83
 
{
84
 
  int fd, res=1;
85
 
  struct ifreq ifr;
86
 
 
87
 
  fd = socket(AF_INET, SOCK_DGRAM, 0);
88
 
  if (fd < 0)
89
 
    goto err;
90
 
 
91
 
  memset(&ifr, 0, sizeof(ifr));
92
 
  my_stpncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
93
 
 
94
 
  do {
95
 
    if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
96
 
      res=memcpy_and_test(to, (unsigned char *)&ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
97
 
  } while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6');
98
 
 
99
 
  close(fd);
100
 
err:
101
 
  return res;
102
 
}
103
 
 
104
 
#else   /* FreeBSD elif linux */
105
 
/* just fail */
106
 
bool my_gethwaddr(unsigned char *)
107
 
{
108
 
  return 1;
109
 
}
110
 
#endif
111
 
 
112
 
#else /* MAIN */
113
 
int main(int argc __attribute__((unused)),char **argv)
114
 
{
115
 
  unsigned char mac[6];
116
 
  uint32_t i;
117
 
  MY_INIT(argv[0]);
118
 
  if (my_gethwaddr(mac))
119
 
  {
120
 
    printf("my_gethwaddr failed with errno %d\n", errno);
121
 
    exit(1);
122
 
  }
123
 
  for (i=0; i < sizeof(mac); i++)
124
 
  {
125
 
    if (i) printf(":");
126
 
    printf("%02x", mac[i]);
127
 
  }
128
 
  printf("\n");
129
 
  return 0;
130
 
}
131
 
#endif
132