1GETHOSTBYNAME(3)           Linux Programmer's Manual          GETHOSTBYNAME(3)
2
3
4

NAME

6       gethostbyname,   gethostbyaddr,   sethostent,  gethostent,  endhostent,
7       h_errno, herror, hstrerror, gethostbyaddr_r, gethostbyname2, gethostby‐
8       name2_r, gethostbyname_r, gethostent_r - get network host entry
9

SYNOPSIS

11       #include <netdb.h>
12       extern int h_errno;
13
14       struct hostent *gethostbyname(const char *name);
15
16       #include <sys/socket.h>       /* for AF_INET */
17       struct hostent *gethostbyaddr(const void *addr,
18                                     socklen_t len, int type);
19
20       void sethostent(int stayopen);
21
22       void endhostent(void);
23
24       void herror(const char *s);
25
26       const char *hstrerror(int err);
27
28       /* System V/POSIX extension */
29       struct hostent *gethostent(void);
30
31       /* GNU extensions */
32       struct hostent *gethostbyname2(const char *name, int af);
33
34       int gethostent_r(
35               struct hostent *ret, char *buf, size_t buflen,
36               struct hostent **result, int *h_errnop);
37
38       int gethostbyaddr_r(const void *addr, socklen_t len, int type,
39               struct hostent *ret, char *buf, size_t buflen,
40               struct hostent **result, int *h_errnop);
41
42       int gethostbyname_r(const char *name,
43               struct hostent *ret, char *buf, size_t buflen,
44               struct hostent **result, int *h_errnop);
45
46       int gethostbyname2_r(const char *name, int af,
47               struct hostent *ret, char *buf, size_t buflen,
48               struct hostent **result, int *h_errnop);
49
50   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
51
52       gethostbyname2(), gethostent_r(), gethostbyaddr_r(), gethostbyname_r(),
53       gethostbyname2_r(): _BSD_SOURCE || _SVID_SOURCE
54
55       herror(), hstrerror() (since glibc 2.8): _BSD_SOURCE || _SVID_SOURCE ||
56       _GNU_SOURCE
57

DESCRIPTION

59       The  gethostbyname*()  and  gethostbyaddr*()  functions  are  obsolete.
60       Applications should use getaddrinfo(3) and getnameinfo(3) instead.
61
62       The gethostbyname() function returns a structure of  type  hostent  for
63       the  given  host  name.   Here  name  is  either a hostname, or an IPv4
64       address in standard dot notation (as  for  inet_addr(3)),  or  an  IPv6
65       address  in  colon  (and possibly dot) notation.  (See RFC 1884 for the
66       description of IPv6 addresses.)  If name is an IPv4 or IPv6 address, no
67       lookup  is  performed  and  gethostbyname() simply copies name into the
68       h_name field and its struct in_addr equivalent into the  h_addr_list[0]
69       field  of the returned hostent structure.  If name doesn't end in a dot
70       and the environment variable HOSTALIASES is set, the alias file pointed
71       to  by HOSTALIASES will first be searched for name (see hostname(7) for
72       the file format).  The current domain  and  its  parents  are  searched
73       unless name ends in a dot.
74
75       The  gethostbyaddr()  function  returns a structure of type hostent for
76       the given host address addr of length len and address type type.  Valid
77       address types are AF_INET and AF_INET6.  The host address argument is a
78       pointer to a struct of a type depending on the address type, for  exam‐
79       ple  a  struct in_addr * (probably obtained via a call to inet_addr(3))
80       for address type AF_INET.
81
82       The sethostent() function specifies, if stayopen is true  (1),  that  a
83       connected  TCP  socket  should  be used for the name server queries and
84       that the connection should remain open during successive queries.  Oth‐
85       erwise, name server queries will use UDP datagrams.
86
87       The  endhostent()  function  ends  the use of a TCP connection for name
88       server queries.
89
90       The (obsolete) herror() function prints the  error  message  associated
91       with the current value of h_errno on stderr.
92
93       The  (obsolete)  hstrerror()  function takes an error number (typically
94       h_errno) and returns the corresponding message string.
95
96       The domain name queries carried out  by  gethostbyname()  and  gethost‐
97       byaddr() use a combination of any or all of the name server named(8), a
98       broken out line from /etc/hosts, and the  Network  Information  Service
99       (NIS  or  YP),  depending  upon  the  contents  of  the  order  line in
100       /etc/host.conf.  The default action is to query named(8),  followed  by
101       /etc/hosts.
102
103       The hostent structure is defined in <netdb.h> as follows:
104
105           struct hostent {
106               char  *h_name;            /* official name of host */
107               char **h_aliases;         /* alias list */
108               int    h_addrtype;        /* host address type */
109               int    h_length;          /* length of address */
110               char **h_addr_list;       /* list of addresses */
111           }
112           #define h_addr h_addr_list[0] /* for backward compatibility */
113
114       The members of the hostent structure are:
115
116       h_name The official name of the host.
117
118       h_aliases
119              An array of alternative names for the host, terminated by a NULL
120              pointer.
121
122       h_addrtype
123              The type of address; always AF_INET or AF_INET6 at present.
124
125       h_length
126              The length of the address in bytes.
127
128       h_addr_list
129              An array of pointers to network addresses for the host (in  net‐
130              work byte order), terminated by a NULL pointer.
131
132       h_addr The first address in h_addr_list for backward compatibility.
133

RETURN VALUE

135       The  gethostbyname()  and  gethostbyaddr() functions return the hostent
136       structure or a NULL pointer if an error occurs.  On error, the  h_errno
137       variable  holds  an  error number.  When non-NULL, the return value may
138       point at static data, see the notes below.
139

ERRORS

141       The variable h_errno can have the following values:
142
143       HOST_NOT_FOUND
144              The specified host is unknown.
145
146       NO_ADDRESS or NO_DATA
147              The requested name is valid but does not have an IP address.
148
149       NO_RECOVERY
150              A nonrecoverable name server error occurred.
151
152       TRY_AGAIN
153              A temporary error occurred on an authoritative name server.  Try
154              again later.
155

FILES

157       /etc/host.conf
158              resolver configuration file
159
160       /etc/hosts
161              host database file
162
163       /etc/nsswitch.conf
164              name service switch configuration
165

CONFORMING TO

167       POSIX.1-2001  specifies gethostbyname(), gethostbyaddr(), sethostent(),
168       endhostent(),  gethostent(),  and  h_errno;  gethostbyname(),  gethost‐
169       byaddr(),   and  h_errno  are  marked  obsolescent  in  that  standard.
170       POSIX.1-2008 removes the specifications  of  gethostbyname(),  gethost‐
171       byaddr(),  and h_errno, recommending the use of getaddrinfo(3) and get‐
172       nameinfo(3) instead.
173

NOTES

175       The functions gethostbyname() and gethostbyaddr() may  return  pointers
176       to  static  data, which may be overwritten by later calls.  Copying the
177       struct hostent does not suffice, since it  contains  pointers;  a  deep
178       copy is required.
179
180       In  the original BSD implementation the len argument of gethostbyname()
181       was an int.  The SUSv2 standard is buggy and declares the len  argument
182       of  gethostbyaddr()  to  be of type size_t.  (That is wrong, because it
183       has to be int, and size_t is not.   POSIX.1-2001  makes  it  socklen_t,
184       which is OK.)  See also accept(2).
185
186       The  BSD  prototype for gethostbyaddr() uses const char * for the first
187       argument.
188
189   System V/POSIX Extension
190       POSIX requires the gethostent() call, that should return the next entry
191       in  the  host  data  base.  When using DNS/BIND this does not make much
192       sense, but it may be reasonable if the host data base is  a  file  that
193       can be read line by line.  On many systems a routine of this name reads
194       from the file /etc/hosts.  It may be available only  when  the  library
195       was  built  without  DNS  support.   The glibc version will ignore ipv6
196       entries.  This function is not reentrant, and glibc  adds  a  reentrant
197       version gethostent_r().
198
199   GNU Extensions
200       Glibc2 also has a gethostbyname2() that works like gethostbyname(), but
201       permits to specify the address family to which the address must belong.
202
203       Glibc2 also has reentrant versions  gethostent_r(),  gethostbyaddr_r(),
204       gethostbyname_r()  and  gethostbyname2_r().  The caller supplies a hos‐
205       tent structure ret which will be filled in on success, and a  temporary
206       work  buffer  buf of size buflen.  After the call, result will point to
207       the result on success.  In case of an error or if  no  entry  is  found
208       result  will  be NULL.  The functions return 0 on success and a nonzero
209       error number on failure.  In addition to the  errors  returned  by  the
210       nonreentrant  versions  of  these  functions,  if buf is too small, the
211       functions will return ERANGE, and the call should  be  retried  with  a
212       larger  buffer.   The  global variable h_errno is not modified, but the
213       address of a variable in which to store  error  numbers  is  passed  in
214       h_errnop.
215

BUGS

217       gethostbyname()  does not recognize components of a dotted IPv4 address
218       string that are expressed in hexadecimal.
219

SEE ALSO

221       getaddrinfo(3), getnameinfo(3),  inet(3),  inet_ntop(3),  inet_pton(3),
222       resolver(3), hosts(5), nsswitch.conf(5), hostname(7), named(8)
223

COLOPHON

225       This  page  is  part of release 3.25 of the Linux man-pages project.  A
226       description of the project, and information about reporting  bugs,  can
227       be found at http://www.kernel.org/doc/man-pages/.
228
229
230
231                                  2009-12-03                  GETHOSTBYNAME(3)
Impressum