|
struct hostent
{
char * h_name; /* The canonical (official) name of host */
// a string variable
char * h_aliases[ ]; /* The list of "aliases" for the hostname */
// h_aliases is an array of string
// h_aliases[i] == NULL marks the end of the array
int h_addrtype; /* host address type (should be = AF_INET (2)) */
int h_length; /* length of address (= 4 bytes IP addresses) */
char * h_addr_list[ ]; /* list of addresses */
// h_addr_list is an array of int (binary IP addresses)
// in network byte order
// h_addr_list[i] == 0 marks the end of the array
};
|
|
Example: how to use the gethostbyname() call
/* -------------------------------
Variables
------------------------------- */
char hname[100];
struct hostent *hp; /* host pointer */
int addr;
int i;
printf("Enter hostname: ");
scanf("%s", hname);
hp = gethostbyname(hname);
if (hp == NULL)
{
printf("host information for %s not found\n", hname);
exit(1);
}
/* -----------------------------------
Print the information
----------------------------------- */
/* --------------
Canonical name
-------------- */
printf("official hostname h_name = `%s'\n", hp->h_name);
/* -----------------------------
List of aliases
----------------------------- */
printf("Host has this alias list:\n");
char *p[]; /* help variable to access h_aliases[ ] */
p = hp->h_aliases; // p[i] is a string (char *)
for ( i = 0 ; p[i] != NULL; i++ )
{
printf("\tAlias %d: %s\n", i+1, p[i] );
}
printf("Host's address type = %d (should be AF_INET = %d)\n",
hp->h_addrtype, AF_INET);
/* -------------------------------------
Length of an IP address
------------------------------------- */
printf("Host's address length = %d\n", hp->h_length);
/* -------------------------------------
List of IP addresses
------------------------------------- */
printf("List of Internet addresses for host:\n");
int *a[]; /* Help variable to access h_addr_list[ ] */
a = (int **) hp->h_addr_list; // a[i] is an IP address
for (i = 0; a[i] != 0; i++)
{
printf("\tIP address %d: ", i+1);
paddr( (void*) a[i] ); // Print IP address as dotted decimal
printf("\n");
}
|
Enter hostname: www.yahoo.com |
Notes:
|
|
h_addr_list[ ] // It's an array (multiple IP addresses)
|
We just need 1, so the safest choice is:
h_addr_list[0] // The other entries may be invalid !!!
|
int main(int argc, char **argv)
{
struct sockaddr_in dest_addr; /* Destination socket address */
struct hostent *hp;
...
/* =============================================================
argv[1] is the string containing the symbolic host name
============================================================= */
hp = gethostbyname(argv[1]); // E.g. argv[1] = "wwww.google.com"
if ( h == NULL )
{
printf("Error... \n");
exit(1);
}
/* =============================================================
Use:
hp->haddr_list[0] // This one is for sure valid :)
Note: hp->haddr_list[0] is in network byte ordering !!!
============================================================= */
memcpy( (char *) &(dest_addr.sin_addr.s_addr),
hp->h_addr_list[0], // Copy first entry and
hp->h_length); // perserve byte ordering
...
}
|
Note:
|
How to run the program:
|