Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 12 Aug 2014 18:02:11 +0000 (UTC)
From:      Alexander V. Chernikov <melifaro@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-projects@freebsd.org
Subject:   svn commit: r269886 - projects/ipfw/sbin/ipfw
Message-ID:  <53ea56a3.6ba3.6c06551c@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: melifaro
Date: Tue Aug 12 18:02:10 2014
New Revision: 269886
URL: http://svnweb.freebsd.org/changeset/base/269886

Log:
  * Update table_handler cmd list
  * Implement partial cmd matching inside table handler.

Modified:
  projects/ipfw/sbin/ipfw/ipfw2.c
  projects/ipfw/sbin/ipfw/ipfw2.h
  projects/ipfw/sbin/ipfw/tables.c

Modified: projects/ipfw/sbin/ipfw/ipfw2.c
==============================================================================
--- projects/ipfw/sbin/ipfw/ipfw2.c	Tue Aug 12 17:56:48 2014	(r269885)
+++ projects/ipfw/sbin/ipfw/ipfw2.c	Tue Aug 12 18:02:10 2014	(r269886)
@@ -681,6 +681,37 @@ match_token(struct _s_x *table, char *st
 }
 
 /**
+ * match_token takes a table and a string, returns the value associated
+ * with the string for the best match.
+ *
+ * Returns:
+ * value from @table for matched records
+ * -1 for non-matched records
+ * -2 if more than one records match @string.
+ */
+int
+match_token_relaxed(struct _s_x *table, char *string)
+{
+	struct _s_x *pt, *m;
+	int i, c;
+
+	i = strlen(string);
+	c = 0;
+
+	for (pt = table ; i != 0 && pt->s != NULL ; pt++) {
+		if (strncmp(pt->s, string, i) != 0)
+			continue;
+		m = pt;
+		c++;
+	}
+
+	if (c == 1)
+		return (m->x);
+
+	return (c > 0 ? -2: -1);
+}
+
+/**
  * match_value takes a table and a value, returns the string associated
  * with the value (NULL in case of failure).
  */

Modified: projects/ipfw/sbin/ipfw/ipfw2.h
==============================================================================
--- projects/ipfw/sbin/ipfw/ipfw2.h	Tue Aug 12 17:56:48 2014	(r269885)
+++ projects/ipfw/sbin/ipfw/ipfw2.h	Tue Aug 12 18:02:10 2014	(r269886)
@@ -261,6 +261,7 @@ int stringnum_cmp(const char *a, const c
 
 /* utility functions */
 int match_token(struct _s_x *table, char *string);
+int match_token_relaxed(struct _s_x *table, char *string);
 char const *match_value(struct _s_x *p, int value);
 size_t concat_tokens(char *buf, size_t bufsize, struct _s_x *table,
     char *delimiter);

Modified: projects/ipfw/sbin/ipfw/tables.c
==============================================================================
--- projects/ipfw/sbin/ipfw/tables.c	Tue Aug 12 17:56:48 2014	(r269885)
+++ projects/ipfw/sbin/ipfw/tables.c	Tue Aug 12 18:02:10 2014	(r269886)
@@ -30,21 +30,15 @@
 #include <err.h>
 #include <errno.h>
 #include <netdb.h>
-#include <stddef.h>	/* offsetof */
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <sysexits.h>
 
-#define IPFW_INTERNAL	/* Access to protected structures in ip_fw.h. */
-
 #include <net/if.h>
-#include <net/if_dl.h>
-#include <net/route.h> /* def. of struct route */
 #include <netinet/in.h>
 #include <netinet/ip_fw.h>
 #include <arpa/inet.h>
-#include <alias.h>
 
 #include "ipfw2.h"
 
@@ -134,15 +128,34 @@ lookup_host (char *host, struct in_addr 
 	return(0);
 }
 
+static int
+get_token(struct _s_x *table, char *string, char *errbase)
+{
+	int tcmd;
+
+	if ((tcmd = match_token_relaxed(table, string)) < 0)
+		errx(EX_USAGE, "%s %s %s",
+		    (tcmd == 0) ? "invalid" : "ambiguous", errbase, string);
+
+	return (tcmd);
+}
+
 /*
  * This one handles all table-related commands
  * 	ipfw table NAME create ...
+ * 	ipfw table NAME modify ...
  * 	ipfw table NAME destroy
- * 	ipfw table NAME add addr[/masklen] [value]
- * 	ipfw table NAME delete addr[/masklen]
+ * 	ipfw table NAME swap NAME
+ * 	ipfw table NAME lock
+ * 	ipfw table NAME unlock
+ * 	ipfw table NAME add addr[/masklen] [value] 
+ * 	ipfw table NAME add [addr[/masklen] value] [addr[/masklen] value] ..
+ * 	ipfw table NAME delete addr[/masklen] [addr[/masklen]] ..
+ * 	ipfw table NAME lookup addr
  * 	ipfw table {NAME | all} flush
  * 	ipfw table {NAME | all} list
  * 	ipfw table {NAME | all} info
+ * 	ipfw table {NAME | all} detail
  */
 void
 ipfw_table_handler(int ac, char *av[])
@@ -178,15 +191,13 @@ ipfw_table_handler(int ac, char *av[])
 	ac--; av++;
 	NEED1("table needs command");
 
-	if ((tcmd = match_token(tablecmds, *av)) == -1)
-		errx(EX_USAGE, "invalid table command %s", *av);
+	tcmd = get_token(tablecmds, *av, "table command");
 	/* Check if atomic operation was requested */
 	atomic = 0;
 	if (tcmd == TOK_ATOMIC) {
 		ac--; av++;
 		NEED1("atomic needs command");
-		if ((tcmd = match_token(tablecmds, *av)) == -1)
-			errx(EX_USAGE, "invalid table command %s", *av);
+		tcmd = get_token(tablecmds, *av, "table command");
 		switch (tcmd) {
 		case TOK_ADD:
 			break;
@@ -385,8 +396,7 @@ table_create(ipfw_obj_header *oh, int ac
 	xi.vtype = IPFW_VTYPE_U32;
 
 	while (ac > 0) {
-		if ((tcmd = match_token(tablenewcmds, *av)) == -1)
-			errx(EX_USAGE, "unknown option: %s", *av);
+		tcmd = get_token(tablenewcmds, *av, "option");
 		ac--; av++;
 
 		switch (tcmd) {
@@ -497,8 +507,7 @@ table_modify(ipfw_obj_header *oh, int ac
 	memset(&xi, 0, sizeof(xi));
 
 	while (ac > 0) {
-		if ((tcmd = match_token(tablenewcmds, *av)) == -1)
-			errx(EX_USAGE, "unknown option: %s", *av);
+		tcmd = get_token(tablenewcmds, *av, "option");
 		ac--; av++;
 
 		switch (tcmd) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?53ea56a3.6ba3.6c06551c>