reversename.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.')
  22. ipv6_reverse_domain = dns.name.from_text('ip6.arpa.')
  23. def from_address(text, v4_origin=ipv4_reverse_domain,
  24. v6_origin=ipv6_reverse_domain):
  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 ``str``, is an IPv4 or IPv6 address in textual form
  28. (e.g. '127.0.0.1', '::1')
  29. *v4_origin*, a ``dns.name.Name`` to append to the labels corresponding to
  30. the address if the address is an IPv4 address, instead of the default
  31. (in-addr.arpa.)
  32. *v6_origin*, a ``dns.name.Name`` to append to the labels corresponding to
  33. the address if the address is an IPv6 address, instead of the default
  34. (ip6.arpa.)
  35. Raises ``dns.exception.SyntaxError`` if the address is badly formed.
  36. Returns a ``dns.name.Name``.
  37. """
  38. try:
  39. v6 = dns.ipv6.inet_aton(text)
  40. if dns.ipv6.is_mapped(v6):
  41. parts = ['%d' % byte for byte in v6[12:]]
  42. origin = v4_origin
  43. else:
  44. parts = [x for x in str(binascii.hexlify(v6).decode())]
  45. origin = v6_origin
  46. except Exception:
  47. parts = ['%d' %
  48. byte for byte in dns.ipv4.inet_aton(text)]
  49. origin = v4_origin
  50. return dns.name.from_text('.'.join(reversed(parts)), origin=origin)
  51. def to_address(name, v4_origin=ipv4_reverse_domain,
  52. v6_origin=ipv6_reverse_domain):
  53. """Convert a reverse map domain name into textual address form.
  54. *name*, a ``dns.name.Name``, an IPv4 or IPv6 address in reverse-map name
  55. form.
  56. *v4_origin*, a ``dns.name.Name`` representing the top-level domain for
  57. IPv4 addresses, instead of the default (in-addr.arpa.)
  58. *v6_origin*, a ``dns.name.Name`` representing the top-level domain for
  59. IPv4 addresses, instead of the default (ip6.arpa.)
  60. Raises ``dns.exception.SyntaxError`` if the name does not have a
  61. reverse-map form.
  62. Returns a ``str``.
  63. """
  64. if name.is_subdomain(v4_origin):
  65. name = name.relativize(v4_origin)
  66. text = b'.'.join(reversed(name.labels))
  67. # run through inet_ntoa() to check syntax and make pretty.
  68. return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text))
  69. elif name.is_subdomain(v6_origin):
  70. name = name.relativize(v6_origin)
  71. labels = list(reversed(name.labels))
  72. parts = []
  73. for i in range(0, len(labels), 4):
  74. parts.append(b''.join(labels[i:i + 4]))
  75. text = b':'.join(parts)
  76. # run through inet_ntoa() to check syntax and make pretty.
  77. return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text))
  78. else:
  79. raise dns.exception.SyntaxError('unknown reverse-map address family')