2016-12-29 21:30:55 +03:00
|
|
|
/* $NetBSD: sockaddr_snprintf.c,v 1.14 2016/12/29 18:30:55 christos Exp $ */
|
2004-11-20 00:37:19 +03:00
|
|
|
|
|
|
|
/*-
|
2016-12-29 21:30:55 +03:00
|
|
|
* Copyright (c) 2004, 2016 The NetBSD Foundation, Inc.
|
2004-11-20 00:37:19 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2016-12-29 21:30:55 +03:00
|
|
|
__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.14 2016/12/29 18:30:55 christos Exp $");
|
2004-11-20 00:37:19 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
2016-12-29 21:30:55 +03:00
|
|
|
#include <sys/param.h>
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <netatalk/at.h>
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <net/if_dl.h>
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2004-11-20 00:37:19 +03:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-06-07 21:23:26 +04:00
|
|
|
#include <stdlib.h>
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_UTIL_H
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <util.h>
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBUTIL_H
|
|
|
|
#include <libutil.h>
|
|
|
|
#endif
|
2004-11-20 00:37:19 +03:00
|
|
|
#include <netdb.h>
|
|
|
|
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef BSD4_4
|
|
|
|
# define SALEN(sa) ((sa)->sa ## _len)
|
|
|
|
#else
|
|
|
|
# define SALEN(sa) ((unsigned)sizeof(*sa))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2013-06-07 21:23:26 +04:00
|
|
|
static int
|
|
|
|
debug_at(char *str, size_t len, const struct sockaddr_at *sat)
|
|
|
|
{
|
|
|
|
return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
|
|
|
|
"sat_addr.s_net=%u, sat_addr.s_node=%u, "
|
|
|
|
"sat_range.r_netrange.nr_phase=%u, "
|
|
|
|
"sat_range.r_netrange.nr_firstnet=%u, "
|
|
|
|
"sat_range.r_netrange.nr_lastnet=%u",
|
2016-12-29 21:30:55 +03:00
|
|
|
SALEN(sat), sat->sat_family, sat->sat_port,
|
2013-06-07 21:23:26 +04:00
|
|
|
sat->sat_addr.s_net, sat->sat_addr.s_node,
|
|
|
|
sat->sat_range.r_netrange.nr_phase,
|
|
|
|
sat->sat_range.r_netrange.nr_firstnet,
|
|
|
|
sat->sat_range.r_netrange.nr_lastnet);
|
|
|
|
}
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2013-06-07 21:23:26 +04:00
|
|
|
|
|
|
|
static int
|
|
|
|
debug_in(char *str, size_t len, const struct sockaddr_in *sin)
|
|
|
|
{
|
|
|
|
return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
|
|
|
|
"sin_addr.s_addr=%08x",
|
2016-12-29 21:30:55 +03:00
|
|
|
SALEN(sin), sin->sin_family, sin->sin_port,
|
2013-06-07 21:23:26 +04:00
|
|
|
sin->sin_addr.s_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
|
|
|
|
{
|
|
|
|
const uint8_t *s = sin6->sin6_addr.s6_addr;
|
|
|
|
|
|
|
|
return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
|
|
|
|
"sin6_flowinfo=%u, "
|
|
|
|
"sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
|
|
|
|
"%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
|
2016-12-29 21:30:55 +03:00
|
|
|
SALEN(sin6), sin6->sin6_family, sin6->sin6_port,
|
2013-06-07 21:23:26 +04:00
|
|
|
sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
|
|
|
|
s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
|
|
|
|
s[0xe], s[0xf], sin6->sin6_scope_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
debug_un(char *str, size_t len, const struct sockaddr_un *sun)
|
|
|
|
{
|
|
|
|
return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
|
2016-12-29 21:30:55 +03:00
|
|
|
SALEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
|
2013-06-07 21:23:26 +04:00
|
|
|
sun->sun_path);
|
|
|
|
}
|
|
|
|
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2013-06-07 21:23:26 +04:00
|
|
|
static int
|
|
|
|
debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
|
|
|
|
{
|
|
|
|
const uint8_t *s = (const void *)sdl->sdl_data;
|
|
|
|
|
|
|
|
return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
|
|
|
|
"sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
|
|
|
|
"%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
|
2016-12-29 21:30:55 +03:00
|
|
|
SALEN(sdl), sdl->sdl_family, sdl->sdl_index,
|
2013-06-07 21:23:26 +04:00
|
|
|
sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
|
|
|
|
s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
|
|
|
|
s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
|
|
|
|
}
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2013-06-07 21:23:26 +04:00
|
|
|
|
2004-11-20 00:37:19 +03:00
|
|
|
int
|
In sockaddr_snprintf(sbuf, len, fmt, sa), do not write the terminating
null character outside of the byte region [sbuf, sbuf + len).
1) If the length of the buffer is 0, do not write a null character
at all. Previously, sockaddr_snprintf() may have been able to
overwrite sbuf[-1] if len was 0.
2) If the length of the buffer, len, is greater than 0, then write
the null at sbuf[len - 1]. Previously, sockaddr_snprintf()
wrote the null at buf[len - 1], where `buf' was a "cursor" that
did not necessarily equal `sbuf', the start of the buffer. Now,
sockaddr_snprintf() always writes the null at sbuf[len - 1].
2007-07-24 12:45:45 +04:00
|
|
|
sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
|
|
|
|
const struct sockaddr * const sa)
|
2004-11-20 00:37:19 +03:00
|
|
|
{
|
|
|
|
const void *a = NULL;
|
2016-12-29 21:30:55 +03:00
|
|
|
char abuf[1024], nbuf[1024], *addr = NULL;
|
2005-04-09 06:05:47 +04:00
|
|
|
char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
|
In sockaddr_snprintf(sbuf, len, fmt, sa), do not write the terminating
null character outside of the byte region [sbuf, sbuf + len).
1) If the length of the buffer is 0, do not write a null character
at all. Previously, sockaddr_snprintf() may have been able to
overwrite sbuf[-1] if len was 0.
2) If the length of the buffer, len, is greater than 0, then write
the null at sbuf[len - 1]. Previously, sockaddr_snprintf()
wrote the null at buf[len - 1], where `buf' was a "cursor" that
did not necessarily equal `sbuf', the start of the buffer. Now,
sockaddr_snprintf() always writes the null at sbuf[len - 1].
2007-07-24 12:45:45 +04:00
|
|
|
char *ebuf = &sbuf[len - 1], *buf = sbuf;
|
2004-11-20 00:37:19 +03:00
|
|
|
const char *ptr, *s;
|
2016-12-29 21:30:55 +03:00
|
|
|
size_t salen;
|
2005-04-09 06:05:47 +04:00
|
|
|
int p = -1;
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2004-11-20 00:37:19 +03:00
|
|
|
const struct sockaddr_at *sat = NULL;
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2004-11-20 00:37:19 +03:00
|
|
|
const struct sockaddr_in *sin4 = NULL;
|
|
|
|
const struct sockaddr_in6 *sin6 = NULL;
|
|
|
|
const struct sockaddr_un *sun = NULL;
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2004-11-20 00:37:19 +03:00
|
|
|
const struct sockaddr_dl *sdl = NULL;
|
2016-12-29 21:30:55 +03:00
|
|
|
char *w = NULL;
|
|
|
|
#endif
|
2005-04-09 06:05:47 +04:00
|
|
|
int na = 1;
|
2004-11-20 00:37:19 +03:00
|
|
|
|
2005-04-09 06:05:47 +04:00
|
|
|
#define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
|
|
|
|
while (/*CONSTCOND*/0)
|
|
|
|
#define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
|
|
|
|
while (/*CONSTCOND*/0)
|
|
|
|
#define ADDNA() do { if (na) ADDS("N/A"); } \
|
|
|
|
while (/*CONSTCOND*/0)
|
2004-11-20 00:37:19 +03:00
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_UNSPEC:
|
|
|
|
goto done;
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2004-11-20 00:37:19 +03:00
|
|
|
case AF_APPLETALK:
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sizeof(*sat);
|
2004-11-20 00:37:19 +03:00
|
|
|
sat = ((const struct sockaddr_at *)(const void *)sa);
|
|
|
|
p = ntohs(sat->sat_port);
|
|
|
|
(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
|
|
|
|
ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
|
2005-04-09 06:05:47 +04:00
|
|
|
(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
|
2004-11-20 00:37:19 +03:00
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2004-11-20 00:37:19 +03:00
|
|
|
case AF_LOCAL:
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sizeof(*sun);
|
2004-11-20 00:37:19 +03:00
|
|
|
sun = ((const struct sockaddr_un *)(const void *)sa);
|
2013-12-31 16:58:02 +04:00
|
|
|
(void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
|
2004-11-20 00:37:19 +03:00
|
|
|
break;
|
|
|
|
case AF_INET:
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sizeof(*sin4);
|
2004-11-20 00:37:19 +03:00
|
|
|
sin4 = ((const struct sockaddr_in *)(const void *)sa);
|
|
|
|
p = ntohs(sin4->sin_port);
|
|
|
|
a = &sin4->sin_addr;
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sizeof(*sin6);
|
2004-11-20 00:37:19 +03:00
|
|
|
sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
|
|
|
|
p = ntohs(sin6->sin6_port);
|
|
|
|
a = &sin6->sin6_addr;
|
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2004-11-20 00:37:19 +03:00
|
|
|
case AF_LINK:
|
|
|
|
sdl = ((const struct sockaddr_dl *)(const void *)sa);
|
2016-04-06 21:04:58 +03:00
|
|
|
addr = abuf;
|
|
|
|
if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
|
|
|
|
&& sdl->sdl_alen == 0) {
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sizeof(*sdl);
|
2016-04-06 21:04:58 +03:00
|
|
|
(void)snprintf(abuf, sizeof(abuf), "link#%hu",
|
|
|
|
sdl->sdl_index);
|
|
|
|
} else {
|
2016-12-29 21:30:55 +03:00
|
|
|
salen = sdl->sdl_slen + sdl->sdl_nlen + sdl->sdl_alen;
|
|
|
|
if (salen < sizeof(*sdl))
|
|
|
|
salen = sizeof(*sdl);
|
2016-04-06 21:04:58 +03:00
|
|
|
(void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
|
2016-06-02 01:58:52 +03:00
|
|
|
if ((w = strchr(addr, ':')) != NULL) {
|
2016-04-06 21:04:58 +03:00
|
|
|
*w++ = '\0';
|
|
|
|
addr = w;
|
|
|
|
}
|
2004-11-20 00:37:19 +03:00
|
|
|
}
|
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2004-11-20 00:37:19 +03:00
|
|
|
default:
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-04-09 06:05:47 +04:00
|
|
|
if (addr == abuf)
|
|
|
|
name = addr;
|
|
|
|
|
2016-12-29 21:30:55 +03:00
|
|
|
if (a && getnameinfo(sa, (socklen_t)salen, addr = abuf,
|
2005-08-27 20:55:59 +04:00
|
|
|
(unsigned int)sizeof(abuf), NULL, 0,
|
|
|
|
NI_NUMERICHOST|NI_NUMERICSERV) != 0)
|
2004-11-20 00:37:19 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (ptr = fmt; *ptr; ptr++) {
|
|
|
|
if (*ptr != '%') {
|
|
|
|
ADDC(*ptr);
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-09 06:05:47 +04:00
|
|
|
next_char:
|
2004-11-20 00:37:19 +03:00
|
|
|
switch (*++ptr) {
|
2005-04-09 06:05:47 +04:00
|
|
|
case '?':
|
|
|
|
na = 0;
|
|
|
|
goto next_char;
|
2004-11-20 00:37:19 +03:00
|
|
|
case 'a':
|
|
|
|
ADDS(addr);
|
|
|
|
break;
|
|
|
|
case 'p':
|
2005-04-09 06:05:47 +04:00
|
|
|
if (p != -1) {
|
|
|
|
(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
|
|
|
|
ADDS(nbuf);
|
|
|
|
} else
|
|
|
|
ADDNA();
|
2004-11-20 00:37:19 +03:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
|
|
|
|
ADDS(nbuf);
|
|
|
|
break;
|
|
|
|
case 'l':
|
2016-12-29 21:30:55 +03:00
|
|
|
(void)snprintf(nbuf, sizeof(nbuf), "%zu", salen);
|
2004-11-20 00:37:19 +03:00
|
|
|
ADDS(nbuf);
|
|
|
|
break;
|
2005-04-09 06:05:47 +04:00
|
|
|
case 'A':
|
|
|
|
if (name)
|
|
|
|
ADDS(name);
|
|
|
|
else if (!a)
|
|
|
|
ADDNA();
|
|
|
|
else {
|
2016-12-29 21:30:55 +03:00
|
|
|
getnameinfo(sa, (socklen_t)salen, name = Abuf,
|
2005-08-27 20:55:59 +04:00
|
|
|
(unsigned int)sizeof(nbuf), NULL, 0, 0);
|
2005-04-09 06:05:47 +04:00
|
|
|
ADDS(name);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
if (port)
|
|
|
|
ADDS(port);
|
|
|
|
else if (p == -1)
|
|
|
|
ADDNA();
|
|
|
|
else {
|
2016-12-29 21:30:55 +03:00
|
|
|
getnameinfo(sa, (socklen_t)salen, NULL, 0,
|
2005-08-27 20:55:59 +04:00
|
|
|
port = pbuf,
|
|
|
|
(unsigned int)sizeof(pbuf), 0);
|
2005-04-09 06:05:47 +04:00
|
|
|
ADDS(port);
|
|
|
|
}
|
|
|
|
break;
|
2004-11-20 00:37:19 +03:00
|
|
|
case 'I':
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2004-11-20 00:37:19 +03:00
|
|
|
if (sdl && addr != abuf) {
|
|
|
|
ADDS(abuf);
|
2016-12-29 21:30:55 +03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2004-11-20 00:37:19 +03:00
|
|
|
ADDNA();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'F':
|
|
|
|
if (sin6) {
|
|
|
|
(void)snprintf(nbuf, sizeof(nbuf), "%d",
|
|
|
|
sin6->sin6_flowinfo);
|
|
|
|
ADDS(nbuf);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ADDNA();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
if (sin6) {
|
|
|
|
(void)snprintf(nbuf, sizeof(nbuf), "%d",
|
|
|
|
sin6->sin6_scope_id);
|
|
|
|
ADDS(nbuf);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
ADDNA();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'R':
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2004-11-20 00:37:19 +03:00
|
|
|
if (sat) {
|
|
|
|
const struct netrange *n =
|
|
|
|
&sat->sat_range.r_netrange;
|
|
|
|
(void)snprintf(nbuf, sizeof(nbuf),
|
|
|
|
"%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
|
|
|
|
n->nr_lastnet);
|
|
|
|
ADDS(nbuf);
|
2016-12-29 21:30:55 +03:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2004-11-20 00:37:19 +03:00
|
|
|
ADDNA();
|
|
|
|
}
|
|
|
|
break;
|
2013-06-07 21:23:26 +04:00
|
|
|
case 'D':
|
|
|
|
switch (sa->sa_family) {
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NETATALK_AT_H
|
2013-06-07 21:23:26 +04:00
|
|
|
case AF_APPLETALK:
|
|
|
|
debug_at(nbuf, sizeof(nbuf), sat);
|
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2013-06-07 21:23:26 +04:00
|
|
|
case AF_LOCAL:
|
|
|
|
debug_un(nbuf, sizeof(nbuf), sun);
|
|
|
|
break;
|
|
|
|
case AF_INET:
|
|
|
|
debug_in(nbuf, sizeof(nbuf), sin4);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
debug_in6(nbuf, sizeof(nbuf), sin6);
|
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2013-06-07 21:23:26 +04:00
|
|
|
case AF_LINK:
|
|
|
|
debug_dl(nbuf, sizeof(nbuf), sdl);
|
|
|
|
break;
|
2016-12-29 21:30:55 +03:00
|
|
|
#endif
|
2013-06-07 21:23:26 +04:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
ADDS(nbuf);
|
|
|
|
break;
|
2004-11-20 00:37:19 +03:00
|
|
|
default:
|
|
|
|
ADDC('%');
|
2005-04-09 06:05:47 +04:00
|
|
|
if (na == 0)
|
|
|
|
ADDC('?');
|
|
|
|
if (*ptr == '\0')
|
|
|
|
goto done;
|
2006-12-09 23:18:43 +03:00
|
|
|
/*FALLTHROUGH*/
|
|
|
|
case '%':
|
2004-11-20 00:37:19 +03:00
|
|
|
ADDC(*ptr);
|
|
|
|
break;
|
|
|
|
}
|
2005-04-09 06:05:47 +04:00
|
|
|
na = 1;
|
2004-11-20 00:37:19 +03:00
|
|
|
}
|
|
|
|
done:
|
In sockaddr_snprintf(sbuf, len, fmt, sa), do not write the terminating
null character outside of the byte region [sbuf, sbuf + len).
1) If the length of the buffer is 0, do not write a null character
at all. Previously, sockaddr_snprintf() may have been able to
overwrite sbuf[-1] if len was 0.
2) If the length of the buffer, len, is greater than 0, then write
the null at sbuf[len - 1]. Previously, sockaddr_snprintf()
wrote the null at buf[len - 1], where `buf' was a "cursor" that
did not necessarily equal `sbuf', the start of the buffer. Now,
sockaddr_snprintf() always writes the null at sbuf[len - 1].
2007-07-24 12:45:45 +04:00
|
|
|
if (buf < ebuf)
|
|
|
|
*buf = '\0';
|
|
|
|
else if (len != 0)
|
|
|
|
sbuf[len - 1] = '\0';
|
2005-08-27 20:55:59 +04:00
|
|
|
return (int)(buf - sbuf);
|
2004-11-20 00:37:19 +03:00
|
|
|
}
|