1GETHOSTBYNAME(3) BSD Library Functions Manual GETHOSTBYNAME(3)
2
4 gethostbyname, gethostbyaddr, gethostent, sethostent, endhostent, herror
5 — get network host entry
6
8 #include <netdb.h>
9
10 extern int h_errno;
11
12 struct hostent *
13 gethostbyname(char *name);
14
15 struct hostent *
16 gethostbyname2(char *name, int af);
17
18 struct hostent *
19 gethostbyaddr(char *addr, int len, type);
20
21 struct hostent *
22 gethostent();
23
24 sethostent(int stayopen);
25
26 endhostent();
27
28 herror(char *string);
29
31 Gethostbyname(), gethostbyname2(), and gethostbyaddr() each return a
32 pointer to a hostent structure (see below) describing an internet host
33 referenced by name or by address, as the function names indicate. This
34 structure contains either the information obtained from the name server,
35 named(8), or broken-out fields from a line in /etc/hosts. If the local
36 name server is not running, these routines do a lookup in /etc/hosts.
37
38 struct hostent {
39 char *h_name; /* official name of host */
40 char **h_aliases; /* alias list */
41 int h_addrtype; /* host address type */
42 int h_length; /* length of address */
43 char **h_addr_list; /* list of addresses from name server */
44 };
45
46 #define h_addr h_addr_list[0] /* address, for backward compatibility */
47
48 The members of this structure are:
49
50 h_name Official name of the host.
51
52 h_aliases A zero-terminated array of alternate names for the host.
53
54 h_addrtype The type of address being returned; usually AF_INET.
55
56 h_length The length, in bytes, of the address.
57
58 h_addr_list A zero-terminated array of network addresses for the host.
59 Host addresses are returned in network byte order.
60
61 h_addr The first address in h_addr_list; this is for backward com‐
62 patibility.
63
64 When using the nameserver, gethostbyname() will search for the named host
65 in each parent domain given in the “search” directive of resolv.conf(5)
66 unless the name contains a dot (“.”). If the name contains no dot, and
67 if the environment variable HOSTALIASES contains the name of an alias
68 file, the alias file will first be searched for an alias matching the
69 input name. See hostname(7) for the domain search procedure and the
70 alias file format.
71
72 Gethostbyname2() is an evolution of gethostbyname() intended to allow
73 lookups in address families other than AF_INET, for example, AF_INET6.
74 Currently, the af argument must be specified as AF_INET else the function
75 will return NULL after having set h_errno to NETDB_INTERNAL.
76
77 Sethostent() may be used to request the use of a connected TCP socket for
78 queries. If the stayopen flag is non-zero, this sets the option to send
79 all queries to the name server using TCP and to retain the connection
80 after each call to gethostbyname() or gethostbyaddr(). Otherwise,
81 queries are performed using UDP datagrams.
82
83 Endhostent() closes the TCP connection.
84
86 HOSTALIASES Name of file containing (host alias, full hostname) pairs.
87
89 /etc/hosts See hosts(5).
90
92 Error return status from gethostbyname() and gethostbyaddr() is indicated
93 by return of a null pointer. The external integer h_errno may then be
94 checked to see whether this is a temporary failure or an invalid or
95 unknown host. The routine herror() can be used to print an error message
96 describing the failure. If its argument string is non-NULL, it is
97 printed, followed by a colon and a space. The error message is printed
98 with a trailing newline.
99
100 h_errno can have the following values:
101
102 NETDB_INTERNAL This indicates an internal error in the library,
103 unrelated to the network or name service. errno
104 will be valid in this case; see perror(3).
105
106 HOST_NOT_FOUND No such host is known.
107
108 TRY_AGAIN This is usually a temporary error and means that
109 the local server did not receive a response from
110 an authoritative server. A retry at some later
111 time may succeed.
112
113 NO_RECOVERY Some unexpected server failure was encountered.
114 This is a non-recoverable error, as one might
115 expect.
116
117 NO_DATA The requested name is valid but does not have an
118 IP address; this is not a temporary error. This
119 means that the name is known to the name server
120 but there is no address associated with this
121 name. Another type of request to the name server
122 using this domain name will result in an answer;
123 for example, a mail-forwarder may be registered
124 for this domain.
125
127 hosts(5), hostname(7), named(8), resolver(3), resolver(5).
128
130 Gethostent() is defined, and sethostent() and endhostent() are redefined,
131 when libc is built to use only the routines to lookup in /etc/hosts and
132 not the name server:
133
134
135 Gethostent() reads the next line of /etc/hosts, opening the file if
136 necessary.
137
138 Sethostent() is redefined to open and rewind the file. If the
139 stayopen argument is non-zero, the hosts data base will not be
140 closed after each call to gethostbyname() or gethostbyaddr().
141
142 Endhostent() is redefined to close the file.
143
145 All information is contained in a static area so it must be copied if it
146 is to be saved. Only the Internet address format is currently under‐
147 stood.
148
1494th Berkeley Distribution June 23, 1990 4th Berkeley Distribution