Home | History | Annotate | Line # | Download | only in libutil
sockaddr_snprintf.c revision 1.9.28.2
      1  1.9.28.1       tls /*	$NetBSD: sockaddr_snprintf.c,v 1.9.28.2 2014/08/20 00:02:21 tls 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.9.28.1       tls __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9.28.2 2014/08/20 00:02:21 tls 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.9.28.1       tls #include <stdlib.h>
     48       1.1  christos #include <util.h>
     49       1.1  christos #include <netdb.h>
     50       1.1  christos 
     51  1.9.28.1       tls static int
     52  1.9.28.1       tls debug_at(char *str, size_t len, const struct sockaddr_at *sat)
     53  1.9.28.1       tls {
     54  1.9.28.1       tls 	return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
     55  1.9.28.1       tls 	    "sat_addr.s_net=%u, sat_addr.s_node=%u, "
     56  1.9.28.1       tls 	    "sat_range.r_netrange.nr_phase=%u, "
     57  1.9.28.1       tls 	    "sat_range.r_netrange.nr_firstnet=%u, "
     58  1.9.28.1       tls 	    "sat_range.r_netrange.nr_lastnet=%u",
     59  1.9.28.1       tls 	    sat->sat_len, sat->sat_family, sat->sat_port,
     60  1.9.28.1       tls 	    sat->sat_addr.s_net, sat->sat_addr.s_node,
     61  1.9.28.1       tls 	    sat->sat_range.r_netrange.nr_phase,
     62  1.9.28.1       tls 	    sat->sat_range.r_netrange.nr_firstnet,
     63  1.9.28.1       tls 	    sat->sat_range.r_netrange.nr_lastnet);
     64  1.9.28.1       tls }
     65  1.9.28.1       tls 
     66  1.9.28.1       tls static int
     67  1.9.28.1       tls debug_in(char *str, size_t len, const struct sockaddr_in *sin)
     68  1.9.28.1       tls {
     69  1.9.28.1       tls 	return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
     70  1.9.28.1       tls 	    "sin_addr.s_addr=%08x",
     71  1.9.28.1       tls 	    sin->sin_len, sin->sin_family, sin->sin_port,
     72  1.9.28.1       tls 	    sin->sin_addr.s_addr);
     73  1.9.28.1       tls }
     74  1.9.28.1       tls 
     75  1.9.28.1       tls static int
     76  1.9.28.1       tls debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
     77  1.9.28.1       tls {
     78  1.9.28.1       tls 	const uint8_t *s = sin6->sin6_addr.s6_addr;
     79  1.9.28.1       tls 
     80  1.9.28.1       tls 	return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
     81  1.9.28.1       tls 	    "sin6_flowinfo=%u, "
     82  1.9.28.1       tls 	    "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
     83  1.9.28.1       tls 	    "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
     84  1.9.28.1       tls 	    sin6->sin6_len, sin6->sin6_family, sin6->sin6_port,
     85  1.9.28.1       tls 	    sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
     86  1.9.28.1       tls 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
     87  1.9.28.1       tls 	    s[0xe], s[0xf], sin6->sin6_scope_id);
     88  1.9.28.1       tls }
     89  1.9.28.1       tls 
     90  1.9.28.1       tls static int
     91  1.9.28.1       tls debug_un(char *str, size_t len, const struct sockaddr_un *sun)
     92  1.9.28.1       tls {
     93  1.9.28.1       tls 	return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
     94  1.9.28.1       tls 	    sun->sun_len, sun->sun_family, (int)sizeof(sun->sun_path),
     95  1.9.28.1       tls 	    sun->sun_path);
     96  1.9.28.1       tls }
     97  1.9.28.1       tls 
     98  1.9.28.1       tls static int
     99  1.9.28.1       tls debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
    100  1.9.28.1       tls {
    101  1.9.28.1       tls 	const uint8_t *s = (const void *)sdl->sdl_data;
    102  1.9.28.1       tls 
    103  1.9.28.1       tls 	return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
    104  1.9.28.1       tls 	    "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
    105  1.9.28.1       tls 	    "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
    106  1.9.28.1       tls 	    sdl->sdl_len, sdl->sdl_family, sdl->sdl_index,
    107  1.9.28.1       tls 	    sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
    108  1.9.28.1       tls 	    s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
    109  1.9.28.1       tls 	    s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
    110  1.9.28.1       tls }
    111  1.9.28.1       tls 
    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.9.28.2       tls 		(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.1  christos 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
    163       1.1  christos 		if ((w = strchr(addr, ':')) != 0) {
    164       1.1  christos 			*w++ = '\0';
    165       1.1  christos 			addr = w;
    166       1.1  christos 		}
    167       1.1  christos 		break;
    168       1.1  christos 	default:
    169       1.1  christos 		errno = EAFNOSUPPORT;
    170       1.1  christos 		return -1;
    171       1.1  christos 	}
    172       1.1  christos 
    173       1.5    atatat 	if (addr == abuf)
    174       1.5    atatat 		name = addr;
    175       1.5    atatat 
    176       1.3  christos 	if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
    177       1.6      elad 	    (unsigned int)sizeof(abuf), NULL, 0,
    178       1.6      elad 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
    179       1.1  christos 		return -1;
    180       1.1  christos 
    181       1.1  christos 	for (ptr = fmt; *ptr; ptr++) {
    182       1.1  christos 		if (*ptr != '%') {
    183       1.1  christos 			ADDC(*ptr);
    184       1.1  christos 			continue;
    185       1.1  christos 		}
    186       1.5    atatat 	  next_char:
    187       1.1  christos 		switch (*++ptr) {
    188       1.5    atatat 		case '?':
    189       1.5    atatat 			na = 0;
    190       1.5    atatat 			goto next_char;
    191       1.1  christos 		case 'a':
    192       1.1  christos 			ADDS(addr);
    193       1.1  christos 			break;
    194       1.1  christos 		case 'p':
    195       1.5    atatat 			if (p != -1) {
    196       1.5    atatat 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
    197       1.5    atatat 				ADDS(nbuf);
    198       1.5    atatat 			} else
    199       1.5    atatat 				ADDNA();
    200       1.1  christos 			break;
    201       1.1  christos 		case 'f':
    202       1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
    203       1.1  christos 			ADDS(nbuf);
    204       1.1  christos 			break;
    205       1.1  christos 		case 'l':
    206       1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
    207       1.1  christos 			ADDS(nbuf);
    208       1.1  christos 			break;
    209       1.5    atatat 		case 'A':
    210       1.5    atatat 			if (name)
    211       1.5    atatat 				ADDS(name);
    212       1.5    atatat 			else if (!a)
    213       1.5    atatat 				ADDNA();
    214       1.5    atatat 			else {
    215       1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len,
    216       1.6      elad 					name = Abuf,
    217       1.6      elad 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
    218       1.5    atatat 				ADDS(name);
    219       1.5    atatat 			}
    220       1.5    atatat 			break;
    221       1.5    atatat 		case 'P':
    222       1.5    atatat 			if (port)
    223       1.5    atatat 				ADDS(port);
    224       1.5    atatat 			else if (p == -1)
    225       1.5    atatat 				ADDNA();
    226       1.5    atatat 			else {
    227       1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
    228       1.6      elad 					port = pbuf,
    229       1.6      elad 					(unsigned int)sizeof(pbuf), 0);
    230       1.5    atatat 				ADDS(port);
    231       1.5    atatat 			}
    232       1.5    atatat 			break;
    233       1.1  christos 		case 'I':
    234       1.1  christos 			if (sdl && addr != abuf) {
    235       1.1  christos 				ADDS(abuf);
    236       1.1  christos 			} else {
    237       1.1  christos 				ADDNA();
    238       1.1  christos 			}
    239       1.1  christos 			break;
    240       1.1  christos 		case 'F':
    241       1.1  christos 			if (sin6) {
    242       1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    243       1.1  christos 				    sin6->sin6_flowinfo);
    244       1.1  christos 				ADDS(nbuf);
    245       1.1  christos 				break;
    246       1.1  christos 			} else {
    247       1.1  christos 				ADDNA();
    248       1.1  christos 			}
    249       1.1  christos 			break;
    250       1.1  christos 		case 'S':
    251       1.1  christos 			if (sin6) {
    252       1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    253       1.1  christos 				    sin6->sin6_scope_id);
    254       1.1  christos 				ADDS(nbuf);
    255       1.1  christos 				break;
    256       1.1  christos 			} else {
    257       1.1  christos 				ADDNA();
    258       1.1  christos 			}
    259       1.1  christos 			break;
    260       1.1  christos 		case 'R':
    261       1.1  christos 			if (sat) {
    262       1.1  christos 				const struct netrange *n =
    263       1.1  christos 				    &sat->sat_range.r_netrange;
    264       1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf),
    265       1.1  christos 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
    266       1.1  christos 				    n->nr_lastnet);
    267       1.1  christos 				ADDS(nbuf);
    268       1.1  christos 			} else {
    269       1.1  christos 				ADDNA();
    270       1.1  christos 			}
    271       1.1  christos 			break;
    272  1.9.28.1       tls 		case 'D':
    273  1.9.28.1       tls 			switch (sa->sa_family) {
    274  1.9.28.1       tls 			case AF_APPLETALK:
    275  1.9.28.1       tls 				debug_at(nbuf, sizeof(nbuf), sat);
    276  1.9.28.1       tls 				break;
    277  1.9.28.1       tls 			case AF_LOCAL:
    278  1.9.28.1       tls 				debug_un(nbuf, sizeof(nbuf), sun);
    279  1.9.28.1       tls 				break;
    280  1.9.28.1       tls 			case AF_INET:
    281  1.9.28.1       tls 				debug_in(nbuf, sizeof(nbuf), sin4);
    282  1.9.28.1       tls 				break;
    283  1.9.28.1       tls 			case AF_INET6:
    284  1.9.28.1       tls 				debug_in6(nbuf, sizeof(nbuf), sin6);
    285  1.9.28.1       tls 				break;
    286  1.9.28.1       tls 			case AF_LINK:
    287  1.9.28.1       tls 				debug_dl(nbuf, sizeof(nbuf), sdl);
    288  1.9.28.1       tls 				break;
    289  1.9.28.1       tls 			default:
    290  1.9.28.1       tls 				abort();
    291  1.9.28.1       tls 			}
    292  1.9.28.1       tls 			ADDS(nbuf);
    293  1.9.28.1       tls 			break;
    294       1.1  christos 		default:
    295       1.1  christos 			ADDC('%');
    296       1.5    atatat 			if (na == 0)
    297       1.5    atatat 				ADDC('?');
    298       1.5    atatat 			if (*ptr == '\0')
    299       1.5    atatat 				goto done;
    300       1.7    dyoung 			/*FALLTHROUGH*/
    301       1.7    dyoung 		case '%':
    302       1.1  christos 			ADDC(*ptr);
    303       1.1  christos 			break;
    304       1.1  christos 		}
    305       1.5    atatat 		na = 1;
    306       1.1  christos 	}
    307       1.1  christos done:
    308       1.8    dyoung 	if (buf < ebuf)
    309       1.8    dyoung 		*buf = '\0';
    310       1.8    dyoung 	else if (len != 0)
    311       1.8    dyoung 		sbuf[len - 1] = '\0';
    312       1.6      elad 	return (int)(buf - sbuf);
    313       1.1  christos }
    314