xref: /netbsd-src/lib/libutil/sockaddr_snprintf.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: sockaddr_snprintf.c,v 1.6 2005/08/27 17:01:49 elad 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.6 2005/08/27 17:01:49 elad 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 	    (unsigned int)sizeof(abuf), NULL, 0,
125 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
126 		return -1;
127 
128 	for (ptr = fmt; *ptr; ptr++) {
129 		if (*ptr != '%') {
130 			ADDC(*ptr);
131 			continue;
132 		}
133 	  next_char:
134 		switch (*++ptr) {
135 		case '?':
136 			na = 0;
137 			goto next_char;
138 		case 'a':
139 			ADDS(addr);
140 			break;
141 		case 'p':
142 			if (p != -1) {
143 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
144 				ADDS(nbuf);
145 			} else
146 				ADDNA();
147 			break;
148 		case 'f':
149 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
150 			ADDS(nbuf);
151 			break;
152 		case 'l':
153 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
154 			ADDS(nbuf);
155 			break;
156 		case 'A':
157 			if (name)
158 				ADDS(name);
159 			else if (!a)
160 				ADDNA();
161 			else {
162 				getnameinfo(sa, (socklen_t)sa->sa_len,
163 					name = Abuf,
164 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
165 				ADDS(name);
166 			}
167 			break;
168 		case 'P':
169 			if (port)
170 				ADDS(port);
171 			else if (p == -1)
172 				ADDNA();
173 			else {
174 				getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
175 					port = pbuf,
176 					(unsigned int)sizeof(pbuf), 0);
177 				ADDS(port);
178 			}
179 			break;
180 		case 'I':
181 			if (sdl && addr != abuf) {
182 				ADDS(abuf);
183 			} else {
184 				ADDNA();
185 			}
186 			break;
187 		case 'F':
188 			if (sin6) {
189 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
190 				    sin6->sin6_flowinfo);
191 				ADDS(nbuf);
192 				break;
193 			} else {
194 				ADDNA();
195 			}
196 			break;
197 		case 'S':
198 			if (sin6) {
199 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
200 				    sin6->sin6_scope_id);
201 				ADDS(nbuf);
202 				break;
203 			} else {
204 				ADDNA();
205 			}
206 			break;
207 		case 'R':
208 			if (sat) {
209 				const struct netrange *n =
210 				    &sat->sat_range.r_netrange;
211 				(void)snprintf(nbuf, sizeof(nbuf),
212 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
213 				    n->nr_lastnet);
214 				ADDS(nbuf);
215 			} else {
216 				ADDNA();
217 			}
218 			break;
219 		default:
220 			ADDC('%');
221 			if (na == 0)
222 				ADDC('?');
223 			if (*ptr == '\0')
224 				goto done;
225 			ADDC(*ptr);
226 			break;
227 		}
228 		na = 1;
229 	}
230 done:
231 	ADDN();
232 	return (int)(buf - sbuf);
233 }
234