ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Does anyone know how to get the MAC Address on Linux? I want to make a program (C++) that only works with a determined MAC Address... if it isn't the right one, it must not work.
//
// 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] );
I just copied the above MAC Address program .
It works fine .But.........
With valgrind it is giving the following error.
==14100== Syscall param socketcall.sendto(msg) points to uninitialised byte(s)
==14100== at 0x1B9D64E1: sendto (in /lib/tls/libc.so.6)
==14100== by 0x1B9F00D3: if_nameindex (in /lib/tls/libc.so.6)
==14100== by 0x8048651: GetSvrMacAddress (ex.c:43)
==14100== by 0x804875C: main (ex.c:86)
==14100== Address 0x52BFE345 is on thread 1's stack
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.