Remove building with NOCRYPTO option
[minix.git] / lib / libutil / sockaddr_snprintf.c
blob0dcd0eba9d16a51da4cda9f764bc88330ebd5183
1 /* $NetBSD: sockaddr_snprintf.c,v 1.11 2013/12/31 12:58:02 mlelstv Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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.
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.
31 #include <sys/cdefs.h>
32 #if defined(LIBC_SCCS) && !defined(lint)
33 __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.11 2013/12/31 12:58:02 mlelstv Exp $");
34 #endif /* LIBC_SCCS and not lint */
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/un.h>
40 #include <netinet/in.h>
41 #include <netatalk/at.h>
42 #include <net/if_dl.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <util.h>
49 #include <netdb.h>
51 static int
52 debug_at(char *str, size_t len, const struct sockaddr_at *sat)
54 return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
55 "sat_addr.s_net=%u, sat_addr.s_node=%u, "
56 "sat_range.r_netrange.nr_phase=%u, "
57 "sat_range.r_netrange.nr_firstnet=%u, "
58 "sat_range.r_netrange.nr_lastnet=%u",
59 sat->sat_len, sat->sat_family, sat->sat_port,
60 sat->sat_addr.s_net, sat->sat_addr.s_node,
61 sat->sat_range.r_netrange.nr_phase,
62 sat->sat_range.r_netrange.nr_firstnet,
63 sat->sat_range.r_netrange.nr_lastnet);
66 static int
67 debug_in(char *str, size_t len, const struct sockaddr_in *sin)
69 return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
70 "sin_addr.s_addr=%08x",
71 sin->sin_len, sin->sin_family, sin->sin_port,
72 sin->sin_addr.s_addr);
75 static int
76 debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
78 const uint8_t *s = sin6->sin6_addr.s6_addr;
80 return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
81 "sin6_flowinfo=%u, "
82 "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
83 "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
84 sin6->sin6_len, sin6->sin6_family, sin6->sin6_port,
85 sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
86 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
87 s[0xe], s[0xf], sin6->sin6_scope_id);
90 static int
91 debug_un(char *str, size_t len, const struct sockaddr_un *sun)
93 return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
94 sun->sun_len, sun->sun_family, (int)sizeof(sun->sun_path),
95 sun->sun_path);
98 static int
99 debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
101 const uint8_t *s = (const void *)sdl->sdl_data;
103 return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
104 "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
105 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
106 sdl->sdl_len, sdl->sdl_family, sdl->sdl_index,
107 sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
108 s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
109 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
113 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
114 const struct sockaddr * const sa)
116 const void *a = NULL;
117 char abuf[1024], nbuf[1024], *addr = NULL, *w = NULL;
118 char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
119 char *ebuf = &sbuf[len - 1], *buf = sbuf;
120 const char *ptr, *s;
121 int p = -1;
122 const struct sockaddr_at *sat = NULL;
123 const struct sockaddr_in *sin4 = NULL;
124 const struct sockaddr_in6 *sin6 = NULL;
125 const struct sockaddr_un *sun = NULL;
126 const struct sockaddr_dl *sdl = NULL;
127 int na = 1;
129 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
130 while (/*CONSTCOND*/0)
131 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
132 while (/*CONSTCOND*/0)
133 #define ADDNA() do { if (na) ADDS("N/A"); } \
134 while (/*CONSTCOND*/0)
136 switch (sa->sa_family) {
137 case AF_UNSPEC:
138 goto done;
139 case AF_APPLETALK:
140 sat = ((const struct sockaddr_at *)(const void *)sa);
141 p = ntohs(sat->sat_port);
142 (void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
143 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
144 (void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
145 break;
146 case AF_LOCAL:
147 sun = ((const struct sockaddr_un *)(const void *)sa);
148 (void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
149 break;
150 case AF_INET:
151 sin4 = ((const struct sockaddr_in *)(const void *)sa);
152 p = ntohs(sin4->sin_port);
153 a = &sin4->sin_addr;
154 break;
155 case AF_INET6:
156 sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
157 p = ntohs(sin6->sin6_port);
158 a = &sin6->sin6_addr;
159 break;
160 case AF_LINK:
161 sdl = ((const struct sockaddr_dl *)(const void *)sa);
162 (void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
163 if ((w = strchr(addr, ':')) != 0) {
164 *w++ = '\0';
165 addr = w;
167 break;
168 default:
169 errno = EAFNOSUPPORT;
170 return -1;
173 if (addr == abuf)
174 name = addr;
176 if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
177 (unsigned int)sizeof(abuf), NULL, 0,
178 NI_NUMERICHOST|NI_NUMERICSERV) != 0)
179 return -1;
181 for (ptr = fmt; *ptr; ptr++) {
182 if (*ptr != '%') {
183 ADDC(*ptr);
184 continue;
186 next_char:
187 switch (*++ptr) {
188 case '?':
189 na = 0;
190 goto next_char;
191 case 'a':
192 ADDS(addr);
193 break;
194 case 'p':
195 if (p != -1) {
196 (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
197 ADDS(nbuf);
198 } else
199 ADDNA();
200 break;
201 case 'f':
202 (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
203 ADDS(nbuf);
204 break;
205 case 'l':
206 (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
207 ADDS(nbuf);
208 break;
209 case 'A':
210 if (name)
211 ADDS(name);
212 else if (!a)
213 ADDNA();
214 else {
215 getnameinfo(sa, (socklen_t)sa->sa_len,
216 name = Abuf,
217 (unsigned int)sizeof(nbuf), NULL, 0, 0);
218 ADDS(name);
220 break;
221 case 'P':
222 if (port)
223 ADDS(port);
224 else if (p == -1)
225 ADDNA();
226 else {
227 getnameinfo(sa, (socklen_t)sa->sa_len, NULL, 0,
228 port = pbuf,
229 (unsigned int)sizeof(pbuf), 0);
230 ADDS(port);
232 break;
233 case 'I':
234 if (sdl && addr != abuf) {
235 ADDS(abuf);
236 } else {
237 ADDNA();
239 break;
240 case 'F':
241 if (sin6) {
242 (void)snprintf(nbuf, sizeof(nbuf), "%d",
243 sin6->sin6_flowinfo);
244 ADDS(nbuf);
245 break;
246 } else {
247 ADDNA();
249 break;
250 case 'S':
251 if (sin6) {
252 (void)snprintf(nbuf, sizeof(nbuf), "%d",
253 sin6->sin6_scope_id);
254 ADDS(nbuf);
255 break;
256 } else {
257 ADDNA();
259 break;
260 case 'R':
261 if (sat) {
262 const struct netrange *n =
263 &sat->sat_range.r_netrange;
264 (void)snprintf(nbuf, sizeof(nbuf),
265 "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
266 n->nr_lastnet);
267 ADDS(nbuf);
268 } else {
269 ADDNA();
271 break;
272 case 'D':
273 switch (sa->sa_family) {
274 case AF_APPLETALK:
275 debug_at(nbuf, sizeof(nbuf), sat);
276 break;
277 case AF_LOCAL:
278 debug_un(nbuf, sizeof(nbuf), sun);
279 break;
280 case AF_INET:
281 debug_in(nbuf, sizeof(nbuf), sin4);
282 break;
283 case AF_INET6:
284 debug_in6(nbuf, sizeof(nbuf), sin6);
285 break;
286 case AF_LINK:
287 debug_dl(nbuf, sizeof(nbuf), sdl);
288 break;
289 default:
290 abort();
292 ADDS(nbuf);
293 break;
294 default:
295 ADDC('%');
296 if (na == 0)
297 ADDC('?');
298 if (*ptr == '\0')
299 goto done;
300 /*FALLTHROUGH*/
301 case '%':
302 ADDC(*ptr);
303 break;
305 na = 1;
307 done:
308 if (buf < ebuf)
309 *buf = '\0';
310 else if (len != 0)
311 sbuf[len - 1] = '\0';
312 return (int)(buf - sbuf);