Added handling for IPs too big

This commit is contained in:
Geoffrey Frogeye 2019-12-25 12:35:06 +01:00
parent 9f343ed296
commit 0e7479e23e
Signed by: geoffrey
GPG key ID: D8A7ECA00A8CD3DD
2 changed files with 17 additions and 10 deletions

View file

@ -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,

View file

@ -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: