home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / internal / 1679 next >
Encoding:
Text File  |  1992-08-12  |  2.2 KB  |  54 lines

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