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