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.
31 #include "extcap_parser.h"
33 void extcap_printf_complex(extcap_complex *comp) {
34 gchar *ret = extcap_get_complex_as_string(comp);
39 gchar *extcap_get_complex_as_string(extcap_complex *comp) {
40 /* Pick an arbitrary size that should be big enough */
41 gchar *ret = g_new(gchar, 32);
44 g_snprintf(ret, 32, "(null)");
48 switch (comp->complex_type) {
49 case EXTCAP_ARG_INTEGER:
50 g_snprintf(ret, 32, "%d", comp->complex_value.int_value);
52 case EXTCAP_ARG_UNSIGNED:
53 g_snprintf(ret, 32, "%u", comp->complex_value.uint_value);
56 g_snprintf(ret, 32, "%ld", comp->complex_value.long_value);
58 case EXTCAP_ARG_DOUBLE:
59 g_snprintf(ret, 32, "%f", comp->complex_value.double_value);
61 case EXTCAP_ARG_BOOLEAN:
62 g_snprintf(ret, 32, "%s",
63 comp->complex_value.bool_value ? "TRUE" : "FALSE");
65 case EXTCAP_ARG_STRING:
66 case EXTCAP_ARG_FILESELECT:
68 ret = g_strdup(comp->complex_value.string_value);
71 /* Nulling out the return string */
72 g_snprintf(ret, 32, " ");
79 extcap_complex *extcap_parse_complex(extcap_arg_type complex_type,
81 extcap_complex *rc = g_new(extcap_complex, 1);
82 gboolean success = FALSE;
85 switch (complex_type) {
86 case EXTCAP_ARG_INTEGER:
87 if (sscanf(data, "%Lf", &exp_f) == 1) {
88 rc->complex_value.int_value = (int) exp_f;
93 case EXTCAP_ARG_UNSIGNED:
94 if (sscanf(data, "%Lf", &exp_f) == 1) {
95 rc->complex_value.uint_value = (unsigned int) exp_f;
100 case EXTCAP_ARG_LONG:
101 if (sscanf(data, "%Lf", &exp_f) == 1) {
102 rc->complex_value.long_value = (long) exp_f;
107 case EXTCAP_ARG_DOUBLE:
108 if (sscanf(data, "%Lf", &exp_f) == 1) {
109 rc->complex_value.double_value = (double) exp_f;
114 case EXTCAP_ARG_BOOLEAN:
115 case EXTCAP_ARG_BOOLFLAG:
116 if (data[0] == 't' || data[0] == 'T' || data[0] == '1') {
117 rc->complex_value.bool_value = 1;
119 rc->complex_value.bool_value = 0;
123 case EXTCAP_ARG_STRING:
124 case EXTCAP_ARG_FILESELECT:
125 rc->complex_value.string_value = g_strdup(data);
137 rc->complex_type = complex_type;
138 rc->value_filled = TRUE;
143 gboolean extcap_compare_is_default(extcap_arg *element, extcap_complex *test) {
144 gboolean result = FALSE;
146 if (element->default_complex == NULL)
149 switch (element->arg_type) {
150 case EXTCAP_ARG_INTEGER:
151 if (extcap_complex_get_int(test)
152 == extcap_complex_get_int(element->default_complex))
155 case EXTCAP_ARG_UNSIGNED:
156 if (extcap_complex_get_uint(test)
157 == extcap_complex_get_uint(element->default_complex))
160 case EXTCAP_ARG_LONG:
161 if (extcap_complex_get_long(test)
162 == extcap_complex_get_long(element->default_complex))
165 case EXTCAP_ARG_DOUBLE:
166 if (extcap_complex_get_double(test)
167 == extcap_complex_get_double(element->default_complex))
170 case EXTCAP_ARG_BOOLEAN:
171 case EXTCAP_ARG_BOOLFLAG:
172 if (extcap_complex_get_bool(test)
173 == extcap_complex_get_bool(element->default_complex))
176 case EXTCAP_ARG_STRING:
177 if (strcmp(extcap_complex_get_string(test),
178 extcap_complex_get_string(element->default_complex)) == 0)
189 void extcap_free_complex(extcap_complex *comp) {
190 if (comp->complex_type == EXTCAP_ARG_STRING
191 || comp->complex_type == EXTCAP_ARG_FILESELECT)
192 g_free(comp->complex_value.string_value);
197 int extcap_complex_get_int(extcap_complex *comp) {
200 return comp->complex_value.int_value;
203 unsigned int extcap_complex_get_uint(extcap_complex *comp) {
205 return (unsigned int)0;
206 return comp->complex_value.uint_value;
209 long extcap_complex_get_long(extcap_complex *comp) {
212 return comp->complex_value.long_value;
215 double extcap_complex_get_double(extcap_complex *comp) {
218 return comp->complex_value.double_value;
221 gboolean extcap_complex_get_bool(extcap_complex *comp) {
224 return comp->complex_value.bool_value;
227 gchar *extcap_complex_get_string(extcap_complex *comp) {
228 return comp->complex_value.string_value;
231 void extcap_free_tokenized_param(extcap_token_param *v) {
238 if (v->value != NULL)
244 void extcap_free_tokenized_sentence(extcap_token_sentence *s) {
245 extcap_token_param *tv;
250 if (s->sentence != NULL)
253 while (s->param_list != NULL ) {
255 s->param_list = tv->next_token;
257 extcap_free_tokenized_param(tv);
261 void extcap_free_tokenized_sentence_list(extcap_token_sentence *f) {
262 extcap_token_sentence *t;
265 t = f->next_sentence;
266 extcap_free_tokenized_sentence(f);
271 extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
274 extcap_token_param *tv = NULL;
276 extcap_token_sentence *rs = g_new(extcap_token_sentence, 1);
279 rs->next_sentence = NULL;
280 rs->param_list = NULL;
282 if ((b = g_strstr_len(s, -1, " ")) == NULL) {
283 extcap_free_tokenized_sentence(rs);
287 rs->sentence = g_strndup(s, b - s);
289 if ((b = g_strstr_len(s, -1, "{")) == NULL) {
290 /* printf("debug - tokenizer - sentence with no values\n"); */
291 extcap_free_tokenized_sentence(rs);
296 if ((e = g_strstr_len(b, -1, "}")) == NULL) {
297 /* printf("debug - tokenizer - invalid, missing }\n"); */
298 extcap_free_tokenized_sentence(rs);
302 if ((eq = g_strstr_len(b, -1, "=")) == NULL) {
303 /* printf("debug - tokenizer - invalid, missing =\n"); */
304 extcap_free_tokenized_sentence(rs);
311 if (b >= eq || e <= eq) {
312 /* printf("debug - tokenizer - invalid, missing arg or value in {}\n"); */
313 extcap_free_tokenized_sentence(rs);
317 tv = g_new(extcap_token_param, 1);
318 tv->arg = g_strndup(b, eq - b);
319 tv->value = g_strndup(eq + 1, e - eq);
321 if (g_ascii_strcasecmp(tv->arg, "number") == 0) {
322 tv->param_type = EXTCAP_PARAM_ARGNUM;
323 } else if (g_ascii_strcasecmp(tv->arg, "call") == 0) {
324 tv->param_type = EXTCAP_PARAM_CALL;
325 } else if (g_ascii_strcasecmp(tv->arg, "display") == 0) {
326 tv->param_type = EXTCAP_PARAM_DISPLAY;
327 } else if (g_ascii_strcasecmp(tv->arg, "type") == 0) {
328 tv->param_type = EXTCAP_PARAM_TYPE;
329 } else if (g_ascii_strcasecmp(tv->arg, "arg") == 0) {
330 tv->param_type = EXTCAP_PARAM_ARG;
331 } else if (g_ascii_strcasecmp(tv->arg, "default") == 0) {
332 tv->param_type = EXTCAP_PARAM_DEFAULT;
333 } else if (g_ascii_strcasecmp(tv->arg, "value") == 0) {
334 tv->param_type = EXTCAP_PARAM_VALUE;
335 } else if (g_ascii_strcasecmp(tv->arg, "range") == 0) {
336 tv->param_type = EXTCAP_PARAM_RANGE;
337 } else if (g_ascii_strcasecmp(tv->arg, "tooltip") == 0) {
338 tv->param_type = EXTCAP_PARAM_TOOLTIP;
339 } else if (g_ascii_strcasecmp(tv->arg, "mustexist") == 0) {
340 tv->param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
341 } else if (g_ascii_strcasecmp(tv->arg, "name") == 0) {
342 tv->param_type = EXTCAP_PARAM_NAME;
343 } else if (g_ascii_strcasecmp(tv->arg, "enabled") == 0) {
344 tv->param_type = EXTCAP_PARAM_ENABLED;
346 tv->param_type = EXTCAP_PARAM_UNKNOWN;
349 tv->next_token = rs->param_list;
352 /* printf("debug - tokenizer - got '%s' = '%s'\n", tv->arg, tv->value); */
355 if ((size_t) (b - s) > strlen(s))
358 b = g_strstr_len(b, -1, "{");
364 extcap_token_sentence *extcap_tokenize_sentences(const gchar *s) {
365 extcap_token_sentence *first = NULL, *cur = NULL, *last = NULL;
367 gchar **list, **list_iter;
369 list_iter = list = g_strsplit(s, "\n", 0);
371 while (*list_iter != NULL ) {
372 cur = extcap_tokenize_sentence(*list_iter);
379 last->next_sentence = cur;
392 extcap_token_param *extcap_find_param_by_type(extcap_token_param *first,
393 extcap_param_type t) {
394 while (first != NULL ) {
395 if (first->param_type == t) {
399 first = first->next_token;
405 void extcap_free_value(extcap_value *v) {
412 if (v->display != NULL)
418 extcap_interface *extcap_new_interface(void) {
419 extcap_interface *r = g_new(extcap_interface, 1);
421 r->call = r->display = NULL;
422 r->next_interface = NULL;
427 void extcap_free_interface(extcap_interface *i) {
434 if (i->display != NULL)
438 extcap_dlt *extcap_new_dlt(void) {
439 extcap_dlt *r = g_new(extcap_dlt, 1);
442 r->name = r->display = NULL;
448 void extcap_free_dlt(extcap_dlt *d) {
455 if (d->display != NULL)
459 extcap_arg *extcap_new_arg(void) {
460 extcap_arg *r = g_new(extcap_arg, 1);
465 r->arg_type = EXTCAP_ARG_UNKNOWN;
466 r->range_start = NULL;
468 r->default_complex = NULL;
469 r->fileexists = FALSE;
472 /*r->next_arg = NULL; */
477 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
478 extcap_free_value((extcap_value *) data);
481 void extcap_free_arg(extcap_arg *a) {
489 if (a->display != NULL)
492 if (a->tooltip != NULL)
495 if (a->range_start != NULL)
496 extcap_free_complex(a->range_start);
498 if (a->range_end != NULL)
499 extcap_free_complex(a->range_end);
501 if (a->default_complex != NULL)
502 extcap_free_complex(a->default_complex);
504 g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
507 static void extcap_free_arg_list_cb(gpointer listentry, gpointer data _U_) {
508 if (listentry != NULL)
509 extcap_free_arg((extcap_arg *) listentry);
512 void extcap_free_arg_list(GList *a) {
513 g_list_foreach(a, extcap_free_arg_list_cb, NULL);
517 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
518 if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
523 extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
524 extcap_token_param *v = NULL;
525 extcap_arg *target_arg = NULL;
526 extcap_value *value = NULL;
527 GList * entry = NULL;
529 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
534 if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
535 sent = EXTCAP_SENTENCE_ARG;
536 /* printf("ARG sentence\n"); */
537 } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
538 sent = EXTCAP_SENTENCE_VALUE;
539 /* printf("VALUE sentence\n"); */
542 if (sent == EXTCAP_SENTENCE_ARG) {
543 target_arg = extcap_new_arg();
545 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
547 extcap_free_arg(target_arg);
551 if (sscanf(v->value, "%d", &(target_arg->arg_num)) != 1) {
552 extcap_free_arg(target_arg);
556 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_CALL))
558 extcap_free_arg(target_arg);
561 target_arg->call = g_strdup(v->value);
563 /* No value only parameters allowed */
564 if (strlen(target_arg->call) == 0) {
565 extcap_free_arg(target_arg);
569 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
571 extcap_free_arg(target_arg);
574 target_arg->display = g_strdup(v->value);
576 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TOOLTIP))
578 target_arg->tooltip = g_strdup(v->value);
581 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_MUSTEXIST))
583 target_arg->fileexists = (v->value[0] == 't' || v->value[0] == 'T');
586 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TYPE))
588 /* printf("no type in ARG sentence\n"); */
589 extcap_free_arg(target_arg);
593 if (g_ascii_strcasecmp(v->value, "integer") == 0) {
594 target_arg->arg_type = EXTCAP_ARG_INTEGER;
595 } else if (g_ascii_strcasecmp(v->value, "unsigned") == 0) {
596 target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
597 } else if (g_ascii_strcasecmp(v->value, "long") == 0) {
598 target_arg->arg_type = EXTCAP_ARG_LONG;
599 } else if (g_ascii_strcasecmp(v->value, "double") == 0) {
600 target_arg->arg_type = EXTCAP_ARG_DOUBLE;
601 } else if (g_ascii_strcasecmp(v->value, "boolean") == 0) {
602 target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
603 } else if (g_ascii_strcasecmp(v->value, "boolflag") == 0) {
604 target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
605 } else if (g_ascii_strcasecmp(v->value, "menu") == 0) {
606 target_arg->arg_type = EXTCAP_ARG_MENU;
607 } else if (g_ascii_strcasecmp(v->value, "selector") == 0) {
608 target_arg->arg_type = EXTCAP_ARG_SELECTOR;
609 } else if (g_ascii_strcasecmp(v->value, "radio") == 0) {
610 target_arg->arg_type = EXTCAP_ARG_RADIO;
611 } else if (g_ascii_strcasecmp(v->value, "string") == 0) {
612 target_arg->arg_type = EXTCAP_ARG_STRING;
613 } else if (g_ascii_strcasecmp(v->value, "fileselect") == 0) {
614 target_arg->arg_type = EXTCAP_ARG_FILESELECT;
615 } else if (g_ascii_strcasecmp(v->value, "multicheck") == 0) {
616 target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
618 printf("invalid type %s in ARG sentence\n", v->value);
619 extcap_free_arg(target_arg);
623 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_RANGE))
625 gchar *cp = g_strstr_len(v->value, -1, ",");
628 printf("invalid range, expected value,value got %s\n",
630 extcap_free_arg(target_arg);
634 if ((target_arg->range_start = extcap_parse_complex(
635 target_arg->arg_type, v->value)) == NULL) {
636 printf("invalid range, expected value,value got %s\n",
638 extcap_free_arg(target_arg);
642 if ((target_arg->range_end = extcap_parse_complex(
643 target_arg->arg_type, cp + 1)) == NULL) {
644 printf("invalid range, expected value,value got %s\n",
646 extcap_free_arg(target_arg);
651 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
653 if ((target_arg->default_complex = extcap_parse_complex(
654 target_arg->arg_type, v->value)) == NULL) {
655 printf("invalid default, couldn't parse %s\n", v->value);
659 } else if (sent == EXTCAP_SENTENCE_VALUE) {
660 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARG))
662 printf("no arg in VALUE sentence\n");
666 if (sscanf(v->value, "%d", &tint) != 1) {
667 printf("invalid arg in VALUE sentence\n");
672 if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
674 printf("couldn't find arg %d in list for VALUE sentence\n", tint);
678 value = g_new(extcap_value, 1);
679 value->display = NULL;
681 value->enabled = FALSE;
682 value->is_default = FALSE;
683 value->arg_num = tint;
685 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
687 /* printf("no value in VALUE sentence\n"); */
688 extcap_free_value(value);
691 value->call = g_strdup(v->value);
693 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
695 /* printf("no display in VALUE sentence\n"); */
696 extcap_free_value(value);
699 value->display = g_strdup(v->value);
701 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
703 /* printf("found default value\n"); */
704 value->is_default = (v->value[0] == 't' || v->value[0] == 'T');
707 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ENABLED))
709 value->enabled = (v->value[0] == 't' || v->value[0] == 'T');
712 ((extcap_arg*) entry->data)->values = g_list_append(
713 ((extcap_arg*) entry->data)->values, value);
721 GList * extcap_parse_args(extcap_token_sentence *first_s) {
725 extcap_arg *ra = NULL;
727 if ((ra = extcap_parse_arg_sentence(args, first_s)) != NULL)
728 args = g_list_append(args, (gpointer) ra);
730 first_s = first_s->next_sentence;
736 int extcap_parse_interface_sentence(extcap_token_sentence *s,
737 extcap_interface **ri) {
738 extcap_token_param *v = NULL;
739 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
746 if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
747 sent = EXTCAP_SENTENCE_INTERFACE;
748 /* printf("INTERFACE sentence\n"); */
751 if (sent == EXTCAP_SENTENCE_UNKNOWN)
754 *ri = extcap_new_interface();
756 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
758 printf("No value in INTERFACE sentence\n");
759 extcap_free_interface(*ri);
762 (*ri)->call = g_strdup(v->value);
764 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
766 printf("No display in INTERFACE sentence\n");
767 extcap_free_interface(*ri);
770 (*ri)->display = g_strdup(v->value);
775 int extcap_parse_interfaces(extcap_token_sentence *first_s,
776 extcap_interface **first_int) {
777 extcap_interface *first_i = NULL, *last_i = NULL;
780 extcap_interface *ri;
782 if (extcap_parse_interface_sentence(first_s, &ri) >= 0 && ri != NULL) {
783 if (first_i == NULL) {
784 first_i = last_i = ri;
786 last_i->next_interface = ri;
791 first_s = first_s->next_sentence;
794 *first_int = first_i;
799 int extcap_parse_dlt_sentence(extcap_token_sentence *s, extcap_dlt **rd) {
800 extcap_token_param *v = NULL;
801 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
808 if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
809 sent = EXTCAP_SENTENCE_DLT;
812 if (sent == EXTCAP_SENTENCE_UNKNOWN)
815 *rd = extcap_new_dlt();
817 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
819 printf("No number in DLT sentence\n");
820 extcap_free_dlt(*rd);
823 if (sscanf(v->value, "%d", &((*rd)->number)) != 1) {
824 printf("Invalid number in DLT sentence\n");
825 extcap_free_dlt(*rd);
829 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_NAME))
831 printf("No name in DLT sentence\n");
832 extcap_free_dlt(*rd);
835 (*rd)->name = g_strdup(v->value);
837 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
839 printf("No display in DLT sentence\n");
840 extcap_free_dlt(*rd);
843 (*rd)->display = g_strdup(v->value);
848 int extcap_parse_dlts(extcap_token_sentence *first_s, extcap_dlt **first_dlt) {
849 extcap_dlt *first_d = NULL, *last_d = NULL;
854 if (extcap_parse_dlt_sentence(first_s, &rd) >= 0 && rd != NULL) {
855 if (first_d == NULL) {
856 first_d = last_d = rd;
858 last_d->next_dlt = rd;
863 first_s = first_s->next_sentence;
866 *first_dlt = first_d;
872 * Editor modelines - http://www.wireshark.org/tools/modelines.html
877 * indent-tabs-mode: t
880 * vi: set shiftwidth=4 tabstop=4 noexpandtab:
881 * :indentSize=4:tabSize=4:noTabs=false: