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;
345 } else if (g_ascii_strcasecmp(tv->arg, "parent") == 0) {
346 tv->param_type = EXTCAP_PARAM_PARENT;
348 tv->param_type = EXTCAP_PARAM_UNKNOWN;
351 tv->next_token = rs->param_list;
354 /* printf("debug - tokenizer - got '%s' = '%s'\n", tv->arg, tv->value); */
357 if ((size_t) (b - s) > strlen(s))
360 b = g_strstr_len(b, -1, "{");
366 extcap_token_sentence *extcap_tokenize_sentences(const gchar *s) {
367 extcap_token_sentence *first = NULL, *cur = NULL, *last = NULL;
369 gchar **list, **list_iter;
371 list_iter = list = g_strsplit(s, "\n", 0);
373 while (*list_iter != NULL ) {
374 cur = extcap_tokenize_sentence(*list_iter);
381 last->next_sentence = cur;
394 extcap_token_param *extcap_find_param_by_type(extcap_token_param *first,
395 extcap_param_type t) {
396 while (first != NULL ) {
397 if (first->param_type == t) {
401 first = first->next_token;
407 void extcap_free_value(extcap_value *v) {
414 if (v->display != NULL)
420 extcap_interface *extcap_new_interface(void) {
421 extcap_interface *r = g_new(extcap_interface, 1);
423 r->call = r->display = NULL;
424 r->next_interface = NULL;
429 void extcap_free_interface(extcap_interface *i) {
436 if (i->display != NULL)
440 extcap_dlt *extcap_new_dlt(void) {
441 extcap_dlt *r = g_new(extcap_dlt, 1);
444 r->name = r->display = NULL;
450 void extcap_free_dlt(extcap_dlt *d) {
457 if (d->display != NULL)
461 extcap_arg *extcap_new_arg(void) {
462 extcap_arg *r = g_new(extcap_arg, 1);
467 r->arg_type = EXTCAP_ARG_UNKNOWN;
468 r->range_start = NULL;
470 r->default_complex = NULL;
471 r->fileexists = FALSE;
474 /*r->next_arg = NULL; */
479 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
480 extcap_free_value((extcap_value *) data);
483 void extcap_free_arg(extcap_arg *a) {
491 if (a->display != NULL)
494 if (a->tooltip != NULL)
497 if (a->range_start != NULL)
498 extcap_free_complex(a->range_start);
500 if (a->range_end != NULL)
501 extcap_free_complex(a->range_end);
503 if (a->default_complex != NULL)
504 extcap_free_complex(a->default_complex);
506 g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
509 static void extcap_free_arg_list_cb(gpointer listentry, gpointer data _U_) {
510 if (listentry != NULL)
511 extcap_free_arg((extcap_arg *) listentry);
514 void extcap_free_arg_list(GList *a) {
515 g_list_foreach(a, extcap_free_arg_list_cb, NULL);
519 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
520 if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
525 extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
526 extcap_token_param *v = NULL;
527 extcap_arg *target_arg = NULL;
528 extcap_value *value = NULL;
529 GList * entry = NULL;
531 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
536 if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
537 sent = EXTCAP_SENTENCE_ARG;
538 /* printf("ARG sentence\n"); */
539 } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
540 sent = EXTCAP_SENTENCE_VALUE;
541 /* printf("VALUE sentence\n"); */
544 if (sent == EXTCAP_SENTENCE_ARG) {
545 target_arg = extcap_new_arg();
547 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
549 extcap_free_arg(target_arg);
553 if (sscanf(v->value, "%d", &(target_arg->arg_num)) != 1) {
554 extcap_free_arg(target_arg);
558 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_CALL))
560 extcap_free_arg(target_arg);
563 target_arg->call = g_strdup(v->value);
565 /* No value only parameters allowed */
566 if (strlen(target_arg->call) == 0) {
567 extcap_free_arg(target_arg);
571 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
573 extcap_free_arg(target_arg);
576 target_arg->display = g_strdup(v->value);
578 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TOOLTIP))
580 target_arg->tooltip = g_strdup(v->value);
583 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_MUSTEXIST))
585 target_arg->fileexists = (v->value[0] == 't' || v->value[0] == 'T');
588 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TYPE))
590 /* printf("no type in ARG sentence\n"); */
591 extcap_free_arg(target_arg);
595 if (g_ascii_strcasecmp(v->value, "integer") == 0) {
596 target_arg->arg_type = EXTCAP_ARG_INTEGER;
597 } else if (g_ascii_strcasecmp(v->value, "unsigned") == 0) {
598 target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
599 } else if (g_ascii_strcasecmp(v->value, "long") == 0) {
600 target_arg->arg_type = EXTCAP_ARG_LONG;
601 } else if (g_ascii_strcasecmp(v->value, "double") == 0) {
602 target_arg->arg_type = EXTCAP_ARG_DOUBLE;
603 } else if (g_ascii_strcasecmp(v->value, "boolean") == 0) {
604 target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
605 } else if (g_ascii_strcasecmp(v->value, "boolflag") == 0) {
606 target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
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;
684 value->parent = NULL;
686 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
688 /* printf("no value in VALUE sentence\n"); */
689 extcap_free_value(value);
692 value->call = g_strdup(v->value);
694 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
696 /* printf("no display in VALUE sentence\n"); */
697 extcap_free_value(value);
700 value->display = g_strdup(v->value);
702 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_PARENT))
704 value->parent = g_strdup(v->value);
707 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
709 /* printf("found default value\n"); */
710 value->is_default = (v->value[0] == 't' || v->value[0] == 'T');
713 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ENABLED))
715 value->enabled = (v->value[0] == 't' || v->value[0] == 'T');
718 ((extcap_arg*) entry->data)->values = g_list_append(
719 ((extcap_arg*) entry->data)->values, value);
727 GList * extcap_parse_args(extcap_token_sentence *first_s) {
731 extcap_arg *ra = NULL;
733 if ((ra = extcap_parse_arg_sentence(args, first_s)) != NULL)
734 args = g_list_append(args, (gpointer) ra);
736 first_s = first_s->next_sentence;
742 int extcap_parse_interface_sentence(extcap_token_sentence *s,
743 extcap_interface **ri) {
744 extcap_token_param *v = NULL;
745 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
752 if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
753 sent = EXTCAP_SENTENCE_INTERFACE;
754 /* printf("INTERFACE sentence\n"); */
757 if (sent == EXTCAP_SENTENCE_UNKNOWN)
760 *ri = extcap_new_interface();
762 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
764 printf("No value in INTERFACE sentence\n");
765 extcap_free_interface(*ri);
768 (*ri)->call = g_strdup(v->value);
770 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
772 printf("No display in INTERFACE sentence\n");
773 extcap_free_interface(*ri);
776 (*ri)->display = g_strdup(v->value);
781 int extcap_parse_interfaces(extcap_token_sentence *first_s,
782 extcap_interface **first_int) {
783 extcap_interface *first_i = NULL, *last_i = NULL;
786 extcap_interface *ri;
788 if (extcap_parse_interface_sentence(first_s, &ri) >= 0 && ri != NULL) {
789 if (first_i == NULL) {
790 first_i = last_i = ri;
792 last_i->next_interface = ri;
797 first_s = first_s->next_sentence;
800 *first_int = first_i;
805 int extcap_parse_dlt_sentence(extcap_token_sentence *s, extcap_dlt **rd) {
806 extcap_token_param *v = NULL;
807 extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
814 if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
815 sent = EXTCAP_SENTENCE_DLT;
818 if (sent == EXTCAP_SENTENCE_UNKNOWN)
821 *rd = extcap_new_dlt();
823 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
825 printf("No number in DLT sentence\n");
826 extcap_free_dlt(*rd);
829 if (sscanf(v->value, "%d", &((*rd)->number)) != 1) {
830 printf("Invalid number in DLT sentence\n");
831 extcap_free_dlt(*rd);
835 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_NAME))
837 printf("No name in DLT sentence\n");
838 extcap_free_dlt(*rd);
841 (*rd)->name = g_strdup(v->value);
843 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
845 printf("No display in DLT sentence\n");
846 extcap_free_dlt(*rd);
849 (*rd)->display = g_strdup(v->value);
854 int extcap_parse_dlts(extcap_token_sentence *first_s, extcap_dlt **first_dlt) {
855 extcap_dlt *first_d = NULL, *last_d = NULL;
860 if (extcap_parse_dlt_sentence(first_s, &rd) >= 0 && rd != NULL) {
861 if (first_d == NULL) {
862 first_d = last_d = rd;
864 last_d->next_dlt = rd;
869 first_s = first_s->next_sentence;
872 *first_dlt = first_d;
878 * Editor modelines - http://www.wireshark.org/tools/modelines.html
883 * indent-tabs-mode: nil
886 * vi: set shiftwidth=4 tabstop=8 expandtab:
887 * :indentSize=4:tabSize=8:noTabs=true: