You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.2 KiB

4 years ago
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2006-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. """DNS Reverse Map Names."""
  17. import binascii
  18. import dns.name
  19. import dns.ipv6
  20. import dns.ipv4
  21. from dns._compat import PY3
  22. ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.')
  23. ipv6_reverse_domain = dns.name.from_text('ip6.arpa.')
  24. def from_address(text):
  25. """Convert an IPv4 or IPv6 address in textual form into a Name object whose
  26. value is the reverse-map domain name of the address.
  27. *text*, a ``text``, is an IPv4 or IPv6 address in textual form
  28. (e.g. '127.0.0.1', '::1')
  29. Raises ``dns.exception.SyntaxError`` if the address is badly formed.
  30. Returns a ``dns.name.Name``.
  31. """
  32. try:
  33. v6 = dns.ipv6.inet_aton(text)
  34. if dns.ipv6.is_mapped(v6):
  35. if PY3:
  36. parts = ['%d' % byte for byte in v6[12:]]
  37. else:
  38. parts = ['%d' % ord(byte) for byte in v6[12:]]
  39. origin = ipv4_reverse_domain
  40. else:
  41. parts = [x for x in str(binascii.hexlify(v6).decode())]
  42. origin = ipv6_reverse_domain
  43. except Exception:
  44. parts = ['%d' %
  45. byte for byte in bytearray(dns.ipv4.inet_aton(text))]
  46. origin = ipv4_reverse_domain
  47. parts.reverse()
  48. return dns.name.from_text('.'.join(parts), origin=origin)
  49. def to_address(name):
  50. """Convert a reverse map domain name into textual address form.
  51. *name*, a ``dns.name.Name``, an IPv4 or IPv6 address in reverse-map name
  52. form.
  53. Raises ``dns.exception.SyntaxError`` if the name does not have a
  54. reverse-map form.
  55. Returns a ``text``.
  56. """
  57. if name.is_subdomain(ipv4_reverse_domain):
  58. name = name.relativize(ipv4_reverse_domain)
  59. labels = list(name.labels)
  60. labels.reverse()
  61. text = b'.'.join(labels)
  62. # run through inet_aton() to check syntax and make pretty.
  63. return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text))
  64. elif name.is_subdomain(ipv6_reverse_domain):
  65. name = name.relativize(ipv6_reverse_domain)
  66. labels = list(name.labels)
  67. labels.reverse()
  68. parts = []
  69. i = 0
  70. l = len(labels)
  71. while i < l:
  72. parts.append(b''.join(labels[i:i + 4]))
  73. i += 4
  74. text = b':'.join(parts)
  75. # run through inet_aton() to check syntax and make pretty.
  76. return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text))
  77. else:
  78. raise dns.exception.SyntaxError('unknown reverse-map address family')