• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Finding Subnet
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding Subnet


  • Subject: Re: Finding Subnet
  • From: Axel Andersson <email@hidden>
  • Date: Tue, 1 Feb 2005 11:58:43 +0100

On Feb 1, 2005, at 06:52, Chris Giddings wrote:

Does anyone here have suggestions or sample code on how to get the sub net of the current system and output and output it to say a text field? I am not very familiar with the IOKit which seems to be the main focus for this kind of information. If anyone could help, or maybe patch together some sample code, I'd be very grateful.

This is outside of the scope of this mailing list, so I'll keep it short.


What you're asking for is difficult to answer. There may be multiple subnets attached to the host, or there may be none at all. You need to iterate over all available interfaces and extract subnets one by one. Though if you're doing a simple app, extracting the IPv4 netmask from the en0 interface will probably cover 90% of cases.

Anyway, some sample code. Not with IOKit, but it could probably be done with that as well. This should also answer your other question about excluding addresses based on socket family, IPv4 or IPv6.


#include <sys/types.h> #include <sys/socket.h> #include <stdio.h> #include <ifaddrs.h> #include <netdb.h> #include <net/if.h>

int main(void) {
	struct ifaddrs	*ifap, *ifp;
	char				ip[NI_MAXHOST], nm[NI_MAXHOST];

	if(getifaddrs(&ifap) < 0)
		perror("getifaddrs");

for(ifp = ifap; ifp; ifp = ifp->ifa_next) {
/* Ignore everything but IPv4, IPv6. */
if(ifp->ifa_addr->sa_family != AF_INET && ifp->ifa_addr->sa_family != AF_INET6)
continue;


		/* Ignore interfaces marked down. */
		if(!(ifp->ifa_flags & IFF_UP))
			continue;

		/* Ignore interfaces without netmask. */
		if(!ifp->ifa_netmask)
			continue;

if(getnameinfo(ifp->ifa_addr, ifp->ifa_addr->sa_len, ip, sizeof(ip), NULL, 0, NI_NUMERICHOST) != 0)
perror("getnameinfo");


if(getnameinfo(ifp->ifa_netmask, ifp->ifa_netmask->sa_len, nm, sizeof(nm), NULL, 0, NI_NUMERICHOST) != 0)
perror("getnameinfo");


		printf("%s %s %s\n", ifp->ifa_name, ip, nm);
	}

	return 0;
}

--
Axel Andersson
email@hidden
http://www.zankasoftware.com/

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: mouseDown/Up on window's titlebar?
  • Next by Date: [Cocoa AS IB] Connecting
  • Previous by thread: Re: mouseDown/Up on window's titlebar?
  • Next by thread: [Cocoa AS IB] Connecting
  • Index(es):
    • Date
    • Thread