diff options
author | Gilbert Ramirez <gram@alumni.rice.edu> | 2001-06-22 16:29:15 +0000 |
---|---|---|
committer | Gilbert Ramirez <gram@alumni.rice.edu> | 2001-06-22 16:29:15 +0000 |
commit | 39b0e82f636f946cca77e6c4302d90761983fedf (patch) | |
tree | adad8a3c0cfb8121539f69b3814133a423a4c733 /epan | |
parent | b3c464723c4292eb31a11c8e20c9850db8683702 (diff) | |
download | wireshark-39b0e82f636f946cca77e6c4302d90761983fedf.tar.gz wireshark-39b0e82f636f946cca77e6c4302d90761983fedf.tar.bz2 wireshark-39b0e82f636f946cca77e6c4302d90761983fedf.zip |
Support CIDR notation in IPv4 address filtering.
svn path=/trunk/; revision=3601
Diffstat (limited to 'epan')
-rw-r--r-- | epan/dfilter/grammar.lemon | 12 | ||||
-rw-r--r-- | epan/dfilter/scanner.l | 13 | ||||
-rw-r--r-- | epan/ftypes/ftype-ipv4.c | 71 |
3 files changed, 85 insertions, 11 deletions
diff --git a/epan/dfilter/grammar.lemon b/epan/dfilter/grammar.lemon index b78a1bff1a..e252b496a8 100644 --- a/epan/dfilter/grammar.lemon +++ b/epan/dfilter/grammar.lemon @@ -1,4 +1,4 @@ -/* $Id: grammar.lemon,v 1.3 2001/03/02 17:04:23 gram Exp $ */ +/* $Id: grammar.lemon,v 1.4 2001/06/22 16:29:13 gram Exp $ */ %include { #ifdef HAVE_CONFIG_H @@ -152,6 +152,16 @@ entity(E) ::= FIELD(F). { E = F; } entity(E) ::= STRING(S). { E = S; } entity(E) ::= range(R). { E = R; } +/* CIDR: ADDRESS/NET */ +entity(E) ::= STRING(A) SLASH STRING(N). +{ + E = stnode_new(STTYPE_STRING, g_strjoin("/", stnode_data(A), + stnode_data(N), NULL)); + + stnode_free(A); + stnode_free(N); +} + /* Ranges */ range(R) ::= FIELD(F) LBRACKET drnode_list(L) RBRACKET. diff --git a/epan/dfilter/scanner.l b/epan/dfilter/scanner.l index 7119e5b5de..a80d9386a8 100644 --- a/epan/dfilter/scanner.l +++ b/epan/dfilter/scanner.l @@ -1,6 +1,6 @@ %{ /* - * $Id: scanner.l,v 1.4 2001/03/02 17:04:23 gram Exp $ + * $Id: scanner.l,v 1.5 2001/06/22 16:29:13 gram Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -53,10 +53,6 @@ static gboolean str_to_guint32(char *s, guint32* pint); %x RANGE_INT %x RANGE_PUNCT -BWCHARS [[:alnum:]\[\]\-_.+!@#%^&*=/:] -INITVAR [_A-Za-z] -VARCHARS [[:alnum:]_] - %% @@ -66,6 +62,7 @@ VARCHARS [[:alnum:]_] "(" return simple(TOKEN_LPAREN); ")" return simple(TOKEN_RPAREN); +"/" return simple(TOKEN_SLASH); "==" return simple(TOKEN_TEST_EQ); "eq" return simple(TOKEN_TEST_EQ); @@ -88,6 +85,7 @@ VARCHARS [[:alnum:]_] "or" return simple(TOKEN_TEST_OR); + "[" { BEGIN(RANGE_INT); return simple(TOKEN_LBRACKET); @@ -144,6 +142,10 @@ VARCHARS [[:alnum:]_] } } +. { + /* Default */ + return set_lval(TOKEN_STRING, g_strdup(yytext)); +} %% @@ -159,6 +161,7 @@ simple(int token) case TOKEN_COLON: case TOKEN_COMMA: case TOKEN_HYPHEN: + case TOKEN_SLASH: case TOKEN_TEST_EQ: case TOKEN_TEST_NE: case TOKEN_TEST_GT: diff --git a/epan/ftypes/ftype-ipv4.c b/epan/ftypes/ftype-ipv4.c index cdbf93b1d9..09a92df649 100644 --- a/epan/ftypes/ftype-ipv4.c +++ b/epan/ftypes/ftype-ipv4.c @@ -1,5 +1,5 @@ /* - * $Id: ftype-ipv4.c,v 1.3 2001/03/02 17:17:56 gram Exp $ + * $Id: ftype-ipv4.c,v 1.4 2001/06/22 16:29:15 gram Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs <gerald@zing.org> @@ -25,6 +25,8 @@ #include "config.h" #endif +#include <string.h> + #include <ftypes-int.h> #include "ipv4.h" #include "resolv.h" @@ -47,14 +49,73 @@ static gboolean val_from_string(fvalue_t *fv, char *s, LogFunc log) { guint32 addr; + unsigned int nmask_bits; + + char *has_slash, *s_copy = NULL; + char *net_str, *addr_str; + fvalue_t *nmask_fvalue; + + /* Look for CIDR: Is there a single slash in the string? */ + has_slash = index(s, '/'); + if (has_slash) { + /* Make a copy of the string and use strtok() to + * get the address portion. */ + s_copy = g_strdup(s); + addr_str = strtok(s_copy, "/"); + + /* I just checked for slash! I shouldn't get NULL here. + * Double check just in case. */ + if (!addr_str) { + log("Unexpected strtok() error parsing IP address: %s", s_copy); + g_free(s_copy); + return FALSE; + } + } + else { + addr_str = s; + } - if (!get_host_ipaddr(s, &addr)) { - log("\"%s\" is not a valid hostname or IPv4 address.", s); + if (!get_host_ipaddr(addr_str, &addr)) { + log("\"%s\" is not a valid hostname or IPv4 address.", addr_str); + if (has_slash) { + g_free(s_copy); + } return FALSE; } + ipv4_addr_set_host_order_addr(&(fv->value.ipv4), addr); - /*ipv4_addr_set_netmask_bits(&node->value.ipv4, nmask_bits);*/ - ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32); + + /* If CIDR, get netmask bits. */ + if (has_slash) { + net_str = strtok(NULL, "/"); + /* I checked for slash! I shouldn't get NULL here. + * Double check just in case. */ + if (!net_str) { + log("Unexpected strtok() error parsing netmask: %s", s_copy); + g_free(s_copy); + return FALSE; + } + + nmask_fvalue = fvalue_from_string(FT_UINT32, net_str, log); + g_free(s_copy); + if (!nmask_fvalue) { + return FALSE; + } + nmask_bits = fvalue_get_integer(nmask_fvalue); + fvalue_free(nmask_fvalue); + + if (nmask_bits > 32) { + log("Netmask bits in a CIDR IPv4 address should be <= 32, not %u", + nmask_bits); + return FALSE; + } + ipv4_addr_set_netmask_bits(&fv->value.ipv4, nmask_bits); + } + else { + /* Not CIDR; mask covers entire address. */ + ipv4_addr_set_netmask_bits(&(fv->value.ipv4), 32); + } + return TRUE; } |