Debian Codegraph @

dns/reversename.py

raw
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.exception
23 import dns.ipv4
24 import dns.ipv6
25 import dns.name
26
27 ipv4_reverse_domain = dns.name.from_text("in-addr.arpa.")
28 ipv6_reverse_domain = dns.name.from_text("ip6.arpa.")
29
30
31 def from_address(
32 text: str,
33 v4_origin: dns.name.Name = ipv4_reverse_domain,
34 v6_origin: dns.name.Name = ipv6_reverse_domain,
35 ) -> dns.name.Name:
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
39 *text*, a ``str``, is an IPv4 or IPv6 address in textual form
40 (e.g. '127.0.0.1', '::1')
41
42 *v4_origin*, a ``dns.name.Name`` to append to the labels corresponding to
43 the address if the address is an IPv4 address, instead of the default
44 (in-addr.arpa.)
45
46 *v6_origin*, a ``dns.name.Name`` to append to the labels corresponding to
47 the address if the address is an IPv6 address, instead of the default
48 (ip6.arpa.)
49
50 Raises ``dns.exception.SyntaxError`` if the address is badly formed.
51
52 Returns a ``dns.name.Name``.
53 """
54
55 try:
56 v6 = dns.ipv6.inet_aton(text)
57 if dns.ipv6.is_mapped(v6):
58 parts = [str(byte) for byte in v6[12:]]
59 origin = v4_origin
60 else:
61 parts = [x for x in str(binascii.hexlify(v6).decode())]
62 origin = v6_origin
63 except Exception:
64 parts = [str(byte) for byte in dns.ipv4.inet_aton(text)]
65 origin = v4_origin
66 return dns.name.from_text(".".join(reversed(parts)), origin=origin)
67
68
69 def to_address(
70 name: dns.name.Name,
71 v4_origin: dns.name.Name = ipv4_reverse_domain,
72 v6_origin: dns.name.Name = ipv6_reverse_domain,
73 ) -> str:
74 """Convert a reverse map domain name into textual address form.
75
76 *name*, a ``dns.name.Name``, an IPv4 or IPv6 address in reverse-map name
77 form.
78
79 *v4_origin*, a ``dns.name.Name`` representing the top-level domain for
80 IPv4 addresses, instead of the default (in-addr.arpa.)
81
82 *v6_origin*, a ``dns.name.Name`` representing the top-level domain for
83 IPv4 addresses, instead of the default (ip6.arpa.)
84
85 Raises ``dns.exception.SyntaxError`` if the name does not have a
86 reverse-map form.
87
88 Returns a ``str``.
89 """
90
91 if name.is_subdomain(v4_origin):
92 name = name.relativize(v4_origin)
93 text = b".".join(reversed(name.labels))
94 # run through inet_ntoa() to check syntax and make pretty.
95 return dns.ipv4.inet_ntoa(dns.ipv4.inet_aton(text))
96 elif name.is_subdomain(v6_origin):
97 name = name.relativize(v6_origin)
98 labels = list(reversed(name.labels))
99 parts = []
100 for i in range(0, len(labels), 4):
101 parts.append(b"".join(labels[i : i + 4]))
102 text = b":".join(parts)
103 # run through inet_ntoa() to check syntax and make pretty.
104 return dns.ipv6.inet_ntoa(dns.ipv6.inet_aton(text))
105 else:
106 raise dns.exception.SyntaxError("unknown reverse-map address family")