reversename.py 3.2 KB

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