xref: /netbsd-src/lib/libc/net/gethostbyname.3 (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1.\" Copyright (c) 1983, 1987, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     from: @(#)gethostbyname.3	6.14 (Berkeley) 7/31/91
33.\"	$Id: gethostbyname.3,v 1.5 1994/04/07 07:00:10 deraadt Exp $
34.\"
35.Dd July 31, 1991
36.Dt GETHOSTBYNAME 3
37.Os BSD 4.2
38.Sh NAME
39.Nm gethostbyname ,
40.Nm gethostbyaddr ,
41.Nm gethostent ,
42.Nm sethostent ,
43.Nm endhostent ,
44.Nm herror
45.Nd get network host entry
46.Sh SYNOPSIS
47.Fd #include <netdb.h>
48.Fd extern int h_errno;
49.Ft struct hostent *
50.Fn gethostbyname "const char *name"
51.Ft struct hostent *
52.Fn gethostbyaddr "const char *addr" "int len" "int type"
53.Ft struct hostent *
54.Fn gethostent void
55.Fn sethostent "int stayopen"
56.Fn endhostent void
57.Fn herror "char *string"
58.Sh DESCRIPTION
59The
60.Fn gethostbyname
61and
62.Fn gethostbyaddr
63functions
64each return a pointer to an object with the
65following structure describing an internet host
66referenced by name or by address, respectively.
67This structure contains either the information obtained from the name server,
68.Xr named 8 ,
69broken-out fields from a line in
70.Pa /etc/hosts ,
71or database entries supplied by the
72.Xr yp 8
73system .
74If the local name server is not running these routines do a lookup in
75.Pa /etc/hosts .
76.Bd -literal
77struct	hostent {
78	char	*h_name;	/* official name of host */
79	char	**h_aliases;	/* alias list */
80	int	h_addrtype;	/* host address type */
81	int	h_length;	/* length of address */
82	char	**h_addr_list;	/* list of addresses from name server */
83};
84#define	h_addr  h_addr_list[0]	/* address, for backward compatibility */
85.Ed
86.Pp
87The members of this structure are:
88.Bl -tag -width h_addr_list
89.It Fa h_name
90Official name of the host.
91.It Fa h_aliases
92A zero terminated array of alternate names for the host.
93.It Fa h_addrtype
94The type of address being returned; currently always
95.Dv AF_INET .
96.It Fa h_length
97The length, in bytes, of the address.
98.It Fa h_addr_list
99A zero terminated array of network addresses for the host.
100Host addresses are returned in network byte order.
101.It Fa h_addr
102The first address in
103.Fa h_addr_list ;
104this is for backward compatibility.
105.Pp
106When using the nameserver,
107.Fn gethostbyname
108will search for the named host in the current domain and its parents
109unless the name ends in a dot.
110If the name contains no dot, and if the environment variable
111.Dq Ev HOSTALIASES
112contains the name of an alias file, the alias file will first be searched
113for an alias matching the input name.
114See
115.Xr hostname 7
116for the domain search procedure and the alias file format.
117.Pp
118The
119.Fn sethostent
120function
121may be used to request the use of a connected
122.Tn TCP
123socket for queries.
124If the
125.Fa stayopen
126flag is non-zero,
127this sets the option to send all queries to the name server using
128.Tn TCP
129and to retain the connection after each call to
130.Fn gethostbyname
131or
132.Fn gethostbyaddr .
133Otherwise, queries are performed using
134.Tn UDP
135datagrams.
136.Pp
137The
138.Fn endhostent
139function
140closes the
141.Tn TCP
142connection.
143.Sh FILES
144.Bl -tag -width /etc/hosts -compact
145.It Pa /etc/hosts
146.El
147.Sh DIAGNOSTICS
148Error return status from
149.Fn gethostbyname
150and
151.Fn gethostbyaddr
152is indicated by return of a null pointer.
153The external integer
154.Va h_errno
155may then be checked to see whether this is a temporary failure
156or an invalid or unknown host.
157The routine
158.Fn herror
159can be used to print an error message describing the failure.
160If its argument
161.Fa string
162is
163.Pf non Dv -NULL ,
164it is printed, followed by a colon and a space.
165The error message is printed with a trailing newline.
166.Pp
167The variable
168.Va h_errno
169can have the following values:
170.Bl -tag -width HOST_NOT_FOUND
171.It Dv HOST_NOT_FOUND
172No such host is known.
173.It Dv TRY_AGAIN
174This is usually a temporary error
175and means that the local server did not receive
176a response from an authoritative server.
177A retry at some later time may succeed.
178.It Dv NO_RECOVERY
179Some unexpected server failure was encountered.
180This is a non-recoverable error.
181.It Dv NO_DATA
182The requested name is valid but does not have an IP address;
183this is not a temporary error.
184This means that the name is known to the name server but there is no address
185associated with this name.
186Another type of request to the name server using this domain name
187will result in an answer;
188for example, a mail-forwarder may be registered for this domain.
189.El
190.Sh SEE ALSO
191.Xr resolver 3 ,
192.Xr hosts 5 ,
193.Xr hostname 7 ,
194.Xr named 8
195.Sh CAVEAT
196The
197.Fn gethostent
198function
199is defined, and
200.Fn sethostent
201and
202.Fn endhostent
203are redefined,
204when
205.Xr libc 3
206is built to use only the routines to lookup in
207.Pa /etc/hosts
208and not the name server.
209.Pp
210The
211.Fn gethostent
212function
213reads the next line of
214.Pa /etc/hosts ,
215opening the file if necessary.
216.Pp
217The
218.Fn sethostent
219function
220is redefined to open and rewind the file.  If the
221.Fa stayopen
222argument is non-zero,
223the hosts data base will not be closed after each call to
224.Fn gethostbyname
225or
226.Fn gethostbyaddr .
227The
228.Fn endhostent
229function
230is redefined to close the file.
231.Sh HISTORY
232The
233.Fn herror
234function appeared in
235.Bx 4.3 .
236The
237.Fn endhostent ,
238.Fn gethostbyaddr ,
239.Fn gethostbyname ,
240.Fn gethostent ,
241and
242.Fn sethostent
243functions appeared in
244.Bx 4.2 .
245.Sh BUGS
246These functions use static data storage;
247if the data is needed for future use, it should be
248copied before any subsequent calls overwrite it.
249Only the Internet
250address format is currently understood.
251