gethostbyname Function

 < Day Day Up > 



gethostbyname Function

The gethostbyname function is used to identify the IP address given a fully qualified domain name. As with gethostbyaddr, a local resolver client must be available in order to communicate with the DNS server. The prototype for the gethostbyname function is:

#include <netdb.h> struct hostent *gethostbyname( const char *name );

The name argument refers to the fully qualified domain name for which we desire an IP address translation. The response hostent structure reference contains all of the relevant information, or NULL is returned to indicate that an error occurred.

An example of the gethostbyname function is shown in Listing 4.9. In this example, we’ll specify a fully qualified domain name and emit all of the relevant information about the name (to illustrate the usage of the hostent structure fields).

Listing 4.9 Sample usage of the gethostbyname function.

start example
struct hostent *hp; int i; hp = gethostbyname( "www.microsoft.com" ); if (hp) {   printf( "h_name is %s\n", hp->h_name );   printf( "h_addrtype is %d\n", hp->h_addrtype );   i=0;   printf( "Aliases:\n" );   while (1) {     if ( hp->h_aliases[i] ) {       printf( "h_aliases[%d] = %s\n", i, hp->h_aliases[i] );       i++;     } else break;   }   i=0;   printf( "Addresses:\n" );   while (1) {     if ( hp->h_addr_list[i] ) {       struct in_addr theAddr;       memcpy( &theAddr.s_addr,                 hp->h_addr_list[i], sizeof(theAddr.s_addr) );       printf( "  h_addr_list[%d] = %s\n",                i, inet_ntoa( theAddr ) );       i++;     } else break;   } }
end example

The function gethostbyname is the most commonly used of the gethostbyname and gethostbyaddr pair. A common code pattern for resolving fully qualified domain names to IP addresses is shown in Listing 4.10.

Listing 4.10 Typical usage of the gethostbyname function with inet_addr (resolve.c).

start example
#include <netinet/in.h> #include <arpa/inet.h> #include <netdb.h> int resolve_name( struct sockaddr_in *addr, char *hostname ) {   addr->sin_family = AF_INET;   /* See if host name actually is a string IP address */   addr->sin_addr.s_addr = inet_addr( hostname );   /* If host name wasn't a string notation IP address,   * use the gethostbyname function to resolve it.    */   if ( addr->sin_addr.s_addr = 0xffffffff ) {     struct hostent *hp;     hp = (struct hostent *)gethostbyname( hostname );     /* Don't know what hostname is... */     if (hp == NULL) return –1;         else {       memcpy( (void *)&addr->sin_addr,               (void *)hp->h_addr_list[0],                sizeof( addr->sin_addr) );     }   }   return 0; }
end example

In this example, the code first tries the inet_addr function. If the host name variable happens to be a string IP address in dotted notation, then inet_addr will convert this into a binary notation address in network byte order. Otherwise, 0xffffffff is returned to indicate the error. Upon detecting this, we assume that the address is a fully qualified domain name and rely on gethostbyname to resolve the name to an address. After successful return from gethostbyname (a valid pointer to a hostent structure), we copy the first address of the h_addr_list to the address portion of the sockaddr_in structure. Sample usage of this function is shown in Listing 4.11.

Listing 4.11 Sample usage of the resolve_name function.

start example
struct sockaddr_in addr; int ret; ret = resolve_name( &addr, "www.microsoft.com" ); if (ret == 0) {   printf( "address is %s\n", inet_ntoa( addr.sin_addr ) ); }
end example

In Listing 4.11, we pass in our sockaddr_in structure with the name for which we want to resolve. Upon return (successful resolution indicated by a return value of zero), we convert the binary address into string dotted-notation using inet_ntoa and emit it using 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