xref: /netbsd-src/lib/libutil/sockaddr_snprintf.c (revision 5d7e998696ff94d2b72a059628d4f6dfafb235b2)
1 /*	$NetBSD: sockaddr_snprintf.c,v 1.14 2016/12/29 18:30:55 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004, 2016 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.14 2016/12/29 18:30:55 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 HAVE_NETATALK_AT_H
47 #include <netatalk/at.h>
48 #endif
49 #ifdef HAVE_NET_IF_DL_H
50 #include <net/if_dl.h>
51 #endif
52 
53 #include <stdio.h>
54 #include <string.h>
55 #include <errno.h>
56 #include <stdlib.h>
57 #ifdef HAVE_UTIL_H
58 #include <util.h>
59 #endif
60 #ifdef HAVE_LIBUTIL_H
61 #include <libutil.h>
62 #endif
63 #include <netdb.h>
64 
65 #ifdef BSD4_4
66 # define SALEN(sa)	((sa)->sa ## _len)
67 #else
68 # define SALEN(sa)	((unsigned)sizeof(*sa))
69 #endif
70 
71 #ifdef HAVE_NETATALK_AT_H
72 static int
debug_at(char * str,size_t len,const struct sockaddr_at * sat)73 debug_at(char *str, size_t len, const struct sockaddr_at *sat)
74 {
75 	return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
76 	    "sat_addr.s_net=%u, sat_addr.s_node=%u, "
77 	    "sat_range.r_netrange.nr_phase=%u, "
78 	    "sat_range.r_netrange.nr_firstnet=%u, "
79 	    "sat_range.r_netrange.nr_lastnet=%u",
80 	    SALEN(sat), sat->sat_family, sat->sat_port,
81 	    sat->sat_addr.s_net, sat->sat_addr.s_node,
82 	    sat->sat_range.r_netrange.nr_phase,
83 	    sat->sat_range.r_netrange.nr_firstnet,
84 	    sat->sat_range.r_netrange.nr_lastnet);
85 }
86 #endif
87 
88 static int
debug_in(char * str,size_t len,const struct sockaddr_in * sin)89 debug_in(char *str, size_t len, const struct sockaddr_in *sin)
90 {
91 	return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
92 	    "sin_addr.s_addr=%08x",
93 	    SALEN(sin), sin->sin_family, sin->sin_port,
94 	    sin->sin_addr.s_addr);
95 }
96 
97 static int
debug_in6(char * str,size_t len,const struct sockaddr_in6 * sin6)98 debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
99 {
100 	const uint8_t *s = sin6->sin6_addr.s6_addr;
101 
102 	return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
103 	    "sin6_flowinfo=%u, "
104 	    "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
105 	    "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
106 	    SALEN(sin6), sin6->sin6_family, sin6->sin6_port,
107 	    sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
108 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
109 	    s[0xe], s[0xf], sin6->sin6_scope_id);
110 }
111 
112 static int
debug_un(char * str,size_t len,const struct sockaddr_un * sun)113 debug_un(char *str, size_t len, const struct sockaddr_un *sun)
114 {
115 	return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
116 	    SALEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
117 	    sun->sun_path);
118 }
119 
120 #ifdef HAVE_NET_IF_DL_H
121 static int
debug_dl(char * str,size_t len,const struct sockaddr_dl * sdl)122 debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
123 {
124 	const uint8_t *s = (const void *)sdl->sdl_data;
125 
126 	return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
127 	    "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
128 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
129 	    SALEN(sdl), sdl->sdl_family, sdl->sdl_index,
130 	    sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
131 	    s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
132 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
133 }
134 #endif
135 
136 int
sockaddr_snprintf(char * const sbuf,const size_t len,const char * const fmt,const struct sockaddr * const sa)137 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
138     const struct sockaddr * const sa)
139 {
140 	const void *a = NULL;
141 	char abuf[1024], nbuf[1024], *addr = NULL;
142 	char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
143 	char *ebuf = &sbuf[len - 1], *buf = sbuf;
144 	const char *ptr, *s;
145 	size_t salen;
146 	int p = -1;
147 #ifdef HAVE_NETATALK_AT_H
148 	const struct sockaddr_at *sat = NULL;
149 #endif
150 	const struct sockaddr_in *sin4 = NULL;
151 	const struct sockaddr_in6 *sin6 = NULL;
152 	const struct sockaddr_un *sun = NULL;
153 #ifdef HAVE_NET_IF_DL_H
154 	const struct sockaddr_dl *sdl = NULL;
155 	char *w = NULL;
156 #endif
157 	int na = 1;
158 
159 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
160 	while (/*CONSTCOND*/0)
161 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
162 	while (/*CONSTCOND*/0)
163 #define ADDNA() do { if (na) ADDS("N/A"); } \
164 	while (/*CONSTCOND*/0)
165 
166 	switch (sa->sa_family) {
167 	case AF_UNSPEC:
168 		goto done;
169 #ifdef HAVE_NETATALK_AT_H
170 	case AF_APPLETALK:
171 		salen = sizeof(*sat);
172 		sat = ((const struct sockaddr_at *)(const void *)sa);
173 		p = ntohs(sat->sat_port);
174 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
175 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
176 		(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
177 		break;
178 #endif
179 	case AF_LOCAL:
180 		salen = sizeof(*sun);
181 		sun = ((const struct sockaddr_un *)(const void *)sa);
182 		(void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
183 		break;
184 	case AF_INET:
185 		salen = sizeof(*sin4);
186 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
187 		p = ntohs(sin4->sin_port);
188 		a = &sin4->sin_addr;
189 		break;
190 	case AF_INET6:
191 		salen = sizeof(*sin6);
192 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
193 		p = ntohs(sin6->sin6_port);
194 		a = &sin6->sin6_addr;
195 		break;
196 #ifdef HAVE_NET_IF_DL_H
197 	case AF_LINK:
198 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
199 		addr = abuf;
200 		if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
201 		    && sdl->sdl_alen == 0) {
202 			salen = sizeof(*sdl);
203 			(void)snprintf(abuf, sizeof(abuf), "link#%hu",
204 			    sdl->sdl_index);
205 		} else {
206 			salen = sdl->sdl_slen + sdl->sdl_nlen +  sdl->sdl_alen;
207 			if (salen < sizeof(*sdl))
208 				salen = sizeof(*sdl);
209 			(void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
210 			if ((w = strchr(addr, ':')) != NULL) {
211 			    *w++ = '\0';
212 			    addr = w;
213 			}
214 		}
215 		break;
216 #endif
217 	default:
218 		errno = EAFNOSUPPORT;
219 		return -1;
220 	}
221 
222 	if (addr == abuf)
223 		name = addr;
224 
225 	if (a && getnameinfo(sa, (socklen_t)salen, addr = abuf,
226 	    (unsigned int)sizeof(abuf), NULL, 0,
227 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
228 		return -1;
229 
230 	for (ptr = fmt; *ptr; ptr++) {
231 		if (*ptr != '%') {
232 			ADDC(*ptr);
233 			continue;
234 		}
235 	  next_char:
236 		switch (*++ptr) {
237 		case '?':
238 			na = 0;
239 			goto next_char;
240 		case 'a':
241 			ADDS(addr);
242 			break;
243 		case 'p':
244 			if (p != -1) {
245 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
246 				ADDS(nbuf);
247 			} else
248 				ADDNA();
249 			break;
250 		case 'f':
251 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
252 			ADDS(nbuf);
253 			break;
254 		case 'l':
255 			(void)snprintf(nbuf, sizeof(nbuf), "%zu", salen);
256 			ADDS(nbuf);
257 			break;
258 		case 'A':
259 			if (name)
260 				ADDS(name);
261 			else if (!a)
262 				ADDNA();
263 			else {
264 				getnameinfo(sa, (socklen_t)salen, name = Abuf,
265 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
266 				ADDS(name);
267 			}
268 			break;
269 		case 'P':
270 			if (port)
271 				ADDS(port);
272 			else if (p == -1)
273 				ADDNA();
274 			else {
275 				getnameinfo(sa, (socklen_t)salen, NULL, 0,
276 					port = pbuf,
277 					(unsigned int)sizeof(pbuf), 0);
278 				ADDS(port);
279 			}
280 			break;
281 		case 'I':
282 #ifdef HAVE_NET_IF_DL_H
283 			if (sdl && addr != abuf) {
284 				ADDS(abuf);
285 			} else
286 #endif
287 			{
288 				ADDNA();
289 			}
290 			break;
291 		case 'F':
292 			if (sin6) {
293 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
294 				    sin6->sin6_flowinfo);
295 				ADDS(nbuf);
296 				break;
297 			} else {
298 				ADDNA();
299 			}
300 			break;
301 		case 'S':
302 			if (sin6) {
303 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
304 				    sin6->sin6_scope_id);
305 				ADDS(nbuf);
306 				break;
307 			} else {
308 				ADDNA();
309 			}
310 			break;
311 		case 'R':
312 #ifdef HAVE_NETATALK_AT_H
313 			if (sat) {
314 				const struct netrange *n =
315 				    &sat->sat_range.r_netrange;
316 				(void)snprintf(nbuf, sizeof(nbuf),
317 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
318 				    n->nr_lastnet);
319 				ADDS(nbuf);
320 			} else
321 #endif
322 			{
323 				ADDNA();
324 			}
325 			break;
326 		case 'D':
327 			switch (sa->sa_family) {
328 #ifdef HAVE_NETATALK_AT_H
329 			case AF_APPLETALK:
330 				debug_at(nbuf, sizeof(nbuf), sat);
331 				break;
332 #endif
333 			case AF_LOCAL:
334 				debug_un(nbuf, sizeof(nbuf), sun);
335 				break;
336 			case AF_INET:
337 				debug_in(nbuf, sizeof(nbuf), sin4);
338 				break;
339 			case AF_INET6:
340 				debug_in6(nbuf, sizeof(nbuf), sin6);
341 				break;
342 #ifdef HAVE_NET_IF_DL_H
343 			case AF_LINK:
344 				debug_dl(nbuf, sizeof(nbuf), sdl);
345 				break;
346 #endif
347 			default:
348 				abort();
349 			}
350 			ADDS(nbuf);
351 			break;
352 		default:
353 			ADDC('%');
354 			if (na == 0)
355 				ADDC('?');
356 			if (*ptr == '\0')
357 				goto done;
358 			/*FALLTHROUGH*/
359 		case '%':
360 			ADDC(*ptr);
361 			break;
362 		}
363 		na = 1;
364 	}
365 done:
366 	if (buf < ebuf)
367 		*buf = '\0';
368 	else if (len != 0)
369 		sbuf[len - 1] = '\0';
370 	return (int)(buf - sbuf);
371 }
372