Have "get_host_ipaddr()" return a Boolean indicating whether it

succeeded or failed, and, if it succeeded, have it fill in the IP
address if found through a pointer passed as the second argument.

Have it first try interpreting its first argument as a dotted-quad IP
address, with "inet_aton()", and, if that fails, have it try to
interpret it as a host name with "gethostbyname()"; don't bother with
"gethostbyaddr()", as we should be allowed to filter on IP addresses
even if there's no host name associated with them (there's no guarantee
that "gethostbyaddr()" will succeed if handed an IP address with no
corresponding name - and it looks as if FreeBSD 3.2, at least, may not
succeed in that case).

Add a "dfilter_fail()" routine that takes "printf()"-like arguments and
uses them to set an error message for the parse; doing so means that
even if the filter expression is syntactically valid, we treat it as
being invalid.  (Is there a better way to force a parse to fail from
arbitrary places in routines called by the parser?)

Use that routine in the lexical analyzer.

If that error message was set, use it as is as the failure message,
rather than adding "Unable to parse filter string XXX" to it.

Have the code to handle IP addresses and host names in display filters
check whether "get_host_ipaddr()" succeeded or failed and, if it failed,
arrange that the parse fail with an error message indicating the source
of the problem.

svn path=/trunk/; revision=802
This commit is contained in:
Guy Harris 1999-10-11 03:03:12 +00:00
parent 5779d0b754
commit 29b9c8a285
6 changed files with 76 additions and 41 deletions

View File

@ -3,7 +3,7 @@
/* dfilter-grammar.y
* Parser for display filters
*
* $Id: dfilter-grammar.y,v 1.20 1999/10/07 21:47:20 guy Exp $
* $Id: dfilter-grammar.y,v 1.21 1999/10/11 03:03:10 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -561,7 +561,12 @@ dfilter_mknode_ipv4_value(char *host)
node->elem_size = sizeof(guint32);
node->fill_array_func = fill_array_numeric_value; /* cheating ! */
node->check_relation_func = check_relation_numeric; /* cheating ! */
node->value.numeric = get_host_ipaddr(host);
if (!get_host_ipaddr(host, &node->value.numeric)) {
/* "Scientist, you've failed." */
dfilter_fail("\"%s\" isn't a valid host name or IP address.",
host);
node->value.numeric = INADDR_NONE;
}
node->value.numeric = htonl(node->value.numeric);
gnode = g_node_new(node);

View File

@ -2,7 +2,7 @@
* Definitions for routines common to multiple modules in the display
* filter code, but not used outside that code.
*
* $Id: dfilter-int.h,v 1.6 1999/10/10 18:15:34 guy Exp $
* $Id: dfilter-int.h,v 1.7 1999/10/11 03:03:11 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -40,8 +40,17 @@ extern GSList *gnode_slist;
int dfilter_lex(void);
void dfilter_error(char *s);
/* Report an error during compilation of a filter */
void dfilter_error(char *s);
/* Report an error during compilation of a filter; this is called by code
* other than parser code, so all it does is record that an error occurred,
* so that even if the filter is nominally syntactically valid, we still
* fail.
*/
#if __GNUC__ == 2
void dfilter_fail(char *fmt, ...)
__attribute__((format (printf, 1, 2)));
#else
void dfilter_fail(char *fmt, ...);
#endif
/* functions that dfilter-grammar.y needs during parsing*/
gboolean check_relation_numeric(gint operand, GArray *a, GArray *b);

View File

@ -3,7 +3,7 @@
/* dfilter-scanner.l
* Scanner for display filters
*
* $Id: dfilter-scanner.l,v 1.16 1999/10/09 14:14:53 deniel Exp $
* $Id: dfilter-scanner.l,v 1.17 1999/10/11 03:03:10 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -171,19 +171,23 @@ le|\<\= { dfilter_lval.operand = TOK_LE; return TOK_LE; }
retval = T_FT_UINT32;
break;
case FT_DOUBLE:
dfilter_error("sorry, we don't yet support filtering on floating-point values");
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on floating-point values.",
yytext);
retval = 0;
break;
case FT_ABSOLUTE_TIME:
dfilter_error("sorry, we don't yet support filtering on time-of-day values");
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-of-day values.",
yytext);
retval = 0;
break;
case FT_RELATIVE_TIME:
dfilter_error("sorry, we don't yet support filtering on time-delta values");
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on time-delta values.",
yytext);
retval = 0;
break;
case FT_STRING:
dfilter_error("sorry, we don't yet support filtering on string values");
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on string values.",
yytext);
retval = 0;
break;
case FT_ETHER:
@ -196,7 +200,8 @@ le|\<\= { dfilter_lval.operand = TOK_LE; return TOK_LE; }
retval = T_FT_IPv4;
break;
case FT_IPv6:
dfilter_error("sorry, we don't yet support filtering on IPv6 addresses");
dfilter_fail("Sorry, you can't filter on field \"%s\", as we don't yet support filtering on IPv6 addresses.",
yytext);
retval = 0;
break;
case FT_IPXNET:

View File

@ -1,7 +1,7 @@
/* dfilter.c
* Routines for display filters
*
* $Id: dfilter.c,v 1.24 1999/10/07 21:47:18 guy Exp $
* $Id: dfilter.c,v 1.25 1999/10/11 03:03:10 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@zing.org>
@ -169,7 +169,7 @@ dfilter_compile(dfilter *df, gchar *dfilter_text)
/* Errors not found by the parser may not cause the parse to
* fail; if "dfilter_error_msg" is set, it means somebody
* else called "dfilter_error()", e.g. the lexical analyzer,
* else called "dfilter_fail()", e.g. the lexical analyzer,
* so treat that as a parse error. */
if (dfilter_error_msg != NULL)
retval = 1;
@ -179,14 +179,8 @@ dfilter_compile(dfilter *df, gchar *dfilter_text)
snprintf(dfilter_error_msg_buf, sizeof(dfilter_error_msg_buf),
"Unable to parse filter string \"%s\".",
dfilter_text);
} else {
/* An error message was supplied; use it in the
* error we display. */
snprintf(dfilter_error_msg_buf, sizeof(dfilter_error_msg_buf),
"Unable to parse filter string \"%s\": %s.",
dfilter_text, dfilter_error_msg);
dfilter_error_msg = &dfilter_error_msg_buf[0];
}
dfilter_error_msg = &dfilter_error_msg_buf[0];
}
/* Clear the list of allocated nodes */
@ -273,12 +267,6 @@ clear_byte_array(gpointer data, gpointer user_data)
void
dfilter_error(char *s)
{
/* Remember the error message we were handed, unless it's
* the boring "parse error" message used by YACC.
*/
if (strcmp(s, "parse error") != 0)
dfilter_error_msg = s;
/* The only data we have to worry about freeing is the
* data used by any GNodes that were allocated during
* parsing. The data in those Gnodes will be cleared
@ -305,6 +293,23 @@ dfilter_error(char *s)
*/
}
/* Called when an error other than a parsing error occurs. */
void
dfilter_fail(char *format, ...)
{
va_list ap;
/* If we've already reported one error, don't overwrite it with this
* one. */
if (dfilter_error_msg != NULL)
return;
va_start(ap, format);
vsnprintf(dfilter_error_msg_buf, sizeof dfilter_error_msg_buf, format, ap);
dfilter_error_msg = dfilter_error_msg_buf;
va_end(ap);
}
static void
unlink_gnode_children(gpointer gnode_ptr, gpointer user_data)
{

View File

@ -1,7 +1,7 @@
/* resolv.c
* Routines for network object lookup
*
* $Id: resolv.c,v 1.12 1999/09/26 14:39:12 deniel Exp $
* $Id: resolv.c,v 1.13 1999/10/11 03:03:11 guy Exp $
*
* Laurent Deniel <deniel@worldnet.fr>
*
@ -56,6 +56,8 @@
#include <netdb.h>
#endif
#include <arpa/inet.h>
#include <signal.h>
#ifdef HAVE_SYS_SOCKET_H
@ -847,22 +849,29 @@ extern u_char *get_manuf_name(u_char *addr)
/* return IP address of either hostname or IP address in text format.
/* Translate a string, assumed either to be a dotted-quad IP address or
* a host name, to a numeric IP address. Return TRUE if we succeed and
* set "*addrp" to that numeric IP address; return FALSE if we fail.
* Used more in the dfilter parser rather than in packet dissectors */
unsigned long get_host_ipaddr(const char *host)
gboolean get_host_ipaddr(const char *host, guint32 *addrp)
{
struct hostent *hp = NULL;
unsigned long ipaddr;
struct in_addr ipaddr;
struct hostent *hp;
hp = gethostbyname(host);
if (hp == NULL) {
hp = gethostbyaddr(host, strlen(host), AF_INET);
if (!inet_aton(host, &ipaddr)) {
/* It's not a valid dotted-quad IP address; is it a valid
* host name? */
hp = gethostbyname(host);
if (hp == NULL) {
return 0;
/* No. */
return FALSE;
} else {
/* XXX - is "hp->h_length" the size of a
* "struct in_addr"? It should be. */
memcpy(&ipaddr, hp->h_addr, hp->h_length);
}
}
memcpy(&ipaddr, hp->h_addr, hp->h_length);
return ntohl(ipaddr);
*addrp = ntohl(ipaddr.s_addr);
return TRUE;
}

View File

@ -1,7 +1,7 @@
/* resolv.h
* Definitions for network object lookup
*
* $Id: resolv.h,v 1.6 1999/09/26 14:39:12 deniel Exp $
* $Id: resolv.h,v 1.7 1999/10/11 03:03:12 guy Exp $
*
* Laurent Deniel <deniel@worldnet.fr>
*
@ -71,7 +71,9 @@ extern u_char *get_ether_addr(u_char *name);
/* adds a hostname/IP in the hash table */
extern void add_host_name(u_int addr, u_char *name);
/* Returns IP address for a string representing the hostname or dotted-decial IP address */
unsigned long get_host_ipaddr(const char *host);
/* Translates a string representing the hostname or dotted-decimal IP address
* into a numeric IP address value, returning TRUE if it succeeds and
* FALSE if it fails. */
gboolean get_host_ipaddr(const char *host, guint32 *addrp);
#endif /* __RESOLV_H__ */