161150Sbostic.\" Copyright (c) 1983, 1987, 1991, 1993 261150Sbostic.\" The Regents of the University of California. All rights reserved. 320518Smckusick.\" 443572Strent.\" %sccs.include.redist.man% 520518Smckusick.\" 6*69691Smckusick.\" @(#)gethostbyname.3 8.4 (Berkeley) 05/25/95 736739Sbostic.\" 848352Scael.Dd 948352Scael.Dt GETHOSTBYNAME 3 1048352Scael.Os BSD 4.2 1148352Scael.Sh NAME 1248352Scael.Nm gethostbyname , 1348352Scael.Nm gethostbyaddr , 1448352Scael.Nm gethostent , 1548352Scael.Nm sethostent , 1648352Scael.Nm endhostent , 1748352Scael.Nm herror 1848352Scael.Nd get network host entry 1948352Scael.Sh SYNOPSIS 2048352Scael.Fd #include <netdb.h> 2168453Smckusick.Fd extern int h_errno; 2248352Scael.Ft struct hostent * 2348352Scael.Fn gethostbyname "char *name" 2448352Scael.Ft struct hostent * 2548352Scael.Fn gethostbyaddr "char *addr" "int len" "int type" 2648352Scael.Ft struct hostent * 2748352Scael.Fn gethostent void 2848352Scael.Fn sethostent "int stayopen" 2948352Scael.Fn endhostent void 3048352Scael.Fn herror "char *string" 3148352Scael.Sh DESCRIPTION 3248352ScaelThe 3348352Scael.Fn gethostbyname 3420518Smckusickand 3548352Scael.Fn gethostbyaddr 3648352Scaelfunctions 3720518Smckusickeach return a pointer to an object with the 3832653Skarelsfollowing structure describing an internet host 3932653Skarelsreferenced by name or by address, respectively. 4028062SkjdThis structure contains either the information obtained from the name server, 4148352Scael.Xr named 8 , 4228278Slepreauor broken-out fields from a line in 4348352Scael.Pa /etc/hosts . 4428062SkjdIf the local name server is not running these routines do a lookup in 4548352Scael.Pa /etc/hosts . 4650728Scael.Bd -literal 4720518Smckusickstruct hostent { 4820518Smckusick char *h_name; /* official name of host */ 4920518Smckusick char **h_aliases; /* alias list */ 5028062Skjd int h_addrtype; /* host address type */ 5120518Smckusick int h_length; /* length of address */ 5228062Skjd char **h_addr_list; /* list of addresses from name server */ 5320518Smckusick}; 5428062Skjd#define h_addr h_addr_list[0] /* address, for backward compatibility */ 5548352Scael.Ed 5648352Scael.Pp 5720518SmckusickThe members of this structure are: 5848352Scael.Bl -tag -width h_addr_list 5948352Scael.It Fa h_name 6020518SmckusickOfficial name of the host. 6148352Scael.It Fa h_aliases 62*69691SmckusickA NULL-terminated array of alternate names for the host. 6348352Scael.It Fa h_addrtype 6448352ScaelThe type of address being returned; currently always 6548352Scael.Dv AF_INET . 6648352Scael.It Fa h_length 6720518SmckusickThe length, in bytes, of the address. 6848352Scael.It Fa h_addr_list 69*69691SmckusickA NULL-terminated array of network addresses for the host. 7028062SkjdHost addresses are returned in network byte order. 7148352Scael.It Fa h_addr 7248352ScaelThe first address in 7348352Scael.Fa h_addr_list ; 7466945Sbosticthis is for backward compatibility. 75*69691Smckusick.El 7648352Scael.Pp 7732653SkarelsWhen using the nameserver, 7848352Scael.Fn gethostbyname 7932653Skarelswill search for the named host in the current domain and its parents 8032653Skarelsunless the name ends in a dot. 8148352ScaelIf the name contains no dot, and if the environment variable 8248352Scael.Dq Ev HOSTALIASES 8332653Skarelscontains the name of an alias file, the alias file will first be searched 8432653Skarelsfor an alias matching the input name. 8532653SkarelsSee 8648352Scael.Xr hostname 7 8732653Skarelsfor the domain search procedure and the alias file format. 8848352Scael.Pp 8948352ScaelThe 9048352Scael.Fn sethostent 9148352Scaelfunction 9248352Scaelmay be used to request the use of a connected 9348352Scael.Tn TCP 9448352Scaelsocket for queries. 9528062SkjdIf the 9648352Scael.Fa stayopen 9720518Smckusickflag is non-zero, 9848352Scaelthis sets the option to send all queries to the name server using 9948352Scael.Tn TCP 10028062Skjdand to retain the connection after each call to 10148352Scael.Fn gethostbyname 10227936Slepreauor 10348352Scael.Fn gethostbyaddr . 10448352ScaelOtherwise, queries are performed using 10548352Scael.Tn UDP 10648352Scaeldatagrams. 10748352Scael.Pp 10848352ScaelThe 10948352Scael.Fn endhostent 11048352Scaelfunction 11148352Scaelcloses the 11248352Scael.Tn TCP 11348352Scaelconnection. 11448352Scael.Sh FILES 11548352Scael.Bl -tag -width /etc/hosts -compact 11648352Scael.It Pa /etc/hosts 11748352Scael.El 11848352Scael.Sh DIAGNOSTICS 11928062SkjdError return status from 12048352Scael.Fn gethostbyname 12120518Smckusickand 12248352Scael.Fn gethostbyaddr 12328062Skjdis indicated by return of a null pointer. 12428062SkjdThe external integer 12548352Scael.Va h_errno 12628062Skjdmay then be checked to see whether this is a temporary failure 12728062Skjdor an invalid or unknown host. 12832649SkarelsThe routine 12948352Scael.Fn herror 13032649Skarelscan be used to print an error message describing the failure. 13132649SkarelsIf its argument 13248352Scael.Fa string 13348352Scaelis 13448352Scael.Pf non Dv -NULL , 13548352Scaelit is printed, followed by a colon and a space. 13632649SkarelsThe error message is printed with a trailing newline. 13748352Scael.Pp 13848352ScaelThe variable 13948352Scael.Va h_errno 14028062Skjdcan have the following values: 14148352Scael.Bl -tag -width HOST_NOT_FOUND 14248352Scael.It Dv HOST_NOT_FOUND 14328062SkjdNo such host is known. 14448352Scael.It Dv TRY_AGAIN 14528062SkjdThis is usually a temporary error 14628062Skjdand means that the local server did not receive 14728062Skjda response from an authoritative server. 14828062SkjdA retry at some later time may succeed. 14948352Scael.It Dv NO_RECOVERY 15032649SkarelsSome unexpected server failure was encountered. 15128062SkjdThis is a non-recoverable error. 15248352Scael.It Dv NO_DATA 15328062SkjdThe requested name is valid but does not have an IP address; 15428062Skjdthis is not a temporary error. 15533978SkarelsThis means that the name is known to the name server but there is no address 15633978Skarelsassociated with this name. 15733978SkarelsAnother type of request to the name server using this domain name 15832649Skarelswill result in an answer; 15932649Skarelsfor example, a mail-forwarder may be registered for this domain. 16048352Scael.El 16148352Scael.Sh SEE ALSO 16248352Scael.Xr resolver 3 , 16348352Scael.Xr hosts 5 , 16448352Scael.Xr hostname 7 , 16548352Scael.Xr named 8 16648352Scael.Sh CAVEAT 16748352ScaelThe 16848352Scael.Fn gethostent 16948352Scaelfunction 17028062Skjdis defined, and 17148352Scael.Fn sethostent 17228278Slepreauand 17348352Scael.Fn endhostent 17428278Slepreauare redefined, 17528062Skjdwhen 17648352Scael.Xr libc 3 17728062Skjdis built to use only the routines to lookup in 17848352Scael.Pa /etc/hosts 17928062Skjdand not the name server. 18048352Scael.Pp 18148352ScaelThe 18248352Scael.Fn gethostent 18348352Scaelfunction 18428062Skjdreads the next line of 18548352Scael.Pa /etc/hosts , 18628062Skjdopening the file if necessary. 18748352Scael.Pp 18848352ScaelThe 18948352Scael.Fn sethostent 19048352Scaelfunction 19156501Sbosticopens and/or rewinds the file 19256501Sbostic.Pa /etc/hosts . 19356501SbosticIf the 19448352Scael.Fa stayopen 19528278Slepreauargument is non-zero, 19656501Sbosticthe file will not be closed after each call to 19748352Scael.Fn gethostbyname 19828062Skjdor 19948352Scael.Fn gethostbyaddr . 20056501Sbostic.Pp 20148352ScaelThe 20248352Scael.Fn endhostent 20348352Scaelfunction 20456501Sbosticcloses the file. 20548352Scael.Sh HISTORY 20648352ScaelThe 20748352Scael.Fn herror 20848352Scaelfunction appeared in 20948352Scael.Bx 4.3 . 21048352ScaelThe 21148352Scael.Fn endhostent , 21248352Scael.Fn gethostbyaddr , 21348352Scael.Fn gethostbyname , 21448352Scael.Fn gethostent , 21548352Scaeland 21648352Scael.Fn sethostent 21748352Scaelfunctions appeared in 21848352Scael.Bx 4.2 . 21948352Scael.Sh BUGS 22048352ScaelThese functions use static data storage; 22148352Scaelif the data is needed for future use, it should be 22248352Scaelcopied before any subsequent calls overwrite it. 22348352ScaelOnly the Internet 22420518Smckusickaddress format is currently understood. 225