Debian Codegraph @

upstream/debian-unstable/man3/gethostbyname.3

raw
1 '\" t
2 .\" Copyright 1993, David Metcalfe <[email protected]>
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .\"
7 .TH gethostbyname 3 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
10 h_errno,
11 herror, hstrerror,
12 gethostbyaddr_r,
13 gethostbyname2, gethostbyname2_r, gethostbyname_r,
14 gethostent_r \- get network host entry
15 .SH LIBRARY
16 Standard C library
17 .RI ( libc ,\~ \-lc )
18 .SH SYNOPSIS
19 .nf
20 .B #include <netdb.h>
21 .P
22 .BI "void sethostent(int " stayopen );
23 .B void endhostent(void);
24 .P
25 .B [[deprecated]] extern int h_errno;
26 .P
27 .BI "[[deprecated]] struct hostent *gethostbyname(const char *" name );
28 .BR "[[deprecated]] struct hostent *gethostbyaddr(" "socklen_t size;"
29 .BI " const void " addr [ size ],
30 .BI " socklen_t " size ", int " type );
31 .P
32 .BI "[[deprecated]] void herror(const char *" s );
33 .BI "[[deprecated]] const char *hstrerror(int " err );
34 .P
35 /* System V/POSIX extension */
36 .B struct hostent *gethostent(void);
37 .P
38 /* GNU extensions */
39 .B [[deprecated]]
40 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
41 .P
42 .BR "int gethostent_r(" "size_t bufsize;"
43 .BI " struct hostent *restrict " ret ,
44 .BI " char " buf "[restrict " bufsize "], size_t " bufsize ,
45 .BI " struct hostent **restrict " result ,
46 .BI " int *restrict " h_errnop );
47 .P
48 .B [[deprecated]]
49 .BR "int gethostbyaddr_r(" "socklen_t size, size_t bufsize;"
50 .BI " const void " addr "[restrict " size "], socklen_t " size ,
51 .BI " int " type ,
52 .BI " struct hostent *restrict " ret ,
53 .BI " char " buf "[restrict " bufsize "], size_t " bufsize ,
54 .BI " struct hostent **restrict " result ,
55 .BI " int *restrict " h_errnop );
56 .B [[deprecated]]
57 .BR "int gethostbyname_r(" "size_t bufsize;"
58 .BI " const char *restrict " name ,
59 .BI " struct hostent *restrict " ret ,
60 .BI " char " buf "[restrict " bufsize "], size_t " bufsize ,
61 .BI " struct hostent **restrict " result ,
62 .BI " int *restrict " h_errnop );
63 .B [[deprecated]]
64 .BR "int gethostbyname2_r(" "size_t bufsize;"
65 .BI " const char *restrict " name ", int " af,
66 .BI " struct hostent *restrict " ret ,
67 .BI " char " buf "[restrict " bufsize "], size_t " bufsize ,
68 .BI " struct hostent **restrict " result ,
69 .BI " int *restrict " h_errnop );
70 .fi
71 .P
72 .RS -4
73 Feature Test Macro Requirements for glibc (see
74 .BR feature_test_macros (7)):
75 .RE
76 .P
77 .BR gethostbyname2 (),
78 .BR gethostent_r (),
79 .BR gethostbyaddr_r (),
80 .BR gethostbyname_r (),
81 .BR gethostbyname2_r ():
82 .nf
83 Since glibc 2.19:
84 _DEFAULT_SOURCE
85 glibc up to and including 2.19:
86 _BSD_SOURCE || _SVID_SOURCE
87 .fi
88 .P
89 .BR herror (),
90 .BR hstrerror ():
91 .nf
92 Since glibc 2.19:
93 _DEFAULT_SOURCE
94 glibc 2.8 to glibc 2.19:
95 _BSD_SOURCE || _SVID_SOURCE
96 Before glibc 2.8:
97 none
98 .fi
99 .P
100 .BR h_errno :
101 .nf
102 Since glibc 2.19
103 _DEFAULT_SOURCE || _POSIX_C_SOURCE < 200809L
104 glibc 2.12 to glibc 2.19:
105 _BSD_SOURCE || _SVID_SOURCE || _POSIX_C_SOURCE < 200809L
106 Before glibc 2.12:
107 none
108 .fi
109 .SH DESCRIPTION
110 The
111 .BR gethostbyname* (),
112 .BR gethostbyaddr* (),
113 .BR herror (),
114 and
115 .BR hstrerror ()
116 functions are obsolete.
117 Applications should use
118 .BR getaddrinfo (3),
119 .BR getnameinfo (3),
120 and
121 .BR gai_strerror (3)
122 instead.
123 .P
124 The
125 .BR sethostent ()
126 function specifies, if
127 .I stayopen
128 is true (1),
129 that a connected TCP socket should be used for the name server queries and
130 that the connection should remain open during successive queries.
131 Otherwise, name server queries will use UDP datagrams.
132 .P
133 The
134 .BR endhostent ()
135 function ends the use of a TCP connection for name
136 server queries.
137 .P
138 The
139 .BR gethostbyname ()
140 function returns a structure of type
141 .I hostent
142 for the given host
143 .IR name .
144 Here
145 .I name
146 is either a hostname or an IPv4 address in standard dot notation (as for
147 .BR inet_addr (3)).
148 If
149 .I name
150 is an IPv4 address, no lookup is performed and
151 .BR gethostbyname ()
152 simply copies
153 .I name
154 into the
155 .I h_name
156 field and its
157 .I struct in_addr
158 equivalent into the
159 .I h_addr_list[0]
160 field of the returned
161 .I hostent
162 structure.
163 If
164 .I name
165 doesn't end in a dot and the environment variable
166 .B HOSTALIASES
167 is set, the alias file pointed to by
168 .B HOSTALIASES
169 will first be searched for
170 .I name
171 (see
172 .BR hostname (7)
173 for the file format).
174 The current domain and its parents are searched unless
175 .I name
176 ends in a dot.
177 .P
178 The
179 .BR gethostbyaddr ()
180 function returns a structure of type
181 .I hostent
182 for the given host address
183 .I addr
184 of size
185 .I size
186 and address type
187 .IR type .
188 Valid address types are
189 .B AF_INET
190 and
191 .B AF_INET6
192 (defined in
193 .IR <sys/socket.h> ).
194 The host address argument is a pointer to a struct of a type depending
195 on the address type, for example a
196 .I struct\ in_addr\ *
197 (probably
198 obtained via a call to
199 .BR inet_addr (3))
200 for address type
201 .BR AF_INET .
202 .P
203 The (obsolete)
204 .BR herror ()
205 function prints the error message associated
206 with the current value of
207 .I h_errno
208 on
209 .IR stderr .
210 .P
211 The (obsolete)
212 .BR hstrerror ()
213 function takes an error number
214 (typically
215 .IR h_errno )
216 and returns the corresponding message string.
217 .P
218 The domain name queries carried out by
219 .BR gethostbyname ()
220 and
221 .BR gethostbyaddr ()
222 rely on the Name Service Switch
223 .RB ( nsswitch.conf (5))
224 configured sources or a local name server
225 .RB ( named (8)).
226 The default action is to query the Name Service Switch
227 .RB ( nsswitch.conf (5))
228 configured sources, failing that, a local name server
229 .RB ( named (8)).
230 .\"
231 .SS Historical
232 The
233 .BR nsswitch.conf (5)
234 file is the modern way of controlling the order of host lookups.
235 .P
236 In glibc 2.4 and earlier, the
237 .I order
238 keyword was used to control the order of host lookups as defined in
239 .I /etc/host.conf
240 .RB ( host.conf (5)).
241 .P
242 The
243 .I hostent
244 structure is defined in
245 .I <netdb.h>
246 as follows:
247 .P
248 .in +4n
249 .EX
250 struct hostent {
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; /* size of address */
255 char **h_addr_list; /* list of addresses */
256 }
257 #define h_addr h_addr_list[0] /* for backward compatibility */
258 .EE
259 .in
260 .P
261 The members of the
262 .I hostent
263 structure are:
264 .TP
265 .I h_name
266 The official name of the host.
267 .TP
268 .I h_aliases
269 An array of alternative names for the host, terminated by a null pointer.
270 .TP
271 .I h_addrtype
272 The type of address;
273 always
274 .B AF_INET
275 or
276 .B AF_INET6
277 at present.
278 .TP
279 .I h_length
280 The size of the address in bytes.
281 .TP
282 .I h_addr_list
283 An array of pointers to network addresses for the host (in network byte
284 order), terminated by a null pointer.
285 .TP
286 .I h_addr
287 The first address in
288 .I h_addr_list
289 for backward compatibility.
290 .SH RETURN VALUE
291 The
292 .BR gethostbyname ()
293 and
294 .BR gethostbyaddr ()
295 functions return the
296 .I hostent
297 structure or a null pointer if an error occurs.
298 On error, the
299 .I h_errno
300 variable holds an error number.
301 When non-NULL, the return value may point at static data, see the notes below.
302 .SH ERRORS
303 The variable
304 .I h_errno
305 can have the following values:
306 .TP
307 .B HOST_NOT_FOUND
308 The specified host is unknown.
309 .TP
310 .B NO_DATA
311 The requested name is valid but does not have an IP address.
312 Another type of request to the name server for this domain
313 may return an answer.
314 The constant
315 .B NO_ADDRESS
316 is a synonym for
317 .BR NO_DATA .
318 .TP
319 .B NO_RECOVERY
320 A nonrecoverable name server error occurred.
321 .TP
322 .B TRY_AGAIN
323 A temporary error occurred on an authoritative name server.
324 Try again later.
325 .SH FILES
326 .TP
327 .I /etc/host.conf
328 resolver configuration file
329 .TP
330 .I /etc/hosts
331 host database file
332 .TP
333 .I /etc/nsswitch.conf
334 name service switch configuration
335 .SH ATTRIBUTES
336 For an explanation of the terms used in this section, see
337 .BR attributes (7).
338 .TS
339 allbox;
340 lb lb lbx
341 l l l.
342 Interface Attribute Value
343 T{
344 .na
345 .nh
346 .BR gethostbyname ()
347 T} Thread safety T{
348 .na
349 .nh
350 MT-Unsafe race:hostbyname env
351 locale
352 T}
353 T{
354 .na
355 .nh
356 .BR gethostbyaddr ()
357 T} Thread safety T{
358 .na
359 .nh
360 MT-Unsafe race:hostbyaddr env
361 locale
362 T}
363 T{
364 .na
365 .nh
366 .BR sethostent (),
367 .BR endhostent (),
368 .BR gethostent_r ()
369 T} Thread safety T{
370 .na
371 .nh
372 MT-Unsafe race:hostent env
373 locale
374 T}
375 T{
376 .na
377 .nh
378 .BR herror (),
379 .BR hstrerror ()
380 T} Thread safety MT-Safe
381 T{
382 .na
383 .nh
384 .BR gethostent ()
385 T} Thread safety T{
386 .na
387 .nh
388 MT-Unsafe race:hostent
389 race:hostentbuf env locale
390 T}
391 T{
392 .na
393 .nh
394 .BR gethostbyname2 ()
395 T} Thread safety T{
396 .na
397 .nh
398 MT-Unsafe race:hostbyname2
399 env locale
400 T}
401 T{
402 .na
403 .nh
404 .BR gethostbyaddr_r (),
405 .BR gethostbyname_r (),
406 .BR gethostbyname2_r ()
407 T} Thread safety MT-Safe env locale
408 .TE
409 .P
410 In the above table,
411 .I hostent
412 in
413 .I race:hostent
414 signifies that if any of the functions
415 .BR sethostent (),
416 .BR gethostent (),
417 .BR gethostent_r (),
418 or
419 .BR \%endhostent ()
420 are used in parallel in different threads of a program,
421 then data races could occur.
422 .SH STANDARDS
423 .TP
424 .BR sethostent ()
425 .TQ
426 .BR endhostent ()
427 .TQ
428 .BR gethostent ()
429 POSIX.1-2008.
430 .TP
431 .BR gethostent_r ()
432 GNU.
433 .TP
434 Others:
435 None.
436 .SH HISTORY
437 .TP
438 .BR sethostent ()
439 .TQ
440 .BR endhostent ()
441 .TQ
442 .BR gethostent ()
443 POSIX.1-2001.
444 .TP
445 .BR gethostbyname ()
446 .TQ
447 .BR gethostbyaddr ()
448 .TQ
449 .I h_errno
450 Marked obsolescent in POSIX.1-2001.
451 Removed in POSIX.1-2008,
452 recommending the use of
453 .BR getaddrinfo (3)
454 and
455 .BR getnameinfo (3)
456 instead.
457 .SH NOTES
458 The functions
459 .BR gethostbyname ()
460 and
461 .BR gethostbyaddr ()
462 may return pointers to static data, which may be overwritten by
463 later calls.
464 Copying the
465 .I struct hostent
466 does not suffice,
467 since it contains pointers;
468 a deep copy is required.
469 .P
470 In the original BSD implementation the
471 .I size
472 argument
473 of
474 .BR gethostbyname ()
475 was an
476 .IR int .
477 The SUSv2 standard is buggy and declares the
478 .I size
479 argument of
480 .BR gethostbyaddr ()
481 to be of type
482 .IR size_t .
483 (That is wrong, because it has to be
484 .IR int ,
485 and
486 .I size_t
487 is not.
488 POSIX.1-2001 makes it
489 .IR socklen_t ,
490 which is OK.)
491 See also
492 .BR accept (2).
493 .P
494 The BSD prototype for
495 .BR gethostbyaddr ()
496 uses
497 .I "const char\ *"
498 for the first argument.
499 .SS System V/POSIX extension
500 POSIX requires the
501 .BR gethostent ()
502 call, which should return the next entry in the host data base.
503 When using DNS/BIND this does not make much sense, but it may
504 be reasonable if the host data base is a file that can be read
505 line by line.
506 On many systems, a routine of this name reads
507 from the file
508 .IR /etc/hosts .
509 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
510 It may be available only when the library was built without DNS support.
511 .\" e.g., FreeBSD, AIX
512 The glibc version will ignore ipv6 entries.
513 This function is not reentrant,
514 and glibc adds a reentrant version
515 .BR gethostent_r ().
516 .SS GNU extensions
517 glibc2 also has a
518 .BR gethostbyname2 ()
519 that works like
520 .BR gethostbyname (),
521 but permits to specify the address family to which the address must belong.
522 .P
523 glibc2 also has reentrant versions
524 .BR gethostent_r (),
525 .BR gethostbyaddr_r (),
526 .BR gethostbyname_r (),
527 and
528 .BR gethostbyname2_r ().
529 The caller supplies a
530 .I hostent
531 structure
532 .I ret
533 which will be filled in on success, and a temporary work buffer
534 .I buf
535 of size
536 .IR bufsize .
537 After the call,
538 .I result
539 will point to the result on success.
540 In case of an error
541 or if no entry is found
542 .I result
543 will be NULL.
544 The functions return 0 on success and a nonzero error number on failure.
545 In addition to the errors returned by the nonreentrant
546 versions of these functions, if
547 .I buf
548 is too small, the functions will return
549 .BR ERANGE ,
550 and the call should be retried with a larger buffer.
551 The global variable
552 .I h_errno
553 is not modified, but the address of a variable in which to store error numbers
554 is passed in
555 .IR h_errnop .
556 .SH BUGS
557 .BR gethostbyname ()
558 does not recognize components of a dotted IPv4 address string
559 that are expressed in hexadecimal.
560 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
561 .SH SEE ALSO
562 .BR getaddrinfo (3),
563 .\" .BR getipnodebyaddr (3),
564 .\" .BR getipnodebyname (3),
565 .BR getnameinfo (3),
566 .BR inet (3),
567 .BR inet_ntop (3),
568 .BR inet_pton (3),
569 .BR resolver (3),
570 .BR hosts (5),
571 .BR nsswitch.conf (5),
572 .BR hostname (7),
573 .BR named (8)
574 .\" .BR resolv+ (8)