xref: /minix3/external/bsd/blacklist/port/sockaddr_snprintf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: sockaddr_snprintf.c,v 1.9 2015/01/23 03:29:18 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*-
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5*0a6a1f1dSLionel Sambuc  * All rights reserved.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
8*0a6a1f1dSLionel Sambuc  * by Christos Zoulas.
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc  * are met:
13*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc  *
19*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*0a6a1f1dSLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*0a6a1f1dSLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*0a6a1f1dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*0a6a1f1dSLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*0a6a1f1dSLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*0a6a1f1dSLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*0a6a1f1dSLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*0a6a1f1dSLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*0a6a1f1dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*0a6a1f1dSLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
30*0a6a1f1dSLionel Sambuc  */
31*0a6a1f1dSLionel Sambuc #ifdef HAVE_CONFIG_H
32*0a6a1f1dSLionel Sambuc #include "config.h"
33*0a6a1f1dSLionel Sambuc #endif
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
36*0a6a1f1dSLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2015/01/23 03:29:18 christos Exp $");
38*0a6a1f1dSLionel Sambuc #endif /* LIBC_SCCS and not lint */
39*0a6a1f1dSLionel Sambuc 
40*0a6a1f1dSLionel Sambuc #include <sys/param.h>
41*0a6a1f1dSLionel Sambuc #include <sys/types.h>
42*0a6a1f1dSLionel Sambuc #include <sys/socket.h>
43*0a6a1f1dSLionel Sambuc #include <sys/un.h>
44*0a6a1f1dSLionel Sambuc 
45*0a6a1f1dSLionel Sambuc #include <netinet/in.h>
46*0a6a1f1dSLionel Sambuc #ifdef __linux__
47*0a6a1f1dSLionel Sambuc #undef HAVE_NETATALK_AT_H
48*0a6a1f1dSLionel Sambuc #endif
49*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
50*0a6a1f1dSLionel Sambuc #include <netatalk/at.h>
51*0a6a1f1dSLionel Sambuc #endif
52*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
53*0a6a1f1dSLionel Sambuc #include <net/if_dl.h>
54*0a6a1f1dSLionel Sambuc #endif
55*0a6a1f1dSLionel Sambuc 
56*0a6a1f1dSLionel Sambuc #include <stdio.h>
57*0a6a1f1dSLionel Sambuc #include <string.h>
58*0a6a1f1dSLionel Sambuc #include <errno.h>
59*0a6a1f1dSLionel Sambuc #include <stdlib.h>
60*0a6a1f1dSLionel Sambuc #ifdef HAVE_UTIL_H
61*0a6a1f1dSLionel Sambuc #include <util.h>
62*0a6a1f1dSLionel Sambuc #endif
63*0a6a1f1dSLionel Sambuc #include <netdb.h>
64*0a6a1f1dSLionel Sambuc 
65*0a6a1f1dSLionel Sambuc #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
66*0a6a1f1dSLionel Sambuc #define SLEN(a)	(a)->a ## _len
67*0a6a1f1dSLionel Sambuc #else
68*0a6a1f1dSLionel Sambuc static socklen_t
socklen(u_int af)69*0a6a1f1dSLionel Sambuc socklen(u_int af)
70*0a6a1f1dSLionel Sambuc {
71*0a6a1f1dSLionel Sambuc 	switch (af) {
72*0a6a1f1dSLionel Sambuc 	case AF_INET:
73*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_in);
74*0a6a1f1dSLionel Sambuc 	case AF_INET6:
75*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_in6);
76*0a6a1f1dSLionel Sambuc 	case AF_LOCAL:
77*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_un);
78*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
79*0a6a1f1dSLionel Sambuc 	case AF_LINK:
80*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_dl);
81*0a6a1f1dSLionel Sambuc #endif
82*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
83*0a6a1f1dSLionel Sambuc 	case AF_APPLETALK:
84*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_at);
85*0a6a1f1dSLionel Sambuc #endif
86*0a6a1f1dSLionel Sambuc 	default:
87*0a6a1f1dSLionel Sambuc 		return sizeof(struct sockaddr_storage);
88*0a6a1f1dSLionel Sambuc 	}
89*0a6a1f1dSLionel Sambuc }
90*0a6a1f1dSLionel Sambuc 
91*0a6a1f1dSLionel Sambuc #define SLEN(a)	socklen((a)->a ## _family)
92*0a6a1f1dSLionel Sambuc #endif
93*0a6a1f1dSLionel Sambuc 
94*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
95*0a6a1f1dSLionel Sambuc static int
debug_at(char * str,size_t len,const struct sockaddr_at * sat)96*0a6a1f1dSLionel Sambuc debug_at(char *str, size_t len, const struct sockaddr_at *sat)
97*0a6a1f1dSLionel Sambuc {
98*0a6a1f1dSLionel Sambuc 	return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
99*0a6a1f1dSLionel Sambuc 	    "sat_addr.s_net=%u, sat_addr.s_node=%u, "
100*0a6a1f1dSLionel Sambuc 	    "sat_range.r_netrange.nr_phase=%u, "
101*0a6a1f1dSLionel Sambuc 	    "sat_range.r_netrange.nr_firstnet=%u, "
102*0a6a1f1dSLionel Sambuc 	    "sat_range.r_netrange.nr_lastnet=%u",
103*0a6a1f1dSLionel Sambuc 	    SLEN(sat), sat->sat_family, sat->sat_port,
104*0a6a1f1dSLionel Sambuc 	    sat->sat_addr.s_net, sat->sat_addr.s_node,
105*0a6a1f1dSLionel Sambuc 	    sat->sat_range.r_netrange.nr_phase,
106*0a6a1f1dSLionel Sambuc 	    sat->sat_range.r_netrange.nr_firstnet,
107*0a6a1f1dSLionel Sambuc 	    sat->sat_range.r_netrange.nr_lastnet);
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc #endif
110*0a6a1f1dSLionel Sambuc 
111*0a6a1f1dSLionel Sambuc static int
debug_in(char * str,size_t len,const struct sockaddr_in * sin)112*0a6a1f1dSLionel Sambuc debug_in(char *str, size_t len, const struct sockaddr_in *sin)
113*0a6a1f1dSLionel Sambuc {
114*0a6a1f1dSLionel Sambuc 	return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
115*0a6a1f1dSLionel Sambuc 	    "sin_addr.s_addr=%08x",
116*0a6a1f1dSLionel Sambuc 	    SLEN(sin), sin->sin_family, sin->sin_port,
117*0a6a1f1dSLionel Sambuc 	    sin->sin_addr.s_addr);
118*0a6a1f1dSLionel Sambuc }
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc static int
debug_in6(char * str,size_t len,const struct sockaddr_in6 * sin6)121*0a6a1f1dSLionel Sambuc debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
122*0a6a1f1dSLionel Sambuc {
123*0a6a1f1dSLionel Sambuc 	const uint8_t *s = sin6->sin6_addr.s6_addr;
124*0a6a1f1dSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc 	return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
126*0a6a1f1dSLionel Sambuc 	    "sin6_flowinfo=%u, "
127*0a6a1f1dSLionel Sambuc 	    "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
128*0a6a1f1dSLionel Sambuc 	    "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
129*0a6a1f1dSLionel Sambuc 	    SLEN(sin6), sin6->sin6_family, sin6->sin6_port,
130*0a6a1f1dSLionel Sambuc 	    sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
131*0a6a1f1dSLionel Sambuc 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
132*0a6a1f1dSLionel Sambuc 	    s[0xe], s[0xf], sin6->sin6_scope_id);
133*0a6a1f1dSLionel Sambuc }
134*0a6a1f1dSLionel Sambuc 
135*0a6a1f1dSLionel Sambuc static int
debug_un(char * str,size_t len,const struct sockaddr_un * sun)136*0a6a1f1dSLionel Sambuc debug_un(char *str, size_t len, const struct sockaddr_un *sun)
137*0a6a1f1dSLionel Sambuc {
138*0a6a1f1dSLionel Sambuc 	return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
139*0a6a1f1dSLionel Sambuc 	    SLEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
140*0a6a1f1dSLionel Sambuc 	    sun->sun_path);
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc 
143*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
144*0a6a1f1dSLionel Sambuc static int
debug_dl(char * str,size_t len,const struct sockaddr_dl * sdl)145*0a6a1f1dSLionel Sambuc debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
146*0a6a1f1dSLionel Sambuc {
147*0a6a1f1dSLionel Sambuc 	const uint8_t *s = (const void *)sdl->sdl_data;
148*0a6a1f1dSLionel Sambuc 
149*0a6a1f1dSLionel Sambuc 	return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
150*0a6a1f1dSLionel Sambuc 	    "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
151*0a6a1f1dSLionel Sambuc 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
152*0a6a1f1dSLionel Sambuc 	    SLEN(sdl), sdl->sdl_family, sdl->sdl_index,
153*0a6a1f1dSLionel Sambuc 	    sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
154*0a6a1f1dSLionel Sambuc 	    s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
155*0a6a1f1dSLionel Sambuc 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc #endif
158*0a6a1f1dSLionel Sambuc 
159*0a6a1f1dSLionel Sambuc int
sockaddr_snprintf(char * const sbuf,const size_t len,const char * const fmt,const struct sockaddr * const sa)160*0a6a1f1dSLionel Sambuc sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
161*0a6a1f1dSLionel Sambuc     const struct sockaddr * const sa)
162*0a6a1f1dSLionel Sambuc {
163*0a6a1f1dSLionel Sambuc 	const void *a = NULL;
164*0a6a1f1dSLionel Sambuc 	char abuf[1024], nbuf[1024], *addr = NULL;
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc 	char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
167*0a6a1f1dSLionel Sambuc 	char *ebuf = &sbuf[len - 1], *buf = sbuf;
168*0a6a1f1dSLionel Sambuc 	const char *ptr, *s;
169*0a6a1f1dSLionel Sambuc 	int p = -1;
170*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
171*0a6a1f1dSLionel Sambuc 	const struct sockaddr_at *sat = NULL;
172*0a6a1f1dSLionel Sambuc #endif
173*0a6a1f1dSLionel Sambuc 	const struct sockaddr_in *sin4 = NULL;
174*0a6a1f1dSLionel Sambuc 	const struct sockaddr_in6 *sin6 = NULL;
175*0a6a1f1dSLionel Sambuc 	const struct sockaddr_un *sun = NULL;
176*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
177*0a6a1f1dSLionel Sambuc 	const struct sockaddr_dl *sdl = NULL;
178*0a6a1f1dSLionel Sambuc 	char *w = NULL;
179*0a6a1f1dSLionel Sambuc #endif
180*0a6a1f1dSLionel Sambuc 	int na = 1;
181*0a6a1f1dSLionel Sambuc 
182*0a6a1f1dSLionel Sambuc #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
183*0a6a1f1dSLionel Sambuc 	while (/*CONSTCOND*/0)
184*0a6a1f1dSLionel Sambuc #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
185*0a6a1f1dSLionel Sambuc 	while (/*CONSTCOND*/0)
186*0a6a1f1dSLionel Sambuc #define ADDNA() do { if (na) ADDS("N/A"); } \
187*0a6a1f1dSLionel Sambuc 	while (/*CONSTCOND*/0)
188*0a6a1f1dSLionel Sambuc 
189*0a6a1f1dSLionel Sambuc 	switch (sa->sa_family) {
190*0a6a1f1dSLionel Sambuc 	case AF_UNSPEC:
191*0a6a1f1dSLionel Sambuc 		goto done;
192*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
193*0a6a1f1dSLionel Sambuc 	case AF_APPLETALK:
194*0a6a1f1dSLionel Sambuc 		sat = ((const struct sockaddr_at *)(const void *)sa);
195*0a6a1f1dSLionel Sambuc 		p = ntohs(sat->sat_port);
196*0a6a1f1dSLionel Sambuc 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
197*0a6a1f1dSLionel Sambuc 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
198*0a6a1f1dSLionel Sambuc 		(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
199*0a6a1f1dSLionel Sambuc 		break;
200*0a6a1f1dSLionel Sambuc #endif
201*0a6a1f1dSLionel Sambuc 	case AF_LOCAL:
202*0a6a1f1dSLionel Sambuc 		sun = ((const struct sockaddr_un *)(const void *)sa);
203*0a6a1f1dSLionel Sambuc 		(void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
204*0a6a1f1dSLionel Sambuc 		break;
205*0a6a1f1dSLionel Sambuc 	case AF_INET:
206*0a6a1f1dSLionel Sambuc 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
207*0a6a1f1dSLionel Sambuc 		p = ntohs(sin4->sin_port);
208*0a6a1f1dSLionel Sambuc 		a = &sin4->sin_addr;
209*0a6a1f1dSLionel Sambuc 		break;
210*0a6a1f1dSLionel Sambuc 	case AF_INET6:
211*0a6a1f1dSLionel Sambuc 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
212*0a6a1f1dSLionel Sambuc 		p = ntohs(sin6->sin6_port);
213*0a6a1f1dSLionel Sambuc 		a = &sin6->sin6_addr;
214*0a6a1f1dSLionel Sambuc 		break;
215*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
216*0a6a1f1dSLionel Sambuc 	case AF_LINK:
217*0a6a1f1dSLionel Sambuc 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
218*0a6a1f1dSLionel Sambuc 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
219*0a6a1f1dSLionel Sambuc 		if ((w = strchr(addr, ':')) != 0) {
220*0a6a1f1dSLionel Sambuc 			*w++ = '\0';
221*0a6a1f1dSLionel Sambuc 			addr = w;
222*0a6a1f1dSLionel Sambuc 		}
223*0a6a1f1dSLionel Sambuc 		break;
224*0a6a1f1dSLionel Sambuc #endif
225*0a6a1f1dSLionel Sambuc 	default:
226*0a6a1f1dSLionel Sambuc 		errno = EAFNOSUPPORT;
227*0a6a1f1dSLionel Sambuc 		return -1;
228*0a6a1f1dSLionel Sambuc 	}
229*0a6a1f1dSLionel Sambuc 
230*0a6a1f1dSLionel Sambuc 	if (addr == abuf)
231*0a6a1f1dSLionel Sambuc 		name = addr;
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc 	if (a && getnameinfo(sa, (socklen_t)SLEN(sa), addr = abuf,
234*0a6a1f1dSLionel Sambuc 	    (unsigned int)sizeof(abuf), NULL, 0,
235*0a6a1f1dSLionel Sambuc 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
236*0a6a1f1dSLionel Sambuc 		return -1;
237*0a6a1f1dSLionel Sambuc 
238*0a6a1f1dSLionel Sambuc 	for (ptr = fmt; *ptr; ptr++) {
239*0a6a1f1dSLionel Sambuc 		if (*ptr != '%') {
240*0a6a1f1dSLionel Sambuc 			ADDC(*ptr);
241*0a6a1f1dSLionel Sambuc 			continue;
242*0a6a1f1dSLionel Sambuc 		}
243*0a6a1f1dSLionel Sambuc 	  next_char:
244*0a6a1f1dSLionel Sambuc 		switch (*++ptr) {
245*0a6a1f1dSLionel Sambuc 		case '?':
246*0a6a1f1dSLionel Sambuc 			na = 0;
247*0a6a1f1dSLionel Sambuc 			goto next_char;
248*0a6a1f1dSLionel Sambuc 		case 'a':
249*0a6a1f1dSLionel Sambuc 			ADDS(addr);
250*0a6a1f1dSLionel Sambuc 			break;
251*0a6a1f1dSLionel Sambuc 		case 'p':
252*0a6a1f1dSLionel Sambuc 			if (p != -1) {
253*0a6a1f1dSLionel Sambuc 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
254*0a6a1f1dSLionel Sambuc 				ADDS(nbuf);
255*0a6a1f1dSLionel Sambuc 			} else
256*0a6a1f1dSLionel Sambuc 				ADDNA();
257*0a6a1f1dSLionel Sambuc 			break;
258*0a6a1f1dSLionel Sambuc 		case 'f':
259*0a6a1f1dSLionel Sambuc 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
260*0a6a1f1dSLionel Sambuc 			ADDS(nbuf);
261*0a6a1f1dSLionel Sambuc 			break;
262*0a6a1f1dSLionel Sambuc 		case 'l':
263*0a6a1f1dSLionel Sambuc 			(void)snprintf(nbuf, sizeof(nbuf), "%d", SLEN(sa));
264*0a6a1f1dSLionel Sambuc 			ADDS(nbuf);
265*0a6a1f1dSLionel Sambuc 			break;
266*0a6a1f1dSLionel Sambuc 		case 'A':
267*0a6a1f1dSLionel Sambuc 			if (name)
268*0a6a1f1dSLionel Sambuc 				ADDS(name);
269*0a6a1f1dSLionel Sambuc 			else if (!a)
270*0a6a1f1dSLionel Sambuc 				ADDNA();
271*0a6a1f1dSLionel Sambuc 			else {
272*0a6a1f1dSLionel Sambuc 				getnameinfo(sa, (socklen_t)SLEN(sa),
273*0a6a1f1dSLionel Sambuc 					name = Abuf,
274*0a6a1f1dSLionel Sambuc 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
275*0a6a1f1dSLionel Sambuc 				ADDS(name);
276*0a6a1f1dSLionel Sambuc 			}
277*0a6a1f1dSLionel Sambuc 			break;
278*0a6a1f1dSLionel Sambuc 		case 'P':
279*0a6a1f1dSLionel Sambuc 			if (port)
280*0a6a1f1dSLionel Sambuc 				ADDS(port);
281*0a6a1f1dSLionel Sambuc 			else if (p == -1)
282*0a6a1f1dSLionel Sambuc 				ADDNA();
283*0a6a1f1dSLionel Sambuc 			else {
284*0a6a1f1dSLionel Sambuc 				getnameinfo(sa, (socklen_t)SLEN(sa), NULL, 0,
285*0a6a1f1dSLionel Sambuc 					port = pbuf,
286*0a6a1f1dSLionel Sambuc 					(unsigned int)sizeof(pbuf), 0);
287*0a6a1f1dSLionel Sambuc 				ADDS(port);
288*0a6a1f1dSLionel Sambuc 			}
289*0a6a1f1dSLionel Sambuc 			break;
290*0a6a1f1dSLionel Sambuc 		case 'I':
291*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
292*0a6a1f1dSLionel Sambuc 			if (sdl && addr != abuf) {
293*0a6a1f1dSLionel Sambuc 				ADDS(abuf);
294*0a6a1f1dSLionel Sambuc 			} else
295*0a6a1f1dSLionel Sambuc #endif
296*0a6a1f1dSLionel Sambuc 			{
297*0a6a1f1dSLionel Sambuc 				ADDNA();
298*0a6a1f1dSLionel Sambuc 			}
299*0a6a1f1dSLionel Sambuc 			break;
300*0a6a1f1dSLionel Sambuc 		case 'F':
301*0a6a1f1dSLionel Sambuc 			if (sin6) {
302*0a6a1f1dSLionel Sambuc 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
303*0a6a1f1dSLionel Sambuc 				    sin6->sin6_flowinfo);
304*0a6a1f1dSLionel Sambuc 				ADDS(nbuf);
305*0a6a1f1dSLionel Sambuc 				break;
306*0a6a1f1dSLionel Sambuc 			} else {
307*0a6a1f1dSLionel Sambuc 				ADDNA();
308*0a6a1f1dSLionel Sambuc 			}
309*0a6a1f1dSLionel Sambuc 			break;
310*0a6a1f1dSLionel Sambuc 		case 'S':
311*0a6a1f1dSLionel Sambuc 			if (sin6) {
312*0a6a1f1dSLionel Sambuc 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
313*0a6a1f1dSLionel Sambuc 				    sin6->sin6_scope_id);
314*0a6a1f1dSLionel Sambuc 				ADDS(nbuf);
315*0a6a1f1dSLionel Sambuc 				break;
316*0a6a1f1dSLionel Sambuc 			} else {
317*0a6a1f1dSLionel Sambuc 				ADDNA();
318*0a6a1f1dSLionel Sambuc 			}
319*0a6a1f1dSLionel Sambuc 			break;
320*0a6a1f1dSLionel Sambuc 		case 'R':
321*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
322*0a6a1f1dSLionel Sambuc 			if (sat) {
323*0a6a1f1dSLionel Sambuc 				const struct netrange *n =
324*0a6a1f1dSLionel Sambuc 				    &sat->sat_range.r_netrange;
325*0a6a1f1dSLionel Sambuc 				(void)snprintf(nbuf, sizeof(nbuf),
326*0a6a1f1dSLionel Sambuc 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
327*0a6a1f1dSLionel Sambuc 				    n->nr_lastnet);
328*0a6a1f1dSLionel Sambuc 				ADDS(nbuf);
329*0a6a1f1dSLionel Sambuc 			} else
330*0a6a1f1dSLionel Sambuc #endif
331*0a6a1f1dSLionel Sambuc 			{
332*0a6a1f1dSLionel Sambuc 				ADDNA();
333*0a6a1f1dSLionel Sambuc 			}
334*0a6a1f1dSLionel Sambuc 			break;
335*0a6a1f1dSLionel Sambuc 		case 'D':
336*0a6a1f1dSLionel Sambuc 			switch (sa->sa_family) {
337*0a6a1f1dSLionel Sambuc #ifdef HAVE_NETATALK_AT_H
338*0a6a1f1dSLionel Sambuc 			case AF_APPLETALK:
339*0a6a1f1dSLionel Sambuc 				debug_at(nbuf, sizeof(nbuf), sat);
340*0a6a1f1dSLionel Sambuc 				break;
341*0a6a1f1dSLionel Sambuc #endif
342*0a6a1f1dSLionel Sambuc 			case AF_LOCAL:
343*0a6a1f1dSLionel Sambuc 				debug_un(nbuf, sizeof(nbuf), sun);
344*0a6a1f1dSLionel Sambuc 				break;
345*0a6a1f1dSLionel Sambuc 			case AF_INET:
346*0a6a1f1dSLionel Sambuc 				debug_in(nbuf, sizeof(nbuf), sin4);
347*0a6a1f1dSLionel Sambuc 				break;
348*0a6a1f1dSLionel Sambuc 			case AF_INET6:
349*0a6a1f1dSLionel Sambuc 				debug_in6(nbuf, sizeof(nbuf), sin6);
350*0a6a1f1dSLionel Sambuc 				break;
351*0a6a1f1dSLionel Sambuc #ifdef HAVE_NET_IF_DL_H
352*0a6a1f1dSLionel Sambuc 			case AF_LINK:
353*0a6a1f1dSLionel Sambuc 				debug_dl(nbuf, sizeof(nbuf), sdl);
354*0a6a1f1dSLionel Sambuc 				break;
355*0a6a1f1dSLionel Sambuc #endif
356*0a6a1f1dSLionel Sambuc 			default:
357*0a6a1f1dSLionel Sambuc 				abort();
358*0a6a1f1dSLionel Sambuc 			}
359*0a6a1f1dSLionel Sambuc 			ADDS(nbuf);
360*0a6a1f1dSLionel Sambuc 			break;
361*0a6a1f1dSLionel Sambuc 		default:
362*0a6a1f1dSLionel Sambuc 			ADDC('%');
363*0a6a1f1dSLionel Sambuc 			if (na == 0)
364*0a6a1f1dSLionel Sambuc 				ADDC('?');
365*0a6a1f1dSLionel Sambuc 			if (*ptr == '\0')
366*0a6a1f1dSLionel Sambuc 				goto done;
367*0a6a1f1dSLionel Sambuc 			/*FALLTHROUGH*/
368*0a6a1f1dSLionel Sambuc 		case '%':
369*0a6a1f1dSLionel Sambuc 			ADDC(*ptr);
370*0a6a1f1dSLionel Sambuc 			break;
371*0a6a1f1dSLionel Sambuc 		}
372*0a6a1f1dSLionel Sambuc 		na = 1;
373*0a6a1f1dSLionel Sambuc 	}
374*0a6a1f1dSLionel Sambuc done:
375*0a6a1f1dSLionel Sambuc 	if (buf < ebuf)
376*0a6a1f1dSLionel Sambuc 		*buf = '\0';
377*0a6a1f1dSLionel Sambuc 	else if (len != 0)
378*0a6a1f1dSLionel Sambuc 		sbuf[len - 1] = '\0';
379*0a6a1f1dSLionel Sambuc 	return (int)(buf - sbuf);
380*0a6a1f1dSLionel Sambuc }
381