gethostent(3N) — NETWORK FUNCTIONS
NAME
gethostent, gethostbyaddr, gethostbyname, sethostent, endhostent, herror − get network host entry
SYNOPSIS
cc [flags] files -lsocket -lnsl
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
extern int h_errno;
struct hostent ∗gethostbyname(name)
char ∗name;
struct hostent ∗gethostbyaddr(addr, len, type)
char ∗addr;
int len, type;
struct hostent ∗gethostent()
sethostent(stayopen)
int stayopen;
endhostent()
herror(string)
char ∗string:
DESCRIPTION
The gethostent(), gethostbyaddr(), and gethostbyname() system calls each return a pointer to an object with the following structure which describes at internet host referenced by name or by address, respectively. This structure contains either the information obtained from the name server, named, or broken-out fields from a line in the network host data base, /etc/hosts. If the local name server is not running, these routines do a lookup in /etc/hosts. In the case of gethostbyaddr(), addr is a pointer to the binary format address of length len (not a character string).
The hostent structure is 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 from name server ∗/ } |
| #define | h_addr h_addr_list[0] | /∗ address, for backward compatibility ∗/ |
The members of this structure are:
h_name The official name of the host.
h_aliases A zero-terminated array of alternate names for the host.
h_addrtype The type of address being returned; currently always AF_INET.
h_length The length, in bytes, of the address.
h_addr_list A zero-terminated array of network addresses for the named host; the host addresses are returned in network byte order.
h_addr The first address in h_addr_list; this is for backward compatibility.
When using the nameserver, gethostbyname() will search for the named host in the current domain and its parents unless the name ends in a dot ("."). It the name contains no dot − and if the environment variable HOSTALIASES contains the name of an alias file − this alias file will be searched first for an alias matching the input name.
The gethostent() system call will read the next line of the file, after opening the file if necessary.
The sethostent() system call will open and rewind the file; sethostent() may be used to request the use of a connected TCP socket for queries. If the stayopen flag is non-zero, the host data base will not be closed after each call to gethostent() − either directly, or indirectly through one of the other gethost∗ calls. In other words, if the stayopen flag is non-zero, this option will send all queries to the named served using TCP and maintain the connection after each call of gethostbyname or gethostbyaddr; otherwise the queries will utilize UDP datagrams.
The endhostent() system call will close the file and the TCP connection.
The gethostbyname() and gethostbyaddr() system calls will search sequentially from the beginning of the file until a matching host name or host address is found, or until an EOF is encountered. The host addresses will be supplied in network order.
The gethostbyaddr() system call will accept a pointer to an address structure. This structure will be unique to each type of address. For an address of type AF_INET, this is an in_addr structure [see netinet/in.h].
DIAGNOSTICS
A NULL pointer will be returned at EOF or when an error has occurred.
An error return status from gethostbyname and gethostbyaddr will be indicated by a NULL pointer. The external integer h_errno may then be checked to see whether this is a temporary failure, or an invalid or unknown host. The routine herror can be used to print an error message describing the failure. If its argument string is non-NULL, it will be printed, followed by a colon and a space. The error message will be printed with a trailing newline symbol.
h_errno can have the following values:
HOST_NOT_FOUND
No such host is known.
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.
NO_RECOVERY
Some unexpected server failure was encountered (This is a non-recoverable error).
NO_DATA The requested name is valid, but does not have an IP address. This is not a temporary error: instead, 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 should result in an answer (for example, a "mail-forwarder" may be registered for this domain).
USER CONSIDERATIONS
Since all information will be stored in a static area it must be copied if it is to be saved.
Only the Internet address format is currently supported.
FILES
/etc/hosts