GETHOSTBYNAME
NAME
gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
herror, hstrerror - get network host entry
SYNOPSIS
#include <netdb.h>
extern int h_errno;
I struct hostent *gethostbyname(const char * name );
R #include <sys/socket.h> /* for AF_INET */
I struct hostent *gethostbyaddr(const void * addr \
", socklen_t " len ", int " type );
I void sethostent(int stayopen );
void endhostent(void);
I void herror(const char * s );
I const char *hstrerror(int err );
/* System V/POSIX extension */
struct hostent *gethostent(void);
/* GNU extensions */
I struct hostent *gethostbyname2(const char * name , int af );
I int gethostent_r(
I struct hostent * ret , char * buf , size_t buflen ,
I struct hostent ** result , int * h_errnop );
I int gethostbyname_r(const char * name ,
I struct hostent * ret , char * buf , size_t buflen ,
I struct hostent ** result , int * h_errnop );
I int gethostbyname2_r(const char * name , int af,
I struct hostent * ret , char * buf , size_t buflen ,
I struct hostent ** result , int * h_errnop );
DESCRIPTION
The
R gethostbyname ()
function returns a structure of type
hostent
for the given host
R name .
Here
name
is either a host name, or an IPv4 address in standard dot notation,
or an IPv6 address in colon (and possibly dot) notation.
(See RFC 1884 for the description of IPv6 addresses.)
If
name
is an IPv4 or IPv6 address, no lookup is performed and
R gethostbyname ()
simply copies
name
into the
h_name
field and its
struct in_addr
equivalent into the
h_addr_list[0]
field of the returned
hostent
structure.
If
name
doesn't end in a dot and the environment variable
HOSTALIASES
is set, the alias file pointed to by
HOSTALIASES
will first be searched for
name
(see
hostname(7)
for the file format).
The current domain and its parents are searched unless
name
ends in a dot.
The
R gethostbyaddr ()
function returns a structure of type
hostent
for the given host address
addr of length
len and address type
type.
Valid address types are
AF_INET
and
R AF_INET6 .
The host address argument is a pointer to a struct of a type depending
on the address type, for example a
struct in_addr * (probably
obtained via a call to
inet_addr(3))
for address type
R AF_INET .
The
R sethostent ()
function specifies, if stayopen is true (1),
that a connected TCP socket should be used for the name server queries and
that the connection should remain open during successive queries.
Otherwise, name server queries will use UDP datagrams.
The
R endhostent ()
function ends the use of a TCP connection for name
server queries.
The (obsolete)
R herror ()
function prints the error message associated
with the current value of h_errno on stderr.
The (obsolete)
R hstrerror ()
function takes an error number
(typically h_errno) and returns the corresponding message string.
The domain name queries carried out by
R gethostbyname ()
and
R gethostbyaddr ()
use a combination of any or all of the name server
named(8),
a broken out line from
/etc/hosts, and the Network
Information Service (NIS or YP), depending upon the contents of the
order line in
R /etc/host.conf .
The default action is to query
named(8),
followed by
R /etc/hosts .
The
hostent structure is defined in
<netdb.h> as follows:
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#define h_addr h_addr_list[0] /* for backward compatibility */
The members of the hostent structure are:
h_name
The official name of the host.
h_aliases
An array of alternative names for the host, terminated by a NULL pointer.
h_addrtype
The type of address; always
AF_INET
or
AF_INET6
at present.
h_length
The length of the address in bytes.
h_addr_list
An array of pointers to network addresses for the host (in network byte
order), terminated by a NULL pointer.
h_addr
The first address in h_addr_list for backward compatibility.
RETURN VALUE
The
R gethostbyname ()
and
R gethostbyaddr ()
functions return the
hostent
structure or a NULL pointer if an error occurs.
On error, the
h_errno
variable holds an error number.
When non-NULL, the return value may point at static data, see the notes below.
ERRORS
The variable h_errno can have the following values:
HOST_NOT_FOUND
The specified host is unknown.
R NO_ADDRESS or NO_DATA
The requested name is valid but does not have an IP address.
NO_RECOVERY
A non-recoverable name server error occurred.
TRY_AGAIN
A temporary error occurred on an authoritative name server.
Try again later.
FILES
/etc/host.conf
resolver configuration file
/etc/hosts
host database file
/etc/nsswitch.conf
name service switch configuration
CONFORMING TO
4.3BSD, POSIX.1-2001.
NOTES
The functions
R gethostbyname ()
and
R gethostbyaddr ()
may return pointers to static data, which may be overwritten by
later calls.
Copying the
struct hostent
does not suffice, since it contains pointers; a deep copy is required.
In the original BSD implementation the
len
argument
of
R gethostbyname ()
was an
R int .
The SUSv2 standard is buggy and declares the
len
parameter of
R gethostbyaddr ()
to be of type
R size_t .
(That is wrong, because it has to be
R int ,
and
size_t
is not.
POSIX.1-2001 makes it
R socklen_t ,
which is OK.)
See also
accept(2).
The BSD prototype for
R gethostbyaddr ()
uses
const char *
for the first argument.
POSIX.1-2001 marks
R gethostbyaddr ()
and
R gethostbyname ()
obsolescent.
See
getaddrinfo(3),
getnameinfo(3),
gai_strerror(3).
System V/POSIX Extension
POSIX requires the
R gethostent ()
call, that should return the next entry in the host data base.
When using DNS/BIND this does not make much sense, but it may
be reasonable if the host data base is a file that can be read
line by line.
On many systems a routine of this name reads
from the file
R /etc/hosts .
It may be available only when the library was built without DNS support.
The glibc version will ignore ipv6 entries.
This function is not reentrant,
and glibc adds a reentrant version
R gethostent_r ().
GNU Extensions
Glibc2 also has a
R gethostbyname2 ()
that works like
R gethostbyname (),
but permits to specify the address family to which the address must belong.
Glibc2 also has reentrant versions
R gethostbyname_r ()
and
R gethostbyname2_r ().
These return 0 on success and non-zero on error.
The result of the call
is now stored in the struct with address
R ret .
After the call,
*result
will be NULL on error or point to the result on success.
Auxiliary data is stored in the buffer
buf
of length
R buflen .
(If the buffer is too small, these functions will return
R ERANGE .)
No global variable
h_errno
is modified, but the address of a variable in which to store error numbers
is passed in
R h_errnop .
SEE ALSO
getaddrinfo(3),
getipnodebyaddr(3),
getipnodebyname(3),
getnameinfo(3),
inet_ntop(3),
inet_pton(3),
resolver(3),
hosts(5),
nsswitch.conf(5),
hostname(7),
named(8)