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