2 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\" References consulted:
7 .\" Linux libc source code
8 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
19 .TH gethostbyname 3 (date) "Linux man-pages (unreleased)"
21 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
25 gethostbyname2, gethostbyname2_r, gethostbyname_r,
26 gethostent_r \- get network host entry
29 .RI ( libc ", " \-lc )
34 .BI "void sethostent(int " stayopen );
35 .B void endhostent(void);
37 .B [[deprecated]] extern int h_errno;
39 .BI "[[deprecated]] struct hostent *gethostbyname(const char *" name );
40 .BI "[[deprecated]] struct hostent *gethostbyaddr(const void " addr [. len ],
41 .BI " socklen_t " len ", int " type );
43 .BI "[[deprecated]] void herror(const char *" s );
44 .BI "[[deprecated]] const char *hstrerror(int " err );
46 /* System V/POSIX extension */
47 .B struct hostent *gethostent(void);
51 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
53 .BI "int gethostent_r(struct hostent *restrict " ret ,
54 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
55 .BI " struct hostent **restrict " result ,
56 .BI " int *restrict " h_errnop );
59 .BI "int gethostbyaddr_r(const void " addr "[restrict ." len "], socklen_t " len ,
61 .BI " struct hostent *restrict " ret ,
62 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
63 .BI " struct hostent **restrict " result ,
64 .BI " int *restrict " h_errnop );
66 .BI "int gethostbyname_r(const char *restrict " name ,
67 .BI " struct hostent *restrict " ret ,
68 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
69 .BI " struct hostent **restrict " result ,
70 .BI " int *restrict " h_errnop );
72 .BI "int gethostbyname2_r(const char *restrict " name ", int " af,
73 .BI " struct hostent *restrict " ret ,
74 .BI " char " buf "[restrict ." buflen "], size_t " buflen ,
75 .BI " struct hostent **restrict " result ,
76 .BI " int *restrict " h_errnop );
80 Feature Test Macro Requirements for glibc (see
81 .BR feature_test_macros (7)):
84 .BR gethostbyname2 (),
86 .BR gethostbyaddr_r (),
87 .BR gethostbyname_r (),
88 .BR gethostbyname2_r ():
92 glibc up to and including 2.19:
93 _BSD_SOURCE || _SVID_SOURCE
101 glibc 2.8 to glibc 2.19:
102 _BSD_SOURCE || _SVID_SOURCE
110 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
111 glibc 2.12 to glibc 2.19:
112 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
118 .BR gethostbyname* (),
119 .BR gethostbyaddr* (),
123 functions are obsolete.
124 Applications should use
133 function specifies, if \fIstayopen\fP is true (1),
134 that a connected TCP socket should be used for the name server queries and
135 that the connection should remain open during successive queries.
136 Otherwise, name server queries will use UDP datagrams.
140 function ends the use of a TCP connection for name
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 prints the error message associated
203 with the current value of \fIh_errno\fP on \fIstderr\fP.
207 function takes an error number
208 (typically \fIh_errno\fP) and returns the corresponding message string.
210 The domain name queries carried out by
214 rely on the Name Service Switch
215 .RB ( nsswitch.conf (5))
216 configured sources or a local name server
218 The default action is to query the Name Service Switch
219 .RB ( nsswitch.conf (5))
220 configured sources, failing that, a local name server
225 .BR nsswitch.conf (5)
226 file is the modern way of controlling the order of host lookups.
228 In glibc 2.4 and earlier, the
230 keyword was used to control the order of host lookups as defined in
232 .RB ( host.conf (5)).
234 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
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 */
245 #define h_addr h_addr_list[0] /* for backward compatibility */
249 The members of the \fIhostent\fP structure are:
252 The official name of the host.
255 An array of alternative names for the host, terminated by a null pointer.
258 The type of address; always
265 The length of the address in bytes.
268 An array of pointers to network addresses for the host (in network byte
269 order), terminated by a null pointer.
272 The first address in \fIh_addr_list\fP for backward compatibility.
280 structure or a null pointer if an error occurs.
283 variable holds an error number.
284 When non-NULL, the return value may point at static data, see the notes below.
286 The variable \fIh_errno\fP can have the following values:
289 The specified host is unknown.
292 The requested name is valid but does not have an IP address.
293 Another type of request to the name server for this domain
294 may return an answer.
301 A nonrecoverable name server error occurred.
304 A temporary error occurred on an authoritative name server.
309 resolver configuration file
314 .I /etc/nsswitch.conf
315 name service switch configuration
317 For an explanation of the terms used in this section, see
323 Interface Attribute Value
331 MT-Unsafe race:hostbyname env
341 MT-Unsafe race:hostbyaddr env
353 MT-Unsafe race:hostent env
361 T} Thread safety MT-Safe
369 MT-Unsafe race:hostent
370 race:hostentbuf env locale
375 .BR gethostbyname2 ()
379 MT-Unsafe race:hostbyname2
385 .BR gethostbyaddr_r (),
386 .BR gethostbyname_r (),
387 .BR gethostbyname2_r ()
388 T} Thread safety MT-Safe env locale
395 signifies that if any of the functions
401 are used in parallel in different threads of a program,
402 then data races could occur.
431 Marked obsolescent in POSIX.1-2001.
432 Removed in POSIX.1-2008,
433 recommending the use of
443 may return pointers to static data, which may be overwritten by
447 does not suffice, since it contains pointers; a deep copy is required.
449 In the original BSD implementation the
456 The SUSv2 standard is buggy and declares the
462 (That is wrong, because it has to be
467 POSIX.1-2001 makes it
473 The BSD prototype for
477 for the first argument.
478 .SS System V/POSIX extension
481 call, which should return the next entry in the host data base.
482 When using DNS/BIND this does not make much sense, but it may
483 be reasonable if the host data base is a file that can be read
485 On many systems, a routine of this name reads
488 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
489 It may be available only when the library was built without DNS support.
490 .\" e.g., FreeBSD, AIX
491 The glibc version will ignore ipv6 entries.
492 This function is not reentrant,
493 and glibc adds a reentrant version
497 .BR gethostbyname2 ()
499 .BR gethostbyname (),
500 but permits to specify the address family to which the address must belong.
502 glibc2 also has reentrant versions
504 .BR gethostbyaddr_r (),
505 .BR gethostbyname_r (),
507 .BR gethostbyname2_r ().
508 The caller supplies a
512 which will be filled in on success, and a temporary work buffer
518 will point to the result on success.
520 or if no entry is found
523 The functions return 0 on success and a nonzero error number on failure.
524 In addition to the errors returned by the nonreentrant
525 versions of these functions, if
527 is too small, the functions will return
529 and the call should be retried with a larger buffer.
532 is not modified, but the address of a variable in which to store error numbers
537 does not recognize components of a dotted IPv4 address string
538 that are expressed in hexadecimal.
539 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
542 .\" .BR getipnodebyaddr (3),
543 .\" .BR getipnodebyname (3),
550 .BR nsswitch.conf (5),