Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/net sync with latest KAME tree.
details: https://anonhg.NetBSD.org/src/rev/2854eeb39634
branches: trunk
changeset: 480656:2854eeb39634
user: itojun <itojun%NetBSD.org@localhost>
date: Mon Jan 17 08:33:45 2000 +0000
description:
sync with latest KAME tree.
add example section to get{addr,name}info.
s/\.Os KAME/.Os/.
diffstat:
lib/libc/net/getaddrinfo.3 | 151 +++++++++++++++++++++++++++++++++++------
lib/libc/net/getnameinfo.3 | 45 +++++++++++-
lib/libc/net/if_indextoname.3 | 2 +-
3 files changed, 171 insertions(+), 27 deletions(-)
diffs (truncated from 325 to 300 lines):
diff -r d130f144c2c4 -r 2854eeb39634 lib/libc/net/getaddrinfo.3
--- a/lib/libc/net/getaddrinfo.3 Mon Jan 17 07:22:45 2000 +0000
+++ b/lib/libc/net/getaddrinfo.3 Mon Jan 17 08:33:45 2000 +0000
@@ -30,11 +30,11 @@
.\" SUCH DAMAGE.
.\"
.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
-.\" KAME Id: getaddrinfo.3,v 1.5 1999/12/10 04:04:30 itojun Exp
+.\" KAME Id: getaddrinfo.3,v 1.8 2000/01/17 08:13:03 itojun Exp
.\"
.Dd May 25, 1995
.Dt GETADDRINFO 3
-.Os KAME
+.Os
.\"
.Sh NAME
.Nm getaddrinfo ,
@@ -61,11 +61,11 @@
The
.Fn getaddrinfo
function is defined for protocol-independent nodename-to-address translation.
-It performs functionality of
+It performs the functionality of
.Xr gethostbyname 3
and
.Xr getservbyname 3 ,
-in more sophisticated manner.
+but in a more sophisticated manner.
.Pp
The
.Li addrinfo
@@ -267,7 +267,7 @@
.Dv EAI_NONAME
is returned.
This flag prevents any type of name resolution service (e.g., the DNS)
-from being called.
+from being called.
.Pp
All of the information returned by
.Fn getaddrinfo
@@ -334,6 +334,105 @@
The current implementation assumes one-by-one relationship between
interface and link, which is not necessarily true from the specification.
.\"
+.Sh EXAMPLES
+The following code tries to connect to
+.Dq Li www.kame.net
+service
+.Dq Li http .
+via stream socket.
+It loops through all the addresses available, regardless from address family.
+If the destination resolves to IPv4 address, it will use
+.Dv AF_INET
+socket.
+Similarly, if it resolves to IPv6,
+.Dv AF_INET6
+socket is used.
+Observe that there is no hardcoded reference to particular address family.
+The code works even if
+.Nm getaddrinfo
+returns addresses that are not IPv4/v6.
+.Bd -literal -offset indent
+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) {
+ err1(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);
+.Ed
+.Pp
+The following example tries to open wildcard listening socket onto service
+.Dq Li http ,
+for all the address families available.
+.Bd -literal -offset indent
+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) {
+ err1(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 (connect(s[nsock], res->ai_addr, res->ai_addrlen) < 0) {
+ cause = "connect";
+ close(s[nsock]);
+ continue;
+ }
+
+ nsock++;
+}
+if (nsock == 0) {
+ err(1, cause);
+ /*NOTREACHED*/
+}
+freeaddrinfo(res0);
+.Ed
+.\"
.Sh FILES
.Bl -tag -width /etc/resolv.conf -compact
.It Pa /etc/hosts
@@ -342,7 +441,7 @@
.El
.\"
.Sh DIAGNOSTICS
-Error return status from
+Error return status from
.Fn getaddrinfo
is zero on success and non-zero on errors.
Non-zero error codes are defined in
@@ -350,40 +449,40 @@
and as follows:
.Pp
.Bl -tag -width EAI_ADDRFAMILY -compact
-.It Dv EAI_ADDRFAMILY
-address family for
+.It Dv EAI_ADDRFAMILY
+Address family for
.Fa nodename
-not supported
+not supported.
.It Dv EAI_AGAIN
-temporary failure in name resolution
+Temporary failure in name resolution.
.It Dv EAI_BADFLAGS
-invalid value for
-.Fa ai_flags
+Invalid value for
+.Fa ai_flags .
.It Dv EAI_FAIL
-non-recoverable failure in name resolution
+Non-recoverable failure in name resolution.
.It Dv EAI_FAMILY
.Fa ai_family
-not supported
+not supported.
.It Dv EAI_MEMORY
-memory allocation failure
+Memory allocation failure.
.It Dv EAI_NODATA
-no address associated with
-.Fa nodename
+No address associated with
+.Fa nodename .
.It Dv EAI_NONAME
.Fa nodename
nor
.Fa servname
-provided, or not known
+provided, or not known.
.It Dv EAI_SERVICE
.Fa servname
not supported for
-.Fa ai_socktype
+.Fa ai_socktype .
.It Dv EAI_SOCKTYPE
.Fa ai_socktype
-not supported
+not supported.
.It Dv EAI_SYSTEM
-system error returned in
-.Va errno
+System error returned in
+.Va errno .
.El
.Pp
If called with proper argument,
@@ -412,6 +511,14 @@
.%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-00.txt
+.%O work in progress material
+.Re
.\"
.Sh HISTORY
The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit.
diff -r d130f144c2c4 -r 2854eeb39634 lib/libc/net/getnameinfo.3
--- a/lib/libc/net/getnameinfo.3 Mon Jan 17 07:22:45 2000 +0000
+++ b/lib/libc/net/getnameinfo.3 Mon Jan 17 08:33:45 2000 +0000
@@ -30,11 +30,11 @@
.\" SUCH DAMAGE.
.\"
.\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95
-.\" KAME Id: getnameinfo.3,v 1.4 1999/12/10 04:04:31 itojun Exp
+.\" KAME Id: getnameinfo.3,v 1.7 2000/01/17 08:13:04 itojun Exp
.\"
.Dd May 25, 1995
.Dt GETNAMEINFO 3
-.Os KAME
+.Os
.\"
.Sh NAME
.Nm getnameinfo
@@ -59,7 +59,7 @@
.Xr getaddrinfo 3 ,
and implements similar functionality with
.Xr gethostbyaddr 3 and
-.Xr getservbyport 3
+.Xr getservbyport 3
in more sophisticated manner.
.Pp
This function looks up an IP address and port number provided by the
@@ -144,7 +144,7 @@
.Po
e.g., by calling
.Fn inet_ntop
-instead of
+instead of
.Fn getnodebyaddr
.Pc .
If the
@@ -194,6 +194,35 @@
.Xr getaddrinfo 3
for the notation.
.\"
+.Sh EXAMPLES
+The following code tries to get numeric hostname, and service name,
+for given socket address.
+Observe that there is no hardcoded reference to particular address family.
+.Bd -literal -offset indent
+struct sockaddr *sa; /* input */
+char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+
+if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf,
+ sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
+ errx(1, "could not get numeric hostname");
+ /*NOTREACHED*/
+}
+printf("host=%s, serv=%s\\n", hbuf, sbuf);
+.Ed
+.Pp
+The following version checks if the socket address has reverse address mapping.
+.Bd -literal -offset indent
+struct sockaddr *sa; /* input */
+char hbuf[NI_MAXHOST];
+
+if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0,
+ NI_NAMEREQD)) {
+ errx(1, "could not resolve hostname");
+ /*NOTREACHED*/
+}
+printf("host=%s\\n", hbuf);
+.Ed
+.\"
.Sh FILES
.Bl -tag -width /etc/resolv.conf -compact
.It Pa /etc/hosts
@@ -223,6 +252,14 @@
.%R RFC2553
Home |
Main Index |
Thread Index |
Old Index