1 /* $NetBSD: sockaddr_snprintf.c,v 1.14 2016/12/29 18:30:55 christos Exp $ */
4 * Copyright (c) 2004, 2016 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 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #ifdef HAVE_NET_IF_DL_H
45 #include <net/if_dl.h>
57 # define SALEN(sa) ((sa)->sa ## _len)
59 # define SALEN(sa) ((unsigned)sizeof(*sa))
63 debug_in(char *str, size_t len, const struct sockaddr_in *sin)
65 return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, "
66 "sin_addr.s_addr=%08x",
67 SALEN(sin), sin->sin_family, sin->sin_port,
68 sin->sin_addr.s_addr);
72 debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6)
74 const uint8_t *s = sin6->sin6_addr.s6_addr;
76 return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, "
78 "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
79 "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u",
80 SALEN(sin6), sin6->sin6_family, sin6->sin6_port,
81 sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
82 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd],
83 s[0xe], s[0xf], sin6->sin6_scope_id);
87 debug_un(char *str, size_t len, const struct sockaddr_un *sun)
89 return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s",
90 SALEN(sun), sun->sun_family, (int)sizeof(sun->sun_path),
94 #ifdef HAVE_NET_IF_DL_H
96 debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl)
98 const uint8_t *s = (const void *)sdl->sdl_data;
100 return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, "
101 "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data="
102 "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
103 SALEN(sdl), sdl->sdl_family, sdl->sdl_index,
104 sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen,
105 s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5],
106 s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]);
111 sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
112 const struct sockaddr * const sa)
114 const void *a = NULL;
115 char abuf[1024], nbuf[1024], *addr = NULL;
116 char Abuf[1024], pbuf[32], *name = NULL, *port = NULL;
117 char *ebuf = &sbuf[len - 1], *buf = sbuf;
121 const struct sockaddr_in *sin4 = NULL;
122 const struct sockaddr_in6 *sin6 = NULL;
123 const struct sockaddr_un *sun = NULL;
124 #ifdef HAVE_NET_IF_DL_H
125 const struct sockaddr_dl *sdl = NULL;
130 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
131 while (/*CONSTCOND*/0)
132 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
133 while (/*CONSTCOND*/0)
134 #define ADDNA() do { if (na) ADDS("N/A"); } \
135 while (/*CONSTCOND*/0)
137 switch (sa->sa_family) {
141 salen = sizeof(*sun);
142 sun = ((const struct sockaddr_un *)(const void *)sa);
143 (void)strlcpy(addr = abuf, sun->sun_path, sizeof(abuf));
146 salen = sizeof(*sin4);
147 sin4 = ((const struct sockaddr_in *)(const void *)sa);
148 p = ntohs(sin4->sin_port);
152 salen = sizeof(*sin6);
153 sin6 = ((const struct sockaddr_in6 *)(const void *)sa);
154 p = ntohs(sin6->sin6_port);
155 a = &sin6->sin6_addr;
157 #ifdef HAVE_NET_IF_DL_H
159 sdl = ((const struct sockaddr_dl *)(const void *)sa);
161 if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
162 && sdl->sdl_alen == 0) {
163 salen = sizeof(*sdl);
164 (void)snprintf(abuf, sizeof(abuf), "link#%hu",
167 salen = sdl->sdl_slen + sdl->sdl_nlen + sdl->sdl_alen;
168 if (salen < sizeof(*sdl))
169 salen = sizeof(*sdl);
170 (void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
171 if ((w = strchr(addr, ':')) != NULL) {
179 errno = EAFNOSUPPORT;
186 if (a && getnameinfo(sa, (socklen_t)salen, addr = abuf,
187 (unsigned int)sizeof(abuf), NULL, 0,
188 NI_NUMERICHOST|NI_NUMERICSERV) != 0)
191 for (ptr = fmt; *ptr; ptr++) {
206 (void)snprintf(nbuf, sizeof(nbuf), "%d", p);
212 (void)snprintf(nbuf, sizeof(nbuf), "%d", sa->sa_family);
216 (void)snprintf(nbuf, sizeof(nbuf), "%zu", salen);
225 getnameinfo(sa, (socklen_t)salen, name = Abuf,
226 (unsigned int)sizeof(nbuf), NULL, 0, 0);
236 getnameinfo(sa, (socklen_t)salen, NULL, 0,
238 (unsigned int)sizeof(pbuf), 0);
243 #ifdef HAVE_NET_IF_DL_H
244 if (sdl && addr != abuf) {
254 (void)snprintf(nbuf, sizeof(nbuf), "%d",
255 sin6->sin6_flowinfo);
264 (void)snprintf(nbuf, sizeof(nbuf), "%d",
265 sin6->sin6_scope_id);
278 switch (sa->sa_family) {
280 debug_un(nbuf, sizeof(nbuf), sun);
283 debug_in(nbuf, sizeof(nbuf), sin4);
286 debug_in6(nbuf, sizeof(nbuf), sin6);
288 #ifdef HAVE_NET_IF_DL_H
290 debug_dl(nbuf, sizeof(nbuf), sdl);
315 sbuf[len - 1] = '\0';
316 return (int)(buf - sbuf);