Obtain MAC addresses of all available interfaces

1,808 views
Skip to first unread message

Miha

unread,
Jan 4, 2012, 10:25:37 AM1/4/12
to android-ndk
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
available, though there have been a few posts about it [1] which are
not really helpful.

Thanks,
Miha.

[1]: https://groups.google.com/d/topic/android-ndk/aSjZ8Szg8_U/discussion
and https://groups.google.com/d/topic/android-ndk/3JIvD0PFaU4/discussion

Miha

unread,
Jan 5, 2012, 4:22:26 AM1/5/12
to android-ndk
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[...]

Elliott Hughes

unread,
Jan 5, 2012, 2:19:03 PM1/5/12
to android-ndk
or you could use NetworkInterface in managed code, and let us do the
work for you. (we do a more complete job than the code you're using.)

Miha

unread,
Jan 16, 2012, 9:21:26 AM1/16/12
to android-ndk
Hi Elliot!

Well, I'm using a (big) native library, which is used on other
platforms as well. And I am implementing a callback. And it seems like
an overhead to call (synchronously) into Java to obtain this
information, doesn't it?

I would appreciate a more complete way to do it in native code, if it
is possible.

Kind regards,
Miha.

Elliott Hughes

unread,
Jan 17, 2012, 4:18:59 PM1/17/12
to android-ndk


On Jan 16, 6:21 am, Miha <miha.valen...@gmail.com> wrote:
> Hi Elliot!
>
> Well, I'm using a (big) native library, which is used on other
> platforms as well. And I am implementing a callback. And it seems like
> an overhead to call (synchronously) into Java to obtain this
> information, doesn't it?

no, it doesn't. if you're enumerating all the interfaces so often that
you can't even afford an upcall, you're doing it far too often and
should fix that.

Nasif Noorudeen

unread,
Mar 10, 2013, 12:40:29 PM3/10/13
to andro...@googlegroups.com
header files used in the android-ndk is diffrent than one in the mac.
Check the include files used for this function in the ndk c code, and
check corresponding struct variable is there in the mac headers. You
can easily find this from xcode

Thanks




On Thu, Mar 7, 2013 at 2:51 PM, Dj <dhiraj...@gmail.com> wrote:
> Hi Miha
>
> I am new to Mac os x and i am porting your code for Mac.
> When i tried, it gave me errors like
>
> /error: 'struct ifreq' has no member named 'ifr_hwaddr'
>
> Why this is happening?
>
> Any help is welcome.
>
> Thanks in advance.
> --
> You received this message because you are subscribed to the Google Groups
> "android-ndk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to android-ndk...@googlegroups.com.
> To post to this group, send email to andro...@googlegroups.com.
> Visit this group at http://groups.google.com/group/android-ndk?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
0 new messages