3 * Routines for extcap external capture
4 * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "extcap_parser.h"
34 void extcap_printf_complex(extcap_complex *comp) {
35 gchar *ret = extcap_get_complex_as_string(comp);
40 gchar *extcap_get_complex_as_string(extcap_complex *comp) {
41 return (comp ? g_strdup(comp->_val) : NULL);
44 extcap_complex *extcap_parse_complex(extcap_arg_type complex_type,
47 extcap_complex *rc = g_new0(extcap_complex, 1);
49 rc->_val = g_strdup(data);
50 rc->complex_type = complex_type;
55 gboolean extcap_compare_is_default(extcap_arg *element, extcap_complex *test) {
56 if ( element == NULL || element->default_complex == NULL || test == NULL )
59 if ( g_strcmp0(element->default_complex->_val, test->_val) == 0 )
65 void extcap_free_complex(extcap_complex *comp) {
71 gint extcap_complex_get_int(extcap_complex *comp) {
72 if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_INTEGER )
75 return (gint) g_ascii_strtoll(comp->_val, NULL, 10);
78 guint extcap_complex_get_uint(extcap_complex *comp) {
79 if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_UNSIGNED )
81 return (guint) g_ascii_strtoull(comp->_val, NULL, 10);
84 gint64 extcap_complex_get_long(extcap_complex *comp) {
85 if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_LONG )
87 return g_ascii_strtoll( comp->_val, NULL, 10 );
90 gdouble extcap_complex_get_double(extcap_complex *comp) {
91 if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_DOUBLE )
93 return g_strtod( comp->_val, NULL );
96 gboolean extcap_complex_get_bool(extcap_complex *comp) {
97 if ( comp == NULL || comp->_val == NULL )
100 if ( comp->complex_type != EXTCAP_ARG_BOOLEAN && comp->complex_type != EXTCAP_ARG_BOOLFLAG )
103 return g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, comp->_val, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
106 gchar *extcap_complex_get_string(extcap_complex *comp) {
107 /* Not checking for argument type, to use this method as fallback if only strings are needed */
108 return comp != NULL ? comp->_val : NULL;
111 static extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
112 GRegex * regex = NULL;
113 GMatchInfo * match_info = NULL;
114 GError * error = NULL;
115 gchar * param_value = NULL;
116 guint param_type = EXTCAP_PARAM_UNKNOWN;
118 extcap_token_sentence *rs = g_new0(extcap_token_sentence, 1);
122 /* Regex for catching just the allowed values for sentences */
123 if ( ( regex = g_regex_new ( "^[\\t| ]*(arg|value|interface|extcap|dlt)(?=[\\t| ]+\\{)",
124 (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL ) ) != NULL ) {
125 g_regex_match ( regex, s, (GRegexMatchFlags) 0, &match_info );
127 if ( g_match_info_matches ( match_info ) )
128 rs->sentence = g_match_info_fetch(match_info, 0);
130 g_match_info_free ( match_info );
131 g_regex_unref ( regex );
133 /* No valid sentence found, exiting here */
134 if ( rs->sentence == NULL ) {
139 rs->param_list = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
141 /* Capture the argument and the value of the list. This will ensure,
142 * that regex patterns given to {validation=} are parsed correctly,
143 * as long as }{ does not occur within the pattern */
144 regex = g_regex_new ( "\\{([a-zA-Z_-]*?)\\=(.*?)\\}(?=\\{|$|\\s)",
145 (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL );
146 if ( regex != NULL ) {
147 g_regex_match_full(regex, s, -1, 0, (GRegexMatchFlags) 0, &match_info, &error );
148 while(g_match_info_matches(match_info)) {
149 gchar * arg = g_match_info_fetch ( match_info, 1 );
154 param_value = g_match_info_fetch ( match_info, 2 );
156 if (g_ascii_strcasecmp(arg, "number") == 0) {
157 param_type = EXTCAP_PARAM_ARGNUM;
158 } else if (g_ascii_strcasecmp(arg, "call") == 0) {
159 param_type = EXTCAP_PARAM_CALL;
160 } else if (g_ascii_strcasecmp(arg, "display") == 0) {
161 param_type = EXTCAP_PARAM_DISPLAY;
162 } else if (g_ascii_strcasecmp(arg, "type") == 0) {
163 param_type = EXTCAP_PARAM_TYPE;
164 } else if (g_ascii_strcasecmp(arg, "arg") == 0) {
165 param_type = EXTCAP_PARAM_ARG;
166 } else if (g_ascii_strcasecmp(arg, "default") == 0) {
167 param_type = EXTCAP_PARAM_DEFAULT;
168 } else if (g_ascii_strcasecmp(arg, "value") == 0) {
169 param_type = EXTCAP_PARAM_VALUE;
170 } else if (g_ascii_strcasecmp(arg, "range") == 0) {
171 param_type = EXTCAP_PARAM_RANGE;
172 } else if (g_ascii_strcasecmp(arg, "tooltip") == 0) {
173 param_type = EXTCAP_PARAM_TOOLTIP;
174 } else if (g_ascii_strcasecmp(arg, "mustexist") == 0) {
175 param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
176 } else if (g_ascii_strcasecmp(arg, "fileext") == 0) {
177 param_type = EXTCAP_PARAM_FILE_EXTENSION;
178 } else if (g_ascii_strcasecmp(arg, "name") == 0) {
179 param_type = EXTCAP_PARAM_NAME;
180 } else if (g_ascii_strcasecmp(arg, "enabled") == 0) {
181 param_type = EXTCAP_PARAM_ENABLED;
182 } else if (g_ascii_strcasecmp(arg, "parent") == 0) {
183 param_type = EXTCAP_PARAM_PARENT;
184 } else if (g_ascii_strcasecmp(arg, "required") == 0) {
185 param_type = EXTCAP_PARAM_REQUIRED;
186 } else if (g_ascii_strcasecmp(arg, "save") == 0) {
187 param_type = EXTCAP_PARAM_SAVE;
188 } else if (g_ascii_strcasecmp(arg, "validation") == 0) {
189 param_type = EXTCAP_PARAM_VALIDATION;
190 } else if (g_ascii_strcasecmp(arg, "version") == 0) {
191 param_type = EXTCAP_PARAM_VERSION;
192 } else if (g_ascii_strcasecmp(arg, "help") == 0) {
193 param_type = EXTCAP_PARAM_HELP;
195 param_type = EXTCAP_PARAM_UNKNOWN;
198 g_hash_table_insert(rs->param_list, ENUM_KEY(param_type), param_value);
200 g_match_info_next(match_info, &error);
203 g_match_info_free(match_info);
204 g_regex_unref(regex);
210 static GList *extcap_tokenize_sentences(const gchar *s) {
212 GList * sentences = NULL;
213 extcap_token_sentence *item = NULL;
214 gchar **list, **list_iter;
216 list_iter = list = g_strsplit(s, "\n", 0);
217 while ( *list_iter != NULL ) {
218 item = extcap_tokenize_sentence(*list_iter);
220 sentences = g_list_append(sentences, item);
229 static void extcap_free_value(extcap_value *v) {
239 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
240 extcap_free_value((extcap_value *) data);
243 void extcap_free_arg(extcap_arg *a) {
251 g_free(a->fileextension);
253 g_free(a->device_name);
255 if (a->range_start != NULL)
256 extcap_free_complex(a->range_start);
258 if (a->range_end != NULL)
259 extcap_free_complex(a->range_end);
261 if (a->default_complex != NULL)
262 extcap_free_complex(a->default_complex);
264 g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
265 g_list_free(a->values);
269 void extcap_free_arg_list(GList *a) {
270 g_list_foreach(a, (GFunc)extcap_free_arg, NULL);
274 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
275 if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
280 static void extcap_free_tokenized_sentence(gpointer s, gpointer user_data _U_) {
281 extcap_token_sentence *t = (extcap_token_sentence *)s;
287 g_hash_table_destroy(t->param_list);
291 static void extcap_free_tokenized_sentences(GList *sentences) {
292 if (sentences == NULL)
295 g_list_foreach(sentences, extcap_free_tokenized_sentence, NULL);
296 g_list_free(sentences);
299 static extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
300 gchar * param_value = NULL;
302 extcap_arg *target_arg = NULL;
303 extcap_value *value = NULL;
304 GList * entry = NULL;
306 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
311 if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
312 sent = EXTCAP_SENTENCE_ARG;
313 /* printf("ARG sentence\n"); */
314 } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
315 sent = EXTCAP_SENTENCE_VALUE;
316 /* printf("VALUE sentence\n"); */
319 if (sent == EXTCAP_SENTENCE_ARG) {
320 target_arg = g_new0(extcap_arg, 1);
321 target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
322 target_arg->save = TRUE;
325 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM))) == NULL) {
326 extcap_free_arg(target_arg);
330 if (sscanf(param_value, "%d", &(target_arg->arg_num)) != 1) {
331 extcap_free_arg(target_arg);
335 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CALL))) == NULL) {
336 extcap_free_arg(target_arg);
339 target_arg->call = g_strdup(param_value);
341 /* No value only parameters allowed */
342 if (strlen(target_arg->call) == 0) {
343 extcap_free_arg(target_arg);
347 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY))) == NULL) {
348 extcap_free_arg(target_arg);
351 target_arg->display = g_strdup(param_value);
353 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP)))
355 target_arg->tooltip = g_strdup(param_value);
358 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_MUSTEXIST)))
360 target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
363 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_EXTENSION)))
365 target_arg->fileextension = g_strdup(param_value);
368 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
370 target_arg->regexp = g_strdup(param_value);
373 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
375 target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
378 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE)))
380 /* printf("no type in ARG sentence\n"); */
381 extcap_free_arg(target_arg);
385 if (g_ascii_strcasecmp(param_value, "integer") == 0) {
386 target_arg->arg_type = EXTCAP_ARG_INTEGER;
387 } else if (g_ascii_strcasecmp(param_value, "unsigned") == 0) {
388 target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
389 } else if (g_ascii_strcasecmp(param_value, "long") == 0) {
390 target_arg->arg_type = EXTCAP_ARG_LONG;
391 } else if (g_ascii_strcasecmp(param_value, "double") == 0) {
392 target_arg->arg_type = EXTCAP_ARG_DOUBLE;
393 } else if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
394 target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
395 } else if (g_ascii_strcasecmp(param_value, "boolflag") == 0) {
396 target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
397 } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
398 target_arg->arg_type = EXTCAP_ARG_SELECTOR;
399 } else if (g_ascii_strcasecmp(param_value, "radio") == 0) {
400 target_arg->arg_type = EXTCAP_ARG_RADIO;
401 } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
402 target_arg->arg_type = EXTCAP_ARG_STRING;
403 } else if (g_ascii_strcasecmp(param_value, "password") == 0) {
404 target_arg->arg_type = EXTCAP_ARG_PASSWORD;
405 /* default setting is to not save passwords */
406 target_arg->save = FALSE;
407 } else if (g_ascii_strcasecmp(param_value, "fileselect") == 0) {
408 target_arg->arg_type = EXTCAP_ARG_FILESELECT;
409 } else if (g_ascii_strcasecmp(param_value, "multicheck") == 0) {
410 target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
412 printf("invalid type %s in ARG sentence\n", param_value);
413 extcap_free_arg(target_arg);
417 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_SAVE)))
419 target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
422 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_RANGE)))
424 gchar *cp = g_strstr_len(param_value, -1, ",");
427 printf("invalid range, expected value,value got %s\n",
429 extcap_free_arg(target_arg);
433 if ((target_arg->range_start = extcap_parse_complex(
434 target_arg->arg_type, param_value)) == NULL) {
435 printf("invalid range, expected value,value got %s\n",
437 extcap_free_arg(target_arg);
441 if ((target_arg->range_end = extcap_parse_complex(
442 target_arg->arg_type, cp + 1)) == NULL) {
443 printf("invalid range, expected value,value got %s\n",
445 extcap_free_arg(target_arg);
450 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
452 if ( target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR )
454 if ((target_arg->default_complex = extcap_parse_complex(
455 target_arg->arg_type, param_value)) == NULL) {
456 printf("invalid default, couldn't parse %s\n", param_value);
461 } else if (sent == EXTCAP_SENTENCE_VALUE) {
462 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARG)))
464 printf("no arg in VALUE sentence\n");
468 if (sscanf(param_value, "%d", &tint) != 1) {
469 printf("invalid arg in VALUE sentence\n");
474 if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
476 printf("couldn't find arg %d in list for VALUE sentence\n", tint);
480 value = g_new0(extcap_value, 1);
481 value->arg_num = tint;
483 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
485 /* printf("no value in VALUE sentence\n"); */
486 extcap_free_value(value);
489 value->call = g_strdup(param_value);
491 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
493 /* printf("no display in VALUE sentence\n"); */
494 extcap_free_value(value);
497 value->display = g_strdup(param_value);
499 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PARENT)))
501 value->parent = g_strdup(param_value);
504 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
506 /* printf("found default value\n"); */
507 value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
510 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ENABLED)))
512 value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
515 ((extcap_arg*) entry->data)->values = g_list_append(
516 ((extcap_arg*) entry->data)->values, value);
524 GList * extcap_parse_args(gchar *output) {
525 GList * result = NULL;
526 GList * walker = NULL;
529 walker = extcap_tokenize_sentences(output);
533 extcap_arg *ra = NULL;
534 extcap_token_sentence * sentence = (extcap_token_sentence *)walker->data;
536 if ((ra = extcap_parse_arg_sentence(result, sentence)) != NULL)
537 result = g_list_append(result, (gpointer) ra);
539 walker = g_list_next(walker);
542 extcap_free_tokenized_sentences(temp);
547 static extcap_interface * extcap_parse_interface_sentence(extcap_token_sentence *s) {
548 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
549 gchar * param_value = NULL;
550 extcap_interface * ri = NULL;
555 if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
556 sent = EXTCAP_SENTENCE_INTERFACE;
557 } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
558 sent = EXTCAP_SENTENCE_EXTCAP;
561 if (sent == EXTCAP_SENTENCE_UNKNOWN)
564 ri = g_new0(extcap_interface, 1);
568 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
569 == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
570 printf("No value in INTERFACE sentence\n");
574 ri->call = g_strdup(param_value);
576 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
577 == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
578 printf("No display in INTERFACE sentence\n");
583 ri->display = g_strdup(param_value);
585 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VERSION)))
587 ri->version = g_strdup(param_value);
590 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_HELP)))
592 ri->help = g_strdup(param_value);
598 GList * extcap_parse_interfaces(gchar *output) {
600 GList * result = NULL;
601 GList * tokens = NULL;
602 GList * walker = extcap_tokenize_sentences(output);
606 extcap_interface * ri = NULL;
607 extcap_token_sentence * if_sentence = (extcap_token_sentence *) walker->data;
609 if ( if_sentence != NULL && ( ri = extcap_parse_interface_sentence ( if_sentence ) ) != NULL )
610 result = g_list_append(result, ri);
612 walker = g_list_next(walker);
615 extcap_free_tokenized_sentences(tokens);
620 /* Parse a tokenized set of sentences and validate, looking for DLT definitions */
621 static extcap_dlt * extcap_parse_dlt_sentence(extcap_token_sentence *s) {
622 gchar *param_value = NULL;
623 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
624 extcap_dlt * result = NULL;
629 if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
630 sent = EXTCAP_SENTENCE_DLT;
633 if (sent == EXTCAP_SENTENCE_UNKNOWN)
636 result = g_new0(extcap_dlt, 1);
640 result->display = NULL;
642 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM)))
644 printf("No number in DLT sentence\n");
648 if (sscanf(param_value, "%d", &(result->number)) != 1) {
649 printf("Invalid number in DLT sentence\n");
654 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_NAME)))
656 printf("No name in DLT sentence\n");
660 result->name = g_strdup(param_value);
662 if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
664 printf("No display in DLT sentence\n");
665 g_free(result->name);
669 result->display = g_strdup(param_value);
674 GList * extcap_parse_dlts(gchar *output) {
676 GList * walker = NULL;
678 GList * result = NULL;
680 walker = extcap_tokenize_sentences(output);
685 extcap_dlt *data = NULL;
687 if ((data = extcap_parse_dlt_sentence((extcap_token_sentence *)walker->data)) != NULL)
688 result = g_list_append(result, data);
690 walker = g_list_next(walker);
693 extcap_free_tokenized_sentences(temp);
699 * Editor modelines - http://www.wireshark.org/tools/modelines.html
704 * indent-tabs-mode: nil
707 * vi: set shiftwidth=4 tabstop=8 expandtab:
708 * :indentSize=4:tabSize=8:noTabs=true: