xref: /netbsd-src/lib/libc/net/gethostbyname.3 (revision 5f7096188587a2c7c95fa3c69b78e1ec9c7923d0)
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.2 1993/08/01 07:45:22 mycroft 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 struct h_errno;
49.Ft struct hostent *
50.Fn gethostbyname "char *name"
51.Ft struct hostent *
52.Fn gethostbyaddr "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 ,
69or broken-out fields from a line in
70.Pa /etc/hosts .
71If the local name server is not running these routines do a lookup in
72.Pa /etc/hosts .
73.Bd -literal
74struct	hostent {
75	char	*h_name;	/* official name of host */
76	char	**h_aliases;	/* alias list */
77	int	h_addrtype;	/* host address type */
78	int	h_length;	/* length of address */
79	char	**h_addr_list;	/* list of addresses from name server */
80};
81#define	h_addr  h_addr_list[0]	/* address, for backward compatibility */
82.Ed
83.Pp
84The members of this structure are:
85.Bl -tag -width h_addr_list
86.It Fa h_name
87Official name of the host.
88.It Fa h_aliases
89A zero terminated array of alternate names for the host.
90.It Fa h_addrtype
91The type of address being returned; currently always
92.Dv AF_INET .
93.It Fa h_length
94The length, in bytes, of the address.
95.It Fa h_addr_list
96A zero terminated array of network addresses for the host.
97Host addresses are returned in network byte order.
98.It Fa h_addr
99The first address in
100.Fa h_addr_list ;
101this is for backward compatiblity.
102.Pp
103When using the nameserver,
104.Fn gethostbyname
105will search for the named host in the current domain and its parents
106unless the name ends in a dot.
107If the name contains no dot, and if the environment variable
108.Dq Ev HOSTALIASES
109contains the name of an alias file, the alias file will first be searched
110for an alias matching the input name.
111See
112.Xr hostname 7
113for the domain search procedure and the alias file format.
114.Pp
115The
116.Fn sethostent
117function
118may be used to request the use of a connected
119.Tn TCP
120socket for queries.
121If the
122.Fa stayopen
123flag is non-zero,
124this sets the option to send all queries to the name server using
125.Tn TCP
126and to retain the connection after each call to
127.Fn gethostbyname
128or
129.Fn gethostbyaddr .
130Otherwise, queries are performed using
131.Tn UDP
132datagrams.
133.Pp
134The
135.Fn endhostent
136function
137closes the
138.Tn TCP
139connection.
140.Sh FILES
141.Bl -tag -width /etc/hosts -compact
142.It Pa /etc/hosts
143.El
144.Sh DIAGNOSTICS
145Error return status from
146.Fn gethostbyname
147and
148.Fn gethostbyaddr
149is indicated by return of a null pointer.
150The external integer
151.Va h_errno
152may then be checked to see whether this is a temporary failure
153or an invalid or unknown host.
154The routine
155.Fn herror
156can be used to print an error message describing the failure.
157If its argument
158.Fa string
159is
160.Pf non Dv -NULL ,
161it is printed, followed by a colon and a space.
162The error message is printed with a trailing newline.
163.Pp
164The variable
165.Va h_errno
166can have the following values:
167.Bl -tag -width HOST_NOT_FOUND
168.It Dv HOST_NOT_FOUND
169No such host is known.
170.It Dv TRY_AGAIN
171This is usually a temporary error
172and means that the local server did not receive
173a response from an authoritative server.
174A retry at some later time may succeed.
175.It Dv NO_RECOVERY
176Some unexpected server failure was encountered.
177This is a non-recoverable error.
178.It Dv NO_DATA
179The requested name is valid but does not have an IP address;
180this is not a temporary error.
181This means that the name is known to the name server but there is no address
182associated with this name.
183Another type of request to the name server using this domain name
184will result in an answer;
185for example, a mail-forwarder may be registered for this domain.
186.El
187.Sh SEE ALSO
188.Xr resolver 3 ,
189.Xr hosts 5 ,
190.Xr hostname 7 ,
191.Xr named 8
192.Sh CAVEAT
193The
194.Fn gethostent
195function
196is defined, and
197.Fn sethostent
198and
199.Fn endhostent
200are redefined,
201when
202.Xr libc 3
203is built to use only the routines to lookup in
204.Pa /etc/hosts
205and not the name server.
206.Pp
207The
208.Fn gethostent
209function
210reads the next line of
211.Pa /etc/hosts ,
212opening the file if necessary.
213.Pp
214The
215.Fn sethostent
216function
217is redefined to open and rewind the file.  If the
218.Fa stayopen
219argument is non-zero,
220the hosts data base will not be closed after each call to
221.Fn gethostbyname
222or
223.Fn gethostbyaddr .
224The
225.Fn endhostent
226function
227is redefined to close the file.
228.Sh HISTORY
229The
230.Fn herror
231function appeared in
232.Bx 4.3 .
233The
234.Fn endhostent ,
235.Fn gethostbyaddr ,
236.Fn gethostbyname ,
237.Fn gethostent ,
238and
239.Fn sethostent
240functions appeared in
241.Bx 4.2 .
242.Sh BUGS
243These functions use static data storage;
244if the data is needed for future use, it should be
245copied before any subsequent calls overwrite it.
246Only the Internet
247address format is currently understood.
248