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