xref: /netbsd-src/lib/libutil/sockaddr_snprintf.c (revision 81e0d2b0af8485d94ed5da487d4253841a2e6e45)
1 /*	$NetBSD: sockaddr_snprintf.c,v 1.4 2005/01/13 00:44:25 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.4 2005/01/13 00:44:25 dyoung Exp $");
41 #endif /* LIBC_SCCS and not lint */
42 
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 
47 #include <netinet/in.h>
48 #include <netatalk/at.h>
49 #include <net/if_dl.h>
50 
51 #include <stdio.h>
52 #include <string.h>
53 #include <errno.h>
54 #include <util.h>
55 #include <netdb.h>
56 
57 int
58 sockaddr_snprintf(char *buf, size_t len, const char *fmt,
59     const struct sockaddr *sa)
60 {
61 	const void *a = NULL;
62 	char abuf[1024], nbuf[1024], *addr, *w;
63 	char *ebuf = &buf[len - 1], *sbuf = buf;
64 	const char *ptr, *s;
65 	in_port_t p = (in_port_t)~0;
66 	const struct sockaddr_at *sat = NULL;
67 	const struct sockaddr_in *sin4 = NULL;
68 	const struct sockaddr_in6 *sin6 = NULL;
69 	const struct sockaddr_un *sun = NULL;
70 	const struct sockaddr_dl *sdl = NULL;
71 
72 #define ADDC(c) if (buf < ebuf) *buf++ = c; else buf++
73 #define ADDN()	if (buf < ebuf) *buf = '\0'; else buf[len - 1] = '\0'
74 #define ADDS(p) for (s = p; *s; s++) ADDC(*s)
75 #define ADDNA() ADDS("N/A")
76 
77 	switch (sa->sa_family) {
78 	case AF_UNSPEC:
79 		goto done;
80 	case AF_APPLETALK:
81 		sat = ((const struct sockaddr_at *)(const void *)sa);
82 		p = ntohs(sat->sat_port);
83 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
84 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
85 		break;
86 	case AF_LOCAL:
87 		sun = ((const struct sockaddr_un *)(const void *)sa);
88 		(void)strlcpy(addr = abuf, sun->sun_path, SUN_LEN(sun));
89 		break;
90 	case AF_INET:
91 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
92 		p = ntohs(sin4->sin_port);
93 		a = &sin4->sin_addr;
94 		break;
95 	case AF_INET6:
96 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
97 		p = ntohs(sin6->sin6_port);
98 		a = &sin6->sin6_addr;
99 		break;
100 	case AF_LINK:
101 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
102 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
103 		if ((w = strchr(addr, ':')) != 0) {
104 			*w++ = '\0';
105 			addr = w;
106 
107 		}
108 		break;
109 	default:
110 		errno = EAFNOSUPPORT;
111 		return -1;
112 	}
113 
114 	if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
115 	    sizeof(abuf), NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV) != 0)
116 		return -1;
117 
118 	for (ptr = fmt; *ptr; ptr++) {
119 		if (*ptr != '%') {
120 			ADDC(*ptr);
121 			continue;
122 		}
123 		switch (*++ptr) {
124 		case '\0':
125 			ADDC('%');
126 			goto done;
127 		case 'a':
128 			ADDS(addr);
129 			break;
130 		case 'p':
131 			if (p != (in_port_t)~0) {
132 			    (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
133 			    ADDS(nbuf);
134 			} else {
135 			    ADDNA();
136 			}
137 			break;
138 		case 'f':
139 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
140 			ADDS(nbuf);
141 			break;
142 		case 'l':
143 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
144 			ADDS(nbuf);
145 			break;
146 		case 'I':
147 			if (sdl && addr != abuf) {
148 				ADDS(abuf);
149 			} else {
150 				ADDNA();
151 			}
152 			break;
153 		case 'F':
154 			if (sin6) {
155 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
156 				    sin6->sin6_flowinfo);
157 				ADDS(nbuf);
158 				break;
159 			} else {
160 				ADDNA();
161 			}
162 			break;
163 		case 'S':
164 			if (sin6) {
165 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
166 				    sin6->sin6_scope_id);
167 				ADDS(nbuf);
168 				break;
169 			} else {
170 				ADDNA();
171 			}
172 			break;
173 		case 'R':
174 			if (sat) {
175 				const struct netrange *n =
176 				    &sat->sat_range.r_netrange;
177 				(void)snprintf(nbuf, sizeof(nbuf),
178 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
179 				    n->nr_lastnet);
180 				ADDS(nbuf);
181 			} else {
182 				ADDNA();
183 			}
184 			break;
185 		default:
186 			ADDC('%');
187 			ADDC(*ptr);
188 			break;
189 		}
190 	}
191 done:
192 	ADDN();
193 	return buf - sbuf;
194 }
195