p The members of this structure are: l -tag -width h_addr_list t Fa h_name Official name of the host. t Fa h_aliases A NULL-terminated array of alternative names for the host. t Fa h_addrtype The type of address being returned; currently always .Dv AF_INET . t Fa h_length The length, in bytes, of the address. t Fa h_addr_list A NULL-terminated array of network addresses for the host. Host addresses are returned in network byte order. t Fa h_addr The first address in .Fa h_addr_list ; this is for backward compatibility. .El
p In the case of .Fn gethostbyname and .Fn gethostbyname2 , the host is specified by name, or using a string representation of a numeric address. In the case of .Fn gethostbyaddr , the host is specified using a binary representation of an address.
p The returned .Ft "struct hostent" structure may contain the result of a simple string to binary conversion, information obtained from the domain name resolver (see .Xr resolver 3 ) , broken-out fields from a line in
a /etc/hosts , or database entries supplied by the .Xr yp 8 system. The order of the lookups is controlled by the .Sq hosts entry in .Xr nsswitch.conf 5 .
p When using the domain name resolver, .Fn gethostbyname and .Fn gethostbyname2 will search for the named host in the current domain and its parents unless the name ends in a dot. If the name contains no dot, and if the environment variable .Dq Ev HOSTALIASES contains the name of an alias file, the alias file will first be searched for an alias matching the input name. See .Xr hostname 7 for the domain search procedure and the alias file format.
p The .Fn gethostbyname2 function is an evolution of .Fn gethostbyname which is intended to allow lookups in address families other than .Dv AF_INET , for example .Dv AF_INET6 . Currently the .Fa af argument must be specified as .Dv AF_INET or .Dv AF_INET6 , else the function will return .Dv NULL after having set .Va h_errno to .Dv NETDB_INTERNAL .
p The .Fn gethostent function reads the next line of the
a /etc/hosts file, opening the file if necessary.
p The .Fn sethostent function may be used to request the use of a connected .Tn TCP socket for queries. If the .Fa stayopen flag is non-zero, this sets the option to send all queries to the name server using .Tn TCP and to retain the connection after each call to .Fn gethostbyname , .Fn gethostbyname2 , or .Fn gethostbyaddr . Otherwise, queries are performed using .Tn UDP datagrams.
p The .Fn endhostent function closes the .Tn TCP connection.
p The .Fn herror function writes a message to the diagnostic output consisting of the string parameter .Fa s , the constant string ": ", and a message corresponding to the value of .Va h_errno .
p The .Fn hstrerror function returns a string which is the message text corresponding to the value of the .Fa err parameter. .Sh FILES l -tag -width /etc/hosts -compact t Pa /etc/hosts .El .Sh RETURN VALUES Error return status from .Fn gethostbyent , .Fn gethostbyname , .Fn gethostbyname2 , and .Fn gethostbyaddr is indicated by return of a null pointer. The external integer .Va h_errno may then be checked to see whether this is a temporary failure or an invalid or unknown host. The routine .Fn herror can be used to print an error message describing the failure. If its argument .Fa string is
f non Dv -NULL , it is printed, followed by a colon and a space. The error message is printed with a trailing newline.
p The variable .Va h_errno can have the following values: l -tag -width HOST_NOT_FOUND t Dv HOST_NOT_FOUND No such host is known. t Dv TRY_AGAIN This is usually a temporary error and means that the local server did not receive a response from an authoritative server. A retry at some later time may succeed. t Dv NO_RECOVERY Some unexpected server failure was encountered. This is a non-recoverable error. t Dv NO_DATA The requested name is valid but does not have an IP address; this is not a temporary error. This means that the name is known to the name server but there is no address associated with this name. Another type of request to the name server using this domain name will result in an answer; for example, a mail-forwarder may be registered for this domain. .El .Sh SEE ALSO .Xr resolver 3 , .Xr hosts 5 , .Xr nsswitch.conf 5 , .Xr hostname 7 , .Xr named 8 .Sh HISTORY The .Fn herror function appeared in x 4.3 . The .Fn endhostent , .Fn gethostbyaddr , .Fn gethostbyname , .Fn gethostent , and .Fn sethostent functions appeared in x 4.2 . The .Fn gethostbyname2 function first appeared in bind-4.9.4. IPv6 support was implemented in WIDE Hydrangea IPv6 protocol stack kit. .Sh CAVEATS If the search routines specified in .Xr nsswitch.conf 5 decide to read the
a /etc/hosts file, .Fn gethostbyname , .Fn gethostbyname2 , and .Fn gethostbyaddr will read the next line of the file, re-opening the file if necessary.
p The .Fn sethostent function opens and/or rewinds the file
a /etc/hosts . If the .Fa stayopen argument is non-zero, the file will not be closed after each call to .Fn gethostbyname , .Fn gethostbyname2 , .Fn gethostbyaddr , or .Fn gethostent .
p The .Fn endhostent function closes the file. .Sh BUGS These functions use static data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it. Only the Internet address format is currently understood.
p The .Fn gethostent does not currently follow the search order specified in .Xr nsswitch.conf 5 and only reads the