xref: /csrg-svn/lib/libc/net/gethostbyname.3 (revision 48352)
1*48352Scael.\" Copyright (c) 1983, 1987, 1991 The Regents of the University of California.
236739Sbostic.\" All rights reserved.
320518Smckusick.\"
443572Strent.\" %sccs.include.redist.man%
520518Smckusick.\"
6*48352Scael.\"     @(#)gethostbyname.3	6.13 (Berkeley) 04/19/91
736739Sbostic.\"
8*48352Scael.Dd
9*48352Scael.Dt GETHOSTBYNAME 3
10*48352Scael.Os BSD 4.2
11*48352Scael.Sh NAME
12*48352Scael.Nm gethostbyname ,
13*48352Scael.Nm gethostbyaddr ,
14*48352Scael.Nm gethostent ,
15*48352Scael.Nm sethostent ,
16*48352Scael.Nm endhostent ,
17*48352Scael.Nm herror
18*48352Scael.Nd get network host entry
19*48352Scael.Sh SYNOPSIS
20*48352Scael.Fd #include <netdb.h>
21*48352Scael.Fd extern struct h_errno;
22*48352Scael.Ft struct hostent *
23*48352Scael.Fn gethostbyname "char *name"
24*48352Scael.Ft struct hostent *
25*48352Scael.Fn gethostbyaddr "char *addr" "int len" "int type"
26*48352Scael.Ft struct hostent *
27*48352Scael.Fn gethostent void
28*48352Scael.Fn sethostent "int stayopen"
29*48352Scael.Fn endhostent void
30*48352Scael.Fn herror "char *string"
31*48352Scael.Sh DESCRIPTION
32*48352ScaelThe
33*48352Scael.Fn gethostbyname
3420518Smckusickand
35*48352Scael.Fn gethostbyaddr
36*48352Scaelfunctions
3720518Smckusickeach return a pointer to an object with the
3832653Skarelsfollowing structure describing an internet host
3932653Skarelsreferenced by name or by address, respectively.
4028062SkjdThis structure contains either the information obtained from the name server,
41*48352Scael.Xr named 8 ,
4228278Slepreauor broken-out fields from a line in
43*48352Scael.Pa /etc/hosts .
4428062SkjdIf the local name server is not running these routines do a lookup in
45*48352Scael.Pa /etc/hosts .
46*48352Scael.Bd -literal -offset indent
4720518Smckusickstruct	hostent {
4820518Smckusick	char	*h_name;	/* official name of host */
4920518Smckusick	char	**h_aliases;	/* alias list */
5028062Skjd	int	h_addrtype;	/* host address type */
5120518Smckusick	int	h_length;	/* length of address */
5228062Skjd	char	**h_addr_list;	/* list of addresses from name server */
5320518Smckusick};
5428062Skjd#define	h_addr  h_addr_list[0]	/* address, for backward compatibility */
55*48352Scael.Ed
56*48352Scael.Pp
5720518SmckusickThe members of this structure are:
58*48352Scael.Bl -tag -width h_addr_list
59*48352Scael.It Fa h_name
6020518SmckusickOfficial name of the host.
61*48352Scael.It Fa h_aliases
6220518SmckusickA zero terminated array of alternate names for the host.
63*48352Scael.It Fa h_addrtype
64*48352ScaelThe type of address being returned; currently always
65*48352Scael.Dv AF_INET .
66*48352Scael.It Fa h_length
6720518SmckusickThe length, in bytes, of the address.
68*48352Scael.It Fa h_addr_list
6928062SkjdA zero terminated array of network addresses for the host.
7028062SkjdHost addresses are returned in network byte order.
71*48352Scael.It Fa h_addr
72*48352ScaelThe first address in
73*48352Scael.Fa h_addr_list ;
74*48352Scaelthis is for backward compatiblity.
75*48352Scael.Pp
7632653SkarelsWhen using the nameserver,
77*48352Scael.Fn gethostbyname
7832653Skarelswill search for the named host in the current domain and its parents
7932653Skarelsunless the name ends in a dot.
80*48352ScaelIf the name contains no dot, and if the environment variable
81*48352Scael.Dq Ev HOSTALIASES
8232653Skarelscontains the name of an alias file, the alias file will first be searched
8332653Skarelsfor an alias matching the input name.
8432653SkarelsSee
85*48352Scael.Xr hostname 7
8632653Skarelsfor the domain search procedure and the alias file format.
87*48352Scael.Pp
88*48352ScaelThe
89*48352Scael.Fn sethostent
90*48352Scaelfunction
91*48352Scaelmay be used to request the use of a connected
92*48352Scael.Tn TCP
93*48352Scaelsocket for queries.
9428062SkjdIf the
95*48352Scael.Fa stayopen
9620518Smckusickflag is non-zero,
97*48352Scaelthis sets the option to send all queries to the name server using
98*48352Scael.Tn TCP
9928062Skjdand to retain the connection after each call to
100*48352Scael.Fn gethostbyname
10127936Slepreauor
102*48352Scael.Fn gethostbyaddr .
103*48352ScaelOtherwise, queries are performed using
104*48352Scael.Tn UDP
105*48352Scaeldatagrams.
106*48352Scael.Pp
107*48352ScaelThe
108*48352Scael.Fn endhostent
109*48352Scaelfunction
110*48352Scaelcloses the
111*48352Scael.Tn TCP
112*48352Scaelconnection.
113*48352Scael.Sh FILES
114*48352Scael.Bl -tag -width /etc/hosts -compact
115*48352Scael.It Pa /etc/hosts
116*48352Scael.El
117*48352Scael.Sh DIAGNOSTICS
11828062SkjdError return status from
119*48352Scael.Fn gethostbyname
12020518Smckusickand
121*48352Scael.Fn gethostbyaddr
12228062Skjdis indicated by return of a null pointer.
12328062SkjdThe external integer
124*48352Scael.Va h_errno
12528062Skjdmay then be checked to see whether this is a temporary failure
12628062Skjdor an invalid or unknown host.
12732649SkarelsThe routine
128*48352Scael.Fn herror
12932649Skarelscan be used to print an error message describing the failure.
13032649SkarelsIf its argument
131*48352Scael.Fa string
132*48352Scaelis
133*48352Scael.Pf non Dv -NULL ,
134*48352Scaelit is printed, followed by a colon and a space.
13532649SkarelsThe error message is printed with a trailing newline.
136*48352Scael.Pp
137*48352ScaelThe variable
138*48352Scael.Va h_errno
13928062Skjdcan have the following values:
140*48352Scael.Bl -tag -width HOST_NOT_FOUND
141*48352Scael.It Dv HOST_NOT_FOUND
14228062SkjdNo such host is known.
143*48352Scael.It Dv TRY_AGAIN
14428062SkjdThis is usually a temporary error
14528062Skjdand means that the local server did not receive
14628062Skjda response from an authoritative server.
14728062SkjdA retry at some later time may succeed.
148*48352Scael.It Dv NO_RECOVERY
14932649SkarelsSome unexpected server failure was encountered.
15028062SkjdThis is a non-recoverable error.
151*48352Scael.It Dv NO_DATA
15228062SkjdThe requested name is valid but does not have an IP address;
15328062Skjdthis is not a temporary error.
15433978SkarelsThis means that the name is known to the name server but there is no address
15533978Skarelsassociated with this name.
15633978SkarelsAnother type of request to the name server using this domain name
15732649Skarelswill result in an answer;
15832649Skarelsfor example, a mail-forwarder may be registered for this domain.
159*48352Scael.El
160*48352Scael.Sh SEE ALSO
161*48352Scael.Xr resolver 3 ,
162*48352Scael.Xr hosts 5 ,
163*48352Scael.Xr hostname 7 ,
164*48352Scael.Xr named 8
165*48352Scael.Sh CAVEAT
166*48352ScaelThe
167*48352Scael.Fn gethostent
168*48352Scaelfunction
16928062Skjdis defined, and
170*48352Scael.Fn sethostent
17128278Slepreauand
172*48352Scael.Fn endhostent
17328278Slepreauare redefined,
17428062Skjdwhen
175*48352Scael.Xr libc 3
17628062Skjdis built to use only the routines to lookup in
177*48352Scael.Pa /etc/hosts
17828062Skjdand not the name server.
179*48352Scael.Pp
180*48352ScaelThe
181*48352Scael.Fn gethostent
182*48352Scaelfunction
18328062Skjdreads the next line of
184*48352Scael.Pa /etc/hosts ,
18528062Skjdopening the file if necessary.
186*48352Scael.Pp
187*48352ScaelThe
188*48352Scael.Fn sethostent
189*48352Scaelfunction
19028278Slepreauis redefined to open and rewind the file.  If the
191*48352Scael.Fa stayopen
19228278Slepreauargument is non-zero,
19328062Skjdthe hosts data base will not be closed after each call to
194*48352Scael.Fn gethostbyname
19528062Skjdor
196*48352Scael.Fn gethostbyaddr .
197*48352ScaelThe
198*48352Scael.Fn endhostent
199*48352Scaelfunction
20028278Slepreauis redefined to close the file.
201*48352Scael.Sh HISTORY
202*48352ScaelThe
203*48352Scael.Fn herror
204*48352Scaelfunction appeared in
205*48352Scael.Bx 4.3 .
206*48352ScaelThe
207*48352Scael.Fn endhostent ,
208*48352Scael.Fn gethostbyaddr ,
209*48352Scael.Fn gethostbyname ,
210*48352Scael.Fn gethostent ,
211*48352Scaeland
212*48352Scael.Fn sethostent
213*48352Scaelfunctions appeared in
214*48352Scael.Bx 4.2 .
215*48352Scael.Sh BUGS
216*48352ScaelThese functions use static data storage;
217*48352Scaelif the data is needed for future use, it should be
218*48352Scaelcopied before any subsequent calls overwrite it.
219*48352ScaelOnly the Internet
22020518Smckusickaddress format is currently understood.
221