aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorMartin Mathieson <martin.mathieson@keysight.com>2020-08-07 21:15:46 +0100
committerAnders Broman <a.broman58@gmail.com>2020-08-07 21:27:26 +0000
commit1dd8bb2811d0e3e676b61f3d7e5ac311aa8ab4fc (patch)
tree63d5c9bac648a0c2a8d58555f5fe70773f2649e3 /tools
parentb711b300cc9da138ff4d1a5aeeae538710c9daa5 (diff)
Find and fix dissectors that should be using a default tfs.
Found using tools/check_tfs.py, included in this commit. Here are the reports that were fixed here: Examining: All dissector modules epan/dissectors/packet-assa_r3.c tfs_mortisepins_flags - could have used tfs_high_low from tfs.c instead: {High,Low} epan/dissectors/packet-btle.c tfs_present_bit - could have used tfs_present_not_present from tfs.c instead: {Present,Not Present} epan/dissectors/packet-dhcp.c tfs_fqdn_s - could have used tfs_server_client from tfs.c instead: {Server,Client} epan/dissectors/packet-docsis-macmgmt.c mdd_tfs_on_off - could have used tfs_on_off from tfs.c instead: {On,Off} epan/dissectors/packet-docsis-macmgmt.c mdd_tfs_en_dis - could have used tfs_enabled_disabled from tfs.c instead: {Enabled,Disabled} epan/dissectors/packet-docsis-macmgmt.c req_not_req_tfs - could have used tfs_requested_not_requested from tfs.c instead: {Requested,Not Requested} epan/dissectors/packet-docsis-tlv.c on_off_tfs - could have used tfs_on_off from tfs.c instead: {On,Off} epan/dissectors/packet-docsis-tlv.c activation_tfs - could have used tfs_active_inactive from tfs.c instead: {Active,Inactive} epan/dissectors/packet-docsis.c ena_dis_tfs - could have used tfs_enabled_disabled from tfs.c instead: {Enabled,Disabled} epan/dissectors/packet-ecmp.c tfs_not_expected_expected - could have used tfs_odd_even from tfs.c instead: {Odd,Even} epan/dissectors/packet-erf.c erf_link_status_tfs - could have used tfs_up_down from tfs.c instead: {Up,Down} epan/dissectors/packet-h263.c on_off_flg - could have used tfs_on_off from tfs.c instead: {On,Off} epan/dissectors/packet-h263.c cpm_flg - could have used tfs_on_off from tfs.c instead: {On,Off} epan/dissectors/packet-interlink.c flags_set_notset - could have used tfs_set_notset from tfs.c instead: {Set,Not set} epan/dissectors/packet-ip.c tos_set_low - could have used tfs_low_normal from tfs.c instead: {Low,Normal} epan/dissectors/packet-ip.c tos_set_high - could have used tfs_high_normal from tfs.c instead: {High,Normal} epan/dissectors/packet-isakmp.c flag_r - could have used tfs_response_request from tfs.c instead: {Response,Request} epan/dissectors/packet-isis-lsp.c tfs_metric_supported_not_supported - could have used tfs_no_yes from tfs.c instead: {No,Yes} epan/dissectors/packet-kerberos.c supported_tfs - could have used tfs_supported_not_supported from tfs.c instead: {Supported,Not supported} epan/dissectors/packet-kerberos.c set_tfs - could have used tfs_set_notset from tfs.c instead: {Set,Not set} epan/dissectors/packet-mac-lte.c mac_lte_scell_status_vals - could have used tfs_activated_deactivated from tfs.c instead: {Activated,Deactivated} epan/dissectors/packet-p_mul.c no_yes - could have used tfs_no_yes from tfs.c instead: {No,Yes} epan/dissectors/packet-pgm.c opts_present - could have used tfs_present_not_present from tfs.c instead: {Present,Not Present} epan/dissectors/packet-rsl.c rsl_ms_fpc_epc_mode_vals - could have used tfs_inuse_not_inuse from tfs.c instead: {In use,Not in use} epan/dissectors/packet-sita.c tfs_sita_on_off - could have used tfs_on_off from tfs.c instead: {On,Off} epan/dissectors/packet-vines.c tfs_vine_rtp_no_yes - could have used tfs_no_yes from tfs.c instead: {No,Yes} epan/dissectors/packet-vnc.c button_mask_tfs - could have used tfs_pressed_not_pressed from tfs.c instead: {Pressed,Not pressed} 27 issues found Change-Id: I7e53b491f20289955c9e9caa8357197d9010a5aa Reviewed-on: https://code.wireshark.org/review/38087 Petri-Dish: Martin Mathieson <martin.r.mathieson@googlemail.com> Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'tools')
-rwxr-xr-xtools/check_tfs.py179
1 files changed, 179 insertions, 0 deletions
diff --git a/tools/check_tfs.py b/tools/check_tfs.py
new file mode 100755
index 0000000000..ecee47db3b
--- /dev/null
+++ b/tools/check_tfs.py
@@ -0,0 +1,179 @@
+#!/usr/bin/env python3
+# Wireshark - Network traffic analyzer
+# By Gerald Combs <gerald@wireshark.org>
+# Copyright 1998 Gerald Combs
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+import os
+import re
+import argparse
+import signal
+
+# This utility scans for tfs items, and works out if standard ones
+# could have been used intead (from epan/tfs.c)
+
+# TODO:
+# - check how many of the definitions in epan/tfs.c are used in other dissectors
+# - see if there are other values that should be in epan/tfs.c and shared
+# - look for leading/trailing whitespace in true/flase strings?
+
+
+# Try to exit soon after Ctrl-C is pressed.
+should_exit = False
+
+def signal_handler(sig, frame):
+ global should_exit
+ should_exit = True
+ print('You pressed Ctrl+C - exiting')
+
+signal.signal(signal.SIGINT, signal_handler)
+
+class TFS:
+ def __init__(self, file, val1, val2):
+ self.file = file
+ self.val1 = val1
+ self.val2 = val2
+
+ def __str__(self):
+ return '{' + self.val1 + ',' + self.val2 + '}'
+
+
+def removeComments(code_string):
+ code_string = re.sub(re.compile("/\*.*?\*/",re.DOTALL ) ,"" ,code_string) # C-style comment
+ code_string = re.sub(re.compile("//.*?\n" ) ,"" ,code_string) # C++-style comment
+ return code_string
+
+
+# Look for hf items in a dissector file.
+def find_items(filename):
+ items = {}
+
+ with open(filename, 'r') as f:
+ contents = f.read()
+ # Example: const true_false_string tfs_true_false = { "True", "False" };
+
+ # Remove comments so as not to trip up RE.
+ contents = removeComments(contents)
+
+ matches = re.finditer(r'.*const\s*true_false_string\s*([a-z_]*)\s*=\s*{\s*\"([a-zA-Z_ ]*)\"\s*,\s*\"([a-zA-Z_ ]*)\"', contents)
+ for m in matches:
+ name = m.group(1)
+ val1 = m.group(2)
+ val2 = m.group(3)
+ # Store this entry.
+ items[name] = TFS(filename, val1, val2)
+
+ return items
+
+
+
+def isDissectorFile(filename):
+ p = re.compile('.*packet-.*\.c')
+ return p.match(filename)
+
+def findDissectorFilesInFolder(folder):
+ # Look at files in sorted order, to give some idea of how far through is.
+ files = []
+
+ for f in sorted(os.listdir(folder)):
+ if should_exit:
+ return
+ if isDissectorFile(f):
+ filename = os.path.join(folder, f)
+ files.append(filename)
+ return files
+
+issues_found = 0
+
+# Check the given dissector file.
+def checkFile(filename, tfs_items):
+ global issues_found
+
+ # Find items.
+ items = find_items(filename)
+
+ # See if any of these items already existed in tfs.c
+ for i in items:
+ for t in tfs_items:
+ if tfs_items[t].val1 == items[i].val1 and tfs_items[t].val2 == items[i].val2:
+ print(filename, i, "- could have used", t, 'from tfs.c instead: ', tfs_items[t])
+ issues_found += 1
+
+
+#################################################################
+# Main logic.
+
+# command-line args. Controls which dissector files should be checked.
+# If no args given, will just scan epan/dissectors folder.
+parser = argparse.ArgumentParser(description='Check calls in dissectors')
+parser.add_argument('--file', action='store', default='',
+ help='specify individual dissector file to test')
+parser.add_argument('--commits', action='store',
+ help='last N commits to check')
+parser.add_argument('--open', action='store_true',
+ help='check open files')
+
+args = parser.parse_args()
+
+
+# Get files from wherever command-line args indicate.
+files = []
+if args.file:
+ # Add single specified file..
+ if not args.file.startswith('epan'):
+ files.append(os.path.join('epan', 'dissectors', args.file))
+ else:
+ files.append(args.file)
+elif args.commits:
+ # Get files affected by specified number of commits.
+ command = ['git', 'diff', '--name-only', 'HEAD~' + args.commits]
+ files = [f.decode('utf-8')
+ for f in subprocess.check_output(command).splitlines()]
+ # Will examine dissector files only
+ files = list(filter(lambda f : isDissectorFile(f), files))
+elif args.open:
+ # Unstaged changes.
+ command = ['git', 'diff', '--name-only']
+ files = [f.decode('utf-8')
+ for f in subprocess.check_output(command).splitlines()]
+ # Only interested in dissector files.
+ files = list(filter(lambda f : isDissectorFile(f), files))
+ # Staged changes.
+ command = ['git', 'diff', '--staged', '--name-only']
+ files_staged = [f.decode('utf-8')
+ for f in subprocess.check_output(command).splitlines()]
+ # Only interested in dissector files.
+ files_staged = list(filter(lambda f : isDissectorFile(f), files_staged))
+ for f in files:
+ files.append(f)
+ for f in files_staged:
+ if not f in files:
+ files.append(f)
+else:
+ # Find all dissector files from folder.
+ files = findDissectorFilesInFolder(os.path.join('epan', 'dissectors'))
+
+
+# If scanning a subset of files, list them here.
+print('Examining:')
+if args.file or args.commits or args.open:
+ if files:
+ print(' '.join(files), '\n')
+ else:
+ print('No files to check.\n')
+else:
+ print('All dissector modules\n')
+
+
+# Get standard/ shared ones.
+tfs_entries = find_items(os.path.join('epan', 'tfs.c'))
+
+# Now check the files to see if they could have used shared ones instead.
+for f in files:
+ if should_exit:
+ exit(1)
+ checkFile(f, tfs_entries)
+
+# Show summary.
+print(issues_found, 'issues found')