home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!pmafire!news.dell.com!swrinde!cs.utexas.edu!ut-emx!ibmchs!auschs!awdprime.austin.ibm.com!ravikm
- From: ravikm@austin.ibm.com (Ravi K Mandava)
- Newsgroups: comp.unix.programmer
- Subject: Re: Converting inet names/addresses....
- Keywords: gethostbyname() , etc....
- Message-ID: <1992Aug18.210719.5002@awdprime.austin.ibm.com>
- Date: 18 Aug 92 21:07:19 GMT
- References: <1992Aug17.214158.6393@ncar.ucar.edu>
- Sender: news@awdprime.austin.ibm.com (USENET News)
- Reply-To: piobe!ravikm@ibmpa.awdpa.ibm.com
- Distribution: usa
- Organization: IBM Austin
- Lines: 56
- Originator: ravikm@piobe.austin.ibm.com
-
-
- In article <1992Aug17.214158.6393@ncar.ucar.edu>, morreale@niwot.scd.ucar.edu (Peter W. Morreale, SCD Consulting) writes:
- > On a Sun, I need the ability to convert:
- > foo.bar.edu
- > To it's inet number:
- > 128.???.??.??
- > (both are character strings....)
- >
- > I've found gethostbyname(3) which returns the proper host (and an
- > address in network byte order), and also inet_ntoa(3) (which converts a
- > struct in_addr to the proper form) but for the life of me I can't figger
- > out how to create a proper struct in_addr for inet_ntoa(). What I need
- > is a:
- > char *get_inet_a_addr_from_host_name(char *hostname)
-
- You're pretty close to the solution. gethostbyname() returns host addresses
- in network long format in the member h_addr_list of struct hostent. Note
- that this member is of type 'char **', and with a suitable cast to struct
- in_addr for each char pointer, you should be able to get the inet address
- in the format *.*.*.*. An example piece of code is included below. Tailor
- it to suit your requirements (char *get_inet_a_...).
-
- --------------------------------------------------------------------------
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <netdb.h>
-
- int
- main(int argc, char **argv)
- {
- struct hostent *hp;
-
- if (argc < 2) {
- (void)fprintf(stderr, "Usage: %s hostname\n", *argv);
- exit(1);
- }
- if (!(hp = gethostbyname(*(argv+1)))) {
- perror("GETHOSTBYNAME");
- exit(2);
- }
- while (hp->h_addr_list && *hp->h_addr_list)
- (void)printf("Inet Address = %s\n",
- inet_ntoa(*(struct in_addr *)*hp->h_addr_list++));
-
- return 0;
- }
- --------------------------------------------------------------------------
-
- Hope this helps,
- --
- *******************************************************************************
- Ravi K Mandava email: piobe!ravikm@ibmpa.awdpa.ibm.com
- *******************************************************************************
-