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