diff options
| author | christos <christos@NetBSD.org> | 2013-06-07 17:23:26 +0000 |
|---|---|---|
| committer | christos <christos@NetBSD.org> | 2013-06-07 17:23:26 +0000 |
| commit | 05669c6ea4d947eabd2987044cae5c8256d7e613 (patch) | |
| tree | 9c76fff2954ff7df6b917944a38299f70c9b1a8a /lib/libutil | |
| parent | 8f2efeec3dabe4c28e8647859131b636a3f9d160 (diff) | |
Add a debugging format that prints all the fields with names.
Diffstat (limited to 'lib/libutil')
| -rw-r--r-- | lib/libutil/sockaddr_snprintf.3 | 8 | ||||
| -rw-r--r-- | lib/libutil/sockaddr_snprintf.c | 88 |
2 files changed, 92 insertions, 4 deletions
diff --git a/lib/libutil/sockaddr_snprintf.3 b/lib/libutil/sockaddr_snprintf.3 index b28fd0ed304..e26ed47be8e 100644 --- a/lib/libutil/sockaddr_snprintf.3 +++ b/lib/libutil/sockaddr_snprintf.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: sockaddr_snprintf.3,v 1.7 2009/04/11 16:13:49 joerg Exp $ +.\" $NetBSD: sockaddr_snprintf.3,v 1.8 2013/06/07 17:23:26 christos Exp $ .\" .\" Copyright (c) 2004 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -27,7 +27,7 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd April 9, 2005 +.Dd June 7, 2013 .Dt SOCKADDR_SNPRINTF 3 .Os .Sh NAME @@ -109,6 +109,10 @@ this is the hostname associated with the address. For all other address families, it is the same as the .Dq a format. +.It D +Debugging output: +For all addresses, print all their fields as +.Dq field_name=value . .It f The numeric value of the family of the address is printed. .It l diff --git a/lib/libutil/sockaddr_snprintf.c b/lib/libutil/sockaddr_snprintf.c index e0af1bdecab..cd46d4fc662 100644 --- a/lib/libutil/sockaddr_snprintf.c +++ b/lib/libutil/sockaddr_snprintf.c @@ -1,4 +1,4 @@ -/* $NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $ */ +/* $NetBSD: sockaddr_snprintf.c,v 1.10 2013/06/07 17:23:26 christos Exp $ */ /*- * Copyright (c) 2004 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> #if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $"); +__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.10 2013/06/07 17:23:26 christos Exp $"); #endif /* LIBC_SCCS and not lint */ #include <sys/types.h> @@ -44,9 +44,71 @@ __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $"); #include <stdio.h> #include <string.h> #include <errno.h> +#include <stdlib.h> #include <util.h> #include <netdb.h> +static int +debug_at(char *str, size_t len, const struct sockaddr_at *sat) +{ + return snprintf(str, len, "sat_len=%u, sat_family=%u, sat_port=%u, " + "sat_addr.s_net=%u, sat_addr.s_node=%u, " + "sat_range.r_netrange.nr_phase=%u, " + "sat_range.r_netrange.nr_firstnet=%u, " + "sat_range.r_netrange.nr_lastnet=%u", + sat->sat_len, sat->sat_family, sat->sat_port, + sat->sat_addr.s_net, sat->sat_addr.s_node, + sat->sat_range.r_netrange.nr_phase, + sat->sat_range.r_netrange.nr_firstnet, + sat->sat_range.r_netrange.nr_lastnet); +} + +static int +debug_in(char *str, size_t len, const struct sockaddr_in *sin) +{ + return snprintf(str, len, "sin_len=%u, sin_family=%u, sin_port=%u, " + "sin_addr.s_addr=%08x", + sin->sin_len, sin->sin_family, sin->sin_port, + sin->sin_addr.s_addr); +} + +static int +debug_in6(char *str, size_t len, const struct sockaddr_in6 *sin6) +{ + const uint8_t *s = sin6->sin6_addr.s6_addr; + + return snprintf(str, len, "sin6_len=%u, sin6_family=%u, sin6_port=%u, " + "sin6_flowinfo=%u, " + "sin6_addr=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:" + "%02x:%02x:%02x:%02x:%02x:%02x, sin6_scope_id=%u", + sin6->sin6_len, sin6->sin6_family, sin6->sin6_port, + sin6->sin6_flowinfo, s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5], + s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb], s[0xc], s[0xd], + s[0xe], s[0xf], sin6->sin6_scope_id); +} + +static int +debug_un(char *str, size_t len, const struct sockaddr_un *sun) +{ + return snprintf(str, len, "sun_len=%u, sun_family=%u, sun_path=%*s", + sun->sun_len, sun->sun_family, (int)sizeof(sun->sun_path), + sun->sun_path); +} + +static int +debug_dl(char *str, size_t len, const struct sockaddr_dl *sdl) +{ + const uint8_t *s = (const void *)sdl->sdl_data; + + return snprintf(str, len, "sdl_len=%u, sdl_family=%u, sdl_index=%u, " + "sdl_type=%u, sdl_nlen=%u, sdl_alen=%u, sdl_slen=%u, sdl_data=" + "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", + sdl->sdl_len, sdl->sdl_family, sdl->sdl_index, + sdl->sdl_type, sdl->sdl_nlen, sdl->sdl_alen, sdl->sdl_slen, + s[0x0], s[0x1], s[0x2], s[0x3], s[0x4], s[0x5], + s[0x6], s[0x7], s[0x8], s[0x9], s[0xa], s[0xb]); +} + int sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, const struct sockaddr * const sa) @@ -207,6 +269,28 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt, ADDNA(); } break; + case 'D': + switch (sa->sa_family) { + case AF_APPLETALK: + debug_at(nbuf, sizeof(nbuf), sat); + break; + case AF_LOCAL: + debug_un(nbuf, sizeof(nbuf), sun); + break; + case AF_INET: + debug_in(nbuf, sizeof(nbuf), sin4); + break; + case AF_INET6: + debug_in6(nbuf, sizeof(nbuf), sin6); + break; + case AF_LINK: + debug_dl(nbuf, sizeof(nbuf), sdl); + break; + default: + abort(); + } + ADDS(nbuf); + break; default: ADDC('%'); if (na == 0) |
