Source-Changes-HG archive

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

[src/trunk]: src/sys put the code that returns the sizeof the socket by famil...



details:   https://anonhg.NetBSD.org/src/rev/31444eb8b4f4
branches:  trunk
changeset: 825150:31444eb8b4f4
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Jul 01 16:59:12 2017 +0000

description:
put the code that returns the sizeof the socket by family in one place.

diffstat:

 sys/kern/uipc_domain.c |  66 ++++++++++++++++++++++++-------------------------
 sys/net/rtsock.c       |  28 ++++----------------
 sys/sys/socket.h       |   5 ++-
 3 files changed, 41 insertions(+), 58 deletions(-)

diffs (181 lines):

diff -r d02edabdf2b5 -r 31444eb8b4f4 sys/kern/uipc_domain.c
--- a/sys/kern/uipc_domain.c    Sat Jul 01 16:34:17 2017 +0000
+++ b/sys/kern/uipc_domain.c    Sat Jul 01 16:59:12 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: uipc_domain.c,v 1.96 2014/12/02 19:45:58 christos Exp $        */
+/*     $NetBSD: uipc_domain.c,v 1.97 2017/07/01 16:59:12 christos Exp $        */
 
 /*
  * Copyright (c) 1982, 1986, 1993
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.96 2014/12/02 19:45:58 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.97 2017/07/01 16:59:12 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/socket.h>
@@ -228,7 +228,7 @@
 }
 
 const struct sockaddr *
-sockaddr_any_by_family(int family)
+sockaddr_any_by_family(sa_family_t family)
 {
        const struct domain *dom;
 
@@ -255,42 +255,40 @@
        return sockaddr_const_addr(any, slenp);
 }
 
+socklen_t
+sockaddr_getsize_by_family(sa_family_t af)
+{
+       switch (af) {
+       case AF_INET:
+               return sizeof(struct sockaddr_in);
+       case AF_INET6:
+               return sizeof(struct sockaddr_in6);
+       case AF_UNIX:
+               return sizeof(struct sockaddr_un);
+       case AF_LINK:
+               return sizeof(struct sockaddr_dl);
+       case AF_APPLETALK:
+               return sizeof(struct sockaddr_at);
+       default:
+#ifdef DIAGNOSTIC
+               printf("%s: Unhandled address family=%hhu\n", __func__, af);
+#endif
+               return 0;
+       }
+}
+
 #ifdef DIAGNOSTIC
 static void
 sockaddr_checklen(const struct sockaddr *sa)
 {
-       socklen_t len = 0;
-       switch (sa->sa_family) {
-       case AF_INET:
-               len = sizeof(struct sockaddr_in);
-               break;
-       case AF_INET6:
-               len = sizeof(struct sockaddr_in6);
-               break;
-       case AF_UNIX:
-               len = sizeof(struct sockaddr_un);
-               break;
-       case AF_LINK:
-               len = sizeof(struct sockaddr_dl);
-               // As long as it is not 0...
-               if (sa->sa_len != 0)
-                       return;
-               break;
-       case AF_APPLETALK:
-               len = sizeof(struct sockaddr_at);
-               break;
-       default:
-               printf("%s: Unhandled af=%hhu socklen=%hhu\n", __func__,
-                   sa->sa_family, sa->sa_len);
+       socklen_t len = sockaddr_getsize_by_family(sa->sa_family);
+       if (len == 0 || len == sa->sa_len)
                return;
-       }
-       if (len != sa->sa_len) {
-               char buf[512];
-               sockaddr_format(sa, buf, sizeof(buf));
-               printf("%s: %p bad len af=%hhu socklen=%hhu len=%u [%s]\n",
-                   __func__, sa, sa->sa_family, sa->sa_len,
-                   (unsigned)len, buf);
-       }
+
+       char buf[512];
+       sockaddr_format(sa, buf, sizeof(buf));
+       printf("%s: %p bad len af=%hhu socklen=%hhu len=%u [%s]\n",
+           __func__, sa, sa->sa_family, sa->sa_len, (unsigned)len, buf);
 }
 #else
 #define sockaddr_checklen(sa) ((void)0)
diff -r d02edabdf2b5 -r 31444eb8b4f4 sys/net/rtsock.c
--- a/sys/net/rtsock.c  Sat Jul 01 16:34:17 2017 +0000
+++ b/sys/net/rtsock.c  Sat Jul 01 16:59:12 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: rtsock.c,v 1.226 2017/06/30 18:28:31 christos Exp $    */
+/*     $NetBSD: rtsock.c,v 1.227 2017/07/01 16:59:12 christos Exp $    */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -61,7 +61,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.226 2017/06/30 18:28:31 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rtsock.c,v 1.227 2017/07/01 16:59:12 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_inet.h"
@@ -721,24 +721,6 @@
        return error;
 }
 
-static socklen_t
-sa_addrlen(const struct sockaddr *sa)
-{
-
-       switch (sa->sa_family) {
-#ifdef INET
-       case AF_INET:
-               return sizeof(struct sockaddr_in);
-#endif
-#ifdef INET6
-       case AF_INET6:
-               return sizeof(struct sockaddr_in6);
-#endif
-       default:
-               return 0;
-       }
-}
-
 /*ARGSUSED*/
 int
 COMPATNAME(route_output)(struct mbuf *m, struct socket *so)
@@ -819,8 +801,10 @@
         * (padded with 0's). We keep the original length of the sockaddr.
         */
        if (info.rti_info[RTAX_NETMASK]) {
-               socklen_t sa_len = sa_addrlen(info.rti_info[RTAX_NETMASK]);
-               socklen_t masklen = info.rti_info[RTAX_NETMASK]->sa_len;
+               socklen_t sa_len = sockaddr_getsize_by_family(
+                   info.rti_info[RTAX_NETMASK]->sa_family);
+               socklen_t masklen = sockaddr_getlen(
+                   info.rti_info[RTAX_NETMASK]);
                if (sa_len != 0 && sa_len > masklen) {
                        KASSERT(sa_len <= sizeof(netmask));
                        memcpy(&netmask, info.rti_info[RTAX_NETMASK], masklen);
diff -r d02edabdf2b5 -r 31444eb8b4f4 sys/sys/socket.h
--- a/sys/sys/socket.h  Sat Jul 01 16:34:17 2017 +0000
+++ b/sys/sys/socket.h  Sat Jul 01 16:59:12 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: socket.h,v 1.122 2017/05/27 21:02:56 bouyer Exp $      */
+/*     $NetBSD: socket.h,v 1.123 2017/07/01 16:59:12 christos Exp $    */
 
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -625,6 +625,7 @@
 }
 
 __BEGIN_DECLS
+socklen_t sockaddr_getsize_by_family(sa_family_t);
 struct sockaddr *sockaddr_copy(struct sockaddr *, socklen_t,
     const struct sockaddr *);
 struct sockaddr *sockaddr_externalize(struct sockaddr *, socklen_t,
@@ -633,7 +634,7 @@
 const void *sockaddr_const_addr(const struct sockaddr *, socklen_t *);
 void *sockaddr_addr(struct sockaddr *, socklen_t *);
 const struct sockaddr *sockaddr_any(const struct sockaddr *);
-const struct sockaddr *sockaddr_any_by_family(int);
+const struct sockaddr *sockaddr_any_by_family(sa_family_t);
 const void *sockaddr_anyaddr(const struct sockaddr *, socklen_t *);
 int sockaddr_cmp(const struct sockaddr *, const struct sockaddr *);
 struct sockaddr *sockaddr_dup(const struct sockaddr *, int);



Home | Main Index | Thread Index | Old Index