xref: /csrg-svn/lib/libc/net/gethostbyname.3 (revision 68453)
161150Sbostic.\" Copyright (c) 1983, 1987, 1991, 1993
261150Sbostic.\"	The Regents of the University of California.  All rights reserved.
320518Smckusick.\"
443572Strent.\" %sccs.include.redist.man%
520518Smckusick.\"
6*68453Smckusick.\"     @(#)gethostbyname.3	8.3 (Berkeley) 02/27/95
736739Sbostic.\"
848352Scael.Dd
948352Scael.Dt GETHOSTBYNAME 3
1048352Scael.Os BSD 4.2
1148352Scael.Sh NAME
1248352Scael.Nm gethostbyname ,
1348352Scael.Nm gethostbyaddr ,
1448352Scael.Nm gethostent ,
1548352Scael.Nm sethostent ,
1648352Scael.Nm endhostent ,
1748352Scael.Nm herror
1848352Scael.Nd get network host entry
1948352Scael.Sh SYNOPSIS
2048352Scael.Fd #include <netdb.h>
21*68453Smckusick.Fd extern int h_errno;
2248352Scael.Ft struct hostent *
2348352Scael.Fn gethostbyname "char *name"
2448352Scael.Ft struct hostent *
2548352Scael.Fn gethostbyaddr "char *addr" "int len" "int type"
2648352Scael.Ft struct hostent *
2748352Scael.Fn gethostent void
2848352Scael.Fn sethostent "int stayopen"
2948352Scael.Fn endhostent void
3048352Scael.Fn herror "char *string"
3148352Scael.Sh DESCRIPTION
3248352ScaelThe
3348352Scael.Fn gethostbyname
3420518Smckusickand
3548352Scael.Fn gethostbyaddr
3648352Scaelfunctions
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,
4148352Scael.Xr named 8 ,
4228278Slepreauor broken-out fields from a line in
4348352Scael.Pa /etc/hosts .
4428062SkjdIf the local name server is not running these routines do a lookup in
4548352Scael.Pa /etc/hosts .
4650728Scael.Bd -literal
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 */
5548352Scael.Ed
5648352Scael.Pp
5720518SmckusickThe members of this structure are:
5848352Scael.Bl -tag -width h_addr_list
5948352Scael.It Fa h_name
6020518SmckusickOfficial name of the host.
6148352Scael.It Fa h_aliases
6220518SmckusickA zero terminated array of alternate names for the host.
6348352Scael.It Fa h_addrtype
6448352ScaelThe type of address being returned; currently always
6548352Scael.Dv AF_INET .
6648352Scael.It Fa h_length
6720518SmckusickThe length, in bytes, of the address.
6848352Scael.It Fa h_addr_list
6928062SkjdA zero terminated array of network addresses for the host.
7028062SkjdHost addresses are returned in network byte order.
7148352Scael.It Fa h_addr
7248352ScaelThe first address in
7348352Scael.Fa h_addr_list ;
7466945Sbosticthis is for backward compatibility.
7548352Scael.Pp
7632653SkarelsWhen using the nameserver,
7748352Scael.Fn gethostbyname
7832653Skarelswill search for the named host in the current domain and its parents
7932653Skarelsunless the name ends in a dot.
8048352ScaelIf the name contains no dot, and if the environment variable
8148352Scael.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
8548352Scael.Xr hostname 7
8632653Skarelsfor the domain search procedure and the alias file format.
8748352Scael.Pp
8848352ScaelThe
8948352Scael.Fn sethostent
9048352Scaelfunction
9148352Scaelmay be used to request the use of a connected
9248352Scael.Tn TCP
9348352Scaelsocket for queries.
9428062SkjdIf the
9548352Scael.Fa stayopen
9620518Smckusickflag is non-zero,
9748352Scaelthis sets the option to send all queries to the name server using
9848352Scael.Tn TCP
9928062Skjdand to retain the connection after each call to
10048352Scael.Fn gethostbyname
10127936Slepreauor
10248352Scael.Fn gethostbyaddr .
10348352ScaelOtherwise, queries are performed using
10448352Scael.Tn UDP
10548352Scaeldatagrams.
10648352Scael.Pp
10748352ScaelThe
10848352Scael.Fn endhostent
10948352Scaelfunction
11048352Scaelcloses the
11148352Scael.Tn TCP
11248352Scaelconnection.
11348352Scael.Sh FILES
11448352Scael.Bl -tag -width /etc/hosts -compact
11548352Scael.It Pa /etc/hosts
11648352Scael.El
11748352Scael.Sh DIAGNOSTICS
11828062SkjdError return status from
11948352Scael.Fn gethostbyname
12020518Smckusickand
12148352Scael.Fn gethostbyaddr
12228062Skjdis indicated by return of a null pointer.
12328062SkjdThe external integer
12448352Scael.Va h_errno
12528062Skjdmay then be checked to see whether this is a temporary failure
12628062Skjdor an invalid or unknown host.
12732649SkarelsThe routine
12848352Scael.Fn herror
12932649Skarelscan be used to print an error message describing the failure.
13032649SkarelsIf its argument
13148352Scael.Fa string
13248352Scaelis
13348352Scael.Pf non Dv -NULL ,
13448352Scaelit is printed, followed by a colon and a space.
13532649SkarelsThe error message is printed with a trailing newline.
13648352Scael.Pp
13748352ScaelThe variable
13848352Scael.Va h_errno
13928062Skjdcan have the following values:
14048352Scael.Bl -tag -width HOST_NOT_FOUND
14148352Scael.It Dv HOST_NOT_FOUND
14228062SkjdNo such host is known.
14348352Scael.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.
14848352Scael.It Dv NO_RECOVERY
14932649SkarelsSome unexpected server failure was encountered.
15028062SkjdThis is a non-recoverable error.
15148352Scael.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.
15948352Scael.El
16048352Scael.Sh SEE ALSO
16148352Scael.Xr resolver 3 ,
16248352Scael.Xr hosts 5 ,
16348352Scael.Xr hostname 7 ,
16448352Scael.Xr named 8
16548352Scael.Sh CAVEAT
16648352ScaelThe
16748352Scael.Fn gethostent
16848352Scaelfunction
16928062Skjdis defined, and
17048352Scael.Fn sethostent
17128278Slepreauand
17248352Scael.Fn endhostent
17328278Slepreauare redefined,
17428062Skjdwhen
17548352Scael.Xr libc 3
17628062Skjdis built to use only the routines to lookup in
17748352Scael.Pa /etc/hosts
17828062Skjdand not the name server.
17948352Scael.Pp
18048352ScaelThe
18148352Scael.Fn gethostent
18248352Scaelfunction
18328062Skjdreads the next line of
18448352Scael.Pa /etc/hosts ,
18528062Skjdopening the file if necessary.
18648352Scael.Pp
18748352ScaelThe
18848352Scael.Fn sethostent
18948352Scaelfunction
19056501Sbosticopens and/or rewinds the file
19156501Sbostic.Pa /etc/hosts .
19256501SbosticIf the
19348352Scael.Fa stayopen
19428278Slepreauargument is non-zero,
19556501Sbosticthe file will not be closed after each call to
19648352Scael.Fn gethostbyname
19728062Skjdor
19848352Scael.Fn gethostbyaddr .
19956501Sbostic.Pp
20048352ScaelThe
20148352Scael.Fn endhostent
20248352Scaelfunction
20356501Sbosticcloses the file.
20448352Scael.Sh HISTORY
20548352ScaelThe
20648352Scael.Fn herror
20748352Scaelfunction appeared in
20848352Scael.Bx 4.3 .
20948352ScaelThe
21048352Scael.Fn endhostent ,
21148352Scael.Fn gethostbyaddr ,
21248352Scael.Fn gethostbyname ,
21348352Scael.Fn gethostent ,
21448352Scaeland
21548352Scael.Fn sethostent
21648352Scaelfunctions appeared in
21748352Scael.Bx 4.2 .
21848352Scael.Sh BUGS
21948352ScaelThese functions use static data storage;
22048352Scaelif the data is needed for future use, it should be
22148352Scaelcopied before any subsequent calls overwrite it.
22248352ScaelOnly the Internet
22320518Smckusickaddress format is currently understood.
224