Home | History | Annotate | Line # | Download | only in libutil
sockaddr_snprintf.c revision 1.7
      1  1.7    dyoung /*	$NetBSD: sockaddr_snprintf.c,v 1.7 2006/12/09 20:18:43 dyoung 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  christos  *    must display the following acknowledgement:
     20  1.1  christos  *        This product includes software developed by the NetBSD
     21  1.1  christos  *        Foundation, Inc. and its contributors.
     22  1.1  christos  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  christos  *    contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  christos  */
     38  1.1  christos #include <sys/cdefs.h>
     39  1.1  christos #if defined(LIBC_SCCS) && !defined(lint)
     40  1.7    dyoung __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.7 2006/12/09 20:18:43 dyoung Exp $");
     41  1.1  christos #endif /* LIBC_SCCS and not lint */
     42  1.1  christos 
     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 #include <netatalk/at.h>
     49  1.1  christos #include <net/if_dl.h>
     50  1.1  christos 
     51  1.1  christos #include <stdio.h>
     52  1.1  christos #include <string.h>
     53  1.1  christos #include <errno.h>
     54  1.1  christos #include <util.h>
     55  1.1  christos #include <netdb.h>
     56  1.1  christos 
     57  1.1  christos int
     58  1.1  christos sockaddr_snprintf(char *buf, size_t len, const char *fmt,
     59  1.1  christos     const struct sockaddr *sa)
     60  1.1  christos {
     61  1.1  christos 	const void *a = NULL;
     62  1.5    atatat 	char abuf[1024], nbuf[1024], *addr = NULL, *w = NULL;
     63  1.5    atatat 	char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
     64  1.1  christos 	char *ebuf = &buf[len - 1], *sbuf = buf;
     65  1.1  christos 	const char *ptr, *s;
     66  1.5    atatat 	int p = -1;
     67  1.1  christos 	const struct sockaddr_at *sat = NULL;
     68  1.1  christos 	const struct sockaddr_in *sin4 = NULL;
     69  1.1  christos 	const struct sockaddr_in6 *sin6 = NULL;
     70  1.1  christos 	const struct sockaddr_un *sun = NULL;
     71  1.1  christos 	const struct sockaddr_dl *sdl = NULL;
     72  1.5    atatat 	int na = 1;
     73  1.1  christos 
     74  1.5    atatat #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
     75  1.5    atatat 	while (/*CONSTCOND*/0)
     76  1.5    atatat #define ADDN()	do { if (buf < ebuf) *buf = '\0'; else buf[len - 1] = '\0'; } \
     77  1.5    atatat 	while (/*CONSTCOND*/0)
     78  1.5    atatat #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
     79  1.5    atatat 	while (/*CONSTCOND*/0)
     80  1.5    atatat #define ADDNA() do { if (na) ADDS("N/A"); } \
     81  1.5    atatat 	while (/*CONSTCOND*/0)
     82  1.1  christos 
     83  1.1  christos 	switch (sa->sa_family) {
     84  1.1  christos 	case AF_UNSPEC:
     85  1.1  christos 		goto done;
     86  1.1  christos 	case AF_APPLETALK:
     87  1.1  christos 		sat = ((const struct sockaddr_at *)(const void *)sa);
     88  1.1  christos 		p = ntohs(sat->sat_port);
     89  1.1  christos 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
     90  1.1  christos 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
     91  1.5    atatat 		(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
     92  1.1  christos 		break;
     93  1.1  christos 	case AF_LOCAL:
     94  1.1  christos 		sun = ((const struct sockaddr_un *)(const void *)sa);
     95  1.1  christos 		(void)strlcpy(addr = abuf, sun->sun_path, SUN_LEN(sun));
     96  1.1  christos 		break;
     97  1.1  christos 	case AF_INET:
     98  1.1  christos 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
     99  1.1  christos 		p = ntohs(sin4->sin_port);
    100  1.1  christos 		a = &sin4->sin_addr;
    101  1.1  christos 		break;
    102  1.1  christos 	case AF_INET6:
    103  1.1  christos 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
    104  1.1  christos 		p = ntohs(sin6->sin6_port);
    105  1.1  christos 		a = &sin6->sin6_addr;
    106  1.1  christos 		break;
    107  1.1  christos 	case AF_LINK:
    108  1.1  christos 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
    109  1.1  christos 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
    110  1.1  christos 		if ((w = strchr(addr, ':')) != 0) {
    111  1.1  christos 			*w++ = '\0';
    112  1.1  christos 			addr = w;
    113  1.1  christos 		}
    114  1.1  christos 		break;
    115  1.1  christos 	default:
    116  1.1  christos 		errno = EAFNOSUPPORT;
    117  1.1  christos 		return -1;
    118  1.1  christos 	}
    119  1.1  christos 
    120  1.5    atatat 	if (addr == abuf)
    121  1.5    atatat 		name = addr;
    122  1.5    atatat 
    123  1.3  christos 	if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
    124  1.6      elad 	    (unsigned int)sizeof(abuf), NULL, 0,
    125  1.6      elad 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
    126  1.1  christos 		return -1;
    127  1.1  christos 
    128  1.1  christos 	for (ptr = fmt; *ptr; ptr++) {
    129  1.1  christos 		if (*ptr != '%') {
    130  1.1  christos 			ADDC(*ptr);
    131  1.1  christos 			continue;
    132  1.1  christos 		}
    133  1.5    atatat 	  next_char:
    134  1.1  christos 		switch (*++ptr) {
    135  1.5    atatat 		case '?':
    136  1.5    atatat 			na = 0;
    137  1.5    atatat 			goto next_char;
    138  1.1  christos 		case 'a':
    139  1.1  christos 			ADDS(addr);
    140  1.1  christos 			break;
    141  1.1  christos 		case 'p':
    142  1.5    atatat 			if (p != -1) {
    143  1.5    atatat 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
    144  1.5    atatat 				ADDS(nbuf);
    145  1.5    atatat 			} else
    146  1.5    atatat 				ADDNA();
    147  1.1  christos 			break;
    148  1.1  christos 		case 'f':
    149  1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
    150  1.1  christos 			ADDS(nbuf);
    151  1.1  christos 			break;
    152  1.1  christos 		case 'l':
    153  1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
    154  1.1  christos 			ADDS(nbuf);
    155  1.1  christos 			break;
    156  1.5    atatat 		case 'A':
    157  1.5    atatat 			if (name)
    158  1.5    atatat 				ADDS(name);
    159  1.5    atatat 			else if (!a)
    160  1.5    atatat 				ADDNA();
    161  1.5    atatat 			else {
    162  1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len,
    163  1.6      elad 					name = Abuf,
    164  1.6      elad 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
    165  1.5    atatat 				ADDS(name);
    166  1.5    atatat 			}
    167  1.5    atatat 			break;
    168  1.5    atatat 		case 'P':
    169  1.5    atatat 			if (port)
    170  1.5    atatat 				ADDS(port);
    171  1.5    atatat 			else if (p == -1)
    172  1.5    atatat 				ADDNA();
    173  1.5    atatat 			else {
    174  1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
    175  1.6      elad 					port = pbuf,
    176  1.6      elad 					(unsigned int)sizeof(pbuf), 0);
    177  1.5    atatat 				ADDS(port);
    178  1.5    atatat 			}
    179  1.5    atatat 			break;
    180  1.1  christos 		case 'I':
    181  1.1  christos 			if (sdl && addr != abuf) {
    182  1.1  christos 				ADDS(abuf);
    183  1.1  christos 			} else {
    184  1.1  christos 				ADDNA();
    185  1.1  christos 			}
    186  1.1  christos 			break;
    187  1.1  christos 		case 'F':
    188  1.1  christos 			if (sin6) {
    189  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    190  1.1  christos 				    sin6->sin6_flowinfo);
    191  1.1  christos 				ADDS(nbuf);
    192  1.1  christos 				break;
    193  1.1  christos 			} else {
    194  1.1  christos 				ADDNA();
    195  1.1  christos 			}
    196  1.1  christos 			break;
    197  1.1  christos 		case 'S':
    198  1.1  christos 			if (sin6) {
    199  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    200  1.1  christos 				    sin6->sin6_scope_id);
    201  1.1  christos 				ADDS(nbuf);
    202  1.1  christos 				break;
    203  1.1  christos 			} else {
    204  1.1  christos 				ADDNA();
    205  1.1  christos 			}
    206  1.1  christos 			break;
    207  1.1  christos 		case 'R':
    208  1.1  christos 			if (sat) {
    209  1.1  christos 				const struct netrange *n =
    210  1.1  christos 				    &sat->sat_range.r_netrange;
    211  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf),
    212  1.1  christos 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
    213  1.1  christos 				    n->nr_lastnet);
    214  1.1  christos 				ADDS(nbuf);
    215  1.1  christos 			} else {
    216  1.1  christos 				ADDNA();
    217  1.1  christos 			}
    218  1.1  christos 			break;
    219  1.1  christos 		default:
    220  1.1  christos 			ADDC('%');
    221  1.5    atatat 			if (na == 0)
    222  1.5    atatat 				ADDC('?');
    223  1.5    atatat 			if (*ptr == '\0')
    224  1.5    atatat 				goto done;
    225  1.7    dyoung 			/*FALLTHROUGH*/
    226  1.7    dyoung 		case '%':
    227  1.1  christos 			ADDC(*ptr);
    228  1.1  christos 			break;
    229  1.1  christos 		}
    230  1.5    atatat 		na = 1;
    231  1.1  christos 	}
    232  1.1  christos done:
    233  1.1  christos 	ADDN();
    234  1.6      elad 	return (int)(buf - sbuf);
    235  1.1  christos }
    236