1 | #!/usr/bin/python |
---|
2 | from twisted.internet import reactor |
---|
3 | from twisted.names import server |
---|
4 | from twisted.names import dns |
---|
5 | from twisted.names import common |
---|
6 | from twisted.internet import defer |
---|
7 | from twisted.python import failure |
---|
8 | |
---|
9 | import sipb_xen_database |
---|
10 | import psycopg2 |
---|
11 | import sqlalchemy |
---|
12 | import time |
---|
13 | |
---|
14 | class DatabaseAuthority(common.ResolverBase): |
---|
15 | """An Authority that is loaded from a file.""" |
---|
16 | |
---|
17 | soa = None |
---|
18 | |
---|
19 | def __init__(self, domain, database=None): |
---|
20 | common.ResolverBase.__init__(self) |
---|
21 | if database is not None: |
---|
22 | sipb_xen_database.connect(database) |
---|
23 | self.domain = domain |
---|
24 | self.soa = dns.Record_SOA(mname='sipb-xen-dev.mit.edu', |
---|
25 | rname='sipb-xen.mit.edu', |
---|
26 | serial=1, refresh=3600, retry=900, |
---|
27 | expire=3600000, minimum=21600, ttl=3600) |
---|
28 | def _lookup(self, name, cls, type, timeout = None): |
---|
29 | print 'Starting lookup' |
---|
30 | for i in range(3): |
---|
31 | try: |
---|
32 | value = self._lookup_unsafe(name, cls, type, timeout = None) |
---|
33 | except (psycopg2.OperationalError, sqlalchemy.exceptions.SQLError): |
---|
34 | if i == 2: |
---|
35 | print 'failed' |
---|
36 | raise |
---|
37 | print "Reloading database" |
---|
38 | time.sleep(0.5) |
---|
39 | continue |
---|
40 | else: |
---|
41 | print 'done' |
---|
42 | return value |
---|
43 | |
---|
44 | def _lookup_unsafe(self, name, cls, type, timeout): |
---|
45 | sipb_xen_database.clear_cache() |
---|
46 | if not (name.lower() == self.domain or |
---|
47 | name.lower().endswith('.'+self.domain)): |
---|
48 | #Not us |
---|
49 | return defer.fail(failure.Failure(dns.DomainError(name))) |
---|
50 | results = [] |
---|
51 | if cls == dns.IN and type in (dns.A, dns.ALL_RECORDS): |
---|
52 | host = name[:-len(self.domain)-1] |
---|
53 | value = sipb_xen_database.Machine.get_by(name=host) |
---|
54 | if value is None or not value.nics: |
---|
55 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
56 | ip = value.nics[0].ip |
---|
57 | if ip is None: #Deactivated? |
---|
58 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
59 | ttl = 900 |
---|
60 | record = dns.Record_A(ip, ttl) |
---|
61 | results.append(dns.RRHeader(name, dns.A, dns.IN, |
---|
62 | ttl, record, auth=True)) |
---|
63 | authority = [] |
---|
64 | authority.append(dns.RRHeader(self.domain, dns.SOA, dns.IN, 3600, |
---|
65 | self.soa, auth=True)) |
---|
66 | return defer.succeed((results, authority, [])) |
---|
67 | #Doesn't exist |
---|
68 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
69 | |
---|
70 | if '__main__' == __name__: |
---|
71 | resolver = DatabaseAuthority('servers.csail.mit.edu', |
---|
72 | 'postgres://sipb-xen@sipb-xen-dev/sipb_xen') |
---|
73 | |
---|
74 | verbosity = 0 |
---|
75 | f = server.DNSServerFactory(authorities=[resolver], verbose=verbosity) |
---|
76 | p = dns.DNSDatagramProtocol(f) |
---|
77 | f.noisy = p.noisy = verbosity |
---|
78 | |
---|
79 | reactor.listenUDP(53, p) |
---|
80 | reactor.listenTCP(53, f) |
---|
81 | reactor.run() |
---|