Reputation: 18451
I´d wrapped up the following code to get the computer MAC address (Linux Ubuntu) and to print it using a custom hexadecimal string:
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <stdexcept>
#include <unistd.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
std::string convertToHex(std::string str)
{
std::stringstream outText;
for(unsigned int i = 0; i < str.size(); i++ )
{
outText << std::hex << std::setw(2) << std::setfill('0') << (0xFF & static_cast<char>(str[i]));
if (i != (str.size() - 1))
outText << ":";
}
return outText.str();
}
std::string getMacId()
{
struct ifreq ifr;
struct ifreq *IFR;
struct ifconf ifc;
char buf[1024];
int s, i;
std::string macAddr("");
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s==-1)
{
return "";
}
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(s, SIOCGIFCONF, &ifc);
IFR = ifc.ifc_req;
int ok = 0;
for (i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; IFR++)
{
strcpy(ifr.ifr_name, IFR->ifr_name);
if (ioctl(s, SIOCGIFFLAGS, &ifr) == 0)
{
if (! (ifr.ifr_flags & IFF_LOOPBACK))
{
if (ioctl(s, SIOCGIFHWADDR, &ifr) == 0)
{
ok = 1;
break;
}
}
}
}
close(s);
std::stringstream data;
for (auto &c : ifr.ifr_addr.sa_data)
data << c;
std::string ret = data.str();
std::cout << ret << std::endl;
return ret;
}
void showMacId()
{
std::string mac = getMacId();
std::string hexmac = convertToHex(mac);
std::cout << hexmac << std::endl;
}
int main()
{
std::cout << "Pass1: " << std::endl;
showMacId();
std::cout << "Pass2: " << std::endl;
showMacId();
std::cout << "Pass3: " << std::endl;
showMacId();
std::cout << "Pass4: " << std::endl;
showMacId();
}
The problem is that I´m getting different outputs on every read. This is the result running the program:
Pass1:
.)� W��
2e:02:00:00:04:29:80:20:57:82:42:08:80:20
Pass2:
p�ЕĿ�wq���Ŀ
70:b7:d0:95:c4:bf:aa:77:71:b7:80:95:c4:bf
Pass3:
p�ЕĿ�wq���Ŀ
70:b7:d0:95:c4:bf:aa:77:71:b7:80:95:c4:bf
Pass4:
p�ЕĿ�wq���Ŀ
70:b7:d0:95:c4:bf:aa:77:71:b7:80:95:c4:bf
And if I run the program again I get different results:
Pass1:
.)� W��
2e:02:00:00:04:29:80:20:57:82:42:08:80:20
Pass2:
q��
���wr�P
��
71:b7:a0:0a:93:bf:aa:77:72:b7:50:0a:93:bf
Pass3:
q��
���wr�P
��
71:b7:a0:0a:93:bf:aa:77:72:b7:50:0a:93:bf
Pass4:
q��
���wr�P
��
71:b7:a0:0a:93:bf:aa:77:72:b7:50:0a:93:bf
So, what am I doing wrong here?
Upvotes: 0
Views: 714
Reputation: 36352
sizeof(buf)
looks dangerous. Use 1024 instead; I'm not absolutely certain you don't ask for sizeof(char*)
this way.
You must check that
ioctl(s, SIOCGIFCONF, &ifc);
doesn't return -1!
All in all, this seems a very old-school method of getting the MAC address.
Why don't you just read /sys/class/net/<devicename>/address
? I could pretty much rely on sysfs being there on any linux system I've encountered, and it's the failsafe, clear, portable thing to do. To find your devices, just list the /sys/calls/net/
directory.
EDIT I was asked to give an example; I really don't know how to do this, it seems so straightforward:
#include <iostream>
#include <fstream>
int main (int argc, char** argv)
{
std::ifstream inp ("/sys/class/net/enp0s25/address", std::ifstream::in);
for(int bytecounter = 0; bytecounter < 6; bytecounter++)
{
unsigned int byte;
inp >>std::hex >> byte;
inp.get(); //drop the :
std::cout <<byte;
if(bytecounter < 5)
std::cout << ":";
}
inp.close();
return 0;
}
Upvotes: 2
Reputation: 16334
I am not sure why the "MAC" addresses you are printing are that long (usually they are 48 bit long), but I guess your problem is that you do not initialize your structs and arrays with zeros.
This should help (given that you use C++11 anyway):
struct ifreq ifr = {0};
struct ifconf ifc = {0};
char buf[1024] = {0};
Upvotes: 0