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