Home | Trees | Indices | Help |
|
---|
|
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license 2 3 # Copyright (C) 2006-2017 Nominum, Inc. 4 # 5 # Permission to use, copy, modify, and distribute this software and its 6 # documentation for any purpose with or without fee is hereby granted, 7 # provided that the above copyright notice and this permission notice 8 # appear in all copies. 9 # 10 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES 11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR 13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 16 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18 """DNS Reverse Map Names.""" 19 20 import binascii 21 22 import dns.name 23 import dns.ipv6 24 import dns.ipv4 25 26 from dns._compat import PY3 27 28 ipv4_reverse_domain = dns.name.from_text('in-addr.arpa.') 29 ipv6_reverse_domain = dns.name.from_text('ip6.arpa.') 30 3133 """Convert an IPv4 or IPv6 address in textual form into a Name object whose 34 value is the reverse-map domain name of the address. 35 36 *text*, a ``text``, is an IPv4 or IPv6 address in textual form 37 (e.g. '127.0.0.1', '::1') 38 39 Raises ``dns.exception.SyntaxError`` if the address is badly formed. 40 41 Returns a ``dns.name.Name``. 42 """ 43 44 try: 45 v6 = dns.ipv6.inet_aton(text) 46 if dns.ipv6.is_mapped(v6): 47 if PY3: 48 parts = ['%d' % byte for byte in v6[12:]] 49 else: 50 parts = ['%d' % ord(byte) for byte in v6[12:]] 51 origin = ipv4_reverse_domain 52 else: 53 parts = [x for x in str(binascii.hexlify(v6).decode())] 54 origin = ipv6_reverse_domain 55 except Exception: 56 parts = ['%d' % 57 byte for byte in bytearray(dns.ipv4.inet_aton(text))] 58 origin = ipv4_reverse_domain 59 parts.reverse() 60 return dns.name.from_text('.'.join(parts), origin=origin)61 6264 """Convert a reverse map domain name into textual address form. 65 66 *name*, a ``dns.name.Name``, an IPv4 or IPv6 address in reverse-map name 67 form. 68 69 Raises ``dns.exception.SyntaxError`` if the name does not have a 70 reverse-map form. 71 72 Returns a ``text``. 73 """ 74 75 if name.is_subdomain(ipv4_reverse_domain): 76 name = name.relativize(ipv4_reverse_domain) 77 labels = list(name.labels) 78 labels.reverse() 79 text = b'.'.join(labels) 80 # run through inet_aton() to check syntax and make pretty. 81 return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text)) 82 elif name.is_subdomain(ipv6_reverse_domain): 83 name = name.relativize(ipv6_reverse_domain) 84 labels = list(name.labels) 85 labels.reverse() 86 parts = [] 87 i = 0 88 l = len(labels) 89 while i < l: 90 parts.append(b''.join(labels[i:i + 4])) 91 i += 4 92 text = b':'.join(parts) 93 # run through inet_aton() to check syntax and make pretty. 94 return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text)) 95 else: 96 raise dns.exception.SyntaxError('unknown reverse-map address family')97
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Dec 8 10:59:57 2018 | http://epydoc.sourceforge.net |