source: trunk/packages/sipb-xen-dns/code/dnsserver.py @ 301

Last change on this file since 301 was 300, checked in by ecprice, 17 years ago

Clear the sql cache for dns and dhcp, fixing #42.

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/python
2from twisted.internet import reactor
3from twisted.names import server
4from twisted.names import dns
5from twisted.names import common
6from twisted.internet import defer
7from twisted.python import failure
8
9import sipb_xen_database
10
11class DatabaseAuthority(common.ResolverBase):
12    """An Authority that is loaded from a file."""
13
14    soa = None
15
16    def __init__(self, domain, database=None):
17        common.ResolverBase.__init__(self)
18        if database is not None:
19            sipb_xen_database.connect(database)
20        self.domain = domain
21        self.soa = dns.Record_SOA(mname='sipb-xen-dev.mit.edu', 
22                                  rname='sipb-xen.mit.edu',
23                                  serial=1, refresh=3600, retry=900,
24                                  expire=3600000, minimum=21600, ttl=3600)
25    def _lookup(self, name, cls, type, timeout = None):
26        sipb_xen_database.clear_cache()
27        if not (name.lower() == self.domain or 
28                name.lower().endswith('.'+self.domain)):
29            #Not us
30            return defer.fail(failure.Failure(dns.DomainError(name)))
31        results = []
32        if cls == dns.IN and type in (dns.A, dns.ALL_RECORDS):
33            host = name[:-len(self.domain)-1]
34            value = sipb_xen_database.Machine.get_by(name=host)
35            if value is None or not value.nics:
36                return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
37            ip = value.nics[0].ip
38            if ip is None:  #Deactivated?
39                return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
40            ttl = 900
41            record = dns.Record_A(ip, ttl)
42            results.append(dns.RRHeader(name, dns.A, dns.IN, 
43                                        ttl, record, auth=True))
44        authority = []
45        authority.append(dns.RRHeader(self.domain, dns.SOA, dns.IN, 3600,
46                                      self.soa, auth=True))
47        return defer.succeed((results, authority, []))
48        #Doesn't exist
49        return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
50
51if '__main__' == __name__:
52    resolver = DatabaseAuthority('servers.csail.mit.edu',
53                                 'postgres://sipb-xen@sipb-xen-dev/sipb_xen')
54
55    verbosity = 0
56    f = server.DNSServerFactory(authorities=[resolver], verbose=verbosity)
57    p = dns.DNSDatagramProtocol(f)
58    f.noisy = p.noisy = verbosity
59   
60    reactor.listenUDP(53, p)
61    reactor.listenTCP(53, f)
62    reactor.run()
Note: See TracBrowser for help on using the repository browser.