From owner-svn-src-projects@FreeBSD.ORG Tue Aug 12 18:10:30 2014 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 1834A549 for ; Tue, 12 Aug 2014 18:10:30 +0000 (UTC) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 8D00038D6 for ; Tue, 12 Aug 2014 18:02:11 +0000 (UTC) Received: from melifaro (uid 1268) (envelope-from melifaro@FreeBSD.org) id 6ba3 by svn.freebsd.org (DragonFly Mail Agent v0.9+); Tue, 12 Aug 2014 18:02:11 +0000 From: Alexander V. Chernikov Date: Tue, 12 Aug 2014 18:02:11 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r269886 - projects/ipfw/sbin/ipfw X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Message-Id: <53ea56a3.6ba3.6c06551c@svn.freebsd.org> X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Aug 2014 18:10:30 -0000 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 #include #include -#include /* offsetof */ #include #include #include #include -#define IPFW_INTERNAL /* Access to protected structures in ip_fw.h. */ - #include -#include -#include /* def. of struct route */ #include #include #include -#include #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) {