class BTrie(object): """ A class to construct and perform lookups on a btrie. Since we do not have python bindings for btrie, we do this in a roundabout way by running rbldnsd with a single ip6trie dataset, and then querying the rbldnsd to perform the lookup. """ def __init__(self, prefixes, **kwargs): self.rbldnsd = Rbldnsd(**kwargs) zonedata = (self._zone_entry(*prefix) for prefix in prefixes) self.rbldnsd.add_dataset('ip6trie', ZoneFile(zonedata)) def __enter__(self): self.rbldnsd.__enter__() return self def __exit__(self, exc_type, exc_value, exc_tb): return self.rbldnsd.__exit__(exc_type, exc_value, exc_tb) def lookup(self, prefix, plen): prefix = pad_prefix(prefix, plen) nibbles = '.'.join("%x" % ((prefix >> n) & 0x0f) for n in range(0, 128, 4)) return self.rbldnsd.query(nibbles + '.example.com') def _zone_entry(self, prefix, plen, data): prefix = pad_prefix(prefix, plen) ip6addr = ':'.join("%x" % ((prefix >> n) & 0xffff) for n in range(112, -16, -16)) return "%s/%u :1:%s" % (ip6addr, plen, data)
def daemon(acl, addr='localhost'): """ Create an Rbldnsd instance with given ACL """ acl_zone = NamedTemporaryFile() acl_zone.writelines("%s\n" % line for line in acl) acl_zone.flush() dnsd = Rbldnsd(daemon_addr=addr) dnsd.add_dataset('acl', acl_zone) dnsd.add_dataset('generic', ZoneFile(['test TXT "Success"'])) return dnsd
def ip6trie(zone_data): """ Run rbldnsd with an ip6trie dataset """ dnsd = Rbldnsd() dnsd.add_dataset('ip6trie', ZoneFile(zone_data)) return dnsd
def ip4trie(zone_data): """ Run rbldnsd with an ip6trie dataset """ dnsd = Rbldnsd() dnsd.add_dataset('ip4trie', ZoneFile(zone_data)) return dnsd