]> git.saurik.com Git - apple/libinfo.git/blame - gen.subproj/gethostbyname.3
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / gen.subproj / gethostbyname.3
CommitLineData
3b7c7bd7
A
1.\" Copyright (c) 1983, 1987, 1991, 1993
2.\" The Regents of the University of California. 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.
3b7c7bd7
A
12.\" 4. Neither the name of the University nor the names of its contributors
13.\" may be used to endorse or promote products derived from this software
14.\" without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
d49d4c81 29.\" $FreeBSD: src/lib/libc/net/gethostbyname.3,v 1.38 2007/01/09 00:28:02 imp Exp $
3b7c7bd7 30.\"
d49d4c81 31.Dd May 12, 2006
3b7c7bd7
A
32.Dt GETHOSTBYNAME 3
33.Os
34.Sh NAME
35.Nm gethostbyname ,
36.Nm gethostbyname2 ,
d49d4c81 37.Nm gethostbyaddr ,
3b7c7bd7 38.Nm gethostent ,
d49d4c81
A
39.Nm sethostent ,
40.Nm endhostent ,
3b7c7bd7 41.Nm herror ,
d49d4c81 42.Nm hstrerror
3b7c7bd7
A
43.Nd get network host entry
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
d49d4c81
A
47.In netdb.h
48.Vt int h_errno ;
3b7c7bd7 49.Ft struct hostent *
d49d4c81 50.Fn gethostbyname "const char *name"
3b7c7bd7 51.Ft struct hostent *
d49d4c81 52.Fn gethostbyname2 "const char *name" "int af"
3b7c7bd7 53.Ft struct hostent *
d49d4c81 54.Fn gethostbyaddr "const void *addr" "socklen_t len" "int type"
3b7c7bd7 55.Ft struct hostent *
d49d4c81 56.Fn gethostent void
3b7c7bd7 57.Ft void
d49d4c81 58.Fn sethostent "int stayopen"
b3dd680f 59.Ft void
d49d4c81
A
60.Fn endhostent void
61.Ft void
62.Fn herror "const char *string"
63.Ft const char *
64.Fn hstrerror "int err"
3b7c7bd7 65.Sh DESCRIPTION
d49d4c81
A
66.Bf -symbolic
67The
68.Xr getaddrinfo 3
69and
70.Xr getnameinfo 3
71functions are preferred over the
72.Fn gethostbyname ,
73.Fn gethostbyname2 ,
74and
75.Fn gethostbyaddr
76functions.
77.Ef
78.Pp
3b7c7bd7
A
79The
80.Fn gethostbyname ,
81.Fn gethostbyname2
82and
83.Fn gethostbyaddr
84functions
85each return a pointer to an object with the
86following structure describing an internet host
87referenced by name or by address, respectively.
d49d4c81
A
88.Pp
89The
90.Fa name
91argument passed to
92.Fn gethostbyname
93or
94.Fn gethostbyname2
95should point to a
96.Dv NUL Ns -terminated
97hostname.
98The
99.Fa addr
100argument passed to
101.Fn gethostbyaddr
102should point to an address which is
103.Fa len
104bytes long,
105in binary form
106(i.e., not an IP address in human readable
107.Tn ASCII
108form).
109The
110.Fa type
111argument specifies the address family
112(e.g.\&
113.Dv AF_INET , AF_INET6 ,
114etc.) of this address.
115.Pp
116The structure returned contains information obtained from
d31dd049 117.Xr mDNSResponder 8 ,
d49d4c81 118including records in
3b7c7bd7 119.Pa /etc/hosts .
d49d4c81
A
120.\"The structure returned contains either the information obtained from the name
121.\"server,
122.\".Xr named 8 ,
123.\"broken-out fields from a line in
124.\".Pa /etc/hosts ,
125.\"or database entries supplied by the
126.\".Xr yp 8
127.\"system.
128.\"The order of the lookups is controlled by the
129.\".Sq hosts
130.\"entry in
131.\".Xr nsswitch.conf 5 .
3b7c7bd7
A
132.Bd -literal
133struct hostent {
134 char *h_name; /* official name of host */
135 char **h_aliases; /* alias list */
136 int h_addrtype; /* host address type */
137 int h_length; /* length of address */
138 char **h_addr_list; /* list of addresses from name server */
139};
140#define h_addr h_addr_list[0] /* address, for backward compatibility */
141.Ed
142.Pp
143The members of this structure are:
144.Bl -tag -width h_addr_list
145.It Va h_name
146Official name of the host.
147.It Va h_aliases
148A
149.Dv NULL Ns -terminated
150array of alternate names for the host.
151.It Va h_addrtype
152The type of address being returned; usually
153.Dv AF_INET .
154.It Va h_length
155The length, in bytes, of the address.
156.It Va h_addr_list
157A
158.Dv NULL Ns -terminated
159array of network addresses for the host.
160Host addresses are returned in network byte order.
161.It Va h_addr
162The first address in
163.Va h_addr_list ;
164this is for backward compatibility.
165.El
166.Pp
167When using the nameserver,
168.Fn gethostbyname
169and
170.Fn gethostbyname2
171will search for the named host in the current domain and its parents
172unless the name ends in a dot.
d49d4c81
A
173.\"If the name contains no dot, and if the environment variable
174.\".Dq Ev HOSTALIASES
175.\"contains the name of an alias file, the alias file will first be searched
176.\"for an alias matching the input name.
177.\"See
178.\".Xr hostname 7
179.\"for the domain search procedure and the alias file format.
3b7c7bd7
A
180.Pp
181The
182.Fn gethostbyname2
183function is an evolution of
184.Fn gethostbyname
185which is intended to allow lookups in address families other than
186.Dv AF_INET ,
187for example
188.Dv AF_INET6 .
3b7c7bd7 189.Pp
d49d4c81
A
190.\"The
191.\".Fn sethostent
192.\"function
193.\"may be used to request the use of a connected
194.\".Tn TCP
195.\"socket for queries.
196.\"If the
197.\".Fa stayopen
198.\"flag is non-zero,
199.\"this sets the option to send all queries to the name server using
200.\".Tn TCP
201.\"and to retain the connection after each call to
202.\".Fn gethostbyname ,
203.\".Fn gethostbyname2
204.\"or
205.\".Fn gethostbyaddr .
206.\"Otherwise, queries are performed using
207.\".Tn UDP
208.\"datagrams.
209.\".Pp
210.\"The
211.\".Fn endhostent
212.\"function
213.\"closes the
214.\".Tn TCP
215.\"connection.
216.\".Pp
3b7c7bd7
A
217The
218.Fn herror
219function writes a message to the diagnostic output consisting of the
d49d4c81
A
220string argument
221.Fa string ,
3b7c7bd7
A
222the constant string
223.Qq Li ":\ " ,
224and a message corresponding to the value of
225.Va h_errno .
226.Pp
227The
228.Fn hstrerror
229function returns a string which is the message text corresponding to the
230value of the
231.Fa err
d49d4c81 232argument.
53dd3e4a
A
233.Pp
234Historically, passing a host's own hostname to
235.Fn gethostbyname
236or
237.Fn gethostbyname2
238has been a popular
239technique for determining that host's IP address(es), but this is fragile,
240and doesn't work reliably in all cases. The appropriate way for software to
241discover the IP address(es) of the host it is running on is to use
242.Xr getifaddrs 3 .
243.Pp
3b7c7bd7 244.Sh FILES
d49d4c81 245.Bl -tag -width /etc/nsswitch.conf -compact
3b7c7bd7 246.It Pa /etc/hosts
d49d4c81 247.\".It Pa /etc/nsswitch.conf
3b7c7bd7
A
248.It Pa /etc/resolv.conf
249.El
d49d4c81
A
250.Sh EXAMPLES
251Print out the hostname associated with a specific IP address:
252.Bd -literal -offset indent
253const char *ipstr = "127.0.0.1";
254struct in_addr ip;
255struct hostent *hp;
256
257if (!inet_aton(ipstr, &ip))
258 errx(1, "can't parse IP address %s", ipstr);
259
260if ((hp = gethostbyaddr((const void *)&ip,
261 sizeof ip, AF_INET)) == NULL)
262 errx(1, "no name associated with %s", ipstr);
263
264printf("name associated with %s is %s\en", ipstr, hp->h_name);
265.Ed
3b7c7bd7
A
266.Sh DIAGNOSTICS
267Error return status from
268.Fn gethostbyname ,
269.Fn gethostbyname2
270and
271.Fn gethostbyaddr
272is indicated by return of a
273.Dv NULL
274pointer.
d49d4c81 275The integer
3b7c7bd7
A
276.Va h_errno
277may then be checked to see whether this is a temporary failure
278or an invalid or unknown host.
279The routine
280.Fn herror
281can be used to print an error message describing the failure.
282If its argument
283.Fa string
284is
285.Pf non- Dv NULL ,
286it is printed, followed by a colon and a space.
287The error message is printed with a trailing newline.
288.Pp
289The variable
290.Va h_errno
291can have the following values:
292.Bl -tag -width HOST_NOT_FOUND
293.It Dv HOST_NOT_FOUND
294No such host is known.
295.It Dv TRY_AGAIN
296This is usually a temporary error
297and means that the local server did not receive
298a response from an authoritative server.
299A retry at some later time may succeed.
300.It Dv NO_RECOVERY
301Some unexpected server failure was encountered.
302This is a non-recoverable error.
303.It Dv NO_DATA
304The requested name is valid but does not have an IP address;
305this is not a temporary error.
306This means that the name is known to the name server but there is no address
307associated with this name.
308Another type of request to the name server using this domain name
309will result in an answer;
310for example, a mail-forwarder may be registered for this domain.
311.El
312.Sh SEE ALSO
313.Xr getaddrinfo 3 ,
d49d4c81
A
314.Xr getnameinfo 3 ,
315.Xr inet_aton 3 ,
3b7c7bd7
A
316.Xr resolver 3 ,
317.Xr hosts 5 ,
318.Xr hostname 7 ,
d31dd049 319.Xr mDNSResponder 8
d49d4c81 320.\".Xr named 8
3b7c7bd7
A
321.Sh CAVEAT
322The
323.Fn gethostent
324function
325is defined, and
326.Fn sethostent
327and
328.Fn endhostent
329are redefined,
d49d4c81
A
330when
331.Lb libc
332is built to use only the routines to lookup in
3b7c7bd7
A
333.Pa /etc/hosts
334and not the name server.
335.Pp
336The
337.Fn gethostent
338function
339reads the next line of
340.Pa /etc/hosts ,
341opening the file if necessary.
342.Pp
343The
344.Fn sethostent
345function
346opens and/or rewinds the file
347.Pa /etc/hosts .
348If the
349.Fa stayopen
350argument is non-zero,
351the file will not be closed after each call to
352.Fn gethostbyname ,
353.Fn gethostbyname2
354or
355.Fn gethostbyaddr .
356.Pp
357The
358.Fn endhostent
359function
360closes the file.
361.Sh HISTORY
362The
363.Fn herror
364function appeared in
365.Bx 4.3 .
366The
367.Fn endhostent ,
368.Fn gethostbyaddr ,
369.Fn gethostbyname ,
370.Fn gethostent ,
371and
372.Fn sethostent
373functions appeared in
374.Bx 4.2 .
375The
376.Fn gethostbyname2
377function first appeared in
378.Tn BIND
379version 4.9.4.
380.Sh BUGS
d49d4c81 381These functions use a thread-specific data storage;
3b7c7bd7
A
382if the data is needed for future use, it should be
383copied before any subsequent calls overwrite it.
d49d4c81
A
384.Pp
385Though these functions are thread-safe,
386still it is recommended to use the
387.Xr getaddrinfo 3
388family of functions, instead.
389.Pp
3b7c7bd7
A
390Only the Internet
391address format is currently understood.