|
NAMEgethostbyname , gethostbyname2 ,
gethostbyaddr , gethostent ,
sethostent , endhostent ,
herror , hstrerror —
get network host entry
LIBRARYStandard C Library (libc, -lc)SYNOPSIS#include <netdb.h>
int h_errno; struct hostent *
struct hostent *
struct hostent *
struct hostent *
void
void
void
const char *
DESCRIPTIONThe
getaddrinfo(3)
and
getnameinfo(3)
functions are preferred over the
gethostbyname (),
gethostbyname2 (), and
gethostbyaddr () functions.The The name argument passed to
The structure returned contains either the information obtained from the name server, named(8), broken-out fields from a line in /etc/hosts, or database entries supplied by the yp(8) system. The order of the lookups is controlled by the ‘hosts’ entry in nsswitch.conf(5). 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:
When using the nameserver, The The The The The FILES
EXAMPLESPrint out the hostname associated with a specific IP address:const char *ipstr = "127.0.0.1"; struct in_addr ip; struct hostent *hp; if (!inet_aton(ipstr, &ip)) errx(1, "can't parse IP address %s", ipstr); if ((hp = gethostbyaddr((const void *)&ip, sizeof ip, AF_INET)) == NULL) errx(1, "no name associated with %s", ipstr); printf("name associated with %s is %s\n", ipstr, hp->h_name); DIAGNOSTICSError return status fromgethostbyname (),
gethostbyname2 () and
gethostbyaddr () is indicated by return of a
NULL pointer. The 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 is printed, followed by a colon and a
space. The error message is printed with a trailing newline.
The variable h_errno can have the following values:
SEE ALSOgetaddrinfo(3), getnameinfo(3), inet_aton(3), resolver(3), hosts(5), hostname(7), named(8)HISTORYTheherror () function appeared in
4.3BSD. The endhostent (),
gethostbyaddr (),
gethostbyname (), gethostent (),
and sethostent () functions appeared in
4.2BSD. The gethostbyname2 ()
function first appeared in BIND version 4.9.4.
CAVEATSThegethostent () function is defined, and
sethostent () and endhostent ()
are redefined, when Standard C Library (libc,
-lc) is built to use only the routines to lookup in
/etc/hosts and not the name server.
The The The BUGSThese functions use a thread-specific data storage; if the data is needed for future use, it should be copied before any subsequent calls overwrite it.Though these functions are thread-safe, still it is recommended to use the getaddrinfo(3) family of functions, instead. Only the Internet address format is currently understood.
Visit the GSP FreeBSD Man Page Interface. |