Boost logo

Boost Users :

From: Vinzenz Feenstra (evilissimo_at_[hidden])
Date: 2008-08-27 02:20:34


Jean-Sebastien Stoezel schrieb:
> Hello,
>
> How to get the MAC address of an ethernet interface, using the boost
> C++ library?

Well it's not implemented in boost. I can show you an example how to get
it on linux and windows (I had to get it for a library I am working on).
However that's just the rough way, I'm not in need to get it for a
specific interface, but with this you'll find the needed info faster ;)

Linux:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netpacket/packet.h>
#include <ifaddrs.h>

ifaddrs * ifap = 0;
if(getifaddrs(&ifap) == 0)
{
     ifaddrs * iter = ifap;
     while(iter)
     {
         sockaddr_ll * sal =
                    reinterpret_cast<sockaddr_ll*>(iter->ifa_addr);
        if(sal->sll_family == AF_PACKET)
        {
                 // get the mac bytes
                // copy(sal->sll_addr,
                 // sal->sll_addr+sal->sll_hallen,
                 // buffer);
        }
         iter = iter->ifa_next;
     }
     freeifaddrs(ifap);
}

Windows:
#include <winsock2.h>
#include <iphlpapi.h>

std::vector<boost::uint8_t> buf;
DWORD bufLen = 0;
GetAdaptersAddresses(0, 0, 0, 0, &bufLen);
if(bufLen)
{
     buf.resize(bufLen, 0);
     IP_ADAPTER_ADDRESSES * ptr =
                     reinterpret_cast<IP_ADAPTER_ADDRESSES*>(&buf[0]);
     DWORD err = GetAdaptersAddresses(0, 0, 0, ptr, &bufLen);
     if(err == NO_ERROR)
     {
          while(ptr)
         {
              if(ptr->PhysicalAddressLength)
              {
                 // get the mac bytes
                // copy(ptr->PhysicalAddress,
                 // ptr->PhysicalAddress+ptr->PhysicalAddressLength,
                 // buffer);
              }
              ptr = ptr->Next;
          }
     }
}

Regards,
Vinzenz


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net