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

NAME

6       gethostbyname,  gethostbyaddr, sethostent, gethostent, endhostent, her‐
7       ror, hstrerror - get network host entry
8

SYNOPSIS

10       #include <netdb.h>
11       extern int h_errno;
12
13       struct hostent *gethostbyname(const char *name);
14
15       #include <sys/socket.h>       /* for AF_INET */
16       struct hostent *
17       gethostbyaddr(const void *addr, int len, int type);
18
19       void sethostent(int stayopen);
20
21       void endhostent(void);
22
23       void herror(const char *s);
24
25       const char *hstrerror(int err);
26
27
28       /* System V/POSIX extension */
29       struct hostent *gethostent(void);
30
31
32       /* GNU extensions */
33       struct hostent *gethostbyname2(const char *name, int af);
34
35       int gethostent_r(
36         struct hostent *ret, char *buf, size_t buflen,
37         struct hostent **result, int *h_errnop);
38
39       int gethostbyname_r(const char *name,
40         struct hostent *ret, char *buf, size_t buflen,
41         struct hostent **result, int *h_errnop);
42
43       int gethostbyname2_r(const char *name, int af,
44         struct hostent *ret, char *buf, size_t buflen,
45         struct hostent **result, int *h_errnop);
46

DESCRIPTION

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

RETURN VALUE

121       The  gethostbyname()  and  gethostbyaddr() functions return the hostent
122       structure or a NULL pointer if an error occurs.  On error, the  h_errno
123       variable  holds  an  error number.  When non-NULL, the return value may
124       point at static data, see the notes below.
125

ERRORS

127       The variable h_errno can have the following values:
128
129       HOST_NOT_FOUND
130              The specified host is unknown.
131
132       NO_ADDRESS or NO_DATA
133              The requested name is valid but does not have an IP address.
134
135       NO_RECOVERY
136              A non-recoverable name server error occurred.
137
138       TRY_AGAIN
139              A temporary error occurred on an authoritative name server.  Try
140              again later.
141

FILES

143       /etc/host.conf
144              resolver configuration file
145
146       /etc/hosts
147              host database file
148
149       /etc/nsswitch.conf
150              name service switch configuration
151

CONFORMING TO

153       4.3BSD, POSIX.1-2001.
154

SYSTEM V/POSIX EXTENSION

156       POSIX requires the gethostent() call, that should return the next entry
157       in the host data base.  When using DNS/BIND this  does  not  make  much
158       sense,  but  it  may be reasonable if the host data base is a file that
159       can be read line by line. On many systems a routine of this name  reads
160       from  the  file  /etc/hosts.  It may be available only when the library
161       was built without DNS support.  The  glibc  version  will  ignore  ipv6
162       entries.  This  function  is  not reentrant, and glibc adds a reentrant
163       version gethostent_r().
164

GNU EXTENSIONS

166       Glibc2 also has a gethostbyname2() that works like gethostbyname(), but
167       permits to specify the address family to which the address must belong.
168
169       Glibc2  also  has  reentrant  versions gethostbyname_r() and gethostby‐
170       name2_r().  These return 0 on success and non-zero on error. The result
171       of  the  call  is now stored in the struct with address ret.  After the
172       call, *result will be NULL on error or point to the result on  success.
173       Auxiliary  data  is stored in the buffer buf of length buflen.  (If the
174       buffer is too small, these functions will return  ERANGE.)   No  global
175       variable h_errno is modified, but the address of a variable in which to
176       store error numbers is passed in h_errnop.
177

NOTES

179       The functions gethostbyname() and gethostbyaddr() may  return  pointers
180       to  static  data,  which may be overwritten by later calls. Copying the
181       struct hostent does not suffice, since it  contains  pointers;  a  deep
182       copy is required.
183
184       The SUS-v2 standard is buggy and declares the len parameter of gethost‐
185       byaddr() to be of type size_t.  (That is wrong, because it  has  to  be
186       int, and size_t is not.  POSIX.1-2001 makes it socklen_t, which is OK.)
187
188       The  BSD  prototype for gethostbyaddr() uses const char * for the first
189       argument.
190
191       POSIX.1-2001 marks gethostbyaddr() and gethostbyname() obsolescent. See
192       getaddrinfo(3), getnameinfo(3), gai_strerror(3).
193

SEE ALSO

195       getaddrinfo(3), getipnodebyaddr(3), getipnodebyname(3), getnameinfo(3),
196       inet_ntop(3), inet_pton(3),  resolver(3),  hosts(5),  nsswitch.conf(5),
197       hostname(7), named(8)
198
199
200
201                                  2004-10-31                  GETHOSTBYNAME(3)
Impressum