1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/* Copyright (C) 2004 MySQL AB
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
the Free Software Foundation; version 2 of the License.
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 */
#include "mysys_priv.h"
#include <mystrings/m_string.h>
#ifndef MAIN
#if defined(__FreeBSD__) || defined(__linux__)
static bool memcpy_and_test(unsigned char *to, unsigned char *from, uint32_t len)
{
uint32_t i, res=1;
for (i=0; i < len; i++)
if ((*to++= *from++))
res=0;
return res;
}
#endif /* FreeBSD || linux */
#ifdef __FreeBSD__
#include <net/ethernet.h>
#include <sys/sysctl.h>
#include <net/route.h>
#include <net/if.h>
#include <net/if_dl.h>
bool my_gethwaddr(unsigned char *to)
{
size_t len;
unsigned char *buf, *next, *end, *addr;
struct if_msghdr *ifm;
struct sockaddr_dl *sdl;
int i, res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
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)
{
sdl = (struct sockaddr_dl *)(ifm + 1);
addr=LLADDR(sdl);
res=memcpy_and_test(to, addr, ETHER_ADDR_LEN);
}
}
err:
return res;
}
#elif defined(__linux__)
#include <net/if.h>
#include <sys/ioctl.h>
#include <net/ethernet.h>
bool my_gethwaddr(unsigned char *to)
{
int fd, res=1;
struct ifreq ifr;
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0)
goto err;
memset(&ifr, 0, sizeof(ifr));
my_stpncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
do {
if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
res=memcpy_and_test(to, (unsigned char *)&ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
} while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6');
close(fd);
err:
return res;
}
#else /* FreeBSD elif linux */
/* just fail */
bool my_gethwaddr(unsigned char *to __attribute__((unused)))
{
return 1;
}
#endif
#else /* MAIN */
int main(int argc __attribute__((unused)),char **argv)
{
unsigned char mac[6];
uint32_t i;
MY_INIT(argv[0]);
if (my_gethwaddr(mac))
{
printf("my_gethwaddr failed with errno %d\n", errno);
exit(1);
}
for (i=0; i < sizeof(mac); i++)
{
if (i) printf(":");
printf("%02x", mac[i]);
}
printf("\n");
return 0;
}
#endif
|