mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysys / my_gethwaddr.c
blob6e49169a0b84e93479b7939a9c415b90e6ee7ef5
1 /*
2 Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 /* get hardware address for an interface */
19 /* if there are many available, any non-zero one can be used */
21 #include "mysys_priv.h"
22 #include <m_string.h>
24 #ifndef MAIN
26 #ifdef __FreeBSD__
28 #include <net/ethernet.h>
29 #include <sys/sysctl.h>
30 #include <net/route.h>
31 #include <net/if.h>
32 #include <net/if_dl.h>
34 my_bool my_gethwaddr(uchar *to)
36 size_t len;
37 char *buf, *next, *end;
38 struct if_msghdr *ifm;
39 struct sockaddr_dl *sdl;
40 int res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
41 char zero_array[ETHER_ADDR_LEN] = {0};
43 if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
44 goto err;
45 if (!(buf = alloca(len)))
46 goto err;
47 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
48 goto err;
50 end = buf + len;
52 for (next = buf ; res && next < end ; next += ifm->ifm_msglen)
54 ifm = (struct if_msghdr *)next;
55 if (ifm->ifm_type == RTM_IFINFO)
57 sdl= (struct sockaddr_dl *)(ifm + 1);
58 memcpy(to, LLADDR(sdl), ETHER_ADDR_LEN);
59 res= memcmp(to, zero_array, ETHER_ADDR_LEN) ? 0 : 1;
63 err:
64 return res;
67 #elif __linux__
69 #include <net/if.h>
70 #include <sys/ioctl.h>
71 #include <net/ethernet.h>
73 my_bool my_gethwaddr(uchar *to)
75 int fd, res= 1;
76 struct ifreq ifr;
77 char zero_array[ETHER_ADDR_LEN] = {0};
79 fd = socket(AF_INET, SOCK_DGRAM, 0);
80 if (fd < 0)
81 goto err;
83 bzero(&ifr, sizeof(ifr));
84 strnmov(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
88 if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
90 memcpy(to, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
91 res= memcmp(to, zero_array, ETHER_ADDR_LEN) ? 0 : 1;
93 } while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6');
95 close(fd);
96 err:
97 return res;
100 #else /* FreeBSD elif linux */
101 /* just fail */
102 my_bool my_gethwaddr(uchar *to __attribute__((unused)))
104 return 1;
106 #endif
108 #else /* MAIN */
109 int main(int argc __attribute__((unused)),char **argv)
111 uchar mac[6];
112 uint i;
113 MY_INIT(argv[0]);
114 if (my_gethwaddr(mac))
116 printf("my_gethwaddr failed with errno %d\n", errno);
117 exit(1);
119 for (i=0; i < sizeof(mac); i++)
121 if (i) printf(":");
122 printf("%02x", mac[i]);
124 printf("\n");
125 return 0;
127 #endif