From 0e7479e23e93559d3630973cd4b8203df620ecbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Geoffrey=20=E2=80=9CFrogeye=E2=80=9D=20Preud=27homme?= Date: Wed, 25 Dec 2019 12:35:06 +0100 Subject: [PATCH] Added handling for IPs too big --- database.py | 23 ++++++++++++++--------- feed_dns.py | 4 +++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/database.py b/database.py index 866eb66..173a34b 100644 --- a/database.py +++ b/database.py @@ -288,11 +288,16 @@ class Database(Profiler): return True @staticmethod - def pack_ip4address(address: str) -> Ip4Path: + def pack_ip4address_low(address: str) -> int: addr = 0 for split in address.split('.'): - addr = (addr << 8) + int(split) - return Ip4Path(addr, 32) + octet = int(split) + addr = (addr << 8) + octet + return addr + + @staticmethod + def pack_ip4address(address: str) -> Ip4Path: + return Ip4Path(Database.pack_ip4address_low(address), 32) @staticmethod def unpack_ip4address(address: Ip4Path) -> str: @@ -627,17 +632,17 @@ class Database(Profiler): def get_ip4(self, ip4_str: str) -> typing.Iterable[Path]: self.enter_step('get_ip4_pack') - ip4 = self.pack_ip4address(ip4_str) + ip4val = self.pack_ip4address_low(ip4_str) self.enter_step('get_ip4_cache') - if not self.ip4cache[ip4.value >> self.ip4cache_shift]: + if not self.ip4cache[ip4val >> self.ip4cache_shift]: return self.enter_step('get_ip4_brws') dic = self.ip4tree - for i in range(31, 31-ip4.prefixlen, -1): - bit = (ip4.value >> i) & 0b1 + for i in range(31, -1, -1): + bit = (ip4val >> i) & 0b1 if dic.active(): self.enter_step('get_ip4_yield') - yield Ip4Path(ip4.value >> (i+1) << (i+1), 31-i) + yield Ip4Path(ip4val >> (i+1) << (i+1), 31-i) self.enter_step('get_ip4_brws') next_dic = dic.one if bit else dic.zero if next_dic is None: @@ -645,7 +650,7 @@ class Database(Profiler): dic = next_dic if dic.active(): self.enter_step('get_ip4_yield') - yield ip4 + yield Ip4Path(ip4val, 32) def _unset_match(self, match: Match, diff --git a/feed_dns.py b/feed_dns.py index 74fe1dd..ebb8fcb 100755 --- a/feed_dns.py +++ b/feed_dns.py @@ -61,7 +61,9 @@ class Writer(multiprocessing.Process): try: for source in select(self.db, value): write(self.db, name, updated, source=source) - except ValueError: + except (ValueError, IndexError): + # ValueError: non-number in IP + # IndexError: IP too big self.log.exception("Cannot execute: %s", record) if next_save > 0 and time.time() > next_save: