| |||
| [Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] |
| 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; }
On Feb 28, 2008, at 4:54 PM, Hamish Allan wrote: Hi, |
_______________________________________________ Do not post admin requests to the list. They will be ignored. Macnetworkprog mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/macnetworkprog/email@hidden This email sent to email@hidden
| References: | |
| >Getting local subnet mask (From: "Hamish Allan" <email@hidden>) |
| Home | Archives | FAQ | Terms/Conditions | Contact | RSS | Lists | About |
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE
Contact Apple | Terms of Use | Privacy Policy
Copyright © 2007 Apple Inc. All rights reserved.