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