gethostbyname(3) — Subroutines
NAME
gethostbyname, gethostbyname_r − Get a network host entry by name
LIBRARY
Standard C Library (libc.a)
SYNOPSIS
#include <netdb.h>
struct hostent ∗gethostbyname(
const char ∗name);
int gethostbyname_r(
const char ∗name,
struct hostent ∗hptr,
struct hostent_data ∗hdptr);
PARAMETERS
nameSpecifies the official network name or alias.
hptrPoints to the hostent structure.
hdptrIs data for hosts database.
DESCRIPTION
The gethostbyname() function returns a pointer to a structure of type hostent. Its members specify data obtained from either the local /etc/hosts file or one of the files distributed by BIND or NIS. To determine which file or files to search, and in which order, the system uses the switches in the /etc/svc.conf file. The netdb.h header file defines the hostent structure.
If using BIND, the information is obtained from a name server specified in the /etc/resolv.conf file.
If the name parameter does not contain a dot "." and you are using BIND, the gethostbyname() function checks whether the environment variable HOSTALIASES is set. If set, it first searches the file named by HOSTALIASES for an alias matching the name parameter. The alias file has the following format:
name1 name2
name1The alias name. This name cannot include dots.
name2The host name used by BIND to look up the host information. The hosts database must be distributed by BIND.
The gethostbyname_r() function is the reentrant version of gethostbyname(). Upon successful completion, the returned structure is stored in hptr. The netdb.h header file defines the hostent and hostent_data structures.
NOTES
For gethostbyname(), a return value points to static data, which is overwritten by any subsequent calls.
You must zero-fill the hdptr structure before its first access by the gethostbyname_r() function.
RETURN VALUES
Upon successful completion of gethostbyname(), a pointer to a hostent structure is returned. A null pointer is returned whenever the end of the /etc/hosts network hostname file is reached.
Upon successful completion of gethostbyname_r(), a a value of 0 (zero) is returned. Upon error, a value of -1 is returned.
ERRORS
If the gethostbyname() or gethostbyname_r() function call fails, h_errno is set to one of the following the values:
[HOST_NOT_FOUND]
The name you have used is not an official hostname or alias; this is not a soft error, another type of name server request may be successful.
[NO_ADDRESS]
The requested name is valid but does not have an Internet address at the name server.
[NO_RECOVERY]
This is a nonrecoverable error.
[TRY_AGAIN]
This is a soft error that indicates that the local server did not receive a response from an authoritative server. A retry at some later time may be successful.
If the name, hptr, or hdptr parameter is invalid, the function sets errno to [EINVAL].
FILES
/etc/hostsThe DARPA Internet network hostname database. Each record in the file occupies a single line and has three fields consisting of the host address, official hostname, and aliases.
/etc/resolv.conf
The resolver configuration file.
RELATED INFORMATION
Functions: gethostbyaddr(3), endhostent(3).
Files: hostname(5), resolv.conf(4), svc.conf(4).