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