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 2015-07-23 "" "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 */
67 .B struct hostent *gethostent(void);
71 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
73 .B "int gethostent_r("
74 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
75 .BI " struct hostent **" result ", int *" h_errnop );
77 .BI "int gethostbyaddr_r(const void *" addr ", socklen_t " len ", int " type ,
78 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
79 .BI " struct hostent **" result ", int *" h_errnop );
81 .BI "int gethostbyname_r(const char *" name ,
82 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
83 .BI " struct hostent **" result ", int *" h_errnop );
85 .BI "int gethostbyname2_r(const char *" name ", int " af,
86 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
87 .BI " struct hostent **" result ", int *" h_errnop );
91 Feature Test Macro Requirements for glibc (see
92 .BR feature_test_macros (7)):
97 .BR gethostbyname2 (),
99 .BR gethostbyaddr_r (),
100 .BR gethostbyname_r (),
101 .BR gethostbyname2_r ():
103 _BSD_SOURCE || _SVID_SOURCE
111 _BSD_SOURCE || _SVID_SOURCE
121 _BSD_SOURCE || _SVID_SOURCE ||
122 (_POSIX_C_SOURCE < 200809L && _XOPEN_SOURCE < 700)
131 .BR gethostbyname* (),
132 .BR gethostbyaddr* (),
136 functions are obsolete.
137 Applications should use
146 function returns a structure of type
152 is either a hostname or an IPv4 address in standard dot notation (as for
156 is an IPv4 address, no lookup is performed and
166 field of the returned
171 doesn't end in a dot and the environment variable
173 is set, the alias file pointed to by
175 will first be searched for
179 for the file format).
180 The current domain and its parents are searched unless \fIname\fP
185 function returns a structure of type \fIhostent\fP
186 for the given host address \fIaddr\fP of length \fIlen\fP and address type
188 Valid address types are
192 The host address argument is a pointer to a struct of a type depending
193 on the address type, for example a \fIstruct in_addr *\fP (probably
194 obtained via a call to
201 function specifies, if \fIstayopen\fP is true (1),
202 that a connected TCP socket should be used for the name server queries and
203 that the connection should remain open during successive queries.
204 Otherwise, name server queries will use UDP datagrams.
208 function ends the use of a TCP connection for name
213 function prints the error message associated
214 with the current value of \fIh_errno\fP on \fIstderr\fP.
218 function takes an error number
219 (typically \fIh_errno\fP) and returns the corresponding message string.
221 The domain name queries carried out by
225 rely on the Name Service Switch
226 .RB ( nsswitch.conf (5))
227 configured sources or a local name server
229 The default action is to query the Name Service Switch
230 .RB ( nsswitch.conf(5))
231 configured sources, failing that, a local name server
236 .BR nsswitch.conf (5)
237 file is the modern way of controlling the order of host lookups.
239 In glibc 2.4 and earlier, the
241 keyword was used to control the order of host lookups as defined in
243 .RB ( host.conf (5)).
246 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
252 char *h_name; /* official name of host */
253 char **h_aliases; /* alias list */
254 int h_addrtype; /* host address type */
255 int h_length; /* length of address */
256 char **h_addr_list; /* list of addresses */
258 #define h_addr h_addr_list[0] /* for backward compatibility */
262 The members of the \fIhostent\fP structure are:
265 The official name of the host.
268 An array of alternative names for the host, terminated by a null pointer.
271 The type of address; always
278 The length of the address in bytes.
281 An array of pointers to network addresses for the host (in network byte
282 order), terminated by a null pointer.
285 The first address in \fIh_addr_list\fP for backward compatibility.
293 structure or a null pointer if an error occurs.
296 variable holds an error number.
297 When non-NULL, the return value may point at static data, see the notes below.
299 The variable \fIh_errno\fP can have the following values:
302 The specified host is unknown.
305 The requested name is valid but does not have an IP address.
306 Another type of request to the name server for this domain
307 may return an answer.
314 A nonrecoverable name server error occurred.
317 A temporary error occurred on an authoritative name server.
322 resolver configuration file
327 .I /etc/nsswitch.conf
328 name service switch configuration
330 For an explanation of the terms used in this section, see
336 Interface Attribute Value
340 MT-Unsafe race:hostbyname env
347 MT-Unsafe race:hostbyaddr env
358 MT-Unsafe race:hostent env
366 T} Thread safety MT-Safe
370 MT-Unsafe race:hostent
372 race:hostentbuf env locale
375 .BR gethostbyname2 ()
377 MT-Unsafe race:hostbyname2
382 .BR gethostbyaddr_r (),
383 .BR gethostbyname_r (),
384 .BR gethostbyname2_r ()
385 T} Thread safety MT-Safe env locale
392 signifies that if any of the functions
395 .BR gethostent_r (3),
398 are used in parallel in different threads of a program,
399 then data races could occur.
401 POSIX.1-2001 specifies
402 .BR gethostbyname (),
403 .BR gethostbyaddr (),
409 .BR gethostbyname (),
410 .BR gethostbyaddr (),
413 are marked obsolescent in that standard.
414 POSIX.1-2008 removes the specifications of
415 .BR gethostbyname (),
416 .BR gethostbyaddr (),
419 recommending the use of
429 may return pointers to static data, which may be overwritten by
433 does not suffice, since it contains pointers; a deep copy is required.
435 In the original BSD implementation the
442 The SUSv2 standard is buggy and declares the
448 (That is wrong, because it has to be
453 POSIX.1-2001 makes it
459 The BSD prototype for
463 for the first argument.
464 .SS System V/POSIX extension
467 call, which should return the next entry in the host data base.
468 When using DNS/BIND this does not make much sense, but it may
469 be reasonable if the host data base is a file that can be read
471 On many systems, a routine of this name reads
474 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
475 It may be available only when the library was built without DNS support.
476 .\" e.g., FreeBSD, AIX
477 The glibc version will ignore ipv6 entries.
478 This function is not reentrant,
479 and glibc adds a reentrant version
483 .BR gethostbyname2 ()
485 .BR gethostbyname (),
486 but permits to specify the address family to which the address must belong.
488 Glibc2 also has reentrant versions
490 .BR gethostbyaddr_r (),
491 .BR gethostbyname_r ()
493 .BR gethostbyname2_r ().
494 The caller supplies a
498 which will be filled in on success, and a temporary work buffer
504 will point to the result on success.
506 or if no entry is found
509 The functions return 0 on success and a nonzero error number on failure.
510 In addition to the errors returned by the nonreentrant
511 versions of these functions, if
513 is too small, the functions will return
515 and the call should be retried with a larger buffer.
518 is not modified, but the address of a variable in which to store error numbers
523 does not recognize components of a dotted IPv4 address string
524 that are expressed in hexadecimal.
525 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
528 .\" .BR getipnodebyaddr (3),
529 .\" .BR getipnodebyname (3),
536 .BR nsswitch.conf (5),