Home | History | Annotate | Line # | Download | only in net
dl_print.c revision 1.3.16.2
      1 /*	$NetBSD: dl_print.c,v 1.3.16.2 2017/12/03 11:39:02 jdolecek Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 #include <sys/cdefs.h>
     29 #include <sys/types.h>
     30 
     31 #ifdef _KERNEL
     32 __KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.3.16.2 2017/12/03 11:39:02 jdolecek Exp $");
     33 #include <sys/systm.h>
     34 #else
     35 __RCSID("$NetBSD: dl_print.c,v 1.3.16.2 2017/12/03 11:39:02 jdolecek Exp $");
     36 #include <stdio.h>
     37 static const uint8_t hexdigits[] = "0123456789abcdef";
     38 #endif
     39 #include <net/if_dl.h>
     40 
     41 int
     42 dl_print(char *buf, size_t len, const struct dl_addr *dl)
     43 {
     44 	const uint8_t *ap = (const uint8_t *)dl->dl_data;
     45 	char abuf[256 * 3], *cp, *ecp;
     46 
     47 	ap += dl->dl_nlen;
     48 	cp = abuf;
     49 	ecp = abuf + sizeof(abuf);
     50 
     51 #define ADDC(c) do { \
     52 		if (cp >= ecp) {\
     53 			cp++; \
     54 		} else \
     55 			*cp++ = (char)(c); \
     56 	} while (/*CONSTCOND*/0)
     57 
     58 #define ADDX(v) do { \
     59 		uint8_t n = hexdigits[(v)]; \
     60 		ADDC(n); \
     61 	} while (/*CONSTCOND*/0)
     62 
     63 	for (size_t i = 0; i < dl->dl_alen; i++) {
     64 		ADDX((u_int)ap[i] >> 4);
     65 		ADDX(ap[i] & 0xf);
     66 		ADDC(':');
     67 	}
     68 	if (cp > abuf)
     69 		--cp;
     70 	if (ecp > abuf) {
     71 		if (cp < ecp)
     72 			*cp = '\0';
     73 		else
     74 			*--ecp = '\0';
     75 	}
     76 	return snprintf(buf, len, "%.*s/%hhu#%s",
     77 	    (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf);
     78 }
     79 
     80 int
     81 sdl_print(char *buf, size_t len, const void *v)
     82 {
     83 	const struct sockaddr_dl *sdl = v;
     84 	char abuf[LINK_ADDRSTRLEN];
     85 
     86 	if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
     87 		return snprintf(buf, len, "link#%hu", sdl->sdl_index);
     88 
     89 	dl_print(abuf, sizeof(abuf), &sdl->sdl_addr);
     90 	return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index);
     91 }
     92