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