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