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 `18.104.22.168' 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'.
AF_INET6, with the latter being used for IPv6 hosts. 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_INET6. See section Socket Addresses.
h_addr_list; in other words, it is the first host address.
As far as the host database is concerned, each address is just a block
h_length bytes long. But in other contexts there is an
implicit assumption that you can convert this to a
struct in_addr or
unsigned long int. Host addresses in a
structure are always given in network byte order; see section Byte Order Conversion.
You can use
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. You can also use
getnameinfo to obtain this information.
gethostbynamefunction returns information about the host named name. If the lookup fails, it returns a null pointer.
gethostbyname2function is like
gethostbyname, but allows the caller to specify the desired address family (e.g.
AF_INET6) for the result.
gethostbyaddrfunction 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
If the lookup fails,
gethostbyaddr returns a null pointer.
If the name lookup by
fails, you can find out the reason by looking at the value of the
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
extern int h_errno;
Here are the error codes that you may find in
You can also scan the entire hosts database one entry at a time using
endhostent. Be careful
in using these functions, because they are not reentrant.
gethostentto read the entries.
If the stayopen argument is nonzero, this sets a flag so that
subsequent calls to
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.
Go to the first, previous, next, last section, table of contents.