Package dns :: Module reversename |
[hide private]
[frames] | no frames]

# Source Code for Module dns.reversename

     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
    16  """DNS Reverse Map Names.
    17
    18  @var ipv4_reverse_domain: The DNS IPv4 reverse-map domain, in-addr.arpa.
    19  @type ipv4_reverse_domain: dns.name.Name object
    20  @var ipv6_reverse_domain: The DNS IPv6 reverse-map domain, ip6.arpa.
    21  @type ipv6_reverse_domain: dns.name.Name object
    22  """
    23
    24  import binascii
    25  import sys
    26
    27  import dns.name
    28  import dns.ipv6
    29  import dns.ipv4
    30
    31  ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.')
    32  ipv6_reverse_domain = dns.name.from_text('ip6.arpa.')
    33
    34
    35 -def from_address(text):
    36      """Convert an IPv4 or IPv6 address in textual form into a Name object whose
    37      value is the reverse-map domain name of the address.
    38      @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1',
    39      '::1')
    40      @type text: str
    41      @rtype: dns.name.Name object
    42      """
    43      try:
    44          v6 = dns.ipv6.inet_aton(text)
    45          if dns.ipv6.is_mapped(v6):
    46              if sys.version_info >= (3,):
    47                  parts = ['%d' % byte for byte in v6[12:]]
    48              else:
    49                  parts = ['%d' % ord(byte) for byte in v6[12:]]
    50              origin = ipv4_reverse_domain
    51          else:
    52              parts = [x for x in str(binascii.hexlify(v6).decode())]
    53              origin = ipv6_reverse_domain
    54      except Exception:
    55          parts = ['%d' %
    56                   byte for byte in bytearray(dns.ipv4.inet_aton(text))]
    57          origin = ipv4_reverse_domain
    58      parts.reverse()
    59      return dns.name.from_text('.'.join(parts), origin=origin)
    60
    61
    62 -def to_address(name):
    63      """Convert a reverse map domain name into textual address form.
    64      @param name: an IPv4 or IPv6 address in reverse-map form.
    65      @type name: dns.name.Name object
    66      @rtype: str
    67      """
    68      if name.is_subdomain(ipv4_reverse_domain):
    69          name = name.relativize(ipv4_reverse_domain)
    70          labels = list(name.labels)
    71          labels.reverse()
    72          text = b'.'.join(labels)
    73          # run through inet_aton() to check syntax and make pretty.
    74          return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text))
    75      elif name.is_subdomain(ipv6_reverse_domain):
    76          name = name.relativize(ipv6_reverse_domain)
    77          labels = list(name.labels)
    78          labels.reverse()
    79          parts = []
    80          i = 0
    81          l = len(labels)
    82          while i < l:
    83              parts.append(b''.join(labels[i:i + 4]))
    84              i += 4
    85          text = b':'.join(parts)
    86          # run through inet_aton() to check syntax and make pretty.
    87          return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text))
    88      else:
    89          raise dns.exception.SyntaxError('unknown reverse-map address family')
    90