sockaddr_snprintf.c revision 1.3 1 1.3 christos /* $NetBSD: sockaddr_snprintf.c,v 1.3 2004/12/11 06:41:16 christos Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1 christos * by Christos Zoulas.
9 1.1 christos *
10 1.1 christos * Redistribution and use in source and binary forms, with or without
11 1.1 christos * modification, are permitted provided that the following conditions
12 1.1 christos * are met:
13 1.1 christos * 1. Redistributions of source code must retain the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer.
15 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 christos * notice, this list of conditions and the following disclaimer in the
17 1.1 christos * documentation and/or other materials provided with the distribution.
18 1.1 christos * 3. All advertising materials mentioning features or use of this software
19 1.1 christos * must display the following acknowledgement:
20 1.1 christos * This product includes software developed by the NetBSD
21 1.1 christos * Foundation, Inc. and its contributors.
22 1.1 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 christos * contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.1 christos */
38 1.1 christos #include <sys/cdefs.h>
39 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
40 1.3 christos __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.3 2004/12/11 06:41:16 christos Exp $");
41 1.1 christos #endif /* LIBC_SCCS and not lint */
42 1.1 christos
43 1.1 christos #include <sys/types.h>
44 1.1 christos #include <sys/socket.h>
45 1.1 christos #include <sys/un.h>
46 1.1 christos
47 1.1 christos #include <netinet/in.h>
48 1.1 christos #include <netatalk/at.h>
49 1.1 christos #include <net/if_dl.h>
50 1.1 christos
51 1.1 christos #include <stdio.h>
52 1.1 christos #include <string.h>
53 1.1 christos #include <errno.h>
54 1.1 christos #include <util.h>
55 1.1 christos #include <netdb.h>
56 1.1 christos
57 1.1 christos int
58 1.1 christos sockaddr_snprintf(char *buf, size_t len, const char *fmt,
59 1.1 christos const struct sockaddr *sa)
60 1.1 christos {
61 1.1 christos const void *a = NULL;
62 1.1 christos char abuf[1024], nbuf[1024], *addr, *w;
63 1.1 christos char *ebuf = &buf[len - 1], *sbuf = buf;
64 1.1 christos const char *ptr, *s;
65 1.1 christos in_port_t p = (in_port_t)~0;
66 1.1 christos const struct sockaddr_at *sat = NULL;
67 1.1 christos const struct sockaddr_in *sin4 = NULL;
68 1.1 christos const struct sockaddr_in6 *sin6 = NULL;
69 1.1 christos const struct sockaddr_un *sun = NULL;
70 1.1 christos const struct sockaddr_dl *sdl = NULL;
71 1.1 christos
72 1.1 christos #define ADDC(c) if (buf < ebuf) *buf++ = c; else buf++
73 1.1 christos #define ADDN() if (buf < ebuf) *buf++ = '\0'; else buf[len - 1] = '\0'
74 1.1 christos #define ADDS(p) for (s = p; *s; s++) ADDC(*s)
75 1.1 christos #define ADDNA() ADDS("N/A")
76 1.1 christos
77 1.1 christos switch (sa->sa_family) {
78 1.1 christos case AF_UNSPEC:
79 1.1 christos goto done;
80 1.1 christos case AF_APPLETALK:
81 1.1 christos sat = ((const struct sockaddr_at *)(const void *)sa);
82 1.1 christos p = ntohs(sat->sat_port);
83 1.1 christos (void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
84 1.1 christos ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
85 1.1 christos break;
86 1.1 christos case AF_LOCAL:
87 1.1 christos sun = ((const struct sockaddr_un *)(const void *)sa);
88 1.1 christos (void)strlcpy(addr = abuf, sun->sun_path, SUN_LEN(sun));
89 1.1 christos break;
90 1.1 christos case AF_INET:
91 1.1 christos sin4 = ((const struct sockaddr_in *)(const void *)sa);
92 1.1 christos p = ntohs(sin4->sin_port);
93 1.1 christos a = &sin4->sin_addr;
94 1.1 christos break;
95 1.1 christos case AF_INET6:
96 1.1 christos sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
97 1.1 christos p = ntohs(sin6->sin6_port);
98 1.1 christos a = &sin6->sin6_addr;
99 1.1 christos break;
100 1.1 christos case AF_LINK:
101 1.1 christos sdl = ((const struct sockaddr_dl *)(const void *)sa);
102 1.1 christos (void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
103 1.1 christos if ((w = strchr(addr, ':')) != 0) {
104 1.1 christos *w++ = '\0';
105 1.1 christos addr = w;
106 1.1 christos
107 1.1 christos }
108 1.1 christos break;
109 1.1 christos default:
110 1.1 christos errno = EAFNOSUPPORT;
111 1.1 christos return -1;
112 1.1 christos }
113 1.1 christos
114 1.3 christos if (a && getnameinfo(sa, (socklen_t)sa->sa_len, addr = abuf,
115 1.3 christos sizeof(abuf), NULL, 0, NI_NUMERICHOST|NI_NUMERICSERV) != 0)
116 1.1 christos return -1;
117 1.1 christos
118 1.1 christos for (ptr = fmt; *ptr; ptr++) {
119 1.1 christos if (*ptr != '%') {
120 1.1 christos ADDC(*ptr);
121 1.1 christos continue;
122 1.1 christos }
123 1.1 christos switch (*++ptr) {
124 1.1 christos case '\0':
125 1.1 christos ADDC('%');
126 1.1 christos goto done;
127 1.1 christos case 'a':
128 1.1 christos ADDS(addr);
129 1.1 christos break;
130 1.1 christos case 'p':
131 1.1 christos if (p != (in_port_t)~0) {
132 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
133 1.1 christos ADDS(nbuf);
134 1.1 christos } else {
135 1.1 christos ADDNA();
136 1.1 christos }
137 1.1 christos break;
138 1.1 christos case 'f':
139 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
140 1.1 christos ADDS(nbuf);
141 1.1 christos break;
142 1.1 christos case 'l':
143 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_len);
144 1.1 christos ADDS(nbuf);
145 1.1 christos break;
146 1.1 christos case 'I':
147 1.1 christos if (sdl && addr != abuf) {
148 1.1 christos ADDS(abuf);
149 1.1 christos } else {
150 1.1 christos ADDNA();
151 1.1 christos }
152 1.1 christos break;
153 1.1 christos case 'F':
154 1.1 christos if (sin6) {
155 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d",
156 1.1 christos sin6->sin6_flowinfo);
157 1.1 christos ADDS(nbuf);
158 1.1 christos break;
159 1.1 christos } else {
160 1.1 christos ADDNA();
161 1.1 christos }
162 1.1 christos break;
163 1.1 christos case 'S':
164 1.1 christos if (sin6) {
165 1.1 christos (void)snprintf(nbuf, sizeof(nbuf), "%d",
166 1.1 christos sin6->sin6_scope_id);
167 1.1 christos ADDS(nbuf);
168 1.1 christos break;
169 1.1 christos } else {
170 1.1 christos ADDNA();
171 1.1 christos }
172 1.1 christos break;
173 1.1 christos case 'R':
174 1.1 christos if (sat) {
175 1.1 christos const struct netrange *n =
176 1.1 christos &sat->sat_range.r_netrange;
177 1.1 christos (void)snprintf(nbuf, sizeof(nbuf),
178 1.1 christos "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
179 1.1 christos n->nr_lastnet);
180 1.1 christos ADDS(nbuf);
181 1.1 christos } else {
182 1.1 christos ADDNA();
183 1.1 christos }
184 1.1 christos break;
185 1.1 christos default:
186 1.1 christos ADDC('%');
187 1.1 christos ADDC(*ptr);
188 1.1 christos break;
189 1.1 christos }
190 1.1 christos }
191 1.1 christos done:
192 1.1 christos ADDN();
193 1.1 christos return buf - sbuf;
194 1.1 christos }
195