From 9a7412c08f9243f38e4457e2d4e5cd031c8655b7 Mon Sep 17 00:00:00 2001 From: Brian May Date: Mon, 11 Jul 2011 10:18:01 +1000 Subject: [PATCH] More changes to simplify the upcomming IPv6 patch. --- client.py | 8 +++----- main.py | 38 ++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/client.py b/client.py index 3074697..0954a09 100644 --- a/client.py +++ b/client.py @@ -125,16 +125,14 @@ class MultiListener: class FirewallClient: - def __init__(self, port, subnets_include, subnets_exclude, dnsport, method): - self.port = port + def __init__(self, port_v4, subnets_include, subnets_exclude, dnsport_v4, method): self.auto_nets = [] self.subnets_include = subnets_include self.subnets_exclude = subnets_exclude - self.dnsport = dnsport argvbase = ([sys.argv[1], sys.argv[0], sys.argv[1]] + ['-v'] * (helpers.verbose or 0) + - ['--firewall', str(port), - str(dnsport), + ['--firewall', str(port_v4), + str(dnsport_v4), method]) if ssyslog._p: argvbase += ['--syslog'] diff --git a/main.py b/main.py index a08bf80..10fe35d 100644 --- a/main.py +++ b/main.py @@ -4,30 +4,36 @@ import compat.ssubprocess as ssubprocess from helpers import * +# 1.2.3.4/5 or just 1.2.3.4 +def parse_subnet4(s): + m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s) + if not m: + raise Fatal('%r is not a valid IP subnet format' % s) + (a,b,c,d,width) = m.groups() + (a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0)) + if width == None: + width = 32 + else: + width = int(width) + if a > 255 or b > 255 or c > 255 or d > 255: + raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d)) + if width > 32: + raise Fatal('*/%d is greater than the maximum of 32' % width) + return(socket.AF_INET, '%d.%d.%d.%d' % (a,b,c,d), width) + + # list of: # 1.2.3.4/5 or just 1.2.3.4 def parse_subnets(subnets_str): subnets = [] for s in subnets_str: - m = re.match(r'(\d+)(?:\.(\d+)\.(\d+)\.(\d+))?(?:/(\d+))?$', s) - if not m: - raise Fatal('%r is not a valid IP subnet format' % s) - (a,b,c,d,width) = m.groups() - (a,b,c,d) = (int(a or 0), int(b or 0), int(c or 0), int(d or 0)) - if width == None: - width = 32 - else: - width = int(width) - if a > 255 or b > 255 or c > 255 or d > 255: - raise Fatal('%d.%d.%d.%d has numbers > 255' % (a,b,c,d)) - if width > 32: - raise Fatal('*/%d is greater than the maximum of 32' % width) - subnets.append((socket.AF_INET, '%d.%d.%d.%d' % (a,b,c,d), width)) + subnet = parse_subnet4(s) + subnets.append(subnet) return subnets # 1.2.3.4:567 or just 1.2.3.4 or just 567 -def parse_ipport(s): +def parse_ipport4(s): s = str(s) m = re.match(r'(?:(\d+)\.(\d+)\.(\d+)\.(\d+))?(?::)?(?:(\d+))?$', s) if not m: @@ -118,7 +124,7 @@ try: method = opt.method else: o.fatal("method %s not supported"%opt.method) - ipport_v4 = parse_ipport(opt.listen or '0.0.0.0:0') + ipport_v4 = parse_ipport4(opt.listen or '0.0.0.0:0') sys.exit(client.main(ipport_v4, opt.ssh_cmd, remotename,