[PC-BSD Commits] r2058 - pcbsd/trunk/libpcbsd

svn at pcbsd.org svn at pcbsd.org
Mon Jun 9 06:50:48 PDT 2008


Author: tim
Date: 2008-06-09 06:50:48 -0700 (Mon, 09 Jun 2008)
New Revision: 2058

Modified:
   pcbsd/trunk/libpcbsd/netif.cpp
Log:
Implemented macAsString

Modified: pcbsd/trunk/libpcbsd/netif.cpp
===================================================================
--- pcbsd/trunk/libpcbsd/netif.cpp	2008-06-09 10:59:27 UTC (rev 2057)
+++ pcbsd/trunk/libpcbsd/netif.cpp	2008-06-09 13:50:48 UTC (rev 2058)
@@ -25,8 +25,11 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <sys/ioctl.h>
+#include <sys/sysctl.h>
 
 #include <net/if.h>
+#include <net/if_dl.h>
+
 #include <ifaddrs.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -81,3 +84,38 @@
 
    return QString(inet_ntoa(in));
 }
+
+QString NetworkInterface::macAsString()
+{
+   int mib[6];
+   size_t len;
+   char *buf;
+   struct sockaddr_dl *sdl;
+   char *ptr;
+
+   mib[0] = CTL_NET;
+   mib[1] = AF_ROUTE;
+   mib[2] = 0;
+   mib[3] = AF_LINK;
+   mib[4] = NET_RT_IFLIST;
+   mib[5] = if_nametoindex(name);
+
+   //First find the size of the return, so we can adjust buf accordingly
+   sysctl(mib, 6, NULL, &len, NULL, 0);
+   buf = (char *) malloc(len);
+   sysctl(mib, 6, buf, &len, NULL, 0);
+
+   sdl = (sockaddr_dl *)(((if_msghdr *)buf)+1);
+   ptr = (char *) LLADDR(sdl);
+   
+   //sprintf(ptr, "%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5));
+
+   QString mac;
+   for (uint i=0; i < 6; i++)
+   {
+      mac += QString::number(*(ptr+i), 16).right(2).rightJustify(2, '0');
+      if (i<5) mac += ":";
+   }
+   
+   return mac;
+}



More information about the Commits mailing list