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 dns.name 
25  import dns.ipv6 
26  import dns.ipv4 
27   
28  ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.') 
29  ipv6_reverse_domain = dns.name.from_text('ip6.arpa.') 
30   
31 -def from_address(text):
32 """Convert an IPv4 or IPv6 address in textual form into a Name object whose 33 value is the reverse-map domain name of the address. 34 @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1', 35 '::1') 36 @type text: str 37 @rtype: dns.name.Name object 38 """ 39 try: 40 v6 = dns.ipv6.inet_aton(text) 41 if dns.ipv6.is_mapped(v6): 42 parts = ['%d' % ord(byte) for byte in v6[12:]] 43 origin = ipv4_reverse_domain 44 else: 45 parts = list(v6.encode('hex_codec')) 46 origin = ipv6_reverse_domain 47 except: 48 parts = ['%d' % ord(byte) for byte in dns.ipv4.inet_aton(text)] 49 origin = ipv4_reverse_domain 50 parts.reverse() 51 return dns.name.from_text('.'.join(parts), origin=origin)
52
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 = '.'.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(''.join(labels[i:i+4])) 75 i += 4 76 text = ':'.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')
81