Besides the standard numbers-and-dots notation for Internet addresses, you can also refer to a host by a symbolic name. The advantage of a symbolic name is that it is usually easier to remember. For example, the machine with Internet address `128.52.46.32' is also known as `churchy.gnu.ai.mit.edu'; and other machines in the `gnu.ai.mit.edu' domain can refer to it simply as `churchy'.
Internally, the system uses a database to keep track of the mapping between host names and host numbers. This database is usually either the file `/etc/hosts' or an equivalent provided by a name server. The functions and other symbols for accessing this database are declared in `netdb.h'. They are BSD features, defined unconditionally if you include `netdb.h'.
char *h_name
char **h_aliases
int h_addrtype
AF_INET
. In principle other kinds of addresses could be represented in the data base as well as Internet addresses; if this were done, you might find a value in this field other than AF_INET
. See Socket Addresses.
int h_length
char **h_addr_list
char *h_addr
h_addr_list[0]
; in other words, it is the first host address.
As far as the host database is concerned, each address is just a block of memory h_length
bytes long. But in other contexts there is an implicit assumption that you can convert this to a struct in_addr
or an unsigned long int
. Host addresses in a struct hostent
structure are always given in network byte order; see Byte Order.
You can use gethostbyname
or gethostbyaddr
to search the hosts database for information about a particular host. The information is returned in a statically-allocated structure; you must copy the information if you need to save it across calls.
gethostbyname
function returns information about the host named name. If the lookup fails, it returns a null pointer.
gethostbyaddr
function returns information about the host with Internet address addr. The length argument is the size (in bytes) of the address at addr. format specifies the address format; for an Internet address, specify a value of AF_INET
.
If the lookup fails, gethostbyaddr
returns a null pointer.
If the name lookup by gethostbyname
or gethostbyaddr
fails, you can find out the reason by looking at the value of the variable h_errno
. (It would be cleaner design for these functions to set errno
, but use of h_errno
is compatible with other systems.) Before using h_errno
, you must declare it like this:
extern int h_errno;
Here are the error codes that you may find in h_errno
:
HOST_NOT_FOUND
TRY_AGAIN
NO_RECOVERY
NO_ADDRESS
You can also scan the entire hosts database one entry at a time using sethostent
, gethostent
, and endhostent
. Be careful in using these functions, because they are not reentrant.
gethostent
to read the entries.
If the stayopen argument is nonzero, this sets a flag so that subsequent calls to gethostbyname
or gethostbyaddr
will not close the database (as they usually would). This makes for more efficiency if you call those functions several times, by avoiding reopening the database for each call.