The UNIX Resolver


The UNIX resolver implements a number of API calls, the most frequently used of which are gethostbyname and gethostbyaddr . These calls are available in most programming languages on the UNIX platform in some form.

gethostbyname and gethostbyaddr

gethostbyname and gethostbyaddr are probably the most frequently used calls in the UNIX resolver library. They work the same for both IPv4 and IPv6 and provide the basic functions of looking up the A or AAAA record of a host (gethostbyname) and looking up the PTR record of an IP address (gethostbyaddr). Complete descriptions of these calls are found in your OS man pages, as well as in network programming texts such as UNIX Network Programming .

The data types involved in these calls are opaque, which enables them to carry information about different addressing families for example, IPv6 as well as IPv4 (for which they were designed) and also OSI addressing, in theory. The opaqueness of the data types makes the calls a bit difficult to use and remember, but don't let that bother you. I don't know any programmers who don't have to look up these calls in manual pages, other documentation, or previously written code. In fact, a lot of the code out there looks very similar to the code samples found in UNIX Network Programming.

If you want to look up the address of a host in C, the code is as follows :

 void do_lookup(char *name) { /* Look up the IP address of the host named by the variable "name" */ struct hostent *rhost;        /* Host lookup address */ struct in_addr addr;          /* TMP: Address for inet_ntoa */ unsigned long inaddr;         /* IP address (IPv4 only) */ char *ipstr;                  /* IP address as a string */ int tcp_port;                 /* Port number */ int cd;                       /* Connection descriptor */ /* First try to convert IP number to address */ if ((inaddr = inet_addr(name)) != INADDR_NONE) { ipstr=name; } else { /* Look up A (or AAAA) record of the host.  This code should work for any address protocol. */ rhost = gethostbyname(name); /* Check return value */ if (rhost==NULL) { printf("%s could not be resolved\n",name); return; } memcpy(&addr,rhost->h_addr,rhost->h_length); ipstr = inet_ntoa(addr); } printf("The address of %s is %s\n",name,ipstr); } 

Other Functions in the Resolver

What you find available in a random OS resolver will vary, but if you installed BIND as described in Chapter 15, "Compiling and Maintaining BIND," you will have a libresolv in /usr/local/lib, which has quite a lot of functionality. This is described in resolver (3) of your man pages. It's probably not in the OS man pages, though, so look in /usr/local/man or in the BIND distribution. I will not describe it here, but if you are interested in functions beyond gethostby*, you should look for them there.



The Concise Guide to DNS and BIND
The Concise Guide to DNS and BIND
ISBN: 0789722739
EAN: 2147483647
Year: 1999
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net