1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" References consulted:
26 .\" Linux libc source code
27 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
38 .TH GETHOSTBYNAME 3 2021-03-22 "" "Linux Programmer's Manual"
40 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
44 gethostbyname2, gethostbyname2_r, gethostbyname_r,
45 gethostent_r \- get network host entry
50 .B extern int h_errno;
52 .BI "struct hostent *gethostbyname(const char *" name );
53 .BI "struct hostent *gethostbyaddr(const void *" addr ,
54 .BI " socklen_t " len ", int " type );
56 .BI "void sethostent(int " stayopen );
57 .B void endhostent(void);
59 .BI "void herror(const char *" s );
60 .BI "const char *hstrerror(int " err );
62 /* System V/POSIX extension */
63 .B struct hostent *gethostent(void);
66 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
68 .BI "int gethostent_r(struct hostent *restrict " ret ,
69 .BI " char *restrict " buf ", size_t " buflen ,
70 .BI " struct hostent **restrict " result ,
71 .BI " int *restrict " h_errnop );
73 .BI "int gethostbyaddr_r(const void *restrict " addr ", socklen_t " len \
75 .BI " struct hostent *restrict " ret ,
76 .BI " char *restrict " buf ", size_t " buflen ,
77 .BI " struct hostent **restrict " result ,
78 .BI " int *restrict " h_errnop );
79 .BI "int gethostbyname_r(const char *restrict " name ,
80 .BI " struct hostent *restrict " ret ,
81 .BI " char *restrict " buf ", size_t " buflen ,
82 .BI " struct hostent **restrict " result ,
83 .BI " int *restrict " h_errnop );
84 .BI "int gethostbyname2_r(const char *restrict " name ", int " af,
85 .BI " struct hostent *restrict " ret ,
86 .BI " char *restrict " buf ", size_t " buflen ,
87 .BI " struct hostent **restrict " result ,
88 .BI " int *restrict " h_errnop );
92 Feature Test Macro Requirements for glibc (see
93 .BR feature_test_macros (7)):
96 .BR gethostbyname2 (),
98 .BR gethostbyaddr_r (),
99 .BR gethostbyname_r (),
100 .BR gethostbyname2_r ():
104 Glibc up to and including 2.19:
105 _BSD_SOURCE || _SVID_SOURCE
114 _BSD_SOURCE || _SVID_SOURCE
122 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
124 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
130 .BR gethostbyname* (),
131 .BR gethostbyaddr* (),
135 functions are obsolete.
136 Applications should use
145 function returns a structure of type
151 is either a hostname or an IPv4 address in standard dot notation (as for
155 is an IPv4 address, no lookup is performed and
165 field of the returned
170 doesn't end in a dot and the environment variable
172 is set, the alias file pointed to by
174 will first be searched for
178 for the file format).
179 The current domain and its parents are searched unless \fIname\fP
184 function returns a structure of type \fIhostent\fP
185 for the given host address \fIaddr\fP of length \fIlen\fP and address type
187 Valid address types are
192 .IR <sys/socket.h> ).
193 The host address argument is a pointer to a struct of a type depending
194 on the address type, for example a \fIstruct in_addr *\fP (probably
195 obtained via a call to
202 function specifies, if \fIstayopen\fP is true (1),
203 that a connected TCP socket should be used for the name server queries and
204 that the connection should remain open during successive queries.
205 Otherwise, name server queries will use UDP datagrams.
209 function ends the use of a TCP connection for name
214 function prints the error message associated
215 with the current value of \fIh_errno\fP on \fIstderr\fP.
219 function takes an error number
220 (typically \fIh_errno\fP) and returns the corresponding message string.
222 The domain name queries carried out by
226 rely on the Name Service Switch
227 .RB ( nsswitch.conf (5))
228 configured sources or a local name server
230 The default action is to query the Name Service Switch
231 .RB ( nsswitch.conf (5))
232 configured sources, failing that, a local name server
237 .BR nsswitch.conf (5)
238 file is the modern way of controlling the order of host lookups.
240 In glibc 2.4 and earlier, the
242 keyword was used to control the order of host lookups as defined in
244 .RB ( host.conf (5)).
246 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
251 char *h_name; /* official name of host */
252 char **h_aliases; /* alias list */
253 int h_addrtype; /* host address type */
254 int h_length; /* length of address */
255 char **h_addr_list; /* list of addresses */
257 #define h_addr h_addr_list[0] /* for backward compatibility */
261 The members of the \fIhostent\fP structure are:
264 The official name of the host.
267 An array of alternative names for the host, terminated by a null pointer.
270 The type of address; always
277 The length of the address in bytes.
280 An array of pointers to network addresses for the host (in network byte
281 order), terminated by a null pointer.
284 The first address in \fIh_addr_list\fP for backward compatibility.
292 structure or a null pointer if an error occurs.
295 variable holds an error number.
296 When non-NULL, the return value may point at static data, see the notes below.
298 The variable \fIh_errno\fP can have the following values:
301 The specified host is unknown.
304 The requested name is valid but does not have an IP address.
305 Another type of request to the name server for this domain
306 may return an answer.
313 A nonrecoverable name server error occurred.
316 A temporary error occurred on an authoritative name server.
321 resolver configuration file
326 .I /etc/nsswitch.conf
327 name service switch configuration
329 For an explanation of the terms used in this section, see
337 Interface Attribute Value
341 MT-Unsafe race:hostbyname env
347 MT-Unsafe race:hostbyaddr env
355 MT-Unsafe race:hostent env
361 T} Thread safety MT-Safe
365 MT-Unsafe race:hostent
366 race:hostentbuf env locale
369 .BR gethostbyname2 ()
371 MT-Unsafe race:hostbyname2
375 .BR gethostbyaddr_r (),
376 .BR gethostbyname_r (),
377 .BR gethostbyname2_r ()
378 T} Thread safety MT-Safe env locale
387 signifies that if any of the functions
393 are used in parallel in different threads of a program,
394 then data races could occur.
396 POSIX.1-2001 specifies
397 .BR gethostbyname (),
398 .BR gethostbyaddr (),
404 .BR gethostbyname (),
405 .BR gethostbyaddr (),
408 are marked obsolescent in that standard.
409 POSIX.1-2008 removes the specifications of
410 .BR gethostbyname (),
411 .BR gethostbyaddr (),
414 recommending the use of
424 may return pointers to static data, which may be overwritten by
428 does not suffice, since it contains pointers; a deep copy is required.
430 In the original BSD implementation the
437 The SUSv2 standard is buggy and declares the
443 (That is wrong, because it has to be
448 POSIX.1-2001 makes it
454 The BSD prototype for
458 for the first argument.
459 .SS System V/POSIX extension
462 call, which should return the next entry in the host data base.
463 When using DNS/BIND this does not make much sense, but it may
464 be reasonable if the host data base is a file that can be read
466 On many systems, a routine of this name reads
469 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
470 It may be available only when the library was built without DNS support.
471 .\" e.g., FreeBSD, AIX
472 The glibc version will ignore ipv6 entries.
473 This function is not reentrant,
474 and glibc adds a reentrant version
478 .BR gethostbyname2 ()
480 .BR gethostbyname (),
481 but permits to specify the address family to which the address must belong.
483 Glibc2 also has reentrant versions
485 .BR gethostbyaddr_r (),
486 .BR gethostbyname_r (),
488 .BR gethostbyname2_r ().
489 The caller supplies a
493 which will be filled in on success, and a temporary work buffer
499 will point to the result on success.
501 or if no entry is found
504 The functions return 0 on success and a nonzero error number on failure.
505 In addition to the errors returned by the nonreentrant
506 versions of these functions, if
508 is too small, the functions will return
510 and the call should be retried with a larger buffer.
513 is not modified, but the address of a variable in which to store error numbers
518 does not recognize components of a dotted IPv4 address string
519 that are expressed in hexadecimal.
520 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
523 .\" .BR getipnodebyaddr (3),
524 .\" .BR getipnodebyname (3),
531 .BR nsswitch.conf (5),