LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2005, 10:00 AM   #1
silentangel
LQ Newbie
 
Registered: May 2005
Posts: 9

Rep: Reputation: 0
Getting MAC Address with C++ (Linux)


Hi folks

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.

Thanks in advance :-)
 
Old 05-19-2005, 11:47 AM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

I recently needed to do this in C. Here is a small utility that you can use as a model in your C++ program:

[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 );
}
[\CODE]

Have fun!
 
1 members found this post helpful.
Old 05-20-2005, 05:30 AM   #3
silentangel
LQ Newbie
 
Registered: May 2005
Posts: 9

Original Poster
Rep: Reputation: 0
thanks a lot rstewart! it helped me a lot

now I'm searching how to get the HD serial number
 
Old 05-20-2005, 05:37 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Look in /proc/ide
 
Old 05-24-2005, 06:09 AM   #5
FredKroket
LQ Newbie
 
Registered: May 2005
Posts: 5

Rep: Reputation: 0
Code:
std::string getDiskSerialNumber()
{
  struct hd_driveid id;
  
  int fd;
  if (fd = open("/dev/hda", O_RDONLY) == -1)
    exit(1) //error opening /dev/hda
   
  if (ioctl(fd, HDIO_GET_IDENTITY, &id) == -1)
   exit(2) //error retrieving indentification
   
  return std::string((const char*)id.serial_no);
}
 
Old 02-17-2006, 05:07 AM   #6
yashwantpinge
LQ Newbie
 
Registered: Nov 2005
Posts: 17

Rep: Reputation: 0
Hi ,

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


How to fix this error ....?

Regards,
Yashwant Pinge
 
Old 02-17-2006, 06:49 AM   #7
shydra
Member
 
Registered: Nov 2005
Posts: 31

Rep: Reputation: 15
Quote:
Originally Posted by silentangel
now I'm searching how to get the HD serial number

Funny.

There is a book in PDF format, "Advanced Programming in Linux "
There is completely explain a lot of the things.
 
Old 11-03-2012, 02:36 PM   #8
tibi38
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
Thanks a lot for this code steward,
but is it safe and read the MAC from the adapter - hw?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
Linux: Determining MAC address from C Mad_C Programming 9 02-11-2005 04:27 AM
DHCP Server MAC Address found, IP address not assigned wmburke Linux - Wireless Networking 17 11-17-2004 10:33 AM
MAC address cloning in Linux sb73542 Linux - Security 1 08-02-2003 07:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration