Taken from the original at http://www.ecst.csuchico.edu/~beej/guide/net/ ... see end of writeup for Copyright statement.
DNS--You say "whitehouse.gov", I say "198.137.240.100"
In case you don't know what DNS is, it stands for "Domain Name Service". In a nutshell, you tell it what the human-readable address is for a site, and it'll give you the IP address (so you can use it with bind(), connect(), sendto(), or whatever you need it for.) This way, when someone enters:
$ telnet whitehouse.gov
telnet can find out that it needs to connect() to "198.137.240.100".
But how does it work? You'll be using the function gethostbyname():
#include <netdb.h>
struct hostent *gethostbyname(const char *name);
As you see, it returns a pointer to a struct hostent, the layout of which is as follows:
struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};
#define h_addr h_addr_list[0]
And here are the descriptions of the fields in the struct hostent:
- h_name - Official name of the host.
- h_aliases - A NULL-terminated array of alternate names for the host.
- h_addrtype - The type of address being returned; usually AF_INET.
- h_length - The length of the address in bytes.
- h_addr_list - A zero-terminated array of network addresses for the host. Host addresses are in Network Byte Order.
- h_addr - The first address in h_addr_list.
gethostbyname() returns a pointer to the filled struct hostent, or NULL on error. (But errno is not set -- h_errno is set instead. See herror(), below.)
But how is it used? Sometimes (as we find from reading computer manuals), just spewing the information at the reader is not enough. This function is certainly easier to use than it looks.
Here's an example program:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct hostent *h;
if (argc != 2) { /* error check the command line */
fprintf(stderr, "usage: getip address\n");
exit(1);
}
if ((h=gethostbyname(argv[1])) == NULL) { /* get the host info */
herror("gethostbyname");
exit(1);
}
printf("Host name : %s\n", h->h_name);
printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return 0;
}
With gethostbyname(), you can't use perror() to print error message (since errno is not used). Instead, call herror().
It's pretty straightforward. You simply pass the string that contains the machine name ("whitehouse.gov") to gethostbyname(), and then grab the information out of the returned struct hostent.
The only possible weirdness might be in the printing of the IP address, above. h->h_addr is a char *, but inet_ntoa() wants a struct in_addr passed to it. So I cast h->h_addr to a struct in_addr *, then dereference it to get at the data.
Prev | Up | Next
Copyright © 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted in any medium provided that its content is not altered, it is presented in its entirety, and this copyright notice remains intact. Contact beej@ecst.csuchico.edu for more information.