Source-Changes-HG archive

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

[src/trunk]: src/sys/net - split struct dladdr out of struct sockaddr_dl



details:   https://anonhg.NetBSD.org/src/rev/ca195a0867fa
branches:  trunk
changeset: 334698:ca195a0867fa
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Dec 02 19:32:09 2014 +0000

description:
- split struct dladdr out of struct sockaddr_dl
- add routines to print struct sockaddr_dl and struct dladdr
- make if_dl.h idempotent

diffstat:

 sys/net/dl_print.c |  88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/net/files.net  |   3 +-
 sys/net/if_dl.h    |  42 +++++++++++++++++++------
 3 files changed, 122 insertions(+), 11 deletions(-)

diffs (177 lines):

diff -r 92f32eec332a -r ca195a0867fa sys/net/dl_print.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/net/dl_print.c        Tue Dec 02 19:32:09 2014 +0000
@@ -0,0 +1,88 @@
+/*     $NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $    */
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#ifdef _KERNEL
+__KERNEL_RCSID(0, "$NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $");
+#include <sys/systm.h>
+#else
+__RCSID("$NetBSD: dl_print.c,v 1.1 2014/12/02 19:32:09 christos Exp $");
+#include <stdio.h>
+static const uint8_t hexdigits[] = "0123456789abcdef";
+#endif
+#include <net/if_dl.h>
+
+int
+dl_print(char *buf, size_t len, const struct dladdr *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)
+
+#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';
+       }
+       return snprintf(buf, len, "%.*s/%hhu#%s",
+           (int)dl->dl_nlen, dl->dl_data, dl->dl_type, abuf);
+}
+
+int
+sdl_print(char *buf, size_t len, const void *v)
+{
+       const struct sockaddr_dl *sdl = v;
+       char abuf[LINK_ADDRSTRLEN];
+
+       dl_print(abuf, sizeof(abuf), &sdl->sdl_addr);
+       return snprintf(buf, len, "[%s]:%hu", abuf, sdl->sdl_index);
+}
diff -r 92f32eec332a -r ca195a0867fa sys/net/files.net
--- a/sys/net/files.net Tue Dec 02 14:34:19 2014 +0000
+++ b/sys/net/files.net Tue Dec 02 19:32:09 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.net,v 1.1 2014/10/12 03:56:18 uebayasi Exp $
+#      $NetBSD: files.net,v 1.2 2014/12/02 19:32:09 christos Exp $
 
 # XXX CLEANUP
 define net
@@ -6,6 +6,7 @@
 file   net/bpf_filter.c                bpf_filter
 file   net/bpf_stub.c                  net
 file   net/bsd-comp.c                  ppp & ppp_bsdcomp
+file   net/dl_print.c
 file   net/if.c                        net
 file   net/if_arcsubr.c                arcnet                  needs-flag
 file   net/if_atmsubr.c                atm
diff -r 92f32eec332a -r ca195a0867fa sys/net/if_dl.h
--- a/sys/net/if_dl.h   Tue Dec 02 14:34:19 2014 +0000
+++ b/sys/net/if_dl.h   Tue Dec 02 19:32:09 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_dl.h,v 1.23 2008/02/20 17:18:11 matt Exp $  */
+/*     $NetBSD: if_dl.h,v 1.24 2014/12/02 19:32:09 christos Exp $      */
 
 /*
  * Copyright (c) 1990, 1993
@@ -58,6 +58,22 @@
 typedef __sa_family_t  sa_family_t;
 #define sa_family_t    __sa_family_t
 #endif
+#ifndef socklen_t
+typedef __socklen_t   socklen_t;
+#define socklen_t     __socklen_t
+#endif
+
+struct dladdr {
+       uint8_t     dl_type;    /* interface type */
+       uint8_t     dl_nlen;    /* interface name length, no trailing 0 reqd. */
+       uint8_t     dl_alen;    /* link level address length */
+       uint8_t     dl_slen;    /* link layer selector length */
+       /*
+        * minimum work area, can be larger; contains both if name
+        * and ll address
+        */
+       char        dl_data[12];
+};
 
 /*
  * Structure of a Link-Level sockaddr:
@@ -65,15 +81,13 @@
 struct sockaddr_dl {
        uint8_t     sdl_len;    /* Total length of sockaddr */
        sa_family_t sdl_family; /* AF_LINK */
-       uint16_t   sdl_index;   /* if != 0, system given index for interface */
-       uint8_t     sdl_type;   /* interface type */
-       uint8_t     sdl_nlen;   /* interface name length, no trailing 0 reqd. */
-       uint8_t     sdl_alen;   /* link level address length */
-       uint8_t     sdl_slen;   /* link layer selector length */
-       /* minimum work area, can be larger; contains both if name
-        * and ll address
-        */
-       char        sdl_data[12];
+       uint16_t    sdl_index;  /* if != 0, system given index for interface */
+       struct dladdr sdl_addr;
+#define sdl_type       sdl_addr.dl_type
+#define sdl_nlen       sdl_addr.dl_nlen
+#define sdl_alen       sdl_addr.dl_alen
+#define sdl_slen       sdl_addr.dl_slen
+#define sdl_data       sdl_addr.dl_data
 };
 
 #define        satosdl(__sa)   ((struct sockaddr_dl *)(__sa))
@@ -102,4 +116,12 @@
 
 #endif /* !_KERNEL */
 
+#if defined(_KERNEL) || defined(_TEST)
+// 255 xx: + 255 'a' + / + # + 3 digits + NUL
+#define LINK_ADDRSTRLEN        ((255 * 4) + 5)
+
+int    dl_print(char *, size_t, const struct dladdr *);
+int    sdl_print(char *, size_t, const void *);
+#endif
+
 #endif /* !_NET_IF_DL_H_ */



Home | Main Index | Thread Index | Old Index