Module Name:    src
Committed By:   roy
Date:           Mon Apr 29 16:05:46 UTC 2019

Modified Files:
        src/sys/net: dl_print.c if_dl.h
        src/sys/netinet: if_arp.c

Log Message:
Move lla_snprintf from if_arp.c to dl_print.c


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/net/dl_print.c
cvs rdiff -u -r1.26 -r1.27 src/sys/net/if_dl.h
cvs rdiff -u -r1.280 -r1.281 src/sys/netinet/if_arp.c

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/dl_print.c
diff -u src/sys/net/dl_print.c:1.3 src/sys/net/dl_print.c:1.4
--- src/sys/net/dl_print.c:1.3	Wed Apr  6 18:04:58 2016
+++ src/sys/net/dl_print.c	Mon Apr 29 16:05:46 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $	*/
+/*	$NetBSD: dl_print.c,v 1.4 2019/04/29 16:05:46 roy Exp $	*/
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -29,50 +29,46 @@
 #include <sys/types.h>
 
 #ifdef _KERNEL
-__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.4 2019/04/29 16:05:46 roy Exp $");
 #include <sys/systm.h>
 #else
-__RCSID("$NetBSD: dl_print.c,v 1.3 2016/04/06 18:04:58 christos Exp $");
+__RCSID("$NetBSD: dl_print.c,v 1.4 2019/04/29 16:05:46 roy Exp $");
 #include <stdio.h>
 static const uint8_t hexdigits[] = "0123456789abcdef";
 #endif
 #include <net/if_dl.h>
 
+char *
+lla_snprintf(char *dst, size_t dst_len, const void *src, size_t src_len)
+{
+	char *dp;
+	const uint8_t *sp, *ep;
+
+	if (src_len == 0 || dst_len < 3)
+		return NULL;
+
+	dp = dst;
+	sp = (const uint8_t *)src;
+	ep = sp + src_len;
+	while (sp < ep) {
+		if (dst_len < 3)
+			break;
+		dst_len -= 3;
+		*dp++ = hexdigits[(*sp) >> 4];
+		*dp++ = hexdigits[(*sp++) & 0xf];
+		*dp++ = ':';
+	}
+	*--dp = 0;
+
+	return dst;
+}
+
 int
 dl_print(char *buf, size_t len, const struct dl_addr *dl)
 {
-	const uint8_t *ap = (const uint8_t *)dl->dl_data;
-	char abuf[256 * 3], *cp, *ecp;
+	char abuf[256 * 3];
 
-	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';
-	}
+	lla_snprintf(abuf, sizeof(abuf), dl->dl_data, dl->dl_alen);
 	return snprintf(buf, len, "%.*s/%hhu#%s",
 	    (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf);
 }

Index: src/sys/net/if_dl.h
diff -u src/sys/net/if_dl.h:1.26 src/sys/net/if_dl.h:1.27
--- src/sys/net/if_dl.h:1.26	Wed Dec  3 01:31:37 2014
+++ src/sys/net/if_dl.h	Mon Apr 29 16:05:46 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_dl.h,v 1.26 2014/12/03 01:31:37 christos Exp $	*/
+/*	$NetBSD: if_dl.h,v 1.27 2019/04/29 16:05:46 roy Exp $	*/
 
 /*
  * Copyright (c) 1990, 1993
@@ -119,7 +119,9 @@ __END_DECLS
 #if defined(_KERNEL) || defined(_TEST)
 // 255 xx: + 255 'a' + / + # + 3 digits + NUL
 #define LINK_ADDRSTRLEN	((255 * 4) + 5)
+#define	LLA_ADDRSTRLEN	(16 * 3)
 
+char	*lla_snprintf(char *, size_t, const void *, size_t);
 int	dl_print(char *, size_t, const struct dl_addr *);
 #define DL_PRINT(b, a) (dl_print((b), sizeof(b), (a)), (b))
 int	sdl_print(char *, size_t, const void *);

Index: src/sys/netinet/if_arp.c
diff -u src/sys/netinet/if_arp.c:1.280 src/sys/netinet/if_arp.c:1.281
--- src/sys/netinet/if_arp.c:1.280	Mon Apr 29 11:57:22 2019
+++ src/sys/netinet/if_arp.c	Mon Apr 29 16:05:46 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_arp.c,v 1.280 2019/04/29 11:57:22 roy Exp $	*/
+/*	$NetBSD: if_arp.c,v 1.281 2019/04/29 16:05:46 roy Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.280 2019/04/29 11:57:22 roy Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_arp.c,v 1.281 2019/04/29 16:05:46 roy Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_ddb.h"
@@ -214,36 +214,6 @@ static int log_permanent_modify = 1;
 static int log_wrong_iface = 1;
 static int log_unknown_network = 1;
 
-/*
- * this should be elsewhere.
- */
-
-#define	LLA_ADDRSTRLEN	(16 * 3)
-
-static char *
-lla_snprintf(char *, const u_int8_t *, int);
-
-static char *
-lla_snprintf(char *dst, const u_int8_t *adrp, int len)
-{
-	int i;
-	char *p;
-
-	p = dst;
-
-	*p++ = hexdigits[(*adrp) >> 4];
-	*p++ = hexdigits[(*adrp++) & 0xf];
-
-	for (i = 1; i < len && i < 16; i++) {
-		*p++ = ':';
-		*p++ = hexdigits[(*adrp) >> 4];
-		*p++ = hexdigits[(*adrp++) & 0xf];
-	}
-
-	*p = 0;
-	return dst;
-}
-
 DOMAIN_DEFINE(arpdomain);	/* forward declare and add to link set */
 
 static void
@@ -1035,7 +1005,7 @@ in_arpinput(struct mbuf *m)
 	uint64_t *arps;
 	struct psref psref, psref_ia;
 	int s;
-	char llabuf[LLA_ADDRSTRLEN];
+	char llabuf[LLA_ADDRSTRLEN], *llastr;
 	char ipbuf[INET_ADDRSTRLEN];
 	bool do_dad;
 
@@ -1179,8 +1149,9 @@ in_arpinput(struct mbuf *m)
 	    (in_nullhost(isaddr) && in_hosteq(itaddr, myaddr) &&
 	     m->m_flags & M_BCAST)))
 	{
-		arp_dad_duplicated((struct ifaddr *)ia,
-		    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln));
+		llastr = lla_snprintf(llabuf, sizeof(llabuf),
+		    ar_sha(ah), ah->ar_hln);
+		arp_dad_duplicated((struct ifaddr *)ia, llastr);
 		goto out;
 	}
 
@@ -1203,16 +1174,18 @@ in_arpinput(struct mbuf *m)
 		goto reply;
 
 	if ((la->la_flags & LLE_VALID) &&
-	    memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen)) {
+	    memcmp(ar_sha(ah), &la->ll_addr, ifp->if_addrlen))
+	{
+		llastr = lla_snprintf(llabuf, sizeof(llabuf),
+		    ar_sha(ah), ah->ar_hln);
+
 		if (la->la_flags & LLE_STATIC) {
 			ARP_STATINC(ARP_STAT_RCVOVERPERM);
 			if (!log_permanent_modify)
 				goto out;
 			log(LOG_INFO,
 			    "%s tried to overwrite permanent arp info"
-			    " for %s\n",
-			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
-			    IN_PRINT(ipbuf, &isaddr));
+			    " for %s\n", llastr, IN_PRINT(ipbuf, &isaddr));
 			goto out;
 		} else if (la->lle_tbl->llt_ifp != ifp) {
 			/* XXX should not happen? */
@@ -1222,7 +1195,7 @@ in_arpinput(struct mbuf *m)
 			log(LOG_INFO,
 			    "%s on %s tried to overwrite "
 			    "arp info for %s on %s\n",
-			    lla_snprintf(llabuf, ar_sha(ah), ah->ar_hln),
+			    llastr,
 			    ifp->if_xname, IN_PRINT(ipbuf, &isaddr),
 			    la->lle_tbl->llt_ifp->if_xname);
 				goto out;
@@ -1231,9 +1204,7 @@ in_arpinput(struct mbuf *m)
 			if (log_movements)
 				log(LOG_INFO, "arp info overwritten "
 				    "for %s by %s\n",
-				    IN_PRINT(ipbuf, &isaddr),
-				    lla_snprintf(llabuf, ar_sha(ah),
-				    ah->ar_hln));
+				    IN_PRINT(ipbuf, &isaddr), llastr);
 		}
 	}
 

Reply via email to