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