1 /* $NetBSD: dl_print.c,v 1.6 2019/04/30 20:56:32 kre 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.6 2019/04/30 20:56:32 kre Exp $"); 33 #include <sys/systm.h> 34 #else 35 __RCSID("$NetBSD: dl_print.c,v 1.6 2019/04/30 20:56:32 kre Exp $"); 36 #include <stdio.h> 37 static const char hexdigits[] = "0123456789abcdef"; 38 #endif 39 #include <net/if_dl.h> 40 41 char * 42 lla_snprintf(char *dst, size_t dst_len, const void *src, size_t src_len) 43 { 44 char *dp; 45 const uint8_t *sp, *ep; 46 47 if (src_len == 0 || dst_len < 3) { 48 if (dst_len != 0) 49 dst[0] = '\0'; 50 return NULL; 51 } 52 53 dp = dst; 54 sp = (const uint8_t *)src; 55 ep = sp + src_len; 56 while (sp < ep) { 57 if (dst_len < 3) 58 break; 59 dst_len -= 3; 60 *dp++ = hexdigits[(*sp) >> 4]; 61 *dp++ = hexdigits[(*sp++) & 0xf]; 62 *dp++ = ':'; 63 } 64 *--dp = 0; 65 66 return dst; 67 } 68 69 int 70 dl_print(char *buf, size_t len, const struct dl_addr *dl) 71 { 72 char abuf[256 * 3]; 73 74 lla_snprintf(abuf, sizeof(abuf), dl->dl_data+dl->dl_nlen, dl->dl_alen); 75 return snprintf(buf, len, "%.*s/%hhu#%s", 76 (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf); 77 } 78 79 int 80 sdl_print(char *buf, size_t len, const void *v) 81 { 82 const struct sockaddr_dl *sdl = v; 83 char abuf[LINK_ADDRSTRLEN]; 84 85 if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0) 86 return snprintf(buf, len, "link#%hu", sdl->sdl_index); 87 88 dl_print(abuf, sizeof(abuf), &sdl->sdl_addr); 89 return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index); 90 } 91