Re: IP Address - revisited
Re: IP Address - revisited
- Subject: Re: IP Address - revisited
- From: Scott Judd <email@hidden>
- Date: Thu, 22 Jul 2004 04:00:16 -0500
On Jul 22, 2004, at 1:01 AM, Larry Fransson wrote:
On Jul 21, 2004, at 21:02, David Cornell wrote:
Given the following how would i print out only IPV4 addresses while
excluding
IPV6 addresses and 127.0.0.1?
NSEnumerator *contents = [[currenthost addresses] objectEnumerator];
NSString *address;
while ((address = [contents nextObject]))
{
//...
}
Given that IPv6 addresses are not allowed to contain periods, you could
exclude anything not containing a period. Matching 127.0.0.1 for
exclusion is easy. If I remember correctly, a continue statement will
continue (imagine that!) to the next iteration of the loop after you
have matched one of the addresses you want to exclude. If you don't
want to exclude it, you print it, like this:
NSArray *adds = [[NSHost currentHost] addresses];
NSEnumerator *e = [adds objectEnumerator];
id o;
while(o = [e nextObject]) {
if([o isEqualToString:@"127.0.0.1"]) continue;
else if([o rangeOfString:@"."].location == NSNotFound) continue;
NSLog(@"%@", o);
}
Larry Fransson
"Filtering the dots" might be a decent enough hack, but there's a much
cleaner/safer approach. You can call the BSD system directly. Granted
this doesn't provide you with the high-level "Cocoa-ness" that you may
be looking for, but it will definitely work in a bulletproof way.
As far as "filtering out" the loopback address, there are probably
several reasons why you wouldn't want to do that. First of all, as
someone recently pointed out on the list, there are typically several
interfaces on any given Mac. You want the IP address for each. If you
"filter out" the loopback interface (lo0), you could be hacking out
functionality that could cause your app to behave in ways that you
don't expect. Just open Terminal.app and type 'ifconfig' (no quotes).
You should see configs for at least two network interfaces: lo0 (the
loopback interface, used by localhost) and en0 (the default ethernet
interface). There will probably be several other interfaces listed,
depending on your machine's configuration.
To implement the necessary api calls, check out the man pages for
getifaddrs and ioctl and if you're a newbie to BSD sockets, you'll want
to locate any info you can find on inet_ntop/inet_pton,
inet_ntoa/inet_aton. They're essentially functions that handle the
IPv4-style numeric and presentational formatting of IP addresses. If
memory serves, the recent versions of these functions will handle IPv6,
as well.
Fair warning that if you're new to BSD socket programming, it can be a
lot more fussy than the safe and friendly Cocoa world that we all know
and love. If you get lost, take a look at the source code for the
terminal command "ifconfig," that ought to provide you with some sample
code to follow.
scott judd
malusdomestica.org
_______________________________________________
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.