3 # Copyright 2013 Michael Mann (see AUTHORS file)
5 # A program to help convert the "old" expert_add_info_format API calls into filterable "items" that
6 # use the other expert API calls. The program requires 2 passes. "Pass 1" (generate) collects
7 # the eligible expert_add_info_format calls and outputs the necessary data into a delimited
8 # file. "Pass 2" (fix-all) takes the data from the delimited file and replaces the
9 # expert_add_info_format calls with filterable "expert info" calls as well as
10 # generating a separate files for the ei variable declarations and array data.
11 # The ei "file" can be copy/pasted into the dissector where appropriate
13 # Note that the output from "Pass 1" won't always be a perfect conversion for "Pass 2", so
14 # "human interaction" is needed as an intermediary to verify and update the delimited file
15 # before "Pass 2" is done.
17 # Delimited file field format:
18 # <convert expert_add_info_format_call[1-4]><add ei variable[0|1]><ei var><[GROUP]><[SEVERITY]><[FIELDNAME]><[EXPERTABBREV]>
19 # <pinfo var><proto_item var><tvb var><offset><length><params>
21 # convert proto_tree_add_text_call enumerations:
23 # 2 - expert_add_info_format
24 # 3 - proto_tree_add_expert
25 # 4 - proto_tree_add_expert_format
27 # Usage: convert_expert_add_info_format.pl action=<generate|fix-all> <file or files>
29 # Based off of convert_proto_tree_add_text.pl
31 # Wireshark - Network traffic analyzer
32 # By Gerald Combs <gerald@wireshark.org>
33 # Copyright 1998 Gerald Combs
35 # This program is free software; you can redistribute it and/or
36 # modify it under the terms of the GNU General Public License
37 # as published by the Free Software Foundation; either version 2
38 # of the License, or (at your option) any later version.
40 # This program is distributed in the hope that it will be useful,
41 # but WITHOUT ANY WARRANTY; without even the implied warranty of
42 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 # GNU General Public License for more details.
45 # You should have received a copy of the GNU General Public License
46 # along with this program; if not, write to the Free Software
47 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
55 my %EXPERT_SEVERITY = ('PI_COMMENT' => "PI_COMMENT",
56 'PI_CHAT' => "PI_CHAT",
57 'PI_NOTE' => "PI_NOTE",
58 'PI_WARN' => "PI_WARN",
59 'PI_ERROR' => "PI_ERROR");
61 my %EXPERT_GROUPS = ('PI_CHECKSUM' => "PI_CHECKSUM",
62 'PI_SEQUENCE' => "PI_SEQUENCE",
63 'PI_RESPONSE_CODE' => "PI_RESPONSE_CODE",
64 'PI_REQUEST_CODE' => "PI_REQUEST_CODE",
65 'PI_UNDECODED' => "PI_UNDECODED",
66 'PI_REASSEMBLE' => "PI_REASSEMBLE",
67 'PI_MALFORMED' => "PI_MALFORMED",
68 'PI_DEBUG' => "PI_DEBUG",
69 'PI_PROTOCOL' => "PI_PROTOCOL",
70 'PI_SECURITY' => "PI_SECURITY",
71 'PI_COMMENTS_GROUP' => "PI_COMMENTS_GROUP");
76 # Perl trim function to remove whitespace from the start and end of the string
85 # ---------------------------------------------------------------------
90 my $action = 'generate';
93 my $result = GetOptions(
94 'action=s' => \$action,
95 'register' => \$register,
96 'help|?' => \$helpFlag
99 if (!$result || $helpFlag || !$ARGV[0]) {
104 print "\nUsage: $0 [--action=generate|fix-all|find-all] FILENAME [...]\n\n";
105 print " --action = generate (default)\n";
106 print " generate - create a delimited file (FILENAME.expert_add_info_input) with\n";
107 print " expert_add_info_format fields in FILENAME(s)\n";
108 print " fix-all - Use delimited file (FILENAME.expert_add_info_input) to convert\n";
109 print " expert_add_info_format to \"filterable\" expert API\n";
110 print " Also generates FILENAME.ei to be copy/pasted into\n";
111 print " the dissector where appropriate\n\n";
112 print " --register = generate ei_register_info and expert register function calls\n\n";
118 # XXX Outline general algorithm here
121 my $protabbrev_index;
124 while (my $fileName = $ARGV[0]) {
126 my $fileContents = '';
128 die "No such file: \"$fileName\"\n" if (! -e $fileName);
130 # delete leading './'
131 $fileName =~ s{ ^ \. / } {}xo;
133 #determine PROTABBREV for dissector based on file name format of (dirs)/packet-PROTABBREV.c
134 $protabbrev_index = rindex($fileName, "packet-");
135 if ($protabbrev_index == -1) {
136 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
140 $protabbrev = substr($fileName, $protabbrev_index+length("packet-"));
141 $protabbrev_index = rindex($protabbrev, ".");
142 if ($protabbrev_index == -1) {
143 print "$fileName doesn't fit format of packet-PROTABBREV.c\n";
146 $protabbrev = lc(substr($protabbrev, 0, $protabbrev_index));
148 # Read in the file (ouch, but it's easier that way)
149 open(FCI, "<", $fileName) || die("Couldn't open $fileName");
155 if ($action eq "generate") {
156 generate_eis(\$fileContents, $fileName);
159 if ($action eq "fix-all") {
160 # Read in the ei "input" file
163 open(FCI, "<", $fileName . ".expert_add_info_input") || die("Couldn't open $fileName.expert_add_info_input");
164 while(my $line=<FCI>){
165 my @expert_item = split(/;|\n/, $line);
168 $errors += verify_line(@expert_item);
170 push(@expert_list, \@expert_item);
175 print "Aborting conversion.\n";
179 fix_expert_add_info_format(\$fileContents, $fileName);
181 # Write out the ei data
182 output_ei_data($fileName);
184 # Write out the changed version to a file
185 open(FCO, ">", $fileName . ".expert_add_info_format");
186 print FCO "$fileContents";
194 # ---------------------------------------------------------------------
195 # Sanity check the data in the .proto_tree_input file
197 my( @expert_item) = @_;
200 #do some basic error checking of the file
201 if (($expert_item[0] eq "1") ||
202 ($expert_item[0] eq "2") ||
203 ($expert_item[0] eq "3") ||
204 ($expert_item[0] eq "4")) {
205 #expert info conversions
206 if (!($expert_item[2] =~ /^ei_/)) {
207 print "$line_number: Poorly formed ei_ variable ($expert_item[2])!\n";
211 print "$line_number: Bad conversion value!\n";
215 if ($expert_item[1] eq "1") {
216 if (!($expert_item[2] =~ /^ei_/)) {
217 print "$line_number: Poorly formed ei_ variable ($expert_item[2])!\n";
220 if (!exists($EXPERT_SEVERITY{$expert_item[4]})) {
221 print "$line_number: Expert severity value '$expert_item[5]' unknown!\n";
224 if (!exists($EXPERT_GROUPS{$expert_item[3]})) {
225 print "$line_number: Expert group value '$expert_item[4]' unknown!\n";
229 } elsif ($expert_item[1] ne "0") {
230 print "$line_number: Bad ei variable generation value!\n";
238 my( $fileContentsRef, $fileName) = @_;
247 (?:expert_add_info_format)\s* \(
254 while ($$fileContentsRef =~ / $pat /xgso) {
256 my @expert_item = (1, 1, "ei_name", "GROUP", "SEVERITY", "fieldfullname", "fieldabbrevname",
257 "pinfo", "item", "tvb", "offset", "length", "params");
260 $str =~ tr/\t\n\r/ /d;
261 $str =~ s/ \s+ / /xg;
262 #print "$fileName: $str\n";
264 @args = split(/,/, $str);
265 #printf "ARGS(%d): %s\n", scalar @args, join("# ", @args);
266 $args[0] =~ s/expert_add_info_format\s*\(\s*//;
268 $expert_item[7] = $args[0]; #pinfo
269 $expert_item[8] = trim($args[1]); #item
270 $expert_item[3] = trim($args[2]); #GROUP
271 $expert_item[4] = trim($args[3]); #SEVERITY
272 $expert_item[5] = trim($args[4]); #fieldfullname
273 $expert_item[5] =~ s/\"//;
276 $expert_item[5] =~ s/\"\s*\)\s*;$//;
277 $expert_item[5] =~ s/\"$//;
280 $expert_item[12] = "";
281 while ($arg_loop < scalar @args) {
282 $expert_item[12] .= trim($args[$arg_loop]);
283 if ($arg_loop+1 < scalar @args) {
284 $expert_item[12] .= ", ";
288 $expert_item[12] =~ s/\s*\)\s*;$//;
291 $expert_item[2] = sprintf("ei_%s_%s", $protabbrev, lc($expert_item[5]));
292 $expert_item[2] =~ s/\s+|-|:/_/g;
294 #field abbreviated name
295 $expert_item[6] = sprintf("%s.%s", $protabbrev, lc($expert_item[5]));
296 $expert_item[6] =~ s/\s+|-|:/_/g;
298 push(@expert_list, \@expert_item);
303 if ($num_items > 0) {
304 open(FCO, ">", $fileName . ".expert_add_info_input");
305 for my $item (@expert_list) {
306 print FCO join(";", @{$item}), "\n";
312 # ---------------------------------------------------------------------
313 # Find all expert_add_info_format calls and replace them with the data
314 # found in expert_list
315 sub fix_expert_add_info_format {
316 my( $fileContentsRef, $fileName) = @_;
322 (?:expert_add_info_format)\s* \(
329 $$fileContentsRef =~ s/ $pat /patsub($found, $1)/xges;
332 # ---------------------------------------------------------------------
333 # Format expert info functions with expert_list data
337 #print $expert_list[$_[0]][2] . " = ";
338 #print $#{$expert_list[$_[0]]}+1;
341 if ($expert_list[$_[0]][0] eq "1") {
342 $item_str = sprintf("expert_add_info(%s, %s, &%s);",
343 $expert_list[$_[0]][7], $expert_list[$_[0]][8], $expert_list[$_[0]][2]);
344 } elsif ($expert_list[$_[0]][0] eq "2") {
345 $item_str = sprintf("expert_add_info_format(%s, %s, &%s, \"%s\"",
346 $expert_list[$_[0]][7], $expert_list[$_[0]][8],
347 $expert_list[$_[0]][2], $expert_list[$_[0]][5]);
348 if (($#{$expert_list[$_[0]]}+1 > 12 ) && ($expert_list[$_[0]][12] ne "")) {
349 $item_str .= ", $expert_list[$_[0]][12]";
352 } elsif ($expert_list[$_[0]][0] eq "3") {
353 $item_str = sprintf("proto_tree_add_expert(%s, %s, &%s, %s, %s, %s);",
354 $expert_list[$_[0]][8], $expert_list[$_[0]][7],
355 $expert_list[$_[0]][2], $expert_list[$_[0]][9],
356 $expert_list[$_[0]][10], $expert_list[$_[0]][11]);
357 } elsif ($expert_list[$_[0]][0] eq "4") {
358 $item_str = sprintf("proto_tree_add_expert_format(%s, %s, &%s, %s, %s, %s, \"%s\"",
359 $expert_list[$_[0]][8], $expert_list[$_[0]][7], $expert_list[$_[0]][2],
360 $expert_list[$_[0]][9], $expert_list[$_[0]][10],
361 $expert_list[$_[0]][11], $expert_list[$_[0]][5]);
362 if (($#{$expert_list[$_[0]]}+1 > 12) && ($expert_list[$_[0]][12] ne "")) {
363 $item_str .= ", $expert_list[$_[0]][12]";
373 # ---------------------------------------------------------------------
374 # Output the ei variable declarations and expert array. For now, write them to a file.
375 # XXX - Eventually find the right place to add it to the modified dissector file
382 #add ei to hash table to prevent against (accidental) duplicates
383 for ($index=0;$index<@expert_list;$index++) {
384 if ($expert_list[$index][1] eq "1") {
385 $eis{$expert_list[$index][2]} = $expert_list[$index][2];
389 open(FCO, ">", $fileName . ".ei");
391 print FCO "/* Generated from convert_expert_add_info_format.pl */\n";
393 foreach $key (keys %eis) {
394 print FCO "static expert_field $key = EI_INIT;\n";
398 if ($register ne "") {
399 print FCO " static ei_register_info ei[] = {\n";
403 for ($index=0;$index<@expert_list;$index++) {
404 if ($expert_list[$index][1] eq "1") {
405 if (exists($eis{$expert_list[$index][2]})) {
406 print "duplicate ei entry '$expert_list[$index][2]' found! Aborting conversion.\n";
409 $eis{$expert_list[$index][2]} = $expert_list[$index][2];
411 print FCO " { &$expert_list[$index][2], { \"$expert_list[$index][6]\", $expert_list[$index][3], ";
412 print FCO "$expert_list[$index][4], \"$expert_list[$index][5]\", EXPFILL }},\r\n";
416 if ($register ne "") {
417 print FCO " };\n\n\n";
418 print FCO " expert_module_t* expert_$protabbrev;\n\n";
420 print FCO " expert_$protabbrev = expert_register_protocol(proto_$protabbrev);\n";
421 print FCO " expert_register_field_array(expert_$protabbrev, ei, array_length(ei));\n\n";