def to_text(self, origin=None, relativize=True, **kw): bits = [] for i in xrange(0, len(self.bitmap)): byte = self.bitmap[i] for j in xrange(0, 8): if byte & (0x80 >> j): bits.append(str(i * 8 + j)) text = ' '.join(bits) return '%s %d %s' % (self.address, self.protocol, text)
def testLRUExpiration(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 4): name = dns.name.from_text("example%d." % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) time.sleep(2) for i in xrange(0, 4): name = dns.name.from_text("example%d." % i) self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def testLRUExpiration(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 4): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) time.sleep(2) for i in xrange(0, 4): name = dns.name.from_text('example%d.' % i) self.failUnless( cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def testLRUReplace(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 5): name = dns.name.from_text("example%d." % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) for i in xrange(0, 5): name = dns.name.from_text("example%d." % i) if i == 0: self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None) else: self.failUnless(not cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def to_text(self, origin=None, relativize=True, **kw): text = '' for (window, bitmap) in self.windows: bits = [] for i in xrange(0, len(bitmap)): byte = bitmap[i] for j in xrange(0, 8): if byte & (0x80 >> j): bits.append( dns.rdatatype.to_text(window * 256 + i * 8 + j)) text += (' ' + ' '.join(bits)) return '%d %d%s' % (self.serial, self.flags, text)
def to_text(self, origin=None, relativize=True, **kw): text = '' for (window, bitmap) in self.windows: bits = [] for i in xrange(0, len(bitmap)): byte = bitmap[i] for j in xrange(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) text += (' ' + ' '.join(bits)) return '%d %d%s' % (self.serial, self.flags, text)
def to_text(self, origin=None, relativize=True, **kw): next = self.next.choose_relativity(origin, relativize) text = '' for (window, bitmap) in self.windows: bits = [] for i in xrange(0, len(bitmap)): byte = bitmap[i] for j in xrange(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) text += (' ' + ' '.join(bits)) return '%s%s' % (next, text)
def testLRUReplace(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 5): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) for i in xrange(0, 5): name = dns.name.from_text('example%d.' % i) if i == 0: self.failUnless( cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None) else: self.failUnless(not cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): address = tok.get_string() protocol = tok.get_string() if protocol.isdigit(): protocol = int(protocol) else: protocol = socket.getprotobyname(protocol) bitmap = bytearray() while 1: token = tok.get().unescape() if token.is_eol_or_eof(): break if token.value.isdigit(): serv = int(token.value) else: if protocol != _proto_udp and protocol != _proto_tcp: raise NotImplementedError("protocol must be TCP or UDP") if protocol == _proto_udp: protocol_text = "udp" else: protocol_text = "tcp" serv = socket.getservbyname(token.value, protocol_text) i = serv // 8 l = len(bitmap) if l < i + 1: for j in xrange(l, i + 1): bitmap.append(0) bitmap[i] = bitmap[i] | (0x80 >> (serv % 8)) bitmap = dns.rdata._truncate_bitmap(bitmap) return cls(rdclass, rdtype, address, protocol, bitmap)
def bad(): q = dns.message.from_text(query_text) for i in xrange(0, 25): rrset = dns.rrset.from_text('foo%d.' % i, 3600, dns.rdataclass.IN, dns.rdatatype.A, '10.0.0.%d' % i) q.additional.append(rrset) q.to_wire(max_size=512)
def testLRUDoesLRU(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 4): name = dns.name.from_text("example%d." % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) name = dns.name.from_text("example0.") cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) # The LRU is now example1. name = dns.name.from_text("example4.") answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) for i in xrange(0, 5): name = dns.name.from_text("example%d." % i) if i == 1: self.failUnless(cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None) else: self.failUnless(not cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def to_text(self, origin=None, relativize=True, **kw): next = base64.b32encode(self.next).translate( b32_normal_to_hex).lower().decode() if self.salt == b'': salt = '-' else: salt = binascii.hexlify(self.salt).decode() text = u'' for (window, bitmap) in self.windows: bits = [] for i in xrange(0, len(bitmap)): byte = bitmap[i] for j in xrange(0, 8): if byte & (0x80 >> j): bits.append(dns.rdatatype.to_text(window * 256 + i * 8 + j)) text += (u' ' + u' '.join(bits)) return u'%u %u %u %s %s%s' % (self.algorithm, self.flags, self.iterations, salt, next, text)
def _exponent_of(what, desc): if what == 0: return 0 exp = None for i in xrange(len(_pows)): if what // _pows[i] == long(0): exp = i - 1 break if exp is None or exp < 0: raise dns.exception.SyntaxError("%s value out of bounds" % desc) return exp
def testLRUDoesLRU(self): cache = dns.resolver.LRUCache(4) for i in xrange(0, 4): name = dns.name.from_text('example%d.' % i) answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) name = dns.name.from_text('example0.') cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) # The LRU is now example1. name = dns.name.from_text('example4.') answer = FakeAnswer(time.time() + 1) cache.put((name, dns.rdatatype.A, dns.rdataclass.IN), answer) for i in xrange(0, 5): name = dns.name.from_text('example%d.' % i) if i == 1: self.failUnless( cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None) else: self.failUnless(not cache.get((name, dns.rdatatype.A, dns.rdataclass.IN)) is None)
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): text = tok.get_string() tok.get_eol() if len(text) != cls.text_len: raise dns.exception.SyntaxError( 'Input text must have %s characters' % cls.text_len) expected_dash_idxs = xrange(2, cls.byte_len * 3 - 1, 3) for i in expected_dash_idxs: if text[i] != '-': raise dns.exception.SyntaxError('Dash expected at position %s' % i) text = text.replace('-', '') try: data = binascii.unhexlify(text.encode()) except (ValueError, TypeError) as ex: raise dns.exception.SyntaxError('Hex decoding error: %s' % str(ex)) return cls(rdclass, rdtype, data)
def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True): text = tok.get_string() tok.get_eol() if len(text) != cls.text_len: raise dns.exception.SyntaxError( 'Input text must have %s characters' % cls.text_len) expected_dash_idxs = xrange(2, cls.byte_len * 3 - 1, 3) for i in expected_dash_idxs: if text[i] != '-': raise dns.exception.SyntaxError( 'Dash expected at position %s' % i) text = text.replace('-', '') try: data = binascii.unhexlify(text.encode()) except (ValueError, TypeError) as ex: raise dns.exception.SyntaxError('Hex decoding error: %s' % str(ex)) return cls(rdclass, rdtype, data)
def to_wire(self, file): if self.family == 1: address = dns.inet.inet_pton(dns.inet.AF_INET, self.address) elif self.family == 2: address = dns.inet.inet_pton(dns.inet.AF_INET6, self.address) else: address = binascii.unhexlify(self.address) # # Truncate least significant zero bytes. # last = 0 for i in xrange(len(address) - 1, -1, -1): if address[i] != chr(0): last = i + 1 break address = address[0:last] l = len(address) assert l < 128 if self.negation: l |= 0x80 header = struct.pack('!HBB', self.family, self.prefix, l) file.write(header) file.write(address)
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): (order, preference) = struct.unpack('!HH', wire[current: current + 4]) current += 4 rdlen -= 4 strings = [] for i in xrange(3): l = wire[current] current += 1 rdlen -= 1 if l > rdlen or rdlen < 0: raise dns.exception.FormError s = wire[current: current + l].unwrap() current += l rdlen -= l strings.append(s) (replacement, cused) = dns.name.from_wire(wire[: current + rdlen], current) if cused != rdlen: raise dns.exception.FormError if origin is not None: replacement = replacement.relativize(origin) return cls(rdclass, rdtype, order, preference, strings[0], strings[1], strings[2], replacement)
def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin=None): (order, preference) = struct.unpack('!HH', wire[current:current + 4]) current += 4 rdlen -= 4 strings = [] for i in xrange(3): l = wire[current] current += 1 rdlen -= 1 if l > rdlen or rdlen < 0: raise dns.exception.FormError s = wire[current:current + l].unwrap() current += l rdlen -= l strings.append(s) (replacement, cused) = dns.name.from_wire(wire[:current + rdlen], current) if cused != rdlen: raise dns.exception.FormError if origin is not None: replacement = replacement.relativize(origin) return cls(rdclass, rdtype, order, preference, strings[0], strings[1], strings[2], replacement)
def to_wire(self, file): if self.family == 1: address = dns.inet.inet_pton(dns.inet.AF_INET, self.address) elif self.family == 2: address = dns.inet.inet_pton(dns.inet.AF_INET6, self.address) else: address = binascii.unhexlify(self.address) # # Truncate least significant zero bytes. # last = 0 for i in xrange(len(address) - 1, -1, -1): if address[i] != chr(0): last = i + 1 break address = address[0: last] l = len(address) assert l < 128 if self.negation: l |= 0x80 header = struct.pack('!HBB', self.family, self.prefix, l) file.write(header) file.write(address)