dl_print.c revision 1.5 1 /* $NetBSD: dl_print.c,v 1.5 2019/04/29 19:08:11 christos 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.5 2019/04/29 19:08:11 christos Exp $");
33 #include <sys/systm.h>
34 #else
35 __RCSID("$NetBSD: dl_print.c,v 1.5 2019/04/29 19:08:11 christos 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 return NULL;
49
50 dp = dst;
51 sp = (const uint8_t *)src;
52 ep = sp + src_len;
53 while (sp < ep) {
54 if (dst_len < 3)
55 break;
56 dst_len -= 3;
57 *dp++ = hexdigits[(*sp) >> 4];
58 *dp++ = hexdigits[(*sp++) & 0xf];
59 *dp++ = ':';
60 }
61 *--dp = 0;
62
63 return dst;
64 }
65
66 int
67 dl_print(char *buf, size_t len, const struct dl_addr *dl)
68 {
69 char abuf[256 * 3];
70
71 lla_snprintf(abuf, sizeof(abuf), dl->dl_data, dl->dl_alen);
72 return snprintf(buf, len, "%.*s/%hhu#%s",
73 (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf);
74 }
75
76 int
77 sdl_print(char *buf, size_t len, const void *v)
78 {
79 const struct sockaddr_dl *sdl = v;
80 char abuf[LINK_ADDRSTRLEN];
81
82 if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
83 return snprintf(buf, len, "link#%hu", sdl->sdl_index);
84
85 dl_print(abuf, sizeof(abuf), &sdl->sdl_addr);
86 return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index);
87 }
88