dns.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import struct
  2. import dns
  3. import dns.rdtypes.txtbase
  4. import dns.rdtypes.ANY.OPENPGPKEY
  5. class LongQuotedTXT(dns.rdtypes.txtbase.TXTBase):
  6. """
  7. A TXT record like RFC 1035, but
  8. - allows arbitrarily long tokens, and
  9. - all tokens must be quoted.
  10. """
  11. @classmethod
  12. def from_text(cls, rdclass, rdtype, tok, origin=None, relativize=True):
  13. strings = []
  14. while 1:
  15. token = tok.get().unescape()
  16. if token.is_eol_or_eof():
  17. break
  18. if not token.is_quoted_string():
  19. raise dns.exception.SyntaxError("Content must be quoted.")
  20. value = token.value
  21. if isinstance(value, bytes):
  22. strings.append(value)
  23. else:
  24. strings.append(value.encode())
  25. if len(strings) == 0:
  26. raise dns.exception.UnexpectedEnd
  27. return cls(rdclass, rdtype, strings)
  28. def to_wire(self, file, compress=None, origin=None):
  29. for long_s in self.strings:
  30. for s in [long_s[i:i+255] for i in range(0, max(len(long_s), 1), 255)]:
  31. l = len(s)
  32. assert l < 256
  33. file.write(struct.pack('!B', l))
  34. file.write(s)
  35. class OPENPGPKEY(dns.rdtypes.ANY.OPENPGPKEY.OPENPGPKEY):
  36. # TODO remove when https://github.com/rthalley/dnspython/commit/d6a95982fcd454a10467260bfb874c3c9d31d06f was
  37. # released
  38. def to_text(self, origin=None, relativize=True, **kw):
  39. return super().to_text(origin, relativize, **kw).replace(' ', '')