]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/gethostbyname.3
gethostbyname.3: ffix
[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 2009-12-03 "" "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 .ad l
94 .BR gethostbyname2 (),
95 .BR gethostent_r (),
96 .BR gethostbyaddr_r (),
97 .BR gethostbyname_r (),
98 .BR gethostbyname2_r ():
99 .RS 4
100 _BSD_SOURCE || _SVID_SOURCE
101 .RE
102
103 .BR herror (),
104 .BR hstrerror ()
105 .RS 4
106 (since glibc 2.8): _BSD_SOURCE || _SVID_SOURCE || _GNU_SOURCE
107 .RE
108 .ad b
109 .SH DESCRIPTION
110 The
111 .BR gethostbyname* ()
112 and
113 .BR gethostbyaddr* ()
114 functions are obsolete.
115 Applications should use
116 .BR getaddrinfo (3)
117 and
118 .BR getnameinfo (3)
119 instead.
120
121 The
122 .BR gethostbyname ()
123 function returns a structure of type
124 .I hostent
125 for the given host
126 .IR name .
127 Here
128 .I name
129 is either a hostname, or an IPv4 address in standard dot notation (as for
130 .BR inet_addr (3)),
131 or an IPv6 address in colon (and possibly dot) notation.
132 (See RFC\ 1884 for the description of IPv6 addresses.)
133 If
134 .I name
135 is an IPv4 or IPv6 address, no lookup is performed and
136 .BR gethostbyname ()
137 simply copies
138 .I name
139 into the
140 .I h_name
141 field and its
142 .I struct in_addr
143 equivalent into the
144 .I h_addr_list[0]
145 field of the returned
146 .I hostent
147 structure.
148 If
149 .I name
150 doesn't end in a dot and the environment variable
151 .B HOSTALIASES
152 is set, the alias file pointed to by
153 .B HOSTALIASES
154 will first be searched for
155 .I name
156 (see
157 .BR hostname (7)
158 for the file format).
159 The current domain and its parents are searched unless \fIname\fP
160 ends in a dot.
161 .PP
162 The
163 .BR gethostbyaddr ()
164 function returns a structure of type \fIhostent\fP
165 for the given host address \fIaddr\fP of length \fIlen\fP and address type
166 \fItype\fP.
167 Valid address types are
168 .B AF_INET
169 and
170 .BR AF_INET6 .
171 The host address argument is a pointer to a struct of a type depending
172 on the address type, for example a \fIstruct in_addr *\fP (probably
173 obtained via a call to
174 .BR inet_addr (3))
175 for address type
176 .BR AF_INET .
177 .PP
178 The
179 .BR sethostent ()
180 function specifies, if \fIstayopen\fP is true (1),
181 that a connected TCP socket should be used for the name server queries and
182 that the connection should remain open during successive queries.
183 Otherwise, name server queries will use UDP datagrams.
184 .PP
185 The
186 .BR endhostent ()
187 function ends the use of a TCP connection for name
188 server queries.
189 .PP
190 The (obsolete)
191 .BR herror ()
192 function prints the error message associated
193 with the current value of \fIh_errno\fP on \fIstderr\fP.
194 .PP
195 The (obsolete)
196 .BR hstrerror ()
197 function takes an error number
198 (typically \fIh_errno\fP) and returns the corresponding message string.
199 .PP
200 The domain name queries carried out by
201 .BR gethostbyname ()
202 and
203 .BR gethostbyaddr ()
204 use a combination of any or all of the name server
205 .BR named (8),
206 a broken out line from \fI/etc/hosts\fP, and the Network
207 Information Service (NIS or YP), depending upon the contents of the
208 \fIorder\fP line in
209 .IR /etc/host.conf .
210 .\" (See
211 .\" .BR resolv+ (8)).
212 The default action is to query
213 .BR named (8),
214 followed by
215 .IR /etc/hosts .
216 .PP
217 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
218 .sp
219 .in +4n
220 .nf
221 .ne 7
222 struct hostent {
223 char *h_name; /* official name of host */
224 char **h_aliases; /* alias list */
225 int h_addrtype; /* host address type */
226 int h_length; /* length of address */
227 char **h_addr_list; /* list of addresses */
228 }
229 #define h_addr h_addr_list[0] /* for backward compatibility */
230 .fi
231 .in
232 .PP
233 The members of the \fIhostent\fP structure are:
234 .TP
235 .I h_name
236 The official name of the host.
237 .TP
238 .I h_aliases
239 An array of alternative names for the host, terminated by a NULL pointer.
240 .TP
241 .I h_addrtype
242 The type of address; always
243 .B AF_INET
244 or
245 .B AF_INET6
246 at present.
247 .TP
248 .I h_length
249 The length of the address in bytes.
250 .TP
251 .I h_addr_list
252 An array of pointers to network addresses for the host (in network byte
253 order), terminated by a NULL pointer.
254 .TP
255 .I h_addr
256 The first address in \fIh_addr_list\fP for backward compatibility.
257 .SH "RETURN VALUE"
258 The
259 .BR gethostbyname ()
260 and
261 .BR gethostbyaddr ()
262 functions return the
263 .I hostent
264 structure or a NULL pointer if an error occurs.
265 On error, the
266 .I h_errno
267 variable holds an error number.
268 When non-NULL, the return value may point at static data, see the notes below.
269 .SH ERRORS
270 The variable \fIh_errno\fP can have the following values:
271 .TP
272 .B HOST_NOT_FOUND
273 The specified host is unknown.
274 .TP
275 .BR NO_ADDRESS " or " NO_DATA
276 The requested name is valid but does not have an IP address.
277 .TP
278 .B NO_RECOVERY
279 A nonrecoverable name server error occurred.
280 .TP
281 .B TRY_AGAIN
282 A temporary error occurred on an authoritative name server.
283 Try again later.
284 .SH FILES
285 .TP
286 .I /etc/host.conf
287 resolver configuration file
288 .TP
289 .I /etc/hosts
290 host database file
291 .TP
292 .I /etc/nsswitch.conf
293 name service switch configuration
294 .SH "CONFORMING TO"
295 POSIX.1-2001 specifies
296 .BR gethostbyname (),
297 .BR gethostbyaddr (),
298 .BR sethostent (),
299 .BR endhostent (),
300 .BR gethostent (),
301 and
302 .IR h_errno ;
303 .BR gethostbyname (),
304 .BR gethostbyaddr (),
305 and
306 .IR h_errno
307 are marked obsolescent in that standard.
308 POSIX.1-2008 removes the specifications of
309 .BR gethostbyname (),
310 .BR gethostbyaddr (),
311 and
312 .IR h_errno ,
313 recommending the use of
314 .BR getaddrinfo (3)
315 and
316 .BR getnameinfo (3)
317 instead.
318 .SH NOTES
319 The functions
320 .BR gethostbyname ()
321 and
322 .BR gethostbyaddr ()
323 may return pointers to static data, which may be overwritten by
324 later calls.
325 Copying the
326 .I struct hostent
327 does not suffice, since it contains pointers; a deep copy is required.
328 .LP
329 In the original BSD implementation the
330 .I len
331 argument
332 of
333 .BR gethostbyname ()
334 was an
335 .IR int .
336 The SUSv2 standard is buggy and declares the
337 .I len
338 argument of
339 .BR gethostbyaddr ()
340 to be of type
341 .IR size_t .
342 (That is wrong, because it has to be
343 .IR int ,
344 and
345 .I size_t
346 is not.
347 POSIX.1-2001 makes it
348 .IR socklen_t ,
349 which is OK.)
350 See also
351 .BR accept (2).
352 .LP
353 The BSD prototype for
354 .BR gethostbyaddr ()
355 uses
356 .I const char *
357 for the first argument.
358 .SS "System V/POSIX Extension"
359 POSIX requires the
360 .BR gethostent ()
361 call, that should return the next entry in the host data base.
362 When using DNS/BIND this does not make much sense, but it may
363 be reasonable if the host data base is a file that can be read
364 line by line.
365 On many systems a routine of this name reads
366 from the file
367 .IR /etc/hosts .
368 .\" e.g., Linux, FreeBSD, Unixware, HP-UX
369 It may be available only when the library was built without DNS support.
370 .\" e.g., FreeBSD, AIX
371 The glibc version will ignore ipv6 entries.
372 This function is not reentrant,
373 and glibc adds a reentrant version
374 .BR gethostent_r ().
375 .SS "GNU Extensions"
376 Glibc2 also has a
377 .BR gethostbyname2 ()
378 that works like
379 .BR gethostbyname (),
380 but permits to specify the address family to which the address must belong.
381 .LP
382 Glibc2 also has reentrant versions
383 .BR gethostent_r (),
384 .BR gethostbyaddr_r (),
385 .BR gethostbyname_r ()
386 and
387 .BR gethostbyname2_r ().
388 The caller supplies a
389 .I hostent
390 structure
391 .I ret
392 which will be filled in on success, and a temporary work buffer
393 .I buf
394 of size
395 .IR buflen .
396 After the call,
397 .I result
398 will point to the result on success.
399 In case of an error
400 or if no entry is found
401 .I result
402 will be NULL.
403 The functions return 0 on success and a nonzero error number on failure.
404 In addition to the errors returned by the nonreentrant
405 versions of these functions, if
406 .I buf
407 is too small, the functions will return
408 .BR ERANGE ,
409 and the call should be retried with a larger buffer.
410 The global variable
411 .I h_errno
412 is not modified, but the address of a variable in which to store error numbers
413 is passed in
414 .IR h_errnop .
415 .SH BUGS
416 .BR gethostbyname ()
417 does not recognize components of a dotted IPv4 address string
418 that are expressed in hexadecimal.
419 .\" http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=482973
420 .SH "SEE ALSO"
421 .BR getaddrinfo (3),
422 .\" .BR getipnodebyaddr (3),
423 .\" .BR getipnodebyname (3),
424 .BR getnameinfo (3),
425 .BR inet (3),
426 .BR inet_ntop (3),
427 .BR inet_pton (3),
428 .BR resolver (3),
429 .BR hosts (5),
430 .BR nsswitch.conf (5),
431 .BR hostname (7),
432 .BR named (8)
433 .\" .BR resolv+ (8)