Major changes:
[ipfw.git] / ipfw / ipfw2.c
index ed12d9c..8d6ed87 100644 (file)
@@ -358,7 +358,8 @@ do_cmd(int optname, void *optval, uintptr_t optlen)
        if (s < 0)
                err(EX_UNAVAILABLE, "socket");
 
-       if (optname == IP_FW_GET || optname == IP_DUMMYNET_GET ||
+       if (optname == IP_FW_GET || optname == IP_FW_DYN_GET ||
+           optname == IP_DUMMYNET_GET ||
            optname == IP_FW_ADD || optname == IP_FW_TABLE_LIST ||
            optname == IP_FW_TABLE_GETSIZE || 
            optname == IP_FW_NAT_GET_CONFIG || 
@@ -1719,12 +1720,14 @@ void
 ipfw_list(int ac, char *av[], int show_counters)
 {
        struct ip_fw *r;
-       ipfw_dyn_rule *dynrules, *d;
+       ipfw_dyn_rule *dynrules = NULL;
+       ipfw_dyn_rule *d;
 
 #define NEXT(r)        ((struct ip_fw *)((char *)r + RULESIZE(r)))
        char *lim;
        void *data = NULL;
-       int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width;
+       int bcwidth, n, nbytes, pcwidth, width, nstat;
+       int ndyn = 0;
        int exitval = EX_OK;
        int lac;
        char **lav;
@@ -1732,8 +1735,13 @@ ipfw_list(int ac, char *av[], int show_counters)
        char *endptr;
        int seen = 0;
        uint8_t set;
+       int ocmd = IP_FW_GET;
+
+       if (co.do_pipe)
+               ocmd = IP_DUMMYNET_GET;
+       else if (co.do_dynamic)
+               ocmd = IP_FW_DYN_GET;
 
-       const int ocmd = co.do_pipe ? IP_DUMMYNET_GET : IP_FW_GET;
        int nalloc = 1024;      /* start somewhere... */
 
        last = 0;
@@ -1767,19 +1775,25 @@ ipfw_list(int ac, char *av[], int show_counters)
         * Count static rules. They have variable size so we
         * need to scan the list to count them.
         */
+       nstat = 0;
+       r = data;
+
+       if (!co.do_dynamic) {
        for (nstat = 1, r = data, lim = (char *)data + nbytes;
                    r->rulenum < IPFW_DEFAULT_RULE && (char *)r < lim;
                    ++nstat, r = NEXT(r) )
                ; /* nothing */
+       }
 
        /*
         * Count dynamic rules. This is easier as they have
         * fixed size.
         */
-       r = NEXT(r);
-       dynrules = (ipfw_dyn_rule *)r ;
-       n = (char *)r - (char *)data;
-       ndyn = (nbytes - n) / sizeof *dynrules;
+       if (co.do_dynamic) {
+               dynrules = (ipfw_dyn_rule *)r ;
+               n = (char *)r - (char *)data;
+               ndyn = (nbytes - n) / sizeof *dynrules;
+       }
 
        /* if showing stats, figure out column widths ahead of time */
        bcwidth = pcwidth = 0;
@@ -1802,6 +1816,7 @@ ipfw_list(int ac, char *av[], int show_counters)
                                bcwidth = width;
                }
        }
+
        if (co.do_dynamic && ndyn) {
                for (n = 0, d = dynrules; n < ndyn; n++, d++) {
                        if (co.use_set) {
@@ -1822,6 +1837,7 @@ ipfw_list(int ac, char *av[], int show_counters)
                                bcwidth = width;
                }
        }
+
        /* if no rule numbers were specified, list all rules */
        if (ac == 0) {
                for (n = 0, r = data; n < nstat; n++, r = NEXT(r)) {
@@ -1847,6 +1863,7 @@ ipfw_list(int ac, char *av[], int show_counters)
 
        /* display specific rules requested on command line */
 
+       if (!co.do_dynamic) {
        for (lac = ac, lav = av; lac != 0; lac--) {
                /* convert command line rule # */
                last = rnum = strtoul(*lav++, &endptr, 10);
@@ -1874,6 +1891,7 @@ ipfw_list(int ac, char *av[], int show_counters)
                        warnx("rule %lu does not exist", rnum);
                }
        }
+       }
 
        if (co.do_dynamic && ndyn) {
                printf("## Dynamic rules:\n");