Thursday, November 3, 2011

struct hostent

struct hostent linux or struct hostent in c

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 */
};

/* 
 * for backward compatibility 
 */
#define h_addr h_addr_list[0]

Become familiar with the hostent structure as you will use it often when doing socket programming.

The hostent h_name Member

The h_name entry within the hostent structure is the official name of the host that you are looking up. It is also known as the canonical name of the host. If you provided and alias, or a hostname without the domain name, then this entry will describe the proper name for what you have queried. this entry is useful for displaying or logging your result to a log file.

The hostent h_aliases Member
The hostent h_aliases member of the returned structure is an array of alias names for the hostname that you have queried. the end of the list is marked by a NULL pointer. As an example, the entire list of aliases for "http://saurabhgupta0527.blogspot.com" could be reported as follow:

struct hostent *ptr;
int x;
ptr = gethostbyname("http://saurabhgupta0527.blogspot.com");
for ( x=0; ptr->h_aliases[x] != NULL; ++x ) {
   printf ("alias = '%s'\n", ptr->h_aliases[x]);
}
No error checking was shown in the preceding example. If ptr is NULL, this indicates that no information was available.

The hostent h_addrtype Member 

The value presently returned in the member h_addrtype is AF_INET. However, as IPv6 becomes fully implemented, the name server will also be capable of returning IPv6 addresses. When this happens, h_addrtype will also return the value AF_INET6 when it is appropriate.

The purpose of the h_addrtype value is to indicate the format of the addresses in the list h_addr_list, which will be described next.

The hostent h_length Member

This value is related to the h_addrtype member. For the current version of the TCP/IP protocol (IPv4), this member always contains the value of 4, indicating 4-byte IP numbers. However, this value will be 16 when IPv6 is implemented, and IPv6 addresses are returned instead.

The hostent h_addr_list Member

When performing a name-to-IP-number translation, this member becomes your most important piece of information. When member h_addrtype contains the value of AF_INET, each pointer in this array of pointers points to a 4-byte IP address. The end of the list is marked by a NULL pointer.

1 comment: