1 # Copyright (C) 2006, 2007, 2009-2011 Nominum, Inc.
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.
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.
16 """DNS Reverse Map Names.
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
28 ipv4_reverse_domain
= dns
.name
.from_text('in-addr.arpa.')
29 ipv6_reverse_domain
= dns
.name
.from_text('ip6.arpa.')
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',
37 @rtype: dns.name.Name object
40 parts
= list(dns
.ipv6
.inet_aton(text
).encode('hex_codec'))
41 origin
= ipv6_reverse_domain
43 parts
= ['%d' % ord(byte
) for byte
in dns
.ipv4
.inet_aton(text
)]
44 origin
= ipv4_reverse_domain
46 return dns
.name
.from_text('.'.join(parts
), origin
=origin
)
49 """Convert a reverse map domain name into textual address form.
50 @param name: an IPv4 or IPv6 address in reverse-map form.
51 @type name: dns.name.Name object
54 if name
.is_subdomain(ipv4_reverse_domain
):
55 name
= name
.relativize(ipv4_reverse_domain
)
56 labels
= list(name
.labels
)
58 text
= '.'.join(labels
)
59 # run through inet_aton() to check syntax and make pretty.
60 return dns
.ipv4
.inet_ntoa(dns
.ipv4
.inet_aton(text
))
61 elif name
.is_subdomain(ipv6_reverse_domain
):
62 name
= name
.relativize(ipv6_reverse_domain
)
63 labels
= list(name
.labels
)
69 parts
.append(''.join(labels
[i
:i
+4]))
71 text
= ':'.join(parts
)
72 # run through inet_aton() to check syntax and make pretty.
73 return dns
.ipv6
.inet_ntoa(dns
.ipv6
.inet_aton(text
))
75 raise dns
.exception
.SyntaxError('unknown reverse-map address family')