Sync p_rttables() to netstat(1) version. Pointed out by claudio and
mpi.

Remaining differences are pledge and priority handling which only
route(8) has.

While here switch flushroutes to get_sysctl() function.

OK?

diff --git route.c route.c
index d93374578c5..85e76621dd3 100644
--- route.c
+++ route.c
@@ -281,7 +281,7 @@ int
 flushroutes(int argc, char **argv)
 {
        size_t needed;
-       int mib[7], rlen, seqno, af = AF_UNSPEC;
+       int mib[7], mcnt, rlen, seqno, af = AF_UNSPEC;
        char *buf = NULL, *next, *lim = NULL;
        struct rt_msghdr *rtm;
        struct sockaddr *sa;
@@ -333,21 +333,10 @@ flushroutes(int argc, char **argv)
        mib[4] = NET_RT_DUMP;
        mib[5] = prio;
        mib[6] = tableid;
-       while (1) {
-               if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1)
-                       err(1, "route-sysctl-estimate");
-               if (needed == 0)
-                       break;
-               if ((buf = realloc(buf, needed)) == NULL)
-                       err(1, "realloc");
-               if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) {
-                       if (errno == ENOMEM)
-                               continue;
-                       err(1, "actual retrieval of routing table");
-               }
-               lim = buf + needed;
-               break;
-       }
+       mcnt = 7;
+
+       needed = get_sysctl(mib, mcnt, &buf);
+       lim = buf + needed;
 
        if (pledge("stdio dns", NULL) == -1)
                err(1, "pledge");
diff --git show.c show.c
index 5898a19cd45..68731c78591 100644
--- show.c
+++ show.c
@@ -107,6 +107,29 @@ char       *routename6(struct sockaddr_in6 *);
 char   *netname4(in_addr_t, struct sockaddr_in *);
 char   *netname6(struct sockaddr_in6 *, struct sockaddr_in6 *);
 
+size_t
+get_sysctl(const int *mib, u_int mcnt, char **buf)
+{
+       size_t needed;
+
+       while (1) {
+               if (sysctl(mib, mcnt, NULL, &needed, NULL, 0) == -1)
+                       err(1, "sysctl-estimate");
+               if (needed == 0)
+                       break;
+               if ((*buf = realloc(*buf, needed)) == NULL)
+                       err(1, NULL);
+               if (sysctl(mib, mcnt, *buf, &needed, NULL, 0) == -1) {
+                       if (errno == ENOMEM)
+                               continue;
+                       err(1, "sysctl");
+               }
+               break;
+       }
+
+       return needed;
+}
+
 /*
  * Print routing tables.
  */
@@ -116,7 +139,7 @@ p_rttables(int af, u_int tableid, char prio)
        struct rt_msghdr *rtm;
        char *buf = NULL, *next, *lim = NULL;
        size_t needed;
-       int mib[7];
+       int mib[7], mcnt;
        struct sockaddr *sa;
 
        mib[0] = CTL_NET;
@@ -126,22 +149,10 @@ p_rttables(int af, u_int tableid, char prio)
        mib[4] = NET_RT_DUMP;
        mib[5] = prio;
        mib[6] = tableid;
+       mcnt = 7;
 
-       while (1) {
-               if (sysctl(mib, 7, NULL, &needed, NULL, 0) == -1)
-                       err(1, "route-sysctl-estimate");
-               if (needed == 0)
-                       break;
-               if ((buf = realloc(buf, needed)) == NULL)
-                       err(1, NULL);
-               if (sysctl(mib, 7, buf, &needed, NULL, 0) == -1) {
-                       if (errno == ENOMEM)
-                               continue;
-                       err(1, "sysctl of routing table");
-               }
-               lim = buf + needed;
-               break;
-       }
+       needed = get_sysctl(mib, mcnt, &buf);
+       lim = buf + needed;
 
        if (pledge("stdio dns", NULL) == -1)
                err(1, "pledge");
@@ -156,9 +167,8 @@ p_rttables(int af, u_int tableid, char prio)
                        sa = (struct sockaddr *)(next + rtm->rtm_hdrlen);
                        p_rtentry(rtm);
                }
-               free(buf);
-               buf = NULL;
        }
+       free(buf);
 }
 
 /*
diff --git show.h show.h
index 03999b7fdd7..461d5a39c5e 100644
--- show.h
+++ show.h
@@ -34,6 +34,7 @@ void   p_sockaddr(struct sockaddr *, struct sockaddr *, int, 
int);
 char   *routename(struct sockaddr *);
 char   *netname(struct sockaddr *, struct sockaddr *);
 char   *mpls_op(u_int32_t);
+size_t  get_sysctl(const int *, u_int, char **);
 
 extern int nflag;
 extern int Fflag;


-- 
I'm not entirely sure you are real.

Reply via email to