Search code examples
objective-ccocoamacosnetworkingnetmask

How to determine netmask of active interface in Objective-C on a Mac?


I'm trying to determine the network segment my Macbook is in, because I then want to scan this segment for active hosts (basic IP scanner) by using the CFHost class. Therefore I need the IP and Netmask of my active interface. This is how I get the IP:

NSString *ipAddr = [[[NSHost currentHost] addresses] objectAtIndex:0];

But I have absolutely no idea how to get the Netmask, so I'm kind of stuck. Especially because I'm fairly new at Objective-C and because I also do not have extensive knowledge of plain C. I've looked at CFHost, CFNetwork, NSHost and various Google hits, but have found nothing useful, so far.

As a last resort I could do a system call of sorts, I suppose, or read it from a file (which one?), but I want to avoid that, if possible.

So, how do I get the matching netmask to the ip obtained through NSHost? Any suggestions would be greatly appreciated.

Thanks!


Solution

  • You will have to use the System Configuration API. See System Configuration Programming Guidelines and System Configuration Framework Reference