Re: How to link against IOKit framework or libraries?
Re: How to link against IOKit framework or libraries?
- Subject: Re: How to link against IOKit framework or libraries?
- From: Jan Brittenson <email@hidden>
- Date: Thu, 07 May 2009 13:16:29 -0700
Xochitl Lunde wrote:
This might not be the right mailing list, so please redirect me if I am
mistaken.
I am porting a Linux/Windows application to MacOS. The application is
still under development, so I really only have unit tests based on CppUnit
library as executable targets. I am doing this using an Intel iMac with
Leopard. Right now I use the automake tools (aclocal;autoconf;automake
--add-missing;./configure;make) to build my application.
Under Linux, my c++ library uses ioctl() to find the MAC address of an
interface given the IP address. I found an example online of getting MAC
addresses on MacOS, but this example uses the IOKit.
Use the BSD interfaces. Here's a simple version that works for 48-bit MAC
addrs.
bool GetIfMacAddr(const char* interface, uint8_t macaddr[6])
{
#ifdef SIOCGIFHWADDR
// Linux, Solaris, etc
const int dummy = ::socket(AF_INET, SOCK_STREAM, 0);
if (dummy == -1) return false;
struct ifreq r;
::strncpy((char*)r.ifr_name, interface, sizeof r.ifr_name - 1);
r.ifr_name[sizeof r.ifr_name - 1] = 0;
const int e = ::ioctl(dummy, SIOCGIFHWADDR, &r);
::memcpy(macaddr, r.ifr_hwaddr.sa_data, 6);
::close(dummy);
return e != -1;
#else
// BSD, Mac OS X
struct ifaddrs *ifap;
bool ok = false;
if (!::getifaddrs(&ifap)) {
for (const ifaddrs *p = ifap; p; p = p->ifa_next) {
if (p->ifa_addr->sa_family == AF_LINK) {
if (p->ifa_name && p->ifa_name[0] &&
!::strcmp((const char*)p->ifa_name, interface)) {
const sockaddr_dl& sdl = *(sockaddr_dl*)p->ifa_addr;
//::memset(macaddr, 0, sizeof macaddr);
::memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen,
min<int>(sdl.sdl_alen, sizeof macaddr));
ok = true;
break;
}
}
}
::freeifaddrs(ifap);
}
return ok;
#endif
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden