xref: /netbsd-src/external/bsd/libbind/dist/doc/gethostbyname.man3 (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1*b5677b36Schristos.\" Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2*b5677b36Schristos.\"
3*b5677b36Schristos.\" Permission to use, copy, modify, and distribute this software for any
4*b5677b36Schristos.\" purpose with or without fee is hereby granted, provided that the above
5*b5677b36Schristos.\" copyright notice and this permission notice appear in all copies.
6*b5677b36Schristos.\"
7*b5677b36Schristos.\" THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
8*b5677b36Schristos.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9*b5677b36Schristos.\" MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
10*b5677b36Schristos.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11*b5677b36Schristos.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12*b5677b36Schristos.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13*b5677b36Schristos.\" OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14*b5677b36Schristos.\"
15*b5677b36Schristos.\" Copyright (c) 1983, 1987 The Regents of the University of California.
16*b5677b36Schristos.\" All rights reserved.
17*b5677b36Schristos.\"
18*b5677b36Schristos.\" Redistribution and use in source and binary forms are permitted provided
19*b5677b36Schristos.\" that: (1) source distributions retain this entire copyright notice and
20*b5677b36Schristos.\" comment, and (2) distributions including binaries display the following
21*b5677b36Schristos.\" acknowledgement:  ``This product includes software developed by the
22*b5677b36Schristos.\" University of California, Berkeley and its contributors'' in the
23*b5677b36Schristos.\" documentation or other materials provided with the distribution and in
24*b5677b36Schristos.\" all advertising materials mentioning features or use of this software.
25*b5677b36Schristos.\" Neither the name of the University nor the names of its contributors may
26*b5677b36Schristos.\" be used to endorse or promote products derived from this software without
27*b5677b36Schristos.\" specific prior written permission.
28*b5677b36Schristos.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
29*b5677b36Schristos.\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
30*b5677b36Schristos.\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31*b5677b36Schristos.\"
32*b5677b36Schristos.\"	@(#)gethostbyname.3	6.12 (Berkeley) 6/23/90
33*b5677b36Schristos.\"
34*b5677b36Schristos.Dd June 23, 1990
35*b5677b36Schristos.Dt GETHOSTBYNAME 3
36*b5677b36Schristos.Os BSD 4
37*b5677b36Schristos.Sh NAME
38*b5677b36Schristos.Nm gethostbyname ,
39*b5677b36Schristos.Nm gethostbyaddr ,
40*b5677b36Schristos.Nm gethostent ,
41*b5677b36Schristos.Nm sethostent ,
42*b5677b36Schristos.Nm endhostent ,
43*b5677b36Schristos.Nm herror
44*b5677b36Schristos.Nd get network host entry
45*b5677b36Schristos.Sh SYNOPSIS
46*b5677b36Schristos.Fd #include <netdb.h>
47*b5677b36Schristos.Ft extern int
48*b5677b36Schristos.Fa h_errno ;
49*b5677b36Schristos.Pp
50*b5677b36Schristos.Ft struct hostent *
51*b5677b36Schristos.Fn gethostbyname "char *name"
52*b5677b36Schristos.Ft struct hostent *
53*b5677b36Schristos.Fn gethostbyname2 "char *name" "int af"
54*b5677b36Schristos.Ft struct hostent *
55*b5677b36Schristos.Fn gethostbyaddr "char *addr" "int len, type"
56*b5677b36Schristos.Ft struct hostent *
57*b5677b36Schristos.Fn gethostent
58*b5677b36Schristos.Fn sethostent "int stayopen"
59*b5677b36Schristos.Fn endhostent
60*b5677b36Schristos.Fn herror "char *string"
61*b5677b36Schristos.Sh DESCRIPTION
62*b5677b36Schristos.Fn Gethostbyname ,
63*b5677b36Schristos.Fn gethostbyname2 ,
64*b5677b36Schristosand
65*b5677b36Schristos.Fn gethostbyaddr
66*b5677b36Schristoseach return a pointer to a
67*b5677b36Schristos.Ft hostent
68*b5677b36Schristosstructure (see below) describing an internet host
69*b5677b36Schristosreferenced by name or by address, as the function names indicate.
70*b5677b36SchristosThis structure contains either the information obtained from the name server,
71*b5677b36Schristosor broken-out fields from a line in
72*b5677b36Schristos.Pa /etc/hosts .
73*b5677b36SchristosIf the local name server is not running, these routines do a lookup in
74*b5677b36Schristos.Pa /etc/hosts .
75*b5677b36Schristos.Bd -literal -offset indent
76*b5677b36Schristosstruct	hostent {
77*b5677b36Schristos	char	*h_name;	/* official name of host */
78*b5677b36Schristos	char	**h_aliases;	/* alias list */
79*b5677b36Schristos	int	h_addrtype;	/* host address type */
80*b5677b36Schristos	int	h_length;	/* length of address */
81*b5677b36Schristos	char	**h_addr_list;	/* list of addresses from name server */
82*b5677b36Schristos};
83*b5677b36Schristos
84*b5677b36Schristos#define	h_addr  h_addr_list[0]	/* address, for backward compatibility */
85*b5677b36Schristos.Ed
86*b5677b36Schristos.Pp
87*b5677b36SchristosThe members of this structure are:
88*b5677b36Schristos.Bl -tag -width "h_addr_list"
89*b5677b36Schristos.It h_name
90*b5677b36SchristosOfficial name of the host.
91*b5677b36Schristos.It h_aliases
92*b5677b36SchristosA zero-terminated array of alternate names for the host.
93*b5677b36Schristos.It h_addrtype
94*b5677b36SchristosThe type of address being returned; usually
95*b5677b36Schristos.Dv AF_INET .
96*b5677b36Schristos.It h_length
97*b5677b36SchristosThe length, in bytes, of the address.
98*b5677b36Schristos.It h_addr_list
99*b5677b36SchristosA zero-terminated array of network addresses for the host.
100*b5677b36SchristosHost addresses are returned in network byte order.
101*b5677b36Schristos.It h_addr
102*b5677b36SchristosThe first address in
103*b5677b36Schristos.Li h_addr_list ;
104*b5677b36Schristosthis is for backward compatibility.
105*b5677b36Schristos.El
106*b5677b36Schristos.Pp
107*b5677b36SchristosWhen using the nameserver,
108*b5677b36Schristos.Fn gethostbyname
109*b5677b36Schristoswill search for the named host in each parent domain given in the
110*b5677b36Schristos.Dq Li search
111*b5677b36Schristosdirective of
112*b5677b36Schristos.Xr resolv.conf 5
113*b5677b36Schristosunless the name contains a dot
114*b5677b36Schristos.Pq Dq \&. .
115*b5677b36SchristosIf the name contains no dot, and if the environment variable
116*b5677b36Schristos.Ev HOSTALIASES
117*b5677b36Schristoscontains the name of an alias file, the alias file will first be searched
118*b5677b36Schristosfor an alias matching the input name.
119*b5677b36SchristosSee
120*b5677b36Schristos.Xr hostname 7
121*b5677b36Schristosfor the domain search procedure and the alias file format.
122*b5677b36Schristos.Pp
123*b5677b36Schristos.Fn Gethostbyname2
124*b5677b36Schristosis an evolution of
125*b5677b36Schristos.Fn gethostbyname
126*b5677b36Schristosintended to allow lookups in address families other than
127*b5677b36Schristos.Dv AF_INET ,
128*b5677b36Schristosfor example,
129*b5677b36Schristos.Dv AF_INET6 .
130*b5677b36SchristosCurrently, the
131*b5677b36Schristos.Fa af
132*b5677b36Schristosargument must be specified as
133*b5677b36Schristos.Dv AF_INET
134*b5677b36Schristoselse the function will return
135*b5677b36Schristos.Dv NULL
136*b5677b36Schristosafter having set
137*b5677b36Schristos.Ft h_errno
138*b5677b36Schristosto
139*b5677b36Schristos.Dv NETDB_INTERNAL .
140*b5677b36Schristos.Pp
141*b5677b36Schristos.Fn Sethostent
142*b5677b36Schristosmay be used to request the use of a connected TCP socket for queries.
143*b5677b36SchristosIf the
144*b5677b36Schristos.Fa stayopen
145*b5677b36Schristosflag is non-zero,
146*b5677b36Schristosthis sets the option to send all queries to the name server using TCP
147*b5677b36Schristosand to retain the connection after each call to
148*b5677b36Schristos.Fn gethostbyname
149*b5677b36Schristosor
150*b5677b36Schristos.Fn gethostbyaddr .
151*b5677b36SchristosOtherwise, queries are performed using UDP datagrams.
152*b5677b36Schristos.Pp
153*b5677b36Schristos.Fn Endhostent
154*b5677b36Schristoscloses the TCP connection.
155*b5677b36Schristos.Sh ENVIRONMENT
156*b5677b36Schristos.Bl -tag -width "HOSTALIASES  " -compact
157*b5677b36Schristos.It Ev HOSTALIASES
158*b5677b36SchristosName of file containing
159*b5677b36Schristos.Pq Ar host alias , full hostname
160*b5677b36Schristospairs.
161*b5677b36Schristos.El
162*b5677b36Schristos.Sh FILES
163*b5677b36Schristos.Bl -tag -width "HOSTALIASES  " -compact
164*b5677b36Schristos.It Pa /etc/hosts
165*b5677b36SchristosSee
166*b5677b36Schristos.Xr hosts 5 .
167*b5677b36Schristos.El
168*b5677b36Schristos.Sh DIAGNOSTICS
169*b5677b36Schristos.Pp
170*b5677b36SchristosError return status from
171*b5677b36Schristos.Fn gethostbyname
172*b5677b36Schristosand
173*b5677b36Schristos.Fn gethostbyaddr
174*b5677b36Schristosis indicated by return of a null pointer.
175*b5677b36SchristosThe external integer
176*b5677b36Schristos.Ft h_errno
177*b5677b36Schristosmay then be checked to see whether this is a temporary failure
178*b5677b36Schristosor an invalid or unknown host.
179*b5677b36SchristosThe routine
180*b5677b36Schristos.Fn herror
181*b5677b36Schristoscan be used to print an error message describing the failure.
182*b5677b36SchristosIf its argument
183*b5677b36Schristos.Fa string
184*b5677b36Schristosis non-NULL, it is printed, followed by a colon and a space.
185*b5677b36SchristosThe error message is printed with a trailing newline.
186*b5677b36Schristos.Pp
187*b5677b36Schristos.Ft h_errno
188*b5677b36Schristoscan have the following values:
189*b5677b36Schristos.Bl -tag -width "HOST_NOT_FOUND  " -offset indent
190*b5677b36Schristos.It Dv NETDB_INTERNAL
191*b5677b36SchristosThis indicates an internal error in the library, unrelated to the network
192*b5677b36Schristosor name service.
193*b5677b36Schristos.Ft errno
194*b5677b36Schristoswill be valid in this case; see
195*b5677b36Schristos.Xr perror  .
196*b5677b36Schristos.It Dv HOST_NOT_FOUND
197*b5677b36SchristosNo such host is known.
198*b5677b36Schristos.It Dv TRY_AGAIN
199*b5677b36SchristosThis is usually a temporary error
200*b5677b36Schristosand means that the local server did not receive
201*b5677b36Schristosa response from an authoritative server.
202*b5677b36SchristosA retry at some later time may succeed.
203*b5677b36Schristos.It Dv NO_RECOVERY
204*b5677b36SchristosSome unexpected server failure was encountered.
205*b5677b36SchristosThis is a non-recoverable error, as one might expect.
206*b5677b36Schristos.It Dv NO_DATA
207*b5677b36SchristosThe requested name is valid but does not have an IP address;
208*b5677b36Schristosthis is not a temporary error.
209*b5677b36SchristosThis means that the name is known to the name server but there is no address
210*b5677b36Schristosassociated with this name.
211*b5677b36SchristosAnother type of request to the name server using this domain name
212*b5677b36Schristoswill result in an answer;
213*b5677b36Schristosfor example, a mail-forwarder may be registered for this domain.
214*b5677b36Schristos.El
215*b5677b36Schristos.Sh SEE ALSO
216*b5677b36Schristos.Xr hosts 5 ,
217*b5677b36Schristos.Xr hostname 7 ,
218*b5677b36Schristos.Xr resolver 3 ,
219*b5677b36Schristos.Xr resolver 5 .
220*b5677b36Schristos.Sh CAVEAT
221*b5677b36Schristos.Pp
222*b5677b36Schristos.Fn Gethostent
223*b5677b36Schristosis defined, and
224*b5677b36Schristos.Fn sethostent
225*b5677b36Schristosand
226*b5677b36Schristos.Fn endhostent
227*b5677b36Schristosare redefined,
228*b5677b36Schristoswhen
229*b5677b36Schristos.Pa libc
230*b5677b36Schristosis built to use only the routines to lookup in
231*b5677b36Schristos.Pa /etc/hosts
232*b5677b36Schristosand not the name server:
233*b5677b36Schristos.Bd -ragged -offset indent
234*b5677b36Schristos.Pp
235*b5677b36Schristos.Fn Gethostent
236*b5677b36Schristosreads the next line of
237*b5677b36Schristos.Pa /etc/hosts ,
238*b5677b36Schristosopening the file if necessary.
239*b5677b36Schristos.Pp
240*b5677b36Schristos.Fn Sethostent
241*b5677b36Schristosis redefined to open and rewind the file.  If the
242*b5677b36Schristos.Fa stayopen
243*b5677b36Schristosargument is non-zero,
244*b5677b36Schristosthe hosts data base will not be closed after each call to
245*b5677b36Schristos.Fn gethostbyname
246*b5677b36Schristosor
247*b5677b36Schristos.Fn gethostbyaddr .
248*b5677b36Schristos.Pp
249*b5677b36Schristos.Fn Endhostent
250*b5677b36Schristosis redefined to close the file.
251*b5677b36Schristos.Ed
252*b5677b36Schristos.Sh BUGS
253*b5677b36SchristosAll information is contained in a static area so it must be copied if it is
254*b5677b36Schristosto be saved.  Only the Internet address format is currently understood.
255