blob: 4874da12d9475142f630ed519e60f84be4682e35 [file] [log] [blame]
Elliott Hughesd8213bb2013-02-13 09:49:33 -08001/* $NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $ */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002/* $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)
Elliott Hughesd8213bb2013-02-13 09:49:33 -080050__RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $");
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051#endif /* LIBC_SCCS and not lint */
52
53#include <sys/types.h>
54#include <sys/socket.h>
Elliott Hughesd8213bb2013-02-13 09:49:33 -080055#include <sys/un.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include <net/if.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057#include <net/if_ieee1394.h>
58#include <net/if_types.h>
59#include <netinet/in.h>
60#include <arpa/inet.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061#include <assert.h>
62#include <limits.h>
63#include <netdb.h>
64#ifdef ANDROID_CHANGES
Elliott Hughesd8213bb2013-02-13 09:49:33 -080065#include "arpa_nameser.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066#include "resolv_private.h"
Mattias Falk149f7df2011-02-15 08:45:26 +010067#include <sys/system_properties.h>
68#include <stdlib.h>
69#include <unistd.h>
Mattias Falk149f7df2011-02-15 08:45:26 +010070#include <errno.h>
Elliott Hughesd8213bb2013-02-13 09:49:33 -080071#define MIN(x,y) ((x) < (y) ? (x) : (y))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#else
73#include <resolv.h>
74#endif
75#include <stddef.h>
76#include <string.h>
77
78static const struct afd {
79 int a_af;
80 socklen_t a_addrlen;
81 socklen_t a_socklen;
82 int a_off;
83} afdl [] = {
84#ifdef INET6
85 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
86 offsetof(struct sockaddr_in6, sin6_addr)},
87#endif
88 {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
89 offsetof(struct sockaddr_in, sin_addr)},
90 {0, 0, 0, 0},
91};
92
93struct sockinet {
94 u_char si_len;
95 u_char si_family;
96 u_short si_port;
97};
98
Elliott Hughesd8213bb2013-02-13 09:49:33 -080099static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *,
100 socklen_t, char *, socklen_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101#ifdef INET6
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800102static int ip6_parsenumeric(const struct sockaddr *, const char *, char *,
103 socklen_t, int);
104static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105#endif
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800106static int getnameinfo_local(const struct sockaddr *, socklen_t, char *,
107 socklen_t, char *, socklen_t, int);
Selim Gurun06e18312012-02-27 15:58:54 -0800108
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109/*
110 * Top-level getnameinfo() code. Look at the address family, and pick an
111 * appropriate function to call.
112 */
113int getnameinfo(const struct sockaddr* sa, socklen_t salen, char* host, size_t hostlen, char* serv, size_t servlen, int flags)
114{
115 switch (sa->sa_family) {
116 case AF_INET:
117 case AF_INET6:
118 return getnameinfo_inet(sa, salen, host, hostlen,
119 serv, servlen, flags);
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800120 case AF_LOCAL:
121 return getnameinfo_local(sa, salen, host, hostlen,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122 serv, servlen, flags);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123 default:
124 return EAI_FAMILY;
125 }
126}
127
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800128/*
129 * getnameinfo_local():
130 * Format an local address into a printable format.
131 */
132/* ARGSUSED */
133static int
134getnameinfo_local(const struct sockaddr *sa, socklen_t salen,
135 char *host, socklen_t hostlen, char *serv, socklen_t servlen,
136 int flags __attribute__((unused)))
137{
138 const struct sockaddr_un *sun =
139 (const struct sockaddr_un *)(const void *)sa;
140
141 if (salen < (socklen_t) offsetof(struct sockaddr_un, sun_path)) {
142 return EAI_FAMILY;
143 }
144
145 if (serv != NULL && servlen > 0)
146 serv[0] = '\0';
147
148 if (host && hostlen > 0)
149 strlcpy(host, sun->sun_path,
150 MIN((socklen_t) sizeof(sun->sun_path) + 1, hostlen));
151
152 return 0;
153}
154
Mattias Falk149f7df2011-02-15 08:45:26 +0100155#ifdef ANDROID_CHANGES
156/* On success length of the host name is returned. A return
157 * value of 0 means there's no host name associated with
158 * the address. On failure -1 is returned in which case
159 * normal execution flow shall continue. */
160static int
Selim Gurun06e18312012-02-27 15:58:54 -0800161android_gethostbyaddr_proxy(char* nameBuf, size_t nameBufLen, const void *addr, socklen_t addrLen, int addrFamily) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
Mattias Falk149f7df2011-02-15 08:45:26 +0100163 int sock;
164 const int one = 1;
165 struct sockaddr_un proxy_addr;
166 const char* cache_mode = getenv("ANDROID_DNS_MODE");
167 FILE* proxy = NULL;
168 int result = -1;
169
170 if (cache_mode != NULL && strcmp(cache_mode, "local") == 0) {
171 // Don't use the proxy in local mode. This is used by the
172 // proxy itself.
173 return -1;
174 }
175
176 // Temporary cautious hack to disable the DNS proxy for processes
177 // requesting special treatment. Ideally the DNS proxy should
178 // accomodate these apps, though.
179 char propname[PROP_NAME_MAX];
180 char propvalue[PROP_VALUE_MAX];
181 snprintf(propname, sizeof(propname), "net.dns1.%d", getpid());
182 if (__system_property_get(propname, propvalue) > 0) {
183 return -1;
184 }
185 // create socket
186 sock = socket(AF_UNIX, SOCK_STREAM, 0);
187 if (sock < 0) {
188 return -1;
189 }
190
191 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
192 memset(&proxy_addr, 0, sizeof(proxy_addr));
193 proxy_addr.sun_family = AF_UNIX;
194 strlcpy(proxy_addr.sun_path, "/dev/socket/dnsproxyd",
195 sizeof(proxy_addr.sun_path));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700196 if (TEMP_FAILURE_RETRY(connect(sock, (const struct sockaddr*) (void*) &proxy_addr,
Mattias Falk149f7df2011-02-15 08:45:26 +0100197 sizeof(proxy_addr))) != 0) {
198 close(sock);
199 return -1;
200 }
201
202 // send request to DnsProxyListener
203 proxy = fdopen(sock,"r+");
204 if (proxy == NULL) {
205 goto exit;
206 }
207
Brian Carlstromc13fa642011-04-27 11:55:43 -0700208 char buf[INET6_ADDRSTRLEN]; // big enough for IPv4 and IPv6
Selim Gurun06e18312012-02-27 15:58:54 -0800209 const char* addrStr = inet_ntop(addrFamily, addr, buf, sizeof(buf));
Brian Carlstromc13fa642011-04-27 11:55:43 -0700210 if (addrStr == NULL) {
211 goto exit;
212 }
Kenny Root1fb66622012-02-24 11:04:42 -0800213 if (fprintf(proxy, "gethostbyaddr %s %d %d", addrStr, addrLen, addrFamily) < 0) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100214 goto exit;
215 }
216
217 // literal NULL byte at end, required by FrameworkListener
218 if (fputc(0, proxy) == EOF || fflush(proxy) != 0) {
219 goto exit;
220 }
221
222 result = 0;
Robert Greenwaltc59ba452012-03-09 11:34:27 -0800223 char msg_buf[4];
Selim Gurun06e18312012-02-27 15:58:54 -0800224 // read result code for gethostbyaddr
225 if (fread(msg_buf, 1, sizeof(msg_buf), proxy) != sizeof(msg_buf)) {
226 goto exit;
227 }
228
229 int result_code = (int)strtol(msg_buf, NULL, 10);
230 // verify the code itself
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800231 // This should be synchronized to ResponseCode.h
232 static const int DnsProxyQueryResult = 222;
Selim Gurun06e18312012-02-27 15:58:54 -0800233 if (result_code != DnsProxyQueryResult) {
234 goto exit;
235 }
236
Mattias Falk149f7df2011-02-15 08:45:26 +0100237 uint32_t name_len;
238 if (fread(&name_len, sizeof(name_len), 1, proxy) != 1) {
239 goto exit;
240 }
241
242 name_len = ntohl(name_len);
Selim Gurun06e18312012-02-27 15:58:54 -0800243 if (name_len <= 0 || name_len >= nameBufLen) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100244 goto exit;
245 }
246
Selim Gurun06e18312012-02-27 15:58:54 -0800247 if (fread(nameBuf, name_len, 1, proxy) != 1) {
Mattias Falk149f7df2011-02-15 08:45:26 +0100248 goto exit;
249 }
250
251 result = name_len;
252
253 exit:
254 if (proxy != NULL) {
255 fclose(proxy);
256 }
257
258 return result;
259}
260#endif
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800261
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800262/*
263 * getnameinfo_inet():
264 * Format an IPv4 or IPv6 sockaddr into a printable string.
265 */
266static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800267getnameinfo_inet(const struct sockaddr* sa, socklen_t salen,
268 char *host, socklen_t hostlen,
269 char *serv, socklen_t servlen,
270 int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271{
272 const struct afd *afd;
273 struct servent *sp;
274 struct hostent *hp;
275 u_short port;
276 int family, i;
277 const char *addr;
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800278 uint32_t v4a;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800279 char numserv[512];
280 char numaddr[512];
281
282 /* sa is checked below */
283 /* host may be NULL */
284 /* serv may be NULL */
285
286 if (sa == NULL)
287 return EAI_FAIL;
288
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289 family = sa->sa_family;
290 for (i = 0; afdl[i].a_af; i++)
291 if (afdl[i].a_af == family) {
292 afd = &afdl[i];
293 goto found;
294 }
295 return EAI_FAMILY;
296
297 found:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800298 // http://b/1889275: callers should be allowed to provide too much
299 // space, but not too little.
300 if (salen < afd->a_socklen) {
301 return EAI_FAMILY;
302 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303
304 /* network byte order */
305 port = ((const struct sockinet *)(const void *)sa)->si_port;
306 addr = (const char *)(const void *)sa + afd->a_off;
307
308 if (serv == NULL || servlen == 0) {
309 /*
310 * do nothing in this case.
311 * in case you are wondering if "&&" is more correct than
312 * "||" here: rfc2553bis-03 says that serv == NULL OR
313 * servlen == 0 means that the caller does not want the result.
314 */
315 } else {
316 if (flags & NI_NUMERICSERV)
317 sp = NULL;
318 else {
319 sp = getservbyport(port,
320 (flags & NI_DGRAM) ? "udp" : "tcp");
321 }
322 if (sp) {
323 if (strlen(sp->s_name) + 1 > (size_t)servlen)
324 return EAI_MEMORY;
325 strlcpy(serv, sp->s_name, servlen);
326 } else {
327 snprintf(numserv, sizeof(numserv), "%u", ntohs(port));
328 if (strlen(numserv) + 1 > (size_t)servlen)
329 return EAI_MEMORY;
330 strlcpy(serv, numserv, servlen);
331 }
332 }
333
334 switch (sa->sa_family) {
335 case AF_INET:
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800336 v4a = (uint32_t)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337 ntohl(((const struct sockaddr_in *)
338 (const void *)sa)->sin_addr.s_addr);
339 if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a))
340 flags |= NI_NUMERICHOST;
341 v4a >>= IN_CLASSA_NSHIFT;
342 if (v4a == 0)
343 flags |= NI_NUMERICHOST;
344 break;
345#ifdef INET6
346 case AF_INET6:
347 {
348 const struct sockaddr_in6 *sin6;
349 sin6 = (const struct sockaddr_in6 *)(const void *)sa;
350 switch (sin6->sin6_addr.s6_addr[0]) {
351 case 0x00:
352 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
353 ;
354 else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr))
355 ;
356 else
357 flags |= NI_NUMERICHOST;
358 break;
359 default:
360 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
361 flags |= NI_NUMERICHOST;
362 }
363 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
364 flags |= NI_NUMERICHOST;
365 break;
366 }
367 }
368 break;
369#endif
370 }
371 if (host == NULL || hostlen == 0) {
372 /*
373 * do nothing in this case.
374 * in case you are wondering if "&&" is more correct than
375 * "||" here: rfc2553bis-03 says that host == NULL or
376 * hostlen == 0 means that the caller does not want the result.
377 */
378 } else if (flags & NI_NUMERICHOST) {
379 size_t numaddrlen;
380
381 /* NUMERICHOST and NAMEREQD conflicts with each other */
382 if (flags & NI_NAMEREQD)
383 return EAI_NONAME;
384
385 switch(afd->a_af) {
386#ifdef INET6
387 case AF_INET6:
388 {
389 int error;
390
391 if ((error = ip6_parsenumeric(sa, addr, host,
392 hostlen, flags)) != 0)
393 return(error);
394 break;
395 }
396#endif
397 default:
398 if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr))
399 == NULL)
400 return EAI_SYSTEM;
401 numaddrlen = strlen(numaddr);
402 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
403 return EAI_MEMORY;
404 strlcpy(host, numaddr, hostlen);
405 break;
406 }
407 } else {
Mattias Falk149f7df2011-02-15 08:45:26 +0100408#ifdef ANDROID_CHANGES
409 struct hostent android_proxy_hostent;
410 char android_proxy_buf[MAXDNAME];
Mattias Falk149f7df2011-02-15 08:45:26 +0100411
Selim Gurun06e18312012-02-27 15:58:54 -0800412 int hostnamelen = android_gethostbyaddr_proxy(android_proxy_buf,
413 MAXDNAME, addr, afd->a_addrlen, afd->a_af);
414 if (hostnamelen > 0) {
415 hp = &android_proxy_hostent;
416 hp->h_name = android_proxy_buf;
417 } else if (!hostnamelen) {
418 hp = NULL;
Mattias Falk149f7df2011-02-15 08:45:26 +0100419 } else {
420 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
421 }
422#else
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af);
Mattias Falk149f7df2011-02-15 08:45:26 +0100424#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425
426 if (hp) {
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800427#if 0
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 /*
429 * commented out, since "for local host" is not
430 * implemented here - see RFC2553 p30
431 */
432 if (flags & NI_NOFQDN) {
433 char *p;
434 p = strchr(hp->h_name, '.');
435 if (p)
436 *p = '\0';
437 }
438#endif
439 if (strlen(hp->h_name) + 1 > (size_t)hostlen) {
440 return EAI_MEMORY;
441 }
442 strlcpy(host, hp->h_name, hostlen);
443 } else {
444 if (flags & NI_NAMEREQD)
445 return EAI_NONAME;
446 switch(afd->a_af) {
447#ifdef INET6
448 case AF_INET6:
449 {
450 int error;
451
452 if ((error = ip6_parsenumeric(sa, addr, host,
453 hostlen,
454 flags)) != 0)
455 return(error);
456 break;
457 }
458#endif
459 default:
460 if (inet_ntop(afd->a_af, addr, host,
461 hostlen) == NULL)
462 return EAI_SYSTEM;
463 break;
464 }
465 }
466 }
467 return(0);
468}
469
470#ifdef INET6
471static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800472ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host,
473 socklen_t hostlen, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800474{
475 size_t numaddrlen;
476 char numaddr[512];
477
478 assert(sa != NULL);
479 assert(addr != NULL);
480 assert(host != NULL);
481
482 if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL)
483 return EAI_SYSTEM;
484
485 numaddrlen = strlen(numaddr);
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700486 if (numaddrlen + 1 > (size_t)hostlen) /* don't forget terminator */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800487 return EAI_OVERFLOW;
488 strlcpy(host, numaddr, hostlen);
489
490 if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) {
491 char zonebuf[MAXHOSTNAMELEN];
492 int zonelen;
493
494 zonelen = ip6_sa2str(
495 (const struct sockaddr_in6 *)(const void *)sa,
496 zonebuf, sizeof(zonebuf), flags);
497 if (zonelen < 0)
498 return EAI_OVERFLOW;
David 'Digit' Turner50ace4f2010-06-16 16:36:41 -0700499 if ((size_t) zonelen + 1 + numaddrlen + 1 > (size_t)hostlen)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800500 return EAI_OVERFLOW;
501 /* construct <numeric-addr><delim><zoneid> */
502 memcpy(host + numaddrlen + 1, zonebuf,
503 (size_t)zonelen);
504 host[numaddrlen] = SCOPE_DELIMITER;
505 host[numaddrlen + 1 + zonelen] = '\0';
506 }
507
508 return 0;
509}
510
511/* ARGSUSED */
512static int
Elliott Hughesd8213bb2013-02-13 09:49:33 -0800513ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800514{
515 unsigned int ifindex;
516 const struct in6_addr *a6;
517 int n;
518
519 assert(sa6 != NULL);
520 assert(buf != NULL);
521
522 ifindex = (unsigned int)sa6->sin6_scope_id;
523 a6 = &sa6->sin6_addr;
524
525#ifdef NI_NUMERICSCOPE
526 if ((flags & NI_NUMERICSCOPE) != 0) {
527 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
528 if (n < 0 || n >= bufsiz)
529 return -1;
530 else
531 return n;
532 }
533#endif
534
535 /* if_indextoname() does not take buffer size. not a good api... */
536 if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) &&
537 bufsiz >= IF_NAMESIZE) {
538 char *p = if_indextoname(ifindex, buf);
539 if (p) {
540 return(strlen(p));
541 }
542 }
543
544 /* last resort */
545 n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id);
546 if (n < 0 || (size_t) n >= bufsiz)
547 return -1;
548 else
549 return n;
550}
551#endif /* INET6 */