reversename.py 3.7 KB

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