Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 24 Jan 2003 20:02:38 +0300 (MSK)
From:      Maxim Konovalov <maxim@FreeBSD.org>
To:        Giorgos Keramidas <keramida@FreeBSD.org>
Cc:        freebsd-audit@FreeBSD.org, "" <des@FreeBSD.org>, "" <luigi@FreeBSD.org>
Subject:   Re: (fwd) bin/47196: ipfw won't format correctly output from 'ipfw show' command
Message-ID:  <20030124194701.P19393@news1.macomnet.ru>
In-Reply-To: <20030123195335.GC678@gothmog.gr>
References:  <20030123034022.GA587@gothmog.gr> <20030123184329.T77556@news1.macomnet.ru> <20030123195335.GC678@gothmog.gr>

next in thread | previous in thread | raw e-mail | index | archive | help
On 21:53+0200, Jan 23, 2003, Giorgos Keramidas wrote:

> On 2003-01-23 19:10, Maxim Konovalov <maxim@FreeBSD.org> wrote:
> > On 05:40+0200, Jan 23, 2003, Giorgos Keramidas wrote:
> > > I've sent the following 2 days ago to ipfw@ but received no reply.
> > > Does anyone feel like testing/reviewing or committing this?  This
> > > change should be all we need to fix & close PR bin/47196 :-)
> >
> > Formatting was broken in rev. 1.103 ipfw.c in -CURRENT and ipfw2
> > in -CURRENT and -STABLE inherited the bug later.
> >
> > I think we should rever this part of rev. 1.103 of ipfw.c in ipfw2.c:
>
> Not quite.  See inline comments.

Yes, you are right. Revert 1.103 a little more. Note we always show
packets/bytes for dynamic rules.

Index: ipfw2.c
===================================================================
RCS file: /home/ncvs/src/sbin/ipfw/ipfw2.c,v
retrieving revision 1.21
diff -u -r1.21 ipfw2.c
--- ipfw2.c	12 Jan 2003 03:31:10 -0000	1.21
+++ ipfw2.c	24 Jan 2003 16:34:48 -0000
@@ -802,7 +802,7 @@
 }

 static void
-show_ipfw(struct ip_fw *rule)
+show_ipfw(struct ip_fw *rule, int pcwidth, int bcwidth)
 {
 	static int twidth = 0;
 	int l;
@@ -823,7 +823,7 @@
 	printf("%05u ", rule->rulenum);

 	if (do_acct)
-		printf("%10qu %10qu ", rule->pcnt, rule->bcnt);
+		printf("%*qu %*qu ", pcwidth, rule->pcnt, bcwidth, rule->bcnt);

 	if (do_time) {
 		char timestr[30];
@@ -1202,7 +1202,7 @@
 }

 static void
-show_dyn_ipfw(ipfw_dyn_rule *d)
+show_dyn_ipfw(ipfw_dyn_rule *d, int pcwidth, int bcwidth)
 {
 	struct protoent *pe;
 	struct in_addr a;
@@ -1212,8 +1212,8 @@
 			return;
 	}

-	printf("%05d %10qu %10qu (%ds)",
-	    d->rulenum, d->pcnt, d->bcnt, d->expire);
+	printf("%05d %*qu %*qu (%ds)", d->rulenum, pcwidth, d->pcnt, bcwidth,
+	    d->bcnt, d->expire);
 	switch (d->dyn_type) {
 	case O_LIMIT_PARENT:
 		printf(" PARENT %d", d->count);
@@ -1557,7 +1557,7 @@
 	ipfw_dyn_rule *dynrules, *d;

 	void *lim, *data = NULL;
-	int n, nbytes, nstat, ndyn;
+	int bcwidth, n, nbytes, nstat, ndyn, pcwidth, width;
 	int exitval = EX_OK;
 	int lac;
 	char **lav;
@@ -1607,16 +1607,43 @@
 	n = (void *)r - data;
 	ndyn = (nbytes - n) / sizeof *dynrules;

+	/* if showing stats, figure out column widths ahead of time */
+	bcwidth = pcwidth = 0;
+	if (do_acct) {
+		for (n = 0, r = data; n < nstat;
+		    n++, r = (void *)r + RULESIZE(r)) {
+			/* packet counter */
+			width = snprintf(NULL, 0, "%qu", r->pcnt);
+			if (width > pcwidth)
+				pcwidth = width;
+
+			/* byte counter */
+			width = snprintf(NULL, 0, "%qu", r->bcnt);
+			if (width > bcwidth)
+				bcwidth = width;
+		}
+	}
+	if (do_dynamic && ndyn) {
+		for (n = 0, d = dynrules; n < ndyn; n++, d++) {
+			width = snprintf(NULL, 0, "%qu", d->pcnt);
+			if (width > pcwidth)
+				pcwidth = width;
+
+			width = snprintf(NULL, 0, "%qu", d->bcnt);
+			if (width > bcwidth)
+				bcwidth = width;
+		}
+	}
 	/* if no rule numbers were specified, list all rules */
 	if (ac == 0) {
 		for (n = 0, r = data; n < nstat;
 		    n++, r = (void *)r + RULESIZE(r) )
-			show_ipfw(r);
+			show_ipfw(r, pcwidth, bcwidth);

 		if (do_dynamic && ndyn) {
 			printf("## Dynamic rules (%d):\n", ndyn);
 			for (n = 0, d = dynrules; n < ndyn; n++, d++)
-				show_dyn_ipfw(d);
+				show_dyn_ipfw(d, pcwidth, bcwidth);
 		}
 		goto done;
 	}
@@ -1636,7 +1663,7 @@
 			if (r->rulenum > rnum)
 				break;
 			if (r->rulenum == rnum) {
-				show_ipfw(r);
+				show_ipfw(r, pcwidth, bcwidth);
 				seen = 1;
 			}
 		}
@@ -1659,7 +1686,7 @@
 				if (d->rulenum > rnum)
 					break;
 				if (d->rulenum == rnum)
-					show_dyn_ipfw(d);
+					show_dyn_ipfw(d, pcwidth, bcwidth);
 			}
 		}
 	}
@@ -3221,7 +3248,7 @@
 	if (getsockopt(s, IPPROTO_IP, IP_FW_ADD, rule, &i) == -1)
 		err(EX_UNAVAILABLE, "getsockopt(%s)", "IP_FW_ADD");
 	if (!do_quiet)
-		show_ipfw(rule);
+		show_ipfw(rule, 10, 10);
 }

 static void

%%%

--
Maxim Konovalov, maxim@macomnet.ru, maxim@FreeBSD.org

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-audit" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030124194701.P19393>