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 void extcap_free_tokenized_param(extcap_token_param *v) {
121 void extcap_free_tokenized_sentence(extcap_token_sentence *s) {
122 extcap_token_param *tv;
127 if (s->sentence != NULL)
130 while (s->param_list != NULL ) {
132 s->param_list = tv->next_token;
134 extcap_free_tokenized_param(tv);
139 void extcap_free_tokenized_sentence_list(extcap_token_sentence *f) {
140 extcap_token_sentence *t;
143 t = f->next_sentence;
144 extcap_free_tokenized_sentence(f);
149 extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
150 extcap_token_param *tv = NULL;
151 GRegex * regex = NULL;
152 GMatchInfo * match_info = NULL;
153 GError * error = NULL;
155 extcap_token_sentence *rs = g_new(extcap_token_sentence, 1);
158 rs->next_sentence = NULL;
159 rs->param_list = NULL;
161 /* Regex for catching just the allowed values for sentences */
162 if ( ( regex = g_regex_new ( "^[\\t| ]*(arg|value|interface|extcap|dlt)(?=[\\t| ]+\\{)",
163 (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL ) ) != NULL ) {
164 g_regex_match ( regex, s, (GRegexMatchFlags) 0, &match_info );
166 if ( g_match_info_matches ( match_info ) )
167 rs->sentence = g_match_info_fetch(match_info, 0);
169 g_match_info_free ( match_info );
170 g_regex_unref ( regex );
172 /* No valid sentence found, exiting here */
173 if ( rs->sentence == NULL ) {
174 extcap_free_tokenized_sentence(rs);
178 /* Capture the argument and the value of the list. This will ensure,
179 * that regex patterns given to {validation=} are parsed correctly,
180 * as long as }{ does not occur within the pattern */
181 regex = g_regex_new ( "\\{([a-zA-Z_-]*?)\\=(.*?)\\}(?=\\{|$|\\s)",
182 (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL );
183 if ( regex != NULL ) {
184 g_regex_match_full(regex, s, -1, 0, (GRegexMatchFlags) 0, &match_info, &error );
185 while(g_match_info_matches(match_info)) {
186 gchar * arg = g_match_info_fetch ( match_info, 1 );
191 tv = g_new(extcap_token_param, 1);
193 tv->value = g_match_info_fetch ( match_info, 2 );
195 if (g_ascii_strcasecmp(tv->arg, "number") == 0) {
196 tv->param_type = EXTCAP_PARAM_ARGNUM;
197 } else if (g_ascii_strcasecmp(tv->arg, "call") == 0) {
198 tv->param_type = EXTCAP_PARAM_CALL;
199 } else if (g_ascii_strcasecmp(tv->arg, "display") == 0) {
200 tv->param_type = EXTCAP_PARAM_DISPLAY;
201 } else if (g_ascii_strcasecmp(tv->arg, "type") == 0) {
202 tv->param_type = EXTCAP_PARAM_TYPE;
203 } else if (g_ascii_strcasecmp(tv->arg, "arg") == 0) {
204 tv->param_type = EXTCAP_PARAM_ARG;
205 } else if (g_ascii_strcasecmp(tv->arg, "default") == 0) {
206 tv->param_type = EXTCAP_PARAM_DEFAULT;
207 } else if (g_ascii_strcasecmp(tv->arg, "value") == 0) {
208 tv->param_type = EXTCAP_PARAM_VALUE;
209 } else if (g_ascii_strcasecmp(tv->arg, "range") == 0) {
210 tv->param_type = EXTCAP_PARAM_RANGE;
211 } else if (g_ascii_strcasecmp(tv->arg, "tooltip") == 0) {
212 tv->param_type = EXTCAP_PARAM_TOOLTIP;
213 } else if (g_ascii_strcasecmp(tv->arg, "mustexist") == 0) {
214 tv->param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
215 } else if (g_ascii_strcasecmp(tv->arg, "fileext") == 0) {
216 tv->param_type = EXTCAP_PARAM_FILE_EXTENSION;
217 } else if (g_ascii_strcasecmp(tv->arg, "name") == 0) {
218 tv->param_type = EXTCAP_PARAM_NAME;
219 } else if (g_ascii_strcasecmp(tv->arg, "enabled") == 0) {
220 tv->param_type = EXTCAP_PARAM_ENABLED;
221 } else if (g_ascii_strcasecmp(tv->arg, "parent") == 0) {
222 tv->param_type = EXTCAP_PARAM_PARENT;
223 } else if (g_ascii_strcasecmp(tv->arg, "required") == 0) {
224 tv->param_type = EXTCAP_PARAM_REQUIRED;
225 } else if (g_ascii_strcasecmp(tv->arg, "save") == 0) {
226 tv->param_type = EXTCAP_PARAM_SAVE;
227 } else if (g_ascii_strcasecmp(tv->arg, "validation") == 0) {
228 tv->param_type = EXTCAP_PARAM_VALIDATION;
229 } else if (g_ascii_strcasecmp(tv->arg, "version") == 0) {
230 tv->param_type = EXTCAP_PARAM_VERSION;
232 tv->param_type = EXTCAP_PARAM_UNKNOWN;
235 tv->next_token = rs->param_list;
238 g_match_info_next(match_info, &error);
240 g_match_info_free(match_info);
241 g_regex_unref(regex);
247 extcap_token_sentence *extcap_tokenize_sentences(const gchar *s) {
248 extcap_token_sentence *first = NULL, *cur = NULL, *last = NULL;
250 gchar **list, **list_iter;
252 list_iter = list = g_strsplit(s, "\n", 0);
254 while (*list_iter != NULL ) {
255 cur = extcap_tokenize_sentence(*list_iter);
262 last->next_sentence = cur;
275 extcap_token_param *extcap_find_param_by_type(extcap_token_param *first,
276 extcap_param_type t) {
277 while (first != NULL ) {
278 if (first->param_type == t) {
282 first = first->next_token;
288 void extcap_free_value(extcap_value *v) {
298 extcap_interface *extcap_new_interface(void) {
299 extcap_interface *r = g_new(extcap_interface, 1);
301 r->call = r->display = r->version = NULL;
302 r->if_type = EXTCAP_SENTENCE_UNKNOWN;
303 r->next_interface = NULL;
308 void extcap_free_interface(extcap_interface *i) {
309 extcap_interface *next_i = i;
312 next_i = i->next_interface;
321 extcap_dlt *extcap_new_dlt(void) {
322 extcap_dlt *r = g_new(extcap_dlt, 1);
325 r->name = r->display = NULL;
331 void extcap_free_dlt(extcap_dlt *d) {
339 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
340 extcap_free_value((extcap_value *) data);
343 void extcap_free_arg(extcap_arg *a) {
351 g_free(a->fileextension);
355 if (a->range_start != NULL)
356 extcap_free_complex(a->range_start);
358 if (a->range_end != NULL)
359 extcap_free_complex(a->range_end);
361 if (a->default_complex != NULL)
362 extcap_free_complex(a->default_complex);
364 g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
367 static void extcap_free_arg_list_cb(gpointer listentry, gpointer data _U_) {
368 if (listentry != NULL)
369 extcap_free_arg((extcap_arg *) listentry);
372 void extcap_free_arg_list(GList *a) {
373 g_list_foreach(a, extcap_free_arg_list_cb, NULL);
377 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
378 if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
383 extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
384 extcap_token_param *v = NULL;
385 extcap_arg *target_arg = NULL;
386 extcap_value *value = NULL;
387 GList * entry = NULL;
389 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
394 if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
395 sent = EXTCAP_SENTENCE_ARG;
396 /* printf("ARG sentence\n"); */
397 } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
398 sent = EXTCAP_SENTENCE_VALUE;
399 /* printf("VALUE sentence\n"); */
402 if (sent == EXTCAP_SENTENCE_ARG) {
403 target_arg = g_new0(extcap_arg, 1);
404 target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
405 target_arg->save = TRUE;
407 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
409 extcap_free_arg(target_arg);
413 if (sscanf(v->value, "%d", &(target_arg->arg_num)) != 1) {
414 extcap_free_arg(target_arg);
418 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_CALL))
420 extcap_free_arg(target_arg);
423 target_arg->call = g_strdup(v->value);
425 /* No value only parameters allowed */
426 if (strlen(target_arg->call) == 0) {
427 extcap_free_arg(target_arg);
431 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
433 extcap_free_arg(target_arg);
436 target_arg->display = g_strdup(v->value);
438 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TOOLTIP))
440 target_arg->tooltip = g_strdup(v->value);
443 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_MUSTEXIST))
445 target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
448 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_EXTENSION))
450 target_arg->fileextension = g_strdup(v->value);
453 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALIDATION))
455 target_arg->regexp = g_strdup(v->value);
458 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_REQUIRED))
460 target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
463 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TYPE))
465 /* printf("no type in ARG sentence\n"); */
466 extcap_free_arg(target_arg);
470 if (g_ascii_strcasecmp(v->value, "integer") == 0) {
471 target_arg->arg_type = EXTCAP_ARG_INTEGER;
472 } else if (g_ascii_strcasecmp(v->value, "unsigned") == 0) {
473 target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
474 } else if (g_ascii_strcasecmp(v->value, "long") == 0) {
475 target_arg->arg_type = EXTCAP_ARG_LONG;
476 } else if (g_ascii_strcasecmp(v->value, "double") == 0) {
477 target_arg->arg_type = EXTCAP_ARG_DOUBLE;
478 } else if (g_ascii_strcasecmp(v->value, "boolean") == 0) {
479 target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
480 } else if (g_ascii_strcasecmp(v->value, "boolflag") == 0) {
481 target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
482 } else if (g_ascii_strcasecmp(v->value, "selector") == 0) {
483 target_arg->arg_type = EXTCAP_ARG_SELECTOR;
484 } else if (g_ascii_strcasecmp(v->value, "radio") == 0) {
485 target_arg->arg_type = EXTCAP_ARG_RADIO;
486 } else if (g_ascii_strcasecmp(v->value, "string") == 0) {
487 target_arg->arg_type = EXTCAP_ARG_STRING;
488 } else if (g_ascii_strcasecmp(v->value, "password") == 0) {
489 target_arg->arg_type = EXTCAP_ARG_PASSWORD;
490 /* default setting is to not save passwords */
491 target_arg->save = FALSE;
492 } else if (g_ascii_strcasecmp(v->value, "fileselect") == 0) {
493 target_arg->arg_type = EXTCAP_ARG_FILESELECT;
494 } else if (g_ascii_strcasecmp(v->value, "multicheck") == 0) {
495 target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
497 printf("invalid type %s in ARG sentence\n", v->value);
498 extcap_free_arg(target_arg);
502 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_SAVE))
504 target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
507 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_RANGE))
509 gchar *cp = g_strstr_len(v->value, -1, ",");
512 printf("invalid range, expected value,value got %s\n",
514 extcap_free_arg(target_arg);
518 if ((target_arg->range_start = extcap_parse_complex(
519 target_arg->arg_type, v->value)) == NULL) {
520 printf("invalid range, expected value,value got %s\n",
522 extcap_free_arg(target_arg);
526 if ((target_arg->range_end = extcap_parse_complex(
527 target_arg->arg_type, cp + 1)) == NULL) {
528 printf("invalid range, expected value,value got %s\n",
530 extcap_free_arg(target_arg);
535 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
537 if ( target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR )
539 if ((target_arg->default_complex = extcap_parse_complex(
540 target_arg->arg_type, v->value)) == NULL) {
541 printf("invalid default, couldn't parse %s\n", v->value);
546 } else if (sent == EXTCAP_SENTENCE_VALUE) {
547 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARG))
549 printf("no arg in VALUE sentence\n");
553 if (sscanf(v->value, "%d", &tint) != 1) {
554 printf("invalid arg in VALUE sentence\n");
559 if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
561 printf("couldn't find arg %d in list for VALUE sentence\n", tint);
565 value = g_new0(extcap_value, 1);
566 value->arg_num = tint;
568 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
570 /* printf("no value in VALUE sentence\n"); */
571 extcap_free_value(value);
574 value->call = g_strdup(v->value);
576 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
578 /* printf("no display in VALUE sentence\n"); */
579 extcap_free_value(value);
582 value->display = g_strdup(v->value);
584 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_PARENT))
586 value->parent = g_strdup(v->value);
589 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
591 /* printf("found default value\n"); */
592 value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
595 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ENABLED))
597 value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
600 ((extcap_arg*) entry->data)->values = g_list_append(
601 ((extcap_arg*) entry->data)->values, value);
609 GList * extcap_parse_args(extcap_token_sentence *first_s) {
613 extcap_arg *ra = NULL;
615 if ((ra = extcap_parse_arg_sentence(args, first_s)) != NULL)
616 args = g_list_append(args, (gpointer) ra);
618 first_s = first_s->next_sentence;
624 int extcap_parse_interface_sentence(extcap_token_sentence *s,
625 extcap_interface **ri) {
626 extcap_token_param *v = NULL;
627 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
634 if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
635 sent = EXTCAP_SENTENCE_INTERFACE;
636 } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
637 sent = EXTCAP_SENTENCE_EXTCAP;
640 if (sent == EXTCAP_SENTENCE_UNKNOWN)
643 *ri = extcap_new_interface();
645 (*ri)->if_type = sent;
647 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
648 == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
649 printf("No value in INTERFACE sentence\n");
650 extcap_free_interface(*ri);
654 (*ri)->call = g_strdup(v->value);
656 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
657 == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
658 printf("No display in INTERFACE sentence\n");
659 extcap_free_interface(*ri);
663 (*ri)->display = g_strdup(v->value);
665 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VERSION))
667 (*ri)->version = g_strdup(v->value);
673 int extcap_parse_interfaces(extcap_token_sentence *first_s,
674 extcap_interface **first_int) {
675 extcap_interface *first_i = NULL, *last_i = NULL;
678 extcap_interface *ri;
680 if (extcap_parse_interface_sentence(first_s, &ri) >= 0 && ri != NULL) {
681 if (first_i == NULL) {
682 first_i = last_i = ri;
684 last_i->next_interface = ri;
689 first_s = first_s->next_sentence;
692 *first_int = first_i;
697 int extcap_parse_dlt_sentence(extcap_token_sentence *s, extcap_dlt **rd) {
698 extcap_token_param *v = NULL;
699 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
706 if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
707 sent = EXTCAP_SENTENCE_DLT;
710 if (sent == EXTCAP_SENTENCE_UNKNOWN)
713 *rd = extcap_new_dlt();
715 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
717 printf("No number in DLT sentence\n");
718 extcap_free_dlt(*rd);
721 if (sscanf(v->value, "%d", &((*rd)->number)) != 1) {
722 printf("Invalid number in DLT sentence\n");
723 extcap_free_dlt(*rd);
727 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_NAME))
729 printf("No name in DLT sentence\n");
730 extcap_free_dlt(*rd);
733 (*rd)->name = g_strdup(v->value);
735 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
737 printf("No display in DLT sentence\n");
738 extcap_free_dlt(*rd);
741 (*rd)->display = g_strdup(v->value);
746 int extcap_parse_dlts(extcap_token_sentence *first_s, extcap_dlt **first_dlt) {
747 extcap_dlt *first_d = NULL, *last_d = NULL;
752 if (extcap_parse_dlt_sentence(first_s, &rd) >= 0 && rd != NULL) {
753 if (first_d == NULL) {
754 first_d = last_d = rd;
756 last_d->next_dlt = rd;
761 first_s = first_s->next_sentence;
764 *first_dlt = first_d;
770 * Editor modelines - http://www.wireshark.org/tools/modelines.html
775 * indent-tabs-mode: nil
778 * vi: set shiftwidth=4 tabstop=8 expandtab:
779 * :indentSize=4:tabSize=8:noTabs=true: