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.\" 32c293d821SPeter Wemm.\" @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 33bc7413d0SPeter Wemm.\" 34c293d821SPeter Wemm.Dd May 25, 1995 35bc7413d0SPeter Wemm.Dt GETHOSTBYNAME 3 36bc7413d0SPeter Wemm.Os BSD 4.2 37bc7413d0SPeter Wemm.Sh NAME 38bc7413d0SPeter Wemm.Nm gethostbyname , 39c293d821SPeter Wemm.Nm gethostbyname2 , 40bc7413d0SPeter Wemm.Nm gethostbyaddr , 41bc7413d0SPeter Wemm.Nm gethostent , 42bc7413d0SPeter Wemm.Nm sethostent , 43bc7413d0SPeter Wemm.Nm endhostent , 44c293d821SPeter Wemm.Nm herror , 45c293d821SPeter Wemm.Nm hstrerror 46bc7413d0SPeter Wemm.Nd get network host entry 47bc7413d0SPeter Wemm.Sh SYNOPSIS 48bc7413d0SPeter Wemm.Fd #include <netdb.h> 49d3281e16SJohn Polstra.Fd extern int h_errno; 50bc7413d0SPeter Wemm.Ft struct hostent * 51d3281e16SJohn Polstra.Fn gethostbyname "const char *name" 52bc7413d0SPeter Wemm.Ft struct hostent * 53c293d821SPeter Wemm.Fn gethostbyname2 "const char *name" "int af" 54c293d821SPeter Wemm.Ft struct hostent * 55d3281e16SJohn Polstra.Fn gethostbyaddr "const char *addr" "int len" "int type" 56bc7413d0SPeter Wemm.Ft struct hostent * 57bc7413d0SPeter Wemm.Fn gethostent void 58bc7413d0SPeter Wemm.Fn sethostent "int stayopen" 59bc7413d0SPeter Wemm.Fn endhostent void 60d3281e16SJohn Polstra.Fn herror "const char *string" 61c293d821SPeter Wemm.Ft const char * 62c293d821SPeter Wemm.Fn hstrerror "int err" 63bc7413d0SPeter Wemm.Sh DESCRIPTION 64bc7413d0SPeter WemmThe 65c293d821SPeter Wemm.Fn gethostbyname , 66c293d821SPeter Wemm.Fn gethostbyname2 67fdf4c7afSPeter Wemmand 68bc7413d0SPeter Wemm.Fn gethostbyaddr 69bc7413d0SPeter Wemmfunctions 70fdf4c7afSPeter Wemmeach return a pointer to an object with the 71fdf4c7afSPeter Wemmfollowing structure describing an internet host 72fdf4c7afSPeter Wemmreferenced by name or by address, respectively. 73fdf4c7afSPeter WemmThis structure contains either the information obtained from the name server, 74bc7413d0SPeter Wemm.Xr named 8 , 75fdf4c7afSPeter Wemmor broken-out fields from a line in 76bc7413d0SPeter Wemm.Pa /etc/hosts . 77fdf4c7afSPeter WemmIf the local name server is not running these routines do a lookup in 78bc7413d0SPeter Wemm.Pa /etc/hosts . 79bc7413d0SPeter Wemm.Bd -literal 80fdf4c7afSPeter Wemmstruct hostent { 81fdf4c7afSPeter Wemm char *h_name; /* official name of host */ 82fdf4c7afSPeter Wemm char **h_aliases; /* alias list */ 83fdf4c7afSPeter Wemm int h_addrtype; /* host address type */ 84fdf4c7afSPeter Wemm int h_length; /* length of address */ 85fdf4c7afSPeter Wemm char **h_addr_list; /* list of addresses from name server */ 86fdf4c7afSPeter Wemm}; 87fdf4c7afSPeter Wemm#define h_addr h_addr_list[0] /* address, for backward compatibility */ 88bc7413d0SPeter Wemm.Ed 89bc7413d0SPeter Wemm.Pp 90fdf4c7afSPeter WemmThe members of this structure are: 91bc7413d0SPeter Wemm.Bl -tag -width h_addr_list 92bc7413d0SPeter Wemm.It Fa h_name 93fdf4c7afSPeter WemmOfficial name of the host. 94bc7413d0SPeter Wemm.It Fa h_aliases 95c293d821SPeter WemmA NULL-terminated array of alternate names for the host. 96bc7413d0SPeter Wemm.It Fa h_addrtype 97c293d821SPeter WemmThe type of address being returned; usually 98bc7413d0SPeter Wemm.Dv AF_INET . 99bc7413d0SPeter Wemm.It Fa h_length 100fdf4c7afSPeter WemmThe length, in bytes, of the address. 101bc7413d0SPeter Wemm.It Fa h_addr_list 102c293d821SPeter WemmA NULL-terminated array of network addresses for the host. 103fdf4c7afSPeter WemmHost addresses are returned in network byte order. 104bc7413d0SPeter Wemm.It Fa h_addr 105bc7413d0SPeter WemmThe first address in 106bc7413d0SPeter Wemm.Fa h_addr_list ; 107bc7413d0SPeter Wemmthis is for backward compatibility. 108c293d821SPeter Wemm.El 109bc7413d0SPeter Wemm.Pp 110fdf4c7afSPeter WemmWhen using the nameserver, 111bc7413d0SPeter Wemm.Fn gethostbyname 112c293d821SPeter Wemmand 113c293d821SPeter Wemm.Fn gethostbyname 114fdf4c7afSPeter Wemmwill search for the named host in the current domain and its parents 115fdf4c7afSPeter Wemmunless the name ends in a dot. 116bc7413d0SPeter WemmIf the name contains no dot, and if the environment variable 117bc7413d0SPeter Wemm.Dq Ev HOSTALIASES 118fdf4c7afSPeter Wemmcontains the name of an alias file, the alias file will first be searched 119fdf4c7afSPeter Wemmfor an alias matching the input name. 120fdf4c7afSPeter WemmSee 121bc7413d0SPeter Wemm.Xr hostname 7 122fdf4c7afSPeter Wemmfor the domain search procedure and the alias file format. 123bc7413d0SPeter Wemm.Pp 124bc7413d0SPeter WemmThe 125c293d821SPeter Wemm.Fn gethostbyname2 126c293d821SPeter Wemmfunction is an evolution of 127c293d821SPeter Wemm.Fn gethostbyname 128c293d821SPeter Wemmwhich is intended to allow lookups in address families other than 129c293d821SPeter Wemm.Dv AF_INET , 130c293d821SPeter Wemmfor example 131c293d821SPeter Wemm.Dv AF_INET6 . 132c293d821SPeter WemmCurrently the 133c293d821SPeter Wemm.Fa af 134c293d821SPeter Wemmargument must be specified as 135c293d821SPeter Wemm.Dv AF_INET 136c293d821SPeter Wemmelse the fuction will return 137c293d821SPeter Wemm.Dv NULL 138c293d821SPeter Wemmafter having set 139c293d821SPeter Wemm.Va h_errno 140c293d821SPeter Wemmto 141c293d821SPeter Wemm.Dv NETDB_INTERNAL 142c293d821SPeter Wemm.Pp 143c293d821SPeter WemmThe 144bc7413d0SPeter Wemm.Fn sethostent 145bc7413d0SPeter Wemmfunction 146bc7413d0SPeter Wemmmay be used to request the use of a connected 147bc7413d0SPeter Wemm.Tn TCP 148bc7413d0SPeter Wemmsocket for queries. 149fdf4c7afSPeter WemmIf the 150bc7413d0SPeter Wemm.Fa stayopen 151fdf4c7afSPeter Wemmflag is non-zero, 152bc7413d0SPeter Wemmthis sets the option to send all queries to the name server using 153bc7413d0SPeter Wemm.Tn TCP 154fdf4c7afSPeter Wemmand to retain the connection after each call to 155c293d821SPeter Wemm.Fn gethostbyname , 156c293d821SPeter Wemm.Fn gethostbyname2 157fdf4c7afSPeter Wemmor 158bc7413d0SPeter Wemm.Fn gethostbyaddr . 159bc7413d0SPeter WemmOtherwise, queries are performed using 160bc7413d0SPeter Wemm.Tn UDP 161bc7413d0SPeter Wemmdatagrams. 162bc7413d0SPeter Wemm.Pp 163bc7413d0SPeter WemmThe 164bc7413d0SPeter Wemm.Fn endhostent 165bc7413d0SPeter Wemmfunction 166bc7413d0SPeter Wemmcloses the 167bc7413d0SPeter Wemm.Tn TCP 168bc7413d0SPeter Wemmconnection. 169c293d821SPeter Wemm.Pp 170c293d821SPeter WemmThe 171c293d821SPeter Wemm.Fn herror 172c293d821SPeter Wemmfunction writes a message to the diagnostic output consisting of the 173c293d821SPeter Wemmstring parameter 174c293d821SPeter Wemm.Fa s , 175c293d821SPeter Wemmthe constant string ": ", and a message corresponding to the value of 176c293d821SPeter Wemm.Va h_errno . 177c293d821SPeter Wemm.Pp 178c293d821SPeter WemmThe 179c293d821SPeter Wemm.Fn hstrerror 180c293d821SPeter Wemmfunction returns a string which is the message text corresponding to the 181c293d821SPeter Wemmvalue of the 182c293d821SPeter Wemm.Fa err 183c293d821SPeter Wemmparameter. 184bc7413d0SPeter Wemm.Sh FILES 185c293d821SPeter Wemm.Bl -tag -width /etc/resolv.conf -compact 186bc7413d0SPeter Wemm.It Pa /etc/hosts 187c293d821SPeter Wemm.It Pa /etc/host.conf 188c293d821SPeter Wemm.It Pa /etc/resolv.conf 189bc7413d0SPeter Wemm.El 190bc7413d0SPeter Wemm.Sh DIAGNOSTICS 191fdf4c7afSPeter WemmError return status from 192c293d821SPeter Wemm.Fn gethostbyname , 193c293d821SPeter Wemm.Fn gethostbyname2 194fdf4c7afSPeter Wemmand 195bc7413d0SPeter Wemm.Fn gethostbyaddr 196fdf4c7afSPeter Wemmis indicated by return of a null pointer. 197fdf4c7afSPeter WemmThe external integer 198bc7413d0SPeter Wemm.Va h_errno 199fdf4c7afSPeter Wemmmay then be checked to see whether this is a temporary failure 200fdf4c7afSPeter Wemmor an invalid or unknown host. 201fdf4c7afSPeter WemmThe routine 202bc7413d0SPeter Wemm.Fn herror 203fdf4c7afSPeter Wemmcan be used to print an error message describing the failure. 204fdf4c7afSPeter WemmIf its argument 205bc7413d0SPeter Wemm.Fa string 206bc7413d0SPeter Wemmis 207bc7413d0SPeter Wemm.Pf non Dv -NULL , 208bc7413d0SPeter Wemmit is printed, followed by a colon and a space. 209fdf4c7afSPeter WemmThe error message is printed with a trailing newline. 210bc7413d0SPeter Wemm.Pp 211bc7413d0SPeter WemmThe variable 212bc7413d0SPeter Wemm.Va h_errno 213fdf4c7afSPeter Wemmcan have the following values: 214bc7413d0SPeter Wemm.Bl -tag -width HOST_NOT_FOUND 215bc7413d0SPeter Wemm.It Dv HOST_NOT_FOUND 216fdf4c7afSPeter WemmNo such host is known. 217bc7413d0SPeter Wemm.It Dv TRY_AGAIN 218fdf4c7afSPeter WemmThis is usually a temporary error 219fdf4c7afSPeter Wemmand means that the local server did not receive 220fdf4c7afSPeter Wemma response from an authoritative server. 221fdf4c7afSPeter WemmA retry at some later time may succeed. 222bc7413d0SPeter Wemm.It Dv NO_RECOVERY 223fdf4c7afSPeter WemmSome unexpected server failure was encountered. 224fdf4c7afSPeter WemmThis is a non-recoverable error. 225bc7413d0SPeter Wemm.It Dv NO_DATA 226fdf4c7afSPeter WemmThe requested name is valid but does not have an IP address; 227fdf4c7afSPeter Wemmthis is not a temporary error. 228fdf4c7afSPeter WemmThis means that the name is known to the name server but there is no address 229fdf4c7afSPeter Wemmassociated with this name. 230fdf4c7afSPeter WemmAnother type of request to the name server using this domain name 231fdf4c7afSPeter Wemmwill result in an answer; 232fdf4c7afSPeter Wemmfor example, a mail-forwarder may be registered for this domain. 233bc7413d0SPeter Wemm.El 234bc7413d0SPeter Wemm.Sh SEE ALSO 235bc7413d0SPeter Wemm.Xr resolver 3 , 236bc7413d0SPeter Wemm.Xr hosts 5 , 237bc7413d0SPeter Wemm.Xr hostname 7 , 238bc7413d0SPeter Wemm.Xr named 8 239bc7413d0SPeter Wemm.Sh CAVEAT 240bc7413d0SPeter WemmThe 241bc7413d0SPeter Wemm.Fn gethostent 242bc7413d0SPeter Wemmfunction 243fdf4c7afSPeter Wemmis defined, and 244bc7413d0SPeter Wemm.Fn sethostent 245fdf4c7afSPeter Wemmand 246bc7413d0SPeter Wemm.Fn endhostent 247fdf4c7afSPeter Wemmare redefined, 248fdf4c7afSPeter Wemmwhen 249bc7413d0SPeter Wemm.Xr libc 3 250fdf4c7afSPeter Wemmis built to use only the routines to lookup in 251bc7413d0SPeter Wemm.Pa /etc/hosts 252fdf4c7afSPeter Wemmand not the name server. 253bc7413d0SPeter Wemm.Pp 254bc7413d0SPeter WemmThe 255bc7413d0SPeter Wemm.Fn gethostent 256bc7413d0SPeter Wemmfunction 257fdf4c7afSPeter Wemmreads the next line of 258bc7413d0SPeter Wemm.Pa /etc/hosts , 259fdf4c7afSPeter Wemmopening the file if necessary. 260bc7413d0SPeter Wemm.Pp 261bc7413d0SPeter WemmThe 262bc7413d0SPeter Wemm.Fn sethostent 263bc7413d0SPeter Wemmfunction 264bc7413d0SPeter Wemmopens and/or rewinds the file 265bc7413d0SPeter Wemm.Pa /etc/hosts . 266bc7413d0SPeter WemmIf the 267bc7413d0SPeter Wemm.Fa stayopen 268fdf4c7afSPeter Wemmargument is non-zero, 269bc7413d0SPeter Wemmthe file will not be closed after each call to 270c293d821SPeter Wemm.Fn gethostbyname , 271c293d821SPeter Wemm.Fn gethostbyname2 272fdf4c7afSPeter Wemmor 273bc7413d0SPeter Wemm.Fn gethostbyaddr . 274bc7413d0SPeter Wemm.Pp 275bc7413d0SPeter WemmThe 276bc7413d0SPeter Wemm.Fn endhostent 277bc7413d0SPeter Wemmfunction 278bc7413d0SPeter Wemmcloses the file. 279bc7413d0SPeter Wemm.Sh HISTORY 280bc7413d0SPeter WemmThe 281bc7413d0SPeter Wemm.Fn herror 282bc7413d0SPeter Wemmfunction appeared in 283bc7413d0SPeter Wemm.Bx 4.3 . 284bc7413d0SPeter WemmThe 285bc7413d0SPeter Wemm.Fn endhostent , 286bc7413d0SPeter Wemm.Fn gethostbyaddr , 287bc7413d0SPeter Wemm.Fn gethostbyname , 288bc7413d0SPeter Wemm.Fn gethostent , 289bc7413d0SPeter Wemmand 290bc7413d0SPeter Wemm.Fn sethostent 291bc7413d0SPeter Wemmfunctions appeared in 292bc7413d0SPeter Wemm.Bx 4.2 . 293c293d821SPeter WemmThe 294c293d821SPeter Wemm.Fn gethostbyname2 295c293d821SPeter Wemmfunctrion first appeared in bind-4.9.4. 296bc7413d0SPeter Wemm.Sh BUGS 297bc7413d0SPeter WemmThese functions use static data storage; 298bc7413d0SPeter Wemmif the data is needed for future use, it should be 299bc7413d0SPeter Wemmcopied before any subsequent calls overwrite it. 300bc7413d0SPeter WemmOnly the Internet 301fdf4c7afSPeter Wemmaddress format is currently understood. 302