Home | History | Annotate | Line # | Download | only in libutil
sockaddr_snprintf.c revision 1.8
      1  1.8    dyoung /*	$NetBSD: sockaddr_snprintf.c,v 1.8 2007/07/24 08:45:45 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.8    dyoung __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.8 2007/07/24 08:45:45 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.8    dyoung sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
     59  1.8    dyoung     const struct sockaddr * const 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.8    dyoung 	char *ebuf = &sbuf[len - 1], *buf = sbuf;
     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 ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
     77  1.5    atatat 	while (/*CONSTCOND*/0)
     78  1.5    atatat #define ADDNA() do { if (na) ADDS("N/A"); } \
     79  1.5    atatat 	while (/*CONSTCOND*/0)
     80  1.1  christos 
     81  1.1  christos 	switch (sa->sa_family) {
     82  1.1  christos 	case AF_UNSPEC:
     83  1.1  christos 		goto done;
     84  1.1  christos 	case AF_APPLETALK:
     85  1.1  christos 		sat = ((const struct sockaddr_at *)(const void *)sa);
     86  1.1  christos 		p = ntohs(sat->sat_port);
     87  1.1  christos 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
     88  1.1  christos 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
     89  1.5    atatat 		(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
     90  1.1  christos 		break;
     91  1.1  christos 	case AF_LOCAL:
     92  1.1  christos 		sun = ((const struct sockaddr_un *)(const void *)sa);
     93  1.1  christos 		(void)strlcpy(addr = abuf, sun->sun_path, SUN_LEN(sun));
     94  1.1  christos 		break;
     95  1.1  christos 	case AF_INET:
     96  1.1  christos 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
     97  1.1  christos 		p = ntohs(sin4->sin_port);
     98  1.1  christos 		a = &sin4->sin_addr;
     99  1.1  christos 		break;
    100  1.1  christos 	case AF_INET6:
    101  1.1  christos 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
    102  1.1  christos 		p = ntohs(sin6->sin6_port);
    103  1.1  christos 		a = &sin6->sin6_addr;
    104  1.1  christos 		break;
    105  1.1  christos 	case AF_LINK:
    106  1.1  christos 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
    107  1.1  christos 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
    108  1.1  christos 		if ((w = strchr(addr, ':')) != 0) {
    109  1.1  christos 			*w++ = '\0';
    110  1.1  christos 			addr = w;
    111  1.1  christos 		}
    112  1.1  christos 		break;
    113  1.1  christos 	default:
    114  1.1  christos 		errno = EAFNOSUPPORT;
    115  1.1  christos 		return -1;
    116  1.1  christos 	}
    117  1.1  christos 
    118  1.5    atatat 	if (addr == abuf)
    119  1.5    atatat 		name = addr;
    120  1.5    atatat 
    121  1.3  christos 	if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
    122  1.6      elad 	    (unsigned int)sizeof(abuf), NULL, 0,
    123  1.6      elad 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
    124  1.1  christos 		return -1;
    125  1.1  christos 
    126  1.1  christos 	for (ptr = fmt; *ptr; ptr++) {
    127  1.1  christos 		if (*ptr != '%') {
    128  1.1  christos 			ADDC(*ptr);
    129  1.1  christos 			continue;
    130  1.1  christos 		}
    131  1.5    atatat 	  next_char:
    132  1.1  christos 		switch (*++ptr) {
    133  1.5    atatat 		case '?':
    134  1.5    atatat 			na = 0;
    135  1.5    atatat 			goto next_char;
    136  1.1  christos 		case 'a':
    137  1.1  christos 			ADDS(addr);
    138  1.1  christos 			break;
    139  1.1  christos 		case 'p':
    140  1.5    atatat 			if (p != -1) {
    141  1.5    atatat 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
    142  1.5    atatat 				ADDS(nbuf);
    143  1.5    atatat 			} else
    144  1.5    atatat 				ADDNA();
    145  1.1  christos 			break;
    146  1.1  christos 		case 'f':
    147  1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
    148  1.1  christos 			ADDS(nbuf);
    149  1.1  christos 			break;
    150  1.1  christos 		case 'l':
    151  1.1  christos 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
    152  1.1  christos 			ADDS(nbuf);
    153  1.1  christos 			break;
    154  1.5    atatat 		case 'A':
    155  1.5    atatat 			if (name)
    156  1.5    atatat 				ADDS(name);
    157  1.5    atatat 			else if (!a)
    158  1.5    atatat 				ADDNA();
    159  1.5    atatat 			else {
    160  1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len,
    161  1.6      elad 					name = Abuf,
    162  1.6      elad 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
    163  1.5    atatat 				ADDS(name);
    164  1.5    atatat 			}
    165  1.5    atatat 			break;
    166  1.5    atatat 		case 'P':
    167  1.5    atatat 			if (port)
    168  1.5    atatat 				ADDS(port);
    169  1.5    atatat 			else if (p == -1)
    170  1.5    atatat 				ADDNA();
    171  1.5    atatat 			else {
    172  1.5    atatat 				getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
    173  1.6      elad 					port = pbuf,
    174  1.6      elad 					(unsigned int)sizeof(pbuf), 0);
    175  1.5    atatat 				ADDS(port);
    176  1.5    atatat 			}
    177  1.5    atatat 			break;
    178  1.1  christos 		case 'I':
    179  1.1  christos 			if (sdl && addr != abuf) {
    180  1.1  christos 				ADDS(abuf);
    181  1.1  christos 			} else {
    182  1.1  christos 				ADDNA();
    183  1.1  christos 			}
    184  1.1  christos 			break;
    185  1.1  christos 		case 'F':
    186  1.1  christos 			if (sin6) {
    187  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    188  1.1  christos 				    sin6->sin6_flowinfo);
    189  1.1  christos 				ADDS(nbuf);
    190  1.1  christos 				break;
    191  1.1  christos 			} else {
    192  1.1  christos 				ADDNA();
    193  1.1  christos 			}
    194  1.1  christos 			break;
    195  1.1  christos 		case 'S':
    196  1.1  christos 			if (sin6) {
    197  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
    198  1.1  christos 				    sin6->sin6_scope_id);
    199  1.1  christos 				ADDS(nbuf);
    200  1.1  christos 				break;
    201  1.1  christos 			} else {
    202  1.1  christos 				ADDNA();
    203  1.1  christos 			}
    204  1.1  christos 			break;
    205  1.1  christos 		case 'R':
    206  1.1  christos 			if (sat) {
    207  1.1  christos 				const struct netrange *n =
    208  1.1  christos 				    &sat->sat_range.r_netrange;
    209  1.1  christos 				(void)snprintf(nbuf, sizeof(nbuf),
    210  1.1  christos 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
    211  1.1  christos 				    n->nr_lastnet);
    212  1.1  christos 				ADDS(nbuf);
    213  1.1  christos 			} else {
    214  1.1  christos 				ADDNA();
    215  1.1  christos 			}
    216  1.1  christos 			break;
    217  1.1  christos 		default:
    218  1.1  christos 			ADDC('%');
    219  1.5    atatat 			if (na == 0)
    220  1.5    atatat 				ADDC('?');
    221  1.5    atatat 			if (*ptr == '\0')
    222  1.5    atatat 				goto done;
    223  1.7    dyoung 			/*FALLTHROUGH*/
    224  1.7    dyoung 		case '%':
    225  1.1  christos 			ADDC(*ptr);
    226  1.1  christos 			break;
    227  1.1  christos 		}
    228  1.5    atatat 		na = 1;
    229  1.1  christos 	}
    230  1.1  christos done:
    231  1.8    dyoung 	if (buf < ebuf)
    232  1.8    dyoung 		*buf = '\0';
    233  1.8    dyoung 	else if (len != 0)
    234  1.8    dyoung 		sbuf[len - 1] = '\0';
    235  1.6      elad 	return (int)(buf - sbuf);
    236  1.1  christos }
    237