Thread: Get the MAC address.. ??

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Get the MAC address.. ??

    I would like to know if there is a simple way to use in order to retreive the machain MAC address..

    i have found this:

    Code:
    #include <sys/ioctl.h> 
    #include <net/if.h>  
    #include <unistd.h> 
    #include <netinet/in.h> 
    #include <string.h> 
     
    int main() 
    { 
        ifreq ifr; 
        ifconf ifc; 
        char buf[1024]; 
        bool success = false; 
     
        int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); 
        if (sock == -1) { /* handle error*/ }; 
     
        ifc.ifc_len = sizeof(buf); 
        ifc.ifc_buf = buf; 
        if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) { /* handle error */ } 
     
        ifreq* it = ifc.ifc_req; 
        const ifreq* const end = it + (ifc.ifc_len / sizeof(ifreq)); 
     
        for (; it != end; ++it) { 
            strcpy(ifr.ifr_name, it->ifr_name); 
            if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) { 
                    if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback 
                            if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) { 
                                    success = true; 
                                    break; 
                            } 
                    } 
            } 
            else { /* handle error */ } 
        } 
     
        unsigned char mac_address[6]; 
     
        if (success) memcpy(mac_address, ifr.ifr_hwaddr.sa_data, 6); 
    }
    any other way??

    waiting..

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you have a DNS library (eg. the one in windows) and know the IP address, you could use the SendArp() function....
    Code:
    // this is windows stuff
    #include <iphlpapi.h>
    
    // get a host's mac address
    BOOL GetMacAddr(SOCKADDR Host, PBYTE Mac)
      { ULONG     smac = 6;           // size of mac address
        // get mac address
        if (SendARP(((PSOCKADDR_IN) &Host)->sin_addr.S_un.S_addr,0,
                                      (PULONG)Mac,&smac) == NO_ERROR)
          return 1;
        return 0; }
    This has the advantage of getting the MAC address at run time, using an open UDP port.
    Last edited by CommonTater; 10-31-2010 at 06:12 AM.

  3. #3
    Registered User matrixx333's Avatar
    Join Date
    Mar 2009
    Posts
    67
    Not sure if you've seen this thread yet, but it may provide some help:

    ioctl request to get the HW address

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    Talking

    Ya, that was great.

    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  3. Spoof MAC Address.
    By eXeCuTeR in forum C Programming
    Replies: 10
    Last Post: 02-07-2008, 05:35 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  5. How to spoof a MAC address?
    By Lateralus in forum Linux Programming
    Replies: 3
    Last Post: 06-26-2005, 07:50 PM