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