home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.internals
- Path: sparky!uunet!sci34hub!tybrin4!tybse1!swhite
- From: swhite@tybse1.uucp (William C. "Spike" White)
- Subject: Re: how to get localhost's IP address?
- Organization: Tybrin Corporation, Shalimar, FL
- Date: Mon, 10 Aug 1992 15:55:53 GMT
- Message-ID: <1992Aug10.155553.7976@tybse1.uucp>
- References: <93040@bu.edu> <MAKI.92Aug7110206@void.jit.dec.com> <JIM.92Aug7125721@hunter.cs.strath.ac.uk>
- Lines: 43
-
- In article <JIM.92Aug7125721@hunter.cs.strath.ac.uk> jim@cs.strath.ac.uk (Jim Reid) writes:
- >In article <MAKI.92Aug7110206@void.jit.dec.com> maki@jit.dec.com (Watanabe Maki) writes:
- >
- > In article <93040@bu.edu> tasos@cs.bu.edu (Anastasios Kotsikonas) writes:
- >
- > > does anyone know how to obtain the IP address of the local host (not
- > > 127.0.0.1, but given the the host name how do you get the internet address)?
- >
- > Use gethostname() + gethostbyname().
- >
- >But what if your machine has more than 1 network interface (=> more
- >than 1 IP address)? A call to gethostbyname works when there is only
- >one network interface, since there can only be one IP address bound to
- >it. If the machine has more than 1 network interface, you'll need to
- >use the SIOCGIFADDR ioctl on each interface.
- >
- > Jim
-
- gethostname() + gethostbyname() should still work. The reason is that
- the gethostbyname function returns a pointer to a hostent structure.
- This structure looks like this:
-
- struct hostent {
- char *h_name; /* official name of host */
- char **h_aliases; /* alias list */
- int h_addrtype; /* host address type */
- int h_length; /* length of address */
- char **h_addr_list; /* <<<----------- */
- #define h_addr h_addr_list[0]
- };
-
- h_addr_list is really an array of pointers to in_addr structures (i.e.,
- 4-byte IP addresses). h_addr_list is terminated w/ a NULL entry.
-
- This was done specifically to deal w/ multi-homed hosts.
-
- For more info, look at the "bible" of UNIX Network programming; i.e.,
- W. Richard Stevens, UNIX Network Programming, p. 393.
-
- --
- Spike White Tybrin Corporation, Shalimar, FL | Moderation in all things --
- swhite@afseo.eglin.af.mil | and abstinence in none!
- Disclaimer: I speak only for myself, not my employer.
-