2007-01-18 02:54:56 +00:00
|
|
|
/* dfilter-macro.c
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 2001 Gerald Combs
|
2007-05-03 16:11:18 +00:00
|
|
|
*
|
2007-01-18 02:54:56 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-05-03 16:11:18 +00:00
|
|
|
*
|
2007-01-18 02:54:56 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2007-05-03 16:11:18 +00:00
|
|
|
*
|
2007-01-18 02:54:56 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-01-18 02:54:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2015-11-07 23:12:18 +00:00
|
|
|
#ifdef DUMP_DFILTER_MACRO
|
2007-01-18 02:54:56 +00:00
|
|
|
#include <stdio.h>
|
2016-07-24 12:53:39 +00:00
|
|
|
#include <wsutil/ws_printf.h> /* ws_debug_printf */
|
2015-11-07 23:12:18 +00:00
|
|
|
#endif
|
2007-01-18 02:54:56 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "dfilter-int.h"
|
|
|
|
#include "dfilter.h"
|
|
|
|
#include "dfilter-macro.h"
|
2013-11-10 13:14:09 +00:00
|
|
|
#include <ftypes/ftypes-int.h>
|
2010-08-30 09:31:52 +00:00
|
|
|
#include <epan/uat-int.h>
|
2007-07-30 23:32:47 +00:00
|
|
|
#include <epan/proto.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char* name;
|
|
|
|
gboolean usable;
|
|
|
|
char* repr;
|
|
|
|
} fvt_cache_entry_t;
|
2007-01-18 02:54:56 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
static uat_t* dfilter_macro_uat = NULL;
|
|
|
|
static dfilter_macro_t* macros = NULL;
|
|
|
|
static guint num_macros;
|
2007-07-30 23:32:47 +00:00
|
|
|
static GHashTable* fvt_cache = NULL;
|
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
/* #define DUMP_DFILTER_MACRO */
|
|
|
|
#ifdef DUMP_DFILTER_MACRO
|
|
|
|
void dump_dfilter_macro_t(const dfilter_macro_t *m, const char *function, const char *file, int line);
|
2014-07-16 00:23:26 +00:00
|
|
|
#define DUMP_MACRO(m) dump_dfilter_macro_t(m, G_STRFUNC, __FILE__, __LINE__)
|
2007-12-29 09:12:29 +00:00
|
|
|
#else
|
|
|
|
#define DUMP_MACRO(m)
|
|
|
|
#endif
|
|
|
|
|
2007-07-30 23:32:47 +00:00
|
|
|
static gboolean fvt_cache_cb(proto_node * node, gpointer data _U_) {
|
2009-08-09 17:57:31 +00:00
|
|
|
field_info* finfo = PNODE_FINFO(node);
|
2007-07-30 23:32:47 +00:00
|
|
|
fvt_cache_entry_t* e;
|
2007-10-16 15:58:25 +00:00
|
|
|
|
2007-07-30 23:32:47 +00:00
|
|
|
if (!finfo) return FALSE;
|
2007-10-16 15:58:25 +00:00
|
|
|
|
2012-06-02 15:52:42 +00:00
|
|
|
if ((e = (fvt_cache_entry_t*)g_hash_table_lookup(fvt_cache,finfo->hfinfo->abbrev))) {
|
2007-07-30 23:32:47 +00:00
|
|
|
e->usable = FALSE;
|
|
|
|
} else if (finfo->value.ftype->val_to_string_repr) {
|
|
|
|
switch (finfo->hfinfo->type) {
|
|
|
|
case FT_NONE:
|
|
|
|
case FT_PROTOCOL:
|
|
|
|
return FALSE;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-06-02 15:52:42 +00:00
|
|
|
e = g_new(fvt_cache_entry_t,1);
|
2007-07-30 23:32:47 +00:00
|
|
|
e->name = finfo->hfinfo->abbrev,
|
2016-05-10 20:04:14 +00:00
|
|
|
e->repr = fvalue_to_string_repr(NULL, &(finfo->value), FTREPR_DFILTER, finfo->hfinfo->display);
|
2007-07-30 23:32:47 +00:00
|
|
|
e->usable = TRUE;
|
|
|
|
g_hash_table_insert(fvt_cache,(void*)finfo->hfinfo->abbrev,e);
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-04-19 21:31:32 +00:00
|
|
|
static void dfilter_free_fvt_entry(gpointer v)
|
|
|
|
{
|
|
|
|
fvt_cache_entry_t* e = (fvt_cache_entry_t*)v;
|
|
|
|
wmem_free(NULL, e->repr);
|
|
|
|
g_free(e);
|
|
|
|
}
|
|
|
|
|
2007-07-30 23:32:47 +00:00
|
|
|
void dfilter_macro_build_ftv_cache(void* tree_root) {
|
2017-04-19 21:31:32 +00:00
|
|
|
g_hash_table_remove_all(fvt_cache);
|
2012-06-02 15:52:42 +00:00
|
|
|
proto_tree_traverse_post_order((proto_tree *)tree_root, fvt_cache_cb, NULL);
|
2007-07-30 23:32:47 +00:00
|
|
|
}
|
2007-01-18 02:54:56 +00:00
|
|
|
|
2015-01-17 04:11:49 +00:00
|
|
|
static gchar* dfilter_macro_resolve(gchar* name, gchar** args, gchar** error) {
|
2007-01-18 02:54:56 +00:00
|
|
|
GString* text;
|
|
|
|
int argc = 0;
|
2007-01-29 04:57:23 +00:00
|
|
|
dfilter_macro_t* m = NULL;
|
2007-07-30 23:32:47 +00:00
|
|
|
fvt_cache_entry_t* e;
|
2007-01-18 02:54:56 +00:00
|
|
|
int* arg_pos_p;
|
|
|
|
gchar** parts;
|
2015-01-17 04:11:49 +00:00
|
|
|
gchar* ret;
|
2007-01-29 04:57:23 +00:00
|
|
|
guint i;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
for (i = 0; i < num_macros; i++) {
|
|
|
|
dfilter_macro_t* c = &(macros[i]);
|
|
|
|
if ( c->usable && g_str_equal(c->name,name) ) {
|
|
|
|
m = c;
|
|
|
|
break;
|
|
|
|
}
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
if (!m) {
|
2011-02-17 08:39:41 +00:00
|
|
|
if (fvt_cache &&
|
2012-06-02 15:52:42 +00:00
|
|
|
(e = (fvt_cache_entry_t *)g_hash_table_lookup(fvt_cache,name)) != NULL) {
|
2011-02-17 08:39:41 +00:00
|
|
|
if(e->usable) {
|
2015-01-17 04:11:49 +00:00
|
|
|
return wmem_strdup(NULL, e->repr);
|
2007-07-30 23:32:47 +00:00
|
|
|
} else {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup_printf("macro '%s' is unusable", name);
|
2007-07-30 23:32:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup_printf("macro '%s' does not exist", name);
|
2007-07-30 23:32:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
DUMP_MACRO(m);
|
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
if (args) {
|
|
|
|
while(args[argc]) argc++;
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
if (argc != m->argc) {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL) {
|
|
|
|
*error = g_strdup_printf("wrong number of arguments for macro '%s', expecting %d instead of %d",
|
|
|
|
name, m->argc, argc);
|
|
|
|
}
|
2007-01-18 02:54:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
arg_pos_p = m->args_pos;
|
|
|
|
parts = m->parts;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
text = g_string_new(*(parts++));
|
2007-05-03 16:11:18 +00:00
|
|
|
|
|
|
|
if (args) {
|
|
|
|
while (*parts) {
|
2008-05-11 18:33:49 +00:00
|
|
|
g_string_append_printf(text,"%s%s",
|
|
|
|
args[*(arg_pos_p++)],
|
|
|
|
*(parts++));
|
2007-05-03 16:11:18 +00:00
|
|
|
}
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2015-01-17 04:11:49 +00:00
|
|
|
ret = wmem_strdup(NULL, text->str);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
g_string_free(text,TRUE);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
return ret;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-05 20:40:58 +00:00
|
|
|
static gchar* dfilter_macro_apply_recurse(const gchar* text, guint depth, gchar** error) {
|
2007-01-18 02:54:56 +00:00
|
|
|
enum { OUTSIDE, STARTING, NAME, ARGS } state = OUTSIDE;
|
|
|
|
GString* out;
|
|
|
|
GString* name = NULL;
|
|
|
|
GString* arg = NULL;
|
|
|
|
GPtrArray* args = NULL;
|
|
|
|
gchar c;
|
|
|
|
const gchar* r = text;
|
|
|
|
gboolean changed = FALSE;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
if ( depth > 31) {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup("too much nesting in macros");
|
2007-01-18 02:54:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
#define FGS(n) if (n) g_string_free(n,TRUE); n = NULL
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
#define FREE_ALL() \
|
|
|
|
do { \
|
|
|
|
FGS(name); \
|
|
|
|
FGS(arg); \
|
|
|
|
if (args) { \
|
|
|
|
while(args->len) { void* p = g_ptr_array_remove_index_fast(args,0); if (p) g_free(p); } \
|
|
|
|
g_ptr_array_free(args,TRUE); \
|
2011-11-15 04:26:38 +00:00
|
|
|
args = NULL; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = NULL;
|
2011-11-15 04:26:38 +00:00
|
|
|
out = g_string_sized_new(64);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
c = *r++;
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case OUTSIDE: {
|
|
|
|
switch(c) {
|
|
|
|
case '\0': {
|
|
|
|
goto finish;
|
|
|
|
} case '$': {
|
|
|
|
state = STARTING;
|
|
|
|
break;
|
|
|
|
} default: {
|
|
|
|
g_string_append_c(out,c);
|
|
|
|
break;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case STARTING: {
|
|
|
|
switch (c) {
|
|
|
|
case '{': {
|
|
|
|
args = g_ptr_array_new();
|
|
|
|
arg = g_string_sized_new(32);
|
|
|
|
name = g_string_sized_new(32);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
state = NAME;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
break;
|
|
|
|
} case '\0': {
|
|
|
|
g_string_append_c(out,'$');
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
goto finish;
|
|
|
|
} default: {
|
|
|
|
g_string_append_c(out,'$');
|
|
|
|
g_string_append_c(out,c);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
state = OUTSIDE;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
break;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case NAME: {
|
2014-10-16 23:41:59 +00:00
|
|
|
if ( g_ascii_isalnum(c) || c == '_' || c == '-' || c == '.' ) {
|
2011-11-15 04:26:38 +00:00
|
|
|
g_string_append_c(name,c);
|
|
|
|
} else if ( c == ':') {
|
|
|
|
state = ARGS;
|
|
|
|
} else if ( c == '}') {
|
2015-01-17 04:11:49 +00:00
|
|
|
gchar* resolved;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
g_ptr_array_add(args,NULL);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
|
2015-01-18 10:22:19 +00:00
|
|
|
if (resolved == NULL)
|
|
|
|
goto on_error;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
changed = TRUE;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
g_string_append(out,resolved);
|
2015-01-17 04:11:49 +00:00
|
|
|
wmem_free(NULL, resolved);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
FREE_ALL();
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
state = OUTSIDE;
|
|
|
|
} else if ( c == '\0') {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup("end of filter in the middle of a macro expression");
|
2011-11-15 04:26:38 +00:00
|
|
|
goto on_error;
|
|
|
|
} else {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup("invalid character in macro name");
|
2011-11-15 04:26:38 +00:00
|
|
|
goto on_error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} case ARGS: {
|
|
|
|
switch(c) {
|
|
|
|
case '\0': {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup("end of filter in the middle of a macro expression");
|
2007-05-03 16:11:18 +00:00
|
|
|
goto on_error;
|
2011-11-15 04:26:38 +00:00
|
|
|
} case ';': {
|
2015-01-13 23:13:40 +00:00
|
|
|
g_ptr_array_add(args,g_string_free(arg,FALSE));
|
2011-11-15 04:26:38 +00:00
|
|
|
|
|
|
|
arg = g_string_sized_new(32);
|
|
|
|
break;
|
|
|
|
} case '\\': {
|
|
|
|
c = *r++;
|
|
|
|
if (c) {
|
2007-01-18 02:54:56 +00:00
|
|
|
g_string_append_c(arg,c);
|
|
|
|
break;
|
2011-11-15 04:26:38 +00:00
|
|
|
} else {
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL)
|
|
|
|
*error = g_strdup("end of filter in the middle of a macro expression");
|
2011-11-15 04:26:38 +00:00
|
|
|
goto on_error;
|
|
|
|
}
|
|
|
|
} default: {
|
|
|
|
g_string_append_c(arg,c);
|
|
|
|
break;
|
|
|
|
} case '}': {
|
2015-01-17 04:11:49 +00:00
|
|
|
gchar* resolved;
|
2015-01-13 23:13:40 +00:00
|
|
|
g_ptr_array_add(args,g_string_free(arg,FALSE));
|
2011-11-15 04:26:38 +00:00
|
|
|
g_ptr_array_add(args,NULL);
|
2007-01-18 02:54:56 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
arg = NULL;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
resolved = dfilter_macro_resolve(name->str, (gchar**)args->pdata, error);
|
2015-01-18 10:22:19 +00:00
|
|
|
if (resolved == NULL)
|
|
|
|
goto on_error;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
changed = TRUE;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
g_string_append(out,resolved);
|
2015-01-17 04:11:49 +00:00
|
|
|
wmem_free(NULL, resolved);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
FREE_ALL();
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
state = OUTSIDE;
|
|
|
|
break;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
break;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-18 02:54:56 +00:00
|
|
|
finish:
|
2011-11-15 04:26:38 +00:00
|
|
|
{
|
|
|
|
FREE_ALL();
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2011-11-15 04:26:38 +00:00
|
|
|
if (changed) {
|
2015-12-05 20:40:58 +00:00
|
|
|
gchar* resolved = dfilter_macro_apply_recurse(out->str, depth + 1, error);
|
2007-01-18 02:54:56 +00:00
|
|
|
g_string_free(out,TRUE);
|
2015-01-18 10:22:19 +00:00
|
|
|
return resolved;
|
2011-11-15 04:26:38 +00:00
|
|
|
} else {
|
2015-12-05 20:40:58 +00:00
|
|
|
gchar* out_str = wmem_strdup(NULL, out->str);
|
2011-11-15 04:26:38 +00:00
|
|
|
g_string_free(out,TRUE);
|
|
|
|
return out_str;
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
}
|
|
|
|
on_error:
|
|
|
|
{
|
|
|
|
FREE_ALL();
|
2015-01-18 10:22:19 +00:00
|
|
|
if (error != NULL) {
|
|
|
|
if (*error == NULL)
|
|
|
|
*error = g_strdup("unknown error in macro expression");
|
|
|
|
}
|
2011-11-15 04:26:38 +00:00
|
|
|
g_string_free(out,TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 20:40:58 +00:00
|
|
|
gchar* dfilter_macro_apply(const gchar* text, gchar** error) {
|
2011-11-15 04:26:38 +00:00
|
|
|
return dfilter_macro_apply_recurse(text, 0, error);
|
2007-01-18 02:54:56 +00:00
|
|
|
}
|
2007-01-29 04:57:23 +00:00
|
|
|
|
2015-02-18 00:40:28 +00:00
|
|
|
static gboolean macro_update(void* mp, gchar** error) {
|
2012-06-02 15:52:42 +00:00
|
|
|
dfilter_macro_t* m = (dfilter_macro_t*)mp;
|
2007-01-29 04:57:23 +00:00
|
|
|
GPtrArray* parts;
|
|
|
|
GArray* args_pos;
|
|
|
|
const gchar* r;
|
|
|
|
gchar* w;
|
|
|
|
gchar* part;
|
|
|
|
int argc = 0;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
DUMP_MACRO(m);
|
|
|
|
|
2015-02-17 22:56:31 +00:00
|
|
|
*error = NULL;
|
|
|
|
|
2010-08-30 08:47:26 +00:00
|
|
|
/* Invalidate the display filter in case it's in use */
|
2010-08-30 09:31:52 +00:00
|
|
|
if (dfilter_macro_uat && dfilter_macro_uat->post_update_cb)
|
|
|
|
dfilter_macro_uat->post_update_cb();
|
2010-08-30 08:47:26 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
parts = g_ptr_array_new();
|
|
|
|
args_pos = g_array_new(FALSE,FALSE,sizeof(int));
|
|
|
|
|
|
|
|
m->priv = part = w = g_strdup(m->text);
|
|
|
|
r = m->text;
|
|
|
|
g_ptr_array_add(parts,part);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
|
|
|
while (r && *r) {
|
|
|
|
|
|
|
|
switch (*r) {
|
2007-01-29 04:57:23 +00:00
|
|
|
default:
|
|
|
|
*(w++) = *(r++);
|
|
|
|
break;
|
|
|
|
case '\0':
|
2011-11-15 04:26:38 +00:00
|
|
|
*w = *r;
|
2007-01-29 04:57:23 +00:00
|
|
|
goto done;
|
|
|
|
case '\\':
|
|
|
|
*(w++) = *(++r);
|
|
|
|
r++;
|
|
|
|
break;
|
|
|
|
case '$': {
|
|
|
|
int cnt = 0;
|
|
|
|
int arg_pos = 0;
|
|
|
|
do {
|
|
|
|
char c = *(r+1);
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
cnt++;
|
|
|
|
r++;
|
2007-05-16 17:35:07 +00:00
|
|
|
*(w++) = '\0';
|
2007-01-29 04:57:23 +00:00
|
|
|
arg_pos *= 10;
|
|
|
|
arg_pos += c - '0';
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
} while(*r);
|
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
if (cnt) {
|
|
|
|
*(w++) = '\0';
|
|
|
|
r++;
|
|
|
|
argc = argc < arg_pos ? arg_pos : argc;
|
|
|
|
arg_pos--;
|
|
|
|
g_array_append_val(args_pos,arg_pos);
|
|
|
|
g_ptr_array_add(parts,w);
|
|
|
|
} else {
|
|
|
|
*(w++) = *(r++);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
|
|
|
}
|
2007-01-29 04:57:23 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
g_ptr_array_add(parts,NULL);
|
|
|
|
|
2009-03-13 22:06:48 +00:00
|
|
|
g_free(m->parts);
|
2007-01-29 04:57:23 +00:00
|
|
|
m->parts = (gchar**)parts->pdata;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2009-03-13 22:06:48 +00:00
|
|
|
g_free(m->args_pos);
|
2007-04-24 06:43:01 +00:00
|
|
|
m->args_pos = (int*)(void *)args_pos->data;
|
2007-01-29 04:57:23 +00:00
|
|
|
|
|
|
|
g_ptr_array_free(parts,FALSE);
|
|
|
|
g_array_free(args_pos,FALSE);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
m->argc = argc;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
m->usable = TRUE;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
DUMP_MACRO(m);
|
|
|
|
|
2015-02-18 00:40:28 +00:00
|
|
|
return TRUE;
|
2007-01-29 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void macro_free(void* r) {
|
2012-06-02 15:52:42 +00:00
|
|
|
dfilter_macro_t* m = (dfilter_macro_t*)r;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
DUMP_MACRO(r);
|
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
g_free(m->name);
|
|
|
|
g_free(m->text);
|
|
|
|
g_free(m->priv);
|
|
|
|
g_free(m->parts);
|
|
|
|
g_free(m->args_pos);
|
|
|
|
}
|
|
|
|
|
2010-10-29 21:11:33 +00:00
|
|
|
static void* macro_copy(void* dest, const void* orig, size_t len _U_) {
|
2012-06-02 15:52:42 +00:00
|
|
|
dfilter_macro_t* d = (dfilter_macro_t*)dest;
|
2012-06-03 09:26:15 +00:00
|
|
|
const dfilter_macro_t* m = (const dfilter_macro_t*)orig;
|
2007-12-29 09:12:29 +00:00
|
|
|
|
|
|
|
DUMP_MACRO(m);
|
2007-01-29 04:57:23 +00:00
|
|
|
|
|
|
|
d->name = g_strdup(m->name);
|
|
|
|
d->text = g_strdup(m->text);
|
|
|
|
d->usable = m->usable;
|
|
|
|
|
|
|
|
if (m->parts) {
|
2007-12-29 09:12:29 +00:00
|
|
|
guint nparts = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the contents of m->priv (a "cooked" version
|
|
|
|
* of m->text) into d->priv.
|
2008-05-22 15:46:27 +00:00
|
|
|
*
|
2007-12-29 09:12:29 +00:00
|
|
|
* First we clone m->text into d->priv, this gets
|
|
|
|
* us a NUL terminated string of the proper length.
|
|
|
|
*
|
2008-05-22 15:46:27 +00:00
|
|
|
* Then we loop copying bytes from m->priv into
|
|
|
|
* d-priv. Since m->priv contains internal ACSII NULs
|
2007-12-29 09:12:29 +00:00
|
|
|
* we use the length of m->text to stop the copy.
|
2015-02-13 19:02:43 +00:00
|
|
|
*/
|
2007-12-29 09:12:29 +00:00
|
|
|
|
|
|
|
d->priv = g_strdup(m->text);
|
|
|
|
{
|
|
|
|
const gchar* oldText = m->text;
|
2012-06-02 15:52:42 +00:00
|
|
|
const gchar* oldPriv = (const gchar*)m->priv;
|
|
|
|
gchar* newPriv = (gchar*)d->priv;
|
2007-12-29 09:12:29 +00:00
|
|
|
while(oldText && *oldText) {
|
|
|
|
*(newPriv++) = *(oldPriv++);
|
2008-05-22 15:46:27 +00:00
|
|
|
oldText++;
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The contents of the m->parts array contains pointers
|
|
|
|
* into various sections of m->priv. Since it's
|
|
|
|
* an argv style array of ponters, this array is
|
|
|
|
* actually one larger than the number of parts
|
|
|
|
* to hold the final NULL terminator.
|
|
|
|
*
|
|
|
|
* The following copy clones the original m->parts
|
2008-05-22 15:46:27 +00:00
|
|
|
* array into d->parts but then fixes-up the pointers
|
|
|
|
* so that they point into the appropriate sections
|
2007-12-29 09:12:29 +00:00
|
|
|
* of the d->priv.
|
2015-02-13 19:02:43 +00:00
|
|
|
*/
|
2007-12-29 09:12:29 +00:00
|
|
|
|
2007-05-03 16:11:18 +00:00
|
|
|
do nparts++; while (m->parts[nparts]);
|
2012-06-02 15:52:42 +00:00
|
|
|
d->parts = (gchar **)g_memdup(m->parts,(nparts+1)*(guint)sizeof(void*));
|
2007-12-29 09:12:29 +00:00
|
|
|
nparts = 0;
|
|
|
|
while(m->parts[nparts]) {
|
|
|
|
if(nparts) {
|
|
|
|
d->parts[nparts] = d->parts[nparts - 1] + (m->parts[nparts] - m->parts[nparts - 1]);
|
|
|
|
} else {
|
2012-06-02 15:52:42 +00:00
|
|
|
d->parts[nparts] = (gchar *)d->priv;
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
nparts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clone the contents of m->args_pos into d->args_pos.
|
|
|
|
*/
|
|
|
|
|
2012-06-02 15:52:42 +00:00
|
|
|
d->args_pos = (int *)g_memdup(m->args_pos,(--nparts)*(guint)sizeof(int));
|
2007-01-29 04:57:23 +00:00
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
DUMP_MACRO(d);
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2015-02-16 03:13:25 +00:00
|
|
|
static gboolean macro_name_chk(void *mp, const char *in_name, guint name_len,
|
|
|
|
const void *u1 _U_, const void *u2 _U_, char **error) {
|
|
|
|
dfilter_macro_t* m = (dfilter_macro_t*)mp;
|
2007-01-29 04:57:23 +00:00
|
|
|
guint i;
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-09-02 12:37:24 +00:00
|
|
|
if (name_len == 0) {
|
2015-01-10 00:50:09 +00:00
|
|
|
*error = g_strdup("invalid name");
|
2007-09-02 12:37:24 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-01-29 04:57:23 +00:00
|
|
|
for (i=0; i < name_len; i++) {
|
2014-10-16 23:41:59 +00:00
|
|
|
if (!(in_name[i] == '_' || g_ascii_isalnum(in_name[i]) ) ) {
|
2015-01-10 00:50:09 +00:00
|
|
|
*error = g_strdup("invalid char in name");
|
2007-01-29 04:57:23 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2015-02-16 03:13:25 +00:00
|
|
|
/* When loading (!m->name) or when adding/changing the an item with a
|
|
|
|
* different name, check for uniqueness. NOTE: if a duplicate already
|
|
|
|
* exists (because the user manually edited the file), then this will
|
|
|
|
* not trigger a warning. */
|
2015-08-22 00:12:39 +00:00
|
|
|
if (!m->name || g_strcmp0(m->name, in_name)) {
|
2015-02-16 03:13:25 +00:00
|
|
|
for (i = 0; i < num_macros; i++) {
|
|
|
|
/* This a string field which is always NUL-terminated,
|
|
|
|
* so no need to check name_len. */
|
2015-08-22 00:12:39 +00:00
|
|
|
if (!g_strcmp0(in_name, macros[i].name)) {
|
2015-02-16 03:13:25 +00:00
|
|
|
*error = g_strdup_printf("macro '%s' already exists",
|
|
|
|
in_name);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-02 12:37:24 +00:00
|
|
|
return TRUE;
|
2007-01-29 04:57:23 +00:00
|
|
|
}
|
|
|
|
|
2007-02-03 14:25:02 +00:00
|
|
|
UAT_CSTRING_CB_DEF(macro,name,dfilter_macro_t)
|
|
|
|
UAT_CSTRING_CB_DEF(macro,text,dfilter_macro_t)
|
2007-01-29 04:57:23 +00:00
|
|
|
|
|
|
|
void dfilter_macro_init(void) {
|
2007-02-03 14:25:02 +00:00
|
|
|
static uat_field_t uat_fields[] = {
|
2009-02-16 04:10:06 +00:00
|
|
|
UAT_FLD_CSTRING_OTHER(macro,name,"Name",macro_name_chk,"The name of the macro."),
|
2013-06-19 17:11:55 +00:00
|
|
|
/* N.B. it would be nice if there was a field type for display filters (with
|
|
|
|
auto-completion & colouring), but this wouldn't work here as the filter string
|
|
|
|
will contain $1, etc... */
|
2009-02-16 04:10:06 +00:00
|
|
|
UAT_FLD_CSTRING_ISPRINT(macro,text,"Text","The text this macro resolves to."),
|
2007-02-03 14:25:02 +00:00
|
|
|
UAT_END_FIELDS
|
|
|
|
};
|
2007-05-03 16:11:18 +00:00
|
|
|
|
2007-02-12 19:57:41 +00:00
|
|
|
dfilter_macro_uat = uat_new("Display Filter Macros",
|
2007-04-03 21:17:13 +00:00
|
|
|
sizeof(dfilter_macro_t),
|
|
|
|
DFILTER_MACRO_FILENAME,
|
2008-03-06 22:13:24 +00:00
|
|
|
TRUE,
|
2014-01-30 17:43:52 +00:00
|
|
|
¯os,
|
2007-04-03 21:17:13 +00:00
|
|
|
&num_macros,
|
UATs could be put into "categories". The categories were defined only
implicitly by the #define name and string they were defined to; not all
UATs neatly fit into any of the categories, so some of them were put
into categories that weren't obviously correct for them, and one - the
display filter macro UAT - wasn't put into any category at all (which
caused crashes when editing them, as the GUI code that handled UAT
changes from a dialog assumed the category field was non-null).
The category was, in practice, used only to decide, in the
aforementioned GUI code, whether the packet summary pane needed to be
updated or not. It also offered no option of "don't update the packet
summary pane *and* don't redissect anything", which is what would be
appropriate for the display filter macro UAT.
Replace the category with a set of fields indicating what the UAT
affects; we currently offer "dissection", which applies to most UATs
(any UAT in libwireshark presumably affects dissection at a minimum) and
"the set of named fields that exist". Changing any UAT that affects
dissection requires a redissection; changing any UAT that affects the
set of named fields that exist requires a redissection *and* rebuilding
the packet summary pane.
Perhaps we also need "filtering", so that if you change a display filter
macro, we re-filter, in case the display is currently filtered with a
display filter that uses a macro that changed.
svn path=/trunk/; revision=43603
2012-07-08 01:00:46 +00:00
|
|
|
0, /* doesn't affect anything that requires a GUI update */
|
2007-04-03 21:17:13 +00:00
|
|
|
"ChDisplayFilterMacrosSection",
|
|
|
|
macro_copy,
|
|
|
|
macro_update,
|
|
|
|
macro_free,
|
2010-08-30 08:47:26 +00:00
|
|
|
NULL, /* Note: This is set in macros_init () */
|
2016-12-21 10:50:47 +00:00
|
|
|
NULL,
|
2007-04-03 21:17:13 +00:00
|
|
|
uat_fields);
|
2007-07-30 23:32:47 +00:00
|
|
|
|
2017-04-19 21:31:32 +00:00
|
|
|
fvt_cache = g_hash_table_new_full(g_str_hash,g_str_equal, NULL, dfilter_free_fvt_entry);
|
2007-01-29 04:57:23 +00:00
|
|
|
}
|
2007-01-29 10:23:38 +00:00
|
|
|
|
2015-08-22 00:12:39 +00:00
|
|
|
void dfilter_macro_get_uat(uat_t **dfmu_ptr_ptr) {
|
|
|
|
*dfmu_ptr_ptr = dfilter_macro_uat;
|
2007-01-29 10:23:38 +00:00
|
|
|
}
|
2007-07-30 23:32:47 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
#ifdef DUMP_DFILTER_MACRO
|
|
|
|
/*
|
|
|
|
* The dfilter_macro_t has several characteristics that are
|
2013-06-19 17:11:55 +00:00
|
|
|
* not immediately obvious. The dump_dfilter_filter_macro_t()
|
2008-05-22 15:46:27 +00:00
|
|
|
* function can be used to help "visualize" the contents of
|
2007-12-29 09:12:29 +00:00
|
|
|
* a dfilter_macro_t.
|
|
|
|
*
|
|
|
|
* Some non-obvious components of this struct include:
|
|
|
|
*
|
|
|
|
* m->parts is an argv style array of pointers into the
|
2008-05-22 15:46:27 +00:00
|
|
|
* m->priv string.
|
2007-12-29 09:12:29 +00:00
|
|
|
*
|
|
|
|
* The last pointer of an m->parts array should contain
|
|
|
|
* NULL to indicate the end of the parts pointer array.
|
|
|
|
*
|
|
|
|
* m->priv is a "cooked" copy of the m->text string.
|
2008-05-22 15:46:27 +00:00
|
|
|
* Any variable substitution indicators within m->text
|
|
|
|
* ("$1", "$2", ...) will have been replaced with ASCII
|
2007-12-29 09:12:29 +00:00
|
|
|
* NUL characters within m->priv.
|
|
|
|
*
|
2008-05-22 15:46:27 +00:00
|
|
|
* The first element of m->parts array (m-parts[0]) will
|
|
|
|
* usually have the same pointer value as m->priv (unless
|
|
|
|
* the dfilter-macro starts off with a variable
|
2007-12-29 09:12:29 +00:00
|
|
|
* substitution indicator (e.g. "$1").
|
|
|
|
*/
|
|
|
|
|
|
|
|
void dump_dfilter_macro_t(const dfilter_macro_t *m, const char *function, const char *file, int line)
|
|
|
|
{
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf("\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
|
|
|
|
if(m == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" dfilter_macro_t * == NULL! (via: %s(): %s:%d)\n", function, file, line);
|
|
|
|
ws_debug_printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf("DUMP of dfilter_macro_t: %p (via: %s(): %s:%d)\n", m, function, file, line);
|
2007-12-29 09:12:29 +00:00
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->name == %p\n", &m->name);
|
2007-12-29 09:12:29 +00:00
|
|
|
if(m->name == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->name == NULL\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
} else {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->name == %p\n", m->name);
|
|
|
|
ws_debug_printf(" ->name == <%s>\n", m->name);
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->text == %p\n", &m->text);
|
2007-12-29 09:12:29 +00:00
|
|
|
if(m->text == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->text == NULL\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
} else {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->text == %p\n", m->text);
|
|
|
|
ws_debug_printf(" ->text == <%s>\n", m->text);
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->usable == %p\n", &m->usable);
|
|
|
|
ws_debug_printf(" ->usable == %u\n", m->usable);
|
2007-07-30 23:32:47 +00:00
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->parts == %p\n", &m->parts);
|
2007-07-30 23:32:47 +00:00
|
|
|
|
2007-12-29 09:12:29 +00:00
|
|
|
if(m->parts == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->parts == NULL\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
} else {
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (m->parts[i]) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->parts[%d] == %p\n", i, m->parts[i]);
|
|
|
|
ws_debug_printf(" ->parts[%d] == <%s>\n", i, m->parts[i]);
|
2007-12-29 09:12:29 +00:00
|
|
|
i++;
|
|
|
|
}
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->parts[%d] == NULL\n", i);
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->args_pos == %p\n", &m->args_pos);
|
2007-12-29 09:12:29 +00:00
|
|
|
if(m->args_pos == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->args_pos == NULL\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
} else {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->args_pos == %p\n", m->args_pos);
|
|
|
|
/*ws_debug_printf(" ->args_pos == <%?>\n", m->args_pos);*/
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->argc == %p\n", &m->argc);
|
|
|
|
ws_debug_printf(" ->argc == %d\n", m->argc);
|
2007-12-29 09:12:29 +00:00
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" &dfilter_macro->priv == %p\n", &m->priv);
|
2007-12-29 09:12:29 +00:00
|
|
|
if(m->priv == NULL) {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->priv == NULL\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
} else {
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf(" ->priv == %p\n", m->priv);
|
|
|
|
ws_debug_printf(" ->priv == <%s>\n", (char *)m->priv);
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
|
2016-07-24 12:53:39 +00:00
|
|
|
ws_debug_printf("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
|
2007-12-29 09:12:29 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-10-16 15:58:25 +00:00
|
|
|
|
2017-02-02 15:43:22 +00:00
|
|
|
void dfilter_macro_cleanup(void)
|
|
|
|
{
|
|
|
|
g_hash_table_destroy(fvt_cache);
|
|
|
|
}
|
|
|
|
|
2015-02-13 19:02:43 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=8:tabSize=8:noTabs=false:
|
|
|
|
*/
|