1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .\" References consulted:
6 .\" Linux libc source code
7 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
9 .\" Modified 1993-05-22, David Metcalfe
10 .\" Modified 1993-07-25, Rik Faith (faith@cs.unc.edu)
11 .\" Modified 1997-02-16, Andries Brouwer (aeb@cwi.nl)
12 .\" Modified 1998-12-21, Andries Brouwer (aeb@cwi.nl)
13 .\" Modified 2000-08-12, Andries Brouwer (aeb@cwi.nl)
14 .\" Modified 2001-05-19, Andries Brouwer (aeb@cwi.nl)
15 .\" Modified 2002-08-05, Michael Kerrisk
16 .\" Modified 2004-10-31, Andries Brouwer
18 .TH gethostbyname 3 (date) "Linux man-pages (unreleased)"
20 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
24 gethostbyname2, gethostbyname2_r, gethostbyname_r,
25 gethostent_r \- get network host entry
28 .RI ( libc ", " \-lc )
33 .BI "void sethostent(int " stayopen );
34 .B void endhostent(void);
36 .B [[deprecated]] extern int h_errno;
38 .BI "[[deprecated]] struct hostent *gethostbyname(const char *" name );
39 .BI "[[deprecated]] struct hostent *gethostbyaddr(const void *" addr ,
40 .BI " socklen_t " len ", int " type );
42 .BI "[[deprecated]] void herror(const char *" s );
43 .BI "[[deprecated]] const char *hstrerror(int " err );
45 /* System V/POSIX extension */
46 .B struct hostent *gethostent(void);
50 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
52 .BI "int gethostent_r(struct hostent *restrict " ret ,
53 .BI " char *restrict " buf ", size_t " buflen ,
54 .BI " struct hostent **restrict " result ,
55 .BI " int *restrict " h_errnop );
58 .BI "int gethostbyaddr_r(const void *restrict " addr ", socklen_t " len \
60 .BI " struct hostent *restrict " ret ,
61 .BI " char *restrict " buf ", size_t " buflen ,
62 .BI " struct hostent **restrict " result ,
63 .BI " int *restrict " h_errnop );
65 .BI "int gethostbyname_r(const char *restrict " name ,
66 .BI " struct hostent *restrict " ret ,
67 .BI " char *restrict " buf ", size_t " buflen ,
68 .BI " struct hostent **restrict " result ,
69 .BI " int *restrict " h_errnop );
71 .BI "int gethostbyname2_r(const char *restrict " name ", int " af,
72 .BI " struct hostent *restrict " ret ,
73 .BI " char *restrict " buf ", size_t " buflen ,
74 .BI " struct hostent **restrict " result ,
75 .BI " int *restrict " h_errnop );
79 Feature Test Macro Requirements for glibc (see
80 .BR feature_test_macros (7)):
83 .BR gethostbyname2 (),
85 .BR gethostbyaddr_r (),
86 .BR gethostbyname_r (),
87 .BR gethostbyname2_r ():
91 Glibc up to and including 2.19:
92 _BSD_SOURCE || _SVID_SOURCE
101 _BSD_SOURCE || _SVID_SOURCE
109 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
111 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
117 .BR gethostbyname* (),
118 .BR gethostbyaddr* (),
122 functions are obsolete.
123 Applications should use
132 function specifies, if \fIstayopen\fP is true (1),
133 that a connected TCP socket should be used for the name server queries and
134 that the connection should remain open during successive queries.
135 Otherwise, name server queries will use UDP datagrams.
139 function ends the use of a TCP connection for name
144 function returns a structure of type
150 is either a hostname or an IPv4 address in standard dot notation (as for
154 is an IPv4 address, no lookup is performed and
164 field of the returned
169 doesn't end in a dot and the environment variable
171 is set, the alias file pointed to by
173 will first be searched for
177 for the file format).
178 The current domain and its parents are searched unless \fIname\fP
183 function returns a structure of type \fIhostent\fP
184 for the given host address \fIaddr\fP of length \fIlen\fP and address type
186 Valid address types are
191 .IR <sys/socket.h> ).
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 prints the error message associated
202 with the current value of \fIh_errno\fP on \fIstderr\fP.
206 function takes an error number
207 (typically \fIh_errno\fP) and returns the corresponding message string.
209 The domain name queries carried out by
213 rely on the Name Service Switch
214 .RB ( nsswitch.conf (5))
215 configured sources or a local name server
217 The default action is to query the Name Service Switch
218 .RB ( nsswitch.conf (5))
219 configured sources, failing that, a local name server
224 .BR nsswitch.conf (5)
225 file is the modern way of controlling the order of host lookups.
227 In glibc 2.4 and earlier, the
229 keyword was used to control the order of host lookups as defined in
231 .RB ( host.conf (5)).
233 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
238 char *h_name; /* official name of host */
239 char **h_aliases; /* alias list */
240 int h_addrtype; /* host address type */
241 int h_length; /* length of address */
242 char **h_addr_list; /* list of addresses */
244 #define h_addr h_addr_list[0] /* for backward compatibility */
248 The members of the \fIhostent\fP structure are:
251 The official name of the host.
254 An array of alternative names for the host, terminated by a null pointer.
257 The type of address; always
264 The length of the address in bytes.
267 An array of pointers to network addresses for the host (in network byte
268 order), terminated by a null pointer.
271 The first address in \fIh_addr_list\fP for backward compatibility.
279 structure or a null pointer if an error occurs.
282 variable holds an error number.
283 When non-NULL, the return value may point at static data, see the notes below.
285 The variable \fIh_errno\fP can have the following values:
288 The specified host is unknown.
291 The requested name is valid but does not have an IP address.
292 Another type of request to the name server for this domain
293 may return an answer.
300 A nonrecoverable name server error occurred.
303 A temporary error occurred on an authoritative name server.
308 resolver configuration file
313 .I /etc/nsswitch.conf
314 name service switch configuration
316 For an explanation of the terms used in this section, see
324 Interface Attribute Value
328 MT-Unsafe race:hostbyname env
334 MT-Unsafe race:hostbyaddr env
342 MT-Unsafe race:hostent env
348 T} Thread safety MT-Safe
352 MT-Unsafe race:hostent
353 race:hostentbuf env locale
356 .BR gethostbyname2 ()
358 MT-Unsafe race:hostbyname2
362 .BR gethostbyaddr_r (),
363 .BR gethostbyname_r (),
364 .BR gethostbyname2_r ()
365 T} Thread safety MT-Safe env locale
374 signifies that if any of the functions
380 are used in parallel in different threads of a program,
381 then data races could occur.
383 POSIX.1-2001 specifies
384 .BR gethostbyname (),
385 .BR gethostbyaddr (),
391 .BR gethostbyname (),
392 .BR gethostbyaddr (),
395 are marked obsolescent in that standard.
396 POSIX.1-2008 removes the specifications of
397 .BR gethostbyname (),
398 .BR gethostbyaddr (),
401 recommending the use of
411 may return pointers to static data, which may be overwritten by
415 does not suffice, since it contains pointers; a deep copy is required.
417 In the original BSD implementation the
424 The SUSv2 standard is buggy and declares the
430 (That is wrong, because it has to be
435 POSIX.1-2001 makes it
441 The BSD prototype for
445 for the first argument.
446 .SS System V/POSIX extension
449 call, which should return the next entry in the host data base.
450 When using DNS/BIND this does not make much sense, but it may
451 be reasonable if the host data base is a file that can be read
453 On many systems, a routine of this name reads
456 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
457 It may be available only when the library was built without DNS support.
458 .\" e.g., FreeBSD, AIX
459 The glibc version will ignore ipv6 entries.
460 This function is not reentrant,
461 and glibc adds a reentrant version
465 .BR gethostbyname2 ()
467 .BR gethostbyname (),
468 but permits to specify the address family to which the address must belong.
470 Glibc2 also has reentrant versions
472 .BR gethostbyaddr_r (),
473 .BR gethostbyname_r (),
475 .BR gethostbyname2_r ().
476 The caller supplies a
480 which will be filled in on success, and a temporary work buffer
486 will point to the result on success.
488 or if no entry is found
491 The functions return 0 on success and a nonzero error number on failure.
492 In addition to the errors returned by the nonreentrant
493 versions of these functions, if
495 is too small, the functions will return
497 and the call should be retried with a larger buffer.
500 is not modified, but the address of a variable in which to store error numbers
505 does not recognize components of a dotted IPv4 address string
506 that are expressed in hexadecimal.
507 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
510 .\" .BR getipnodebyaddr (3),
511 .\" .BR getipnodebyname (3),
518 .BR nsswitch.conf (5),