• 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: Network interface active?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Network interface active?


  • Subject: Re: Network interface active?
  • From: email@hidden
  • Date: Mon, 10 Jun 2002 08:03:02 -0700

I just use BSD to find this out... here's some code that might help you.

#import <unistd.h>
#import <sys/types.h>
#import <sys/socket.h>
#import <sys/ioctl.h>
#import <netinet/in.h>
#import <net/if.h>
#import <arpa/inet.h>

#define IFCONF_BUFFERSIZE 4000
#define max(a,b) ((a) > (b) ? (a) : (b))

+ (NSDictionary *)getIPAddresses
{
NSMutableDictionary *ip_addrs;

char buffer[IFCONF_BUFFERSIZE];
char *ptr = buffer;
struct ifconf ifc = { IFCONF_BUFFERSIZE, { buffer } };

int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == SOCKET_INVALID_DESCRIPTOR)
[NSException raise:@"Bad socket descriptor" format:@"Bad socket descriptor"];

if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
[NSException raise:@"Ioctl failed" format:@"Ioctl failed: %s", strerror(errno)];

ip_addrs = [NSMutableDictionary dictionary];
while (ptr < buffer + ifc.ifc_len) {
struct ifreq *ifr = (struct ifreq *)ptr;
struct sockaddr_in *sin = (struct sockaddr_in *)&ifr->ifr_addr;
NSString *ipaddr = [NSString stringWithCString:inet_ntoa(sin->sin_addr)];
char *cptr;

// for next one in buffer
ptr += sizeof(ifr->ifr_name) + max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);

if (ifr->ifr_addr.sa_family != AF_INET) {
continue; // ignore if not desired address family
}

if ((cptr = (char *)strchr(ifr->ifr_name, ':')) != NULL) {
*cptr = 0; // replace colon with null
}

ioctl(sockfd, SIOCGIFFLAGS, ifr);
if ((ifr->ifr_flags & IFF_UP) == 0) {
continue; // ignore if interface not up
}
[ip_addrs setObject:ipaddr forKey:[NSString stringWithCString:ifr->ifr_name]];

}
close(sockfd);
return ip_addrs;
}

On Sunday, June 9, 2002, at 06:55 PM, Matt Ronge wrote:

Is there a way to determine if a network interface is active? I see that
ifconfig returns this information, so I could parse it. However, I'm
wondering if I/O Kit can do this also?

I tried searching the web, Apple's website, and reading the docs but I can't
find anything on this subject.

Thanks,
--
Matt Ronge

Monkeybread Software
http://www.monkeybreadsoftware.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Network interface active? (From: Matt Ronge <email@hidden>)

  • Prev by Date: Filter services
  • Next by Date: Syntax Coloring...
  • Previous by thread: Re: Network interface active?
  • Next by thread: Re: MP3 ID tags
  • Index(es):
    • Date
    • Thread