1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
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
36 .TH GETHOSTBYNAME 3 2004-10-31 "" "Linux Programmer's Manual"
38 gethostbyname, gethostbyaddr, sethostent, gethostend, endhostent,
39 herror, hstrerror \- get network host entry
43 .B extern int h_errno;
45 .BI "struct hostent *gethostbyname(const char *" name );
47 .BR "#include <sys/socket.h>" " /* for AF_INET */"
48 .BI "struct hostent *"
50 .BI "gethostbyaddr(const void *" addr ", int " len ", int " type );
52 .BI "void sethostent(int " stayopen );
54 .B void endhostent(void);
56 .BI "void herror(const char *" s );
58 .BI "const char *hstrerror(int " err );
60 /* SYSV/POSIX extension */
62 .B struct hostent *gethostent(void);
66 .BI "struct hostent *gethostbyname2(const char *" name ", int " af );
68 .BI "int gethostent_r("
69 .BI " struct hostent *" ret ", char *" buf ", size_t " buflen ,
70 .BI " struct hostent **" result ", int *" h_errnop );
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 );
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 );
83 function returns a structure of type
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.)
94 is an IPv4 or IPv6 address, no lookup is performed and
104 field of the returned
109 doesn't end in a dot and the environment variable
111 is set, the alias file pointed to by
113 will first be searched for
117 for the file format).
118 The current domain and its parents are searched unless \fIname\fP
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
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.
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.
136 The \fBendhostent()\fP function ends the use of a TCP connection for name
139 The (obsolete) \fBherror()\fP function prints the error message associated
140 with the current value of \fIh_errno\fP on stderr.
142 The (obsolete) \fBhstrerror()\fP function takes an error number
143 (typically \fIh_errno\fP) and returns the corresponding message string.
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
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
154 The default action is to query
159 The \fIhostent\fP structure is defined in \fI<netdb.h>\fP as follows:
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 */
172 #define h_addr h_addr_list[0] /* for backward compatibility */
177 The members of the \fIhostent\fP structure are:
180 The official name of the host.
183 A zero-terminated array of alternative names for the host.
186 The type of address; always
193 The length of the address in bytes.
196 A zero-terminated array of network addresses for the host in network byte
200 The first address in \fIh_addr_list\fP for backward compatibility.
208 structure or a NULL pointer if an error occurs. On error, the
210 variable holds an error number.
211 When non-NULL, the return value may point at static data, see the notes below.
213 The variable \fIh_errno\fP can have the following values:
216 The specified host is unknown.
218 .BR NO_ADDRESS " or " NO_DATA
219 The requested name is valid but does not have an IP address.
222 A non-recoverable name server error occurred.
225 A temporary error occurred on an authoritative name server. Try again
230 resolver configuration file
236 .SH "SYSV/POSIX EXTENSION"
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
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
255 .BR gethostbyname() ,
256 but permits to specify the address family to which the address must belong.
258 Glibc2 also has reentrant versions
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
267 will be NULL on error or point to the result on success.
268 Auxiliary data is stored in the buffer
272 (If the buffer is too small, these functions will return
276 is modified, but the address of a variable in which to store error numbers
284 may return pointers to static data, which may be overwritten by
285 later calls. Copying the
287 does not suffice, since it contains pointers - a deep copy is required.
289 The SUS-v2 standard is buggy and declares the
295 (That is wrong, because it has to be
299 is not. POSIX 1003.1-2001 makes it
303 The BSD prototype for
307 for the first argument.
309 POSIX 1003.1-2001 marks
316 .BR gai_strerror (3).
319 .BR getipnodebyaddr (3),
320 .BR getipnodebyname (3),