Re: Getting the MAC address via ioctl calls
Re: Getting the MAC address via ioctl calls
- Subject: Re: Getting the MAC address via ioctl calls
- From: Justin Walker <email@hidden>
- Date: Fri, 9 Jan 2004 18:12:31 -0800
On Friday, January 9, 2004, at 12:50 PM, Aaron Ballman wrote:
Hello all!
I am using ioctl and OSIOCGIFCONF to obtain a list of all the
interfaces installed on the system. I'm able to obtain information
like the local address, subnet mask, broadcast address, etc. But I'm
having a hard time with the MAC address. The reason I am using ioctl
to do this is because I'm cross-compiling to Linux and would like to
keep the code-base the same (just use a bridge to make sure the
function pointers are all correct, hence the reason you'll see
v_foobar in any of my code samples).
On Linux, you can use:
Darwin is not Linux. Or vice versa. You need to get over that.
This will get you the MAC address in the ifr->ifr_hwaddr memeber of a
struct ifreq. I notice that Apple deviates from the Linux version of
ifreq and furthermore (more importantly!) there's no SIOCGIFHWADDR
defined anywhere.
Actually, Linux deviates. It's true that there is no direct way to
just get 'the' hardware address.
On Darwin (and other recent BSD's), you use 'getifaddrs' (available, I
think on 10.2 and 10.3). This gets you the list of all the addresses
for all the interfaces in town. Check the man page for details.
Am I just missing something, or is there really just no way to get the
MAC address using ioctl?
On newer BSD's, the underlying mechanism is sysctl(2), but the
venerable ioctl(SIOCGIFCONF) will work as well. See Stevens's book on
Unix Network Programming (v. 1).
Thanks for your help!
As a special added bonus, I'm appending a code snippet that will help
you get started.
Regards,
Justin
main()
{ struct ifaddrs *ifap, *ifa;
int rv;
if ((rv=getifaddrs(&ifap))==-1)
{ perror("getifaddrs");
exit(-1);
}
if (ifap == NULL)
{ printf("Null pointer returned\n");
exit(-1);
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next)
{ printf("Interface %s\n", ifa->ifa_name);
printf("Interface address family: %d\n",
ifa->ifa_addr->sa_family);
}
freeifaddrs(ifap);
}
--
Justin C. Walker, Curmudgeon-At-Large *
Institute for General Semantics | Men are from Earth.
| Women are from Earth.
| Deal with it.
*--------------------------------------*-------------------------------*
_______________________________________________
macnetworkprog mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/macnetworkprog
Do not post admin requests to the list. They will be ignored.