Thread: How to get my own mac address (c programming)

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    5

    Question How to get my own mac address (c programming)

    Hi,

    I want to write a little programm in order to make an inventory of my machines. I want to start this programm at startup, and it should write some information in a mysqldb : ipaddress, date etc...
    I would like to use the macid as primary key for this table, but I could'nt find a function to get the macid from the machine that is executing the programm...

    Can somebody tell me how to do that ? I couldn't find anything in the Internet...

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    try something like this - code by rstewart www.linuxquestions.org
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <net/if.h>
    #include <sys/ioctl.h>
    
    //
    // Global public data
    //
    unsigned char cMacAddr[8]; // Server's MAC address
    
    static int GetSvrMacAddress( char *pIface )
    {
    int nSD; // Socket descriptor
    struct ifreq sIfReq; // Interface request
    struct if_nameindex *pIfList; // Ptr to interface name index
    struct if_nameindex *pListSave; // Ptr to interface name index
    
    //
    // Initialize this function
    //
    pIfList = (struct if_nameindex *)NULL;
    pListSave = (struct if_nameindex *)NULL;
    #ifndef SIOCGIFADDR
    // The kernel does not support the required ioctls
    return( 0 );
    #endif
    
    //
    // Create a socket that we can use for all of our ioctls
    //
    nSD = socket( PF_INET, SOCK_STREAM, 0 );
    if ( nSD < 0 )
    {
    // Socket creation failed, this is a fatal error
    printf( "File %s: line %d: Socket failed\n", __FILE__, __LINE__ );
    return( 0 );
    }
    
    //
    // Obtain a list of dynamically allocated structures
    //
    pIfList = pListSave = if_nameindex();
    
    //
    // Walk thru the array returned and query for each interface's
    // address
    //
    for ( pIfList; *(char *)pIfList != 0; pIfList++ )
    {
    //
    // Determine if we are processing the interface that we
    // are interested in
    //
    if ( strcmp(pIfList->if_name, pIface) )
    // Nope, check the next one in the list
    continue;
    strncpy( sIfReq.ifr_name, pIfList->if_name, IF_NAMESIZE );
    
    //
    // Get the MAC address for this interface
    //
    if ( ioctl(nSD, SIOCGIFHWADDR, &sIfReq) != 0 )
    {
    // We failed to get the MAC address for the interface
    printf( "File %s: line %d: Ioctl failed\n", __FILE__, __LINE__ );
    return( 0 );
    }
    memmove( (void *)&cMacAddr[0], (void *)&sIfReq.ifr_ifru.ifru_hwaddr.sa_data[0], 6 );
    break;
    }
    
    //
    // Clean up things and return
    //
    if_freenameindex( pListSave );
    close( nSD );
    return( 1 );
    }
    
    int main( int argc, char * argv[] )
    {
    //
    // Initialize this program
    //
    bzero( (void *)&cMacAddr[0], sizeof(cMacAddr) );
    if ( !GetSvrMacAddress("eth0") )
    {
    // We failed to get the local host's MAC address
    printf( "Fatal error: Failed to get local host's MAC address\n" );
    }
    printf( "HWaddr %02X:%02X:%02X:%02X:%02X:%02X\n",
    cMacAddr[0], cMacAddr[1], cMacAddr[2],
    cMacAddr[3], cMacAddr[4], cMacAddr[5] );
    
    //
    // And exit
    //
    exit( 0 );
    }

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