1bc7413d0SPeter Wemm.\" Copyright (c) 1983, 1987, 1991, 1993 2bc7413d0SPeter Wemm.\" The Regents of the University of California. All rights reserved. 3fdf4c7afSPeter Wemm.\" 4bc7413d0SPeter Wemm.\" Redistribution and use in source and binary forms, with or without 5bc7413d0SPeter Wemm.\" modification, are permitted provided that the following conditions 6bc7413d0SPeter Wemm.\" are met: 7bc7413d0SPeter Wemm.\" 1. Redistributions of source code must retain the above copyright 8bc7413d0SPeter Wemm.\" notice, this list of conditions and the following disclaimer. 9bc7413d0SPeter Wemm.\" 2. Redistributions in binary form must reproduce the above copyright 10bc7413d0SPeter Wemm.\" notice, this list of conditions and the following disclaimer in the 11bc7413d0SPeter Wemm.\" documentation and/or other materials provided with the distribution. 12bc7413d0SPeter Wemm.\" 3. All advertising materials mentioning features or use of this software 13bc7413d0SPeter Wemm.\" must display the following acknowledgement: 14bc7413d0SPeter Wemm.\" This product includes software developed by the University of 15bc7413d0SPeter Wemm.\" California, Berkeley and its contributors. 16bc7413d0SPeter Wemm.\" 4. Neither the name of the University nor the names of its contributors 17bc7413d0SPeter Wemm.\" may be used to endorse or promote products derived from this software 18bc7413d0SPeter Wemm.\" without specific prior written permission. 19fdf4c7afSPeter Wemm.\" 20bc7413d0SPeter Wemm.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21bc7413d0SPeter Wemm.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22bc7413d0SPeter Wemm.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23bc7413d0SPeter Wemm.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24bc7413d0SPeter Wemm.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25bc7413d0SPeter Wemm.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26bc7413d0SPeter Wemm.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27bc7413d0SPeter Wemm.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28bc7413d0SPeter Wemm.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29bc7413d0SPeter Wemm.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30bc7413d0SPeter Wemm.\" SUCH DAMAGE. 31fdf4c7afSPeter Wemm.\" 32577b61c6SPhilippe Charnier.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 337f3dea24SPeter Wemm.\" $FreeBSD$ 34bc7413d0SPeter Wemm.\" 35c293d821SPeter Wemm.Dd May 25, 1995 36bc7413d0SPeter Wemm.Dt GETHOSTBYNAME 3 37bc7413d0SPeter Wemm.Os BSD 4.2 38bc7413d0SPeter Wemm.Sh NAME 39bc7413d0SPeter Wemm.Nm gethostbyname , 40c293d821SPeter Wemm.Nm gethostbyname2 , 41bc7413d0SPeter Wemm.Nm gethostbyaddr , 42bc7413d0SPeter Wemm.Nm gethostent , 43bc7413d0SPeter Wemm.Nm sethostent , 44bc7413d0SPeter Wemm.Nm endhostent , 45c293d821SPeter Wemm.Nm herror , 46c293d821SPeter Wemm.Nm hstrerror 47bc7413d0SPeter Wemm.Nd get network host entry 48bc7413d0SPeter Wemm.Sh SYNOPSIS 49bc7413d0SPeter Wemm.Fd #include <netdb.h> 50c7091c31SBruce Evans.Vt extern int h_errno; 51bc7413d0SPeter Wemm.Ft struct hostent * 52d3281e16SJohn Polstra.Fn gethostbyname "const char *name" 53bc7413d0SPeter Wemm.Ft struct hostent * 54c293d821SPeter Wemm.Fn gethostbyname2 "const char *name" "int af" 55c293d821SPeter Wemm.Ft struct hostent * 56d3281e16SJohn Polstra.Fn gethostbyaddr "const char *addr" "int len" "int type" 57bc7413d0SPeter Wemm.Ft struct hostent * 58bc7413d0SPeter Wemm.Fn gethostent void 593ce29386SBruce Evans.Ft void 60bc7413d0SPeter Wemm.Fn sethostent "int stayopen" 613ce29386SBruce Evans.Ft void 62bc7413d0SPeter Wemm.Fn endhostent void 633ce29386SBruce Evans.Ft void 64d3281e16SJohn Polstra.Fn herror "const char *string" 65c293d821SPeter Wemm.Ft const char * 66c293d821SPeter Wemm.Fn hstrerror "int err" 67bc7413d0SPeter Wemm.Sh DESCRIPTION 68bc7413d0SPeter WemmThe 69c293d821SPeter Wemm.Fn gethostbyname , 70c293d821SPeter Wemm.Fn gethostbyname2 71fdf4c7afSPeter Wemmand 72bc7413d0SPeter Wemm.Fn gethostbyaddr 73bc7413d0SPeter Wemmfunctions 74fdf4c7afSPeter Wemmeach return a pointer to an object with the 75fdf4c7afSPeter Wemmfollowing structure describing an internet host 76fdf4c7afSPeter Wemmreferenced by name or by address, respectively. 77fdf4c7afSPeter WemmThis structure contains either the information obtained from the name server, 78bc7413d0SPeter Wemm.Xr named 8 , 79fdf4c7afSPeter Wemmor broken-out fields from a line in 80bc7413d0SPeter Wemm.Pa /etc/hosts . 81fdf4c7afSPeter WemmIf the local name server is not running these routines do a lookup in 82bc7413d0SPeter Wemm.Pa /etc/hosts . 83bc7413d0SPeter Wemm.Bd -literal 84fdf4c7afSPeter Wemmstruct hostent { 85fdf4c7afSPeter Wemm char *h_name; /* official name of host */ 86fdf4c7afSPeter Wemm char **h_aliases; /* alias list */ 87fdf4c7afSPeter Wemm int h_addrtype; /* host address type */ 88fdf4c7afSPeter Wemm int h_length; /* length of address */ 89fdf4c7afSPeter Wemm char **h_addr_list; /* list of addresses from name server */ 90fdf4c7afSPeter Wemm}; 91fdf4c7afSPeter Wemm#define h_addr h_addr_list[0] /* address, for backward compatibility */ 92bc7413d0SPeter Wemm.Ed 93bc7413d0SPeter Wemm.Pp 94fdf4c7afSPeter WemmThe members of this structure are: 95bc7413d0SPeter Wemm.Bl -tag -width h_addr_list 96bc7413d0SPeter Wemm.It Fa h_name 97fdf4c7afSPeter WemmOfficial name of the host. 98bc7413d0SPeter Wemm.It Fa h_aliases 99c293d821SPeter WemmA NULL-terminated array of alternate names for the host. 100bc7413d0SPeter Wemm.It Fa h_addrtype 101c293d821SPeter WemmThe type of address being returned; usually 102bc7413d0SPeter Wemm.Dv AF_INET . 103bc7413d0SPeter Wemm.It Fa h_length 104fdf4c7afSPeter WemmThe length, in bytes, of the address. 105bc7413d0SPeter Wemm.It Fa h_addr_list 106c293d821SPeter WemmA NULL-terminated array of network addresses for the host. 107fdf4c7afSPeter WemmHost addresses are returned in network byte order. 108bc7413d0SPeter Wemm.It Fa h_addr 109bc7413d0SPeter WemmThe first address in 110bc7413d0SPeter Wemm.Fa h_addr_list ; 111bc7413d0SPeter Wemmthis is for backward compatibility. 112c293d821SPeter Wemm.El 113bc7413d0SPeter Wemm.Pp 114fdf4c7afSPeter WemmWhen using the nameserver, 115bc7413d0SPeter Wemm.Fn gethostbyname 116c293d821SPeter Wemmand 117aec5b6f6SAlexey Zelkin.Fn gethostbyname2 118fdf4c7afSPeter Wemmwill search for the named host in the current domain and its parents 119fdf4c7afSPeter Wemmunless the name ends in a dot. 120bc7413d0SPeter WemmIf the name contains no dot, and if the environment variable 121bc7413d0SPeter Wemm.Dq Ev HOSTALIASES 122fdf4c7afSPeter Wemmcontains the name of an alias file, the alias file will first be searched 123fdf4c7afSPeter Wemmfor an alias matching the input name. 124fdf4c7afSPeter WemmSee 125bc7413d0SPeter Wemm.Xr hostname 7 126fdf4c7afSPeter Wemmfor the domain search procedure and the alias file format. 127bc7413d0SPeter Wemm.Pp 128bc7413d0SPeter WemmThe 129c293d821SPeter Wemm.Fn gethostbyname2 130c293d821SPeter Wemmfunction is an evolution of 131c293d821SPeter Wemm.Fn gethostbyname 132c293d821SPeter Wemmwhich is intended to allow lookups in address families other than 133c293d821SPeter Wemm.Dv AF_INET , 134c293d821SPeter Wemmfor example 135c293d821SPeter Wemm.Dv AF_INET6 . 136c293d821SPeter WemmCurrently the 137c293d821SPeter Wemm.Fa af 138c293d821SPeter Wemmargument must be specified as 139c293d821SPeter Wemm.Dv AF_INET 1409c727d2cSJoseph Koshyelse the function will return 141c293d821SPeter Wemm.Dv NULL 142c293d821SPeter Wemmafter having set 143c293d821SPeter Wemm.Va h_errno 144c293d821SPeter Wemmto 145c293d821SPeter Wemm.Dv NETDB_INTERNAL 146c293d821SPeter Wemm.Pp 147c293d821SPeter WemmThe 148bc7413d0SPeter Wemm.Fn sethostent 149bc7413d0SPeter Wemmfunction 150bc7413d0SPeter Wemmmay be used to request the use of a connected 151bc7413d0SPeter Wemm.Tn TCP 152bc7413d0SPeter Wemmsocket for queries. 153fdf4c7afSPeter WemmIf the 154bc7413d0SPeter Wemm.Fa stayopen 155fdf4c7afSPeter Wemmflag is non-zero, 156bc7413d0SPeter Wemmthis sets the option to send all queries to the name server using 157bc7413d0SPeter Wemm.Tn TCP 158fdf4c7afSPeter Wemmand to retain the connection after each call to 159c293d821SPeter Wemm.Fn gethostbyname , 160c293d821SPeter Wemm.Fn gethostbyname2 161fdf4c7afSPeter Wemmor 162bc7413d0SPeter Wemm.Fn gethostbyaddr . 163bc7413d0SPeter WemmOtherwise, queries are performed using 164bc7413d0SPeter Wemm.Tn UDP 165bc7413d0SPeter Wemmdatagrams. 166bc7413d0SPeter Wemm.Pp 167bc7413d0SPeter WemmThe 168bc7413d0SPeter Wemm.Fn endhostent 169bc7413d0SPeter Wemmfunction 170bc7413d0SPeter Wemmcloses the 171bc7413d0SPeter Wemm.Tn TCP 172bc7413d0SPeter Wemmconnection. 173c293d821SPeter Wemm.Pp 174c293d821SPeter WemmThe 175c293d821SPeter Wemm.Fn herror 176c293d821SPeter Wemmfunction writes a message to the diagnostic output consisting of the 177c293d821SPeter Wemmstring parameter 178c293d821SPeter Wemm.Fa s , 179c293d821SPeter Wemmthe constant string ": ", and a message corresponding to the value of 180c293d821SPeter Wemm.Va h_errno . 181c293d821SPeter Wemm.Pp 182c293d821SPeter WemmThe 183c293d821SPeter Wemm.Fn hstrerror 184c293d821SPeter Wemmfunction returns a string which is the message text corresponding to the 185c293d821SPeter Wemmvalue of the 186c293d821SPeter Wemm.Fa err 187c293d821SPeter Wemmparameter. 188bc7413d0SPeter Wemm.Sh FILES 189c293d821SPeter Wemm.Bl -tag -width /etc/resolv.conf -compact 190bc7413d0SPeter Wemm.It Pa /etc/hosts 191c293d821SPeter Wemm.It Pa /etc/host.conf 192c293d821SPeter Wemm.It Pa /etc/resolv.conf 193bc7413d0SPeter Wemm.El 194bc7413d0SPeter Wemm.Sh DIAGNOSTICS 195fdf4c7afSPeter WemmError return status from 196c293d821SPeter Wemm.Fn gethostbyname , 197c293d821SPeter Wemm.Fn gethostbyname2 198fdf4c7afSPeter Wemmand 199bc7413d0SPeter Wemm.Fn gethostbyaddr 200fdf4c7afSPeter Wemmis indicated by return of a null pointer. 201fdf4c7afSPeter WemmThe external integer 202bc7413d0SPeter Wemm.Va h_errno 203fdf4c7afSPeter Wemmmay then be checked to see whether this is a temporary failure 204fdf4c7afSPeter Wemmor an invalid or unknown host. 205fdf4c7afSPeter WemmThe routine 206bc7413d0SPeter Wemm.Fn herror 207fdf4c7afSPeter Wemmcan be used to print an error message describing the failure. 208fdf4c7afSPeter WemmIf its argument 209bc7413d0SPeter Wemm.Fa string 210bc7413d0SPeter Wemmis 211bc7413d0SPeter Wemm.Pf non Dv -NULL , 212bc7413d0SPeter Wemmit is printed, followed by a colon and a space. 213fdf4c7afSPeter WemmThe error message is printed with a trailing newline. 214bc7413d0SPeter Wemm.Pp 215bc7413d0SPeter WemmThe variable 216bc7413d0SPeter Wemm.Va h_errno 217fdf4c7afSPeter Wemmcan have the following values: 218bc7413d0SPeter Wemm.Bl -tag -width HOST_NOT_FOUND 219bc7413d0SPeter Wemm.It Dv HOST_NOT_FOUND 220fdf4c7afSPeter WemmNo such host is known. 221bc7413d0SPeter Wemm.It Dv TRY_AGAIN 222fdf4c7afSPeter WemmThis is usually a temporary error 223fdf4c7afSPeter Wemmand means that the local server did not receive 224fdf4c7afSPeter Wemma response from an authoritative server. 225fdf4c7afSPeter WemmA retry at some later time may succeed. 226bc7413d0SPeter Wemm.It Dv NO_RECOVERY 227fdf4c7afSPeter WemmSome unexpected server failure was encountered. 228fdf4c7afSPeter WemmThis is a non-recoverable error. 229bc7413d0SPeter Wemm.It Dv NO_DATA 230fdf4c7afSPeter WemmThe requested name is valid but does not have an IP address; 231fdf4c7afSPeter Wemmthis is not a temporary error. 232fdf4c7afSPeter WemmThis means that the name is known to the name server but there is no address 233fdf4c7afSPeter Wemmassociated with this name. 234fdf4c7afSPeter WemmAnother type of request to the name server using this domain name 235fdf4c7afSPeter Wemmwill result in an answer; 236fdf4c7afSPeter Wemmfor example, a mail-forwarder may be registered for this domain. 237bc7413d0SPeter Wemm.El 238bc7413d0SPeter Wemm.Sh SEE ALSO 239bc7413d0SPeter Wemm.Xr resolver 3 , 240bc7413d0SPeter Wemm.Xr hosts 5 , 241bc7413d0SPeter Wemm.Xr hostname 7 , 242bc7413d0SPeter Wemm.Xr named 8 243bc7413d0SPeter Wemm.Sh CAVEAT 244bc7413d0SPeter WemmThe 245bc7413d0SPeter Wemm.Fn gethostent 246bc7413d0SPeter Wemmfunction 247fdf4c7afSPeter Wemmis defined, and 248bc7413d0SPeter Wemm.Fn sethostent 249fdf4c7afSPeter Wemmand 250bc7413d0SPeter Wemm.Fn endhostent 251fdf4c7afSPeter Wemmare redefined, 252fdf4c7afSPeter Wemmwhen 253bc7413d0SPeter Wemm.Xr libc 3 254fdf4c7afSPeter Wemmis built to use only the routines to lookup in 255bc7413d0SPeter Wemm.Pa /etc/hosts 256fdf4c7afSPeter Wemmand not the name server. 257bc7413d0SPeter Wemm.Pp 258bc7413d0SPeter WemmThe 259bc7413d0SPeter Wemm.Fn gethostent 260bc7413d0SPeter Wemmfunction 261fdf4c7afSPeter Wemmreads the next line of 262bc7413d0SPeter Wemm.Pa /etc/hosts , 263fdf4c7afSPeter Wemmopening the file if necessary. 264bc7413d0SPeter Wemm.Pp 265bc7413d0SPeter WemmThe 266bc7413d0SPeter Wemm.Fn sethostent 267bc7413d0SPeter Wemmfunction 268bc7413d0SPeter Wemmopens and/or rewinds the file 269bc7413d0SPeter Wemm.Pa /etc/hosts . 270bc7413d0SPeter WemmIf the 271bc7413d0SPeter Wemm.Fa stayopen 272fdf4c7afSPeter Wemmargument is non-zero, 273bc7413d0SPeter Wemmthe file will not be closed after each call to 274c293d821SPeter Wemm.Fn gethostbyname , 275c293d821SPeter Wemm.Fn gethostbyname2 276fdf4c7afSPeter Wemmor 277bc7413d0SPeter Wemm.Fn gethostbyaddr . 278bc7413d0SPeter Wemm.Pp 279bc7413d0SPeter WemmThe 280bc7413d0SPeter Wemm.Fn endhostent 281bc7413d0SPeter Wemmfunction 282bc7413d0SPeter Wemmcloses the file. 283bc7413d0SPeter Wemm.Sh HISTORY 284bc7413d0SPeter WemmThe 285bc7413d0SPeter Wemm.Fn herror 286bc7413d0SPeter Wemmfunction appeared in 287bc7413d0SPeter Wemm.Bx 4.3 . 288bc7413d0SPeter WemmThe 289bc7413d0SPeter Wemm.Fn endhostent , 290bc7413d0SPeter Wemm.Fn gethostbyaddr , 291bc7413d0SPeter Wemm.Fn gethostbyname , 292bc7413d0SPeter Wemm.Fn gethostent , 293bc7413d0SPeter Wemmand 294bc7413d0SPeter Wemm.Fn sethostent 295bc7413d0SPeter Wemmfunctions appeared in 296bc7413d0SPeter Wemm.Bx 4.2 . 297c293d821SPeter WemmThe 298c293d821SPeter Wemm.Fn gethostbyname2 299577b61c6SPhilippe Charnierfunction first appeared in bind-4.9.4. 300bc7413d0SPeter Wemm.Sh BUGS 301bc7413d0SPeter WemmThese functions use static data storage; 302bc7413d0SPeter Wemmif the data is needed for future use, it should be 303bc7413d0SPeter Wemmcopied before any subsequent calls overwrite it. 304bc7413d0SPeter WemmOnly the Internet 305fdf4c7afSPeter Wemmaddress format is currently understood. 306