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 2017-09-15 "" "Linux Programmer's Manual"
40 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
44 gethostbyname2, gethostbyname2_r, gethostbyname_r,
45 gethostent_r \- get network host entry
49 .B extern int h_errno;
51 .BI "struct hostent *gethostbyname(const char *" name );
53 .BR "#include <sys/socket.h>" " /* for AF_INET */"
54 .BI "struct hostent *gethostbyaddr(const void *" addr ,
55 .BI " socklen_t " len ", int " type );
57 .BI "void sethostent(int " stayopen );
59 .B void endhostent(void);
61 .BI "void herror(const char *" s );
63 .BI "const char *hstrerror(int " err );
65 /* System V/POSIX extension */
66 .B struct hostent *gethostent(void);
69 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
71 .B "int gethostent_r("
72 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
73 .BI " struct hostent **" result ", int *" h_errnop );
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 );
79 .BI "int gethostbyname_r(const char *" name ,
80 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
81 .BI " struct hostent **" result ", int *" h_errnop );
83 .BI "int gethostbyname2_r(const char *" name ", int " af,
84 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
85 .BI " struct hostent **" result ", int *" h_errnop );
89 Feature Test Macro Requirements for glibc (see
90 .BR feature_test_macros (7)):
95 .BR gethostbyname2 (),
97 .BR gethostbyaddr_r (),
98 .BR gethostbyname_r (),
99 .BR gethostbyname2_r ():
105 Glibc versions up to and including 2.19:
106 _BSD_SOURCE || _SVID_SOURCE
119 _BSD_SOURCE || _SVID_SOURCE
131 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
134 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
143 .BR gethostbyname* (),
144 .BR gethostbyaddr* (),
148 functions are obsolete.
149 Applications should use
158 function returns a structure of type
164 is either a hostname or an IPv4 address in standard dot notation (as for
168 is an IPv4 address, no lookup is performed and
178 field of the returned
183 doesn't end in a dot and the environment variable
185 is set, the alias file pointed to by
187 will first be searched for
191 for the file format).
192 The current domain and its parents are searched unless \fIname\fP
197 function returns a structure of type \fIhostent\fP
198 for the given host address \fIaddr\fP of length \fIlen\fP and address type
200 Valid address types are
204 The host address argument is a pointer to a struct of a type depending
205 on the address type, for example a \fIstruct in_addr *\fP (probably
206 obtained via a call to
213 function specifies, if \fIstayopen\fP is true (1),
214 that a connected TCP socket should be used for the name server queries and
215 that the connection should remain open during successive queries.
216 Otherwise, name server queries will use UDP datagrams.
220 function ends the use of a TCP connection for name
225 function prints the error message associated
226 with the current value of \fIh_errno\fP on \fIstderr\fP.
230 function takes an error number
231 (typically \fIh_errno\fP) and returns the corresponding message string.
233 The domain name queries carried out by
237 rely on the Name Service Switch
238 .RB ( nsswitch.conf (5))
239 configured sources or a local name server
241 The default action is to query the Name Service Switch
242 .RB ( nsswitch.conf(5))
243 configured sources, failing that, a local name server
248 .BR nsswitch.conf (5)
249 file is the modern way of controlling the order of host lookups.
251 In glibc 2.4 and earlier, the
253 keyword was used to control the order of host lookups as defined in
255 .RB ( host.conf (5)).
258 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
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 */
269 #define h_addr h_addr_list[0] /* for backward compatibility */
273 The members of the \fIhostent\fP structure are:
276 The official name of the host.
279 An array of alternative names for the host, terminated by a null pointer.
282 The type of address; always
289 The length of the address in bytes.
292 An array of pointers to network addresses for the host (in network byte
293 order), terminated by a null pointer.
296 The first address in \fIh_addr_list\fP for backward compatibility.
304 structure or a null pointer if an error occurs.
307 variable holds an error number.
308 When non-NULL, the return value may point at static data, see the notes below.
310 The variable \fIh_errno\fP can have the following values:
313 The specified host is unknown.
316 The requested name is valid but does not have an IP address.
317 Another type of request to the name server for this domain
318 may return an answer.
325 A nonrecoverable name server error occurred.
328 A temporary error occurred on an authoritative name server.
333 resolver configuration file
338 .I /etc/nsswitch.conf
339 name service switch configuration
341 For an explanation of the terms used in this section, see
347 Interface Attribute Value
351 MT-Unsafe race:hostbyname env
358 MT-Unsafe race:hostbyaddr env
369 MT-Unsafe race:hostent env
377 T} Thread safety MT-Safe
381 MT-Unsafe race:hostent
383 race:hostentbuf env locale
386 .BR gethostbyname2 ()
388 MT-Unsafe race:hostbyname2
393 .BR gethostbyaddr_r (),
394 .BR gethostbyname_r (),
395 .BR gethostbyname2_r ()
396 T} Thread safety MT-Safe env locale
403 signifies that if any of the functions
409 are used in parallel in different threads of a program,
410 then data races could occur.
412 POSIX.1-2001 specifies
413 .BR gethostbyname (),
414 .BR gethostbyaddr (),
420 .BR gethostbyname (),
421 .BR gethostbyaddr (),
424 are marked obsolescent in that standard.
425 POSIX.1-2008 removes the specifications of
426 .BR gethostbyname (),
427 .BR gethostbyaddr (),
430 recommending the use of
440 may return pointers to static data, which may be overwritten by
444 does not suffice, since it contains pointers; a deep copy is required.
446 In the original BSD implementation the
453 The SUSv2 standard is buggy and declares the
459 (That is wrong, because it has to be
464 POSIX.1-2001 makes it
470 The BSD prototype for
474 for the first argument.
475 .SS System V/POSIX extension
478 call, which should return the next entry in the host data base.
479 When using DNS/BIND this does not make much sense, but it may
480 be reasonable if the host data base is a file that can be read
482 On many systems, a routine of this name reads
485 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
486 It may be available only when the library was built without DNS support.
487 .\" e.g., FreeBSD, AIX
488 The glibc version will ignore ipv6 entries.
489 This function is not reentrant,
490 and glibc adds a reentrant version
494 .BR gethostbyname2 ()
496 .BR gethostbyname (),
497 but permits to specify the address family to which the address must belong.
499 Glibc2 also has reentrant versions
501 .BR gethostbyaddr_r (),
502 .BR gethostbyname_r ()
504 .BR gethostbyname2_r ().
505 The caller supplies a
509 which will be filled in on success, and a temporary work buffer
515 will point to the result on success.
517 or if no entry is found
520 The functions return 0 on success and a nonzero error number on failure.
521 In addition to the errors returned by the nonreentrant
522 versions of these functions, if
524 is too small, the functions will return
526 and the call should be retried with a larger buffer.
529 is not modified, but the address of a variable in which to store error numbers
534 does not recognize components of a dotted IPv4 address string
535 that are expressed in hexadecimal.
536 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
539 .\" .BR getipnodebyaddr (3),
540 .\" .BR getipnodebyname (3),
547 .BR nsswitch.conf (5),