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