Home | History | Annotate | Line # | Download | only in net
getnameinfo.c revision 1.56
      1  1.56     joerg /*	$NetBSD: getnameinfo.c,v 1.56 2015/05/15 14:26:02 joerg 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.56     joerg __RCSID("$NetBSD: getnameinfo.c,v 1.56 2015/05/15 14:26:02 joerg Exp $");
     51  1.15    itojun #endif /* LIBC_SCCS and not lint */
     52   1.1    itojun 
     53  1.55     ozaki #ifndef RUMP_ACTION
     54  1.14    itojun #include "namespace.h"
     55  1.55     ozaki #endif
     56   1.1    itojun #include <sys/types.h>
     57   1.1    itojun #include <sys/socket.h>
     58  1.53  christos #include <sys/un.h>
     59   1.5    itojun #include <net/if.h>
     60  1.32     bjh21 #include <net/if_dl.h>
     61  1.32     bjh21 #include <net/if_ieee1394.h>
     62  1.32     bjh21 #include <net/if_types.h>
     63  1.48        is #include <netatalk/at.h>
     64   1.1    itojun #include <netinet/in.h>
     65   1.1    itojun #include <arpa/inet.h>
     66   1.1    itojun #include <arpa/nameser.h>
     67  1.24     lukem #include <assert.h>
     68  1.32     bjh21 #include <limits.h>
     69   1.1    itojun #include <netdb.h>
     70   1.1    itojun #include <resolv.h>
     71  1.24     lukem #include <stddef.h>
     72   1.1    itojun #include <string.h>
     73  1.14    itojun 
     74  1.44  christos #include "servent.h"
     75  1.54  christos #include "hostent.h"
     76  1.44  christos 
     77  1.56     joerg #ifndef RUMP_ACTION
     78  1.14    itojun #ifdef __weak_alias
     79  1.14    itojun __weak_alias(getnameinfo,_getnameinfo)
     80  1.14    itojun #endif
     81  1.56     joerg #endif
     82   1.1    itojun 
     83  1.25  jdolecek static const struct afd {
     84  1.27    kleink 	int		a_af;
     85  1.27    kleink 	socklen_t	a_addrlen;
     86  1.27    kleink 	socklen_t	a_socklen;
     87  1.27    kleink 	int		a_off;
     88   1.1    itojun } afdl [] = {
     89   1.1    itojun #ifdef INET6
     90   1.1    itojun 	{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
     91   1.1    itojun 		offsetof(struct sockaddr_in6, sin6_addr)},
     92   1.1    itojun #endif
     93   1.1    itojun 	{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
     94   1.1    itojun 		offsetof(struct sockaddr_in, sin_addr)},
     95  1.45  christos 	{0, 0, 0, 0},
     96   1.1    itojun };
     97   1.1    itojun 
     98   1.1    itojun struct sockinet {
     99   1.1    itojun 	u_char	si_len;
    100   1.1    itojun 	u_char	si_family;
    101   1.1    itojun 	u_short	si_port;
    102   1.1    itojun };
    103   1.1    itojun 
    104  1.52      matt static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
    105  1.52      matt     socklen_t, char *, socklen_t, int);
    106  1.10    itojun #ifdef INET6
    107  1.52      matt static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
    108  1.52      matt 				 socklen_t, int);
    109  1.52      matt static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
    110  1.18    itojun #endif
    111  1.52      matt static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *,
    112  1.52      matt     socklen_t, char *, socklen_t, int);
    113  1.53  christos static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
    114  1.53  christos     socklen_t, char *, socklen_t, int);
    115  1.48        is 
    116  1.52      matt static int getnameinfo_link(const struct sockaddr *, socklen_t, char *,
    117  1.52      matt     socklen_t, char *, socklen_t, int);
    118  1.52      matt static int hexname(const uint8_t *, size_t, char *, socklen_t);
    119  1.10    itojun 
    120  1.32     bjh21 /*
    121  1.32     bjh21  * Top-level getnameinfo() code.  Look at the address family, and pick an
    122  1.32     bjh21  * appropriate function to call.
    123  1.32     bjh21  */
    124   1.1    itojun int
    125  1.52      matt getnameinfo(const struct sockaddr *sa, socklen_t salen,
    126  1.52      matt 	char *host, socklen_t hostlen,
    127  1.52      matt 	char *serv, socklen_t servlen,
    128  1.52      matt 	int flags)
    129  1.32     bjh21 {
    130  1.32     bjh21 
    131  1.32     bjh21 	switch (sa->sa_family) {
    132  1.48        is 	case AF_APPLETALK:
    133  1.48        is 		return getnameinfo_atalk(sa, salen, host, hostlen,
    134  1.48        is 		    serv, servlen, flags);
    135  1.32     bjh21 	case AF_INET:
    136  1.32     bjh21 	case AF_INET6:
    137  1.32     bjh21 		return getnameinfo_inet(sa, salen, host, hostlen,
    138  1.32     bjh21 		    serv, servlen, flags);
    139  1.32     bjh21 	case AF_LINK:
    140  1.32     bjh21 		return getnameinfo_link(sa, salen, host, hostlen,
    141  1.32     bjh21 		    serv, servlen, flags);
    142  1.53  christos 	case AF_LOCAL:
    143  1.53  christos 		return getnameinfo_local(sa, salen, host, hostlen,
    144  1.53  christos 		    serv, servlen, flags);
    145  1.32     bjh21 	default:
    146  1.32     bjh21 		return EAI_FAMILY;
    147  1.32     bjh21 	}
    148  1.35  sommerfe }
    149  1.32     bjh21 
    150  1.48        is /*
    151  1.48        is  * getnameinfo_atalk():
    152  1.48        is  * Format an AppleTalk address into a printable format.
    153  1.48        is  */
    154  1.48        is /* ARGSUSED */
    155  1.48        is static int
    156  1.48        is getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen,
    157  1.48        is     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    158  1.48        is     int flags)
    159  1.48        is {
    160  1.48        is 	char numserv[8];
    161  1.49        is 	int n, m=0;
    162  1.48        is 
    163  1.48        is 	const struct sockaddr_at *sat =
    164  1.48        is 	    (const struct sockaddr_at *)(const void *)sa;
    165  1.48        is 
    166  1.48        is 	if (serv != NULL && servlen > 0) {
    167  1.48        is 		snprintf(numserv, sizeof(numserv), "%u", sat->sat_port);
    168  1.48        is 		if (strlen(numserv) + 1 > servlen)
    169  1.48        is 			return EAI_MEMORY;
    170  1.48        is 		strlcpy(serv, numserv, servlen);
    171  1.48        is 	}
    172  1.48        is 
    173  1.49        is         n = snprintf(host, hostlen, "%u.%u",
    174  1.49        is 	    ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
    175  1.49        is 
    176  1.49        is 	if (n < 0 || (socklen_t)(m+n) >= hostlen)
    177  1.49        is 		goto errout;
    178  1.49        is 
    179  1.49        is 	m += n;
    180  1.49        is 
    181  1.48        is 	if (sat->sat_range.r_netrange.nr_phase) {
    182  1.49        is         	n = snprintf(host+m, hostlen-m, " phase %u",
    183  1.48        is 			sat->sat_range.r_netrange.nr_phase);
    184  1.49        is 
    185  1.49        is 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
    186  1.49        is 			goto errout;
    187  1.49        is 
    188  1.49        is 		m += n;
    189  1.48        is 	}
    190  1.49        is 	if (sat->sat_range.r_netrange.nr_firstnet) {
    191  1.49        is         	n = snprintf(host+m, hostlen-m, " range %u - %u",
    192  1.49        is 			ntohs(sat->sat_range.r_netrange.nr_firstnet),
    193  1.49        is 			ntohs(sat->sat_range.r_netrange.nr_lastnet ));
    194  1.49        is 
    195  1.49        is 		if (n < 0 || (socklen_t)(m+n) >= hostlen)
    196  1.49        is 			goto errout;
    197  1.48        is 
    198  1.49        is 		m += n;
    199  1.48        is 	}
    200  1.49        is 
    201  1.48        is 	return 0;
    202  1.49        is 
    203  1.49        is errout:
    204  1.49        is 	if (host && hostlen>0)
    205  1.49        is 		host[m] = '\0';	/* XXX ??? */
    206  1.49        is 
    207  1.49        is 	return EAI_MEMORY;
    208  1.48        is }
    209  1.32     bjh21 
    210  1.32     bjh21 /*
    211  1.53  christos  * getnameinfo_local():
    212  1.53  christos  * Format an local address into a printable format.
    213  1.53  christos  */
    214  1.53  christos /* ARGSUSED */
    215  1.53  christos static int
    216  1.53  christos getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
    217  1.53  christos     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    218  1.53  christos     int flags)
    219  1.53  christos {
    220  1.53  christos 	const struct sockaddr_un *sun =
    221  1.53  christos 	    (const struct sockaddr_un *)(const void *)sa;
    222  1.53  christos 
    223  1.53  christos 	if (serv != NULL && servlen > 0)
    224  1.53  christos 		serv[0] = '\0';
    225  1.53  christos 
    226  1.53  christos 	if (host && hostlen > 0)
    227  1.53  christos 		strlcpy(host, sun->sun_path,
    228  1.53  christos 		    MIN(sizeof(sun->sun_path) + 1, hostlen));
    229  1.53  christos 
    230  1.53  christos 	return 0;
    231  1.53  christos }
    232  1.53  christos 
    233  1.53  christos /*
    234  1.32     bjh21  * getnameinfo_inet():
    235  1.32     bjh21  * Format an IPv4 or IPv6 sockaddr into a printable string.
    236  1.32     bjh21  */
    237  1.32     bjh21 static int
    238  1.52      matt getnameinfo_inet(const struct sockaddr *sa, socklen_t salen,
    239  1.52      matt 	char *host, socklen_t hostlen,
    240  1.52      matt 	char *serv, socklen_t servlen,
    241  1.52      matt 	int flags)
    242   1.1    itojun {
    243  1.25  jdolecek 	const struct afd *afd;
    244   1.1    itojun 	struct servent *sp;
    245   1.1    itojun 	struct hostent *hp;
    246   1.1    itojun 	u_short port;
    247   1.5    itojun 	int family, i;
    248  1.16    itojun 	const char *addr;
    249  1.52      matt 	uint32_t v4a;
    250   1.1    itojun 	char numserv[512];
    251   1.1    itojun 	char numaddr[512];
    252   1.1    itojun 
    253  1.24     lukem 	/* sa is checked below */
    254  1.24     lukem 	/* host may be NULL */
    255  1.24     lukem 	/* serv may be NULL */
    256  1.24     lukem 
    257   1.1    itojun 	if (sa == NULL)
    258  1.33    itojun 		return EAI_FAIL;
    259   1.1    itojun 
    260   1.1    itojun 	family = sa->sa_family;
    261   1.1    itojun 	for (i = 0; afdl[i].a_af; i++)
    262   1.1    itojun 		if (afdl[i].a_af == family) {
    263   1.1    itojun 			afd = &afdl[i];
    264   1.1    itojun 			goto found;
    265   1.1    itojun 		}
    266  1.33    itojun 	return EAI_FAMILY;
    267  1.33    itojun 
    268   1.1    itojun  found:
    269   1.5    itojun 	if (salen != afd->a_socklen)
    270  1.33    itojun 		return EAI_FAIL;
    271  1.33    itojun 
    272  1.16    itojun 	/* network byte order */
    273  1.20  christos 	port = ((const struct sockinet *)(const void *)sa)->si_port;
    274  1.20  christos 	addr = (const char *)(const void *)sa + afd->a_off;
    275   1.1    itojun 
    276   1.1    itojun 	if (serv == NULL || servlen == 0) {
    277   1.5    itojun 		/*
    278   1.5    itojun 		 * do nothing in this case.
    279   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    280  1.33    itojun 		 * "||" here: rfc2553bis-03 says that serv == NULL OR
    281  1.33    itojun 		 * servlen == 0 means that the caller does not want the result.
    282   1.5    itojun 		 */
    283   1.1    itojun 	} else {
    284  1.47     seanb 		struct servent_data svd;
    285  1.47     seanb 		struct servent sv;
    286  1.47     seanb 
    287   1.5    itojun 		if (flags & NI_NUMERICSERV)
    288   1.5    itojun 			sp = NULL;
    289   1.5    itojun 		else {
    290  1.44  christos 			(void)memset(&svd, 0, sizeof(svd));
    291  1.44  christos 			sp = getservbyport_r(port,
    292  1.44  christos 				(flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd);
    293   1.5    itojun 		}
    294   1.1    itojun 		if (sp) {
    295  1.47     seanb 			if (strlen(sp->s_name) + 1 > servlen) {
    296  1.47     seanb 				endservent_r(&svd);
    297  1.33    itojun 				return EAI_MEMORY;
    298  1.47     seanb 			}
    299  1.38    itojun 			strlcpy(serv, sp->s_name, servlen);
    300  1.47     seanb 			endservent_r(&svd);
    301   1.5    itojun 		} else {
    302  1.40    itojun 			snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
    303  1.22    itojun 			if (strlen(numserv) + 1 > servlen)
    304  1.33    itojun 				return EAI_MEMORY;
    305  1.38    itojun 			strlcpy(serv, numserv, servlen);
    306   1.5    itojun 		}
    307   1.1    itojun 	}
    308   1.1    itojun 
    309   1.1    itojun 	switch (sa->sa_family) {
    310   1.1    itojun 	case AF_INET:
    311  1.52      matt 		v4a = (uint32_t)
    312  1.20  christos 		    ntohl(((const struct sockaddr_in *)
    313  1.20  christos 		    (const void *)sa)->sin_addr.s_addr);
    314   1.1    itojun 		if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
    315   1.1    itojun 			flags |= NI_NUMERICHOST;
    316   1.1    itojun 		v4a >>= IN_CLASSA_NSHIFT;
    317   1.7    itojun 		if (v4a == 0)
    318  1.35  sommerfe 			flags |= NI_NUMERICHOST;
    319   1.1    itojun 		break;
    320   1.1    itojun #ifdef INET6
    321   1.1    itojun 	case AF_INET6:
    322   1.3    itojun 	    {
    323  1.16    itojun 		const struct sockaddr_in6 *sin6;
    324  1.20  christos 		sin6 = (const struct sockaddr_in6 *)(const void *)sa;
    325   1.5    itojun 		switch (sin6->sin6_addr.s6_addr[0]) {
    326   1.3    itojun 		case 0x00:
    327   1.3    itojun 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
    328   1.3    itojun 				;
    329   1.3    itojun 			else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
    330   1.3    itojun 				;
    331   1.3    itojun 			else
    332   1.3    itojun 				flags |= NI_NUMERICHOST;
    333   1.3    itojun 			break;
    334   1.3    itojun 		default:
    335   1.5    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
    336   1.3    itojun 				flags |= NI_NUMERICHOST;
    337   1.5    itojun 			}
    338   1.3    itojun 			else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
    339   1.3    itojun 				flags |= NI_NUMERICHOST;
    340   1.3    itojun 			break;
    341   1.3    itojun 		}
    342   1.3    itojun 	    }
    343   1.1    itojun 		break;
    344   1.1    itojun #endif
    345   1.1    itojun 	}
    346   1.1    itojun 	if (host == NULL || hostlen == 0) {
    347   1.5    itojun 		/*
    348   1.5    itojun 		 * do nothing in this case.
    349   1.5    itojun 		 * in case you are wondering if "&&" is more correct than
    350  1.33    itojun 		 * "||" here: rfc2553bis-03 says that host == NULL or
    351  1.33    itojun 		 * hostlen == 0 means that the caller does not want the result.
    352   1.5    itojun 		 */
    353   1.1    itojun 	} else if (flags & NI_NUMERICHOST) {
    354  1.41   thorpej 		size_t numaddrlen;
    355  1.10    itojun 
    356   1.3    itojun 		/* NUMERICHOST and NAMEREQD conflicts with each other */
    357   1.3    itojun 		if (flags & NI_NAMEREQD)
    358  1.33    itojun 			return EAI_NONAME;
    359   1.5    itojun 
    360  1.16    itojun 		switch(afd->a_af) {
    361  1.16    itojun #ifdef INET6
    362  1.16    itojun 		case AF_INET6:
    363  1.16    itojun 		{
    364  1.16    itojun 			int error;
    365  1.16    itojun 
    366  1.16    itojun 			if ((error = ip6_parsenumeric(sa, addr, host,
    367  1.16    itojun 						      hostlen, flags)) != 0)
    368  1.16    itojun 				return(error);
    369  1.16    itojun 			break;
    370  1.16    itojun 		}
    371  1.11    itojun #endif
    372  1.16    itojun 		default:
    373  1.51  christos 			if (inet_ntop(afd->a_af, addr, numaddr,
    374  1.51  christos 			    (socklen_t)sizeof(numaddr)) == NULL)
    375  1.33    itojun 				return EAI_SYSTEM;
    376  1.16    itojun 			numaddrlen = strlen(numaddr);
    377  1.16    itojun 			if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    378  1.33    itojun 				return EAI_MEMORY;
    379  1.38    itojun 			strlcpy(host, numaddr, hostlen);
    380  1.16    itojun 			break;
    381   1.5    itojun 		}
    382   1.1    itojun 	} else {
    383  1.54  christos 		struct hostent hent;
    384  1.54  christos 		char hbuf[4096];
    385  1.54  christos 		int he;
    386  1.54  christos 		hp = gethostbyaddr_r(addr, afd->a_addrlen, afd->a_af, &hent,
    387  1.54  christos 		    hbuf, sizeof(hbuf), &he);
    388   1.1    itojun 
    389   1.1    itojun 		if (hp) {
    390  1.16    itojun #if 0
    391  1.16    itojun 			/*
    392  1.16    itojun 			 * commented out, since "for local host" is not
    393  1.16    itojun 			 * implemented here - see RFC2553 p30
    394  1.16    itojun 			 */
    395   1.1    itojun 			if (flags & NI_NOFQDN) {
    396  1.16    itojun 				char *p;
    397   1.1    itojun 				p = strchr(hp->h_name, '.');
    398  1.16    itojun 				if (p)
    399  1.16    itojun 					*p = '\0';
    400   1.1    itojun 			}
    401  1.16    itojun #endif
    402  1.22    itojun 			if (strlen(hp->h_name) + 1 > hostlen) {
    403  1.33    itojun 				return EAI_MEMORY;
    404   1.1    itojun 			}
    405  1.38    itojun 			strlcpy(host, hp->h_name, hostlen);
    406   1.1    itojun 		} else {
    407   1.1    itojun 			if (flags & NI_NAMEREQD)
    408  1.33    itojun 				return EAI_NONAME;
    409  1.16    itojun 			switch(afd->a_af) {
    410  1.16    itojun #ifdef INET6
    411  1.16    itojun 			case AF_INET6:
    412  1.16    itojun 			{
    413  1.16    itojun 				int error;
    414  1.16    itojun 
    415  1.16    itojun 				if ((error = ip6_parsenumeric(sa, addr, host,
    416  1.16    itojun 							      hostlen,
    417  1.16    itojun 							      flags)) != 0)
    418  1.16    itojun 					return(error);
    419  1.16    itojun 				break;
    420  1.16    itojun 			}
    421  1.16    itojun #endif
    422  1.16    itojun 			default:
    423  1.16    itojun 				if (inet_ntop(afd->a_af, addr, host,
    424  1.16    itojun 				    hostlen) == NULL)
    425  1.33    itojun 					return EAI_SYSTEM;
    426  1.16    itojun 				break;
    427  1.16    itojun 			}
    428   1.1    itojun 		}
    429   1.1    itojun 	}
    430  1.33    itojun 	return(0);
    431   1.1    itojun }
    432  1.10    itojun 
    433  1.10    itojun #ifdef INET6
    434  1.16    itojun static int
    435  1.52      matt ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
    436  1.52      matt 	socklen_t hostlen, int flags)
    437  1.16    itojun {
    438  1.41   thorpej 	size_t numaddrlen;
    439  1.16    itojun 	char numaddr[512];
    440  1.16    itojun 
    441  1.24     lukem 	_DIAGASSERT(sa != NULL);
    442  1.24     lukem 	_DIAGASSERT(addr != NULL);
    443  1.24     lukem 	_DIAGASSERT(host != NULL);
    444  1.24     lukem 
    445  1.51  christos 	if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr))
    446  1.51  christos 	    == NULL)
    447  1.33    itojun 		return EAI_SYSTEM;
    448  1.16    itojun 
    449  1.16    itojun 	numaddrlen = strlen(numaddr);
    450  1.16    itojun 	if (numaddrlen + 1 > hostlen) /* don't forget terminator */
    451  1.43  ginsbach 		return EAI_OVERFLOW;
    452  1.38    itojun 	strlcpy(host, numaddr, hostlen);
    453  1.16    itojun 
    454  1.20  christos 	if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
    455  1.34    itojun 		char zonebuf[MAXHOSTNAMELEN];
    456  1.34    itojun 		int zonelen;
    457  1.16    itojun 
    458  1.34    itojun 		zonelen = ip6_sa2str(
    459  1.34    itojun 		    (const struct sockaddr_in6 *)(const void *)sa,
    460  1.34    itojun 		    zonebuf, sizeof(zonebuf), flags);
    461  1.34    itojun 		if (zonelen < 0)
    462  1.43  ginsbach 			return EAI_OVERFLOW;
    463  1.41   thorpej 		if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen)
    464  1.43  ginsbach 			return EAI_OVERFLOW;
    465  1.34    itojun 		/* construct <numeric-addr><delim><zoneid> */
    466  1.34    itojun 		memcpy(host + numaddrlen + 1, zonebuf,
    467  1.34    itojun 		    (size_t)zonelen);
    468  1.34    itojun 		host[numaddrlen] = SCOPE_DELIMITER;
    469  1.34    itojun 		host[numaddrlen + 1 + zonelen] = '\0';
    470  1.16    itojun 	}
    471  1.16    itojun 
    472  1.16    itojun 	return 0;
    473  1.16    itojun }
    474  1.16    itojun 
    475  1.10    itojun /* ARGSUSED */
    476  1.16    itojun static int
    477  1.52      matt ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
    478  1.10    itojun {
    479  1.24     lukem 	unsigned int ifindex;
    480  1.24     lukem 	const struct in6_addr *a6;
    481  1.28    itojun 	int n;
    482  1.24     lukem 
    483  1.24     lukem 	_DIAGASSERT(sa6 != NULL);
    484  1.24     lukem 	_DIAGASSERT(buf != NULL);
    485  1.35  sommerfe 
    486  1.24     lukem 	ifindex = (unsigned int)sa6->sin6_scope_id;
    487  1.24     lukem 	a6 = &sa6->sin6_addr;
    488  1.10    itojun 
    489  1.28    itojun #ifdef NI_NUMERICSCOPE
    490  1.28    itojun 	if ((flags & NI_NUMERICSCOPE) != 0) {
    491  1.28    itojun 		n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    492  1.46     lukem 		if (n < 0 || (size_t)n >= bufsiz)
    493  1.28    itojun 			return -1;
    494  1.28    itojun 		else
    495  1.28    itojun 			return n;
    496  1.10    itojun 	}
    497  1.10    itojun #endif
    498  1.18    itojun 
    499  1.16    itojun 	/* if_indextoname() does not take buffer size.  not a good api... */
    500  1.16    itojun 	if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
    501  1.16    itojun 	    bufsiz >= IF_NAMESIZE) {
    502  1.16    itojun 		char *p = if_indextoname(ifindex, buf);
    503  1.16    itojun 		if (p) {
    504  1.51  christos 			return (int)strlen(p);
    505  1.16    itojun 		}
    506  1.16    itojun 	}
    507  1.10    itojun 
    508  1.16    itojun 	/* last resort */
    509  1.28    itojun 	n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
    510  1.41   thorpej 	if (n < 0 || (size_t) n >= bufsiz)
    511  1.28    itojun 		return -1;
    512  1.28    itojun 	else
    513  1.28    itojun 		return n;
    514  1.10    itojun }
    515  1.16    itojun #endif /* INET6 */
    516  1.32     bjh21 
    517  1.32     bjh21 
    518  1.32     bjh21 /*
    519  1.32     bjh21  * getnameinfo_link():
    520  1.32     bjh21  * Format a link-layer address into a printable format, paying attention to
    521  1.32     bjh21  * the interface type.
    522  1.32     bjh21  */
    523  1.32     bjh21 /* ARGSUSED */
    524  1.32     bjh21 static int
    525  1.32     bjh21 getnameinfo_link(const struct sockaddr *sa, socklen_t salen,
    526  1.37    kleink     char *host, socklen_t hostlen, char *serv, socklen_t servlen,
    527  1.39    kleink     int flags)
    528  1.32     bjh21 {
    529  1.32     bjh21 	const struct sockaddr_dl *sdl =
    530  1.32     bjh21 	    (const struct sockaddr_dl *)(const void *)sa;
    531  1.32     bjh21 	const struct ieee1394_hwaddr *iha;
    532  1.32     bjh21 	int n;
    533  1.32     bjh21 
    534  1.32     bjh21 	if (serv != NULL && servlen > 0)
    535  1.32     bjh21 		*serv = '\0';
    536  1.32     bjh21 
    537  1.32     bjh21 	if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) {
    538  1.40    itojun 		n = snprintf(host, hostlen, "link#%u", sdl->sdl_index);
    539  1.41   thorpej 		if (n < 0 || (socklen_t) n > hostlen) {
    540  1.32     bjh21 			*host = '\0';
    541  1.32     bjh21 			return EAI_MEMORY;
    542  1.32     bjh21 		}
    543  1.32     bjh21 		return 0;
    544  1.32     bjh21 	}
    545  1.32     bjh21 
    546  1.32     bjh21 	switch (sdl->sdl_type) {
    547  1.32     bjh21 #ifdef IFT_ECONET
    548  1.32     bjh21 	case IFT_ECONET:
    549  1.32     bjh21 		if (sdl->sdl_alen < 2)
    550  1.32     bjh21 			return EAI_FAMILY;
    551  1.42  christos 		if (CLLADDR(sdl)[1] == 0)
    552  1.42  christos 			n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]);
    553  1.32     bjh21 		else
    554  1.40    itojun 			n = snprintf(host, hostlen, "%u.%u",
    555  1.42  christos 			    CLLADDR(sdl)[1], CLLADDR(sdl)[0]);
    556  1.41   thorpej 		if (n < 0 || (socklen_t) n >= hostlen) {
    557  1.32     bjh21 			*host = '\0';
    558  1.32     bjh21 			return EAI_MEMORY;
    559  1.32     bjh21 		} else
    560  1.32     bjh21 			return 0;
    561  1.32     bjh21 #endif
    562  1.32     bjh21 	case IFT_IEEE1394:
    563  1.32     bjh21 		if (sdl->sdl_alen < sizeof(iha->iha_uid))
    564  1.32     bjh21 			return EAI_FAMILY;
    565  1.32     bjh21 		iha =
    566  1.42  christos 		    (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl);
    567  1.32     bjh21 		return hexname(iha->iha_uid, sizeof(iha->iha_uid),
    568  1.32     bjh21 		    host, hostlen);
    569  1.32     bjh21 	/*
    570  1.32     bjh21 	 * The following have zero-length addresses.
    571  1.32     bjh21 	 * IFT_ATM	(net/if_atmsubr.c)
    572  1.32     bjh21 	 * IFT_FAITH	(net/if_faith.c)
    573  1.32     bjh21 	 * IFT_GIF	(net/if_gif.c)
    574  1.32     bjh21 	 * IFT_LOOP	(net/if_loop.c)
    575  1.32     bjh21 	 * IFT_PPP	(net/if_ppp.c, net/if_spppsubr.c)
    576  1.32     bjh21 	 * IFT_SLIP	(net/if_sl.c, net/if_strip.c)
    577  1.32     bjh21 	 * IFT_STF	(net/if_stf.c)
    578  1.32     bjh21 	 * IFT_L2VLAN	(net/if_vlan.c)
    579  1.32     bjh21 	 * IFT_PROPVIRTUAL (net/if_bridge.h>
    580  1.32     bjh21 	 */
    581  1.32     bjh21 	/*
    582  1.32     bjh21 	 * The following use IPv4 addresses as link-layer addresses:
    583  1.32     bjh21 	 * IFT_OTHER	(net/if_gre.c)
    584  1.32     bjh21 	 */
    585  1.32     bjh21 	case IFT_ARCNET: /* default below is believed correct for all these. */
    586  1.32     bjh21 	case IFT_ETHER:
    587  1.32     bjh21 	case IFT_FDDI:
    588  1.32     bjh21 	case IFT_HIPPI:
    589  1.32     bjh21 	case IFT_ISO88025:
    590  1.32     bjh21 	default:
    591  1.52      matt 		return hexname((const uint8_t *)CLLADDR(sdl),
    592  1.42  christos 		    (size_t)sdl->sdl_alen, host, hostlen);
    593  1.32     bjh21 	}
    594  1.32     bjh21 }
    595  1.32     bjh21 
    596  1.32     bjh21 static int
    597  1.52      matt hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen)
    598  1.32     bjh21 {
    599  1.41   thorpej 	int n;
    600  1.41   thorpej 	size_t i;
    601  1.32     bjh21 	char *outp = host;
    602  1.32     bjh21 
    603  1.32     bjh21 	*outp = '\0';
    604  1.32     bjh21 	for (i = 0; i < len; i++) {
    605  1.32     bjh21 		n = snprintf(outp, hostlen, "%s%02x",
    606  1.32     bjh21 		    i ? ":" : "", cp[i]);
    607  1.41   thorpej 		if (n < 0 || (socklen_t) n >= hostlen) {
    608  1.32     bjh21 			*host = '\0';
    609  1.32     bjh21 			return EAI_MEMORY;
    610  1.32     bjh21 		}
    611  1.32     bjh21 		outp += n;
    612  1.32     bjh21 		hostlen -= n;
    613  1.32     bjh21 	}
    614  1.32     bjh21 	return 0;
    615  1.32     bjh21 }
    616