home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / programm / 4358 < prev    next >
Encoding:
Text File  |  1992-08-18  |  2.5 KB  |  72 lines

  1. Path: sparky!uunet!pmafire!news.dell.com!swrinde!cs.utexas.edu!ut-emx!ibmchs!auschs!awdprime.austin.ibm.com!ravikm
  2. From: ravikm@austin.ibm.com (Ravi K Mandava)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: Converting inet names/addresses....
  5. Keywords: gethostbyname() , etc....
  6. Message-ID: <1992Aug18.210719.5002@awdprime.austin.ibm.com>
  7. Date: 18 Aug 92 21:07:19 GMT
  8. References: <1992Aug17.214158.6393@ncar.ucar.edu>
  9. Sender: news@awdprime.austin.ibm.com (USENET News)
  10. Reply-To: piobe!ravikm@ibmpa.awdpa.ibm.com
  11. Distribution: usa
  12. Organization: IBM Austin
  13. Lines: 56
  14. Originator: ravikm@piobe.austin.ibm.com
  15.  
  16.  
  17. In article <1992Aug17.214158.6393@ncar.ucar.edu>, morreale@niwot.scd.ucar.edu (Peter W. Morreale, SCD Consulting) writes:
  18.  > On a Sun, I need the ability to convert:
  19.  >      foo.bar.edu
  20.  > To it's inet number:
  21.  >     128.???.??.??
  22.  > (both are character strings....)
  23.  > 
  24.  > I've found gethostbyname(3) which returns the proper host (and an
  25.  > address in network byte order), and also inet_ntoa(3) (which converts a
  26.  > struct in_addr to the proper form) but for the life of me I can't figger
  27.  > out how to create a proper struct in_addr for inet_ntoa().  What I need
  28.  > is a:
  29.  > char *get_inet_a_addr_from_host_name(char *hostname) 
  30.  
  31.  You're pretty close to the solution.  gethostbyname() returns host addresses
  32.  in network long format in the member h_addr_list of struct hostent.  Note
  33.  that this member is of type 'char **', and with a suitable cast to struct
  34.  in_addr for each char pointer, you should be able to get the inet address
  35.  in the format *.*.*.*.  An example piece of code is included below.  Tailor
  36.  it to suit your requirements (char *get_inet_a_...).
  37.  
  38. --------------------------------------------------------------------------
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <sys/socket.h>
  42. #include <netinet/in.h>
  43. #include <arpa/inet.h>
  44. #include <netdb.h>
  45.  
  46. int
  47. main(int argc, char **argv)
  48. {
  49.    struct hostent    *hp;
  50.  
  51.    if (argc < 2) {
  52.       (void)fprintf(stderr, "Usage: %s hostname\n", *argv);
  53.       exit(1);
  54.    }
  55.    if (!(hp = gethostbyname(*(argv+1)))) {
  56.       perror("GETHOSTBYNAME");
  57.       exit(2);
  58.    }
  59.    while (hp->h_addr_list && *hp->h_addr_list)
  60.       (void)printf("Inet Address = %s\n",
  61.            inet_ntoa(*(struct in_addr *)*hp->h_addr_list++));
  62.  
  63.    return 0;
  64. }
  65. --------------------------------------------------------------------------
  66.  
  67. Hope this helps,
  68. -- 
  69. *******************************************************************************
  70. Ravi K Mandava        email:    piobe!ravikm@ibmpa.awdpa.ibm.com
  71. *******************************************************************************
  72.