Module Name: src Committed By: christos Date: Tue Dec 2 19:32:09 UTC 2014
Modified Files: src/sys/net: files.net if_dl.h Added Files: src/sys/net: dl_print.c Log Message: - split struct dladdr out of struct sockaddr_dl - add routines to print struct sockaddr_dl and struct dladdr - make if_dl.h idempotent To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/sys/net/dl_print.c cvs rdiff -u -r1.1 -r1.2 src/sys/net/files.net cvs rdiff -u -r1.23 -r1.24 src/sys/net/if_dl.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/net/files.net diff -u src/sys/net/files.net:1.1 src/sys/net/files.net:1.2 --- src/sys/net/files.net:1.1 Sat Oct 11 23:56:18 2014 +++ src/sys/net/files.net Tue Dec 2 14:32:09 2014 @@ -1,4 +1,4 @@ -# $NetBSD: files.net,v 1.1 2014/10/12 03:56:18 uebayasi Exp $ +# $NetBSD: files.net,v 1.2 2014/12/02 19:32:09 christos Exp $ # XXX CLEANUP define net @@ -6,6 +6,7 @@ file net/bpf.c bpfilter file net/bpf_filter.c bpf_filter file net/bpf_stub.c net file net/bsd-comp.c ppp & ppp_bsdcomp +file net/dl_print.c file net/if.c net file net/if_arcsubr.c arcnet needs-flag file net/if_atmsubr.c atm Index: src/sys/net/if_dl.h diff -u src/sys/net/if_dl.h:1.23 src/sys/net/if_dl.h:1.24 --- src/sys/net/if_dl.h:1.23 Wed Feb 20 12:18:11 2008 +++ src/sys/net/if_dl.h Tue Dec 2 14:32:09 2014 @@ -1,4 +1,4 @@ -/* $NetBSD: if_dl.h,v 1.23 2008/02/20 17:18:11 matt Exp $ */ +/* $NetBSD: if_dl.h,v 1.24 2014/12/02 19:32:09 christos Exp $ */ /* * Copyright (c) 1990, 1993 @@ -58,6 +58,22 @@ typedef __sa_family_t sa_family_t; #define sa_family_t __sa_family_t #endif +#ifndef socklen_t +typedef __socklen_t socklen_t; +#define socklen_t __socklen_t +#endif + +struct dladdr { + uint8_t dl_type; /* interface type */ + uint8_t dl_nlen; /* interface name length, no trailing 0 reqd. */ + uint8_t dl_alen; /* link level address length */ + uint8_t dl_slen; /* link layer selector length */ + /* + * minimum work area, can be larger; contains both if name + * and ll address + */ + char dl_data[12]; +}; /* * Structure of a Link-Level sockaddr: @@ -65,15 +81,13 @@ typedef __sa_family_t sa_family_t; struct sockaddr_dl { uint8_t sdl_len; /* Total length of sockaddr */ sa_family_t sdl_family; /* AF_LINK */ - uint16_t sdl_index; /* if != 0, system given index for interface */ - uint8_t sdl_type; /* interface type */ - uint8_t sdl_nlen; /* interface name length, no trailing 0 reqd. */ - uint8_t sdl_alen; /* link level address length */ - uint8_t sdl_slen; /* link layer selector length */ - /* minimum work area, can be larger; contains both if name - * and ll address - */ - char sdl_data[12]; + uint16_t sdl_index; /* if != 0, system given index for interface */ + struct dladdr sdl_addr; +#define sdl_type sdl_addr.dl_type +#define sdl_nlen sdl_addr.dl_nlen +#define sdl_alen sdl_addr.dl_alen +#define sdl_slen sdl_addr.dl_slen +#define sdl_data sdl_addr.dl_data }; #define satosdl(__sa) ((struct sockaddr_dl *)(__sa)) @@ -102,4 +116,12 @@ __END_DECLS #endif /* !_KERNEL */ +#if defined(_KERNEL) || defined(_TEST) +// 255 xx: + 255 'a' + / + # + 3 digits + NUL +#define LINK_ADDRSTRLEN ((255 * 4) + 5) + +int dl_print(char *, size_t, const struct dladdr *); +int sdl_print(char *, size_t, const void *); +#endif + #endif /* !_NET_IF_DL_H_ */ Added files: Index: src/sys/net/dl_print.c diff -u /dev/null src/sys/net/dl_print.c:1.1 --- /dev/null Tue Dec 2 14:32:09 2014 +++ src/sys/net/dl_print.c Tue Dec 2 14:32:09 2014 @@ -0,0 +1,88 @@ +/* $NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $ */ + +/*- + * Copyright (c) 2014 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include <sys/cdefs.h> +#include <sys/types.h> + +#ifdef _KERNEL +__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $"); +#include <sys/systm.h> +#else +__RCSID("$NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $"); +#include <stdio.h> +static const uint8_t hexdigits[] = "0123456789abcdef"; +#endif +#include <net/if_dl.h> + +int +dl_print(char *buf, size_t len, const struct dladdr *dl) +{ + const uint8_t *ap = (const uint8_t *)dl->dl_data; + char abuf[256 * 3], *cp, *ecp; + + ap += dl->dl_nlen; + cp = abuf; + ecp = abuf + sizeof(abuf); + +#define ADDC(c) do { \ + if (cp >= ecp) {\ + cp++; \ + } else \ + *cp++ = (char)(c); \ + } while (/*CONSTCOND*/0) + +#define ADDX(v) do { \ + uint8_t n = hexdigits[(v)]; \ + ADDC(n); \ + } while (/*CONSTCOND*/0) + + for (size_t i = 0; i < dl->dl_alen; i++) { + ADDX((u_int)ap[i] >> 4); + ADDX(ap[i] & 0xf); + ADDC(':'); + } + if (cp > abuf) + --cp; + if (ecp > abuf) { + if (cp < ecp) + *cp = '\0'; + else + *--ecp = '\0'; + } + return snprintf(buf, len, "%.*s/%hhu#%s", + (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf); +} + +int +sdl_print(char *buf, size_t len, const void *v) +{ + const struct sockaddr_dl *sdl = v; + char abuf[LINK_ADDRSTRLEN]; + + dl_print(abuf, sizeof(abuf), &sdl->sdl_addr); + return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index); +}