1 /* $NetBSD: sockaddr_snprintf.c,v 1.10 2016/04/05 12:28:57 christos Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
37 __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.10 2016/04/05 12:28:57 christos Exp $");
38 #endif /* LIBC_SCCS and not lint */
40 #include <sys/param.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #undef HAVE_NETATALK_AT_H
49 #ifdef HAVE_NETATALK_AT_H
50 #include <netatalk/at.h>
52 #ifdef HAVE_NET_IF_DL_H
53 #include <net/if_dl.h>
68 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
69 #define SLEN(a) (a)->a ## _len
76 return sizeof(struct sockaddr_in);
78 return sizeof(struct sockaddr_in6);
80 return sizeof(struct sockaddr_un);
81 #ifdef HAVE_NET_IF_DL_H
83 return sizeof(struct sockaddr_dl);
85 #ifdef HAVE_NETATALK_AT_H
87 return sizeof(struct sockaddr_at);
90 return sizeof(struct sockaddr_storage);
94 #define SLEN(a) socklen((a)->a ## _family)
97 #ifdef HAVE_NETATALK_AT_H
99 debug_at(char *str, size_t len, const struct sockaddr_at *sat)
101 return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, "
102 "sat_addr.s_net=%u, sat_addr.s_node=%u, "
103 "sat_range.r_netrange.nr_phase=%u, "
104 "sat_range.r_netrange.nr_firstnet=%u, "
105 "sat_range.r_netrange.nr_lastnet=%u",
106 SLEN(sat), sat->sat_family, sat->sat_port,
107 sat->sat_addr.s_net, sat->sat_addr.s_node,
108 sat->sat_range.r_netrange.nr_phase,
109 sat->sat_range.r_netrange.nr_firstnet,
110 sat->sat_range.r_netrange.nr_lastnet);
115 debug_in(char *str, size_t len, const struct sockaddr_in *sin)
117 return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
118 "sin_addr.s_addr=%08x",
119 SLEN(sin), sin->sin_family, sin->sin_port,
120 sin->sin_addr.s_addr);
124 debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
126 const uint8_t *s = sin6->sin6_addr.s6_addr;
128 return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
130 "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
131 "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
132 SLEN(sin6), sin6->sin6_family, sin6->sin6_port,
133 sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
134 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
135 s[0xe], s[0xf], sin6->sin6_scope_id);
139 debug_un(char *str, size_t len, const struct sockaddr_un *sun)
141 return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
142 SLEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
146 #ifdef HAVE_NET_IF_DL_H
148 debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
150 const uint8_t *s = (const void *)sdl->sdl_data;
152 return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
153 "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
154 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
155 SLEN(sdl), sdl->sdl_family, sdl->sdl_index,
156 sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
157 s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
158 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
163 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
164 const struct sockaddr * const sa)
166 const void *a = NULL;
167 char abuf[1024], nbuf[1024], *addr = NULL;
169 char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
170 char *ebuf = &sbuf[len - 1], *buf = sbuf;
173 #ifdef HAVE_NETATALK_AT_H
174 const struct sockaddr_at *sat = NULL;
176 const struct sockaddr_in *sin4 = NULL;
177 const struct sockaddr_in6 *sin6 = NULL;
178 const struct sockaddr_un *sun = NULL;
179 #ifdef HAVE_NET_IF_DL_H
180 const struct sockaddr_dl *sdl = NULL;
185 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
186 while (/*CONSTCOND*/0)
187 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
188 while (/*CONSTCOND*/0)
189 #define ADDNA() do { if (na) ADDS("N/A"); } \
190 while (/*CONSTCOND*/0)
192 switch (sa->sa_family) {
195 #ifdef HAVE_NETATALK_AT_H
197 sat = ((const struct sockaddr_at *)(const void *)sa);
198 p = ntohs(sat->sat_port);
199 (void)snprintf(addr = abuf, sizeof(abuf), "%u.%u",
200 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
201 (void)snprintf(port = pbuf, sizeof(pbuf), "%d", p);
205 sun = ((const struct sockaddr_un *)(const void *)sa);
206 (void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
209 sin4 = ((const struct sockaddr_in *)(const void *)sa);
210 p = ntohs(sin4->sin_port);
214 sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
215 p = ntohs(sin6->sin6_port);
216 a = &sin6->sin6_addr;
218 #ifdef HAVE_NET_IF_DL_H
220 sdl = ((const struct sockaddr_dl *)(const void *)sa);
221 (void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
222 if ((w = strchr(addr, ':')) != 0) {
229 errno = EAFNOSUPPORT;
236 if (a && getnameinfo(sa, (socklen_t)SLEN(sa), addr = abuf,
237 (unsigned int)sizeof(abuf), NULL, 0,
238 NI_NUMERICHOST|NI_NUMERICSERV) != 0)
241 for (ptr = fmt; *ptr; ptr++) {
256 (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
262 (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
266 (void)snprintf(nbuf, sizeof(nbuf), "%d", SLEN(sa));
275 getnameinfo(sa, (socklen_t)SLEN(sa),
277 (unsigned int)sizeof(nbuf), NULL, 0, 0);
287 getnameinfo(sa, (socklen_t)SLEN(sa), NULL, 0,
289 (unsigned int)sizeof(pbuf), 0);
294 #ifdef HAVE_NET_IF_DL_H
295 if (sdl && addr != abuf) {
305 (void)snprintf(nbuf, sizeof(nbuf), "%d",
306 sin6->sin6_flowinfo);
315 (void)snprintf(nbuf, sizeof(nbuf), "%d",
316 sin6->sin6_scope_id);
324 #ifdef HAVE_NETATALK_AT_H
326 const struct netrange *n =
327 &sat->sat_range.r_netrange;
328 (void)snprintf(nbuf, sizeof(nbuf),
329 "%d:[%d,%d]", n->nr_phase , n->nr_firstnet,
339 switch (sa->sa_family) {
340 #ifdef HAVE_NETATALK_AT_H
342 debug_at(nbuf, sizeof(nbuf), sat);
346 debug_un(nbuf, sizeof(nbuf), sun);
349 debug_in(nbuf, sizeof(nbuf), sin4);
352 debug_in6(nbuf, sizeof(nbuf), sin6);
354 #ifdef HAVE_NET_IF_DL_H
356 debug_dl(nbuf, sizeof(nbuf), sdl);
381 sbuf[len - 1] = '\0';
382 return (int)(buf - sbuf);