]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man/man3/gethostbyname.3
man/, share/mk/: Move man*/ to man/
[thirdparty/man-pages.git] / man / man3 / gethostbyname.3
CommitLineData
a1eaacb1 1'\" t
fea681da
MK
2.\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3.\"
5fbde956 4.\" SPDX-License-Identifier: Linux-man-pages-copyleft
fea681da
MK
5.\"
6.\" References consulted:
7.\" Linux libc source code
8.\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9.\" 386BSD man pages
10.\" Modified 1993-05-22, David Metcalfe
11.\" Modified 1993-07-25, Rik Faith (faith@cs.unc.edu)
12.\" Modified 1997-02-16, Andries Brouwer (aeb@cwi.nl)
13.\" Modified 1998-12-21, Andries Brouwer (aeb@cwi.nl)
14.\" Modified 2000-08-12, Andries Brouwer (aeb@cwi.nl)
15.\" Modified 2001-05-19, Andries Brouwer (aeb@cwi.nl)
16.\" Modified 2002-08-05, Michael Kerrisk
17.\" Modified 2004-10-31, Andries Brouwer
18.\"
4c1c5274 19.TH gethostbyname 3 (date) "Linux man-pages (unreleased)"
fea681da 20.SH NAME
d302a9a9 21gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
a039244f 22h_errno,
7fa78155 23herror, hstrerror,
1dcf5e2f 24gethostbyaddr_r,
7fa78155
MK
25gethostbyname2, gethostbyname2_r, gethostbyname_r,
26gethostent_r \- get network host entry
42009080
AC
27.SH LIBRARY
28Standard C library
29.RI ( libc ", " \-lc )
fea681da
MK
30.SH SYNOPSIS
31.nf
32.B #include <netdb.h>
c6d039a3 33.P
0bda2996
AC
34.BI "void sethostent(int " stayopen );
35.B void endhostent(void);
c6d039a3 36.P
dcd7bc53 37.B [[deprecated]] extern int h_errno;
c6d039a3 38.P
dcd7bc53 39.BI "[[deprecated]] struct hostent *gethostbyname(const char *" name );
c64cd13e
AC
40.BI "[[deprecated]] struct hostent *gethostbyaddr(const void " addr [. len ],
41.BI " socklen_t " len ", int " type );
c6d039a3 42.P
dcd7bc53
AC
43.BI "[[deprecated]] void herror(const char *" s );
44.BI "[[deprecated]] const char *hstrerror(int " err );
c6d039a3 45.P
80b50848 46/* System V/POSIX extension */
fea681da 47.B struct hostent *gethostent(void);
c6d039a3 48.P
fea681da 49/* GNU extensions */
dcd7bc53 50.B [[deprecated]]
fea681da 51.BI "struct hostent *gethostbyname2(const char *" name ", int " af );
c6d039a3 52.P
938c8215 53.BI "int gethostent_r(struct hostent *restrict " ret ,
1eed67e7 54.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
938c8215
AC
55.BI " struct hostent **restrict " result ,
56.BI " int *restrict " h_errnop );
c6d039a3 57.P
dcd7bc53 58.B [[deprecated]]
c64cd13e
AC
59.BI "int gethostbyaddr_r(const void " addr "[restrict ." len "], socklen_t " len ,
60.BI " int " type ,
938c8215 61.BI " struct hostent *restrict " ret ,
1eed67e7 62.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
938c8215
AC
63.BI " struct hostent **restrict " result ,
64.BI " int *restrict " h_errnop );
dcd7bc53 65.B [[deprecated]]
938c8215
AC
66.BI "int gethostbyname_r(const char *restrict " name ,
67.BI " struct hostent *restrict " ret ,
1eed67e7 68.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
938c8215
AC
69.BI " struct hostent **restrict " result ,
70.BI " int *restrict " h_errnop );
dcd7bc53 71.B [[deprecated]]
938c8215
AC
72.BI "int gethostbyname2_r(const char *restrict " name ", int " af,
73.BI " struct hostent *restrict " ret ,
1eed67e7 74.BI " char " buf "[restrict ." buflen "], size_t " buflen ,
938c8215
AC
75.BI " struct hostent **restrict " result ,
76.BI " int *restrict " h_errnop );
fea681da 77.fi
c6d039a3 78.P
d39ad78f 79.RS -4
cc4615cc
MK
80Feature Test Macro Requirements for glibc (see
81.BR feature_test_macros (7)):
d39ad78f 82.RE
c6d039a3 83.P
cc4615cc
MK
84.BR gethostbyname2 (),
85.BR gethostent_r (),
dd0fee67 86.BR gethostbyaddr_r (),
cc4615cc
MK
87.BR gethostbyname_r (),
88.BR gethostbyname2_r ():
9d2adbae
MK
89.nf
90 Since glibc 2.19:
91 _DEFAULT_SOURCE
75c018a1 92 glibc up to and including 2.19:
9d2adbae
MK
93 _BSD_SOURCE || _SVID_SOURCE
94.fi
c6d039a3 95.P
f87ab547 96.BR herror (),
6d80e8c7 97.BR hstrerror ():
9d2adbae
MK
98.nf
99 Since glibc 2.19:
100 _DEFAULT_SOURCE
75c018a1 101 glibc 2.8 to glibc 2.19:
9d2adbae
MK
102 _BSD_SOURCE || _SVID_SOURCE
103 Before glibc 2.8:
104 none
105.fi
c6d039a3 106.P
930c9b6d 107.BR h_errno :
9d2adbae
MK
108.nf
109 Since glibc 2.19
110 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
75c018a1 111 glibc 2.12 to glibc 2.19:
9d2adbae
MK
112 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
113 Before glibc 2.12:
114 none
115.fi
fea681da 116.SH DESCRIPTION
aa966d08 117The
7d0ec5b6
MK
118.BR gethostbyname* (),
119.BR gethostbyaddr* (),
120.BR herror (),
aa966d08 121and
7d0ec5b6 122.BR hstrerror ()
aa966d08 123functions are obsolete.
567e8a7f 124Applications should use
7d0ec5b6
MK
125.BR getaddrinfo (3),
126.BR getnameinfo (3),
567e8a7f 127and
7d0ec5b6 128.BR gai_strerror (3)
567e8a7f 129instead.
c6d039a3 130.P
fea681da 131The
0bda2996
AC
132.BR sethostent ()
133function specifies, if \fIstayopen\fP is true (1),
134that a connected TCP socket should be used for the name server queries and
135that the connection should remain open during successive queries.
136Otherwise, name server queries will use UDP datagrams.
c6d039a3 137.P
0bda2996
AC
138The
139.BR endhostent ()
140function ends the use of a TCP connection for name
141server queries.
c6d039a3 142.P
0bda2996 143The
fea681da
MK
144.BR gethostbyname ()
145function returns a structure of type
146.I hostent
147for the given host
148.IR name .
149Here
150.I name
0b8a39ad
MK
151is either a hostname or an IPv4 address in standard dot notation (as for
152.BR inet_addr (3)).
fea681da
MK
153If
154.I name
0b8a39ad 155is an IPv4 address, no lookup is performed and
fea681da
MK
156.BR gethostbyname ()
157simply copies
158.I name
159into the
160.I h_name
161field and its
162.I struct in_addr
163equivalent into the
164.I h_addr_list[0]
165field of the returned
166.I hostent
167structure.
168If
169.I name
170doesn't end in a dot and the environment variable
171.B HOSTALIASES
172is set, the alias file pointed to by
173.B HOSTALIASES
174will first be searched for
175.I name
176(see
177.BR hostname (7)
178for the file format).
179The current domain and its parents are searched unless \fIname\fP
180ends in a dot.
c6d039a3 181.P
60a90ecd
MK
182The
183.BR gethostbyaddr ()
184function returns a structure of type \fIhostent\fP
fea681da 185for the given host address \fIaddr\fP of length \fIlen\fP and address type
c13182ef
MK
186\fItype\fP.
187Valid address types are
fea681da
MK
188.B AF_INET
189and
1ae6b2c7 190.B AF_INET6
742694e9
MK
191(defined in
192.IR <sys/socket.h> ).
fea681da 193The host address argument is a pointer to a struct of a type depending
89f9f5b2 194on the address type, for example a \fIstruct in_addr *\fP (probably
988db661 195obtained via a call to
d9c1ae64 196.BR inet_addr (3))
2f0af33b
MK
197for address type
198.BR AF_INET .
c6d039a3 199.P
60a90ecd
MK
200The (obsolete)
201.BR herror ()
202function prints the error message associated
ce2801d0 203with the current value of \fIh_errno\fP on \fIstderr\fP.
c6d039a3 204.P
60a90ecd
MK
205The (obsolete)
206.BR hstrerror ()
207function takes an error number
fea681da 208(typically \fIh_errno\fP) and returns the corresponding message string.
c6d039a3 209.P
60a90ecd
MK
210The domain name queries carried out by
211.BR gethostbyname ()
212and
213.BR gethostbyaddr ()
923d125a 214rely on the Name Service Switch
0fe4c79a 215.RB ( nsswitch.conf (5))
923d125a
CD
216configured sources or a local name server
217.RB ( named (8)).
81d6e51d 218The default action is to query the Name Service Switch
3c320453 219.RB ( nsswitch.conf (5))
923d125a 220configured sources, failing that, a local name server
0fe4c79a 221.RB ( named (8)).
787dd4ad 222.\"
923d125a
CD
223.SS Historical
224The
225.BR nsswitch.conf (5)
226file is the modern way of controlling the order of host lookups.
c6d039a3 227.P
923d125a
CD
228In glibc 2.4 and earlier, the
229.I order
230keyword was used to control the order of host lookups as defined in
1ae6b2c7 231.I /etc/host.conf
beb9356f 232.RB ( host.conf (5)).
c6d039a3 233.P
fea681da 234The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
c6d039a3 235.P
a08ea57c 236.in +4n
b8302363 237.EX
fea681da 238struct hostent {
b9f02710
MK
239 char *h_name; /* official name of host */
240 char **h_aliases; /* alias list */
241 int h_addrtype; /* host address type */
242 int h_length; /* length of address */
243 char **h_addr_list; /* list of addresses */
fea681da 244}
b9f02710 245#define h_addr h_addr_list[0] /* for backward compatibility */
b8302363 246.EE
a08ea57c 247.in
c6d039a3 248.P
fea681da
MK
249The members of the \fIhostent\fP structure are:
250.TP
251.I h_name
252The official name of the host.
253.TP
254.I h_aliases
b437fdd9 255An array of alternative names for the host, terminated by a null pointer.
fea681da
MK
256.TP
257.I h_addrtype
258The type of address; always
259.B AF_INET
260or
261.B AF_INET6
262at present.
263.TP
264.I h_length
265The length of the address in bytes.
266.TP
267.I h_addr_list
28d88c17 268An array of pointers to network addresses for the host (in network byte
b437fdd9 269order), terminated by a null pointer.
fea681da
MK
270.TP
271.I h_addr
272The first address in \fIh_addr_list\fP for backward compatibility.
47297adb 273.SH RETURN VALUE
fea681da
MK
274The
275.BR gethostbyname ()
276and
63aa9df0 277.BR gethostbyaddr ()
fea681da
MK
278functions return the
279.I hostent
b437fdd9 280structure or a null pointer if an error occurs.
c13182ef 281On error, the
fea681da
MK
282.I h_errno
283variable holds an error number.
284When non-NULL, the return value may point at static data, see the notes below.
285.SH ERRORS
286The variable \fIh_errno\fP can have the following values:
287.TP
288.B HOST_NOT_FOUND
289The specified host is unknown.
290.TP
1ae6b2c7 291.B NO_DATA
fea681da 292The requested name is valid but does not have an IP address.
ae9b41ac
MK
293Another type of request to the name server for this domain
294may return an answer.
491e87a5 295The constant
1ae6b2c7 296.B NO_ADDRESS
491e87a5
MK
297is a synonym for
298.BR NO_DATA .
fea681da
MK
299.TP
300.B NO_RECOVERY
b28f6e56 301A nonrecoverable name server error occurred.
fea681da
MK
302.TP
303.B TRY_AGAIN
c13182ef
MK
304A temporary error occurred on an authoritative name server.
305Try again later.
fea681da
MK
306.SH FILES
307.TP
308.I /etc/host.conf
309resolver configuration file
310.TP
311.I /etc/hosts
312host database file
24268a72
MK
313.TP
314.I /etc/nsswitch.conf
315name service switch configuration
c7bd50f1
ZL
316.SH ATTRIBUTES
317For an explanation of the terms used in this section, see
318.BR attributes (7).
319.TS
320allbox;
c466875e 321lb lb lbx
c7bd50f1
ZL
322l l l.
323Interface Attribute Value
324T{
9e54434e
BR
325.na
326.nh
c7bd50f1
ZL
327.BR gethostbyname ()
328T} Thread safety T{
9e54434e
BR
329.na
330.nh
c7bd50f1 331MT-Unsafe race:hostbyname env
c7bd50f1
ZL
332locale
333T}
334T{
9e54434e
BR
335.na
336.nh
c7bd50f1
ZL
337.BR gethostbyaddr ()
338T} Thread safety T{
9e54434e
BR
339.na
340.nh
c7bd50f1 341MT-Unsafe race:hostbyaddr env
c7bd50f1
ZL
342locale
343T}
344T{
9e54434e
BR
345.na
346.nh
c7bd50f1 347.BR sethostent (),
c7bd50f1 348.BR endhostent (),
c7bd50f1
ZL
349.BR gethostent_r ()
350T} Thread safety T{
9e54434e
BR
351.na
352.nh
c7bd50f1 353MT-Unsafe race:hostent env
c7bd50f1
ZL
354locale
355T}
356T{
9e54434e
BR
357.na
358.nh
c7bd50f1 359.BR herror (),
c7bd50f1
ZL
360.BR hstrerror ()
361T} Thread safety MT-Safe
362T{
9e54434e
BR
363.na
364.nh
c7bd50f1
ZL
365.BR gethostent ()
366T} Thread safety T{
9e54434e
BR
367.na
368.nh
c7bd50f1 369MT-Unsafe race:hostent
c7bd50f1
ZL
370race:hostentbuf env locale
371T}
372T{
9e54434e
BR
373.na
374.nh
c7bd50f1
ZL
375.BR gethostbyname2 ()
376T} Thread safety T{
9e54434e
BR
377.na
378.nh
c7bd50f1 379MT-Unsafe race:hostbyname2
c7bd50f1
ZL
380env locale
381T}
382T{
9e54434e
BR
383.na
384.nh
c7bd50f1
ZL
385.BR gethostbyaddr_r (),
386.BR gethostbyname_r (),
387.BR gethostbyname2_r ()
388T} Thread safety MT-Safe env locale
389.TE
c6d039a3 390.P
c7bd50f1
ZL
391In the above table,
392.I hostent
393in
394.I race:hostent
395signifies that if any of the functions
a39dc3ee
MK
396.BR sethostent (),
397.BR gethostent (),
398.BR gethostent_r (),
c7bd50f1 399or
18a46670 400.BR \%endhostent ()
c7bd50f1
ZL
401are used in parallel in different threads of a program,
402then data races could occur.
3113c7f3 403.SH STANDARDS
4131356c
AC
404.TP
405.BR sethostent ()
406.TQ
407.BR endhostent ()
408.TQ
409.BR gethostent ()
410POSIX.1-2008.
411.TP
412.BR gethostent_r ()
413GNU.
414.TP
415Others:
416None.
417.SH HISTORY
418.TP
419.BR sethostent ()
420.TQ
421.BR endhostent ()
422.TQ
423.BR gethostent ()
424POSIX.1-2001.
425.TP
426.BR gethostbyname ()
427.TQ
428.BR gethostbyaddr ()
429.TQ
1ae6b2c7 430.I h_errno
4131356c
AC
431Marked obsolescent in POSIX.1-2001.
432Removed in POSIX.1-2008,
aeb2174a
MK
433recommending the use of
434.BR getaddrinfo (3)
435and
436.BR getnameinfo (3)
437instead.
8af1ba10
MK
438.SH NOTES
439The functions
440.BR gethostbyname ()
441and
442.BR gethostbyaddr ()
443may return pointers to static data, which may be overwritten by
444later calls.
445Copying the
446.I struct hostent
447does not suffice, since it contains pointers; a deep copy is required.
c6d039a3 448.P
988db661 449In the original BSD implementation the
f9ee70bd
MK
450.I len
451argument
988db661 452of
f9ee70bd
MK
453.BR gethostbyname ()
454was an
455.IR int .
456The SUSv2 standard is buggy and declares the
8af1ba10 457.I len
c4bb193f 458argument of
8af1ba10
MK
459.BR gethostbyaddr ()
460to be of type
461.IR size_t .
462(That is wrong, because it has to be
463.IR int ,
464and
465.I size_t
466is not.
467POSIX.1-2001 makes it
468.IR socklen_t ,
469which is OK.)
f9ee70bd
MK
470See also
471.BR accept (2).
c6d039a3 472.P
8af1ba10
MK
473The BSD prototype for
474.BR gethostbyaddr ()
475uses
5049da5b 476.I "const char\ *"
8af1ba10 477for the first argument.
73d8cece 478.SS System V/POSIX extension
fea681da
MK
479POSIX requires the
480.BR gethostent ()
7f325581 481call, which should return the next entry in the host data base.
fea681da
MK
482When using DNS/BIND this does not make much sense, but it may
483be reasonable if the host data base is a file that can be read
c13182ef 484line by line.
7f325581 485On many systems, a routine of this name reads
fea681da
MK
486from the file
487.IR /etc/hosts .
1f409d5d 488.\" e.g., Linux, FreeBSD, UnixWare, HP-UX
fea681da 489It may be available only when the library was built without DNS support.
75b94dc3 490.\" e.g., FreeBSD, AIX
c13182ef
MK
491The glibc version will ignore ipv6 entries.
492This function is not reentrant,
fea681da
MK
493and glibc adds a reentrant version
494.BR gethostent_r ().
73d8cece 495.SS GNU extensions
75c018a1 496glibc2 also has a
63aa9df0 497.BR gethostbyname2 ()
fea681da 498that works like
63aa9df0 499.BR gethostbyname (),
fea681da 500but permits to specify the address family to which the address must belong.
c6d039a3 501.P
75c018a1 502glibc2 also has reentrant versions
dd0fee67
MK
503.BR gethostent_r (),
504.BR gethostbyaddr_r (),
d556548b 505.BR gethostbyname_r (),
fea681da 506and
63aa9df0 507.BR gethostbyname2_r ().
635e8d5a
MK
508The caller supplies a
509.I hostent
510structure
dd0fee67 511.I ret
635e8d5a 512which will be filled in on success, and a temporary work buffer
fea681da 513.I buf
dd0fee67 514of size
fea681da 515.IR buflen .
dd0fee67
MK
516After the call,
517.I result
20523df7
MK
518will point to the result on success.
519In case of an error
dd0fee67
MK
520or if no entry is found
521.I result
635e8d5a 522will be NULL.
c7094399 523The functions return 0 on success and a nonzero error number on failure.
54d75d6c 524In addition to the errors returned by the nonreentrant
dd0fee67
MK
525versions of these functions, if
526.I buf
527is too small, the functions will return
528.BR ERANGE ,
529and the call should be retried with a larger buffer.
530The global variable
fea681da 531.I h_errno
dd0fee67 532is not modified, but the address of a variable in which to store error numbers
fea681da
MK
533is passed in
534.IR h_errnop .
444aa1ce
MK
535.SH BUGS
536.BR gethostbyname ()
537does not recognize components of a dotted IPv4 address string
538that are expressed in hexadecimal.
539.\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
47297adb 540.SH SEE ALSO
fea681da 541.BR getaddrinfo (3),
652abd33
MK
542.\" .BR getipnodebyaddr (3),
543.\" .BR getipnodebyname (3),
fea681da 544.BR getnameinfo (3),
444aa1ce 545.BR inet (3),
fea681da
MK
546.BR inet_ntop (3),
547.BR inet_pton (3),
548.BR resolver (3),
549.BR hosts (5),
24268a72 550.BR nsswitch.conf (5),
fea681da 551.BR hostname (7),
24268a72
MK
552.BR named (8)
553.\" .BR resolv+ (8)