xref: /netbsd-src/sys/net/dl_print.c (revision e99a3658f800eac40263b1b68cd0a303f565cb02)
1 /*	$NetBSD: dl_print.c,v 1.8 2021/05/27 13:40:38 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.8 2021/05/27 13:40:38 christos Exp $");
33 #include <sys/systm.h>
34 #else
35 __RCSID("$NetBSD: dl_print.c,v 1.8 2021/05/27 13:40:38 christos Exp $");
36 #include <stdio.h>
37 static const char hexdigits[] = "0123456789abcdef";
38 #endif
39 #include <net/if_dl.h>
40 
41 static int
lla_snprintf1(char * dst,size_t dst_len,const void * src,size_t src_len)42 lla_snprintf1(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 	dp = dst;
48 	sp = (const uint8_t *)src;
49 	ep = sp + src_len;
50 	while (sp < ep) {
51 		if (dst_len-- == 0)
52 			break;
53 		*dp++ = hexdigits[(*sp) >> 4];
54 		if (dst_len-- == 0)
55 			break;
56 		*dp++ = hexdigits[(*sp++) & 0xf];
57 		if (dst_len-- == 0)
58 			break;
59 		*dp++ = ':';
60 	}
61 	if (dp != dst)
62 		*--dp = '\0';
63 
64 	return src_len ? (int)(src_len * 3) - 1 : 0;
65 }
66 
67 char *
lla_snprintf(char * dst,size_t dst_len,const void * src,size_t src_len)68 lla_snprintf(char *dst, size_t dst_len, const void *src, size_t src_len)
69 {
70 	(void)lla_snprintf1(dst, dst_len, src, src_len);
71 	return dst;
72 }
73 
74 #define clip(a, b) ((a) > (size_t)(b) ? (a) - (size_t)(b) : 0)
75 
76 int
dl_print(char * buf,size_t len,const struct dl_addr * dl)77 dl_print(char *buf, size_t len, const struct dl_addr *dl)
78 {
79 	int l = snprintf(buf, len, "%.*s/%hhu#",
80 	    (int)dl->dl_nlen, dl->dl_data, dl->dl_type);
81 	if (l == -1)
82 		return l;
83 	int ll = lla_snprintf1(buf + l, clip(len, l),
84 	    dl->dl_data + dl->dl_nlen, dl->dl_alen);
85 	if (ll == -1)
86 		return ll;
87 
88 	return ll + l;
89 }
90 
91 int
sdl_print(char * buf,size_t len,const void * v)92 sdl_print(char *buf, size_t len, const void *v)
93 {
94 	const struct sockaddr_dl *sdl = v;
95 
96 	if (sdl->sdl_slen == 0 && sdl->sdl_nlen == 0 && sdl->sdl_alen == 0)
97 		return snprintf(buf, len, "link#%hu", sdl->sdl_index);
98 
99 	if (len > 0) {
100 		buf[0] = '[';
101 		len--;
102 	}
103 	int l = dl_print(buf + 1, len, &sdl->sdl_addr);
104 	if (l == -1)
105 		return l;
106 	l++;
107 	len++;
108 	int ll = snprintf(buf + l, clip(len, l), "]:%hu", sdl->sdl_index);
109 	if (ll == -1)
110 		return ll;
111 	return ll + l;
112 }
113