From: Bob Halley Date: Mon, 26 Dec 2005 01:39:53 +0000 (+0000) Subject: add reversemap module X-Git-Tag: v1.4.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e44553b48021d9e976fe8c4095349c39963c4258;p=thirdparty%2Fdnspython.git add reversemap module --- diff --git a/dns/__init__.py b/dns/__init__.py index f14e06a1..2b888242 100644 --- a/dns/__init__.py +++ b/dns/__init__.py @@ -35,6 +35,7 @@ __all__ = [ 'rdatatype', 'renderer', 'resolver', + 'reversemap', 'rrset', 'set', 'tokenizer', diff --git a/dns/reversemap.py b/dns/reversemap.py new file mode 100644 index 00000000..2a4998f3 --- /dev/null +++ b/dns/reversemap.py @@ -0,0 +1,39 @@ +# Copyright (C) 2005 Nominum, Inc. +# +# Permission to use, copy, modify, and distribute this software and its +# documentation for any purpose with or without fee is hereby granted, +# provided that the above copyright notice and this permission notice +# appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES +# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR +# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT +# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +"""DNS Reverse Map Names.""" + +import dns.name +import dns.ipv6 +import dns.ipv4 + +def from_text(text): + """Convert an IPv4 or IPv6 address in textual form into a Name object whose + value is the reverse-map domain name of the address. + @param text: an IPv4 or IPv6 address in textual form (e.g. '127.0.0.1', + '::1') + @type text: str + @rtype: dns.name.Name object + """ + + try: + parts = list(dns.ipv6.inet_aton(text).encode('hex_codec')) + suffix = 'ip6.arpa.' + except: + parts = ['%d' % ord(byte) for byte in dns.ipv4.inet_aton(text)] + suffix = 'in-addr.arpa.' + parts.reverse() + parts.append(suffix) + return dns.name.from_text('.'.join(parts))