Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.48
      1  1.48        is /*	$NetBSD: getnameinfo.c,v 1.48 2010/01/26 21:27:54 is Exp $	*/
      2  1.22    itojun /*	$KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $	*/
      3   1.2    itojun 
      4   1.1    itojun /*
      5  1.32     bjh21  * Copyright (c) 2000 Ben Harris.
      6   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      7   1.1    itojun  * All rights reserved.
      8  1.18    itojun  *
      9   1.1    itojun  * Redistribution and use in source and binary forms, with or without
     10   1.1    itojun  * modification, are permitted provided that the following conditions
     11   1.1    itojun  * are met:
     12   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     13   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     14   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     17   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     18   1.1    itojun  *    may be used to endorse or promote products derived from this software
     19   1.1    itojun  *    without specific prior written permission.
     20  1.18    itojun  *
     21   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     22   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     25   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31   1.1    itojun  * SUCH DAMAGE.
     32   1.1    itojun  */
     33   1.1    itojun 
     34   1.1    itojun /*
     35   1.1    itojun  * Issues to be discussed:
     36   1.1    itojun  * - Thread safe-ness must be checked
     37   1.5    itojun  * - RFC2553 says that we should raise error on short buffer.  X/Open says
     38   1.5    itojun  *   we need to truncate the result.  We obey RFC2553 (and X/Open should be
     39  1.21    itojun  *   modified).  ipngwg rough consensus seems to follow RFC2553.
     40  1.16    itojun  * - What is "local" in NI_FQDN?
     41  1.16    itojun  * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other.
     42  1.34    itojun  * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if
     43  1.34    itojun  *   sin6_scope_id is filled - standardization status?
     44  1.34    itojun  *   XXX breaks backward compat for code that expects no scopeid.
     45  1.34    itojun  *   beware on merge.
     46   1.1    itojun  */
     47  1.15    itojun 
     48  1.15    itojun #include <sys/cdefs.h>
     49  1.15    itojun #if defined(LIBC_SCCS) && !defined(lint)
     50  1.48        is __RCSID("$NetBSD: getnameinfo.c,v 1.48 2010/01/26 21:27:54 is Exp $");
     51  1.15    itojun #endif /* LIBC_SCCS and not lint */
     52   1.1    itojun 
     53  1.14    itojun #include "namespace.h"
     54   1.1    itojun #include <sys/types.h>
     55   1.1    itojun #include <sys/socket.h>
     56   1.5    itojun #include <net/if.h>
     57  1.32     bjh21 #include <net/if_dl.h>
     58  1.32     bjh21 #include <net/if_ieee1394.h>
     59  1.32     bjh21 #include <net/if_types.h>
     60  1.48        is #include <netatalk/at.h>
     61   1.1    itojun #include <netinet/in.h>
     62   1.1    itojun #include <arpa/inet.h>
     63   1.1    itojun #include <arpa/nameser.h>
     64  1.24     lukem #include <assert.h>
     65  1.32     bjh21 #include <limits.h>
     66   1.1    itojun #include <netdb.h>
     67   1.1    itojun #include <resolv.h>
     68  1.24     lukem #include <stddef.h>
     69   1.1    itojun #include <string.h>
     70  1.14    itojun 
     71  1.44  christos #include "servent.h"
     72  1.44  christos 
     73  1.14    itojun #ifdef __weak_alias
     74  1.14    itojun __weak_alias(getnameinfo,_getnameinfo)
     75  1.14    itojun #endif
     76   1.1    itojun 
     77  1.25  jdolecek static const struct afd {
     78  1.27    kleink 	int		a_af;
     79  1.27    kleink 	socklen_t	a_addrlen;
     80  1.27    kleink 	socklen_t	a_socklen;
     81  1.27    kleink 	int		a_off;
     82   1.1    itojun } afdl [] = {
     83   1.1    itojun #ifdef INET6
     84   1.1    itojun 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     85   1.1    itojun 		offsetof(struct sockaddr_in6, sin6_addr)},
     86   1.1    itojun #endif
     87   1.1    itojun 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     88   1.1    itojun 		offsetof(struct sockaddr_in, sin_addr)},
     89  1.45  christos 	{0, 0, 0, 0},
     90   1.1    itojun };
     91   1.1    itojun 
     92   1.1    itojun struct sockinet {
     93   1.1    itojun 	u_char	si_len;
     94   1.1    itojun 	u_char	si_family;
     95   1.1    itojun 	u_short	si_port;
     96   1.1    itojun };
     97   1.1    itojun 
     98  1.32     bjh21 static int getnameinfo_inet __P((const struct sockaddr *, socklen_t, char *,
     99  1.39    kleink     socklen_t, char *, socklen_t, int));
    100  1.10    itojun #ifdef INET6
    101  1.16    itojun static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *,
    102  1.39    kleink 				 socklen_t, int));
    103  1.37    kleink static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t,
    104  1.39    kleink 				 int));
    105  1.18    itojun #endif
    106  1.48        is static int getnameinfo_atalk __P((const struct sockaddr *, socklen_t, char *,
    107  1.48        is     socklen_t, char *, socklen_t, int));
    108  1.48        is 
    109  1.32     bjh21 static int getnameinfo_link __P((const struct sockaddr *, socklen_t, char *,
    110  1.39    kleink     socklen_t, char *, socklen_t, int));
    111  1.36    kleink static int hexname __P((const u_int8_t *, size_t, char *, socklen_t));
    112  1.10    itojun 
    113  1.32     bjh21 /*
    114  1.32     bjh21  * Top-level getnameinfo() code.  Look at the address family, and pick an
    115  1.32     bjh21  * appropriate function to call.
    116  1.32     bjh21  */
    117   1.1    itojun int
    118   1.1    itojun getnameinfo(sa, salen, host, hostlen, serv, servlen, flags)
    119   1.1    itojun 	const struct sockaddr *sa;
    120  1.17    itojun 	socklen_t salen;
    121  1.32     bjh21 	char *host, *serv;
    122  1.36    kleink 	socklen_t hostlen, servlen;
    123  1.39    kleink 	int flags;
    124  1.32     bjh21 {
    125  1.32     bjh21 
    126  1.32     bjh21 	switch (sa->sa_family) {
    127  1.48        is 	case AF_APPLETALK:
    128  1.48        is 		return getnameinfo_atalk(sa, salen, host, hostlen,
    129  1.48        is 		    serv, servlen, flags);
    130  1.32     bjh21 	case AF_INET:
    131  1.32     bjh21 	case AF_INET6:
    132  1.32     bjh21 		return getnameinfo_inet(sa, salen, host, hostlen,
    133  1.32     bjh21 		    serv, servlen, flags);
    134  1.32     bjh21 	case AF_LINK:
    135  1.32     bjh21 		return getnameinfo_link(sa, salen, host, hostlen,
    136  1.32     bjh21 		    serv, servlen, flags);
    137  1.32     bjh21 	default:
    138  1.32     bjh21 		return EAI_FAMILY;
    139  1.32     bjh21 	}
    140  1.35  sommerfe }
    141  1.32     bjh21 
    142  1.48        is /*
    143  1.48        is  * getnameinfo_atalk():
    144  1.48        is  * Format an AppleTalk address into a printable format.
    145  1.48        is  */
    146  1.48        is /* ARGSUSED */
    147  1.48        is static int
    148  1.48        is getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
    149  1.48        is     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    150  1.48        is     int flags)
    151  1.48        is {
    152  1.48        is 	char numserv[8];
    153  1.48        is 	int n;
    154  1.48        is 
    155  1.48        is 	const struct sockaddr_at *sat =
    156  1.48        is 	    (const struct sockaddr_at *)(const void *)sa;
    157  1.48        is 
    158  1.48        is 	if (serv != NULL && servlen > 0) {
    159  1.48        is 		snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
    160  1.48        is 		if (strlen(numserv) + 1 > servlen)
    161  1.48        is 			return EAI_MEMORY;
    162  1.48        is 		strlcpy(serv, numserv, servlen);
    163  1.48        is 	}
    164  1.48        is 
    165  1.48        is 	if (sat->sat_range.r_netrange.nr_phase) {
    166  1.48        is         	n = snprintf(host, hostlen, "%u.%u phase %u",
    167  1.48        is 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
    168  1.48        is 			sat->sat_range.r_netrange.nr_phase);
    169  1.48        is 	} else {
    170  1.48        is         	n = snprintf(host, hostlen, "%u.%u",
    171  1.48        is 		    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    172  1.48        is 	}
    173  1.48        is 
    174  1.48        is 	if (n < 0 || (socklen_t) n >= hostlen) {
    175  1.48        is 		if (host != NULL && hostlen > 0)
    176  1.48        is 			*host = '\0';	/* XXX ??? */
    177  1.48        is 		return EAI_MEMORY;
    178  1.48        is 	}
    179  1.48        is 	return 0;
    180  1.48        is }
    181  1.32     bjh21 
    182  1.32     bjh21 /*
    183  1.32     bjh21  * getnameinfo_inet():
    184  1.32     bjh21  * Format an IPv4 or IPv6 sockaddr into a printable string.
    185  1.32     bjh21  */
    186  1.32     bjh21 static int
    187  1.32     bjh21 getnameinfo_inet(sa, salen, host, hostlen, serv, servlen, flags)
    188  1.32     bjh21 	const struct sockaddr *sa;
    189  1.32     bjh21 	socklen_t salen;
    190   1.1    itojun 	char *host;
    191  1.36    kleink 	socklen_t hostlen;
    192   1.1    itojun 	char *serv;
    193  1.36    kleink 	socklen_t servlen;
    194  1.39    kleink 	int flags;
    195   1.1    itojun {
    196  1.25  jdolecek 	const struct afd *afd;
    197   1.1    itojun 	struct servent *sp;
    198   1.1    itojun 	struct hostent *hp;
    199   1.1    itojun 	u_short port;
    200   1.5    itojun 	int family, i;
    201  1.16    itojun 	const char *addr;
    202   1.5    itojun 	u_int32_t v4a;
    203   1.1    itojun 	char numserv[512];
    204   1.1    itojun 	char numaddr[512];
    205   1.1    itojun 
    206  1.24     lukem 	/* sa is checked below */
    207  1.24     lukem 	/* host may be NULL */
    208  1.24     lukem 	/* serv may be NULL */
    209  1.24     lukem 
    210   1.1    itojun 	if (sa == NULL)
    211  1.33    itojun 		return EAI_FAIL;
    212   1.1    itojun 
    213  1.13  christos #ifdef BSD4_4
    214   1.5    itojun 	if (sa->sa_len != salen)
    215  1.33    itojun 		return EAI_FAIL;
    216  1.13  christos #endif
    217  1.33    itojun 
    218   1.1    itojun 	family = sa->sa_family;
    219   1.1    itojun 	for (i = 0; afdl[i].a_af; i++)
    220   1.1    itojun 		if (afdl[i].a_af == family) {
    221   1.1    itojun 			afd = &afdl[i];
    222   1.1    itojun 			goto found;
    223   1.1    itojun 		}
    224  1.33    itojun 	return EAI_FAMILY;
    225  1.33    itojun 
    226   1.1    itojun  found:
    227   1.5    itojun 	if (salen != afd->a_socklen)
    228  1.33    itojun 		return EAI_FAIL;
    229  1.33    itojun 
    230  1.16    itojun 	/* network byte order */
    231  1.20  christos 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    232  1.20  christos 	addr = (const char *)(const void *)sa + afd->a_off;
    233   1.1    itojun 
    234   1.1    itojun 	if (serv == NULL || servlen == 0) {
    235   1.5    itojun 		/*
    236   1.5    itojun 		 * do nothing in this case.
    237   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    238  1.33    itojun 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
    239  1.33    itojun 		 * servlen == 0 means that the caller does not want the result.
    240   1.5    itojun 		 */
    241   1.1    itojun 	} else {
    242  1.47     seanb 		struct servent_data svd;
    243  1.47     seanb 		struct servent sv;
    244  1.47     seanb 
    245   1.5    itojun 		if (flags & NI_NUMERICSERV)
    246   1.5    itojun 			sp = NULL;
    247   1.5    itojun 		else {
    248  1.44  christos 			(void)memset(&svd, 0, sizeof(svd));
    249  1.44  christos 			sp = getservbyport_r(port,
    250  1.44  christos 				(flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
    251   1.5    itojun 		}
    252   1.1    itojun 		if (sp) {
    253  1.47     seanb 			if (strlen(sp->s_name) + 1 > servlen) {
    254  1.47     seanb 				endservent_r(&svd);
    255  1.33    itojun 				return EAI_MEMORY;
    256  1.47     seanb 			}
    257  1.38    itojun 			strlcpy(serv, sp->s_name, servlen);
    258  1.47     seanb 			endservent_r(&svd);
    259   1.5    itojun 		} else {
    260  1.40    itojun 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
    261  1.22    itojun 			if (strlen(numserv) + 1 > servlen)
    262  1.33    itojun 				return EAI_MEMORY;
    263  1.38    itojun 			strlcpy(serv, numserv, servlen);
    264   1.5    itojun 		}
    265   1.1    itojun 	}
    266   1.1    itojun 
    267   1.1    itojun 	switch (sa->sa_family) {
    268   1.1    itojun 	case AF_INET:
    269   1.5    itojun 		v4a = (u_int32_t)
    270  1.20  christos 		    ntohl(((const struct sockaddr_in *)
    271  1.20  christos 		    (const void *)sa)->sin_addr.s_addr);
    272   1.1    itojun 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    273   1.1    itojun 			flags |= NI_NUMERICHOST;
    274   1.1    itojun 		v4a >>= IN_CLASSA_NSHIFT;
    275   1.7    itojun 		if (v4a == 0)
    276  1.35  sommerfe 			flags |= NI_NUMERICHOST;
    277   1.1    itojun 		break;
    278   1.1    itojun #ifdef INET6
    279   1.1    itojun 	case AF_INET6:
    280   1.3    itojun 	    {
    281  1.16    itojun 		const struct sockaddr_in6 *sin6;
    282  1.20  christos 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    283   1.5    itojun 		switch (sin6->sin6_addr.s6_addr[0]) {
    284   1.3    itojun 		case 0x00:
    285   1.3    itojun 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    286   1.3    itojun 				;
    287   1.3    itojun 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    288   1.3    itojun 				;
    289   1.3    itojun 			else
    290   1.3    itojun 				flags |= NI_NUMERICHOST;
    291   1.3    itojun 			break;
    292   1.3    itojun 		default:
    293   1.5    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    294   1.3    itojun 				flags |= NI_NUMERICHOST;
    295   1.5    itojun 			}
    296   1.3    itojun 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    297   1.3    itojun 				flags |= NI_NUMERICHOST;
    298   1.3    itojun 			break;
    299   1.3    itojun 		}
    300   1.3    itojun 	    }
    301   1.1    itojun 		break;
    302   1.1    itojun #endif
    303   1.1    itojun 	}
    304   1.1    itojun 	if (host == NULL || hostlen == 0) {
    305   1.5    itojun 		/*
    306   1.5    itojun 		 * do nothing in this case.
    307   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    308  1.33    itojun 		 * "||" here: rfc2553bis-03 says that host == NULL or
    309  1.33    itojun 		 * hostlen == 0 means that the caller does not want the result.
    310   1.5    itojun 		 */
    311   1.1    itojun 	} else if (flags & NI_NUMERICHOST) {
    312  1.41   thorpej 		size_t numaddrlen;
    313  1.10    itojun 
    314   1.3    itojun 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    315   1.3    itojun 		if (flags & NI_NAMEREQD)
    316  1.33    itojun 			return EAI_NONAME;
    317   1.5    itojun 
    318  1.16    itojun 		switch(afd->a_af) {
    319  1.16    itojun #ifdef INET6
    320  1.16    itojun 		case AF_INET6:
    321  1.16    itojun 		{
    322  1.16    itojun 			int error;
    323  1.16    itojun 
    324  1.16    itojun 			if ((error = ip6_parsenumeric(sa, addr, host,
    325  1.16    itojun 						      hostlen, flags)) != 0)
    326  1.16    itojun 				return(error);
    327  1.16    itojun 			break;
    328  1.16    itojun 		}
    329  1.11    itojun #endif
    330  1.16    itojun 		default:
    331  1.16    itojun 			if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
    332  1.16    itojun 			    == NULL)
    333  1.33    itojun 				return EAI_SYSTEM;
    334  1.16    itojun 			numaddrlen = strlen(numaddr);
    335  1.16    itojun 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    336  1.33    itojun 				return EAI_MEMORY;
    337  1.38    itojun 			strlcpy(host, numaddr, hostlen);
    338  1.16    itojun 			break;
    339   1.5    itojun 		}
    340   1.1    itojun 	} else {
    341   1.1    itojun 		hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
    342   1.1    itojun 
    343   1.1    itojun 		if (hp) {
    344  1.16    itojun #if 0
    345  1.16    itojun 			/*
    346  1.16    itojun 			 * commented out, since "for local host" is not
    347  1.16    itojun 			 * implemented here - see RFC2553 p30
    348  1.16    itojun 			 */
    349   1.1    itojun 			if (flags & NI_NOFQDN) {
    350  1.16    itojun 				char *p;
    351   1.1    itojun 				p = strchr(hp->h_name, '.');
    352  1.16    itojun 				if (p)
    353  1.16    itojun 					*p = '\0';
    354   1.1    itojun 			}
    355  1.16    itojun #endif
    356  1.22    itojun 			if (strlen(hp->h_name) + 1 > hostlen) {
    357  1.33    itojun 				return EAI_MEMORY;
    358   1.1    itojun 			}
    359  1.38    itojun 			strlcpy(host, hp->h_name, hostlen);
    360   1.1    itojun 		} else {
    361   1.1    itojun 			if (flags & NI_NAMEREQD)
    362  1.33    itojun 				return EAI_NONAME;
    363  1.16    itojun 			switch(afd->a_af) {
    364  1.16    itojun #ifdef INET6
    365  1.16    itojun 			case AF_INET6:
    366  1.16    itojun 			{
    367  1.16    itojun 				int error;
    368  1.16    itojun 
    369  1.16    itojun 				if ((error = ip6_parsenumeric(sa, addr, host,
    370  1.16    itojun 							      hostlen,
    371  1.16    itojun 							      flags)) != 0)
    372  1.16    itojun 					return(error);
    373  1.16    itojun 				break;
    374  1.16    itojun 			}
    375  1.16    itojun #endif
    376  1.16    itojun 			default:
    377  1.16    itojun 				if (inet_ntop(afd->a_af, addr, host,
    378  1.16    itojun 				    hostlen) == NULL)
    379  1.33    itojun 					return EAI_SYSTEM;
    380  1.16    itojun 				break;
    381  1.16    itojun 			}
    382   1.1    itojun 		}
    383   1.1    itojun 	}
    384  1.33    itojun 	return(0);
    385   1.1    itojun }
    386  1.10    itojun 
    387  1.10    itojun #ifdef INET6
    388  1.16    itojun static int
    389  1.16    itojun ip6_parsenumeric(sa, addr, host, hostlen, flags)
    390  1.16    itojun 	const struct sockaddr *sa;
    391  1.16    itojun 	const char *addr;
    392  1.16    itojun 	char *host;
    393  1.36    kleink 	socklen_t hostlen;
    394  1.39    kleink 	int flags;
    395  1.16    itojun {
    396  1.41   thorpej 	size_t numaddrlen;
    397  1.16    itojun 	char numaddr[512];
    398  1.16    itojun 
    399  1.24     lukem 	_DIAGASSERT(sa != NULL);
    400  1.24     lukem 	_DIAGASSERT(addr != NULL);
    401  1.24     lukem 	_DIAGASSERT(host != NULL);
    402  1.24     lukem 
    403  1.33    itojun 	if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
    404  1.33    itojun 		return EAI_SYSTEM;
    405  1.16    itojun 
    406  1.16    itojun 	numaddrlen = strlen(numaddr);
    407  1.16    itojun 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    408  1.43  ginsbach 		return EAI_OVERFLOW;
    409  1.38    itojun 	strlcpy(host, numaddr, hostlen);
    410  1.16    itojun 
    411  1.20  christos 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    412  1.34    itojun 		char zonebuf[MAXHOSTNAMELEN];
    413  1.34    itojun 		int zonelen;
    414  1.16    itojun 
    415  1.34    itojun 		zonelen = ip6_sa2str(
    416  1.34    itojun 		    (const struct sockaddr_in6 *)(const void *)sa,
    417  1.34    itojun 		    zonebuf, sizeof(zonebuf), flags);
    418  1.34    itojun 		if (zonelen < 0)
    419  1.43  ginsbach 			return EAI_OVERFLOW;
    420  1.41   thorpej 		if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
    421  1.43  ginsbach 			return EAI_OVERFLOW;
    422  1.34    itojun 		/* construct <numeric-addr><delim><zoneid> */
    423  1.34    itojun 		memcpy(host + numaddrlen + 1, zonebuf,
    424  1.34    itojun 		    (size_t)zonelen);
    425  1.34    itojun 		host[numaddrlen] = SCOPE_DELIMITER;
    426  1.34    itojun 		host[numaddrlen + 1 + zonelen] = '\0';
    427  1.16    itojun 	}
    428  1.16    itojun 
    429  1.16    itojun 	return 0;
    430  1.16    itojun }
    431  1.16    itojun 
    432  1.10    itojun /* ARGSUSED */
    433  1.16    itojun static int
    434  1.16    itojun ip6_sa2str(sa6, buf, bufsiz, flags)
    435  1.16    itojun 	const struct sockaddr_in6 *sa6;
    436  1.10    itojun 	char *buf;
    437  1.16    itojun 	size_t bufsiz;
    438  1.39    kleink 	int flags;
    439  1.10    itojun {
    440  1.24     lukem 	unsigned int ifindex;
    441  1.24     lukem 	const struct in6_addr *a6;
    442  1.28    itojun 	int n;
    443  1.24     lukem 
    444  1.24     lukem 	_DIAGASSERT(sa6 != NULL);
    445  1.24     lukem 	_DIAGASSERT(buf != NULL);
    446  1.35  sommerfe 
    447  1.24     lukem 	ifindex = (unsigned int)sa6->sin6_scope_id;
    448  1.24     lukem 	a6 = &sa6->sin6_addr;
    449  1.10    itojun 
    450  1.28    itojun #ifdef NI_NUMERICSCOPE
    451  1.28    itojun 	if ((flags & NI_NUMERICSCOPE) != 0) {
    452  1.28    itojun 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    453  1.46     lukem 		if (n < 0 || (size_t)n >= bufsiz)
    454  1.28    itojun 			return -1;
    455  1.28    itojun 		else
    456  1.28    itojun 			return n;
    457  1.10    itojun 	}
    458  1.10    itojun #endif
    459  1.18    itojun 
    460  1.16    itojun 	/* if_indextoname() does not take buffer size.  not a good api... */
    461  1.16    itojun 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    462  1.16    itojun 	    bufsiz >= IF_NAMESIZE) {
    463  1.16    itojun 		char *p = if_indextoname(ifindex, buf);
    464  1.16    itojun 		if (p) {
    465  1.16    itojun 			return(strlen(p));
    466  1.16    itojun 		}
    467  1.16    itojun 	}
    468  1.10    itojun 
    469  1.16    itojun 	/* last resort */
    470  1.28    itojun 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    471  1.41   thorpej 	if (n < 0 || (size_t) n >= bufsiz)
    472  1.28    itojun 		return -1;
    473  1.28    itojun 	else
    474  1.28    itojun 		return n;
    475  1.10    itojun }
    476  1.16    itojun #endif /* INET6 */
    477  1.32     bjh21 
    478  1.32     bjh21 
    479  1.32     bjh21 /*
    480  1.32     bjh21  * getnameinfo_link():
    481  1.32     bjh21  * Format a link-layer address into a printable format, paying attention to
    482  1.32     bjh21  * the interface type.
    483  1.32     bjh21  */
    484  1.32     bjh21 /* ARGSUSED */
    485  1.32     bjh21 static int
    486  1.32     bjh21 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
    487  1.37    kleink     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    488  1.39    kleink     int flags)
    489  1.32     bjh21 {
    490  1.32     bjh21 	const struct sockaddr_dl *sdl =
    491  1.32     bjh21 	    (const struct sockaddr_dl *)(const void *)sa;
    492  1.32     bjh21 	const struct ieee1394_hwaddr *iha;
    493  1.32     bjh21 	int n;
    494  1.32     bjh21 
    495  1.32     bjh21 	if (serv != NULL && servlen > 0)
    496  1.32     bjh21 		*serv = '\0';
    497  1.32     bjh21 
    498  1.32     bjh21 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
    499  1.40    itojun 		n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
    500  1.41   thorpej 		if (n < 0 || (socklen_t) n > hostlen) {
    501  1.32     bjh21 			*host = '\0';
    502  1.32     bjh21 			return EAI_MEMORY;
    503  1.32     bjh21 		}
    504  1.32     bjh21 		return 0;
    505  1.32     bjh21 	}
    506  1.32     bjh21 
    507  1.32     bjh21 	switch (sdl->sdl_type) {
    508  1.32     bjh21 #ifdef IFT_ECONET
    509  1.32     bjh21 	case IFT_ECONET:
    510  1.32     bjh21 		if (sdl->sdl_alen < 2)
    511  1.32     bjh21 			return EAI_FAMILY;
    512  1.42  christos 		if (CLLADDR(sdl)[1] == 0)
    513  1.42  christos 			n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
    514  1.32     bjh21 		else
    515  1.40    itojun 			n = snprintf(host, hostlen, "%u.%u",
    516  1.42  christos 			    CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
    517  1.41   thorpej 		if (n < 0 || (socklen_t) n >= hostlen) {
    518  1.32     bjh21 			*host = '\0';
    519  1.32     bjh21 			return EAI_MEMORY;
    520  1.32     bjh21 		} else
    521  1.32     bjh21 			return 0;
    522  1.32     bjh21 #endif
    523  1.32     bjh21 	case IFT_IEEE1394:
    524  1.32     bjh21 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
    525  1.32     bjh21 			return EAI_FAMILY;
    526  1.32     bjh21 		iha =
    527  1.42  christos 		    (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
    528  1.32     bjh21 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
    529  1.32     bjh21 		    host, hostlen);
    530  1.32     bjh21 	/*
    531  1.32     bjh21 	 * The following have zero-length addresses.
    532  1.32     bjh21 	 * IFT_ATM	(net/if_atmsubr.c)
    533  1.32     bjh21 	 * IFT_FAITH	(net/if_faith.c)
    534  1.32     bjh21 	 * IFT_GIF	(net/if_gif.c)
    535  1.32     bjh21 	 * IFT_LOOP	(net/if_loop.c)
    536  1.32     bjh21 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
    537  1.32     bjh21 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
    538  1.32     bjh21 	 * IFT_STF	(net/if_stf.c)
    539  1.32     bjh21 	 * IFT_L2VLAN	(net/if_vlan.c)
    540  1.32     bjh21 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
    541  1.32     bjh21 	 */
    542  1.32     bjh21 	/*
    543  1.32     bjh21 	 * The following use IPv4 addresses as link-layer addresses:
    544  1.32     bjh21 	 * IFT_OTHER	(net/if_gre.c)
    545  1.32     bjh21 	 */
    546  1.32     bjh21 	case IFT_ARCNET: /* default below is believed correct for all these. */
    547  1.32     bjh21 	case IFT_ETHER:
    548  1.32     bjh21 	case IFT_FDDI:
    549  1.32     bjh21 	case IFT_HIPPI:
    550  1.32     bjh21 	case IFT_ISO88025:
    551  1.32     bjh21 	default:
    552  1.42  christos 		return hexname((const u_int8_t *)CLLADDR(sdl),
    553  1.42  christos 		    (size_t)sdl->sdl_alen, host, hostlen);
    554  1.32     bjh21 	}
    555  1.32     bjh21 }
    556  1.32     bjh21 
    557  1.32     bjh21 static int
    558  1.32     bjh21 hexname(cp, len, host, hostlen)
    559  1.32     bjh21 	const u_int8_t *cp;
    560  1.32     bjh21 	char *host;
    561  1.36    kleink 	size_t len;
    562  1.36    kleink 	socklen_t hostlen;
    563  1.32     bjh21 {
    564  1.41   thorpej 	int n;
    565  1.41   thorpej 	size_t i;
    566  1.32     bjh21 	char *outp = host;
    567  1.32     bjh21 
    568  1.32     bjh21 	*outp = '\0';
    569  1.32     bjh21 	for (i = 0; i < len; i++) {
    570  1.32     bjh21 		n = snprintf(outp, hostlen, "%s%02x",
    571  1.32     bjh21 		    i ? ":" : "", cp[i]);
    572  1.41   thorpej 		if (n < 0 || (socklen_t) n >= hostlen) {
    573  1.32     bjh21 			*host = '\0';
    574  1.32     bjh21 			return EAI_MEMORY;
    575  1.32     bjh21 		}
    576  1.32     bjh21 		outp += n;
    577  1.32     bjh21 		hostlen -= n;
    578  1.32     bjh21 	}
    579  1.32     bjh21 	return 0;
    580  1.32     bjh21 }
    581