diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/acl.py b/acl.py deleted file mode 100644 index 8eeba5f..0000000 --- a/acl.py +++ /dev/null @@ -1,104 +0,0 @@ -############################################################################### -# Copyright (C) 2018 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 -############################################################################### - -from dmr_utils.utils import int_id - -# Lowest possible Subscirber and/or talkgroup IDs allowed by ETSI standard -ID_MIN = 1 -ID_MAX = 16776415 - - -# Checks the supplied ID against the ID given, and the ACL list, and the action -# Returns True if the ID should be allowed, False if it should not be -def acl_check(_id, _acl): - id = int_id(_id) - for entry in _acl[1]: - if entry[0] <= id <= entry[1]: - return _acl[0] - return not _acl[0] - - -def acl_build(_acl): - if not _acl: - return(True, set((ID_MIN, ID_MAX))) - - acl = set() - sections = _acl.split(':') - - if sections[0] == 'PERMIT': - action = True - else: - action = False - - for entry in sections[1].split(','): - if entry == 'ALL': - acl.add((ID_MIN, ID_MAX)) - break - - elif '-' in entry: - start,end = entry.split('-') - start,end = int(start), int(end) - if (ID_MIN <= start <= ID_MAX) or (ID_MIN <= end <= ID_MAX): - acl.add((start, end)) - else: - pass #logger message here - else: - id = int(entry) - if (ID_MIN <= id <= ID_MAX): - acl.add((id, id)) - else: - pass #logger message here - - return (action, acl) - - -if __name__ == '__main__': - from time import time - from pprint import pprint - - ACL = { - 'SUB': { - 'K0USY': { - 1: 'PERMIT:1-5,3120101,3120124', - 2: 'DENY:1-5,3120101,3120124' - } - }, - 'TGID': { - 'GLOBAL': { - 1: 'PERMIT:ALL', - 2: 'DENY:ALL' - }, - 'K0USY': { - 1: 'PERMIT:1-5,3120,31201', - 2: 'DENY:1-5,3120,31201' - } - } - } - - for acl in ACL: - if 'GLOBAL' not in ACL[acl]: - ACL[acl].update({'GLOBAL': {1:'PERMIT:ALL',2:'PERMIT:ALL'}}) - for acltype in ACL[acl]: - for slot in ACL[acl][acltype]: - ACL[acl][acltype][slot] = acl_build(ACL[acl][acltype][slot]) - - pprint(ACL) - print - - print(acl_check('\x00\x00\x01', ACL['TGID']['GLOBAL'][1])) - print(acl_check('\x00\x00\x01', ACL['TGID']['K0USY'][2])) \ No newline at end of file diff --git a/hb_bridge_all.py b/hb_bridge_all.py index c371d0a..472d172 100755 --- a/hb_bridge_all.py +++ b/hb_bridge_all.py @@ -45,14 +45,18 @@ from twisted.protocols.basic import NetstringReceiver from twisted.internet import reactor, task # Things we import from the main hblink module -from hblink import HBSYSTEM, systems, hblink_handler, reportFactory, REPORT_OPCODES, config_reports, build_reg_acl +from hblink import HBSYSTEM, OPENBRIDGE, systems, hblink_handler, reportFactory, REPORT_OPCODES, config_reports, mk_aliases from dmr_utils.utils import hex_str_3, int_id, get_alias from dmr_utils import decode, bptc, const -from acl import acl_check, acl_build import hb_config import hb_log import hb_const +# The module needs logging 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-2018 Cortney T. Buffington, N0MJS and the K0USY Group' @@ -65,23 +69,10 @@ __status__ = 'pre-alpha' # Module gobal varaibles -# Import rules -- at this point, just ACLs -def import_rules(_rules): - try: - rules_file = import_module(_rules) - logger.info('Rules file found and bridges imported') - return rules_file - except ImportError: - logger.info('Rules file not found. Initializing defaults') - rules_file = ModuleType('rules_file') - rules_file.ACL = {'SID':{}, 'TGID':{}} - return rules_file - - class bridgeallSYSTEM(HBSYSTEM): - def __init__(self, _name, _config, _logger, _report): - HBSYSTEM.__init__(self, _name, _config, _logger, _report) + def __init__(self, _name, _config, _report): + HBSYSTEM.__init__(self, _name, _config, _report) # Status information for the system, TS1 & TS2 # 1 & 2 are "timeslot" @@ -140,32 +131,6 @@ class bridgeallSYSTEM(HBSYSTEM): if _call_type == 'group': - # Check for GLOBAL Subscriber ID ACL Match - if acl_check(_rf_src, ACL['SID']['GLOBAL'][_slot]) == False: - if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY INGRESS GLOBAL ACL*** SID: %s SLOT: %s HBP Peer %s', self._system, int_id(_rf_src), _slot, int_id(_peer_id)) - self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id - return - # Check for SYSTEM Subscriber ID ACL Match - if acl_check(_rf_src, ACL['SID'][self._system][_slot]) == False: - if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY INGRESS SYSTEM ACL*** SID: %s SLOT: %s HBP Peer %s', self._system, int_id(_rf_src), _slot, int_id(_peer_id)) - self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id - return - - # Check for GLOBAL Talkgroup ID ACL Match - if acl_check(_dst_id, ACL['TGID']['GLOBAL'][_slot]) == False: - if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY INGRESS GLOBAL ACL*** TGID: %s SLOT: %s HBP Peer %s', self._system, int_id(_dst_id), _slot, int_id(_peer_id)) - self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id - return - # Check for SYSTEM Talkgroup ID ID ACL Match - if acl_check(_dst_id, ACL['TGID'][self._system][_slot]) == False: - if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY INGRESS SYSTEM ACL*** TGID: %s SLOT: %s HBP Peer %s', self._system, int_id(_dst_id), _slot, int_id(_peer_id)) - self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id - return - # Is this is a new call stream? if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): self.STATUS['RX_START'] = pkt_time @@ -191,39 +156,45 @@ class bridgeallSYSTEM(HBSYSTEM): _target_status = systems[_target].STATUS _target_system = self._CONFIG['SYSTEMS'][_target] - - # Check for GLOBAL Subscriber ID ACL Match - if acl_check(_rf_src, ACL['SID']['GLOBAL'][_slot]) == False: - if (_stream_id != _target_status[_slot]['TX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY EGRESS GLOBAL ACL*** SID: %s SLOT: %s HBP Peer %s', _target, int_id(_rf_src), _slot, int_id(_peer_id)) - _target_status[_slot]['TX_STREAM_ID'] = _stream_id - return - # Check for SYSTEM Subscriber ID ACL Match - if acl_check(_rf_src, ACL['SID'][_target][_slot]) == False: - if (_stream_id != _target_status[_slot]['TX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY EGRESS SYSTEM ACL*** SID: %s SLOT: %s HBP Peer %s', _target, int_id(_rf_src), _slot, int_id(_peer_id)) - _target_status[_slot]['TX_STREAM_ID'] = _stream_id - return - - # Check for GLOBAL Talkgroup ID ACL Match - if acl_check(_dst_id, ACL['TGID']['GLOBAL'][_slot]) == False: - if (_stream_id != _target_status[_slot]['TX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY EGRESS GLOBAL ACL*** TGID: %s SLOT: %s HBP Peer %s', _target, int_id(_dst_id), _slot, int_id(_peer_id)) - _target_status[_slot]['TX_STREAM_ID'] = _stream_id - return - # Check for SYSTEM Talkgroup ID ID ACL Match - if acl_check(_dst_id, ACL['TGID'][_target][_slot]) == False: - if (_stream_id != _target_status[_slot]['TX_STREAM_ID']): - self._logger.warning('(%s) Group Voice Call ***REJECTED BY EGRESS SYSTEM ACL*** TGID: %s HBP Peer %s', _target, int_id(_dst_id), int_id(_peer_id)) - _target_status[_slot]['TX_STREAM_ID'] = _stream_id - return + _target_status[_slot]['TX_STREAM_ID'] = _stream_id + + # ACL Processing + if self._CONFIG['GLOBAL']['USE_ACL']: + if not acl_check(_rf_src, self._CONFIG['GLOBAL']['SUB_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s FROM SUBSCRIBER %s BY GLOBAL ACL', _target_system, int_id(_stream_id), int_id(_rf_src)) + self._laststrid = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG1_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s ON TGID %s BY GLOBAL TS1 ACL', _target_system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG2_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s ON TGID %s BY GLOBAL TS2 ACL', _target_system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid = _stream_id + return + if self._target_system['USE_ACL']: + if not acl_check(_rf_src, _target_system['SUB_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s FROM SUBSCRIBER %s BY SYSTEM ACL', _target_system, int_id(_stream_id), int_id(_rf_src)) + self._laststrid = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, _target_system['TG1_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s ON TGID %s BY SYSTEM TS1 ACL', _target_system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, _target_system['TG2_ACL']): + if self._laststrid != _stream_id: + self._logger.debug('(%s) CALL DROPPED ON EGRESS WITH STREAM ID %s ON TGID %s BY SYSTEM TS2 ACL', _target_system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid = _stream_id + return + self._laststrid = _stream_id systems[_target].send_system(_data) #self._logger.debug('(%s) Packet routed to system: %s', self._system, _target) - - - - #************************************************ @@ -257,81 +228,35 @@ if __name__ == '__main__': if cli_args.LOG_LEVEL: CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL logger = hb_log.config_logging(CONFIG['LOGGER']) + logger.info('\n\nCopyright (c) 2013, 2014, 2015, 2016, 2018\n\tThe Founding Members of the K0USY Group. All rights reserved.\n') logger.debug('Logging system started, anything from here on gets logged') # Set up the signal handler def sig_handler(_signal, _frame): logger.info('SHUTDOWN: HBROUTER IS TERMINATING WITH SIGNAL %s', str(_signal)) - hblink_handler(_signal, _frame, logger) + hblink_handler(_signal, _frame) logger.info('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.SIGTERM, signal.SIGINT]: signal.signal(sig, sig_handler) - - # Build the Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) - - # ID ALIAS CREATION - # Download - if CONFIG['ALIASES']['TRY_DOWNLOAD'] == True: - # Try updating peer aliases file - result = try_download(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['PEER_FILE'], CONFIG['ALIASES']['PEER_URL'], CONFIG['ALIASES']['STALE_TIME']) - logger.info(result) - # Try updating subscriber aliases file - result = try_download(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE'], CONFIG['ALIASES']['SUBSCRIBER_URL'], CONFIG['ALIASES']['STALE_TIME']) - logger.info(result) - - # Make Dictionaries - peer_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['PEER_FILE']) - if peer_ids: - logger.info('ID ALIAS MAPPER: peer_ids dictionary is available') - - subscriber_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE']) - if subscriber_ids: - logger.info('ID ALIAS MAPPER: subscriber_ids dictionary is available') - - talkgroup_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['TGID_FILE']) - if talkgroup_ids: - logger.info('ID ALIAS MAPPER: talkgroup_ids dictionary is available') - - # Import rules file - rules_file = import_rules('hb_bridge_all_rules') - - # Create ACLs - ACL = rules_file.ACL - - for acl_type in ACL: - if acl_type != 'SID' and acl_type != 'TGID': - sys.exit(('TERMINATE: SID or TGID stanzas not in ACL!!! Exiting to save you grief later')) - - if 'GLOBAL' not in ACL[acl_type]: - ACL[acl_type].update({'GLOBAL': {1:'PERMIT:ALL',2:'PERMIT:ALL'}}) - - for system_acl in ACL[acl_type]: - if system_acl not in CONFIG['SYSTEMS'] and system_acl != 'GLOBAL': - sys.exit(('TERMINATE: {} ACL configured for system {} that does not exist!!! Exiting to save you grief later'.format(acl_type, system_acl))) - for slot in ACL[acl_type][system_acl]: - ACL[acl_type][system_acl][slot] = acl_build(ACL[acl_type][system_acl][slot]) - - for system in CONFIG['SYSTEMS']: - for acl_type in ACL: - if system not in ACL[acl_type]: - logger.warning('No %s ACL for system %s - initializing \'PERMIT:ALL\'', acl_type, system) - ACL[acl_type].update({system: {1: acl_build('PERMIT:ALL'), 2: acl_build('PERMIT:ALL')}}) - - # Build the Registration Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) - + + # Create the name-number mapping dictionaries + peer_ids, subscriber_ids, talkgroup_ids = mk_aliases(CONFIG) + # INITIALIZE THE REPORTING LOOP - report_server = config_reports(CONFIG, logger, reportFactory) - + report_server = config_reports(CONFIG, reportFactory) + # HBlink instance creation - logger.info('HBlink \'hb_bridge_all.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') + logger.info('HBlink \'hb_bridge_all.py\' -- SYSTEM STARTING...') for system in CONFIG['SYSTEMS']: if CONFIG['SYSTEMS'][system]['ENABLED']: - systems[system] = bridgeallSYSTEM(system, CONFIG, logger, report_server) + if CONFIG['SYSTEMS'][system]['MODE'] == 'OPENBRIDGE': + logger.critical('%s FATAL: Instance is mode \'OPENBRIDGE\', \n\t\t...Which would be tragic for Bridge All, since it carries multiple call\n\t\tstreams simultaneously. hb_bridge_all.py onlyl works with MMDVM-based systems', system) + sys.exit('hb_bridge_all.py cannot function with systems that are not MMDVM devices. System {} is configured as an OPENBRIDGE'.format(system)) + else: + systems[system] = HBSYSTEM(system, CONFIG, report_server) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) diff --git a/hb_bridge_all_rules_SAMPLE.py b/hb_bridge_all_rules_SAMPLE.py deleted file mode 100644 index e06039a..0000000 --- a/hb_bridge_all_rules_SAMPLE.py +++ /dev/null @@ -1,62 +0,0 @@ -# ACL Entries -# -# The 'action' May be PERMIT|DENY -# Each entry may be a single radio id, a hypenated range (e.g. 1-2999), or the string 'ALL'. -# if "ALL" is used, you may not include any other ranges or individual IDs. -# Format: -# ACL = 'action:id|start-end|,id|start-end,....' -# -# Sections exist for both TGIDs and Subscriber IDs. -# Sections exist for glboal actions, and per-system actions. -# ***FIRST MATCH EXITS*** - -# SID - Subscriber ID section. -# TGID - Talkgroup ID section. -# -# "GLOBAL" affects ALL systems -# "SYSTEM NAME" affects the system in quetion -# ACLs are applied both ingress AND egress -# If you omit GLOBAL or SYSTEM level ACLs, they will be initilzied -# automatically as "PERMIT:ALL" -# Each system (or global) has two sections 1 and 2, which correspond -# to timeslots 1 and 2 respectively -# -# EXAMPLE: -#ACL = { -# 'SID': { -# 'GLOBAL': { -# 1: 'PERMIT:ALL', -# 2: 'PERMIT:ALL' -# }, -# 'LINK': { -# 1: 'DENY:3120121', -# 2: 'PERMIT:ALL' -# } -# }, -# 'TGID': { -# 'GLOBAL': { -# 1: 'PERMIT:ALL', -# 2: 'PERMIT:ALL' -# }, -# 'LINK': { -# 1: 'DENY:1-5,1616', -# 2: 'PERMIT:3120' -# } -# } -#} - -ACL = { - 'SID': { - 'GLOBAL': { - 1: 'PERMIT:ALL', - 2: 'PERMIT:ALL' - } - }, - 'TGID': { - 'GLOBAL': { - 1: 'PERMIT:ALL', - 2: 'PERMIT:ALL' - } - } -} - diff --git a/hb_confbridge.py b/hb_confbridge.py index cfa0c32..73c142a 100755 --- a/hb_confbridge.py +++ b/hb_confbridge.py @@ -45,7 +45,7 @@ from twisted.protocols.basic import NetstringReceiver from twisted.internet import reactor, task # Things we import from the main hblink module -from hblink import HBSYSTEM, systems, hblink_handler, reportFactory, REPORT_OPCODES, config_reports, build_reg_acl +from hblink import HBSYSTEM, OPENBRIDGE, systems, hblink_handler, reportFactory, REPORT_OPCODES, mk_aliases from dmr_utils.utils import hex_str_3, int_id, get_alias from dmr_utils import decode, bptc, const import hb_config @@ -55,20 +55,46 @@ import hb_const # Stuff for socket reporting import cPickle as pickle +# The module needs logging 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-2018PEER Cortney T. Buffington, N0MJS and the K0USY Group' +__copyright__ = 'Copyright (c) 2016-2018 Cortney T. Buffington, N0MJS and the K0USY Group' __credits__ = 'Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman, N4IRR; Jonathan Naylor, G4KLX; Hans Barthen, DL5DI; Torsten Shultze, DG1HT' __license__ = 'GNU GPLv3' __maintainer__ = 'Cort Buffington, N0MJS' __email__ = 'n0mjs@me.com' -__status__ = 'pre-alpha' # Module gobal varaibles +# Timed loop used for reporting HBP status +# +# REPORT BASED ON THE TYPE SELECTED IN THE MAIN CONFIG FILE +def config_reports(_config, _factory): + if True: #_config['REPORTS']['REPORT']: + def reporting_loop(logger, _server): + logger.debug('Periodic reporting loop started') + _server.send_config() + _server.send_bridge() + + logger.info('HBlink TCP reporting server configured') + + report_server = _factory(_config) + report_server.clients = [] + reactor.listenTCP(_config['REPORTS']['REPORT_PORT'], report_server) + + reporting = task.LoopingCall(reporting_loop, logger, report_server) + reporting.start(_config['REPORTS']['REPORT_INTERVAL']) + + return report_server + + # Import Bridging rules # Note: A stanza *must* exist for any MASTER or CLIENT configured in the main -# configuration file and listed as "active". It can be empty, +# configuration file and listed as "active". It can be empty, # but it has to exist. def make_bridges(_hb_confbridge_bridges): try: @@ -76,80 +102,30 @@ def make_bridges(_hb_confbridge_bridges): logger.info('Routing bridges file found and bridges imported') except ImportError: sys.exit('Routing bridges file not found or invalid') - + # Convert integer GROUP ID numbers from the config into hex strings # we need to send in the actual data packets. for _bridge in bridge_file.BRIDGES: for _system in bridge_file.BRIDGES[_bridge]: if _system['SYSTEM'] not in CONFIG['SYSTEMS']: sys.exit('ERROR: Conference bridges found for system not configured main configuration') - + _system['TGID'] = hex_str_3(_system['TGID']) for i, e in enumerate(_system['ON']): _system['ON'][i] = hex_str_3(_system['ON'][i]) for i, e in enumerate(_system['OFF']): _system['OFF'][i] = hex_str_3(_system['OFF'][i]) _system['TIMEOUT'] = _system['TIMEOUT']*60 - _system['TIMER'] = time() + _system['TIMEOUT'] - + if _system['ACTIVE'] == True: + _system['TIMER'] = time() + _system['TIMEOUT'] + else: + _system['TIMER'] = time() return bridge_file.BRIDGES - - -# Import subscriber ACL -# ACL may be a single list of subscriber IDs -# Global action is to allow or deny them. Multiple lists with different actions and ranges -# are not yet implemented. -def build_acl(_sub_acl): - ACL = set() - try: - acl_file = import_module(_sub_acl) - logger.info('ACL file found, importing entries. This will take about 1.5 seconds per 1 million IDs') - sections = acl_file.ACL.split(':') - ACL_ACTION = sections[0] - entries_str = sections[1] - - - for entry in entries_str.split(','): - if '-' in entry: - start,end = entry.split('-') - start,end = int(start), int(end) - for id in range(start, end+1): - ACL.add(hex_str_3(id)) - else: - id = int(entry) - ACL.add(hex_str_3(id)) - - logger.info('ACL loaded: action "{}" for {:,} radio IDs'.format(ACL_ACTION, len(ACL))) - except ImportError: - logger.info('ACL file not found or invalid - all subscriber IDs are valid') - ACL_ACTION = 'NONE' - - # Depending on which type of ACL is used (PERMIT, DENY... or there isn't one) - # define a differnet function to be used to check the ACL - global allow_sub - if ACL_ACTION == 'PERMIT': - def allow_sub(_sub): - if _sub in ACL: - return True - else: - return False - elif ACL_ACTION == 'DENY': - def allow_sub(_sub): - if _sub not in ACL: - return True - else: - return False - else: - def allow_sub(_sub): - return True - - return ACL - # Run this every minute for rule timer updates def rule_timer_loop(): - logger.info('(ALL HBSYSTEMS) Rule timer loop started') + logger.debug('(ALL HBSYSTEMS) Rule timer loop started') _now = time() for _bridge in BRIDGES: @@ -180,17 +156,256 @@ def rule_timer_loop(): if CONFIG['REPORTS']['REPORT']: report_server.send_clients('bridge updated') -class routerSYSTEM(HBSYSTEM): - - def __init__(self, _name, _config, _logger, _report): - HBSYSTEM.__init__(self, _name, _config, _logger, _report) - + +# run this every 10 seconds to trim orphaned stream ids +def stream_trimmer_loop(): + logger.debug('(ALL OPENBRIDGE SYSTEMS) Trimming inactive stream IDs from system lists') + _now = time() + + for system in systems: + # HBP systems, master and peer + if CONFIG['SYSTEMS'][system]['MODE'] != 'OPENBRIDGE': + for slot in range(1,3): + _slot = systems[system].STATUS[slot] + if _slot['RX_TYPE'] != hb_const.HBPF_SLT_VTERM and _slot['RX_TIME'] < _now - 5: + _slot['RX_TYPE'] = hb_const.HBPF_SLT_VTERM + logger.info('(%s) *TIME OUT* STREAM ID: %s SUB: %s TGID %s, TS %s, Duration: %s', \ + system, int_id(_slot['RX_STREAM_ID']), int_id(_slot['RX_RFS']), int_id(_slot['RX_TGID']), slot, _slot['RX_TIME'] - _slot['RX_START']) + # OBP systems + # We can't delete items from a dicationry that's being iterated, so we have to make a temporarly list of entrys to remove later + if CONFIG['SYSTEMS'][system]['MODE'] == 'OPENBRIDGE': + remove_list = [] + for stream_id in systems[system].STATUS: + if systems[system].STATUS[stream_id]['LAST'] < _now - 5: + remove_list.append(stream_id) + for stream_id in remove_list: + if stream_id in systems[system].STATUS: + _system = systems[system].STATUS[stream_id] + _config = CONFIG['SYSTEMS'][system] + if systems[system].STATUS[stream_id]['REMOVE'] == True: + logger.debug('(%s) *REMOVE ENDED* STREAM ID: %s SUB: %s PEER: %s TGID: %s TS 1 Duration: %s', \ + system, int_id(stream_id), get_alias(int_id(_system['RFS']), subscriber_ids), get_alias(int_id(_config['NETWORK_ID']), peer_ids), get_alias(int_id(_system['TGID']), talkgroup_ids), _system['LAST'] - _system['START']) + else: + logger.info('(%s) *TIME OUT* STREAM ID: %s SUB: %s PEER: %s TGID: %s TS 1 Duration: %s', \ + system, int_id(stream_id), get_alias(int_id(_system['RFS']), subscriber_ids), get_alias(int_id(_config['NETWORK_ID']), peer_ids), get_alias(int_id(_system['TGID']), talkgroup_ids), _system['LAST'] - _system['START']) + # self._report.send_bridgeEvent('GROUP VOICE,END,{},{},{},{},{},{},{:.2f}'.format(self._system, int_id(_stream_id), int_id(_peer_id), int_id(_rf_src), _slot, int_id(_dst_id), call_duration)) + removed = systems[system].STATUS.pop(stream_id) + else: + logger.error('(%s) Attemped to remove OpenBridge Stream ID %s not in the Stream ID list: %s', system, int_id(stream_id), [id for id in systems[system].STATUS]) + +class routerOBP(OPENBRIDGE): + + def __init__(self, _name, _config, _report): + OPENBRIDGE.__init__(self, _name, _config, _report) + self.STATUS = {} + + + def dmrd_received(self, _peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): + pkt_time = time() + dmrpkt = _data[20:53] + _bits = int_id(_data[15]) + + if _call_type == 'group': + # Is this a new call stream? + if (_stream_id not in self.STATUS): + # This is a new call stream + self.STATUS[_stream_id] = { + 'START': pkt_time, + 'CONTENTION':False, + 'RFS': _rf_src, + 'TGID': _dst_id, + 'REMOVE': False + } + + # If we can, use the LC from the voice header as to keep all options intact + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + decoded = decode.voice_head_term(dmrpkt) + self.STATUS[_stream_id]['LC'] = decoded['LC'] + + # If we don't have a voice header then don't wait to decode the Embedded LC + # just make a new one from the HBP header. This is good enough, and it saves lots of time + else: + self.STATUS[_stream_id]['LC'] = const.LC_OPT + _dst_id + _rf_src + + + logger.info('(%s) *CALL START* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s', \ + self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot) + if CONFIG['REPORTS']['REPORT']: + self._report.send_bridgeEvent('GROUP VOICE,START,{},{},{},{},{},{}'.format(self._system, int_id(_stream_id), int_id(_peer_id), int_id(_rf_src), _slot, int_id(_dst_id))) + + + self.STATUS[_stream_id]['LAST'] = pkt_time + + + for _bridge in BRIDGES: + for _system in BRIDGES[_bridge]: + + if (_system['SYSTEM'] == self._system and _system['TGID'] == _dst_id and _system['TS'] == _slot and _system['ACTIVE'] == True): + + for _target in BRIDGES[_bridge]: + if (_target['SYSTEM'] != self._system) and (_target['ACTIVE']): + _target_status = systems[_target['SYSTEM']].STATUS + _target_system = self._CONFIG['SYSTEMS'][_target['SYSTEM']] + if _target_system['MODE'] == 'OPENBRIDGE': + # Is this a new call stream on the target? + if (_stream_id not in _target_status): + # This is a new call stream on the target + _target_status[_stream_id] = { + 'START': pkt_time, + 'CONTENTION':False, + 'RFS': _rf_src, + 'TGID': _dst_id, + 'REMOVE': False + } + # If we can, use the LC from the voice header as to keep all options intact + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + decoded = decode.voice_head_term(dmrpkt) + _target_status[_stream_id]['LC'] = decoded['LC'] + logger.debug('(%s) Created LC for OpenBridge destination: System: %s, TGID: %s', self._system, _target['SYSTEM'], int_id(_target['TGID'])) + + # If we don't have a voice header then don't wait to decode the Embedded LC + # just make a new one from the HBP header. This is good enough, and it saves lots of time + else: + _target_status[_stream_id]['LC'] = const.LC_OPT + _dst_id + _rf_src + logger.info('(%s) Created LC with *LATE ENTRY* for OpenBridge destination: System: %s, TGID: %s', self._system, _target['SYSTEM'], int_id(_target['TGID'])) + + _target_status[_stream_id]['H_LC'] = bptc.encode_header_lc(_target_status[_stream_id]['LC']) + _target_status[_stream_id]['T_LC'] = bptc.encode_terminator_lc(_target_status[_stream_id]['LC']) + _target_status[_stream_id]['EMB_LC'] = bptc.encode_emblc(_target_status[_stream_id]['LC']) + logger.info('(%s) Conference Bridge: %s, Call Bridged to OBP System: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + # Record the time of this packet so we can later identify a stale stream + _target_status[_stream_id]['LAST'] = pkt_time + # Clear the TS bit -- all OpenBridge streams are effectively on TS1 + _tmp_bits = _bits & ~(1 << 7) + + # Assemble transmit HBP packet header + _tmp_data = _data[:8] + _target['TGID'] + _data[11:15] + chr(_tmp_bits) + _data[16:20] + + # MUST TEST FOR NEW STREAM AND IF SO, RE-WRITE THE LC FOR THE TARGET + # MUST RE-WRITE DESTINATION TGID IF DIFFERENT + # if _dst_id != rule['DST_GROUP']: + dmrbits = bitarray(endian='big') + dmrbits.frombytes(dmrpkt) + # Create a voice header packet (FULL LC) + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + dmrbits = _target_status[_stream_id]['H_LC'][0:98] + dmrbits[98:166] + _target_status[_stream_id]['H_LC'][98:197] + # Create a voice terminator packet (FULL LC) + elif _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VTERM: + dmrbits = _target_status[_stream_id]['T_LC'][0:98] + dmrbits[98:166] + _target_status[_stream_id]['T_LC'][98:197] + _target_status[_stream_id]['REMOVE'] = True + # Create a Burst B-E packet (Embedded LC) + elif _dtype_vseq in [1,2,3,4]: + dmrbits = dmrbits[0:116] + _target_status[_stream_id]['EMB_LC'][_dtype_vseq] + dmrbits[148:264] + dmrpkt = dmrbits.tobytes() + _tmp_data = _tmp_data + dmrpkt #+ _data[53:55] + + else: + # BEGIN CONTENTION HANDLING + # + # The rules for each of the 4 "ifs" below are listed here for readability. The Frame To Send is: + # From a different group than last RX from this HBSystem, but it has been less than Group Hangtime + # From a different group than last TX to this HBSystem, but it has been less than Group Hangtime + # From the same group as the last RX from this HBSystem, but from a different subscriber, and it has been less than stream timeout + # From the same group as the last TX to this HBSystem, but from a different subscriber, and it has been less than stream timeout + # The "continue" at the end of each means the next iteration of the for loop that tests for matching rules + # + if ((_target['TGID'] != _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < _target_system['GROUP_HANGTIME'])): + if self.STATUS[_stream_id]['CONTENTION'] == False: + self.STATUS[_stream_id]['CONTENTION'] = True + logger.info('(%s) Call not routed to TGID %s, target active or in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) + continue + if ((_target['TGID'] != _target_status[_target['TS']]['TX_TGID']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < _target_system['GROUP_HANGTIME'])): + if self.STATUS[_stream_id]['CONTENTION'] == False: + self.STATUS[_stream_id]['CONTENTION'] = True + logger.info('(%s) Call not routed to TGID%s, target in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID'])) + continue + if (_target['TGID'] == _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < hb_const.STREAM_TO): + if self.STATUS[_stream_id]['CONTENTION'] == False: + self.STATUS[_stream_id]['CONTENTION'] = True + logger.info('(%s) Call not routed to TGID%s, matching call already active on target: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) + continue + if (_target['TGID'] == _target_status[_target['TS']]['TX_TGID']) and (_rf_src != _target_status[_target['TS']]['TX_RFS']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < hb_const.STREAM_TO): + if self.STATUS[_stream_id]['CONTENTION'] == False: + self.STATUS[_stream_id]['CONTENTION'] = True + logger.info('(%s) Call not routed for subscriber %s, call route in progress on target: HBSystem: %s, TS: %s, TGID: %s, SUB: %s', self._system, int_id(_rf_src), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID']), int_id(_target_status[_target['TS']]['TX_RFS'])) + continue + + # Is this a new call stream? + if (_target_status[_target['TS']]['TX_STREAM_ID'] != _stream_id): #(_target_status[_target['TS']]['TX_RFS'] != _rf_src) or (_target_status[_target['TS']]['TX_TGID'] != _target['TGID']): + #if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']) or (_target_status[_target['TS']]['TX_RFS'] != _rf_src) or (_target_status[_target['TS']]['TX_TGID'] != _target['TGID']): + # Record the DST TGID and Stream ID + _target_status[_target['TS']]['TX_START'] = pkt_time + _target_status[_target['TS']]['TX_TGID'] = _target['TGID'] + _target_status[_target['TS']]['TX_STREAM_ID'] = _stream_id + _target_status[_target['TS']]['TX_RFS'] = _rf_src + # Generate LCs (full and EMB) for the TX stream + dst_lc = self.STATUS[_stream_id]['LC'][0:3] + _target['TGID'] + _rf_src + _target_status[_target['TS']]['TX_H_LC'] = bptc.encode_header_lc(dst_lc) + _target_status[_target['TS']]['TX_T_LC'] = bptc.encode_terminator_lc(dst_lc) + _target_status[_target['TS']]['TX_EMB_LC'] = bptc.encode_emblc(dst_lc) + logger.debug('(%s) Generating TX FULL and EMB LCs for HomeBrew destination: System: %s, TS: %s, TGID: %s', self._system, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + logger.info('(%s) Conference Bridge: %s, Call Bridged to HBP System: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + # Set other values for the contention handler to test next time there is a frame to forward + _target_status[_target['TS']]['TX_TIME'] = pkt_time + + # Handle any necessary re-writes for the destination + if _system['TS'] != _target['TS']: + _tmp_bits = _bits ^ 1 << 7 + else: + _tmp_bits = _bits + + # Assemble transmit HBP packet header + _tmp_data = _data[:8] + _target['TGID'] + _data[11:15] + chr(_tmp_bits) + _data[16:20] + + # MUST TEST FOR NEW STREAM AND IF SO, RE-WRITE THE LC FOR THE TARGET + # MUST RE-WRITE DESTINATION TGID IF DIFFERENT + # if _dst_id != rule['DST_GROUP']: + dmrbits = bitarray(endian='big') + dmrbits.frombytes(dmrpkt) + # Create a voice header packet (FULL LC) + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + dmrbits = _target_status[_target['TS']]['TX_H_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_H_LC'][98:197] + # Create a voice terminator packet (FULL LC) + elif _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VTERM: + dmrbits = _target_status[_target['TS']]['TX_T_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_T_LC'][98:197] + # Create a Burst B-E packet (Embedded LC) + elif _dtype_vseq in [1,2,3,4]: + dmrbits = dmrbits[0:116] + _target_status[_target['TS']]['TX_EMB_LC'][_dtype_vseq] + dmrbits[148:264] + dmrpkt = dmrbits.tobytes() + _tmp_data = _tmp_data + dmrpkt + '\x00\x00' # Add two bytes of nothing since OBP doesn't include BER & RSSI bytes #_data[53:55] + + # Transmit the packet to the destination system + systems[_target['SYSTEM']].send_system(_tmp_data) + #logger.debug('(%s) Packet routed by bridge: %s to system: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + + + # Final actions - Is this a voice terminator? + if (_frame_type == hb_const.HBPF_DATA_SYNC) and (_dtype_vseq == hb_const.HBPF_SLT_VTERM): + call_duration = pkt_time - self.STATUS[_stream_id]['START'] + logger.info('(%s) *CALL END* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s, Duration: %s', \ + self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot, call_duration) + if CONFIG['REPORTS']['REPORT']: + self._report.send_bridgeEvent('GROUP VOICE,END,{},{},{},{},{},{},{:.2f}'.format(self._system, int_id(_stream_id), int_id(_peer_id), int_id(_rf_src), _slot, int_id(_dst_id), call_duration)) + removed = self.STATUS.pop(_stream_id) + logger.debug('(%s) OpenBridge sourced call stream end, remove terminated Stream ID: %s', self._system, int_id(_stream_id)) + if not removed: + selflogger.error('(%s) *CALL END* STREAM ID: %s NOT IN LIST -- THIS IS A REAL PROBLEM', self._system, int_id(_stream_id)) + +class routerHBP(HBSYSTEM): + + def __init__(self, _name, _config, _report): + HBSYSTEM.__init__(self, _name, _config, _report) + # Status information for the system, TS1 & TS2 # 1 & 2 are "timeslot" # In TX_EMB_LC, 2-5 are burst B-E self.STATUS = { 1: { 'RX_START': time(), + 'TX_START': time(), 'RX_SEQ': '\x00', 'RX_RFS': '\x00', 'TX_RFS': '\x00', @@ -213,6 +428,7 @@ class routerSYSTEM(HBSYSTEM): }, 2: { 'RX_START': time(), + 'TX_START': time(), 'RX_SEQ': '\x00', 'RX_RFS': '\x00', 'TX_RFS': '\x00', @@ -241,144 +457,195 @@ class routerSYSTEM(HBSYSTEM): _bits = int_id(_data[15]) if _call_type == 'group': - - # Check for ACL match, and return if the subscriber is not allowed - if allow_sub(_rf_src) == False: - self._logger.warning('(%s) Group Voice Packet ***REJECTED BY ACL*** From: %s, HBP Peer %s, Destination TGID %s', self._system, int_id(_rf_src), int_id(_peer_id), int_id(_dst_id)) - return - - # Is this a new call stream? + + # Is this a new call stream? if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): if (self.STATUS[_slot]['RX_TYPE'] != hb_const.HBPF_SLT_VTERM) and (pkt_time < (self.STATUS[_slot]['RX_TIME'] + hb_const.STREAM_TO)) and (_rf_src != self.STATUS[_slot]['RX_RFS']): - self._logger.warning('(%s) Packet received with STREAM ID: %s SUB: %s PEER: %s TGID %s, SLOT %s collided with existing call', self._system, int_id(_stream_id), int_id(_rf_src), int_id(_peer_id), int_id(_dst_id), _slot) + logger.warning('(%s) Packet received with STREAM ID: %s SUB: %s PEER: %s TGID %s, SLOT %s collided with existing call', self._system, int_id(_stream_id), int_id(_rf_src), int_id(_peer_id), int_id(_dst_id), _slot) return - + # This is a new call stream - self.STATUS['RX_START'] = pkt_time - self._logger.info('(%s) *CALL START* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s', \ + self.STATUS[_slot]['RX_START'] = pkt_time + logger.info('(%s) *CALL START* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s', \ self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot) if CONFIG['REPORTS']['REPORT']: self._report.send_bridgeEvent('GROUP VOICE,START,{},{},{},{},{},{}'.format(self._system, int_id(_stream_id), int_id(_peer_id), int_id(_rf_src), _slot, int_id(_dst_id))) - + # If we can, use the LC from the voice header as to keep all options intact if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: decoded = decode.voice_head_term(dmrpkt) self.STATUS[_slot]['RX_LC'] = decoded['LC'] - + # If we don't have a voice header then don't wait to decode it from the Embedded LC # just make a new one from the HBP header. This is good enough, and it saves lots of time else: self.STATUS[_slot]['RX_LC'] = const.LC_OPT + _dst_id + _rf_src - for _bridge in BRIDGES: for _system in BRIDGES[_bridge]: - + if (_system['SYSTEM'] == self._system and _system['TGID'] == _dst_id and _system['TS'] == _slot and _system['ACTIVE'] == True): - + for _target in BRIDGES[_bridge]: if _target['SYSTEM'] != self._system: - if _target['ACTIVE']: + if _target['ACTIVE']: _target_status = systems[_target['SYSTEM']].STATUS _target_system = self._CONFIG['SYSTEMS'][_target['SYSTEM']] - - # BEGIN CONTENTION HANDLING - # - # The rules for each of the 4 "ifs" below are listed here for readability. The Frame To Send is: - # From a different group than last RX from this HBSystem, but it has been less than Group Hangtime - # From a different group than last TX to this HBSystem, but it has been less than Group Hangtime - # From the same group as the last RX from this HBSystem, but from a different subscriber, and it has been less than stream timeout - # From the same group as the last TX to this HBSystem, but from a different subscriber, and it has been less than stream timeout - # The "continue" at the end of each means the next iteration of the for loop that tests for matching rules - # - if ((_target['TGID'] != _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < _target_system['GROUP_HANGTIME'])): - if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: - self._logger.info('(%s) Call not routed to TGID %s, target active or in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) - continue - if ((_target['TGID'] != _target_status[_target['TS']]['TX_TGID']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < _target_system['GROUP_HANGTIME'])): - if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: - self._logger.info('(%s) Call not routed to TGID%s, target in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID'])) - continue - if (_target['TGID'] == _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < hb_const.STREAM_TO): - if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: - self._logger.info('(%s) Call not routed to TGID%s, matching call already active on target: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) - continue - if (_target['TGID'] == _target_status[_target['TS']]['TX_TGID']) and (_rf_src != _target_status[_target['TS']]['TX_RFS']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < hb_const.STREAM_TO): - if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: - self._logger.info('(%s) Call not routed for subscriber %s, call route in progress on target: HBSystem: %s, TS: %s, TGID: %s, SUB: %s', self._system, int_id(_rf_src), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID']), int_id(_target_status[_target['TS']]['TX_RFS'])) - continue - - # Set values for the contention handler to test next time there is a frame to forward - _target_status[_target['TS']]['TX_TIME'] = pkt_time - - if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']) or (_target_status[_target['TS']]['TX_RFS'] != _rf_src) or (_target_status[_target['TS']]['TX_TGID'] != _target['TGID']): - # Record the DST TGID and Stream ID - _target_status[_target['TS']]['TX_TGID'] = _target['TGID'] - _target_status[_target['TS']]['TX_STREAM_ID'] = _stream_id - _target_status[_target['TS']]['TX_RFS'] = _rf_src - # Generate LCs (full and EMB) for the TX stream - dst_lc = self.STATUS[_slot]['RX_LC'][0:3] + _target['TGID'] + _rf_src - _target_status[_target['TS']]['TX_H_LC'] = bptc.encode_header_lc(dst_lc) - _target_status[_target['TS']]['TX_T_LC'] = bptc.encode_terminator_lc(dst_lc) - _target_status[_target['TS']]['TX_EMB_LC'] = bptc.encode_emblc(dst_lc) - self._logger.debug('(%s) Generating TX FULL and EMB LCs for destination: System: %s, TS: %s, TGID: %s', self._system, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) - self._logger.info('(%s) Conference Bridge: %s, Call Bridged to: System: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) - - # Handle any necessary re-writes for the destination - if _system['TS'] != _target['TS']: - _tmp_bits = _bits ^ 1 << 7 + + if _target_system['MODE'] == 'OPENBRIDGE': + # Is this a new call stream on the target? + if (_stream_id not in _target_status): + # This is a new call stream on the target + _target_status[_stream_id] = { + 'START': pkt_time, + 'CONTENTION':False, + 'RFS': _rf_src, + 'TGID': _dst_id, + 'REMOVE': False + } + # If we can, use the LC from the voice header as to keep all options intact + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + decoded = decode.voice_head_term(dmrpkt) + _target_status[_stream_id]['LC'] = decoded['LC'] + logger.debug('(%s) Created LC for OpenBridge destination: System: %s, TGID: %s', self._system, _target['SYSTEM'], int_id(_target['TGID'])) + + # If we don't have a voice header then don't wait to decode the Embedded LC + # just make a new one from the HBP header. This is good enough, and it saves lots of time + else: + _target_status[_stream_id]['LC'] = const.LC_OPT + _dst_id + _rf_src + logger.info('(%s) Created LC with *LATE ENTRY* for OpenBridge destination: System: %s, TGID: %s', self._system, _target['SYSTEM'], int_id(_target['TGID'])) + + _target_status[_stream_id]['H_LC'] = bptc.encode_header_lc(_target_status[_stream_id]['LC']) + _target_status[_stream_id]['T_LC'] = bptc.encode_terminator_lc(_target_status[_stream_id]['LC']) + _target_status[_stream_id]['EMB_LC'] = bptc.encode_emblc(_target_status[_stream_id]['LC']) + logger.info('(%s) Conference Bridge: %s, Call Bridged to OBP System: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + # Record the time of this packet so we can later identify a stale stream + _target_status[_stream_id]['LAST'] = pkt_time + # Clear the TS bit -- all OpenBridge streams are effectively on TS1 + _tmp_bits = _bits & ~(1 << 7) + + # Assemble transmit HBP packet header + _tmp_data = _data[:8] + _target['TGID'] + _data[11:15] + chr(_tmp_bits) + _data[16:20] + + # MUST TEST FOR NEW STREAM AND IF SO, RE-WRITE THE LC FOR THE TARGET + # MUST RE-WRITE DESTINATION TGID IF DIFFERENT + # if _dst_id != rule['DST_GROUP']: + dmrbits = bitarray(endian='big') + dmrbits.frombytes(dmrpkt) + # Create a voice header packet (FULL LC) + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + dmrbits = _target_status[_stream_id]['H_LC'][0:98] + dmrbits[98:166] + _target_status[_stream_id]['H_LC'][98:197] + # Create a voice terminator packet (FULL LC) + elif _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VTERM: + dmrbits = _target_status[_stream_id]['T_LC'][0:98] + dmrbits[98:166] + _target_status[_stream_id]['T_LC'][98:197] + _target_status[_stream_id]['REMOVE'] = True + # Create a Burst B-E packet (Embedded LC) + elif _dtype_vseq in [1,2,3,4]: + dmrbits = dmrbits[0:116] + _target_status[_stream_id]['EMB_LC'][_dtype_vseq] + dmrbits[148:264] + dmrpkt = dmrbits.tobytes() + _tmp_data = _tmp_data + dmrpkt #+ _data[53:55] + else: - _tmp_bits = _bits - - # Assemble transmit HBP packet header - _tmp_data = _data[:8] + _target['TGID'] + _data[11:15] + chr(_tmp_bits) + _data[16:20] - - # MUST TEST FOR NEW STREAM AND IF SO, RE-WRITE THE LC FOR THE TARGET - # MUST RE-WRITE DESTINATION TGID IF DIFFERENT - # if _dst_id != rule['DST_GROUP']: - dmrbits = bitarray(endian='big') - dmrbits.frombytes(dmrpkt) - # Create a voice header packet (FULL LC) - if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: - dmrbits = _target_status[_target['TS']]['TX_H_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_H_LC'][98:197] - # Create a voice terminator packet (FULL LC) - elif _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VTERM: - dmrbits = _target_status[_target['TS']]['TX_T_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_T_LC'][98:197] - # Create a Burst B-E packet (Embedded LC) - elif _dtype_vseq in [1,2,3,4]: - dmrbits = dmrbits[0:116] + _target_status[_target['TS']]['TX_EMB_LC'][_dtype_vseq] + dmrbits[148:264] - dmrpkt = dmrbits.tobytes() - _tmp_data = _tmp_data + dmrpkt + _data[53:55] - + # BEGIN STANDARD CONTENTION HANDLING + # + # The rules for each of the 4 "ifs" below are listed here for readability. The Frame To Send is: + # From a different group than last RX from this HBSystem, but it has been less than Group Hangtime + # From a different group than last TX to this HBSystem, but it has been less than Group Hangtime + # From the same group as the last RX from this HBSystem, but from a different subscriber, and it has been less than stream timeout + # From the same group as the last TX to this HBSystem, but from a different subscriber, and it has been less than stream timeout + # The "continue" at the end of each means the next iteration of the for loop that tests for matching rules + # + if ((_target['TGID'] != _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < _target_system['GROUP_HANGTIME'])): + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: + logger.info('(%s) Call not routed to TGID %s, target active or in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) + continue + if ((_target['TGID'] != _target_status[_target['TS']]['TX_TGID']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < _target_system['GROUP_HANGTIME'])): + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: + logger.info('(%s) Call not routed to TGID%s, target in group hangtime: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID'])) + continue + if (_target['TGID'] == _target_status[_target['TS']]['RX_TGID']) and ((pkt_time - _target_status[_target['TS']]['RX_TIME']) < hb_const.STREAM_TO): + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: + logger.info('(%s) Call not routed to TGID%s, matching call already active on target: HBSystem: %s, TS: %s, TGID: %s', self._system, int_id(_target['TGID']), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['RX_TGID'])) + continue + if (_target['TGID'] == _target_status[_target['TS']]['TX_TGID']) and (_rf_src != _target_status[_target['TS']]['TX_RFS']) and ((pkt_time - _target_status[_target['TS']]['TX_TIME']) < hb_const.STREAM_TO): + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD and self.STATUS[_slot]['RX_STREAM_ID'] != _seq: + logger.info('(%s) Call not routed for subscriber %s, call route in progress on target: HBSystem: %s, TS: %s, TGID: %s, SUB: %s', self._system, int_id(_rf_src), _target['SYSTEM'], _target['TS'], int_id(_target_status[_target['TS']]['TX_TGID']), int_id(_target_status[_target['TS']]['TX_RFS'])) + continue + + # Is this a new call stream? + if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']) or (_target_status[_target['TS']]['TX_RFS'] != _rf_src) or (_target_status[_target['TS']]['TX_TGID'] != _target['TGID']): + # Record the DST TGID and Stream ID + _target_status[_target['TS']]['TX_START'] = pkt_time + _target_status[_target['TS']]['TX_TGID'] = _target['TGID'] + _target_status[_target['TS']]['TX_STREAM_ID'] = _stream_id + _target_status[_target['TS']]['TX_RFS'] = _rf_src + # Generate LCs (full and EMB) for the TX stream + dst_lc = self.STATUS[_slot]['RX_LC'][0:3] + _target['TGID'] + _rf_src + _target_status[_target['TS']]['TX_H_LC'] = bptc.encode_header_lc(dst_lc) + _target_status[_target['TS']]['TX_T_LC'] = bptc.encode_terminator_lc(dst_lc) + _target_status[_target['TS']]['TX_EMB_LC'] = bptc.encode_emblc(dst_lc) + logger.debug('(%s) Generating TX FULL and EMB LCs for HomeBrew destination: System: %s, TS: %s, TGID: %s', self._system, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + logger.info('(%s) Conference Bridge: %s, Call Bridged to HBP System: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + # Set other values for the contention handler to test next time there is a frame to forward + _target_status[_target['TS']]['TX_TIME'] = pkt_time + + # Handle any necessary re-writes for the destination + if _system['TS'] != _target['TS']: + _tmp_bits = _bits ^ 1 << 7 + else: + _tmp_bits = _bits + + # Assemble transmit HBP packet header + _tmp_data = _data[:8] + _target['TGID'] + _data[11:15] + chr(_tmp_bits) + _data[16:20] + + # MUST TEST FOR NEW STREAM AND IF SO, RE-WRITE THE LC FOR THE TARGET + # MUST RE-WRITE DESTINATION TGID IF DIFFERENT + # if _dst_id != rule['DST_GROUP']: + dmrbits = bitarray(endian='big') + dmrbits.frombytes(dmrpkt) + # Create a voice header packet (FULL LC) + if _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VHEAD: + dmrbits = _target_status[_target['TS']]['TX_H_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_H_LC'][98:197] + # Create a voice terminator packet (FULL LC) + elif _frame_type == hb_const.HBPF_DATA_SYNC and _dtype_vseq == hb_const.HBPF_SLT_VTERM: + dmrbits = _target_status[_target['TS']]['TX_T_LC'][0:98] + dmrbits[98:166] + _target_status[_target['TS']]['TX_T_LC'][98:197] + # Create a Burst B-E packet (Embedded LC) + elif _dtype_vseq in [1,2,3,4]: + dmrbits = dmrbits[0:116] + _target_status[_target['TS']]['TX_EMB_LC'][_dtype_vseq] + dmrbits[148:264] + dmrpkt = dmrbits.tobytes() + _tmp_data = _tmp_data + dmrpkt + _data[53:55] + # Transmit the packet to the destination system systems[_target['SYSTEM']].send_system(_tmp_data) - #self._logger.debug('(%s) Packet routed by bridge: %s to system: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) - - - + #logger.debug('(%s) Packet routed by bridge: %s to system: %s TS: %s, TGID: %s', self._system, _bridge, _target['SYSTEM'], _target['TS'], int_id(_target['TGID'])) + + + # Final actions - Is this a voice terminator? if (_frame_type == hb_const.HBPF_DATA_SYNC) and (_dtype_vseq == hb_const.HBPF_SLT_VTERM) and (self.STATUS[_slot]['RX_TYPE'] != hb_const.HBPF_SLT_VTERM): - call_duration = pkt_time - self.STATUS['RX_START'] - self._logger.info('(%s) *CALL END* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s, Duration: %s', \ + call_duration = pkt_time - self.STATUS[_slot]['RX_START'] + logger.info('(%s) *CALL END* STREAM ID: %s SUB: %s (%s) PEER: %s (%s) TGID %s (%s), TS %s, Duration: %s', \ self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot, call_duration) if CONFIG['REPORTS']['REPORT']: self._report.send_bridgeEvent('GROUP VOICE,END,{},{},{},{},{},{},{:.2f}'.format(self._system, int_id(_stream_id), int_id(_peer_id), int_id(_rf_src), _slot, int_id(_dst_id), call_duration)) - + # # Begin in-band signalling for call end. This has nothign to do with routing traffic directly. # - + # Iterate the rules dictionary - + for _bridge in BRIDGES: for _system in BRIDGES[_bridge]: if _system['SYSTEM'] == self._system: - + # TGID matches a rule source, reset its timer if _slot == _system['TS'] and _dst_id == _system['TGID'] and ((_system['TO_TYPE'] == 'ON' and (_system['ACTIVE'] == True)) or (_system['TO_TYPE'] == 'OFF' and _system['ACTIVE'] == False)): _system['TIMER'] = pkt_time + _system['TIMEOUT'] - self._logger.info('(%s) Transmission match for Bridge: %s. Reset timeout to %s', self._system, _bridge, _system['TIMER']) - + logger.info('(%s) Transmission match for Bridge: %s. Reset timeout to %s', self._system, _bridge, _system['TIMER']) + # TGID matches an ACTIVATION trigger if (_dst_id in _system['ON'] or _dst_id in _system['RESET']) and _slot == _system['TS']: # Set the matching rule as ACTIVE @@ -386,41 +653,41 @@ class routerSYSTEM(HBSYSTEM): if _system['ACTIVE'] == False: _system['ACTIVE'] = True _system['TIMER'] = pkt_time + _system['TIMEOUT'] - self._logger.info('(%s) Bridge: %s, connection changed to state: %s', self._system, _bridge, _system['ACTIVE']) + logger.info('(%s) Bridge: %s, connection changed to state: %s', self._system, _bridge, _system['ACTIVE']) # Cancel the timer if we've enabled an "OFF" type timeout if _system['TO_TYPE'] == 'OFF': _system['TIMER'] = pkt_time - self._logger.info('(%s) Bridge: %s set to "OFF" with an on timer rule: timeout timer cancelled', self._system, _bridge) + logger.info('(%s) Bridge: %s set to "OFF" with an on timer rule: timeout timer cancelled', self._system, _bridge) # Reset the timer for the rule if _system['ACTIVE'] == True and _system['TO_TYPE'] == 'ON': _system['TIMER'] = pkt_time + _system['TIMEOUT'] - self._logger.info('(%s) Bridge: %s, timeout timer reset to: %s', self._system, _bridge, _system['TIMER'] - pkt_time) - + logger.info('(%s) Bridge: %s, timeout timer reset to: %s', self._system, _bridge, _system['TIMER'] - pkt_time) + # TGID matches an DE-ACTIVATION trigger if (_dst_id in _system['OFF'] or _dst_id in _system['RESET']) and _slot == _system['TS']: # Set the matching rule as ACTIVE if _dst_id in _system['OFF']: if _system['ACTIVE'] == True: _system['ACTIVE'] = False - self._logger.info('(%s) Bridge: %s, connection changed to state: %s', self._system, _bridge, _system['ACTIVE']) + logger.info('(%s) Bridge: %s, connection changed to state: %s', self._system, _bridge, _system['ACTIVE']) # Cancel the timer if we've enabled an "ON" type timeout if _system['TO_TYPE'] == 'ON': _system['TIMER'] = pkt_time - self._logger.info('(%s) Bridge: %s set to ON with and "OFF" timer rule: timeout timer cancelled', self._system, _bridge) + logger.info('(%s) Bridge: %s set to ON with and "OFF" timer rule: timeout timer cancelled', self._system, _bridge) # Reset the timer for the rule if _system['ACTIVE'] == False and _system['TO_TYPE'] == 'OFF': _system['TIMER'] = pkt_time + _system['TIMEOUT'] - self._logger.info('(%s) Bridge: %s, timeout timer reset to: %s', self._system, _bridge, _system['TIMER'] - pkt_time) + logger.info('(%s) Bridge: %s, timeout timer reset to: %s', self._system, _bridge, _system['TIMER'] - pkt_time) # Cancel the timer if we've enabled an "ON" type timeout if _system['ACTIVE'] == True and _system['TO_TYPE'] == 'ON' and _dst_group in _system['OFF']: _system['TIMER'] = pkt_time - self._logger.info('(%s) Bridge: %s set to ON with and "OFF" timer rule: timeout timer cancelled', self._system, _bridge) + logger.info('(%s) Bridge: %s set to ON with and "OFF" timer rule: timeout timer cancelled', self._system, _bridge) - # + # # END IN-BAND SIGNALLING # - - + + # Mark status variables for use later self.STATUS[_slot]['RX_SEQ'] = _seq self.STATUS[_slot]['RX_RFS'] = _rf_src @@ -428,16 +695,16 @@ class routerSYSTEM(HBSYSTEM): self.STATUS[_slot]['RX_TGID'] = _dst_id self.STATUS[_slot]['RX_TIME'] = pkt_time self.STATUS[_slot]['RX_STREAM_ID'] = _stream_id - + # # Socket-based reporting section # class confbridgeReportFactory(reportFactory): - + def send_bridge(self): serialized = pickle.dumps(BRIDGES, protocol=pickle.HIGHEST_PROTOCOL) self.send_clients(REPORT_OPCODES['BRIDGE_SND']+serialized) - + def send_bridgeEvent(self, _data): self.send_clients(REPORT_OPCODES['BRDG_EVENT']+_data) @@ -447,13 +714,12 @@ class confbridgeReportFactory(reportFactory): #************************************************ if __name__ == '__main__': - + import argparse import sys import os import signal - from dmr_utils.utils import try_download, mk_id_dict - + # Change the current directory to the location of the application os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) @@ -469,72 +735,58 @@ if __name__ == '__main__': # Call the external routine to build the configuration dictionary CONFIG = hb_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 = hb_log.config_logging(CONFIG['LOGGER']) + logger.info('\n\nCopyright (c) 2013, 2014, 2015, 2016, 2018\n\tThe Founding Members of the K0USY Group. All rights reserved.\n') logger.debug('Logging system started, anything from here on gets logged') - + # Set up the signal handler def sig_handler(_signal, _frame): - logger.info('SHUTDOWN: HBROUTER IS TERMINATING WITH SIGNAL %s', str(_signal)) - hblink_handler(_signal, _frame, logger) + logger.info('SHUTDOWN: CONFBRIDGE IS TERMINATING WITH SIGNAL %s', str(_signal)) + hblink_handler(_signal, _frame) logger.info('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.SIGTERM, signal.SIGINT]: + for sig in [signal.SIGINT, signal.SIGTERM]: signal.signal(sig, sig_handler) - # Build the Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) - - # ID ALIAS CREATION - # Download - if CONFIG['ALIASES']['TRY_DOWNLOAD'] == True: - # Try updating peer aliases file - result = try_download(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['PEER_FILE'], CONFIG['ALIASES']['PEER_URL'], CONFIG['ALIASES']['STALE_TIME']) - logger.info(result) - # Try updating subscriber aliases file - result = try_download(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE'], CONFIG['ALIASES']['SUBSCRIBER_URL'], CONFIG['ALIASES']['STALE_TIME']) - logger.info(result) - - # Make Dictionaries - peer_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['PEER_FILE']) - if peer_ids: - logger.info('ID ALIAS MAPPER: peer_ids dictionary is available') - - subscriber_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE']) - if subscriber_ids: - logger.info('ID ALIAS MAPPER: subscriber_ids dictionary is available') - - talkgroup_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['TGID_FILE']) - if talkgroup_ids: - logger.info('ID ALIAS MAPPER: talkgroup_ids dictionary is available') - + # Create the name-number mapping dictionaries + peer_ids, subscriber_ids, talkgroup_ids = mk_aliases(CONFIG) + # Build the routing rules file BRIDGES = make_bridges('hb_confbridge_rules') - - # Build the Access Control List - ACL = build_acl('sub_acl') - - # Build the Registration Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) - + # INITIALIZE THE REPORTING LOOP - report_server = config_reports(CONFIG, logger, confbridgeReportFactory) - + report_server = config_reports(CONFIG, confbridgeReportFactory) + # HBlink instance creation - logger.info('HBlink \'hb_router.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') + logger.info('HBlink \'hb_confbridge.py\' -- SYSTEM STARTING...') for system in CONFIG['SYSTEMS']: if CONFIG['SYSTEMS'][system]['ENABLED']: - systems[system] = routerSYSTEM(system, CONFIG, logger, report_server) + if CONFIG['SYSTEMS'][system]['MODE'] == 'OPENBRIDGE': + systems[system] = routerOBP(system, CONFIG, report_server) + else: + systems[system] = routerHBP(system, CONFIG, report_server) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) - - # Initialize the rule timer -- this if for user activated stuff - rule_timer = task.LoopingCall(rule_timer_loop) - rule_timer.start(60) - reactor.run() \ No newline at end of file + def loopingErrHandle(failure): + logger.error('STOPPING REACTOR TO AVOID MEMORY LEAK: Unhandled error in timed loop.\n %s', failure) + reactor.stop() + + # Initialize the rule timer -- this if for user activated stuff + rule_timer_task = task.LoopingCall(rule_timer_loop) + rule_timer = rule_timer_task.start(60) + rule_timer.addErrback(loopingErrHandle) + + # Initialize the stream trimmer + stream_trimmer_task = task.LoopingCall(stream_trimmer_loop) + stream_trimmer = stream_trimmer_task.start(5) + stream_trimmer.addErrback(loopingErrHandle) + + + reactor.run() diff --git a/hb_confbridge_rules-SAMPLE.py b/hb_confbridge_rules-SAMPLE.py old mode 100644 new mode 100755 diff --git a/hb_config.py b/hb_config.py index 628f6ac..c3e3ef6 100755 --- a/hb_config.py +++ b/hb_config.py @@ -28,6 +28,7 @@ change. import ConfigParser import sys +import hb_const as const from socket import gethostbyname @@ -39,6 +40,61 @@ __license__ = 'GNU GPLv3' __maintainer__ = 'Cort Buffington, N0MJS' __email__ = 'n0mjs@me.com' +# Processing of ALS goes here. It's separated from the acl_build function because this +# code is hblink config-file format specific, and acl_build is abstracted +def process_acls(_config): + # Global registration ACL + _config['GLOBAL']['REG_ACL'] = acl_build(_config['GLOBAL']['REG_ACL'], const.PEER_MAX) + + # Global subscriber and TGID ACLs + for acl in ['SUB_ACL', 'TG1_ACL', 'TG2_ACL']: + _config['GLOBAL'][acl] = acl_build(_config['GLOBAL'][acl], const.ID_MAX) + + # System level ACLs + for system in _config['SYSTEMS']: + # Registration ACLs (which make no sense for peer systems) + if _config['SYSTEMS'][system]['MODE'] == 'MASTER': + _config['SYSTEMS'][system]['REG_ACL'] = acl_build(_config['SYSTEMS'][system]['REG_ACL'], const.PEER_MAX) + + # Subscriber and TGID ACLs (valid for all system types) + for acl in ['SUB_ACL', 'TG1_ACL', 'TG2_ACL']: + _config['SYSTEMS'][system][acl] = acl_build(_config['SYSTEMS'][system][acl], const.ID_MAX) + +# Create an access control list that is programatically useable from human readable: +# ORIGINAL: 'DENY:1-5,3120101,3120124' +# PROCESSED: (False, set([(1, 5), (3120124, 3120124), (3120101, 3120101)])) +def acl_build(_acl, _max): + if not _acl: + return(True, set((const.ID_MIN, _max))) + + acl = set() + sections = _acl.split(':') + + if sections[0] == 'PERMIT': + action = True + else: + action = False + + for entry in sections[1].split(','): + if entry == 'ALL': + acl.add((const.ID_MIN, _max)) + break + + elif '-' in entry: + start,end = entry.split('-') + start,end = int(start), int(end) + if (const.ID_MIN <= start <= _max) or (const.ID_MIN <= end <= _max): + acl.add((start, end)) + else: + sys.exit('ACL CREATION ERROR, VALUE OUT OF RANGE (} - {})IN RANGE-BASED ENTRY: {}'.format(const.ID_MIN, _max, entry)) + else: + id = int(entry) + if (const.ID_MIN <= id <= _max): + acl.add((id, id)) + else: + sys.exit('ACL CREATION ERROR, VALUE OUT OF RANGE ({} - {}) IN SINGLE ID ENTRY: {}'.format(const.ID_MIN, _max, entry)) + + return (action, acl) def build_config(_config_file): config = ConfigParser.ConfigParser() @@ -51,7 +107,6 @@ def build_config(_config_file): CONFIG['REPORTS'] = {} CONFIG['LOGGER'] = {} CONFIG['ALIASES'] = {} - CONFIG['AMBE'] = {} CONFIG['SYSTEMS'] = {} try: @@ -60,7 +115,12 @@ def build_config(_config_file): CONFIG['GLOBAL'].update({ 'PATH': config.get(section, 'PATH'), 'PING_TIME': config.getint(section, 'PING_TIME'), - 'MAX_MISSED': config.getint(section, 'MAX_MISSED') + 'MAX_MISSED': config.getint(section, 'MAX_MISSED'), + 'USE_ACL': config.get(section, 'USE_ACL'), + 'REG_ACL': config.get(section, 'REG_ACL'), + 'SUB_ACL': config.get(section, 'SUB_ACL'), + 'TG1_ACL': config.get(section, 'TGID_TS1_ACL'), + 'TG2_ACL': config.get(section, 'TGID_TS2_ACL') }) elif section == 'REPORTS': @@ -91,19 +151,12 @@ def build_config(_config_file): 'STALE_TIME': config.getint(section, 'STALE_DAYS') * 86400, }) - elif section == 'AMBE': - CONFIG['AMBE'].update({ - 'EXPORT_IP': gethostbyname(config.get(section, 'EXPORT_IP')), - 'EXPORT_PORT': config.getint(section, 'EXPORT_PORT'), - }) - elif config.getboolean(section, 'ENABLED'): if config.get(section, 'MODE') == 'PEER': CONFIG['SYSTEMS'].update({section: { 'MODE': config.get(section, 'MODE'), 'ENABLED': config.getboolean(section, 'ENABLED'), 'LOOSE': config.getboolean(section, 'LOOSE'), - 'EXPORT_AMBE': config.getboolean(section, 'EXPORT_AMBE'), 'SOCK_ADDR': (gethostbyname(config.get(section, 'IP')), config.getint(section, 'PORT')), 'IP': gethostbyname(config.get(section, 'IP')), 'PORT': config.getint(section, 'PORT'), @@ -127,7 +180,11 @@ def build_config(_config_file): 'SOFTWARE_ID': config.get(section, 'SOFTWARE_ID').ljust(40)[:40], 'PACKAGE_ID': config.get(section, 'PACKAGE_ID').ljust(40)[:40], 'GROUP_HANGTIME': config.getint(section, 'GROUP_HANGTIME'), - 'OPTIONS': config.get(section, 'OPTIONS') + 'OPTIONS': config.get(section, 'OPTIONS'), + 'USE_ACL': config.getboolean(section, 'USE_ACL'), + 'SUB_ACL': config.get(section, 'SUB_ACL'), + 'TG1_ACL': config.get(section, 'TGID_TS1_ACL'), + 'TG2_ACL': config.get(section, 'TGID_TS2_ACL') }}) CONFIG['SYSTEMS'][section].update({'STATS': { 'CONNECTION': 'NO', # NO, RTPL_SENT, AUTHENTICATED, CONFIG-SENT, YES @@ -144,24 +201,44 @@ def build_config(_config_file): 'MODE': config.get(section, 'MODE'), 'ENABLED': config.getboolean(section, 'ENABLED'), 'REPEAT': config.getboolean(section, 'REPEAT'), - 'EXPORT_AMBE': config.getboolean(section, 'EXPORT_AMBE'), + 'MAX_PEERS': config.getint(section, 'MAX_PEERS'), 'IP': gethostbyname(config.get(section, 'IP')), 'PORT': config.getint(section, 'PORT'), 'PASSPHRASE': config.get(section, 'PASSPHRASE'), - 'GROUP_HANGTIME': config.getint(section, 'GROUP_HANGTIME') + 'GROUP_HANGTIME': config.getint(section, 'GROUP_HANGTIME'), + 'USE_ACL': config.getboolean(section, 'USE_ACL'), + 'REG_ACL': config.get(section, 'REG_ACL'), + 'SUB_ACL': config.get(section, 'SUB_ACL'), + 'TG1_ACL': config.get(section, 'TGID_TS1_ACL'), + 'TG2_ACL': config.get(section, 'TGID_TS2_ACL') }}) CONFIG['SYSTEMS'][section].update({'PEERS': {}}) + + elif config.get(section, 'MODE') == 'OPENBRIDGE': + CONFIG['SYSTEMS'].update({section: { + 'MODE': config.get(section, 'MODE'), + 'ENABLED': config.getboolean(section, 'ENABLED'), + 'NETWORK_ID': hex(int(config.get(section, 'NETWORK_ID')))[2:].rjust(8,'0').decode('hex'), + 'IP': gethostbyname(config.get(section, 'IP')), + 'PORT': config.getint(section, 'PORT'), + 'PASSPHRASE': config.get(section, 'PASSPHRASE').ljust(20,'\x00')[:20], + 'TARGET_SOCK': (gethostbyname(config.get(section, 'TARGET_IP')), config.getint(section, 'TARGET_PORT')), + 'TARGET_IP': gethostbyname(config.get(section, 'TARGET_IP')), + 'TARGET_PORT': config.getint(section, 'TARGET_PORT'), + 'USE_ACL': config.getboolean(section, 'USE_ACL'), + 'SUB_ACL': config.get(section, 'SUB_ACL'), + 'TG1_ACL': config.get(section, 'TGID_ACL'), + 'TG2_ACL': 'PERMIT:ALL' + }}) + except ConfigParser.Error, err: - print "Cannot parse configuration file. %s" %err - sys.exit('Could not parse configuration file, exiting...') + sys.exit('Error processing configuration file -- {}'.format(err)) + process_acls(CONFIG) + return CONFIG - - - - # Used to run this file direclty and print the config, # which might be useful for debugging if __name__ == '__main__': @@ -169,6 +246,7 @@ if __name__ == '__main__': import os import argparse from pprint import pprint + from dmr_utils.utils import int_id # Change the current directory to the location of the application os.chdir(os.path.dirname(os.path.realpath(sys.argv[0]))) @@ -183,5 +261,14 @@ if __name__ == '__main__': if not cli_args.CONFIG_FILE: cli_args.CONFIG_FILE = os.path.dirname(os.path.abspath(__file__))+'/hblink.cfg' + CONFIG = build_config(cli_args.CONFIG_FILE) + pprint(CONFIG) - pprint(build_config(cli_args.CONFIG_FILE)) + def acl_check(_id, _acl): + id = int_id(_id) + for entry in _acl[1]: + if entry[0] <= id <= entry[1]: + return _acl[0] + return not _acl[0] + + print acl_check('\x00\x01\x37', CONFIG['GLOBAL']['TG1_ACL']) diff --git a/hb_const.py b/hb_const.py index 815f5e5..578b363 100755 --- a/hb_const.py +++ b/hb_const.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # ############################################################################### -# Copyright (C) 2016 Cortney T. Buffington, N0MJS +# Copyright (C) 2016-2018 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 @@ -46,4 +46,7 @@ HBPF_VOICE = 0x0 HBPF_VOICE_SYNC = 0x1 HBPF_DATA_SYNC = 0x2 HBPF_SLT_VHEAD = 0x1 -HBPF_SLT_VTERM = 0x2 \ No newline at end of file +HBPF_SLT_VTERM = 0x2 + +# Higheset peer ID permitted by HBP +PEER_MAX = 4294967295 \ No newline at end of file diff --git a/hb_log.py b/hb_log.py index 9260618..41726c1 100755 --- a/hb_log.py +++ b/hb_log.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # ############################################################################### -# Copyright (C) 2016 Cortney T. Buffington, N0MJS +# Copyright (C) 2016-2018 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 @@ -29,7 +29,7 @@ from logging.config import dictConfig # 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 Cortney T. Buffington, N0MJS and the K0USY Group' +__copyright__ = 'Copyright (c) 2016-2018 Cortney T. Buffington, N0MJS and the K0USY Group' __credits__ = 'Colin Durbridge, G4EML, Steve Zingman, N4IRS; Mike Zingman, N4IRR; Jonathan Naylor, G4KLX; Hans Barthen, DL5DI; Torsten Shultze, DG1HT' __license__ = 'GNU GPLv3' __maintainer__ = 'Cort Buffington, N0MJS' @@ -83,13 +83,11 @@ def config_logging(_logger): 'formatter': 'syslog', } }, - 'loggers': { - _logger['LOG_NAME']: { - 'handlers': _logger['LOG_HANDLERS'].split(','), - 'level': _logger['LOG_LEVEL'], - 'propagate': True, - } - } + 'root': { + 'handlers': _logger['LOG_HANDLERS'].split(','), + 'level': _logger['LOG_LEVEL'], + 'propagate': True, + }, }) return logging.getLogger(_logger['LOG_NAME']) \ No newline at end of file diff --git a/hb_parrot.py b/hb_parrot.py index 4f95d71..5173adf 100755 --- a/hb_parrot.py +++ b/hb_parrot.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # ############################################################################### -# Copyright (C) 2016 Cortney T. Buffington, N0MJS (and Mike Zingman N4IRR) +# Copyright (C) 2016-2018 Cortney T. Buffington, N0MJS (and Mike Zingman N4IRR) # # 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 @@ -35,13 +35,18 @@ from twisted.protocols.basic import NetstringReceiver from twisted.internet import reactor, task # Things we import from the main hblink module -from hblink import HBSYSTEM, systems, hblink_handler, reportFactory, REPORT_OPCODES, config_reports, build_reg_acl +from hblink import HBSYSTEM, systems, hblink_handler, reportFactory, REPORT_OPCODES, config_reports, mk_aliases from dmr_utils.utils import hex_str_3, int_id, get_alias from dmr_utils import decode, bptc, const import hb_config import hb_log import hb_const +# The module needs logging 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 and Mike Zingman, N4IRR' __copyright__ = 'Copyright (c) 2016 Cortney T. Buffington, N0MJS and the K0USY Group' @@ -55,8 +60,8 @@ __status__ = 'pre-alpha' class parrot(HBSYSTEM): - def __init__(self, _name, _config, _logger, _report): - HBSYSTEM.__init__(self, _name, _config, _logger, _report) + def __init__(self, _name, _config, _report): + HBSYSTEM.__init__(self, _name, _config, _report) # Status information for the system, TS1 & TS2 # 1 & 2 are "timeslot" @@ -119,14 +124,14 @@ class parrot(HBSYSTEM): # Is this is a new call stream? if (_stream_id != self.STATUS[_slot]['RX_STREAM_ID']): self.STATUS['RX_START'] = pkt_time - self._logger.info('(%s) *CALL START* STREAM ID: %s SUB: %s (%s) REPEATER: %s (%s) TGID %s (%s), TS %s', \ + logger.info('(%s) *CALL START* STREAM ID: %s SUB: %s (%s) REPEATER: %s (%s) TGID %s (%s), TS %s', \ self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot) # Final actions - Is this a voice terminator? if (_frame_type == hb_const.HBPF_DATA_SYNC) and (_dtype_vseq == hb_const.HBPF_SLT_VTERM) and (self.STATUS[_slot]['RX_TYPE'] != hb_const.HBPF_SLT_VTERM): call_duration = pkt_time - self.STATUS['RX_START'] - self._logger.info('(%s) *CALL END* STREAM ID: %s SUB: %s (%s) REPEATER: %s (%s) TGID %s (%s), TS %s, Duration: %s', \ + logger.info('(%s) *CALL END* STREAM ID: %s SUB: %s (%s) REPEATER: %s (%s) TGID %s (%s), TS %s, Duration: %s', \ self._system, int_id(_stream_id), get_alias(_rf_src, subscriber_ids), int_id(_rf_src), get_alias(_peer_id, peer_ids), int_id(_peer_id), get_alias(_dst_id, talkgroup_ids), int_id(_dst_id), _slot, call_duration) self.CALL_DATA.append(_data) sleep(2) @@ -182,21 +187,19 @@ if __name__ == '__main__': if cli_args.LOG_LEVEL: CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL logger = hb_log.config_logging(CONFIG['LOGGER']) + logger.info('\n\nCopyright (c) 2013, 2014, 2015, 2016, 2018\n\tThe Founding Members of the K0USY Group. All rights reserved.\n') logger.debug('Logging system started, anything from here on gets logged') # Set up the signal handler def sig_handler(_signal, _frame): logger.info('SHUTDOWN: HBROUTER IS TERMINATING WITH SIGNAL %s', str(_signal)) - hblink_handler(_signal, _frame, logger) + hblink_handler(_signal, _frame) logger.info('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.SIGTERM, signal.SIGINT]: signal.signal(sig, sig_handler) - - # Build the Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) # ID ALIAS CREATION # Download @@ -208,27 +211,21 @@ if __name__ == '__main__': result = try_download(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE'], CONFIG['ALIASES']['SUBSCRIBER_URL'], CONFIG['ALIASES']['STALE_TIME']) logger.info(result) - # Make Dictionaries - peer_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['PEER_FILE']) - if peer_ids: - logger.info('ID ALIAS MAPPER: peer_ids dictionary is available') - - subscriber_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['SUBSCRIBER_FILE']) - if subscriber_ids: - logger.info('ID ALIAS MAPPER: subscriber_ids dictionary is available') - - talkgroup_ids = mk_id_dict(CONFIG['ALIASES']['PATH'], CONFIG['ALIASES']['TGID_FILE']) - if talkgroup_ids: - logger.info('ID ALIAS MAPPER: talkgroup_ids dictionary is available') + # Create the name-number mapping dictionaries + peer_ids, subscriber_ids, talkgroup_ids = mk_aliases(CONFIG) # INITIALIZE THE REPORTING LOOP - report_server = config_reports(CONFIG, logger, reportFactory) + report_server = config_reports(CONFIG, reportFactory) # HBlink instance creation - logger.info('HBlink \'hb_parrot.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') + logger.info('HBlink \'hb_parrot.py\' (c) 2017 Mike Zingman, N4IRR -- SYSTEM STARTING...') for system in CONFIG['SYSTEMS']: if CONFIG['SYSTEMS'][system]['ENABLED']: - systems[system] = parrot(system, CONFIG, logger, report_server) + if CONFIG['SYSTEMS'][system]['MODE'] == 'OPENBRIDGE': + logger.critical('%s FATAL: Instance is mode \'OPENBRIDGE\', \n\t\t...Which would be tragic for parrot, since it carries multiple call\n\t\tstreams simultaneously. hb_parrot.py onlyl works with MMDVM-based systems', system) + sys.exit('hb_parrot.py cannot function with systems that are not MMDVM devices. System {} is configured as an OPENBRIDGE'.format(system)) + else: + systems[system] = HBSYSTEM(system, CONFIG, report_server) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) diff --git a/hblink-SAMPLE.cfg b/hblink-SAMPLE.cfg old mode 100644 new mode 100755 index e8b5743..98fa335 --- a/hblink-SAMPLE.cfg +++ b/hblink-SAMPLE.cfg @@ -4,10 +4,47 @@ # - how often the Master maintenance loop runs # MAX_MISSED - how many pings are missed before we give up and re-register # - number of times the master maintenance loop runs before de-registering a peer +# +# ACLs: +# +# Access Control Lists are a very powerful tool for administering your system. +# But they consume packet processing time. Disable them if you are not using them. +# But be aware that, as of now, the configuration stanzas still need the ACL +# sections configured even if you're not using them. +# +# REGISTRATION ACLS ARE ALWAYS USED, ONLY SUBSCRIBER AND TGID MAY BE DISABLED!!! +# +# The 'action' May be PERMIT|DENY +# Each entry may be a single radio id, or a hypenated range (e.g. 1-2999) +# Format: +# ACL = 'action:id|start-end|,id|start-end,....' +# --for example-- +# SUB_ACL: DENY:1,1000-2000,4500-60000,17 +# +# ACL Types: +# REG_ACL: peer radio IDs for registration (only used on HBP master systems) +# SUB_ACL: subscriber IDs for end-users +# TGID_TS1_ACL: destination talkgroup IDs on Timeslot 1 +# TGID_TS2_ACL: destination talkgroup IDs on Timeslot 2 +# +# ACLs may be repeated for individual systems if needed for granularity +# Global ACLs will be processed BEFORE the system level ACLs +# Packets will be matched against all ACLs, GLOBAL first. If a packet 'passes' +# All elements, processing continues. Packets are discarded at the first +# negative match, or 'reject' from an ACL element. +# +# If you do not wish to use ACLs, set them to 'PERMIT:ALL' +# TGID_TS1_ACL in the global stanza is used for OPENBRIDGE systems, since all +# traffic is passed as TS 1 between OpenBridges [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 # NOT YET WORKING: NETWORK REPORTING CONFIGURATION @@ -68,12 +105,35 @@ PEER_URL: https://www.radioid.net/static/rptrs.csv SUBSCRIBER_URL: https://www.radioid.net/static/users.csv STALE_DAYS: 7 -# EXPORT AMBE DATA -# This is for exporting AMBE audio frames to an an "external" process for -# decoding or other nefarious actions. -[AMBE] -EXPORT_IP: 127.0.0.1 -EXPORT_PORT: 1234 +# OPENBRIDGE INSTANCES - DUPLICATE SECTION FOR MULTIPLE CONNECTIONS +# OpenBridge is a protocol originall created by DMR+ for connection between an +# IPSC2 server and Brandmeister. It has been implemented here at the suggestion +# of the Brandmeister team as a way to legitimately connect HBlink to the +# Brandemiester network. +# It is recommended to name the system the ID of the Brandmeister server that +# it connects to, but is not necessary. TARGET_IP and TARGET_PORT are of the +# Brandmeister or IPSC2 server you are connecting to. PASSPHRASE is the password +# that must be agreed upon between you and the operator of the server you are +# connecting to. NETWORK_ID is a number in the format of a DMR Radio ID that +# will be sent to the other server to identify this connection. +# other parameters follow the other system types. +# +# ACLs: +# OpenBridge does not 'register', so registration ACL is meaningless. +# OpenBridge passes all traffic on TS1, so there is only 1 TGID ACL. +# Otherwise ACLs work as described in the global stanza +[OBP-1] +MODE: OPENBRIDGE +ENABLED: True +IP: +PORT: 62035 +NETWORK_ID: 3129100 +PASSPHRASE: password +TARGET_IP: 1.2.3.4 +TARGET_PORT: 62035 +USE_ACL: True +SUB_ACL: DENY:1 +TGID_ACL: PERMIT:ALL # MASTER INSTANCES - DUPLICATE SECTION FOR MULTIPLE MASTERS # HomeBrew Protocol Master instances go here. @@ -81,15 +141,28 @@ EXPORT_PORT: 1234 # Port should be the port you want this master to listen on. It must be unique # and unused by anything else. # Repeat - if True, the master repeats traffic to peers, False, it does nothing. +# +# MAX_PEERS -- maximun number of peers that may be connect to this master +# at any given time. This is very handy if you're allowing hotspots to +# connect, or using a limited computer like a Raspberry Pi. +# +# ACLs: +# See comments in the GLOBAL stanza [MASTER-1] MODE: MASTER ENABLED: True REPEAT: True +MAX_PEERS: 10 EXPORT_AMBE: False IP: PORT: 54000 PASSPHRASE: s3cr37w0rd GROUP_HANGTIME: 5 +USE_ACL: True +REG_ACL: DENY:1 +SUB_ACL: DENY:1 +TGID_TS1_ACL: PERMIT:ALL +TGID_TS2_ACL: PERMIT:ALL # PEER INSTANCES - DUPLICATE SECTION FOR MULTIPLE PEERS # There are a LOT of errors in the HB Protocol specifications on this one! @@ -100,6 +173,9 @@ GROUP_HANGTIME: 5 # Height is in meters # Setting Loose to True relaxes the validation on packets received from the master. # This will allow HBlink to connect to a non-compliant system such as XLXD, DMR+ etc. +# +# ACLs: +# See comments in the GLOBAL stanza [REPEATER-1] MODE: PEER ENABLED: True @@ -126,4 +202,8 @@ URL: www.w1abc.org SOFTWARE_ID: 20170620 PACKAGE_ID: MMDVM_HBlink GROUP_HANGTIME: 5 -OPTIONS: +OPTIONS: +USE_ACL: True +SUB_ACL: DENY:1 +TGID_TS1_ACL: PERMIT:ALL +TGID_TS2_ACL: PERMIT:ALL diff --git a/hblink.py b/hblink.py index c003c3f..fdd422a 100755 --- a/hblink.py +++ b/hblink.py @@ -33,11 +33,12 @@ from __future__ import print_function from binascii import b2a_hex as ahex from binascii import a2b_hex as bhex from random import randint -from hashlib import sha256 +from hashlib import sha256, sha1 +from hmac import new as hmac_new, compare_digest from time import time from bitstring import BitArray from importlib import import_module -import socket +from collections import deque # Twisted is pretty important, so I keep it separate from twisted.internet.protocol import DatagramProtocol, Factory, Protocol @@ -47,12 +48,17 @@ from twisted.internet import reactor, task # Other files we pull from -- this is mostly for readability and segmentation import hb_log import hb_config -from dmr_utils.utils import int_id, hex_str_4 +import hb_const as const +from dmr_utils.utils import int_id, hex_str_4, try_download, mk_id_dict # Imports for the reporting server import cPickle as pickle from reporting_const import * +# The module needs logging 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-2018 Cortney T. Buffington, N0MJS and the K0USY Group' @@ -61,124 +67,137 @@ __license__ = 'GNU GPLv3' __maintainer__ = 'Cort Buffington, N0MJS' __email__ = 'n0mjs@me.com' - # Global variables used whether we are a module or __main__ systems = {} # Timed loop used for reporting HBP status # # REPORT BASED ON THE TYPE SELECTED IN THE MAIN CONFIG FILE -def config_reports(_config, _logger, _factory): +def config_reports(_config, _factory): if True: #_config['REPORTS']['REPORT']: def reporting_loop(_logger, _server): _logger.debug('Periodic reporting loop started') _server.send_config() - - _logger.info('HBlink TCP reporting server configured') - - report_server = _factory(_config, _logger) + + logger.info('HBlink TCP reporting server configured') + + report_server = _factory(_config) report_server.clients = [] reactor.listenTCP(_config['REPORTS']['REPORT_PORT'], report_server) - - reporting = task.LoopingCall(reporting_loop, _logger, report_server) + + reporting = task.LoopingCall(reporting_loop, logger, report_server) reporting.start(_config['REPORTS']['REPORT_INTERVAL']) - + return report_server # Shut ourselves down gracefully by disconnecting from the masters and peers. -def hblink_handler(_signal, _frame, _logger): +def hblink_handler(_signal, _frame): for system in systems: - _logger.info('SHUTDOWN: DE-REGISTER SYSTEM: %s', system) + logger.info('SHUTDOWN: DE-REGISTER SYSTEM: %s', system) systems[system].dereg() +# Check a supplied ID against the ACL provided. Returns action (True|False) based +# on matching and the action specified. +def acl_check(_id, _acl): + id = int_id(_id) + for entry in _acl[1]: + if entry[0] <= id <= entry[1]: + return _acl[0] + return not _acl[0] -# Import subscriber registration ACL -# REG_ACL may be a single list of subscriber IDs -# Global action is to allow or deny them. Multiple lists with different actions and ranges -# are not yet implemented. -def build_reg_acl(_reg_acl, _logger): - REG_ACL = set() - try: - acl_file = import_module(_reg_acl) - _logger.info('Registration ACL file found, importing entries. This will take about 1.5 seconds per 1 million IDs') - sections = acl_file.REG_ACL.split(':') - REG_ACL_ACTION = sections[0] - entries_str = sections[1] - - for entry in entries_str.split(','): - if '-' in entry: - start,end = entry.split('-') - start,end = int(start), int(end) - for id in range(start, end+1): - REG_ACL.add(hex_str_4(id)) - else: - id = int(entry) - REG_ACL.add(hex_str_4(id)) - - _logger.info('Registration ACL loaded: action "{}" for {:,} registration IDs'.format( REG_ACL_ACTION, len(REG_ACL))) - - except ImportError: - _logger.info('Registration ACL file not found or invalid - all IDs may register with this system') - REG_ACL_ACTION = 'NONE' - - # Depending on which type of REG_ACL is used (PERMIT, DENY... or there isn't one) - # define a differnet function to be used to check the ACL - global allow_reg - if REG_ACL_ACTION == 'PERMIT': - def allow_reg(_id): - if _id in REG_ACL: - return True - else: - return False - elif REG_ACL_ACTION == 'DENY': - def allow_reg(_id): - if _id not in REG_ACL: - return True - else: - return False - else: - def allow_reg(_id): - return True - - return REG_ACL #************************************************ -# AMBE CLASS: Used to parse out AMBE and send to gateway +# OPENBRIDGE CLASS #************************************************ -class AMBE: - def __init__(self, _config, _logger): +class OPENBRIDGE(DatagramProtocol): + def __init__(self, _name, _config, _report): + # Define a few shortcuts to make the rest of the class more readable self._CONFIG = _config - self._logger = _logger - - self._sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) - self._exp_ip = self._CONFIG['AMBE']['EXPORT_IP'] - self._exp_port = self._CONFIG['AMBE']['EXPORT_PORT'] + self._system = _name + self._report = _report + self._config = self._CONFIG['SYSTEMS'][self._system] + self._laststrid = deque([], 20) - def parseAMBE(self, _peer, _data): - _seq = int_id(_data[4:5]) - _srcID = int_id(_data[5:8]) - _dstID = int_id(_data[8:11]) - _rptID = int_id(_data[11:15]) - _bits = int_id(_data[15:16]) # SCDV NNNN (Slot|Call type|Data|Voice|Seq or Data type) - _slot = 2 if _bits & 0x80 else 1 - _callType = 1 if (_bits & 0x40) else 0 - _frameType = (_bits & 0x30) >> 4 - _voiceSeq = (_bits & 0x0f) - _streamID = int_id(_data[16:20]) - self._logger.debug('(%s) seq: %d srcID: %d dstID: %d rptID: %d bits: %0X slot:%d callType: %d frameType: %d voiceSeq: %d streamID: %0X', - _peer, _seq, _srcID, _dstID, _rptID, _bits, _slot, _callType, _frameType, _voiceSeq, _streamID ) + def dereg(self): + logger.info('(%s) is mode OPENBRIDGE. No De-Registration required, continuing shutdown', self._system) - #logger.debug('Frame 1:(%s)', self.ByteToHex(_data)) - _dmr_frame = BitArray('0x'+ahex(_data[20:])) - _ambe = _dmr_frame[0:108] + _dmr_frame[156:264] - #_sock.sendto(_ambe.tobytes(), ("127.0.0.1", 31000)) + def send_system(self, _packet): + if _packet[:4] == 'DMRD': + _packet = _packet[:11] + self._config['NETWORK_ID'] + _packet[15:] + _packet += hmac_new(self._config['PASSPHRASE'],_packet,sha1).digest() + self.transport.write(_packet, (self._config['TARGET_IP'], self._config['TARGET_PORT'])) + # KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!! + # logger.debug('(%s) TX Packet to OpenBridge %s:%s -- %s', self._system, self._config['TARGET_IP'], self._config['TARGET_PORT'], ahex(_packet)) + else: + logger.error('(%s) OpenBridge system was asked to send non DMRD packet', self._system) - ambeBytes = _ambe.tobytes() - self._sock.sendto(ambeBytes[0:9], (self._exp_ip, self._exp_port)) - self._sock.sendto(ambeBytes[9:18], (self._exp_ip, self._exp_port)) - self._sock.sendto(ambeBytes[18:27], (self._exp_ip, self._exp_port)) + def dmrd_received(self, _peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): + pass + #print(int_id(_peer_id), int_id(_rf_src), int_id(_dst_id), int_id(_seq), _slot, _call_type, _frame_type, repr(_dtype_vseq), int_id(_stream_id)) + + def datagramReceived(self, _packet, _sockaddr): + # Keep This Line Commented Unless HEAVILY Debugging! + #logger.debug('(%s) RX packet from %s -- %s', self._system, _sockaddr, ahex(_packet)) + + if _packet[:4] == 'DMRD': # DMRData -- encapsulated DMR data frame + _data = _packet[:53] + _hash = _packet[53:] + _ckhs = hmac_new(self._config['PASSPHRASE'],_data,sha1).digest() + + if compare_digest(_hash, _ckhs) and _sockaddr == self._config['TARGET_SOCK']: + _peer_id = _data[11:15] + _seq = _data[4] + _rf_src = _data[5:8] + _dst_id = _data[8:11] + _bits = int_id(_data[15]) + _slot = 2 if (_bits & 0x80) else 1 + #_call_type = 'unit' if (_bits & 0x40) else 'group' + if _bits & 0x40: + _call_type = 'unit' + elif (_bits & 0x23) == 0x23: + _call_type = 'vcsbk' + else: + _call_type = 'group' + _frame_type = (_bits & 0x30) >> 4 + _dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F + _stream_id = _data[16:20] + #logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) + + # Sanity check for OpenBridge -- all calls must be on Slot 1 + if _slot != 1: + logger.error('(%s) OpenBridge packet discarded because it was not received on slot 1. SID: %s, TGID %s', self._system, int_id(_rf_src), int_id(_dst_id)) + return + + # ACL Processing + if self._CONFIG['GLOBAL']['USE_ACL']: + if not acl_check(_rf_src, self._CONFIG['GLOBAL']['SUB_ACL']): + if _stream_id not in self._laststrid: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY GLOBAL ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + self._laststrid.append(_stream_id) + return + if _slot == 1 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG1_ACL']): + if _stream_id not in self._laststrid: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY GLOBAL TS1 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid.append(_stream_id) + return + if self._config['USE_ACL']: + if not acl_check(_rf_src, self._config['SUB_ACL']): + if _stream_id not in self._laststrid: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY SYSTEM ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + self._laststrid.append(_stream_id) + return + if not acl_check(_dst_id, self._config['TG1_ACL']): + if _stream_id not in self._laststrid: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY SYSTEM ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid.append(_stream_id) + return + + # Userland actions -- typically this is the function you subclass for an application + self.dmrd_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data) + else: + logger.info('(%s) OpenBridge HMAC failed, packet discarded - OPCODE: %s DATA: %s HMAC LENGTH: %s HMAC: %s', self._system, _packet[:4], repr(_packet[:53]), len(_packet[53:]), repr(_packet[53:])) #************************************************ @@ -186,14 +205,15 @@ class AMBE: #************************************************ class HBSYSTEM(DatagramProtocol): - def __init__(self, _name, _config, _logger, _report): + def __init__(self, _name, _config, _report): # Define a few shortcuts to make the rest of the class more readable self._CONFIG = _config self._system = _name - self._logger = _logger self._report = _report self._config = self._CONFIG['SYSTEMS'][self._system] - + self._laststrid1 = '' + self._laststrid2 = '' + # Define shortcuts and generic function names based on the type of system we are if self._config['MODE'] == 'MASTER': self._peers = self._CONFIG['SYSTEMS'][self._system]['PEERS'] @@ -201,37 +221,36 @@ class HBSYSTEM(DatagramProtocol): self.maintenance_loop = self.master_maintenance_loop self.datagramReceived = self.master_datagramReceived self.dereg = self.master_dereg - + elif self._config['MODE'] == 'PEER': self._stats = self._config['STATS'] self.send_system = self.send_master self.maintenance_loop = self.peer_maintenance_loop self.datagramReceived = self.peer_datagramReceived self.dereg = self.peer_dereg - - # Configure for AMBE audio export if enabled - if self._config['EXPORT_AMBE']: - self._ambe = AMBE(_config, _logger) def startProtocol(self): # Set up periodic loop for tracking pings from peers. Run every 'PING_TIME' seconds self._system_maintenance = task.LoopingCall(self.maintenance_loop) self._system_maintenance_loop = self._system_maintenance.start(self._CONFIG['GLOBAL']['PING_TIME']) - + # Aliased in __init__ to maintenance_loop if system is a master def master_maintenance_loop(self): - self._logger.debug('(%s) Master maintenance loop started', self._system) + logger.debug('(%s) Master maintenance loop started', self._system) + remove_list = [] for peer in self._peers: _this_peer = self._peers[peer] # Check to see if any of the peers have been quiet (no ping) longer than allowed - if _this_peer['LAST_PING']+self._CONFIG['GLOBAL']['PING_TIME']*self._CONFIG['GLOBAL']['MAX_MISSED'] < time(): - self._logger.info('(%s) Peer %s (%s) has timed out', self._system, _this_peer['CALLSIGN'], _this_peer['RADIO_ID']) - # Remove any timed out peers from the configuration - del self._CONFIG['SYSTEMS'][self._system]['PEERS'][peer] - - # Aliased in __init__ to maintenance_loop if system is a peer + if _this_peer['LAST_PING']+(self._CONFIG['GLOBAL']['PING_TIME']*self._CONFIG['GLOBAL']['MAX_MISSED']) < time(): + remove_list.append(peer) + for peer in remove_list: + logger.info('(%s) Peer %s (%s) has timed out and is being removed', self._system, self._peers[peer]['CALLSIGN'], self._peers[peer]['RADIO_ID']) + # Remove any timed out peers from the configuration + del self._CONFIG['SYSTEMS'][self._system]['PEERS'][peer] + + # Aliased in __init__ to maintenance_loop if system is a peer def peer_maintenance_loop(self): - self._logger.debug('(%s) Peer maintenance loop started', self._system) + logger.debug('(%s) Peer maintenance loop started', self._system) if self._stats['PING_OUTSTANDING']: self._stats['NUM_OUTSTANDING'] += 1 # If we're not connected, zero out the stats and send a login request RPTL @@ -242,49 +261,49 @@ class HBSYSTEM(DatagramProtocol): self._stats['PING_OUTSTANDING'] = False self._stats['CONNECTION'] = 'RPTL_SENT' self.send_master('RPTL'+self._config['RADIO_ID']) - self._logger.info('(%s) Sending login request to master %s:%s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT']) + logger.info('(%s) Sending login request to master %s:%s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT']) # If we are connected, sent a ping to the master and increment the counter if self._stats['CONNECTION'] == 'YES': self.send_master('RPTPING'+self._config['RADIO_ID']) - self._logger.debug('(%s) RPTPING Sent to Master. Total Sent: %s, Total Missed: %s, Currently Outstanding: %s', self._system, self._stats['PINGS_SENT'], self._stats['PINGS_SENT'] - self._stats['PINGS_ACKD'], self._stats['NUM_OUTSTANDING']) + logger.debug('(%s) RPTPING Sent to Master. Total Sent: %s, Total Missed: %s, Currently Outstanding: %s', self._system, self._stats['PINGS_SENT'], self._stats['PINGS_SENT'] - self._stats['PINGS_ACKD'], self._stats['NUM_OUTSTANDING']) self._stats['PINGS_SENT'] += 1 self._stats['PING_OUTSTANDING'] = True def send_peers(self, _packet): for _peer in self._peers: self.send_peer(_peer, _packet) - #self._logger.debug('(%s) Packet sent to peer %s', self._system, self._peers[_peer]['RADIO_ID']) + #logger.debug('(%s) Packet sent to peer %s', self._system, self._peers[_peer]['RADIO_ID']) def send_peer(self, _peer, _packet): if _packet[:4] == 'DMRD': _packet = _packet[:11] + _peer + _packet[15:] self.transport.write(_packet, self._peers[_peer]['SOCKADDR']) # KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!! - #self._logger.debug('(%s) TX Packet to %s on port %s: %s', self._peers[_peer]['RADIO_ID'], self._peers[_peer]['IP'], self._peers[_peer]['PORT'], ahex(_packet)) + #logger.debug('(%s) TX Packet to %s on port %s: %s', self._peers[_peer]['RADIO_ID'], self._peers[_peer]['IP'], self._peers[_peer]['PORT'], ahex(_packet)) def send_master(self, _packet): if _packet[:4] == 'DMRD': _packet = _packet[:11] + self._config['RADIO_ID'] + _packet[15:] self.transport.write(_packet, self._config['MASTER_SOCKADDR']) # KEEP THE FOLLOWING COMMENTED OUT UNLESS YOU'RE DEBUGGING DEEPLY!!!! - # self._logger.debug('(%s) TX Packet to %s:%s -- %s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT'], ahex(_packet)) + # logger.debug('(%s) TX Packet to %s:%s -- %s', self._system, self._config['MASTER_IP'], self._config['MASTER_PORT'], ahex(_packet)) def dmrd_received(self, _peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data): pass - + def master_dereg(self): for _peer in self._peers: self.send_peer(_peer, 'MSTCL'+_peer) - self._logger.info('(%s) De-Registration sent to Peer: %s (%s)', self._system, self._peers[_peer]['CALLSIGN'], self._peers[_peer]['RADIO_ID']) - + logger.info('(%s) De-Registration sent to Peer: %s (%s)', self._system, self._peers[_peer]['CALLSIGN'], self._peers[_peer]['RADIO_ID']) + def peer_dereg(self): self.send_master('RPTCL'+self._config['RADIO_ID']) - self._logger.info('(%s) De-Registration sent to Master: %s:%s', self._system, self._config['MASTER_SOCKADDR'][0], self._config['MASTER_SOCKADDR'][1]) - + logger.info('(%s) De-Registration sent to Master: %s:%s', self._system, self._config['MASTER_SOCKADDR'][0], self._config['MASTER_SOCKADDR'][1]) + # Aliased in __init__ to datagramReceived if system is a master def master_datagramReceived(self, _data, _sockaddr): # Keep This Line Commented Unless HEAVILY Debugging! - # self._logger.debug('(%s) RX packet from %s -- %s', self._system, _sockaddr, ahex(_data)) + # logger.debug('(%s) RX packet from %s -- %s', self._system, _sockaddr, ahex(_data)) # Extract the command, which is various length, all but one 4 significant characters -- RPTCL _command = _data[:4] @@ -299,15 +318,56 @@ class HBSYSTEM(DatagramProtocol): _dst_id = _data[8:11] _bits = int_id(_data[15]) _slot = 2 if (_bits & 0x80) else 1 - _call_type = 'unit' if (_bits & 0x40) else 'group' + #_call_type = 'unit' if (_bits & 0x40) else 'group' + if _bits & 0x40: + _call_type = 'unit' + elif (_bits & 0x23) == 0x23: + _call_type = 'vcsbk' + else: + _call_type = 'group' _frame_type = (_bits & 0x30) >> 4 _dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F _stream_id = _data[16:20] - #self._logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) - - # If AMBE audio exporting is configured... - if self._config['EXPORT_AMBE']: - self._ambe.parseAMBE(self._system, _data) + #logger.debug('(%s) DMRD - Seqence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) + # ACL Processing + if self._CONFIG['GLOBAL']['USE_ACL']: + if not acl_check(_rf_src, self._CONFIG['GLOBAL']['SUB_ACL']): + if self._laststrid != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY GLOBAL ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + if _slot == 1: + self._laststrid1 = _stream_id + else: + self._laststrid2 = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG1_ACL']): + if self._laststrid1 != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY GLOBAL TS1 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid1 = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG2_ACL']): + if self._laststrid2 != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY GLOBAL TS2 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid2 = _stream_id + return + if self._config['USE_ACL']: + if not acl_check(_rf_src, self._config['SUB_ACL']): + if self._laststrid != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY SYSTEM ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + if _slot == 1: + self._laststrid1 = _stream_id + else: + self._laststrid2 = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, self._config['TG1_ACL']): + if self._laststrid1 != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY SYSTEM TS1 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid1 = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, self._config['TG2_ACL']): + if self._laststrid2 != _stream_id: + logger.info('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY SYSTEM TS2 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid2 = _stream_id + return # The basic purpose of a master is to repeat to the peers if self._config['REPEAT'] == True: @@ -316,46 +376,54 @@ class HBSYSTEM(DatagramProtocol): #self.send_peer(_peer, _data) self.send_peer(_peer, _data[:11] + _peer + _data[15:]) #self.send_peer(_peer, _data[:11] + self._config['RADIO_ID'] + _data[15:]) - #self._logger.debug('(%s) Packet on TS%s from %s (%s) for destination ID %s repeated to peer: %s (%s) [Stream ID: %s]', self._system, _slot, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id), int_id(_dst_id), self._peers[_peer]['CALLSIGN'], int_id(_peer), int_id(_stream_id)) + #logger.debug('(%s) Packet on TS%s from %s (%s) for destination ID %s repeated to peer: %s (%s) [Stream ID: %s]', self._system, _slot, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id), int_id(_dst_id), self._peers[_peer]['CALLSIGN'], int_id(_peer), int_id(_stream_id)) + # Userland actions -- typically this is the function you subclass for an application self.dmrd_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data) elif _command == 'RPTL': # RPTLogin -- a repeater wants to login _peer_id = _data[4:8] - if allow_reg(_peer_id): # Check for valid Radio ID - self._peers.update({_peer_id: { # Build the configuration data strcuture for the peer - 'CONNECTION': 'RPTL-RECEIVED', - 'PINGS_RECEIVED': 0, - 'LAST_PING': time(), - 'SOCKADDR': _sockaddr, - 'IP': _sockaddr[0], - 'PORT': _sockaddr[1], - 'SALT': randint(0,0xFFFFFFFF), - 'RADIO_ID': str(int(ahex(_peer_id), 16)), - 'CALLSIGN': '', - 'RX_FREQ': '', - 'TX_FREQ': '', - 'TX_POWER': '', - 'COLORCODE': '', - 'LATITUDE': '', - 'LONGITUDE': '', - 'HEIGHT': '', - 'LOCATION': '', - 'DESCRIPTION': '', - 'SLOTS': '', - 'URL': '', - 'SOFTWARE_ID': '', - 'PACKAGE_ID': '', - }}) - self._logger.info('(%s) Repeater Logging in with Radio ID: %s, %s:%s', self._system, int_id(_peer_id), _sockaddr[0], _sockaddr[1]) - _salt_str = hex_str_4(self._peers[_peer_id]['SALT']) - self.send_peer(_peer_id, 'RPTACK'+_salt_str) - self._peers[_peer_id]['CONNECTION'] = 'CHALLENGE_SENT' - self._logger.info('(%s) Sent Challenge Response to %s for login: %s', self._system, int_id(_peer_id), self._peers[_peer_id]['SALT']) + # Check to see if we've reached the maximum number of allowed peers + if len(self._peers) < self._config['MAX_PEERS']: + # Check for valid Radio ID + if acl_check(_peer_id, self._CONFIG['GLOBAL']['REG_ACL']) and acl_check(_peer_id, self._config['REG_ACL']): + # Build the configuration data strcuture for the peer + self._peers.update({_peer_id: { + 'CONNECTION': 'RPTL-RECEIVED', + 'PINGS_RECEIVED': 0, + 'LAST_PING': time(), + 'SOCKADDR': _sockaddr, + 'IP': _sockaddr[0], + 'PORT': _sockaddr[1], + 'SALT': randint(0,0xFFFFFFFF), + 'RADIO_ID': str(int(ahex(_peer_id), 16)), + 'CALLSIGN': '', + 'RX_FREQ': '', + 'TX_FREQ': '', + 'TX_POWER': '', + 'COLORCODE': '', + 'LATITUDE': '', + 'LONGITUDE': '', + 'HEIGHT': '', + 'LOCATION': '', + 'DESCRIPTION': '', + 'SLOTS': '', + 'URL': '', + 'SOFTWARE_ID': '', + 'PACKAGE_ID': '', + }}) + logger.info('(%s) Repeater Logging in with Radio ID: %s, %s:%s', self._system, int_id(_peer_id), _sockaddr[0], _sockaddr[1]) + _salt_str = hex_str_4(self._peers[_peer_id]['SALT']) + self.send_peer(_peer_id, 'RPTACK'+_salt_str) + self._peers[_peer_id]['CONNECTION'] = 'CHALLENGE_SENT' + logger.info('(%s) Sent Challenge Response to %s for login: %s', self._system, int_id(_peer_id), self._peers[_peer_id]['SALT']) + else: + self.transport.write('MSTNAK'+_peer_id, _sockaddr) + logger.warning('(%s) Invalid Login from Radio ID: %s Denied by Registation ACL', self._system, int_id(_peer_id)) else: self.transport.write('MSTNAK'+_peer_id, _sockaddr) - self._logger.warning('(%s) Invalid Login from Radio ID: %s Denied by Registation ACL', self._system, int_id(_peer_id)) + logger.warning('(%s) Registration denied from Radio ID: %s Maximum number of peers exceeded', self._system, int_id(_peer_id)) elif _command == 'RPTK': # Repeater has answered our login challenge _peer_id = _data[4:8] @@ -370,14 +438,14 @@ class HBSYSTEM(DatagramProtocol): if _sent_hash == _calc_hash: _this_peer['CONNECTION'] = 'WAITING_CONFIG' self.send_peer(_peer_id, 'RPTACK'+_peer_id) - self._logger.info('(%s) Peer %s has completed the login exchange successfully', self._system, _this_peer['RADIO_ID']) + logger.info('(%s) Peer %s has completed the login exchange successfully', self._system, _this_peer['RADIO_ID']) else: - self._logger.info('(%s) Peer %s has FAILED the login exchange successfully', self._system, _this_peer['RADIO_ID']) + logger.info('(%s) Peer %s has FAILED the login exchange successfully', self._system, _this_peer['RADIO_ID']) self.transport.write('MSTNAK'+_peer_id, _sockaddr) del self._peers[_peer_id] else: self.transport.write('MSTNAK'+_peer_id, _sockaddr) - self._logger.warning('(%s) Login challenge from Radio ID that has not logged in: %s', self._system, int_id(_peer_id)) + logger.warning('(%s) Login challenge from Radio ID that has not logged in: %s', self._system, int_id(_peer_id)) elif _command == 'RPTC': # Repeater is sending it's configuraiton OR disconnecting if _data[:5] == 'RPTCL': # Disconnect command @@ -385,7 +453,7 @@ class HBSYSTEM(DatagramProtocol): if _peer_id in self._peers \ and self._peers[_peer_id]['CONNECTION'] == 'YES' \ and self._peers[_peer_id]['SOCKADDR'] == _sockaddr: - self._logger.info('(%s) Peer is closing down: %s (%s)', self._system, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id)) + logger.info('(%s) Peer is closing down: %s (%s)', self._system, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id)) self.transport.write('MSTNAK'+_peer_id, _sockaddr) del self._peers[_peer_id] @@ -413,10 +481,10 @@ class HBSYSTEM(DatagramProtocol): _this_peer['PACKAGE_ID'] = _data[262:302] self.send_peer(_peer_id, 'RPTACK'+_peer_id) - self._logger.info('(%s) Peer %s (%s) has sent repeater configuration', self._system, _this_peer['CALLSIGN'], _this_peer['RADIO_ID']) + logger.info('(%s) Peer %s (%s) has sent repeater configuration', self._system, _this_peer['CALLSIGN'], _this_peer['RADIO_ID']) else: self.transport.write('MSTNAK'+_peer_id, _sockaddr) - self._logger.warning('(%s) Peer info from Radio ID that has not logged in: %s', self._system, int_id(_peer_id)) + logger.warning('(%s) Peer info from Radio ID that has not logged in: %s', self._system, int_id(_peer_id)) elif _command == 'RPTP': # RPTPing -- peer is pinging us _peer_id = _data[7:11] @@ -426,18 +494,18 @@ class HBSYSTEM(DatagramProtocol): self._peers[_peer_id]['PINGS_RECEIVED'] += 1 self._peers[_peer_id]['LAST_PING'] = time() self.send_peer(_peer_id, 'MSTPONG'+_peer_id) - self._logger.debug('(%s) Received and answered RPTPING from peer %s (%s)', self._system, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id)) + logger.debug('(%s) Received and answered RPTPING from peer %s (%s)', self._system, self._peers[_peer_id]['CALLSIGN'], int_id(_peer_id)) else: self.transport.write('MSTNAK'+_peer_id, _sockaddr) - self._logger.warning('(%s) Peer info from Radio ID that has not logged in: %s', self._system, int_id(_peer_id)) + logger.warning('(%s) Ping from Radio ID that is not logged in: %s', self._system, int_id(_peer_id)) else: - self._logger.error('(%s) Unrecognized command. Raw HBP PDU: %s', self._system, ahex(_data)) + logger.error('(%s) Unrecognized command. Raw HBP PDU: %s', self._system, ahex(_data)) # Aliased in __init__ to datagramReceived if system is a peer def peer_datagramReceived(self, _data, _sockaddr): # Keep This Line Commented Unless HEAVILY Debugging! - # self._logger.debug('(%s) RX packet from %s -- %s', self._system, _sockaddr, ahex(_data)) + # logger.debug('(%s) RX packet from %s -- %s', self._system, _sockaddr, ahex(_data)) # Validate that we receveived this packet from the master - security check! if self._config['MASTER_SOCKADDR'] == _sockaddr: @@ -451,15 +519,58 @@ class HBSYSTEM(DatagramProtocol): _dst_id = _data[8:11] _bits = int_id(_data[15]) _slot = 2 if (_bits & 0x80) else 1 - _call_type = 'unit' if (_bits & 0x40) else 'group' + #_call_type = 'unit' if (_bits & 0x40) else 'group' + if _bits & 0x40: + _call_type = 'unit' + elif (_bits & 0x23) == 0x23: + _call_type = 'vcsbk' + else: + _call_type = 'group' _frame_type = (_bits & 0x30) >> 4 _dtype_vseq = (_bits & 0xF) # data, 1=voice header, 2=voice terminator; voice, 0=burst A ... 5=burst F _stream_id = _data[16:20] - self._logger.debug('(%s) DMRD - Sequence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) + logger.debug('(%s) DMRD - Sequence: %s, RF Source: %s, Destination ID: %s', self._system, int_id(_seq), int_id(_rf_src), int_id(_dst_id)) + + # ACL Processing + if self._CONFIG['GLOBAL']['USE_ACL']: + if not acl_check(_rf_src, self._CONFIG['GLOBAL']['SUB_ACL']): + if self._laststrid != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY GLOBAL ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + if _slot == 1: + self._laststrid1 = _stream_id + else: + self._laststrid2 = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG1_ACL']): + if self._laststrid1 != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY GLOBAL TS1 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid1 = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, self._CONFIG['GLOBAL']['TG2_ACL']): + if self._laststrid2 != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY GLOBAL TS2 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid2 = _stream_id + return + if self._config['USE_ACL']: + if not acl_check(_rf_src, self._config['SUB_ACL']): + if self._laststrid != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s FROM SUBSCRIBER %s BY SYSTEM ACL', self._system, int_id(_stream_id), int_id(_rf_src)) + if _slot == 1: + self._laststrid1 = _stream_id + else: + self._laststrid2 = _stream_id + return + if _slot == 1 and not acl_check(_dst_id, self._config['TG1_ACL']): + if self._laststrid1 != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY SYSTEM TS1 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid1 = _stream_id + return + if _slot == 2 and not acl_check(_dst_id, self._config['TG2_ACL']): + if self._laststrid2 != _stream_id: + logger.debug('(%s) CALL DROPPED WITH STREAM ID %s ON TGID %s BY SYSTEM TS2 ACL', self._system, int_id(_stream_id), int_id(_dst_id)) + self._laststrid2 = _stream_id + return - # If AMBE audio exporting is configured... - if self._config['EXPORT_AMBE']: - self._ambe.parseAMBE(self._system, _data) # Userland actions -- typically this is the function you subclass for an application self.dmrd_received(_peer_id, _rf_src, _dst_id, _seq, _slot, _call_type, _frame_type, _dtype_vseq, _stream_id, _data) @@ -467,14 +578,14 @@ class HBSYSTEM(DatagramProtocol): elif _command == 'MSTN': # Actually MSTNAK -- a NACK from the master _peer_id = _data[6:10] if self._config['LOOSE'] or _peer_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation - self._logger.warning('(%s) MSTNAK Received. Resetting connection to the Master.', self._system) + logger.warning('(%s) MSTNAK Received. Resetting connection to the Master.', self._system) self._stats['CONNECTION'] = 'NO' # Disconnect ourselves and re-register elif _command == 'RPTA': # Actually RPTACK -- an ACK from the master # Depending on the state, an RPTACK means different things, in each clause, we check and/or set the state if self._stats['CONNECTION'] == 'RPTL_SENT': # If we've sent a login request... _login_int32 = _data[6:10] - self._logger.info('(%s) Repeater Login ACK Received with 32bit ID: %s', self._system, int_id(_login_int32)) + logger.info('(%s) Repeater Login ACK Received with 32bit ID: %s', self._system, int_id(_login_int32)) _pass_hash = sha256(_login_int32+self._config['PASSPHRASE']).hexdigest() _pass_hash = bhex(_pass_hash) self.send_master('RPTK'+self._config['RADIO_ID']+_pass_hash) @@ -483,7 +594,7 @@ class HBSYSTEM(DatagramProtocol): elif self._stats['CONNECTION'] == 'AUTHENTICATED': # If we've sent the login challenge... _peer_id = _data[6:10] if self._config['LOOSE'] or _peer_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation - self._logger.info('(%s) Repeater Authentication Accepted', self._system) + logger.info('(%s) Repeater Authentication Accepted', self._system) _config_packet = self._config['RADIO_ID']+\ self._config['CALLSIGN']+\ self._config['RX_FREQ']+\ @@ -502,35 +613,35 @@ class HBSYSTEM(DatagramProtocol): self.send_master('RPTC'+_config_packet) self._stats['CONNECTION'] = 'CONFIG-SENT' - self._logger.info('(%s) Repeater Configuration Sent', self._system) + logger.info('(%s) Repeater Configuration Sent', self._system) else: self._stats['CONNECTION'] = 'NO' - self._logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) + logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) elif self._stats['CONNECTION'] == 'CONFIG-SENT': # If we've sent out configuration to the master _peer_id = _data[6:10] if self._config['LOOSE'] or _peer_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation - self._logger.info('(%s) Repeater Configuration Accepted', self._system) + logger.info('(%s) Repeater Configuration Accepted', self._system) if self._config['OPTIONS']: self.send_master('RPTO'+self._config['RADIO_ID']+self._config['OPTIONS']) self._stats['CONNECTION'] = 'OPTIONS-SENT' - self._logger.info('(%s) Sent options: (%s)', self._system, self._config['OPTIONS']) + logger.info('(%s) Sent options: (%s)', self._system, self._config['OPTIONS']) else: self._stats['CONNECTION'] = 'YES' - self._logger.info('(%s) Connection to Master Completed', self._system) + logger.info('(%s) Connection to Master Completed', self._system) else: self._stats['CONNECTION'] = 'NO' - self._logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) + logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) elif self._stats['CONNECTION'] == 'OPTIONS-SENT': # If we've sent out options to the master _peer_id = _data[6:10] if self._config['LOOSE'] or _peer_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation - self._logger.info('(%s) Repeater Options Accepted', self._system) + logger.info('(%s) Repeater Options Accepted', self._system) self._stats['CONNECTION'] = 'YES' - self._logger.info('(%s) Connection to Master Completed with options', self._system) + logger.info('(%s) Connection to Master Completed with options', self._system) else: self._stats['CONNECTION'] = 'NO' - self._logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) + logger.error('(%s) Master ACK Contained wrong ID - Connection Reset', self._system) elif _command == 'MSTP': # Actually MSTPONG -- a reply to RPTPING (send by peer) _peer_id = _data[7:11] @@ -538,16 +649,16 @@ class HBSYSTEM(DatagramProtocol): self._stats['PING_OUTSTANDING'] = False self._stats['NUM_OUTSTANDING'] = 0 self._stats['PINGS_ACKD'] += 1 - self._logger.debug('(%s) MSTPONG Received. Pongs Since Connected: %s', self._system, self._stats['PINGS_ACKD']) + logger.debug('(%s) MSTPONG Received. Pongs Since Connected: %s', self._system, self._stats['PINGS_ACKD']) elif _command == 'MSTC': # Actually MSTCL -- notify us the master is closing down _peer_id = _data[5:9] if self._config['LOOSE'] or _peer_id == self._config['RADIO_ID']: # Validate the Radio_ID unless using loose validation self._stats['CONNECTION'] = 'NO' - self._logger.info('(%s) MSTCL Recieved', self._system) + logger.info('(%s) MSTCL Recieved', self._system) else: - self._logger.error('(%s) Received an invalid command in packet: %s', self._system, ahex(_data)) + logger.error('(%s) Received an invalid command in packet: %s', self._system, ahex(_data)) # # Socket-based reporting section @@ -558,10 +669,10 @@ class report(NetstringReceiver): def connectionMade(self): self._factory.clients.append(self) - self._factory._logger.info('HBlink reporting client connected: %s', self.transport.getPeer()) + logger.info('HBlink reporting client connected: %s', self.transport.getPeer()) def connectionLost(self, reason): - self._factory._logger.info('HBlink reporting client disconnected: %s', self.transport.getPeer()) + logger.info('HBlink reporting client disconnected: %s', self.transport.getPeer()) self._factory.clients.remove(self) def stringReceived(self, data): @@ -570,32 +681,57 @@ class report(NetstringReceiver): def process_message(self, _message): opcode = _message[:1] if opcode == REPORT_OPCODES['CONFIG_REQ']: - self._factory._logger.info('HBlink reporting client sent \'CONFIG_REQ\': %s', self.transport.getPeer()) + logger.info('HBlink reporting client sent \'CONFIG_REQ\': %s', self.transport.getPeer()) self.send_config() else: - self._factory._logger.error('got unknown opcode') - + logger.error('got unknown opcode') + class reportFactory(Factory): - def __init__(self, config, logger): + def __init__(self, config): self._config = config - self._logger = logger - + def buildProtocol(self, addr): if (addr.host) in self._config['REPORTS']['REPORT_CLIENTS'] or '*' in self._config['REPORTS']['REPORT_CLIENTS']: - self._logger.debug('Permitting report server connection attempt from: %s:%s', addr.host, addr.port) + logger.debug('Permitting report server connection attempt from: %s:%s', addr.host, addr.port) return report(self) else: - self._logger.error('Invalid report server connection attempt from: %s:%s', addr.host, addr.port) + logger.error('Invalid report server connection attempt from: %s:%s', addr.host, addr.port) return None - + def send_clients(self, _message): for client in self.clients: client.sendString(_message) - + def send_config(self): serialized = pickle.dumps(self._config['SYSTEMS'], protocol=pickle.HIGHEST_PROTOCOL) self.send_clients(REPORT_OPCODES['CONFIG_SND']+serialized) - + + +# ID ALIAS CREATION +# Download +def mk_aliases(_config): + if _config['ALIASES']['TRY_DOWNLOAD'] == True: + # Try updating peer aliases file + result = try_download(_config['ALIASES']['PATH'], _config['ALIASES']['PEER_FILE'], _config['ALIASES']['PEER_URL'], _config['ALIASES']['STALE_TIME']) + logger.info(result) + # Try updating subscriber aliases file + result = try_download(_config['ALIASES']['PATH'], _config['ALIASES']['SUBSCRIBER_FILE'], _config['ALIASES']['SUBSCRIBER_URL'], _config['ALIASES']['STALE_TIME']) + logger.info(result) + + # Make Dictionaries + peer_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['PEER_FILE']) + if peer_ids: + logger.info('ID ALIAS MAPPER: peer_ids dictionary is available') + + subscriber_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['SUBSCRIBER_FILE']) + if subscriber_ids: + logger.info('ID ALIAS MAPPER: subscriber_ids dictionary is available') + + talkgroup_ids = mk_id_dict(_config['ALIASES']['PATH'], _config['ALIASES']['TGID_FILE']) + if talkgroup_ids: + logger.info('ID ALIAS MAPPER: talkgroup_ids dictionary is available') + + return peer_ids, subscriber_ids, talkgroup_ids #************************************************ # MAIN PROGRAM LOOP STARTS HERE @@ -607,8 +743,8 @@ if __name__ == '__main__': 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]))) @@ -624,36 +760,39 @@ if __name__ == '__main__': # Call the external routine to build the configuration dictionary CONFIG = hb_config.build_config(cli_args.CONFIG_FILE) - + # Call the external routing to start the system logger if cli_args.LOG_LEVEL: CONFIG['LOGGER']['LOG_LEVEL'] = cli_args.LOG_LEVEL logger = hb_log.config_logging(CONFIG['LOGGER']) + logger.info('\n\nCopyright (c) 2013, 2014, 2015, 2016, 2018\n\tThe Founding Members of the K0USY Group. All rights reserved.\n') logger.debug('Logging system started, anything from here on gets logged') - + # Set up the signal handler def sig_handler(_signal, _frame): logger.info('SHUTDOWN: HBLINK IS TERMINATING WITH SIGNAL %s', str(_signal)) - hblink_handler(_signal, _frame, logger) + hblink_handler(_signal, _frame) logger.info('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.SIGTERM, signal.SIGINT]: signal.signal(sig, sig_handler) - - # Build the Registration Access Control List - REG_ACL = build_reg_acl('reg_acl', logger) - + + peer_ids, subscriber_ids, talkgroup_ids = mk_aliases(CONFIG) + # INITIALIZE THE REPORTING LOOP - report_server = config_reports(CONFIG, logger, reportFactory) + report_server = config_reports(CONFIG, reportFactory) # HBlink instance creation - logger.info('HBlink \'HBlink.py\' (c) 2016 N0MJS & the K0USY Group - SYSTEM STARTING...') + logger.info('HBlink \'HBlink.py\' -- SYSTEM STARTING...') for system in CONFIG['SYSTEMS']: if CONFIG['SYSTEMS'][system]['ENABLED']: - systems[system] = HBSYSTEM(system, CONFIG, logger, report_server) + if CONFIG['SYSTEMS'][system]['MODE'] == 'OPENBRIDGE': + systems[system] = OPENBRIDGE(system, CONFIG, report_server) + else: + systems[system] = HBSYSTEM(system, CONFIG, report_server) reactor.listenUDP(CONFIG['SYSTEMS'][system]['PORT'], systems[system], interface=CONFIG['SYSTEMS'][system]['IP']) logger.debug('%s instance created: %s, %s', CONFIG['SYSTEMS'][system]['MODE'], system, systems[system]) - + reactor.run() diff --git a/reg_acl-SAMPLE.py b/reg_acl-SAMPLE.py deleted file mode 100644 index 9a9e701..0000000 --- a/reg_acl-SAMPLE.py +++ /dev/null @@ -1,11 +0,0 @@ -# -# Used to limit HomeBrew repeater Protocol registrations. -# -# If this is the SAMPLE file, you'll need to made a copy or start from scratch -# with one called reg_acl.py -# -# The 'action' May be PERMIT|DENY -# Each entry may be a single radio id, or a hypenated range (e.g. 1-2999) -# Format: -# ACL = 'action:id|start-end|,id|start-end,....' -REG_ACL = 'DENY:1' diff --git a/reporting_const.py b/reporting_const.py old mode 100644 new mode 100755 diff --git a/requirements.txt b/requirements.txt old mode 100644 new mode 100755 diff --git a/retired/hb_routing_rules-SAMPLE.py b/retired/hb_routing_rules-SAMPLE.py old mode 100644 new mode 100755 diff --git a/sub_acl-SAMPLE.py b/sub_acl-SAMPLE.py deleted file mode 100644 index c249001..0000000 --- a/sub_acl-SAMPLE.py +++ /dev/null @@ -1,9 +0,0 @@ -# -# To use this feature, you'll need to copy this, or create a file called -# sub_acl.py that's like this one, with your local parameters in it. -# -# The 'action' May be PERMIT|DENY -# Each entry may be a single radio id, or a hypenated range (e.g. 1-2999) -# Format: -# ACL = 'action:id|start-end|,id|start-end,....' -ACL = 'DENY:0-2999,4000000-4000999'