Re: local ip address?
Re: local ip address?
- Subject: Re: local ip address?
- From: Tom Waters <email@hidden>
- Date: Wed, 21 Nov 2001 14:37:21 -0800
Thanks Carl!
I hacked your source up so that it only returned the ip addresses (since
that's all I needed!) and returned them in an Objective-C dictionary,
with the interface as the key and the dotted string representation of
the ip address as the values.
I added this to Steven Frank's SmallSockets classes in my own copy as a
static method on Socket.
I've attached the implementation if Steven or anyone else wants to
incorporate it.
usage:
NSLog(@"my ip addrs = %@", [Socket getIPAddresses]);
prints:
2001-11-21 14:31:06.661 NetFrame[2859] my ip addrs = <CFDictionary
0x2a0a00 [0x8016024c]>{count = 2, capacity = 3, pairs = (
2 : <CFString 0x2a0a50 [0x8016024c]>{contents = "lo0"} = <CFString
0x2a0a30 [0x8016024c]>{contents = "127.0.0.1"}
3 : <CFString 0x2a0ae0 [0x8016024c]>{contents = "en0"} = <CFString
0x2a0ac0 [0x8016024c]>{contents = "192.168.1.100"}
)}
// this goes in Socket.m, i forget which headers he had in there
originally, so i'll just show em all here.
#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>
#import "Socket.h"
@implementation Socket
///// THE REST OF Socket.m DELETED FOR BREVITY...
#define IFCONF_BUFFERSIZE 4000
#define max(a,b) ((a) > (b) ? (a) : (b))
// these really go in AbstractSocket.h, but i put them here just for
this email...
#define SOCKET_EX_IOCTL_FAILED @"Socket: Ioctl failed"
#define SOCKET_EX_IOCTL_FAILED_F @"Socket: Ioctl failed: %s"
+ (NSDictionary *)getIPAddresses
{
NSMutableDictionary *ip_addrs = [NSMutableDictionary dictionary];
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:SOCKET_EX_BAD_SOCKET_DESCRIPTOR
format:SOCKET_EX_BAD_SOCKET_DESCRIPTOR];
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0)
[NSException raise:SOCKET_EX_IOCTL_FAILED
format:SOCKET_EX_IOCTL_FAILED_F, strerror(errno)];
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 Wednesday, November 21, 2001, at 12:18 PM, Bell, Carl wrote:
-----Original Message-----
From: Tom Waters [mailto:email@hidden]
Sent: Wed, November 21, 2001 11:22 AM
To: Cocoa Dev Mailing List
Subject: local ip address?
(this was asked on this list in september, but unanswered)
Is there a way in Cocoa/Foundation to get the ip address of
the machine my program is running on?
Howdy,
Although technically it's not Cocoa, I do have some code to get the
machine's local ip address(es), hardware address(es), and interface
name(s), that folks are welcome to use. It works in Cocoa apps and
should work in Carbon apps as well. IIRC, it was based on code in
one of the Stevens Unix Network Programming books. Anyway, you can
find it here:
<http://carl-bell-2.baylor.edu/~bellc/temp/dumpaddrs.tar.gz>.
Included is a quick and dirty main.c file that just dumps the
addresses to stdout. If you try it and have any problems with it
or have any questions (or find any bugs! :-) please let me know.
-cb
________________________________________________________________________
Carl W. Bell <http://www.baylor.edu/~Carl_Bell/index.html>
Sr. Analyst/Programmer Baylor University Academic Technology Center