Source-Changes-HG archive

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

[src/trunk]: src/sbin/route Factor out the netstat route printing code and us...



details:   https://anonhg.NetBSD.org/src/rev/0f7f4ab55822
branches:  trunk
changeset: 333515:0f7f4ab55822
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Nov 06 21:29:32 2014 +0000

description:
Factor out the netstat route printing code and use it here. There is no
point in having 2 different copies; fixes PR/49371

diffstat:

 sbin/route/Makefile   |    4 +-
 sbin/route/extern.h   |    8 +-
 sbin/route/prog_ops.h |    8 +-
 sbin/route/route.c    |  459 ++--------------------------
 sbin/route/rtutil.c   |  786 ++++++++++++++++++++++++++++++++++++++++++++++++++
 sbin/route/rtutil.h   |   58 +++
 sbin/route/show.c     |  299 +------------------
 7 files changed, 907 insertions(+), 715 deletions(-)

diffs (truncated from 1833 to 300 lines):

diff -r 2a0cb37d8f4e -r 0f7f4ab55822 sbin/route/Makefile
--- a/sbin/route/Makefile       Thu Nov 06 18:58:18 2014 +0000
+++ b/sbin/route/Makefile       Thu Nov 06 21:29:32 2014 +0000
@@ -1,11 +1,11 @@
-#      $NetBSD: Makefile,v 1.25 2010/12/13 17:39:47 pooka Exp $
+#      $NetBSD: Makefile,v 1.26 2014/11/06 21:29:32 christos Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/5/93
 
 .include <bsd.own.mk>
 
 RUMPPRG=route
 MAN=   route.8
-SRCS=  route.c show.c keywords.c
+SRCS=  route.c show.c keywords.c rtutil.c
 
 .if (${USE_INET6} != "no")
 CPPFLAGS+=-DINET6
diff -r 2a0cb37d8f4e -r 0f7f4ab55822 sbin/route/extern.h
--- a/sbin/route/extern.h       Thu Nov 06 18:58:18 2014 +0000
+++ b/sbin/route/extern.h       Thu Nov 06 21:29:32 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.14 2009/10/21 01:07:46 snj Exp $  */
+/*     $NetBSD: extern.h,v 1.15 2014/11/06 21:29:32 christos Exp $     */
 
 /*
  * Copyright (c) 1997 Christos Zoulas.  All rights reserved.
@@ -30,15 +30,11 @@
 
 void parse_show_opts(int, char * const *, int *, int *, const char **, bool);
 /* show.c */
-void show(int, char * const *);
+void show(int, char * const *, int);
 
 /* route.c */
 extern int nflag, Sflag;
 #define NOTDEFSTRING "0.0.0.0/xxx.xxx.xxx.xxx\0"
 int keyword(const char *);
-int netmask_length(struct sockaddr *, int);
-char *netmask_string(const struct sockaddr *, int, int);
-const char *routename(const struct sockaddr *, struct sockaddr *, int);
-const char *netname(const struct sockaddr *, struct sockaddr *);
 const char *ns_print(struct sockaddr_ns *);
 void usage(const char *)__attribute__((__noreturn__));
diff -r 2a0cb37d8f4e -r 0f7f4ab55822 sbin/route/prog_ops.h
--- a/sbin/route/prog_ops.h     Thu Nov 06 18:58:18 2014 +0000
+++ b/sbin/route/prog_ops.h     Thu Nov 06 21:29:32 2014 +0000
@@ -1,4 +1,4 @@
-/*      $NetBSD: prog_ops.h,v 1.2 2010/12/13 19:19:10 pooka Exp $      */
+/*      $NetBSD: prog_ops.h,v 1.3 2014/11/06 21:29:32 christos Exp $   */
 
 /*
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -32,9 +32,13 @@
 #include <sys/types.h>
 
 #ifndef CRUNCHOPS
+/* XXX: Keep same order with netstat! */
 struct prog_ops {
        int (*op_init)(void);
 
+       int (*op_sysctl)(const int *, u_int, void *, size_t *,
+                        const void *, size_t);
+
        int (*op_socket)(int, int, int);
        int (*op_open)(const char *, int, ...);
        pid_t (*op_getpid)(void);
@@ -42,8 +46,6 @@
        ssize_t (*op_read)(int, void *, size_t);
        ssize_t (*op_write)(int, const void *, size_t);
 
-       int (*op_sysctl)(const int *, u_int, void *, size_t *,
-                        const void *, size_t);
 
        int (*op_shutdown)(int, int);
 };
diff -r 2a0cb37d8f4e -r 0f7f4ab55822 sbin/route/route.c
--- a/sbin/route/route.c        Thu Nov 06 18:58:18 2014 +0000
+++ b/sbin/route/route.c        Thu Nov 06 21:29:32 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: route.c,v 1.144 2013/10/19 15:59:15 christos Exp $     */
+/*     $NetBSD: route.c,v 1.145 2014/11/06 21:29:32 christos Exp $     */
 
 /*
  * Copyright (c) 1983, 1989, 1991, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)route.c    8.6 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: route.c,v 1.144 2013/10/19 15:59:15 christos Exp $");
+__RCSID("$NetBSD: route.c,v 1.145 2014/11/06 21:29:32 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -73,6 +73,7 @@
 #include "keywords.h"
 #include "extern.h"
 #include "prog_ops.h"
+#include "rtutil.h"
 
 union sockunion {
        struct  sockaddr sa;
@@ -95,7 +96,6 @@
                *so_ifp, *so_mpls;
 };
 
-static char *any_ntoa(const struct sockaddr *);
 static const char *route_strerror(int);
 static void set_metric(const char *, int);
 static int newroute(int, char *const *);
@@ -105,6 +105,7 @@
 #endif
 static int getaddr(int, const char *, struct hostent **, struct sou *);
 static int flushroutes(int, char *const [], int);
+static char *netmask_string(const struct sockaddr *, int, int);
 static int prefixlen(const char *, struct sou *);
 #ifndef SMALL
 static void interfaces(void);
@@ -168,7 +169,7 @@
                        doflush = 1;
                        break;
                case 'n':
-                       nflag = 1;
+                       nflag = RT_NFLAG;
                        break;
                case 'q':
                        qflag = 1;
@@ -224,7 +225,7 @@
                return newroute(argc, argv);
 
        case K_SHOW:
-               show(argc, argv);
+               show(argc, argv, nflag);
                return 0;
 
 #ifndef SMALL
@@ -245,6 +246,36 @@
        }
 }
 
+static char *
+netmask_string(const struct sockaddr *mask, int len, int family)
+{
+       static char smask[INET6_ADDRSTRLEN];
+       struct sockaddr_in nsin;
+       struct sockaddr_in6 nsin6;
+
+       if (len >= 0)
+               snprintf(smask, sizeof(smask), "%d", len);
+       else {
+               switch (family) {
+               case AF_INET:
+                       memset(&nsin, 0, sizeof(nsin));
+                       memcpy(&nsin, mask, mask->sa_len);
+                       snprintf(smask, sizeof(smask), "%s",
+                           inet_ntoa(nsin.sin_addr));
+                       break;
+               case AF_INET6:
+                       memset(&nsin6, 0, sizeof(nsin6));
+                       memcpy(&nsin6, mask, mask->sa_len);
+                       inet_ntop(family, &nsin6.sin6_addr, smask,
+                           sizeof(smask));
+                       break;
+               default:
+                       snprintf(smask, sizeof(smask), "%s", any_ntoa(mask));
+               }
+       }
+
+       return smask;
+}
 /*
  * Purge all entries in the routing tables not
  * associated with network interfaces.
@@ -320,12 +351,10 @@
                if (verbose)
                        print_rtmsg(rtm, rlen);
                else {
-                       (void)printf("%-20.20s ",
-                           routename(sa, NULL, rtm->rtm_flags));
+                       (void)printf("%-20.20s ", netname(sa, NULL, nflag));
                        sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) +
                            (char *)sa);
-                       (void)printf("%-20.20s ",
-                           routename(sa, NULL, RTF_HOST));
+                       (void)printf("%-20.20s ", routename(sa, nflag));
                        (void)printf("done\n");
                }
        }
@@ -333,402 +362,6 @@
        return 0;
 }
 
-
-static char hexlist[] = "0123456789abcdef";
-
-static char *
-any_ntoa(const struct sockaddr *sa)
-{
-       static char obuf[3 * 256];
-       const char *in;
-       char *out;
-       int len;
-
-#if __GNUC__ > 2
-       len = sa->sa_len - offsetof(struct sockaddr, sa_data);
-#else
-       len = sa->sa_len - ((struct sockaddr*)&sa->sa_data - sa);
-#endif
-       in  = sa->sa_data;
-       out = obuf;
-
-       do {
-               *out++ = hexlist[(*in >> 4) & 15];
-               *out++ = hexlist[(*in++)    & 15];
-               *out++ = '.';
-       } while (--len > 0);
-       out[-1] = '\0';
-       return obuf;
-}
-
-int
-netmask_length(struct sockaddr *nm, int family)
-{
-       static int
-           /* number of bits in a nibble */
-           _t[] = { 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4 },
-           /* good nibbles are 1111, 1110, 1100, 1000, 0000 */
-           _g[] = { 1,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1 };
-       int mask, good, zeroes, maskbytes, bit, i;
-       unsigned char *maskdata;
-
-       if (nm == NULL)
-               return 0;
-
-       mask = 0;
-       good = 1;
-       zeroes = 0;
-
-       switch (family) {
-       case AF_INET: {
-               struct sockaddr_in *nsin = (struct sockaddr_in *)nm;
-               maskdata = (unsigned char *)&nsin->sin_addr;
-               maskbytes = nsin->sin_len -
-                   ((caddr_t)&nsin->sin_addr - (caddr_t)nsin);
-               break;
-       }
-       case AF_INET6: {
-               struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nm;
-               maskdata = (unsigned char *)&sin6->sin6_addr;
-               maskbytes = sin6->sin6_len -
-                   ((caddr_t)&sin6->sin6_addr - (caddr_t)sin6);
-               break;
-       }
-       default:
-               return 0;
-       }
-
-       /*
-        * Count the bits in the nibbles of the mask, and marking the
-        * netmask as not good (or at best, non-standard and very
-        * discouraged, in the case of AF_INET) if we find either of
-        * a nibble with non-contiguous bits, or a non-zero nibble
-        * after we've found a zero nibble.
-        */
-       for (i = 0; i < maskbytes; i++) {
-               /* high nibble */
-               mask += bit = _t[maskdata[i] >> 4];
-               good &= _g[maskdata[i] >> 4];
-               if (zeroes && bit)
-                       good = 0;
-               if (bit == 0)
-                       zeroes = 1;
-               /* low nibble */
-               mask += bit = _t[maskdata[i] & 0xf];
-               good &= _g[maskdata[i] & 0xf];
-               if (zeroes && bit)
-                       good = 0;
-               if (bit == 0)
-                       zeroes = 1;
-       }
-
-       /*
-        * Always return the number of bits found, but as a negative
-        * if the mask wasn't one we like.
-        */
-       return good ? mask : -mask;
-}
-
-char *
-netmask_string(const struct sockaddr *mask, int len, int family)
-{
-       static char smask[INET6_ADDRSTRLEN];
-       struct sockaddr_in nsin;
-       struct sockaddr_in6 nsin6;
-
-       if (len >= 0)
-               snprintf(smask, sizeof(smask), "%d", len);
-       else {
-               switch (family) {
-               case AF_INET:
-                       memset(&nsin, 0, sizeof(nsin));



Home | Main Index | Thread Index | Old Index