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