gethostbyname(3N) gethostbyname(3N) NAME gethostbyname, gethostbyname_r, gethostbyaddr, gethostbyaddr_r, gethostent, gethostent_r, fgethostent, fgethostent_r, sethostent, endhostent, herror, hstrerror - get network host entry SYNOPSIS #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> extern int h_errno; struct hostent *gethostbyname(const char *name); struct hostent *gethostbyname_r(const char *name, struct hostent *hent, char *buffer, int bufsize, int *h_errnop); struct hostent *gethostbyaddr(const void *addr, socklen_t addrlen, int type); struct hostent *gethostbyaddr_r(const void *addr, size_t addrlen, int type, struct hostent *hent, char *buffer, int bufsize, int *h_errnop); struct hostent *gethostent(void); struct hostent *gethostent_r(struct hostent *hent, char buffer, int bufsize); struct hostent *fgethostent(FILE *f); struct hostent *fgethostent_r(FILE *f, struct hostent *hent, char buffer, int bufsize); void sethostent(int stayopen); void endhostent(void); void herror(const char *string); char *hstrerror(int err); DESCRIPTION gethostbyname, gethostbyaddr and their reentrant counterpoints each return a pointer to a hostent data structure describing an Internet host referenced by name or by address, respectively. This structure contains either the information obtained from broken-out fields from a line in /etc/hosts. or some other back-end hosts database. 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 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 host. Host addresses are returned in network byte order. h_addr The first address in h_addr_list; this is for backward compatibility. The name argument to gethostbyname is a character string containing an Internet hostname or an Internet address in standard dot notation (see inet(3N)). If the name contains no dot, and if the environment variable HOSTALIASES contains the name of an alias file, the alias file is first searched for an alias matching the input name. See hostname(5) for the alias file format. The addr argument to gethostbyaddr points to a buffer containing a 32-bit Internet host address in network byte order. addrlen contains the address length in bytes; it should be set to sizeof(struct in_addr). type specifies the address family and should be set to AF_INET. The gethostbyname and gethostbyaddr routines only parse /etc/hosts format configuration files. An external data supplier nsd(1M) may be used to provide data from another source such as the Domain Name Service daemon named, or the NIS databases. An administrator may specify a default ordering of the services in the nsd configuration file /etc/nsswitch.conf. When using the Domain Name Server, gethostbyname searches for the named host in the current domain and its parents unless the name ends in a dot. See hostname(5) for the domain search procedure. Also when using the name server, gethostbyname restricts the length of each subdomain name in a hostname to at most 63 characters. When nsd is running, gethostent obtains the next entry in the hosts.byaddr table. sethostent and endhostent reset the pointer into the map to the beginning. If nsd is not running, gethostent reads the next line of /etc/hosts, opening the file if necessary. sethostent opens and rewinds the file. If the stayopen flag is non-zero, the file is not closed after each call to gethostbynameorgethostbyaddr. endhostent closes the file. The routines fgethostent and fgethostent_r will return the next line from the supplied file broken out into a hostent structure. This file must be of the same format as /etc/hosts . The routines gethostbyname, gethostbyaddr, gethostent, and fgethostent all return pointers to data in a static space. Reentrant versions of each of these are supplied as gethostbyname_r, gethostbyaddr_r, gethostent_r and fgethostent_r. These routines parse the results into passed in space. They each have extra arguments for an address to a struct hostent structure and a flat buffer. DIAGNOSTICS Error return status from gethostbyname and gethostbyaddr is indicated by return of a null (0) pointer. The global integer h_errno can 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 to file descriptor 2 (standard error) describing the failure. If its argument string is non-NULL, it is printed, followed by a colon and a space. The error message is printed with a trailing newline. To simplify variant formatting of messages, hstrerror takes an error number (typically h_errno) and returns the corresponding message string. 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. 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 results in an answer; for example, a mail-forwarder can be registered for this domain. NOTE The Mips ABI specifies nothing but local files so applications which wish to use anything else must compile with libc prior to libnsl in the library list. When nsd is running changes to the local host file may not be noticed by gethostent() until the enumeration cache file has timed out. FILES /etc/hosts /etc/nsswitch.conf /etc/resolv.conf contains addresses of remote name servers ENVIRONMENT HOSTALIASES contains hostname aliases getaddrinfo(3), getnameinfo(3), getipnodebyname(3), named(1M), nsd(1N), gethostent(3N), resolver(3N), hosts(4), resolver(4), hostname(5). IRIX Network Programming Guide IRIX Admin: Networking and Mail BUGS Only the Internet address format is currently understood. Page 4