xref: /freebsd/lib/libnetbsd/sockaddr_snprintf.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
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/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 
40 #include <netinet/in.h>
41 #ifdef HAVE_NET_IF_DL_H
42 #include <net/if_dl.h>
43 #endif
44 
45 #include <stdio.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <util.h>
50 #include <libutil.h>
51 #include <netdb.h>
52 
53 #ifdef BSD4_4
54 # define SALEN(sa)	((sa)->sa ## _len)
55 #else
56 # define SALEN(sa)	((unsigned)sizeof(*sa))
57 #endif
58 
59 static int
60 debug_in(char *str, size_t len, const struct sockaddr_in *sin)
61 {
62 	return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
63 	    "sin_addr.s_addr=%08x",
64 	    SALEN(sin), sin->sin_family, sin->sin_port,
65 	    sin->sin_addr.s_addr);
66 }
67 
68 static int
69 debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
70 {
71 	const uint8_t *s = sin6->sin6_addr.s6_addr;
72 
73 	return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
74 	    "sin6_flowinfo=%u, "
75 	    "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
76 	    "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
77 	    SALEN(sin6), sin6->sin6_family, sin6->sin6_port,
78 	    sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
79 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
80 	    s[0xe], s[0xf], sin6->sin6_scope_id);
81 }
82 
83 static int
84 debug_un(char *str, size_t len, const struct sockaddr_un *sun)
85 {
86 	return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
87 	    SALEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
88 	    sun->sun_path);
89 }
90 
91 #ifdef HAVE_NET_IF_DL_H
92 static int
93 debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
94 {
95 	const uint8_t *s = (const void *)sdl->sdl_data;
96 
97 	return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
98 	    "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
99 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
100 	    SALEN(sdl), sdl->sdl_family, sdl->sdl_index,
101 	    sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
102 	    s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
103 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
104 }
105 #endif
106 
107 int
108 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
109     const struct sockaddr * const sa)
110 {
111 	const void *a = NULL;
112 	char abuf[1024], nbuf[1024], *addr = NULL;
113 	char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
114 	char *ebuf = &sbuf[len - 1], *buf = sbuf;
115 	const char *ptr, *s;
116 	size_t salen;
117 	int p = -1;
118 	const struct sockaddr_in *sin4 = NULL;
119 	const struct sockaddr_in6 *sin6 = NULL;
120 	const struct sockaddr_un *sun = NULL;
121 #ifdef HAVE_NET_IF_DL_H
122 	const struct sockaddr_dl *sdl = NULL;
123 	char *w = NULL;
124 #endif
125 	int na = 1;
126 
127 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
128 	while (/*CONSTCOND*/0)
129 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
130 	while (/*CONSTCOND*/0)
131 #define ADDNA() do { if (na) ADDS("N/A"); } \
132 	while (/*CONSTCOND*/0)
133 
134 	switch (sa->sa_family) {
135 	case AF_UNSPEC:
136 		goto done;
137 	case AF_LOCAL:
138 		salen = sizeof(*sun);
139 		sun = ((const struct sockaddr_un *)(const void *)sa);
140 		(void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
141 		break;
142 	case AF_INET:
143 		salen = sizeof(*sin4);
144 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
145 		p = ntohs(sin4->sin_port);
146 		a = &sin4->sin_addr;
147 		break;
148 	case AF_INET6:
149 		salen = sizeof(*sin6);
150 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
151 		p = ntohs(sin6->sin6_port);
152 		a = &sin6->sin6_addr;
153 		break;
154 #ifdef HAVE_NET_IF_DL_H
155 	case AF_LINK:
156 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
157 		addr = abuf;
158 		if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
159 		    && sdl->sdl_alen == 0) {
160 			salen = sizeof(*sdl);
161 			(void)snprintf(abuf, sizeof(abuf), "link#%hu",
162 			    sdl->sdl_index);
163 		} else {
164 			salen = sdl->sdl_slen + sdl->sdl_nlen +  sdl->sdl_alen;
165 			if (salen < sizeof(*sdl))
166 				salen = sizeof(*sdl);
167 			(void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
168 			if ((w = strchr(addr, ':')) != NULL) {
169 			    *w++ = '\0';
170 			    addr = w;
171 			}
172 		}
173 		break;
174 #endif
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)salen, 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), "%zu", salen);
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)salen, name = Abuf,
223 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
224 				ADDS(name);
225 			}
226 			break;
227 		case 'P':
228 			if (port)
229 				ADDS(port);
230 			else if (p == -1)
231 				ADDNA();
232 			else {
233 				getnameinfo(sa, (socklen_t)salen, NULL, 0,
234 					port = pbuf,
235 					(unsigned int)sizeof(pbuf), 0);
236 				ADDS(port);
237 			}
238 			break;
239 		case 'I':
240 #ifdef HAVE_NET_IF_DL_H
241 			if (sdl && addr != abuf) {
242 				ADDS(abuf);
243 			} else
244 #endif
245 			{
246 				ADDNA();
247 			}
248 			break;
249 		case 'F':
250 			if (sin6) {
251 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
252 				    sin6->sin6_flowinfo);
253 				ADDS(nbuf);
254 				break;
255 			} else {
256 				ADDNA();
257 			}
258 			break;
259 		case 'S':
260 			if (sin6) {
261 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
262 				    sin6->sin6_scope_id);
263 				ADDS(nbuf);
264 				break;
265 			} else {
266 				ADDNA();
267 			}
268 			break;
269 		case 'R':
270 			{
271 				ADDNA();
272 			}
273 			break;
274 		case 'D':
275 			switch (sa->sa_family) {
276 			case AF_LOCAL:
277 				debug_un(nbuf, sizeof(nbuf), sun);
278 				break;
279 			case AF_INET:
280 				debug_in(nbuf, sizeof(nbuf), sin4);
281 				break;
282 			case AF_INET6:
283 				debug_in6(nbuf, sizeof(nbuf), sin6);
284 				break;
285 #ifdef HAVE_NET_IF_DL_H
286 			case AF_LINK:
287 				debug_dl(nbuf, sizeof(nbuf), sdl);
288 				break;
289 #endif
290 			default:
291 				abort();
292 			}
293 			ADDS(nbuf);
294 			break;
295 		default:
296 			ADDC('%');
297 			if (na == 0)
298 				ADDC('?');
299 			if (*ptr == '\0')
300 				goto done;
301 			/*FALLTHROUGH*/
302 		case '%':
303 			ADDC(*ptr);
304 			break;
305 		}
306 		na = 1;
307 	}
308 done:
309 	if (buf < ebuf)
310 		*buf = '\0';
311 	else if (len != 0)
312 		sbuf[len - 1] = '\0';
313 	return (int)(buf - sbuf);
314 }
315