From e198d71e66f565f1b105993a32008bab5c57e8e1 Mon Sep 17 00:00:00 2001 From: itojun <> Date: Wed, 9 Feb 2000 12:22:09 +0000 Subject: revise extended scoped address format support. delimiter and the order is changed, based on discussion in ipngwg scoped address cabal. past code: fe80::1@de0 now: de0%fe80::1 this will be in sync with next extended address format proposal (which should be final - I don't want to make this kind of change again). --- src/lib/libc/net/getnameinfo.c | 77 ++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 18 deletions(-) (limited to 'src/lib/libc/net/getnameinfo.c') diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c index 8b2ca299ef..e203cd5d16 100644 --- a/src/lib/libc/net/getnameinfo.c +++ b/src/lib/libc/net/getnameinfo.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getnameinfo.c,v 1.7 2000/01/17 08:15:27 itojun Exp $ */ +/* $OpenBSD: getnameinfo.c,v 1.8 2000/02/09 12:22:09 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -78,6 +78,10 @@ struct sockinet { u_short si_port; }; +#ifdef INET6 +static char *ip6_sa2str __P((struct sockaddr_in6 *, char *, int)); +#endif + #define ENI_NOSOCKET 0 #define ENI_NOSERVNAME 1 #define ENI_NOHOSTNAME 2 @@ -198,13 +202,16 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) * means that the caller does not want the result. */ } else if (flags & NI_NUMERICHOST) { + int numaddrlen; + /* NUMERICHOST and NAMEREQD conflicts with each other */ if (flags & NI_NAMEREQD) return ENI_NOHOSTNAME; if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) == NULL) return ENI_SYSTEM; - if (strlen(numaddr) > hostlen) + numaddrlen = strlen(numaddr); + if (numaddrlen + 1 > hostlen) /* don't forget terminator */ return ENI_MEMORY; strcpy(host, numaddr); #if defined(INET6) && defined(NI_WITHSCOPEID) @@ -216,24 +223,34 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) if (flags & NI_WITHSCOPEID) #endif /* !ALWAYS_WITHSCOPE */ { - char *ep = strchr(host, '\0'); - unsigned int ifindex = - ((struct sockaddr_in6 *)sa)->sin6_scope_id; + char scopebuf[MAXHOSTNAMELEN], *s; + int scopelen; - *ep = SCOPE_DELIMITER; - if ((if_indextoname(ifindex, ep + 1)) == NULL) + if ((s = ip6_sa2str((struct sockaddr_in6 *)sa, + scopebuf, 0)) == NULL) /* XXX what should we do? */ - strncpy(ep + 1, "???", 3); + strcpy(scopebuf, "0"); + + scopelen = strlen(scopebuf); + if (scopelen + 1 + numaddrlen + 1 > hostlen) + return ENI_MEMORY; + + /* + * Shift the host string to allocate + * space for the scope ID part. + */ + memmove(host + scopelen + 1, host, + numaddrlen); + /* copy the scope ID and the delimiter */ + memcpy(host, scopebuf, scopelen); + host[scopelen] = SCOPE_DELIMITER; + host[scopelen + 1 + numaddrlen] = '\0'; } } #endif /* INET6 */ } else { -#ifdef USE_GETIPNODEBY - hp = getipnodebyaddr(addr, afd->a_addrlen, afd->a_af, &h_error); -#else hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); h_error = h_errno; -#endif if (hp) { if (flags & NI_NOFQDN) { @@ -241,15 +258,9 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) if (p) *p = '\0'; } if (strlen(hp->h_name) > hostlen) { -#ifdef USE_GETIPNODEBY - freehostent(hp); -#endif return ENI_MEMORY; } strcpy(host, hp->h_name); -#ifdef USE_GETIPNODEBY - freehostent(hp); -#endif } else { if (flags & NI_NAMEREQD) return ENI_NOHOSTNAME; @@ -263,3 +274,33 @@ getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) } return SUCCESS; } + +#ifdef INET6 +/* ARGSUSED */ +static char * +ip6_sa2str(sa6, buf, flags) + struct sockaddr_in6 *sa6; + char *buf; + int flags; +{ + unsigned int ifindex = (unsigned int)sa6->sin6_scope_id; + struct in6_addr *a6 = &sa6->sin6_addr; + +#ifdef notyet + if (flags & NI_NUMERICSCOPE) { + sprintf(buf, "%d", sa6->sin6_scope_id); + return(buf); + } +#endif + + if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) + return(if_indextoname(ifindex, buf)); + + if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) + return(NULL); /* XXX */ + if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) + return(NULL); /* XXX */ + + return(NULL); /* XXX */ +} +#endif -- cgit v1.2.3-55-g6feb