📄 net-common-tcpip-manpages-getaddrinfo.html
字号:
values, the function still returns a pointer to a string whose contents indicate an unknown error. Extension for scoped IPv6 address The implementation allows experimental numeric IPv6 address notation with scope identifier. By appending the percent character and scope identi- fier to addresses, you can fill sin6_scope_id field for addresses. This would make management of scoped address easier, and allows cut-and-paste input of scoped address. At this moment the code supports only link-local addresses with the for- mat. Scope identifier is hardcoded to name of hardware interface associ- ated with the link. (such as ne0). Example would be like ``fe80::1%ne0'', which means ``fe80::1 on the link associated with ne0 interface''. The implementation is still very experimental and non-standard. The cur- rent implementation assumes one-by-one relationship between interface and link, which is not necessarily true from the specification.EXAMPLES The following code tries to connect to ``www.kame.net'' service ``http''. via stream socket. It loops through all the addresses available, regard- less from address family. If the destination resolves to IPv4 address, it will use AF_INET socket. Similarly, if it resolves to IPv6, AF_INET6 socket is used. Observe that there is no hardcoded reference to particu- lar address family. The code works even if getaddrinfo returns addresses that are not IPv4/v6. struct 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; 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); The following example tries to open a wildcard listening socket onto ser- vice ``http'', for all the address families available. struct 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; } (void) listen(s[nsock], 5); nsock++; } if (nsock == 0) { err(1, cause); /*NOTREACHED*/ } freeaddrinfo(res0);DIAGNOSTICS Error return status from getaddrinfo() is zero on success and non-zero on errors. Non-zero error codes are defined in <netdb.h>, and as follows: EAI_ADDRFAMILY Address family for nodename not supported. EAI_AGAIN Temporary failure in name resolution. EAI_BADFLAGS Invalid value for ai_flags. EAI_FAIL Non-recoverable failure in name resolution. EAI_FAMILY ai_family not supported. EAI_MEMORY Memory allocation failure. EAI_NODATA No address associated with nodename. EAI_NONAME nodename nor servname provided, or not known. EAI_SERVICE servname not supported for ai_socktype. EAI_SOCKTYPE ai_socktype not supported. EAI_SYSTEM System error returned in errno. If called with proper argument, gai_strerror() returns a pointer to a string describing the given error code. If the argument is not one of the EAI_xxx values, the function still returns a pointer to a string whose contents indicate an unknown error.SEE ALSO getnameinfo(3), gethostbyname(3), getservbyname(3), hosts(5), resolv.conf(5), services(5), hostname(7), named(8) R. Gilligan, S. Thomson, J. Bound, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC2553, March 1999. Tatsuya Jinmei and Atsushi Onoe, An Extension of Format for IPv6 Scoped Addresses, internet draft, draft-ietf-ipngwg-scopedaddr-format-02.txt, work in progress material. Craig Metz, "Protocol Independence Using the Sockets API", Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000.HISTORY The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.STANDARDS The getaddrinfo() function is defined in IEEE POSIX 1003.1g draft speci- fication, and documented in ``Basic Socket Interface Extensions for IPv6'' (RFC2553).BUGS The current implementation is not thread-safe. The text was shamelessly copied from RFC2553.BSD May 25, 1995 BSD </PRE></TD></TR></TABLE></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="net-common-tcpip-manpages-ethers.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ecos-ref.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="net-common-tcpip-manpages-gethostbyname.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">ethers</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="tcpip-library-reference.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">gethostbyname</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -