Skip to content

Commit

Permalink
pretty-print link addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
zoulasc committed Apr 6, 2016
1 parent 4115088 commit 059fd57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
19 changes: 13 additions & 6 deletions lib/libutil/sockaddr_snprintf.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: sockaddr_snprintf.c,v 1.11 2013/12/31 12:58:02 mlelstv Exp $ */
/* $NetBSD: sockaddr_snprintf.c,v 1.12 2016/04/06 18:08:16 christos Exp $ */

/*-
* Copyright (c) 2004 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.11 2013/12/31 12:58:02 mlelstv Exp $");
__RCSID("$NetBSD: sockaddr_snprintf.c,v 1.12 2016/04/06 18:08:16 christos Exp $");
#endif /* LIBC_SCCS and not lint */

#include <sys/types.h>
Expand Down Expand Up @@ -159,10 +159,17 @@ sockaddr_snprintf(char * const sbuf, const size_t len, const char * const fmt,
break;
case AF_LINK:
sdl = ((const struct sockaddr_dl *)(const void *)sa);
(void)strlcpy(addr = abuf, link_ntoa(sdl), sizeof(abuf));
if ((w = strchr(addr, ':')) != 0) {
*w++ = '\0';
addr = w;
addr = abuf;
if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0
&& sdl->sdl_alen == 0) {
(void)snprintf(abuf, sizeof(abuf), "link#%hu",
sdl->sdl_index);
} else {
(void)strlcpy(abuf, link_ntoa(sdl), sizeof(abuf));
if ((w = strchr(addr, ':')) != 0) {
*w++ = '\0';
addr = w;
}
}
break;
default:
Expand Down
9 changes: 6 additions & 3 deletions sys/net/dl_print.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: dl_print.c,v 1.2 2014/12/02 19:34:33 christos Exp $ */
/* $NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $ */

/*-
* Copyright (c) 2014 The NetBSD Foundation, Inc.
Expand Down Expand Up @@ -29,10 +29,10 @@
#include <sys/types.h>

#ifdef _KERNEL
__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.2 2014/12/02 19:34:33 christos Exp $");
__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $");
#include <sys/systm.h>
#else
__RCSID("$NetBSD: dl_print.c,v 1.2 2014/12/02 19:34:33 christos Exp $");
__RCSID("$NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $");
#include <stdio.h>
static const uint8_t hexdigits[] = "0123456789abcdef";
#endif
Expand Down Expand Up @@ -83,6 +83,9 @@ sdl_print(char *buf, size_t len, const void *v)
const struct sockaddr_dl *sdl = v;
char abuf[LINK_ADDRSTRLEN];

if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
return snprintf(buf, len, "link#%hu", sdl->sdl_index);

dl_print(abuf, sizeof(abuf), &sdl->sdl_addr);
return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index);
}

0 comments on commit 059fd57

Please sign in to comment.