I solved it by using ioctl to enumerate interfaces and then ioctl to
get the HW address like so (for future reference and if it helps
anyone):
struct ifreq *ifr;
struct ifconf ifc;
int s, i;
int numif;
// find number of interfaces.
memset(&ifc, 0, sizeof(ifc));
ifc.ifc_ifcu.ifcu_req = NULL;
ifc.ifc_len = 0;
if ((s = ::socket(PF_INET, SOCK_STREAM, 0)) < 0) {
_d("Could not obtain socket!");
throw 2;
}
if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
_d("ioctl SIOCGIFCONF error!");
throw 3;
}
if ((ifr = (ifreq*) malloc(ifc.ifc_len)) == NULL) {
_d("Could not malloc ifreq!");
throw 4;
}
ifc.ifc_ifcu.ifcu_req = ifr;
if (ioctl(s, SIOCGIFCONF, &ifc) < 0) {
_d("ioctl SIOCGIFCONF error!");
throw 5;
}
numif = ifc.ifc_len / sizeof(struct ifreq);
for (i = 0; i < numif; i++) {
struct ifreq *r = &ifr[i];
struct sockaddr_in *sin = (struct sockaddr_in *)&r->ifr_addr;
if (!strcmp(r->ifr_name, "lo"))
continue; // skip loopback interface
// get MAC address
if(ioctl(s, SIOCGIFHWADDR, r) < 0) {
_v("ioctl(SIOCGIFHWADDR) error!");
throw 6;
}
char macaddr[18];
sprintf(macaddr, " %02X:%02X:%02X:%02X:%02X:%02X",
(unsigned char)r->ifr_hwaddr.sa_data[1],
(unsigned char)r->ifr_hwaddr.sa_data[0],
(unsigned char)r->ifr_hwaddr.sa_data[2],
(unsigned char)r->ifr_hwaddr.sa_data[3],
(unsigned char)r->ifr_hwaddr.sa_data[4],
(unsigned char)r->ifr_hwaddr.sa_data[5]);
macs.push_back(macaddr);
}
close(s);
free(ifr);
Regards,
Miha.
On Jan 4, 4:25 pm, Miha <
miha.valen...@gmail.com> wrote:
> Hi!
>
> I need to obtain MAC addresses of all available interfaces from native
> code, but I'm not sure how to do it. getifaddr or if_nameindex are not[...]