1
# Copyright (C) 2008 PreludeIDS Technologies. All Rights Reserved.
2
# Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
4
# This file is part of the Prewikka program.
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2, or (at your option)
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program; see the file COPYING. If not, write to
18
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27
from twisted.internet import reactor
28
from twisted.names import client, dns, hosts, cache, resolve
29
except Exception, err:
33
from threading import Lock
35
from dummy_threading import Lock
42
self._cache = cache.CacheResolver()
43
rlist = [ self._cache, client.Resolver('/etc/resolv.conf') ]
44
self._resolve = resolve.ResolverChain(rlist)
46
def _error_cb(self, failure):
49
if failure.check(dns.DomainError, dns.AuthoritativeDomainError):
52
def _resolve_cb(self, (ans, auth, add), ptr, resolve_cb):
55
resolve_cb(str(ans[0].payload.name))
57
q = dns.Query(str(ans[0].name), ans[0].type, ans[0].cls)
58
self._cache.cacheResult(q, (ans, auth, add))
60
def _ip_reverse(self, addr):
62
parts = list(socket.inet_pton(socket.AF_INET6, addr).encode('hex_codec'))
65
parts = ["%d" % ord(byte) for byte in socket.inet_aton(addr)]
66
origin = ".in-addr.arpa"
69
return '.'.join(parts) + origin
71
def process(self, timeout=0):
72
end = now = time.time()
82
reactor.runUntilCurrent();
83
reactor.doIteration(timeout)
91
#print "max=%f elapsed:%f" % (timeout, end-now)
93
def doQuery(self, addr, resolve_cb):
97
self._resolve.lookupPointer(addr).addCallback(self._resolve_cb, addr, resolve_cb).addErrback(self._error_cb)
102
def resolve(self, addr, resolve_cb):
104
addr = self._ip_reverse(addr)
108
self.doQuery(addr, resolve_cb)
112
class AddressResolve:
113
def _resolve_cb(self, value):
116
def __init__(self, addr):
123
resolver.resolve(addr, self._resolve_cb)
126
return len(str(self))
132
return self._name or self._addr
138
def process(timeout=0):
142
resolver.process(timeout)
148
if env.dns_max_delay == -1:
152
env.log.warning("Asynchronous DNS resolution disabled: twisted.names and twisted.internet required: %s" % err)
155
resolver = DNSResolver()