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