Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Move lla_snprintf from if_arp.c to dl_print.c



details:   https://anonhg.NetBSD.org/src/rev/f022fd687152
branches:  trunk
changeset: 450912:f022fd687152
user:      roy <roy%NetBSD.org@localhost>
date:      Mon Apr 29 16:05:46 2019 +0000

description:
Move lla_snprintf from if_arp.c to dl_print.c

diffstat:

 sys/net/dl_print.c   |  64 ++++++++++++++++++++++++---------------------------
 sys/net/if_dl.h      |   4 ++-
 sys/netinet/if_arp.c |  57 +++++++++++----------------------------------
 3 files changed, 47 insertions(+), 78 deletions(-)

diffs (227 lines):

diff -r dcf636a67cff -r f022fd687152 sys/net/dl_print.c
--- a/sys/net/dl_print.c        Mon Apr 29 12:53:15 2019 +0000
+++ b/sys/net/dl_print.c        Mon Apr 29 16:05:46 2019 +0000
@@ -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;
-
-       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)
+       char abuf[256 * 3];
 
-#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);
 }
diff -r dcf636a67cff -r f022fd687152 sys/net/if_dl.h
--- a/sys/net/if_dl.h   Mon Apr 29 12:53:15 2019 +0000
+++ b/sys/net/if_dl.h   Mon Apr 29 16:05:46 2019 +0000
@@ -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 @@
 #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 *);
diff -r dcf636a67cff -r f022fd687152 sys/netinet/if_arp.c
--- a/sys/netinet/if_arp.c      Mon Apr 29 12:53:15 2019 +0000
+++ b/sys/netinet/if_arp.c      Mon Apr 29 16:05:46 2019 +0000
@@ -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_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 @@
        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_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 @@
                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 @@
                        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 @@
                        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);
                }
        }
 



Home | Main Index | Thread Index | Old Index