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