reversename.py 3.8 KB

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