Home | Trees | Indices | Help |
|
---|
|
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 base64 25 26 import dns.name 27 import dns.ipv6 28 import dns.ipv4 29 30 ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.') 31 ipv6_reverse_domain = dns.name.from_text('ip6.arpa.') 3234 """Convert an IPv4 or IPv6 address in textual form into a Name object whose 35 value is the reverse-map domain name of the address. 36 @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1', 37 '::1') 38 @type text: str 39 @rtype: dns.name.Name object 40 """ 41 try: 42 parts = ['%x.%x' % (byte & 0x0f, byte >> 4) for byte in dns.ipv6.inet_aton(text)] 43 origin = ipv6_reverse_domain 44 except: 45 parts = ['%d' % byte for byte in dns.ipv4.inet_aton(text)] 46 origin = ipv4_reverse_domain 47 parts.reverse() 48 return dns.name.from_text('.'.join(parts).lower(), origin=origin)4951 """Convert a reverse map domain name into textual address form. 52 @param name: an IPv4 or IPv6 address in reverse-map form. 53 @type name: dns.name.Name object 54 @rtype: str 55 """ 56 if name.is_subdomain(ipv4_reverse_domain): 57 name = name.relativize(ipv4_reverse_domain) 58 labels = list(name.labels) 59 labels.reverse() 60 text = '.'.join([x.decode('ascii') for x in labels]) 61 # run through inet_aton() to check syntax and make pretty. 62 return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text)) 63 elif name.is_subdomain(ipv6_reverse_domain): 64 name = name.relativize(ipv6_reverse_domain) 65 labels = list(name.labels) 66 labels.reverse() 67 parts = [] 68 i = 0 69 l = len(labels) 70 while i < l: 71 parts.append(''.join([x.decode('ascii') for x in labels[i:i+4]])) 72 i += 4 73 text = ':'.join(parts) 74 # run through inet_aton() to check syntax and make pretty. 75 return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text)) 76 else: 77 raise dns.exception.SyntaxError('unknown reverse-map address family')78
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sun Mar 31 05:21:21 2013 | http://epydoc.sourceforge.net |