[181] | 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 |
---|
[302] | 10 | import psycopg2 |
---|
| 11 | import sqlalchemy |
---|
| 12 | import time |
---|
[181] | 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): |
---|
[302] | 29 | for i in range(3): |
---|
| 30 | try: |
---|
| 31 | value = self._lookup_unsafe(name, cls, type, timeout = None) |
---|
| 32 | except (psycopg2.OperationalError, sqlalchemy.exceptions.SQLError): |
---|
| 33 | if i == 2: |
---|
| 34 | raise |
---|
| 35 | print "Reloading database" |
---|
| 36 | time.sleep(0.5) |
---|
| 37 | continue |
---|
| 38 | else: |
---|
| 39 | return value |
---|
| 40 | |
---|
| 41 | def _lookup_unsafe(self, name, cls, type, timeout): |
---|
[300] | 42 | sipb_xen_database.clear_cache() |
---|
[181] | 43 | if not (name.lower() == self.domain or |
---|
| 44 | name.lower().endswith('.'+self.domain)): |
---|
| 45 | #Not us |
---|
| 46 | return defer.fail(failure.Failure(dns.DomainError(name))) |
---|
| 47 | results = [] |
---|
| 48 | if cls == dns.IN and type in (dns.A, dns.ALL_RECORDS): |
---|
| 49 | host = name[:-len(self.domain)-1] |
---|
[226] | 50 | value = sipb_xen_database.Machine.get_by(name=host) |
---|
| 51 | if value is None or not value.nics: |
---|
[181] | 52 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
[226] | 53 | ip = value.nics[0].ip |
---|
[181] | 54 | if ip is None: #Deactivated? |
---|
| 55 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
| 56 | ttl = 900 |
---|
| 57 | record = dns.Record_A(ip, ttl) |
---|
| 58 | results.append(dns.RRHeader(name, dns.A, dns.IN, |
---|
| 59 | ttl, record, auth=True)) |
---|
| 60 | authority = [] |
---|
| 61 | authority.append(dns.RRHeader(self.domain, dns.SOA, dns.IN, 3600, |
---|
| 62 | self.soa, auth=True)) |
---|
| 63 | return defer.succeed((results, authority, [])) |
---|
| 64 | #Doesn't exist |
---|
| 65 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
| 66 | |
---|
| 67 | if '__main__' == __name__: |
---|
| 68 | resolver = DatabaseAuthority('servers.csail.mit.edu', |
---|
| 69 | 'postgres://sipb-xen@sipb-xen-dev/sipb_xen') |
---|
| 70 | |
---|
| 71 | verbosity = 0 |
---|
| 72 | f = server.DNSServerFactory(authorities=[resolver], verbose=verbosity) |
---|
| 73 | p = dns.DNSDatagramProtocol(f) |
---|
| 74 | f.noisy = p.noisy = verbosity |
---|
| 75 | |
---|
| 76 | reactor.listenUDP(53, p) |
---|
| 77 | reactor.listenTCP(53, f) |
---|
| 78 | reactor.run() |
---|