⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gethostbyname.3

📁 用于嵌入式Linux系统的标准C的库函数
💻 3
字号:
.\" Copyright (c) 1983, 1987, 1991, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"     From: @(#)gethostbyname.3	8.4 (Berkeley) 5/25/95.\" $FreeBSD: src/lib/libc/net/gethostbyname.3,v 1.26 2002/01/14 16:59:00 ru Exp $.\".Dd May 25, 1995.Dt GETHOSTBYNAME 3.Os.Sh NAME.Nm gethostbyname ,.Nm gethostbyname2 ,.Nm gethostbyaddr ,.Nm gethostent ,.Nm sethostent ,.Nm endhostent ,.Nm herror ,.Nm hstrerror.Nd get network host entry.Sh LIBRARY.Lb libc.Sh SYNOPSIS.In netdb.h.Vt extern int h_errno ;.Ft struct hostent *.Fn gethostbyname "const char *name".Ft struct hostent *.Fn gethostbyname2 "const char *name" "int af".Ft struct hostent *.Fn gethostbyaddr "const char *addr" "int len" "int type".Ft struct hostent *.Fn gethostent void.Ft void.Fn sethostent "int stayopen".Ft void.Fn endhostent void.Ft void.Fn herror "const char *string".Ft const char *.Fn hstrerror "int err".Sh DESCRIPTION.Bf -symbolicThe.Xr getaddrinfo 3and.Xr getnameinfo 3functions are preferred over the.Fn gethostbyname ,.Fn gethostbyname2 ,and.Fn gethostbyaddrfunctions..Ef.PpThe.Fn gethostbyname ,.Fn gethostbyname2and.Fn gethostbyaddrfunctionseach return a pointer to an object with thefollowing structure describing an internet hostreferenced by name or by address, respectively..PpThe.Fa nameparameter passed to.Fn gethostbynameor.Fn gethostbyname2should point to a.Dv NUL Ns -terminatedhostname.The.Fa addrparameter passed to.Fn gethostbyaddrshould point to an address which is.Fa lenbytes long,in binary form(i.e. not an IP address in human readable.Tn ASCIIform).The.Fa typeparameter specifies the address family(e.g.\&.Dv AF_INET , AF_INET6 ,etc.) of this address..PpThe structure returned contains either the information obtained from the nameserver,.Xr named 8 ,broken-out fields from a line in.Pa /etc/hosts ,or database entries supplied by the.Xr yp 8system.The order of the lookups is controlled by the.Sq hostsentry in.Xr nsswitch.conf 5 ..Bd -literalstruct	hostent {	char	*h_name;	/* official name of host */	char	**h_aliases;	/* alias list */	int	h_addrtype;	/* host address type */	int	h_length;	/* length of address */	char	**h_addr_list;	/* list of addresses from name server */};#define	h_addr  h_addr_list[0]	/* address, for backward compatibility */.Ed.PpThe members of this structure are:.Bl -tag -width h_addr_list.It Va h_nameOfficial name of the host..It Va h_aliasesA.Dv NULL Ns -terminatedarray of alternate names for the host..It Va h_addrtypeThe type of address being returned; usually.Dv AF_INET ..It Va h_lengthThe length, in bytes, of the address..It Va h_addr_listA.Dv NULL Ns -terminatedarray of network addresses for the host.Host addresses are returned in network byte order..It Va h_addrThe first address in.Va h_addr_list ;this is for backward compatibility..El.PpWhen using the nameserver,.Fn gethostbynameand.Fn gethostbyname2will search for the named host in the current domain and its parentsunless the name ends in a dot.If the name contains no dot, and if the environment variable.Dq Ev HOSTALIASEScontains the name of an alias file, the alias file will first be searchedfor an alias matching the input name.See.Xr hostname 7for the domain search procedure and the alias file format..PpThe.Fn gethostbyname2function is an evolution of.Fn gethostbynamewhich is intended to allow lookups in address families other than.Dv AF_INET ,for example.Dv AF_INET6 ..PpThe.Fn sethostentfunctionmay be used to request the use of a connected.Tn TCPsocket for queries.If the.Fa stayopenflag is non-zero,this sets the option to send all queries to the name server using.Tn TCPand to retain the connection after each call to.Fn gethostbyname ,.Fn gethostbyname2or.Fn gethostbyaddr .Otherwise, queries are performed using.Tn UDPdatagrams..PpThe.Fn endhostentfunctioncloses the.Tn TCPconnection..PpThe.Fn herrorfunction writes a message to the diagnostic output consisting of thestring parameter.Fa s ,the constant string.Qq Li ":\ " ,and a message corresponding to the value of.Va h_errno ..PpThe.Fn hstrerrorfunction returns a string which is the message text corresponding to thevalue of the.Fa errparameter..Sh FILES.Bl -tag -width /etc/nsswitch.conf -compact.It Pa /etc/hosts.It Pa /etc/nsswitch.conf.It Pa /etc/resolv.conf.El.Sh EXAMPLESPrint out the hostname associated with a specific IP address:.Bd -literal -offset indentconst char *ipstr = "127.0.0.1";struct in_addr ip;struct hostent *hp;if (!inet_aton(ipstr, &ip))	errx(1, "can't parse IP address %s", ipstr);if ((hp = gethostbyaddr((const char *)&ip,    sizeof ip, AF_INET)) == NULL)	errx(1, "no name associated with %s", ipstr);printf("name associated with %s is %s\en", ipstr, hp->h_name);.Ed.Sh DIAGNOSTICSError return status from.Fn gethostbyname ,.Fn gethostbyname2and.Fn gethostbyaddris indicated by return of a.Dv NULLpointer.The external integer.Va h_errnomay then be checked to see whether this is a temporary failureor an invalid or unknown host.The routine.Fn herrorcan be used to print an error message describing the failure.If its argument.Fa stringis.Pf non- Dv NULL ,it is printed, followed by a colon and a space.The error message is printed with a trailing newline..PpThe variable.Va h_errnocan have the following values:.Bl -tag -width HOST_NOT_FOUND.It Dv HOST_NOT_FOUNDNo such host is known..It Dv TRY_AGAINThis is usually a temporary errorand means that the local server did not receivea response from an authoritative server.A retry at some later time may succeed..It Dv NO_RECOVERYSome unexpected server failure was encountered.This is a non-recoverable error..It Dv NO_DATAThe requested name is valid but does not have an IP address;this is not a temporary error.This means that the name is known to the name server but there is no addressassociated with this name.Another type of request to the name server using this domain namewill result in an answer;for example, a mail-forwarder may be registered for this domain..El.Sh SEE ALSO.Xr getaddrinfo 3 ,.Xr getnameinfo 3 ,.Xr inet_aton 3 ,.Xr resolver 3 ,.Xr hosts 5 ,.Xr hostname 7 ,.Xr named 8.Sh CAVEATThe.Fn gethostentfunctionis defined, and.Fn sethostentand.Fn endhostentare redefined,when.Xr libc 3is built to use only the routines to lookup in.Pa /etc/hostsand not the name server..PpThe.Fn gethostentfunctionreads the next line of.Pa /etc/hosts ,opening the file if necessary..PpThe.Fn sethostentfunctionopens and/or rewinds the file.Pa /etc/hosts .If the.Fa stayopenargument is non-zero,the file will not be closed after each call to.Fn gethostbyname ,.Fn gethostbyname2or.Fn gethostbyaddr ..PpThe.Fn endhostentfunctioncloses the file..Sh HISTORYThe.Fn herrorfunction appeared in.Bx 4.3 .The.Fn endhostent ,.Fn gethostbyaddr ,.Fn gethostbyname ,.Fn gethostent ,and.Fn sethostentfunctions appeared in.Bx 4.2 .The.Fn gethostbyname2function first appeared in.Tn BINDversion 4.9.4..Sh BUGSThese functions use static data storage;if the data is needed for future use, it should becopied before any subsequent calls overwrite it.Only the Internetaddress format is currently understood..Pp.Fn gethostbyname2cannot perform.Dv AF_INET6lookups over NIS..Xr getaddrinfo 3must be used instead.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -