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