diff --git a/fail2ban/server/filter.py b/fail2ban/server/filter.py index baf4bc9b..b808a6e3 100644 --- a/fail2ban/server/filter.py +++ b/fail2ban/server/filter.py @@ -1102,11 +1102,10 @@ class IPAddr(object): for family in [socket.AF_INET, socket.AF_INET6]: try: binary = socket.inet_pton(family, ipstring) - except socket.error: - continue - else: self.valid = True break + except socket.error: + continue if self.valid and family == socket.AF_INET: # convert host to network byte order @@ -1178,9 +1177,13 @@ class IPAddr(object): return self.family < other.family or self.addr < other.addr def __add__(self, other): + if not isinstance(other, IPAddr): + other = IPAddr(other) return "%s%s" % (self, other) def __radd__(self, other): + if not isinstance(other, IPAddr): + other = IPAddr(other) return "%s%s" % (other, self) def __hash__(self): diff --git a/fail2ban/tests/filtertestcase.py b/fail2ban/tests/filtertestcase.py index 7198185a..371db3bc 100644 --- a/fail2ban/tests/filtertestcase.py +++ b/fail2ban/tests/filtertestcase.py @@ -1383,20 +1383,16 @@ class DNSUtilsNetworkTests(unittest.TestCase): self.assertEqual(res, None) def testAddr2bin(self): - res = DNSUtils.addr2bin('10.0.0.0') - self.assertEqual(res, 167772160L) - res = DNSUtils.addr2bin('10.0.0.0', cidr=None) - self.assertEqual(res, 167772160L) - res = DNSUtils.addr2bin('10.0.0.0', cidr=32L) - self.assertEqual(res, 167772160L) - res = DNSUtils.addr2bin('10.0.0.1', cidr=32L) - self.assertEqual(res, 167772161L) - res = DNSUtils.addr2bin('10.0.0.1', cidr=31L) - self.assertEqual(res, 167772160L) - - def testBin2addr(self): - res = DNSUtils.bin2addr(167772160L) - self.assertEqual(res, '10.0.0.0') + res = IPAddr('10.0.0.0') + self.assertEqual(res.addr, 167772160L) + res = IPAddr('10.0.0.0', cidr=None) + self.assertEqual(res.addr, 167772160L) + res = IPAddr('10.0.0.0', cidr=32L) + self.assertEqual(res.addr, 167772160L) + res = IPAddr('10.0.0.1', cidr=32L) + self.assertEqual(res.addr, 167772161L) + res = IPAddr('10.0.0.1', cidr=31L) + self.assertEqual(res.addr, 167772160L) def testIPAddr_Equal6(self): self.assertEqual(