]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/gethostbyname.3
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man3 / gethostbyname.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\" Modified 1993-05-22, David Metcalfe
28 .\" Modified 1993-07-25, Rik Faith (faith@cs.unc.edu)
29 .\" Modified 1997-02-16, Andries Brouwer (aeb@cwi.nl)
30 .\" Modified 1998-12-21, Andries Brouwer (aeb@cwi.nl)
31 .\" Modified 2000-08-12, Andries Brouwer (aeb@cwi.nl)
32 .\" Modified 2001-05-19, Andries Brouwer (aeb@cwi.nl)
33 .\" Modified 2002-08-05, Michael Kerrisk
34 .\" Modified 2004-10-31, Andries Brouwer
35 .\"
36 .TH GETHOSTBYNAME 3 2010-10-04 "" "Linux Programmer's Manual"
37 .SH NAME
38 gethostbyname, gethostbyaddr, sethostent, gethostent, endhostent,
39 h_errno,
40 herror, hstrerror,
41 gethostbyaddr_r,
42 gethostbyname2, gethostbyname2_r, gethostbyname_r,
43 gethostent_r \- get network host entry
44 .SH SYNOPSIS
45 .nf
46 .B #include <netdb.h>
47 .B extern int h_errno;
48 .sp
49 .BI "struct hostent *gethostbyname(const char *" name );
50 .sp
51 .BR "#include <sys/socket.h>" " /* for AF_INET */"
52 .BI "struct hostent *gethostbyaddr(const void *" addr ,
53 .BI " socklen_t " len ", int " type );
54 .sp
55 .BI "void sethostent(int " stayopen );
56 .sp
57 .B void endhostent(void);
58 .sp
59 .BI "void herror(const char *" s );
60 .sp
61 .BI "const char *hstrerror(int " err );
62 .sp
63 /* System V/POSIX extension */
64 .br
65 .B struct hostent *gethostent(void);
66 .sp
67 /* GNU extensions */
68 .br
69 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
70 .sp
71 .B "int gethostent_r("
72 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
73 .BI " struct hostent **" result ", int *" h_errnop );
74 .sp
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 );
78 .sp
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 );
82 .sp
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 );
86 .fi
87 .sp
88 .in -4n
89 Feature Test Macro Requirements for glibc (see
90 .BR feature_test_macros (7)):
91 .in
92 .sp
93 .PD 0
94 .ad l
95 .BR gethostbyname2 (),
96 .BR gethostent_r (),
97 .BR gethostbyaddr_r (),
98 .BR gethostbyname_r (),
99 .BR gethostbyname2_r ():
100 .RS 4
101 _BSD_SOURCE || _SVID_SOURCE
102 .RE
103
104 .BR herror (),
105 .BR hstrerror ():
106 .RS 4
107 .TP 4
108 Since glibc 2.8:
109 _BSD_SOURCE || _SVID_SOURCE || _GNU_SOURCE
110 .TP
111 Before glibc 2.8:
112 none
113 .RE
114 .ad b
115 .PD
116 .SH DESCRIPTION
117 The
118 .BR gethostbyname* ()
119 and
120 .BR gethostbyaddr* ()
121 functions are obsolete.
122 Applications should use
123 .BR getaddrinfo (3)
124 and
125 .BR getnameinfo (3)
126 instead.
127
128 The
129 .BR gethostbyname ()
130 function returns a structure of type
131 .I hostent
132 for the given host
133 .IR name .
134 Here
135 .I name
136 is either a hostname, or an IPv4 address in standard dot notation (as for
137 .BR inet_addr (3)),
138 or an IPv6 address in colon (and possibly dot) notation.
139 (See RFC\ 1884 for the description of IPv6 addresses.)
140 If
141 .I name
142 is an IPv4 or IPv6 address, no lookup is performed and
143 .BR gethostbyname ()
144 simply copies
145 .I name
146 into the
147 .I h_name
148 field and its
149 .I struct in_addr
150 equivalent into the
151 .I h_addr_list[0]
152 field of the returned
153 .I hostent
154 structure.
155 If
156 .I name
157 doesn't end in a dot and the environment variable
158 .B HOSTALIASES
159 is set, the alias file pointed to by
160 .B HOSTALIASES
161 will first be searched for
162 .I name
163 (see
164 .BR hostname (7)
165 for the file format).
166 The current domain and its parents are searched unless \fIname\fP
167 ends in a dot.
168 .PP
169 The
170 .BR gethostbyaddr ()
171 function returns a structure of type \fIhostent\fP
172 for the given host address \fIaddr\fP of length \fIlen\fP and address type
173 \fItype\fP.
174 Valid address types are
175 .B AF_INET
176 and
177 .BR AF_INET6 .
178 The host address argument is a pointer to a struct of a type depending
179 on the address type, for example a \fIstruct in_addr *\fP (probably
180 obtained via a call to
181 .BR inet_addr (3))
182 for address type
183 .BR AF_INET .
184 .PP
185 The
186 .BR sethostent ()
187 function specifies, if \fIstayopen\fP is true (1),
188 that a connected TCP socket should be used for the name server queries and
189 that the connection should remain open during successive queries.
190 Otherwise, name server queries will use UDP datagrams.
191 .PP
192 The
193 .BR endhostent ()
194 function ends the use of a TCP connection for name
195 server queries.
196 .PP
197 The (obsolete)
198 .BR herror ()
199 function prints the error message associated
200 with the current value of \fIh_errno\fP on \fIstderr\fP.
201 .PP
202 The (obsolete)
203 .BR hstrerror ()
204 function takes an error number
205 (typically \fIh_errno\fP) and returns the corresponding message string.
206 .PP
207 The domain name queries carried out by
208 .BR gethostbyname ()
209 and
210 .BR gethostbyaddr ()
211 use a combination of any or all of the name server
212 .BR named (8),
213 a broken out line from \fI/etc/hosts\fP, and the Network
214 Information Service (NIS or YP), depending upon the contents of the
215 \fIorder\fP line in
216 .IR /etc/host.conf .
217 .\" (See
218 .\" .BR resolv+ (8)).
219 The default action is to query
220 .BR named (8),
221 followed by
222 .IR /etc/hosts .
223 .PP
224 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
225 .sp
226 .in +4n
227 .nf
228 .ne 7
229 struct hostent {
230 char *h_name; /* official name of host */
231 char **h_aliases; /* alias list */
232 int h_addrtype; /* host address type */
233 int h_length; /* length of address */
234 char **h_addr_list; /* list of addresses */
235 }
236 #define h_addr h_addr_list[0] /* for backward compatibility */
237 .fi
238 .in
239 .PP
240 The members of the \fIhostent\fP structure are:
241 .TP
242 .I h_name
243 The official name of the host.
244 .TP
245 .I h_aliases
246 An array of alternative names for the host, terminated by a NULL pointer.
247 .TP
248 .I h_addrtype
249 The type of address; always
250 .B AF_INET
251 or
252 .B AF_INET6
253 at present.
254 .TP
255 .I h_length
256 The length of the address in bytes.
257 .TP
258 .I h_addr_list
259 An array of pointers to network addresses for the host (in network byte
260 order), terminated by a NULL pointer.
261 .TP
262 .I h_addr
263 The first address in \fIh_addr_list\fP for backward compatibility.
264 .SH RETURN VALUE
265 The
266 .BR gethostbyname ()
267 and
268 .BR gethostbyaddr ()
269 functions return the
270 .I hostent
271 structure or a NULL pointer if an error occurs.
272 On error, the
273 .I h_errno
274 variable holds an error number.
275 When non-NULL, the return value may point at static data, see the notes below.
276 .SH ERRORS
277 The variable \fIh_errno\fP can have the following values:
278 .TP
279 .B HOST_NOT_FOUND
280 The specified host is unknown.
281 .TP
282 .BR NO_ADDRESS " or " NO_DATA
283 The requested name is valid but does not have an IP address.
284 .TP
285 .B NO_RECOVERY
286 A nonrecoverable name server error occurred.
287 .TP
288 .B TRY_AGAIN
289 A temporary error occurred on an authoritative name server.
290 Try again later.
291 .SH FILES
292 .TP
293 .I /etc/host.conf
294 resolver configuration file
295 .TP
296 .I /etc/hosts
297 host database file
298 .TP
299 .I /etc/nsswitch.conf
300 name service switch configuration
301 .SH CONFORMING TO
302 POSIX.1-2001 specifies
303 .BR gethostbyname (),
304 .BR gethostbyaddr (),
305 .BR sethostent (),
306 .BR endhostent (),
307 .BR gethostent (),
308 and
309 .IR h_errno ;
310 .BR gethostbyname (),
311 .BR gethostbyaddr (),
312 and
313 .IR h_errno
314 are marked obsolescent in that standard.
315 POSIX.1-2008 removes the specifications of
316 .BR gethostbyname (),
317 .BR gethostbyaddr (),
318 and
319 .IR h_errno ,
320 recommending the use of
321 .BR getaddrinfo (3)
322 and
323 .BR getnameinfo (3)
324 instead.
325 .SH NOTES
326 The functions
327 .BR gethostbyname ()
328 and
329 .BR gethostbyaddr ()
330 may return pointers to static data, which may be overwritten by
331 later calls.
332 Copying the
333 .I struct hostent
334 does not suffice, since it contains pointers; a deep copy is required.
335 .LP
336 In the original BSD implementation the
337 .I len
338 argument
339 of
340 .BR gethostbyname ()
341 was an
342 .IR int .
343 The SUSv2 standard is buggy and declares the
344 .I len
345 argument of
346 .BR gethostbyaddr ()
347 to be of type
348 .IR size_t .
349 (That is wrong, because it has to be
350 .IR int ,
351 and
352 .I size_t
353 is not.
354 POSIX.1-2001 makes it
355 .IR socklen_t ,
356 which is OK.)
357 See also
358 .BR accept (2).
359 .LP
360 The BSD prototype for
361 .BR gethostbyaddr ()
362 uses
363 .I const char *
364 for the first argument.
365 .SS "System V/POSIX extension"
366 POSIX requires the
367 .BR gethostent ()
368 call, that should return the next entry in the host data base.
369 When using DNS/BIND this does not make much sense, but it may
370 be reasonable if the host data base is a file that can be read
371 line by line.
372 On many systems a routine of this name reads
373 from the file
374 .IR /etc/hosts .
375 .\" e.g., Linux, FreeBSD, UnixWare, HP-UX
376 It may be available only when the library was built without DNS support.
377 .\" e.g., FreeBSD, AIX
378 The glibc version will ignore ipv6 entries.
379 This function is not reentrant,
380 and glibc adds a reentrant version
381 .BR gethostent_r ().
382 .SS "GNU extensions"
383 Glibc2 also has a
384 .BR gethostbyname2 ()
385 that works like
386 .BR gethostbyname (),
387 but permits to specify the address family to which the address must belong.
388 .LP
389 Glibc2 also has reentrant versions
390 .BR gethostent_r (),
391 .BR gethostbyaddr_r (),
392 .BR gethostbyname_r ()
393 and
394 .BR gethostbyname2_r ().
395 The caller supplies a
396 .I hostent
397 structure
398 .I ret
399 which will be filled in on success, and a temporary work buffer
400 .I buf
401 of size
402 .IR buflen .
403 After the call,
404 .I result
405 will point to the result on success.
406 In case of an error
407 or if no entry is found
408 .I result
409 will be NULL.
410 The functions return 0 on success and a nonzero error number on failure.
411 In addition to the errors returned by the nonreentrant
412 versions of these functions, if
413 .I buf
414 is too small, the functions will return
415 .BR ERANGE ,
416 and the call should be retried with a larger buffer.
417 The global variable
418 .I h_errno
419 is not modified, but the address of a variable in which to store error numbers
420 is passed in
421 .IR h_errnop .
422 .SH BUGS
423 .BR gethostbyname ()
424 does not recognize components of a dotted IPv4 address string
425 that are expressed in hexadecimal.
426 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
427 .SH SEE ALSO
428 .BR getaddrinfo (3),
429 .\" .BR getipnodebyaddr (3),
430 .\" .BR getipnodebyname (3),
431 .BR getnameinfo (3),
432 .BR inet (3),
433 .BR inet_ntop (3),
434 .BR inet_pton (3),
435 .BR resolver (3),
436 .BR hosts (5),
437 .BR nsswitch.conf (5),
438 .BR hostname (7),
439 .BR named (8)
440 .\" .BR resolv+ (8)