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