• 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: Getting local subnet mask
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting local subnet mask


  • Subject: Re: Getting local subnet mask
  • From: Rich Kubota <email@hidden>
  • Date: Thu, 28 Feb 2008 17:28:06 -0800

How about using getifaddrs. Also, you make the assumption that there is only one active interface. Try building a command line tool with the following, then try running the tool on a mac portable system when both the built-in and airport connections are active (course the MB Air has only the built-in airport). The sample could also use a bit more error checking

best wishes.

rich kubota

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

int main (int argc, const char * argv[]) {
    // insert code here...
int result;
struct ifaddrs *ifbase, *ifiterator;
result = getifaddrs(&ifbase);
ifiterator = ifbase;
while (!result && (ifiterator != NULL))
{
if (ifiterator->ifa_addr->sa_family == AF_INET)
{
struct sockaddr *saddr, *netmask, *daddr;
saddr = ifiterator->ifa_addr;
netmask = ifiterator->ifa_netmask;
daddr = ifiterator->ifa_dstaddr;

// we've found an entry for the IP address
struct sockaddr_in *iaddr;
char addrstr[64];
iaddr = (struct sockaddr_in *)saddr;
inet_ntop(saddr->sa_family, &iaddr->sin_addr, addrstr, sizeof(addrstr));
fprintf(stderr, "ipv4 enet interface name %s, source IP addr %s ", ifiterator->ifa_name, addrstr);

iaddr = (struct sockaddr_in *)netmask;
if (iaddr)
{
inet_ntop(saddr->sa_family, &iaddr->sin_addr, addrstr, sizeof(addrstr));
fprintf(stderr, "netmask IP addr %s ", addrstr);
}

iaddr = (struct sockaddr_in *)daddr;
if (iaddr)
{
inet_ntop(saddr->sa_family, &iaddr->sin_addr, addrstr, sizeof(addrstr));
fprintf(stderr, "dest/broadcast IP addr %s.\n\n", addrstr);
}


}
else if (ifiterator->ifa_addr->sa_family == AF_INET6)
{
// we've found an entry for the IP address
struct sockaddr_in6 *iaddr6 = (struct sockaddr_in6 *)ifiterator->ifa_addr;
char addrstr[256];
inet_ntop(ifiterator->ifa_addr->sa_family, iaddr6, addrstr, sizeof(addrstr));
fprintf(stderr, "ipv6 enet interface name %s, source IP addr %s \n\n", ifiterator->ifa_name, addrstr);
}
ifiterator = ifiterator->ifa_next;
}
    return 0;
}
Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Getting local subnet mask
      • From: Vincent Lubet <email@hidden>
References: 
 >Getting local subnet mask (From: "Hamish Allan" <email@hidden>)

  • Prev by Date: Getting local subnet mask
  • Next by Date: Re: Getting local subnet mask
  • Previous by thread: Getting local subnet mask
  • Next by thread: Re: Getting local subnet mask
  • Index(es):
    • Date
    • Thread