Determining one's own IP address.
Steve Dempsey
steved at longs.LANCE.ColoState.Edu
Wed Dec 13 06:55:56 AEST 1989
> > 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 <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
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 at cs.cornell.edu
Steve Dempsey, Center for Computer Assisted Engineering
Colorado State University, Fort Collins, CO 80523 +1 303 491 0630
INET: steved at longs.LANCE.ColoState.Edu, dempsey at handel.CS.ColoState.Edu
UUCP: boulder!ccncsu!longs.LANCE.ColoState.Edu!steved, ...!ncar!handel!dempsey
More information about the Comp.unix.wizards
mailing list