def TestSipPortValidation(self): m = MyConfigParser() with self.assertRaises(ValueError): m.check_and_set('sip_port', "-1") with self.assertRaises(ValueError): m.check_and_set('sip_port', "0") with self.assertRaises(ValueError): m.check_and_set('sip_port', "65536") self.assertEquals(m.check_and_set('sip_port', "1"), None) self.assertEquals(m.check_and_set('sip_port', "65535"), None)
def reopen(signum, logfile): print 'Signal %d received, reopening logs' % signum fd = os.open(logfile, os.O_WRONLY | os.O_CREAT | os.O_APPEND) os.dup2(fd, sys.__stdout__.fileno()) os.dup2(fd, sys.__stderr__.fileno()) os.close(fd) def usage(global_config, brief = False): print 'usage: b2bua.py [--option1=value1] [--option2=value2] ... [--optionN==valueN]' if not brief: print '\navailable options:\n' global_config.options_help() sys.exit(1) if __name__ == '__main__': global_config = MyConfigParser() global_config['digest_auth'] = True global_config['start_acct_enable'] = False global_config['keepalive_ans'] = 0 global_config['keepalive_orig'] = 0 global_config['auth_enable'] = True global_config['acct_enable'] = True global_config['_pass_headers'] = [] global_config['_orig_argv'] = sys.argv[:] global_config['_orig_cwd'] = os.getcwd() try: opts, args = getopt.getopt(sys.argv[1:], 'fDl:p:d:P:L:s:a:t:T:k:m:A:ur:F:R:h:c:M:HC:W:', global_config.get_longopts()) except getopt.GetoptError: usage(global_config) global_config['foreground'] = False
def main_func(): global_config = MyConfigParser() global_config['digest_auth'] = True global_config['start_acct_enable'] = False global_config['keepalive_ans'] = 0 global_config['keepalive_orig'] = 0 global_config['auth_enable'] = True global_config['acct_enable'] = True global_config['_pass_headers'] = [] global_config['_orig_argv'] = sys.argv[:] global_config['_orig_cwd'] = os.getcwd() try: opts, args = getopt.getopt( sys.argv[1:], 'fDl:p:d:P:L:s:a:t:T:k:m:A:ur:F:R:h:c:M:HC:W:', global_config.get_longopts()) except getopt.GetoptError: usage(global_config) global_config['foreground'] = False global_config['pidfile'] = '/var/run/b2bua.pid' global_config['logfile'] = '/var/log/b2bua.log' global_config['b2bua_socket'] = '/var/run/b2bua.sock' global_config['_sip_address'] = SipConf.my_address global_config['_sip_port'] = SipConf.my_port global_config['_my_pid'] = os.getpid() rtp_proxy_clients = [] writeconf = None for o, a in opts: if o == '-f': global_config['foreground'] = True continue if o == '-l': global_config.check_and_set('sip_address', a) continue if o == '-p': global_config.check_and_set('sip_port', a) continue if o == '-P': global_config.check_and_set('pidfile', a) continue if o == '-L': global_config.check_and_set('logfile', a) continue if o == '-s': global_config.check_and_set('static_route', a) continue if o == '-a': global_config.check_and_set('accept_ips', a) continue if o == '-D': global_config['digest_auth'] = False continue if o == '-A': acct_level = int(a.strip()) if acct_level == 0: global_config['acct_enable'] = False global_config['start_acct_enable'] = False elif acct_level == 1: global_config['acct_enable'] = True global_config['start_acct_enable'] = False elif acct_level == 2: global_config['acct_enable'] = True global_config['start_acct_enable'] = True else: sys.__stderr__.write( 'ERROR: -A argument not in the range 0-2\n') usage(global_config, True) continue if o == '-t': global_config.check_and_set('static_tr_in', a) continue if o == '-T': global_config.check_and_set('static_tr_out', a) continue if o == '-k': ka_level = int(a.strip()) if ka_level == 0: pass elif ka_level == 1: global_config['keepalive_ans'] = 32 elif ka_level == 2: global_config['keepalive_orig'] = 32 elif ka_level == 3: global_config['keepalive_ans'] = 32 global_config['keepalive_orig'] = 32 else: sys.__stderr__.write( 'ERROR: -k argument not in the range 0-3\n') usage(global_config, True) if o == '-m': global_config.check_and_set('max_credit_time', a) continue if o == '-u': global_config['auth_enable'] = False continue if o == '-r': global_config.check_and_set('rtp_proxy_client', a) continue if o == '-F': global_config.check_and_set('allowed_pts', a) continue if o == '-R': global_config.check_and_set('radiusclient.conf', a) continue if o == '-h': for a in a.split(','): global_config.check_and_set('pass_header', a) continue if o == '-c': global_config.check_and_set('b2bua_socket', a) continue if o == '-M': global_config.check_and_set('max_radiusclients', a) continue if o == '-H': global_config['hide_call_id'] = True continue if o in ('-C', '--config'): global_config.read(a.strip()) continue if o.startswith('--'): global_config.check_and_set(o[2:], a) continue if o == '-W': writeconf = a.strip() continue if '_rtp_proxy_clients' in global_config: for a in global_config['_rtp_proxy_clients']: rtp_proxy_clients.append(a) if 'static_route' in global_config: global_config['_static_route'] = B2BRoute( global_config['static_route']) elif not global_config['auth_enable']: sys.__stderr__.write( 'ERROR: static route should be specified when Radius auth is disabled\n' ) usage(global_config, True) if writeconf != None: global_config.write(open(writeconf, 'w')) if not global_config['foreground']: daemonize(logfile=global_config['logfile']) global_config['_sip_logger'] = SipLogger('b2bua') if len(rtp_proxy_clients) > 0: global_config['_rtp_proxy_clients'] = [] for address in rtp_proxy_clients: global_config['_rtp_proxy_clients'].append( Rtp_proxy_client(global_config, spath=address)) if global_config['auth_enable'] or global_config['acct_enable']: global_config['_radius_client'] = RadiusAuthorisation(global_config) global_config['_uaname'] = 'Sippy B2BUA (RADIUS)' global_config['_cmap'] = CallMap(global_config) if 'sip_proxy' in global_config: host_port = global_config['sip_proxy'].split(':', 1) if len(host_port) == 1: global_config['_sip_proxy'] = (host_port[0], 5060) else: global_config['_sip_proxy'] = (host_port[0], int(host_port[1])) global_config['_cmap'].proxy = StatefulProxy( global_config, global_config['_sip_proxy']) if global_config.getdefault('xmpp_b2bua_id', None) != None: global_config['_xmpp_mode'] = True global_config['_sip_tm'] = SipTransactionManager( global_config, global_config['_cmap'].recvRequest) global_config['_sip_tm'].nat_traversal = global_config.getdefault( 'nat_traversal', False) cmdfile = global_config['b2bua_socket'] if cmdfile.startswith('unix:'): cmdfile = cmdfile[5:] cli_server = CLIConnectionManager(global_config['_cmap'].recvCommand, cmdfile) if not global_config['foreground']: open(global_config['pidfile'], 'w').write(str(os.getpid()) + '\n') Signal(SIGUSR1, reopen, SIGUSR1, global_config['logfile']) ED2.loop()
def main_func(): global_config = MyConfigParser() global_config['digest_auth'] = True global_config['start_acct_enable'] = False global_config['keepalive_ans'] = 0 global_config['keepalive_orig'] = 0 global_config['auth_enable'] = True global_config['acct_enable'] = True global_config['_pass_headers'] = [] global_config['_orig_argv'] = sys.argv[:] global_config['_orig_cwd'] = os.getcwd() try: opts, args = getopt.getopt(sys.argv[1:], 'fDl:p:d:P:L:s:a:t:T:k:m:A:ur:F:R:h:c:M:HC:W:', global_config.get_longopts()) except getopt.GetoptError: usage(global_config) global_config['foreground'] = False global_config['pidfile'] = '/var/run/b2bua.pid' global_config['logfile'] = '/var/log/b2bua.log' global_config['b2bua_socket'] = '/var/run/b2bua.sock' global_config['_sip_address'] = SipConf.my_address global_config['_sip_port'] = SipConf.my_port global_config['_my_pid'] = os.getpid() rtp_proxy_clients = [] writeconf = None for o, a in opts: if o == '-f': global_config['foreground'] = True continue if o == '-l': global_config.check_and_set('sip_address', a) continue if o == '-p': global_config.check_and_set('sip_port', a) continue if o == '-P': global_config.check_and_set('pidfile', a) continue if o == '-L': global_config.check_and_set('logfile', a) continue if o == '-s': global_config.check_and_set('static_route', a) continue if o == '-a': global_config.check_and_set('accept_ips', a) continue if o == '-D': global_config['digest_auth'] = False continue if o == '-A': acct_level = int(a.strip()) if acct_level == 0: global_config['acct_enable'] = False global_config['start_acct_enable'] = False elif acct_level == 1: global_config['acct_enable'] = True global_config['start_acct_enable'] = False elif acct_level == 2: global_config['acct_enable'] = True global_config['start_acct_enable'] = True else: sys.__stderr__.write('ERROR: -A argument not in the range 0-2\n') usage(global_config, True) continue if o == '-t': global_config.check_and_set('static_tr_in', a) continue if o == '-T': global_config.check_and_set('static_tr_out', a) continue if o == '-k': ka_level = int(a.strip()) if ka_level == 0: pass elif ka_level == 1: global_config['keepalive_ans'] = 32 elif ka_level == 2: global_config['keepalive_orig'] = 32 elif ka_level == 3: global_config['keepalive_ans'] = 32 global_config['keepalive_orig'] = 32 else: sys.__stderr__.write('ERROR: -k argument not in the range 0-3\n') usage(global_config, True) if o == '-m': global_config.check_and_set('max_credit_time', a) continue if o == '-u': global_config['auth_enable'] = False continue if o == '-r': global_config.check_and_set('rtp_proxy_client', a) continue if o == '-F': global_config.check_and_set('allowed_pts', a) continue if o == '-R': global_config.check_and_set('radiusclient.conf', a) continue if o == '-h': for a in a.split(','): global_config.check_and_set('pass_header', a) continue if o == '-c': global_config.check_and_set('b2bua_socket', a) continue if o == '-M': global_config.check_and_set('max_radiusclients', a) continue if o == '-H': global_config['hide_call_id'] = True continue if o in ('-C', '--config'): global_config.read(a.strip()) continue if o.startswith('--'): global_config.check_and_set(o[2:], a) continue if o == '-W': writeconf = a.strip() continue if '_rtp_proxy_clients' in global_config: for a in global_config['_rtp_proxy_clients']: rtp_proxy_clients.append(a) if 'static_route' in global_config: global_config['_static_route'] = B2BRoute(global_config['static_route']) elif not global_config['auth_enable']: sys.__stderr__.write('ERROR: static route should be specified when Radius auth is disabled\n') usage(global_config, True) if writeconf != None: global_config.write(open(writeconf, 'w')) if not global_config['foreground']: daemonize(logfile = global_config['logfile']) global_config['_sip_logger'] = SipLogger('b2bua') if len(rtp_proxy_clients) > 0: global_config['_rtp_proxy_clients'] = [] for address in rtp_proxy_clients: global_config['_rtp_proxy_clients'].append(Rtp_proxy_client(global_config, spath = address)) if global_config['auth_enable'] or global_config['acct_enable']: global_config['_radius_client'] = RadiusAuthorisation(global_config) global_config['_uaname'] = 'Sippy B2BUA (RADIUS)' global_config['_cmap'] = CallMap(global_config) if 'sip_proxy' in global_config: host_port = global_config['sip_proxy'].split(':', 1) if len(host_port) == 1: global_config['_sip_proxy'] = (host_port[0], 5060) else: global_config['_sip_proxy'] = (host_port[0], int(host_port[1])) global_config['_cmap'].proxy = StatefulProxy(global_config, global_config['_sip_proxy']) if global_config.getdefault('xmpp_b2bua_id', None) != None: global_config['_xmpp_mode'] = True global_config['_sip_tm'] = SipTransactionManager(global_config, global_config['_cmap'].recvRequest) global_config['_sip_tm'].nat_traversal = global_config.getdefault('nat_traversal', False) cmdfile = global_config['b2bua_socket'] if cmdfile.startswith('unix:'): cmdfile = cmdfile[5:] cli_server = CLIConnectionManager(global_config['_cmap'].recvCommand, cmdfile) if not global_config['foreground']: open(global_config['pidfile'], 'w').write(str(os.getpid()) + '\n') Signal(SIGUSR1, reopen, SIGUSR1, global_config['logfile']) ED2.loop()
os.dup2(fd, sys.__stderr__.fileno()) os.close(fd) def usage(global_config, brief=False): print( 'usage: b2bua.py [--option1=value1] [--option2=value2] ... [--optionN==valueN]' ) if not brief: print('\navailable options:\n') global_config.options_help() sys.exit(1) if __name__ == '__main__': global_config = MyConfigParser() global_config['digest_auth'] = True global_config['start_acct_enable'] = False global_config['keepalive_ans'] = 0 global_config['keepalive_orig'] = 0 global_config['auth_enable'] = True global_config['acct_enable'] = True global_config['_pass_headers'] = [] global_config['_orig_argv'] = sys.argv[:] global_config['_orig_cwd'] = os.getcwd() try: opts, args = getopt.getopt( sys.argv[1:], 'fDl:p:d:P:L:s:a:t:T:k:m:A:ur:F:R:h:c:M:HC:W:', global_config.get_longopts()) except getopt.GetoptError: usage(global_config)
def TestMaxKeepAlive(self): m = MyConfigParser() with self.assertRaises(ValueError): m.check_and_set('keepalive_ans', "-1")
def TestMaxCreditTime(self): m = MyConfigParser() with self.assertRaises(ValueError): m.check_and_set('max_credit_time', "-1")
def TestParamHandling(self): m = MyConfigParser() m['_foo'] = 'bar' m['b2bua_socket'] = 'bar1' m['acct_enable'] = True m['auth_enable'] = 'False' self.assertTrue(m.has_key('_foo')) self.assertEqual(m['_foo'], 'bar') self.assertEqual(m['b2bua_socket'], 'bar1') self.assertEqual(m.get('_foo'), 'bar') self.assertEqual(m.get('b2bua_socket'), 'bar1') self.assertEqual(m.get('general', 'b2bua_socket'), 'bar1') self.assertTrue(m.get('acct_enable')) self.assertFalse(m.get('auth_enable')) m.check_and_set('keepalive_ans', '15') self.assertEqual(m['keepalive_ans'], 15) self.assertIsInstance(m.get('keepalive_ans'), int) m.check_and_set('pass_header', 'a') m.check_and_set('pass_header', 'b') self.assertEqual(m['pass_headers'], 'a,b') self.assertEqual(m['_pass_headers'][0], 'a') self.assertEqual(m['_pass_headers'][1], 'b') m.check_and_set('accept_ips', '1.2.3.4, 5.6.7.8') self.assertEqual(m['_accept_ips'][0], '1.2.3.4') self.assertEqual(m['_accept_ips'][1], '5.6.7.8') self.assertEqual(m.check_and_set('hrtb_ival', "1"), None) self.assertEqual(m['hrtb_ival'], 1) self.assertEqual(m.get('hrtb_ival'), 1) # Test that get method returns correct type self.assertIsInstance(m.get('hrtb_ival'), float) # Test that access by index returns correct type self.assertIsInstance(m['hrtb_ival'], float) with self.assertRaises(KeyError): m.check_and_set('non_existant_key', "1")