]>
jfr.im git - irc/quakenet/qwebirc.git/blob - qwebirc/dns.py
1 from twisted
.names
import client
2 from twisted
.internet
import reactor
, defer
4 class LookupException(Exception): pass
5 class VerificationException(Exception): pass
6 TimeoutException
= defer
.TimeoutError
8 def lookupPTR(ip
, *args
, **kwargs
):
10 answer
, auth
, add
= result
13 raise LookupException
, "No ANSWERS in PTR response for %s." % repr(ip
)
14 return str(answer
[0].payload
.name
)
16 ptr
= ".".join(ip
.split(".")[::-1]) + ".in-addr.arpa."
17 return client
.lookupPointer(ptr
, **kwargs
).addCallback(callback
)
19 def lookupAs(hostname
, *args
, **kwargs
):
21 answer
, auth
, add
= result
23 raise LookupException
, "No ANSWERS in A response for %s." % repr(hostname
)
24 return [x
.payload
.dottedQuad() for x
in answer
]
26 return client
.lookupAddress(hostname
, *args
, **kwargs
).addCallback(callback
)
28 def lookupAndVerifyPTR(ip
, *args
, **kwargs
):
31 def gotPTRResult(ptr
):
32 def gotAResult(a_records
):
36 raise VerificationException("IP mismatch: %s != %s%s" % (repr(ip
), repr(ptr
), repr(a_records
)))
37 lookupAs(ptr
, *args
, **kwargs
).addCallback(gotAResult
).addErrback(d
.errback
)
39 lookupPTR(ip
, *args
, **kwargs
).addCallback(gotPTRResult
).addErrback(d
.errback
)
42 if __name__
== "__main__":
53 d
= lookupAndVerifyPTR(sys
.argv
[1], timeout
=[.001])
54 d
.addCallbacks(callback
, errback
)