Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!ames!ncar!boulder!ccncsu!longs.LANCE.ColoState.Edu!steved From: steved@longs.LANCE.ColoState.Edu (Steve Dempsey) Newsgroups: comp.unix.wizards Subject: Re: Determining one's own IP address. Message-ID: <3451@ccncsu.ColoState.EDU> Date: 12 Dec 89 19:55:56 GMT References: <35095@cornell.UUCP> <601@bmers58.UUCP> <4429@ur-cc.UUCP> <604@bmers58.UUCP> Sender: news@ccncsu.ColoState.EDU Reply-To: steved@longs.LANCE.ColoState.Edu (Steve Dempsey) Organization: Colorado State University, College of Engineering Lines: 43 > > I would like to be able to determine my local IP address without > > involving a hosts file or yp lookup, i.e. from memory, from within a c > > program. > > Here's a little program that should do it for 4.3BSD systems... [Larry's program deleted] If you have a decent RPC implementation on your system, you can use get_myaddress(). This works on Ultrix3.[01], HPUX 6.[25], SunOS4.0, and 4.3BSD (at least): #include #include #include #include #include main() { struct sockaddr_in me; get_myaddress(&me); fprintf(stderr,"local IP address = %u.%u.%u.%u\n", ((unsigned char*) & me.sin_addr.s_addr)[0], ((unsigned char*) & me.sin_addr.s_addr)[1], ((unsigned char*) & me.sin_addr.s_addr)[2], ((unsigned char*) & me.sin_addr.s_addr)[3]); } Of course you only get one interface address, usually the first one to be configured via ifconfig(8). > parmelee@cs.cornell.edu Steve Dempsey, Center for Computer Assisted Engineering Colorado State University, Fort Collins, CO 80523 +1 303 491 0630 INET: steved@longs.LANCE.ColoState.Edu, dempsey@handel.CS.ColoState.Edu UUCP: boulder!ccncsu!longs.LANCE.ColoState.Edu!steved, ...!ncar!handel!dempsey