-> Click here to learn how to get live help <-
NAMEgethostbyname, gethostbyaddr, sethostent, endhostent, herror, hstrerror - get network host entry
#include <netdb.h> extern int h_errno; struct hostent *gethostbyname(const char *name); #include <sys/socket.h> /* for AF_INET */ struct hostent *gethostbyaddr(const char *addr, int len, int type); void sethostent(int stayopen); void endhostent(void); void herror(const char *s); const char *hstrerror(int err); /* GNU extensions */
DESCRIPTIONThe fBgethostbyname()fP function returns a structure of type fIhostentfP for the given host fInamefP. Here fInamefP 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 gethostbyname() simply copies name into the h_name field and its struct in_addr equivalent into the h_addr_list field of the returned hostent structure. If fInamefP doesn't end in a dot and the environment variable fBHOSTALIASESfP is set, the alias file pointed to by fBHOSTALIASESfP will first be searched for fInamefP (see hostname(7) for the file format). The current domain and its parents are searched unless fInamefP ends in a dot.
The fBgethostbyaddr()fP function returns a structure of type fIhostentfP for the given host address fIaddrfP of length fIlenfP and address type fItypefP. Valid address types are AF_INET and AF_INET6.
The fBsethostent()fP function specifies, if fIstayopenfP 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 fBendhostent()fP function ends the use of a TCP connection for name server queries.
The (obsolete) fBherror()fP function prints the error message associated with the current value of fIh_errnofP on stderr.
The (obsolete) fBhstrerror()fP function takes an error number (typically fIh_errnofP) and returns the corresponding message string.
The domain name queries carried out by fBgethostbyname()fP and fBgethostbyaddr()fP use a combination of any or all of the name server fBnamedfP(8), a broken out line from fI/etc/hostsfP, and the Network Information Service (NIS or YP), depending upon the contents of the fIorderfP line in fI/etc/host.conffP. (See resolv+(8)). The default action is to query fBnamedfP(8), followed by fI/etc/hostsfP.
The fIhostentfP structure is defined in fI<netdb.h>fP as follows:
The members of the fIhostentfP structure are:
RETURN VALUEThe fBgethostbyname()fP and fBgethostbyaddr()fP functions return the fIhostentfP structure or a NULL pointer if an error occurs. On error, the fIh_errnofP variable holds an error number. When non-NULL, the return value may point at static data, see the Notes below.
ERRORSThe variable fIh_errnofP can have the following values:
CONFORMING TOBSD 4.3.
NOTESThe SUS-v2 standard is buggy and declares the len parameter of gethostbyaddr() 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 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.
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 gethostbyname2_r(). These return 0 on success and nonzero on error. The result of the call is now stored in the struct with address 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 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 which to store error numbers is passed in h_errnop.
SEE ALSOgetaddrinfo(3), getipnodebyaddr(3), getipnodebyaddr(3), getnameinfo(3), inet_ntop(3), inet_pton(3), resolver(3), hosts(5), hostname(7), resolv+(8), named(8)