gethostbyname(3) - OpenBSD manual pages (original) (raw)

NAME

gethostbyname,gethostbyname2,gethostbyaddr, gethostent,sethostent, endhostent,hstrerror, herror —get network host entry

SYNOPSIS

#include <netdb.h>

extern int h_errno;

struct hostent *
gethostbyname(const char *name);

struct hostent *
gethostent(void);

void
sethostent(int stayopen);

void
endhostent(void);

void
herror(const char *string);

const char *
hstrerror(int err);

#include <sys/socket.h>
#include <netdb.h>

struct hostent *
gethostbyname2(const char *name, int af);

struct hostent *
gethostbyaddr(const void *addr, socklen_t len, int af);

DESCRIPTION

Thegethostbyname(),gethostbyname2(), andgethostbyaddr() functions each return a pointer to an object with the following structure describing an Internet host referenced by name or addr, respectively. This structure contains either information obtained from a name server, broken-out fields from a line in/etc/hosts, or database entries supplied by theyp(8) system.resolv.conf(5) describes how the particular database is chosen.

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 returned addresses / }; #define h_addr h_addr_list[0] / address, for backward compat */

The members of this structure are:

h_name

Official name of the host.

h_aliases

A NULL-terminated array of alternate names for the host.

h_addrtype

The type of address being returned.

h_length

The length, in bytes, of the address.

h_addr_list

A NULL-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 functiongethostbyname() will search for the named host in the current domain and its parents using the search lookup semantics detailed inresolv.conf(5) andhostname(7).

gethostbyname2() is similar to gethostbyname() except that it supports an af of AF_INET6 in addition to AF_INET.

Thegethostbyaddr() function will search for the specified address of lengthlen in the address family af. The only address family supported is AF_INET.

Thesethostent(),gethostent(), andendhostent() functions are deprecated and no longer have any effect. They could be used in the past for queries over a persistent TCP connection or to iterate entries in the hosts(5) file.

Theherror() function prints an error message describing the failure. If its argumentstring is not NULL, it is prepended to the message string and separated from it by a colon (‘:’) and a space. The error message is printed with a trailing newline. The contents of the error message is the same as that returned byhstrerror() with argument h_errno.

ENVIRONMENT

RES_OPTIONS

A list of options to override the resolver's internal defaults. Seeresolv.conf(5) for more information.

FILES

/etc/hosts

/etc/resolv.conf

DIAGNOSTICS

Error return status from gethostbyname(),gethostbyname2(), andgethostbyaddr() is indicated by return of aNULL pointer. The external integerh_errno may then be checked to see whether this is a temporary failure or an invalid or unknown host.

The variable 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 will result in an answer; for example, a mail-forwarder may be registered for this domain.

NETDB_INTERNAL

An internal error occurred. This may occur when an address family other than AF_INET or AF_INET6 is specified or when a resource is unable to be allocated. It is always set by gethostent().

NETDB_SUCCESS

The function completed successfully.

SEE ALSO

getaddrinfo(3),getnameinfo(3),res_init(3), hosts(5),resolv.conf(5),hostname(7)

HISTORY

The endhostent(),gethostbyaddr(),gethostbyname(),gethostent(), andsethostent() functions appeared in4.1cBSD. The functionherror() was added in4.3BSD-Tahoe, hstrerror() in4.4BSD, and gethostbyname2() in OpenBSD 2.1.

BUGS

These functions use static data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it.

Only the Internet address formats are currently understood.

YP does not support any address families other thanAF_INET and uses the traditional database format.