[Commits] Rev 2948: my_gethwaddr() on Windows in http://bazaar.launchpad.net/~maria-captains/maria/5.1/

serg at askmonty.org serg at askmonty.org
Wed Sep 29 16:14:37 EEST 2010


At http://bazaar.launchpad.net/~maria-captains/maria/5.1/

------------------------------------------------------------
revno: 2948
revision-id: sergii at pisem.net-20100929131435-rno0t2zrhyespsv1
parent: sergii at pisem.net-20100929131148-j8fc39r4o52yssb1
committer: Sergei Golubchik <sergii at pisem.net>
branch nick: 5.1
timestamp: Wed 2010-09-29 15:14:35 +0200
message:
  my_gethwaddr() on Windows
  (using GetAdaptersInfo())
-------------- next part --------------
=== modified file 'mysys/CMakeLists.txt'
--- a/mysys/CMakeLists.txt	2010-06-25 13:09:45 +0000
+++ b/mysys/CMakeLists.txt	2010-09-29 13:14:35 +0000
@@ -49,6 +49,7 @@ SET(MYSYS_SOURCES  array.c charset-def.c
 
 IF(NOT SOURCE_SUBLIBS)
   ADD_LIBRARY(mysys ${MYSYS_SOURCES})
+  TARGET_LINK_LIBRARIES(mysys IPHLPAPI)
   
   INSTALL(TARGETS mysys DESTINATION lib/opt COMPONENT runtime) # TODO: Component?
 ENDIF(NOT SOURCE_SUBLIBS)

=== modified file 'mysys/my_gethwaddr.c'
--- a/mysys/my_gethwaddr.c	2010-07-02 18:30:47 +0000
+++ b/mysys/my_gethwaddr.c	2010-09-29 13:14:35 +0000
@@ -16,22 +16,21 @@
 /* get hardware address for an interface */
 /* if there are many available, any non-zero one can be used */
 
+#define DONT_DEFINE_VOID /* windows includes break if we do */
 #include "mysys_priv.h"
 #include <m_string.h>
 
 #ifndef MAIN
 
-#if defined(__FreeBSD__) || defined(__linux__)
 static my_bool memcpy_and_test(uchar *to, uchar *from, uint len)
 {
-  uint i, res=1;
+  uint i, res= 1;
 
-  for (i=0; i < len; i++)
+  for (i= 0; i < len; i++)
     if ((*to++= *from++))
-      res=0;
+      res= 0;
   return res;
 }
-#endif   /* FreeBSD || linux */
 
 #ifdef __FreeBSD__
 
@@ -47,7 +46,7 @@ my_bool my_gethwaddr(uchar *to)
   uchar  *buf, *next, *end, *addr;
   struct if_msghdr *ifm;
   struct sockaddr_dl *sdl;
-  int res=1, mib[6]={CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0};
+  int 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;
@@ -64,8 +63,8 @@ my_bool my_gethwaddr(uchar *to)
     if (ifm->ifm_type == RTM_IFINFO)
     {
       sdl = (struct sockaddr_dl *)(ifm + 1);
-      addr=LLADDR(sdl);
-      res=memcpy_and_test(to, addr, ETHER_ADDR_LEN);
+      addr= LLADDR(sdl);
+      res= memcpy_and_test(to, addr, ETHER_ADDR_LEN);
     }
   }
 
@@ -81,7 +80,7 @@ err:
 
 my_bool my_gethwaddr(uchar *to)
 {
-  int fd, res=1;
+  int fd, res= 1;
   struct ifreq ifr;
 
   fd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -93,7 +92,7 @@ my_bool my_gethwaddr(uchar *to)
 
   do {
     if (ioctl(fd, SIOCGIFHWADDR, &ifr) >= 0)
-      res=memcpy_and_test(to, (uchar *)&ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
+      res= memcpy_and_test(to, (uchar *)&ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN);
   } while (res && (errno == 0 || errno == ENODEV) && ifr.ifr_name[3]++ < '6');
 
   close(fd);
@@ -101,7 +100,39 @@ err:
   return res;
 }
 
-#else   /* FreeBSD elif linux */
+#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)
+      res= memcpy_and_test(to, info->Address, ETHER_ADDR_LEN);
+  }
+
+err:
+  return res;
+}
+
+#else   /* neither FreeBSD nor linux not Windows */
 /* just fail */
 my_bool my_gethwaddr(uchar *to __attribute__((unused)))
 {
@@ -120,7 +151,7 @@ int main(int argc __attribute__((unused)
     printf("my_gethwaddr failed with errno %d\n", errno);
     exit(1);
   }
-  for (i=0; i < sizeof(mac); i++)
+  for (i= 0; i < sizeof(mac); i++)
   {
     if (i) printf(":");
     printf("%02x", mac[i]);



More information about the commits mailing list