19 #include "mysys_priv.h"
26 #include <net/ethernet.h>
27 #include <sys/sysctl.h>
28 #include <net/route.h>
30 #include <net/if_dl.h>
32 my_bool my_gethwaddr(uchar *
to)
35 char *
buf, *next, *end;
36 struct if_msghdr *ifm;
37 struct sockaddr_dl *sdl;
38 int res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
39 char zero_array[ETHER_ADDR_LEN] = {0};
41 if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1)
43 if (!(buf = alloca(len)))
45 if (sysctl(mib, 6, buf, &len, NULL, 0) < 0)
50 for (next = buf ; res && next < end ; next += ifm->ifm_msglen)
52 ifm = (
struct if_msghdr *)next;
53 if (ifm->ifm_type == RTM_IFINFO)
55 sdl= (
struct sockaddr_dl *)(ifm + 1);
56 memcpy(to, LLADDR(sdl), ETHER_ADDR_LEN);
57 res= memcmp(to, zero_array, ETHER_ADDR_LEN) ? 0 : 1;
68 #include <sys/ioctl.h>
69 #include <net/ethernet.h>
71 my_bool my_gethwaddr(uchar *to)
75 char zero_array[ETHER_ADDR_LEN] = {0};
77 fd = socket(AF_INET, SOCK_DGRAM, 0);
81 memset(&ifr, 0,
sizeof(ifr));
82 strnmov(ifr.ifr_name,
"eth0",
sizeof(ifr.ifr_name) - 1);
86 if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
88 memcpy(to, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
89 res= memcmp(to, zero_array, ETHER_ADDR_LEN) ? 0 : 1;
91 }
while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ <
'6');
98 #elif defined(__WIN__)
109 #include <iphlpapi.h>
117 typedef DWORD (WINAPI *pfnGetAdaptersAddresses)(IN ULONG Family,
118 IN DWORD Flags,IN PVOID Reserved,
119 OUT PIP_ADAPTER_ADDRESSES pAdapterAddresses,
120 IN OUT PULONG pOutBufLen);
133 my_bool my_gethwaddr(uchar *to)
135 PIP_ADAPTER_ADDRESSES pAdapterAddresses;
136 PIP_ADAPTER_ADDRESSES pCurrAddresses;
137 IP_ADAPTER_ADDRESSES adapterAddresses;
139 my_bool return_val= 1;
140 static pfnGetAdaptersAddresses fnGetAdaptersAddresses=
141 (pfnGetAdaptersAddresses)-1;
143 if(fnGetAdaptersAddresses == (pfnGetAdaptersAddresses)-1)
146 fnGetAdaptersAddresses= (pfnGetAdaptersAddresses)
147 GetProcAddress(LoadLibrary(
"iphlpapi.dll"),
148 "GetAdaptersAddresses");
150 if (!fnGetAdaptersAddresses)
152 address_len=
sizeof (IP_ADAPTER_ADDRESSES);
155 if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, &adapterAddresses, &address_len)
156 == ERROR_BUFFER_OVERFLOW)
158 pAdapterAddresses= my_malloc(address_len, 0);
159 if (!pAdapterAddresses)
163 pAdapterAddresses= &adapterAddresses;
166 if (fnGetAdaptersAddresses(AF_UNSPEC, 0, 0, pAdapterAddresses, &address_len)
169 pCurrAddresses= pAdapterAddresses;
171 while (pCurrAddresses)
174 if (pCurrAddresses->IfType == IF_TYPE_ETHERNET_CSMACD)
177 if (pCurrAddresses->PhysicalAddressLength < 6)
181 memcpy(to, pCurrAddresses->PhysicalAddress, 6);
187 pCurrAddresses= pCurrAddresses->Next;
192 if (pAdapterAddresses != &adapterAddresses)
193 my_free(pAdapterAddresses);
200 my_bool my_gethwaddr(uchar *to __attribute__((unused)))
207 int main(
int argc __attribute__((unused)),
char **argv)
212 if (my_gethwaddr(mac))
214 printf(
"my_gethwaddr failed with errno %d\n", errno);
217 for (i=0; i <
sizeof(mac); i++)
220 printf(
"%02x", mac[i]);