b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | # |
| 2 | # tshark module - refactored from test_scan.py |
| 3 | # |
| 4 | # Copyright (c) 2014, Qualcomm Atheros, Inc. |
| 5 | # Copyright (c) 2015, Intel Mobile Communications GmbH |
| 6 | # |
| 7 | # This software may be distributed under the terms of the BSD license. |
| 8 | # See README for more details. |
| 9 | |
| 10 | import time |
| 11 | import subprocess |
| 12 | import logging |
| 13 | logger = logging.getLogger() |
| 14 | |
| 15 | from utils import * |
| 16 | |
| 17 | class UnknownFieldsException(Exception): |
| 18 | def __init__(self, fields): |
| 19 | Exception.__init__(self, "unknown tshark fields %s" % ','.join(fields)) |
| 20 | self.fields = fields |
| 21 | |
| 22 | _tshark_filter_arg = '-Y' |
| 23 | |
| 24 | def _run_tshark(filename, filter, display=None, wait=True): |
| 25 | global _tshark_filter_arg |
| 26 | |
| 27 | if wait: |
| 28 | # wait a bit to make it more likely for wlantest sniffer to have |
| 29 | # captured and written the results into a file that we can process here |
| 30 | time.sleep(0.1) |
| 31 | |
| 32 | try: |
| 33 | arg = ["tshark", "-r", filename, |
| 34 | _tshark_filter_arg, filter] |
| 35 | if display: |
| 36 | arg.append('-Tfields') |
| 37 | for d in display: |
| 38 | arg.append('-e') |
| 39 | arg.append(d) |
| 40 | else: |
| 41 | arg.append('-V') |
| 42 | cmd = subprocess.Popen(arg, stdout=subprocess.PIPE, |
| 43 | stderr=subprocess.PIPE) |
| 44 | except Exception as e: |
| 45 | logger.info("Could run run tshark check: " + str(e)) |
| 46 | if "No such file or directory: 'tshark'" in str(e): |
| 47 | raise HwsimSkip("No tshark available") |
| 48 | cmd = None |
| 49 | return None |
| 50 | |
| 51 | output = cmd.communicate() |
| 52 | out = output[0].decode(errors='ignore') |
| 53 | out1 = output[1].decode() |
| 54 | res = cmd.wait() |
| 55 | if res == 1: |
| 56 | errmsg = "Some fields aren't valid" |
| 57 | if errmsg in out1: |
| 58 | errors = out1.split('\n') |
| 59 | fields = [] |
| 60 | collect = False |
| 61 | for f in errors: |
| 62 | if collect: |
| 63 | f = f.strip() |
| 64 | if f: |
| 65 | fields.append(f) |
| 66 | continue |
| 67 | if errmsg in f: |
| 68 | collect = True |
| 69 | continue |
| 70 | raise UnknownFieldsException(fields) |
| 71 | # remember this for efficiency |
| 72 | _tshark_filter_arg = '-R' |
| 73 | arg[3] = '-R' |
| 74 | cmd = subprocess.Popen(arg, stdout=subprocess.PIPE, |
| 75 | stderr=open('/dev/null', 'w')) |
| 76 | out = cmd.communicate()[0].decode() |
| 77 | cmd.wait() |
| 78 | if res == 2: |
| 79 | if "tshark: Neither" in out1 and "are field or protocol names" in out1: |
| 80 | errors = out1.split('\n') |
| 81 | fields = [] |
| 82 | for f in errors: |
| 83 | if f.startswith("tshark: Neither "): |
| 84 | f = f.split(' ')[2].strip('"') |
| 85 | if f: |
| 86 | fields.append(f) |
| 87 | continue |
| 88 | raise UnknownFieldsException(fields) |
| 89 | |
| 90 | return out |
| 91 | |
| 92 | def run_tshark(filename, filter, display=None, wait=True): |
| 93 | if display is None: display = [] |
| 94 | try: |
| 95 | return _run_tshark(filename, filter.replace('wlan_mgt', 'wlan'), |
| 96 | [x.replace('wlan_mgt', 'wlan') for x in display], |
| 97 | wait) |
| 98 | except UnknownFieldsException as e: |
| 99 | all_wlan_mgt = True |
| 100 | for f in e.fields: |
| 101 | if not f.startswith('wlan_mgt.'): |
| 102 | all_wlan_mgt = False |
| 103 | break |
| 104 | if not all_wlan_mgt: |
| 105 | raise |
| 106 | return _run_tshark(filename, filter, display, wait) |
| 107 | |
| 108 | def run_tshark_json(filename, filter): |
| 109 | arg = ["tshark", "-r", filename, |
| 110 | _tshark_filter_arg, filter] |
| 111 | arg.append('-Tjson') |
| 112 | arg.append('-x') |
| 113 | try: |
| 114 | cmd = subprocess.Popen(arg, stdout=subprocess.PIPE, |
| 115 | stderr=subprocess.PIPE) |
| 116 | except Exception as e: |
| 117 | logger.info("Could run run tshark: " + str(e)) |
| 118 | if "No such file or directory: 'tshark'" in str(e): |
| 119 | raise HwsimSkip("No tshark available") |
| 120 | return None |
| 121 | output = cmd.communicate() |
| 122 | out = output[0].decode() |
| 123 | res = cmd.wait() |
| 124 | return out |