xref: /minix3/lib/libutil/sockaddr_snprintf.c (revision 08ff44c446cce1dfd25870fb4e9daf97f613ca95)
1 /*	$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 #if defined(LIBC_SCCS) && !defined(lint)
33 __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $");
34 #endif /* LIBC_SCCS and not lint */
35 
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
39 
40 #include <netinet/in.h>
41 #ifndef __minix
42 #include <netatalk/at.h>
43 #include <net/if_dl.h>
44 #endif
45 
46 #include <stdio.h>
47 #include <string.h>
48 #include <errno.h>
49 #include <util.h>
50 #include <netdb.h>
51 
52 int
53 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
54     const struct sockaddr * const sa)
55 {
56 	const void *a = NULL;
57 #ifndef __minix
58 	char abuf[1024], nbuf[1024], *addr = NULL, *w = NULL;
59 #else
60 	char abuf[1024], nbuf[1024], *addr = NULL;
61 #endif
62 	char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
63 	char *ebuf = &sbuf[len - 1], *buf = sbuf;
64 	const char *ptr, *s;
65 	int p = -1;
66 #ifndef __minix
67 	const struct sockaddr_at *sat = NULL;
68 #endif
69 	const struct sockaddr_in *sin4 = NULL;
70 #ifndef __minix
71 	const struct sockaddr_in6 *sin6 = NULL;
72 	const struct sockaddr_un *sun = NULL;
73 	const struct sockaddr_dl *sdl = NULL;
74 #endif
75 	int na = 1;
76 
77 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
78 	while (/*CONSTCOND*/0)
79 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
80 	while (/*CONSTCOND*/0)
81 #define ADDNA() do { if (na) ADDS("N/A"); } \
82 	while (/*CONSTCOND*/0)
83 
84 	switch (sa->sa_family) {
85 	case AF_UNSPEC:
86 		goto done;
87 #ifndef __minix
88 	case AF_APPLETALK:
89 		sat = ((const struct sockaddr_at *)(const void *)sa);
90 		p = ntohs(sat->sat_port);
91 		(void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
92 			ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
93 		(void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
94 		break;
95 	case AF_LOCAL:
96 		sun = ((const struct sockaddr_un *)(const void *)sa);
97 		(void)strlcpy(addr = abuf, sun->sun_path, SUN_LEN(sun));
98 		break;
99 #endif
100 	case AF_INET:
101 		sin4 = ((const struct sockaddr_in *)(const void *)sa);
102 		p = ntohs(sin4->sin_port);
103 		a = &sin4->sin_addr;
104 		break;
105 #ifndef __minix
106 	case AF_INET6:
107 		sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
108 		p = ntohs(sin6->sin6_port);
109 		a = &sin6->sin6_addr;
110 		break;
111 	case AF_LINK:
112 		sdl = ((const struct sockaddr_dl *)(const void *)sa);
113 		(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
114 		if ((w = strchr(addr, ':')) != 0) {
115 			*w++ = '\0';
116 			addr = w;
117 		}
118 		break;
119 #endif
120 	default:
121 		errno = EAFNOSUPPORT;
122 		return -1;
123 	}
124 
125 	if (addr == abuf)
126 		name = addr;
127 
128 #ifndef __minix
129 	if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
130 #else
131 	if (a && getnameinfo(sa, (socklen_t)len, addr = abuf,
132 #endif
133 	    (unsigned int)sizeof(abuf), NULL, 0,
134 	    NI_NUMERICHOST|NI_NUMERICSERV) != 0)
135 		return -1;
136 
137 	for (ptr = fmt; *ptr; ptr++) {
138 		if (*ptr != '%') {
139 			ADDC(*ptr);
140 			continue;
141 		}
142 	  next_char:
143 		switch (*++ptr) {
144 		case '?':
145 			na = 0;
146 			goto next_char;
147 		case 'a':
148 			ADDS(addr);
149 			break;
150 		case 'p':
151 			if (p != -1) {
152 				(void)snprintf(nbuf, sizeof(nbuf), "%d", p);
153 				ADDS(nbuf);
154 			} else
155 				ADDNA();
156 			break;
157 		case 'f':
158 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
159 			ADDS(nbuf);
160 			break;
161 		case 'l':
162 #ifndef __minix
163 			(void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
164 #else
165 			(void)snprintf(nbuf, sizeof(nbuf), "%d", len);
166 #endif
167 			ADDS(nbuf);
168 			break;
169 		case 'A':
170 			if (name)
171 				ADDS(name);
172 			else if (!a)
173 				ADDNA();
174 			else {
175 #ifndef __minix
176 				getnameinfo(sa, (socklen_t)sa->sa_len,
177 #else
178 				getnameinfo(sa, (socklen_t)len,
179 #endif
180 					name = Abuf,
181 					(unsigned int)sizeof(nbuf), NULL, 0, 0);
182 				ADDS(name);
183 			}
184 			break;
185 		case 'P':
186 			if (port)
187 				ADDS(port);
188 			else if (p == -1)
189 				ADDNA();
190 			else {
191 #ifndef __minix
192 				getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
193 #else
194 				getnameinfo(sa, (socklen_t)len, NULL, 0,
195 #endif
196 					port = pbuf,
197 					(unsigned int)sizeof(pbuf), 0);
198 				ADDS(port);
199 			}
200 			break;
201 #ifndef __minix
202 		case 'I':
203 			if (sdl && addr != abuf) {
204 				ADDS(abuf);
205 			} else {
206 				ADDNA();
207 			}
208 			break;
209 		case 'F':
210 			if (sin6) {
211 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
212 				    sin6->sin6_flowinfo);
213 				ADDS(nbuf);
214 				break;
215 			} else {
216 				ADDNA();
217 			}
218 			break;
219 		case 'S':
220 			if (sin6) {
221 				(void)snprintf(nbuf, sizeof(nbuf), "%d",
222 				    sin6->sin6_scope_id);
223 				ADDS(nbuf);
224 				break;
225 			} else {
226 				ADDNA();
227 			}
228 			break;
229 		case 'R':
230 			if (sat) {
231 				const struct netrange *n =
232 				    &sat->sat_range.r_netrange;
233 				(void)snprintf(nbuf, sizeof(nbuf),
234 				    "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
235 				    n->nr_lastnet);
236 				ADDS(nbuf);
237 			} else {
238 				ADDNA();
239 			}
240 			break;
241 #endif
242 		default:
243 			ADDC('%');
244 			if (na == 0)
245 				ADDC('?');
246 			if (*ptr == '\0')
247 				goto done;
248 			/*FALLTHROUGH*/
249 		case '%':
250 			ADDC(*ptr);
251 			break;
252 		}
253 		na = 1;
254 	}
255 done:
256 	if (buf < ebuf)
257 		*buf = '\0';
258 	else if (len != 0)
259 		sbuf[len - 1] = '\0';
260 	return (int)(buf - sbuf);
261 }
262