From f0a8a881c259df436edd96846f25d49df247a4ed Mon Sep 17 00:00:00 2001 From: n0mjs710 Date: Tue, 11 Feb 2020 14:38:05 -0600 Subject: [PATCH] debug deconstructor added --- deconstructor.cfg | 87 +++++++++++++++++++++ desconstructor.py | 195 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100755 deconstructor.cfg create mode 100755 desconstructor.py diff --git a/deconstructor.cfg b/deconstructor.cfg new file mode 100755 index 0000000..f6edebe --- /dev/null +++ b/deconstructor.cfg @@ -0,0 +1,87 @@ +# THIS IS A SPECIAL VERSION OF hblink.cfg FOR USE WITH +# hb_deconstructor.py. TO MAKE IT EASIER TO USE THE +# DECONSTRUCTOR, IT WILL DEFAULT TO USE THIS CONFIG FILE +# THIS IS NOT INTENDED FOR NOVICES. DON'T TRY TO USE +# THIS AND POST FOR HELP ON THE DVSWITCH FORUMS. CONTACT +# THE AUTHOR DIRECTLY. + +[GLOBAL] +PATH: ./ +PING_TIME: 5 +MAX_MISSED: 3 +USE_ACL: True +REG_ACL: PERMIT:ALL +SUB_ACL: DENY:1 +TGID_TS1_ACL: PERMIT:ALL +TGID_TS2_ACL: PERMIT:ALL + +[REPORTS] +REPORT: False +REPORT_INTERVAL: 60 +REPORT_PORT: 4321 +REPORT_CLIENTS: 127.0.0.1 + +[LOGGER] +LOG_FILE: +LOG_HANDLERS: console-timed +LOG_LEVEL: INFO +LOG_NAME: DECON + +[ALIASES] +TRY_DOWNLOAD: False +PATH: ./ +PEER_FILE: peer_ids.json +SUBSCRIBER_FILE: subscriber_ids.json +TGID_FILE: talkgroup_ids.json +PEER_URL: +SUBSCRIBER_URL: +STALE_DAYS: 10000 + +[MASTER] +MODE: MASTER +ENABLED: False +REPEAT: True +MAX_PEERS: 10 +EXPORT_AMBE: False +IP: +PORT: 62071 +PASSPHRASE: 1234 +GROUP_HANGTIME: 5 +USE_ACL: False +REG_ACL: PERMIT:ALL +SUB_ACL: PERMIT:ALL +TGID_TS1_ACL: PERMIT:ALL +TGID_TS2_ACL: PERMIT:ALL + + +[PEER] +MODE: PEER +ENABLED: True +LOOSE: False +EXPORT_AMBE: False +IP: +PORT: 54001 +MASTER_IP: olympic.k0usy.org +MASTER_PORT: 62071 +PASSPHRASE: c0ffee +CALLSIGN: DECONSTRUCTOR +RADIO_ID: 666 +RX_FREQ: 444000001 +TX_FREQ: 444000000 +TX_POWER: 1 +COLORCODE: 1 +SLOTS: 3 +LATITUDE: 000.0000 +LONGITUDE: -000.0000 +HEIGHT: 1 +LOCATION: Anywhere, USA +DESCRIPTION: HBP/DMR Deconstructor +URL: www.w1abc.org +SOFTWARE_ID: 20170620 +PACKAGE_ID: MMDVM_HBlink +GROUP_HANGTIME: 5 +OPTIONS: +USE_ACL: False +SUB_ACL: PERMIT:ALL +TGID_TS1_ACL: PERMIT:ALL +TGID_TS2_ACL: PERMIT:ALL \ No newline at end of file diff --git a/desconstructor.py b/desconstructor.py new file mode 100755 index 0000000..345db1d --- /dev/null +++ b/desconstructor.py @@ -0,0 +1,195 @@ +#!/usr/bin/env python +# +############################################################################### +# Copyright (C) 2016-2019 Cortney T. Buffington, N0MJS +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +############################################################################### + +''' +This tool listens to network traffic and breaks apart HBP and DMR packets. + +It is a development and debugging tool and has no purpose in actual production +DMR networking systems. +''' + +# Python modules we need +from binascii import b2a_hex as bhex +from bitarray import bitarray + +# Twisted is pretty important, so I keep it separate +from twisted.internet.protocol import Factory, Protocol +from twisted.internet import reactor + +# Things we import from the main hblink module +from hblink import HBSYSTEM, systems, hblink_handler +from dmr_utils3.utils import bytes_3, bytes_4, int_id +from dmr_utils3 import bptc +import config +import log +from const import * + + +# REMOVE LATER from datetime import datetime +# The module needs logging, but handlers, etc. are controlled by the parent +import logging +logger = logging.getLogger(__name__) + + +# Does anybody read this stuff? There's a PEP somewhere that says I should do this. +__author__ = 'Cortney T. Buffington, N0MJS' +__copyright__ = 'Copyright (c) 2016-2019 Cortney T. Buffington, N0MJS and the K0USY Group' +__license__ = 'GNU GPLv3' +__maintainer__ = 'Cort Buffington, N0MJS' +__email__ = 'n0mjs@me.com' + + +DUMP_HBP = False +DUMP_DMR = True + +# Module gobal varaibles + +class HBP(HBSYSTEM): + + def __init__(self, _name, _config, _report): + HBSYSTEM.__init__(self, _name, _config, _report) + self.last_stream = b'\x00\x00\x00\x00' + self.lcfrags = 0 + self.lostcount = 0 + + + def dmrd_received(self, _peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): + + pktype = 'UNIDENTIFIED' + + if (_frame_type == HBPF_DATA_SYNC) and (_dtype_vseq == HBPF_SLT_VTERM): + pktype = 'VTERM ' + + if (_frame_type == HBPF_DATA_SYNC) and (_dtype_vseq == HBPF_SLT_VHEAD): + pktype = 'VHEAD ' + + if (_frame_type == HBPF_VOICE_SYNC): + pktype = 'VOICE SYNC ' + str(_dtype_vseq + 1) + + if (_frame_type == HBPF_VOICE): + pktype = 'VOICE ' + str(_dtype_vseq + 1) + + if DUMP_HBP: + print('STREAM: {} SEQ: {} PEER: {} SRC: {} DST: {} SLOT: {} CALL: {} FRAME: {} DTYPE: {}'.format( + int_id(_stream_id), int_id(_seq), int_id(_peer_id), int_id(_rf_src), int_id(_dst_id), _slot, pktype, _frame_type, _dtype_vseq + )) + + if DUMP_DMR: + payload = _data[20:53] + bitload = bitarray(endian='big') + bitload.frombytes(payload) + if pktype == ('VHEAD '): + LC = bptc.decode_full_lc(bitload[:98] + bitload[-98:]).tobytes() + OPT = bhex(LC[0:3]) + DST = int_id(LC[3:6]) + SRC = int_id(LC[6:9]) + + ST = bin(int.from_bytes((bitload[98:109] + bitload[-108:-99])[:8].tobytes(), 'big')) + SC = bhex(bitload[108:-108].tobytes()) + + print('VOICE HEADER: LC:(OPTIONS: {} DEST: {} SOURCE: {}) -- SLOT TYPE: {} -- SYNC: {}'.format(OPT, DST, SRC, ST, SC)) + + elif pktype == ('VTERM '): + LC = bptc.decode_full_lc(bitload[:98] + bitload[-98:]).tobytes() + OPT = bhex(LC[0:3]) + DST = int_id(LC[3:6]) + SRC = int_id(LC[6:9]) + + ST = bin(int.from_bytes((bitload[98:109] + bitload[-108:-99])[:8].tobytes(), 'big')) + SC = bhex(bitload[108:-108].tobytes()) + + print('VOICE TERMINATOR: LC:(OPTIONS: {} DEST: {} SOURCE: {}) -- SLOT TYPE: {} -- SYNC: {}'.format(OPT, DST, SRC, ST, SC)) + + elif pktype == ('VOICE SYNC 1'): + self.lcfrags=0 + print('Voice Burst A') + BVC = bitload[0:108] + bitload[-108:] + BSC = bitload[108:-108] + self.lcfrags = 0 + + elif 'VOICE ' in pktype: + print('Voice Burst B-F') + BVC = bitload[0:108] + bitload[-108:] + BEB = bitload[108:116] + bitload[-116:-108] + BES = bitload[116:-116] + print(len(BVC), len(BEB), len(BES)) + + +#************************************************ +# MAIN PROGRAM LOOP STARTS HERE +#************************************************ + +if __name__ == '__main__': + import argparse + import sys + import os + import signal + + # Change the current directory to the location of the application + os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) + + # CLI argument parser - handles picking up the config file from the command line, and sending a "help" message + parser = argparse.ArgumentParser() + parser.add_argument('-c', '--config', action='store', dest='CONFIG_FILE', help='/full/path/to/config.file (usually deconstructor.cfg)') + parser.add_argument('-l', '--logging', action='store', dest='LOG_LEVEL', help='Override config file logging level.') + cli_args = parser.parse_args() + + # Ensure we have a path for the config file, if one wasn't specified, then use the default (top of file) + if not cli_args.CONFIG_FILE: + cli_args.CONFIG_FILE = os.path.dirname(os.path.abspath(__file__))+'/deconstructor.cfg' + + # Call the external routine to build the configuration dictionary + CONFIG = config.build_config(cli_args.CONFIG_FILE) + + # Start the system logger + if cli_args.LOG_LEVEL: + CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL + logger = log.config_logging(CONFIG['LOGGER']) + logger.info('\n\nCopyright (c) 2013, 2014, 2015, 2016, 2018, 2019\n\tThe Regents of the K0USY Group. All rights reserved.\n') + logger.debug('(GLOBAL) Logging system started, anything from here on gets logged') + + # Set up the signal handler + def sig_handler(_signal, _frame): + logger.info('(GLOBAL) SHUTDOWN: DECONSTRUCTOR IS TERMINATING WITH SIGNAL %s', str(_signal)) + hblink_handler(_signal, _frame) + logger.info('(GLOBAL) SHUTDOWN: ALL SYSTEM HANDLERS EXECUTED - STOPPING REACTOR') + reactor.stop() + + # Set signal handers so that we can gracefully exit if need be + for sig in [signal.SIGINT, signal.SIGTERM]: + signal.signal(sig, sig_handler) + + # INITIALIZE THE REPORTING LOOP + report_server = None + + # HBlink instance creation + logger.info('(GLOBAL) HBlink \'hbp_deconstructor.py\' -- SYSTEM STARTING...') + for system in CONFIG['SYSTEMS']: + if CONFIG['SYSTEMS'][system]['ENABLED']: + if CONFIG['SYSTEMS'][system]['MODE'] == 'MASTER' or CONFIG['SYSTEMS'][system]['MODE'] == 'PEER': + systems[system] = HBP(system, CONFIG, report_server) + reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) + logger.debug('(GLOBAL) %s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) + + def loopingErrHandle(failure): + logger.error('(GLOBAL) STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error in timed loop.\n %s', failure) + reactor.stop() + + reactor.run() \ No newline at end of file