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

📄 getaddrinfo.3

📁 KPIT GNU Tools is a set of GNU development tools for Renesas microcontrollers.
💻 3
字号:
.\"	$FreeBSD: src/lib/libc/net/getaddrinfo.3,v 1.18 2002/04/19 04:46:20 suz Exp $.\"	$KAME: getaddrinfo.3,v 1.31 2001/08/05 18:19:38 itojun Exp $.\".\" 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.\".Dd May 25, 1995.Dt GETADDRINFO 3.Os.\".Sh NAME.Nm getaddrinfo ,.Nm freeaddrinfo ,.Nm gai_strerror.Nd nodename-to-address translation in protocol-independent manner.\".Sh LIBRARY.Lb libc.Sh SYNOPSIS.In sys/types.h.In sys/socket.h.In netdb.h.Ft int.Fn getaddrinfo "const char *nodename" "const char *servname" \"const struct addrinfo *hints" "struct addrinfo **res".Ft void.Fn freeaddrinfo "struct addrinfo *ai".Ft "char *".Fn gai_strerror "int ecode".\".Sh DESCRIPTIONThe.Fn getaddrinfofunction is defined for protocol-independent nodename-to-address translation.It performs the functionality of.Xr gethostbyname 3and.Xr getservbyname 3 ,but in a more sophisticated manner..PpThe.Li addrinfostructure is defined as a result of including the.Aq Pa netdb.hheader:.Bd -literal -offsetstruct addrinfo {     int     ai_flags;     /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */     int     ai_family;    /* PF_xxx */     int     ai_socktype;  /* SOCK_xxx */     int     ai_protocol;  /* 0 or IPPROTO_xxx for IPv4 and IPv6 */     size_t  ai_addrlen;   /* length of ai_addr */     char   *ai_canonname; /* canonical name for nodename */     struct sockaddr  *ai_addr; /* binary address */     struct addrinfo  *ai_next; /* next structure in linked list */};.Ed.PpThe.Fa nodenameand.Fa servnamearguments are pointers to null-terminated strings or.Dv NULL .One or both of these two arguments must be a.Pf non Dv -NULLpointer.In the normal client scenario, both the.Fa nodenameand.Fa servnameare specified.In the normal server scenario, only the.Fa servnameis specified.A.Pf non Dv -NULL.Fa nodenamestring can be either a node name or a numeric host address string(i.e., a dotted-decimal IPv4 address or an IPv6 hex address).A.Pf non Dv -NULL.Fa servnamestring can be either a service name or a decimal port number..PpThe caller can optionally pass an.Li addrinfostructure, pointed to by the third argument,to provide hints concerning the type of socket that the caller supports.In this.Fa hintsstructure all members other than.Fa ai_flags ,.Fa ai_family ,.Fa ai_socktype ,and.Fa ai_protocolmust be zero or a.Dv NULLpointer.A value of.Dv PF_UNSPECfor.Fa ai_familymeans the caller will accept any protocol family.A value of 0 for.Fa ai_socktypemeans the caller will accept any socket type.A value of 0 for.Fa ai_protocolmeans the caller will accept any protocol.For example, if the caller handles only TCP and not UDP, then the.Fa ai_socktypemember of the hints structure should be set to.Dv SOCK_STREAMwhen.Fn getaddrinfois called.If the caller handles only IPv4 and not IPv6, then the.Fa ai_familymember of the.Fa hintsstructure should be set to.Dv PF_INETwhen.Fn getaddrinfois called.If the third argument to.Fn getaddrinfois a.Dv NULLpointer, this is the same as if the caller had filled in an.Li addrinfostructure initialized to zero with.Fa ai_familyset to.Dv PF_UNSPEC ..PpUpon successful return a pointer to a linked list of one or more.Li addrinfostructures is returned through the final argument.The caller can process each.Li addrinfostructure in this list by following the.Fa ai_nextpointer, until a.Dv NULLpointer is encountered.In each returned.Li addrinfostructure the three members.Fa ai_family ,.Fa ai_socktype ,and.Fa ai_protocolare the corresponding arguments for a call to the.Fn socketfunction.In each.Li addrinfostructure the.Fa ai_addrmember points to a filled-in socket address structure whose length isspecified by the.Fa ai_addrlenmember..PpIf the.Dv AI_PASSIVEbit is set in the.Fa ai_flagsmember of the.Fa hintsstructure, then the caller plans to use the returned socket addressstructure in a call to.Fn bind .In this case, if the.Fa nodenameargument is a.Dv NULLpointer, then the IP address portion of the socketaddress structure will be set to.Dv INADDR_ANYfor an IPv4 address or.Dv IN6ADDR_ANY_INITfor an IPv6 address..PpIf the.Dv AI_PASSIVEbit is not set in the.Fa ai_flagsmember of the.Fa hintsstructure, then the returned socket address structure will be ready for acall to.Fn connect(for a connection-oriented protocol)or either.Fn connect ,.Fn sendto ,or.Fn sendmsg(for a connectionless protocol).In this case, if the.Fa nodenameargument is a.Dv NULLpointer, then the IP address portion of thesocket address structure will be set to the loopback address..PpIf the.Dv AI_CANONNAMEbit is set in the.Fa ai_flagsmember of the.Fa hintsstructure, then upon successful return the.Fa ai_canonnamemember of the first.Li addrinfostructure in the linked list will point to a null-terminated stringcontaining the canonical name of the specified.Fa nodename ..PpIf the.Dv AI_NUMERICHOSTbit is set in the.Fa ai_flagsmember of the.Fa hintsstructure, then a.Pf non Dv -NULL.Fa nodenamestring must be a numeric host address string.Otherwise an error of.Dv EAI_NONAMEis returned.This flag prevents any type of name resolution service (e.g., the DNS)from being called..PpThe arguments to.Fn getaddrinfomust be sufficiently consistent and unambiguous.Here are some problem cases you may encounter:.Bl -bullet.It.Fn getaddrinfowill fail if the members in the.Fa hintsstructure are not consistent.For example, for internet address families,.Fn getaddrinfowill fail if you specify.Dv SOCK_STREAMto.Fa ai_socktypewhile you specify.Dv IPPROTO_UDPto.Fa ai_protocol ..ItIf you specify a.Fa servnamewhich is defined only for certain.Fa ai_socktype ,.Fn getaddrinfowill fail because the arguments are not consistent.For example,.Fn getaddrinfowill return an error if you ask for.Dq Li tftpservice on.Dv SOCK_STREAM ..ItFor internet address families, if you specify.Fa servnamewhile you set.Fa ai_socktypeto.Dv SOCK_RAW ,.Fn getaddrinfowill fail, because service names are not defined for the internet.Dv SOCK_RAWspace..ItIf you specify numeric.Fa servname ,while leaving.Fa ai_socktypeand.Fa ai_protocolunspecified,.Fn getaddrinfowill fail.This is because the numeric.Fa servnamedoes not identify any socket type, and.Fn getaddrinfois not allowed to glob the argument in such case..El.PpAll of the information returned by.Fn getaddrinfois dynamically allocated:the.Li addrinfostructures, the socket address structures, and canonical node namestrings pointed to by the addrinfo structures.To return this information to the system the function.Fn freeaddrinfois called.The.Fa addrinfostructure pointed to by the.Fa ai argumentis freed, along with any dynamic storage pointed to by the structure.This operation is repeated until a.Dv NULL.Fa ai_nextpointer is encountered..PpTo aid applications in printing error messages based on the.Dv EAI_xxxcodes returned by.Fn getaddrinfo ,.Fn gai_strerroris defined.The argument is one of the.Dv EAI_xxxvalues defined earlier and the return value points to a string describingthe error.If the argument is not one of the.Dv EAI_xxxvalues, the function still returns a pointer to a string whose contentsindicate an unknown error..\".Sh EXTENSIONSThis implementation supports numeric IPv6 address notation with theexperimental scope identifier.By appending a percent sign and scope identifier to the address, youcan specify the value of the.Li sin6_scope_idfield of the socket address.This makes management of scoped address easier,and allows cut-and-paste input of scoped addresses..PpAt the moment the code supports only link-local addresses in this format.The scope identifier is hardcoded to name of hardware interface associatedwith the link,(such as.Li ne0 ) .For example,.Dq Li fe80::1%ne0 ,which means.Do.Li fe80::1on the link associated with the.Li ne0interface.Dc ..PpThis implementation is still very experimental and non-standard.The current implementation assumes a one-to-one relationship betweeninterfaces and links, which is not necessarily true according to thespecification..\".Sh EXAMPLESThe following code tries to connect to.Dq Li www.kame.netservice.Dq Li http .via stream socket.It loops through all the addresses available, regardless of the address family.If the destination resolves to an IPv4 address, it will use an.Dv AF_INETsocket.Similarly, if it resolves to IPv6, an.Dv AF_INET6socket is used.Observe that there is no hardcoded reference to particular address family.The code works even if.Fn getaddrinforeturns addresses that are not IPv4/v6..Bd -literal -offset indentstruct addrinfo hints, *res, *res0;int error;int s;const char *cause = NULL;memset(&hints, 0, sizeof(hints));hints.ai_family = PF_UNSPEC;hints.ai_socktype = SOCK_STREAM;error = getaddrinfo("www.kame.net", "http", &hints, &res0);if (error) {	errx(1, "%s", gai_strerror(error));	/*NOTREACHED*/}s = -1;cause = "no addresses";errno = EADDRNOTAVAIL;for (res = res0; res; res = res->ai_next) {	s = socket(res->ai_family, res->ai_socktype,	    res->ai_protocol);	if (s < 0) {		cause = "socket";		continue;	}	if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {		cause = "connect";		close(s);		s = -1;		continue;	}	break;	/* okay we got one */}if (s < 0) {	err(1, cause);	/*NOTREACHED*/}freeaddrinfo(res0);.Ed.PpThe following example tries to open a wildcard listening socket onto service.Dq Li http ,for all the address families available..Bd -literal -offset indentstruct addrinfo hints, *res, *res0;int error;int s[MAXSOCK];int nsock;const char *cause = NULL;memset(&hints, 0, sizeof(hints));hints.ai_family = PF_UNSPEC;hints.ai_socktype = SOCK_STREAM;hints.ai_flags = AI_PASSIVE;error = getaddrinfo(NULL, "http", &hints, &res0);if (error) {	errx(1, "%s", gai_strerror(error));	/*NOTREACHED*/}nsock = 0;for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) {	s[nsock] = socket(res->ai_family, res->ai_socktype,	    res->ai_protocol);	if (s[nsock] < 0) {		cause = "socket";		continue;	}	if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {		cause = "bind";		close(s[nsock]);		continue;	}	if (listen(s[nsock], SOMAXCONN) < 0) {		cause = "listen";		close(s[nsock]);		continue;	}	nsock++;}if (nsock == 0) {	err(1, cause);	/*NOTREACHED*/}freeaddrinfo(res0);.Ed.\".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 DIAGNOSTICSError return status from.Fn getaddrinfois zero on success and non-zero on errors.Non-zero error codes are defined in.Aq Pa netdb.h ,and as follows:.Pp.Bl -tag -width EAI_ADDRFAMILY -compact.It Dv EAI_ADDRFAMILYAddress family for.Fa nodenamenot supported..It Dv EAI_AGAINTemporary failure in name resolution..It Dv EAI_BADFLAGSInvalid value for.Fa ai_flags ..It Dv EAI_FAILNon-recoverable failure in name resolution..It Dv EAI_FAMILY.Fa ai_familynot supported..It Dv EAI_MEMORYMemory allocation failure..It Dv EAI_NODATANo address associated with.Fa nodename ..It Dv EAI_NONAME.Fa nodenamenor.Fa servnameprovided, or not known..It Dv EAI_SERVICE.Fa servnamenot supported for.Fa ai_socktype ..It Dv EAI_SOCKTYPE.Fa ai_socktypenot supported..It Dv EAI_SYSTEMSystem error returned in.Va errno ..It Dv EAI_BADHINTSInvalid value for.Fa hints ..It Dv EAI_PROTOCOLResolved protocol is unknown..It Dv EAI_MAXUnknown error..El.PpIf called with an appropriate argument,.Fn gai_strerrorreturns a pointer to a string describing the given error code.If the argument is not one of the.Dv EAI_xxxvalues, the function still returns a pointer to a string whose contentsindicate an unknown error..\".Sh SEE ALSO.Xr gethostbyname 3 ,.Xr getnameinfo 3 ,.Xr getservbyname 3 ,.Xr hosts 5 ,.Xr resolv.conf 5 ,.Xr services 5 ,.Xr hostname 7 ,.Xr named 8.Pp.Rs.%A R. Gilligan.%A S. Thomson.%A J. Bound.%A W. Stevens.%T Basic Socket Interface Extensions for IPv6.%R RFC2553.%D March 1999.Re.Rs.%A Tatsuya Jinmei.%A Atsushi Onoe.%T "An Extension of Format for IPv6 Scoped Addresses".%R internet draft.%N draft-ietf-ipngwg-scopedaddr-format-02.txt.%O work in progress material.Re.Rs.%A Craig Metz.%T Protocol Independence Using the Sockets API.%B "Proceedings of the freenix track: 2000 USENIX annual technical conference".%D June 2000.Re.\".Sh HISTORYThe implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit..\".Sh STANDARDSThe.Fn getaddrinfofunction is defined in.St -p1003.1g-2000 ,and documented in.Dq Basic Socket Interface Extensions for IPv6(RFC2553)..\".Sh BUGSThe current implementation is not thread-safe..PpThe text was shamelessly copied from RFC2553.

⌨️ 快捷键说明

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