gethostbyaddr Function

 < Day Day Up > 



gethostbyaddr Function

The gethostbyaddr function is used to identify the fully qualified domain name of a host given its IP address. In order to take advantage of this functionality, the host on which the application operates must support a DNS resolver. This DNS client communicates with a DNS server to resolve host names to IP addresses and vice versa. The prototype for the gethostbyaddr function is:

#include <netdb.h> struct hostent *gethostbyaddr( const char *addr,                                    int len, int type);

We pass in our character string IP address as addr and its length as len. The final type argument refers to the address family, which will be AF_INET for all examples in this book (IP-based protocols). The gethostbyaddr function returns a pointer to a hostent structure that will contain the relevant naming information. If an error occurs, the return value will be NULL, otherwise it’s a valid pointer to a hostent structure. The hostent structure has the following format:

struct hostent {      char    *h_name;            /* official name of host */     char    **h_aliases;        /* alias list            */     int     h_addrtype;         /* host address type     */     int     h_length;           /* length of address     */     char    **h_addr_list;      /* list of addresses     */ }; #define h_addr h_addr_list[0]   /* first address, NBO    */

Field h_name refers to the official name of the host (compared to the aliases that can also be used for the host, defined as the list h_aliases). The h_addrtype will always be AF_INET. Because a host may be accessible by more than one address, a list of addresses is provided in h_addr_list. Finally, the h_addr macro is provided for backward compatibility reasons (an old member of this structure) and refers to the first element of the h_addr_list. We look at each of these fields in the discussion of gethostbyname.

A simple example of the gethostbyaddr function is shown in Listing 4.8. In this example, we’ll emit the fully qualified domain name given an IP address.

Listing 4.8 Sample usage of the gethostbyaddr function.

start example
struct in_addr in; struct hostent *hp; inet_aton( "192.168.1.1", &in ); if ( hp = gethostbyaddr( (char *)&in.s_addr,                            sizeof(in.s_addr), AF_INET )) {   printf( "Host name is %s\n", hp->h_name ); }
end example

For demonstration purposes, we fill an in_addr structure with a binary representation of our string IP address using the inet_aton function. The gethostbyaddr function is called next with the s_addr field of the in_addr structure, storing the response in the hostent structure pointer. The h_name field of the hostent structure refers to the host name, and is emitted in this example by printf.



 < Day Day Up > 



BSD Sockets Programming from a Multi-Language Perspective
Network Programming for Microsoft Windows , Second Edition (Microsoft Programming Series)
ISBN: 1584502681
EAN: 2147483647
Year: 2003
Pages: 225
Authors: Jim Ohlund

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