sockaddr_snprintf.c revision 1.15 1 1.15 christos /* $NetBSD: sockaddr_snprintf.c,v 1.15 2025/09/14 17:26:20 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.14 christos * Copyright (c) 2004, 2016 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1 christos */
31 1.14 christos #ifdef HAVE_CONFIG_H
32 1.14 christos #include "config.h"
33 1.14 christos #endif
34 1.14 christos
35 1.1 christos #include <sys/cdefs.h>
36 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
37 1.15 christos __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.15 2025/09/14 17:26:20 christos Exp $");
38 1.1 christos #endif /* LIBC_SCCS and not lint */
39 1.1 christos
40 1.14 christos #include <sys/param.h>
41 1.1 christos #include <sys/types.h>
42 1.1 christos #include <sys/socket.h>
43 1.1 christos #include <sys/un.h>
44 1.1 christos
45 1.1 christos #include <netinet/in.h>
46 1.14 christos #ifdef HAVE_NETATALK_AT_H
47 1.1 christos #include <netatalk/at.h>
48 1.14 christos #endif
49 1.14 christos #ifdef HAVE_NET_IF_DL_H
50 1.1 christos #include <net/if_dl.h>
51 1.14 christos #endif
52 1.1 christos
53 1.1 christos #include <stdio.h>
54 1.1 christos #include <string.h>
55 1.1 christos #include <errno.h>
56 1.10 christos #include <stdlib.h>
57 1.14 christos #ifdef HAVE_UTIL_H
58 1.1 christos #include <util.h>
59 1.14 christos #endif
60 1.14 christos #ifdef HAVE_LIBUTIL_H
61 1.14 christos #include <libutil.h>
62 1.14 christos #endif
63 1.1 christos #include <netdb.h>
64 1.1 christos
65 1.14 christos #ifdef BSD4_4
66 1.14 christos # define SALEN(sa) ((sa)->sa ## _len)
67 1.14 christos #else
68 1.14 christos # define SALEN(sa) ((unsigned)sizeof(*sa))
69 1.14 christos #endif
70 1.14 christos
71 1.14 christos #ifdef HAVE_NETATALK_AT_H
72 1.10 christos static int
73 1.10 christos debug_at(char *str, size_t len, const struct sockaddr_at *sat)
74 1.10 christos {
75 1.10 christos return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
76 1.10 christos "sat_addr.s_net=%u, sat_addr.s_node=%u, "
77 1.10 christos "sat_range.r_netrange.nr_phase=%u, "
78 1.10 christos "sat_range.r_netrange.nr_firstnet=%u, "
79 1.10 christos "sat_range.r_netrange.nr_lastnet=%u",
80 1.14 christos SALEN(sat), sat->sat_family, sat->sat_port,
81 1.10 christos sat->sat_addr.s_net, sat->sat_addr.s_node,
82 1.10 christos sat->sat_range.r_netrange.nr_phase,
83 1.10 christos sat->sat_range.r_netrange.nr_firstnet,
84 1.10 christos sat->sat_range.r_netrange.nr_lastnet);
85 1.10 christos }
86 1.14 christos #endif
87 1.10 christos
88 1.10 christos static int
89 1.10 christos debug_in(char *str, size_t len, const struct sockaddr_in *sin)
90 1.10 christos {
91 1.10 christos return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
92 1.10 christos "sin_addr.s_addr=%08x",
93 1.14 christos SALEN(sin), sin->sin_family, sin->sin_port,
94 1.10 christos sin->sin_addr.s_addr);
95 1.10 christos }
96 1.10 christos
97 1.10 christos static int
98 1.10 christos debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
99 1.10 christos {
100 1.10 christos const uint8_t *s = sin6->sin6_addr.s6_addr;
101 1.10 christos
102 1.10 christos return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
103 1.10 christos "sin6_flowinfo=%u, "
104 1.10 christos "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
105 1.10 christos "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
106 1.14 christos SALEN(sin6), sin6->sin6_family, sin6->sin6_port,
107 1.10 christos sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
108 1.10 christos s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
109 1.10 christos s[0xe], s[0xf], sin6->sin6_scope_id);
110 1.10 christos }
111 1.10 christos
112 1.10 christos static int
113 1.10 christos debug_un(char *str, size_t len, const struct sockaddr_un *sun)
114 1.10 christos {
115 1.10 christos return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
116 1.14 christos SALEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
117 1.10 christos sun->sun_path);
118 1.10 christos }
119 1.10 christos
120 1.14 christos #ifdef HAVE_NET_IF_DL_H
121 1.10 christos static int
122 1.10 christos debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
123 1.10 christos {
124 1.10 christos const uint8_t *s = (const void *)sdl->sdl_data;
125 1.10 christos
126 1.10 christos return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
127 1.10 christos "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
128 1.10 christos "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
129 1.14 christos SALEN(sdl), sdl->sdl_family, sdl->sdl_index,
130 1.10 christos sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
131 1.10 christos s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
132 1.10 christos s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
133 1.10 christos }
134 1.14 christos #endif
135 1.10 christos
136 1.1 christos int
137 1.8 dyoung sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
138 1.8 dyoung const struct sockaddr * const sa)
139 1.1 christos {
140 1.1 christos const void *a = NULL;
141 1.14 christos char abuf[1024], nbuf[1024], *addr = NULL;
142 1.5 atatat char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
143 1.8 dyoung char *ebuf = &sbuf[len - 1], *buf = sbuf;
144 1.15 christos const char *ptr, *s, *f;
145 1.14 christos size_t salen;
146 1.5 atatat int p = -1;
147 1.14 christos #ifdef HAVE_NETATALK_AT_H
148 1.1 christos const struct sockaddr_at *sat = NULL;
149 1.14 christos #endif
150 1.1 christos const struct sockaddr_in *sin4 = NULL;
151 1.1 christos const struct sockaddr_in6 *sin6 = NULL;
152 1.1 christos const struct sockaddr_un *sun = NULL;
153 1.14 christos #ifdef HAVE_NET_IF_DL_H
154 1.1 christos const struct sockaddr_dl *sdl = NULL;
155 1.14 christos char *w = NULL;
156 1.14 christos #endif
157 1.5 atatat int na = 1;
158 1.1 christos
159 1.5 atatat #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
160 1.5 atatat while (/*CONSTCOND*/0)
161 1.5 atatat #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
162 1.5 atatat while (/*CONSTCOND*/0)
163 1.5 atatat #define ADDNA() do { if (na) ADDS("N/A"); } \
164 1.5 atatat while (/*CONSTCOND*/0)
165 1.1 christos
166 1.1 christos switch (sa->sa_family) {
167 1.1 christos case AF_UNSPEC:
168 1.1 christos goto done;
169 1.14 christos #ifdef HAVE_NETATALK_AT_H
170 1.1 christos case AF_APPLETALK:
171 1.14 christos salen = sizeof(*sat);
172 1.1 christos sat = ((const struct sockaddr_at *)(const void *)sa);
173 1.1 christos p = ntohs(sat->sat_port);
174 1.1 christos (void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
175 1.1 christos ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
176 1.5 atatat (void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
177 1.1 christos break;
178 1.14 christos #endif
179 1.1 christos case AF_LOCAL:
180 1.14 christos salen = sizeof(*sun);
181 1.1 christos sun = ((const struct sockaddr_un *)(const void *)sa);
182 1.11 mlelstv (void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
183 1.1 christos break;
184 1.1 christos case AF_INET:
185 1.14 christos salen = sizeof(*sin4);
186 1.1 christos sin4 = ((const struct sockaddr_in *)(const void *)sa);
187 1.1 christos p = ntohs(sin4->sin_port);
188 1.1 christos a = &sin4->sin_addr;
189 1.1 christos break;
190 1.1 christos case AF_INET6:
191 1.14 christos salen = sizeof(*sin6);
192 1.1 christos sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
193 1.1 christos p = ntohs(sin6->sin6_port);
194 1.1 christos a = &sin6->sin6_addr;
195 1.1 christos break;
196 1.14 christos #ifdef HAVE_NET_IF_DL_H
197 1.1 christos case AF_LINK:
198 1.1 christos sdl = ((const struct sockaddr_dl *)(const void *)sa);
199 1.12 christos addr = abuf;
200 1.12 christos if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
201 1.12 christos && sdl->sdl_alen == 0) {
202 1.14 christos salen = sizeof(*sdl);
203 1.12 christos (void)snprintf(abuf, sizeof(abuf), "link#%hu",
204 1.12 christos sdl->sdl_index);
205 1.12 christos } else {
206 1.14 christos salen = sdl->sdl_slen + sdl->sdl_nlen + sdl->sdl_alen;
207 1.14 christos if (salen < sizeof(*sdl))
208 1.14 christos salen = sizeof(*sdl);
209 1.12 christos (void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
210 1.13 christos if ((w = strchr(addr, ':')) != NULL) {
211 1.12 christos *w++ = '\0';
212 1.12 christos addr = w;
213 1.12 christos }
214 1.1 christos }
215 1.1 christos break;
216 1.14 christos #endif
217 1.1 christos default:
218 1.1 christos errno = EAFNOSUPPORT;
219 1.1 christos return -1;
220 1.1 christos }
221 1.1 christos
222 1.5 atatat if (addr == abuf)
223 1.5 atatat name = addr;
224 1.5 atatat
225 1.14 christos if (a && getnameinfo(sa, (socklen_t)salen, addr = abuf,
226 1.6 elad (unsigned int)sizeof(abuf), NULL, 0,
227 1.6 elad NI_NUMERICHOST|NI_NUMERICSERV) != 0)
228 1.1 christos return -1;
229 1.1 christos
230 1.1 christos for (ptr = fmt; *ptr; ptr++) {
231 1.1 christos if (*ptr != '%') {
232 1.1 christos ADDC(*ptr);
233 1.1 christos continue;
234 1.1 christos }
235 1.5 atatat next_char:
236 1.1 christos switch (*++ptr) {
237 1.5 atatat case '?':
238 1.5 atatat na = 0;
239 1.5 atatat goto next_char;
240 1.1 christos case 'a':
241 1.1 christos ADDS(addr);
242 1.1 christos break;
243 1.1 christos case 'p':
244 1.5 atatat if (p != -1) {
245 1.5 atatat (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
246 1.5 atatat ADDS(nbuf);
247 1.5 atatat } else
248 1.5 atatat ADDNA();
249 1.1 christos break;
250 1.1 christos case 'f':
251 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
252 1.1 christos ADDS(nbuf);
253 1.1 christos break;
254 1.1 christos case 'l':
255 1.14 christos (void)snprintf(nbuf, sizeof(nbuf), "%zu", salen);
256 1.1 christos ADDS(nbuf);
257 1.1 christos break;
258 1.5 atatat case 'A':
259 1.5 atatat if (name)
260 1.5 atatat ADDS(name);
261 1.5 atatat else if (!a)
262 1.5 atatat ADDNA();
263 1.5 atatat else {
264 1.14 christos getnameinfo(sa, (socklen_t)salen, name = Abuf,
265 1.6 elad (unsigned int)sizeof(nbuf), NULL, 0, 0);
266 1.5 atatat ADDS(name);
267 1.5 atatat }
268 1.5 atatat break;
269 1.5 atatat case 'P':
270 1.5 atatat if (port)
271 1.5 atatat ADDS(port);
272 1.5 atatat else if (p == -1)
273 1.5 atatat ADDNA();
274 1.5 atatat else {
275 1.14 christos getnameinfo(sa, (socklen_t)salen, NULL, 0,
276 1.6 elad port = pbuf,
277 1.6 elad (unsigned int)sizeof(pbuf), 0);
278 1.5 atatat ADDS(port);
279 1.5 atatat }
280 1.5 atatat break;
281 1.1 christos case 'I':
282 1.14 christos #ifdef HAVE_NET_IF_DL_H
283 1.1 christos if (sdl && addr != abuf) {
284 1.1 christos ADDS(abuf);
285 1.14 christos } else
286 1.14 christos #endif
287 1.14 christos {
288 1.1 christos ADDNA();
289 1.1 christos }
290 1.1 christos break;
291 1.1 christos case 'F':
292 1.1 christos if (sin6) {
293 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d",
294 1.1 christos sin6->sin6_flowinfo);
295 1.1 christos ADDS(nbuf);
296 1.1 christos break;
297 1.1 christos } else {
298 1.1 christos ADDNA();
299 1.1 christos }
300 1.1 christos break;
301 1.1 christos case 'S':
302 1.1 christos if (sin6) {
303 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d",
304 1.1 christos sin6->sin6_scope_id);
305 1.1 christos ADDS(nbuf);
306 1.1 christos break;
307 1.1 christos } else {
308 1.1 christos ADDNA();
309 1.1 christos }
310 1.1 christos break;
311 1.1 christos case 'R':
312 1.14 christos #ifdef HAVE_NETATALK_AT_H
313 1.1 christos if (sat) {
314 1.1 christos const struct netrange *n =
315 1.1 christos &sat->sat_range.r_netrange;
316 1.1 christos (void)snprintf(nbuf, sizeof(nbuf),
317 1.1 christos "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
318 1.1 christos n->nr_lastnet);
319 1.1 christos ADDS(nbuf);
320 1.14 christos } else
321 1.14 christos #endif
322 1.14 christos {
323 1.1 christos ADDNA();
324 1.1 christos }
325 1.1 christos break;
326 1.15 christos case 'm':
327 1.15 christos switch (sa->sa_family) {
328 1.15 christos #ifdef HAVE_NETATALK_AT_H
329 1.15 christos case AF_APPLETALK:
330 1.15 christos ADDS("appletalk");
331 1.15 christos break;
332 1.15 christos #endif
333 1.15 christos case AF_LOCAL:
334 1.15 christos ADDS("unix");
335 1.15 christos break;
336 1.15 christos case AF_INET:
337 1.15 christos ADDS("inet");
338 1.15 christos break;
339 1.15 christos case AF_INET6:
340 1.15 christos ADDS("inet6");
341 1.15 christos break;
342 1.15 christos #ifdef HAVE_NET_IF_DL_H
343 1.15 christos case AF_LINK:
344 1.15 christos ADDS("link");
345 1.15 christos break;
346 1.15 christos #endif
347 1.15 christos default:
348 1.15 christos abort();
349 1.15 christos }
350 1.15 christos break;
351 1.15 christos case 'n':
352 1.15 christos f = p == -1 ? "%a" : (sa->sa_family == AF_INET6
353 1.15 christos ? "[%a]:%p" : "%a:%p");
354 1.15 christos sockaddr_snprintf(nbuf, sizeof(nbuf), f, sa);
355 1.15 christos ADDS(nbuf);
356 1.15 christos break;
357 1.15 christos case 'N':
358 1.15 christos f = p == -1 ? "%A" : "%A:%P";
359 1.15 christos sockaddr_snprintf(nbuf, sizeof(nbuf), f, sa);
360 1.15 christos ADDS(nbuf);
361 1.15 christos break;
362 1.10 christos case 'D':
363 1.10 christos switch (sa->sa_family) {
364 1.14 christos #ifdef HAVE_NETATALK_AT_H
365 1.10 christos case AF_APPLETALK:
366 1.10 christos debug_at(nbuf, sizeof(nbuf), sat);
367 1.10 christos break;
368 1.14 christos #endif
369 1.10 christos case AF_LOCAL:
370 1.10 christos debug_un(nbuf, sizeof(nbuf), sun);
371 1.10 christos break;
372 1.10 christos case AF_INET:
373 1.10 christos debug_in(nbuf, sizeof(nbuf), sin4);
374 1.10 christos break;
375 1.10 christos case AF_INET6:
376 1.10 christos debug_in6(nbuf, sizeof(nbuf), sin6);
377 1.10 christos break;
378 1.14 christos #ifdef HAVE_NET_IF_DL_H
379 1.10 christos case AF_LINK:
380 1.10 christos debug_dl(nbuf, sizeof(nbuf), sdl);
381 1.10 christos break;
382 1.14 christos #endif
383 1.10 christos default:
384 1.10 christos abort();
385 1.10 christos }
386 1.10 christos ADDS(nbuf);
387 1.10 christos break;
388 1.1 christos default:
389 1.1 christos ADDC('%');
390 1.5 atatat if (na == 0)
391 1.5 atatat ADDC('?');
392 1.5 atatat if (*ptr == '\0')
393 1.5 atatat goto done;
394 1.7 dyoung /*FALLTHROUGH*/
395 1.7 dyoung case '%':
396 1.1 christos ADDC(*ptr);
397 1.1 christos break;
398 1.1 christos }
399 1.5 atatat na = 1;
400 1.1 christos }
401 1.1 christos done:
402 1.8 dyoung if (buf < ebuf)
403 1.8 dyoung *buf = '\0';
404 1.8 dyoung else if (len != 0)
405 1.8 dyoung sbuf[len - 1] = '\0';
406 1.6 elad return (int)(buf - sbuf);
407 1.1 christos }
408