GETHOSTBYNAME(3) Linux Programmer's Manual GETHOSTBYNAME(3)
NAME
gethostbyname, gethostbyaddr, sethostent, gethostend, endhostent, her-
ror, hstrerror - get network host(1,5) entry
SYNOPSIS
#include <netdb.h>
extern int h_errno;
struct hostent *gethostbyname(const char *name);
#include <sys/socket.h> /* for AF_INET */
struct hostent *
gethostbyaddr(const void *addr, int len, int type);
void sethostent(int stayopen);
void endhostent(void);
void herror(const char *s);
const char *hstrerror(int err);
/* SYSV/POSIX extension */
struct hostent *gethostent(void);
/* GNU extensions */
struct hostent *gethostbyname2(const char *name, int af);
int gethostent_r(
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname_r(const char *name,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
int gethostbyname2_r(const char *name, int af,
struct hostent *ret, char *buf, size_t buflen,
struct hostent **result, int *h_errnop);
DESCRIPTION
The gethostbyname() function returns a structure of type hostent for
the given host(1,5) name. Here name is either a host(1,5) name, or an IPv4
address in(1,8) standard dot notation, or an IPv6 address in(1,8) colon (and pos-
sibly dot) notation. (See RFC 1884 for the description of IPv6
addresses.) If name is an IPv4 or IPv6 address, no lookup is performed
and 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(1,8) a dot and the environment
variable HOSTALIASES is set(7,n,1 builtins), the alias file(1,n) pointed to by HOSTALIASES
will first be searched for name (see hostname(7) for the file(1,n) format).
The current domain and its parents are searched unless name ends in(1,8) a
dot.
The gethostbyaddr() function returns a structure of type hostent for
the given host(1,5) address addr of length len and address type type. Valid
address types are AF_INET and AF_INET6. The host(1,5) address argument is a
pointer to a struct of a type depending on the address type, for exam-
ple a struct in_addr * (probably obtained via a call to inet_addr())
for address type AF_INET.
The sethostent() function specifies, if(3,n) stayopen is true (1), that a
connected TCP socket(2,7,n) should be used for the name server queries and
that the connection should remain open(2,3,n) during successive queries. Oth-
erwise, name server queries will use UDP datagrams.
The endhostent() function ends the use of a TCP connection for name
server queries.
The (obsolete) herror() function prints the error(8,n) message associated
with the current value of h_errno on stderr.
The (obsolete) hstrerror() function takes an error(8,n) number (typically
h_errno) and returns the corresponding message string.
The domain name queries carried out by gethostbyname() and gethost-
byaddr() use a combination of any or all of the name server named(5,8)(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(1,8)
/etc/host.conf. (See resolv+(8)). The default action is to query
named(5,8)(8), followed by /etc/hosts.
The hostent structure is defined in(1,8) <netdb.h> as follows:
struct hostent {
char *h_name; /* official name of host(1,5) */
char **h_aliases; /* alias list */
int h_addrtype; /* host(1,5) 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
A zero-terminated array of alternative names for the host.
h_addrtype
The type of address; always AF_INET or AF_INET6 at present.
h_length
The length of the address in(1,8) bytes.
h_addr_list
A zero-terminated array of network addresses for the host(1,5) in(1,8)
network byte order.
h_addr The first address in(1,8) h_addr_list for backward compatibility.
RETURN VALUE
The gethostbyname() and gethostbyaddr() functions return the hostent
structure or a NULL pointer if(3,n) an error(8,n) occurs. On error(8,n), the h_errno
variable holds an error(8,n) 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(1,5) is unknown.
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(8,n) occurred.
TRY_AGAIN
A temporary error(8,n) occurred on an authoritative name server. Try
again later.
FILES
/etc/host.conf
resolver(3,5) configuration file(1,n)
/etc/hosts
host(1,5) database file(1,n)
CONFORMING TO
BSD 4.3.
SYSV/POSIX EXTENSION
POSIX requires the gethostent() call, that should return the next entry
in(1,8) the host(1,5) data base. When using DNS/BIND this does not make much
sense, but it may be reasonable if(3,n) the host(1,5) data base is a file(1,n) that
can be read(2,n,1 builtins) line by line. On many systems a routine of this name reads
from the file(1,n) /etc/hosts. It may be available only when the library
was built without DNS support. The glibc version(1,3,5) will ignore ipv6
entries. This function is not reentrant, and glibc adds a reentrant
version(1,3,5) gethostent_r().
GNU EXTENSIONS
Glibc2 also has a gethostbyname2() that works like gethostbyname(), but
permits to specify the address family to which the address must belong.
Glibc2 also has reentrant versions gethostbyname_r() and gethostby-
name2_r(). These return 0 on success and nonzero on error. The result
of the call is now stored in(1,8) the struct with address ret. After the
call, *result will be NULL on error(8,n) or point to the result on success.
Auxiliary data is stored in(1,8) the buffer buf of length buflen. (If the
buffer is too small, these functions will return ERANGE.) No global
variable h_errno is modified, but the address of a variable in(1,8) which to
store error(8,n) numbers is passed in(1,8) h_errnop.
NOTES
The functions gethostbyname() and 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.
The SUS-v2 standard is buggy and declares the len parameter of gethost-
byaddr() to be of type size_t. (That is wrong, because it has to be
int, and size_t is not. POSIX 1003.1-2001 makes it socklen_t, which is
OK.)
The BSD prototype for gethostbyaddr() uses const char * for the first
argument.
POSIX 1003.1-2001 marks gethostbyaddr() and gethostbyname() obsoles-
cent. See getaddrinfo(3), getnameinfo(3), gai_strerror(3).
SEE ALSO
getaddrinfo(3), getipnodebyaddr(3), getipnodebyname(3), getnameinfo(3),
inet_ntop(3), inet_pton(3), resolver(3,5)(3), hosts(5), hostname(7),
named(5,8)(8), resolv+(8)
2004-10-31 GETHOSTBYNAME(3)