dyndns

Check-in [e2a053c2e7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Initial commit, DNS server framework
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:e2a053c2e77e71c741e6abfc25fd8b3ddf3e521396d70e2d50b79db53598e2c4
User & Date: ajv-899-334-8894@vsta.org 2017-05-11 17:07:32
Context
2017-06-15
04:49
Initial commit check-in: e689ff47d9 user: ajv-900-660-8060@vsta.org tags: master, trunk
2017-05-12
16:19
Initial README for the project check-in: 8e43c0e365 user: ajv-899-334-8894@vsta.org tags: master, trunk
2017-05-11
17:07
Initial commit, DNS server framework check-in: e2a053c2e7 user: ajv-899-334-8894@vsta.org tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added server.py.





















































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#
# dyndns_server.py
#	Field UDP DNS updates, serve a dynamic DNS sub-domain
#
import sys, time, socket
from dnslib import \
    SOA, NS, A, DNSRecord, DNSHeader, QTYPE, RR, CNAME

# Dotted domain name helper
class DomainName(str):
    def __getattr__(self, item):
	return DomainName(item + '.' + self)

D = DomainName('dyn.vsta.org')
IP = '208.82.102.26'
TTL = 60 * 5
PORT = 5053

soa_record = SOA(
    mname=D.ns1,    # primary name server
    rname=D.vandys, # email of the domain administrator
    times=(
	2017051001,   # serial number
	60 * 60 * 1,  # refresh
	60 * 60 * 3,  # retry
	60 * 60 * 24, # expire
	60 * 60 * 1,  # minimum
    )
)

ns_records = [NS(D.ns1)]

records = {
    D: [A(IP), soa_record] + ns_records,

    # MX and NS records must never point to a CNAME alias
    D.ns1: [A(IP)],
    D.vandys: [CNAME(D)],
}

def dns_response(data):
    request = DNSRecord.parse(data)
    print request
    reply = DNSRecord(DNSHeader(id=request.header.id,
	qr=1, aa=1, ra=1), q=request.q)
    qname = request.q.qname
    qn = str(qname)
    qtype = request.q.qtype
    qt = QTYPE[qtype]
    if (qn == D) or qn.endswith('.' + D):
	for name,rrs in records.iteritems():
	    if name == qn:
		for rdata in rrs:
		    rqt = rdata.__class__.__name__
		    if qt in ['*', rqt]:
			reply.add_answer(RR(rname=qname,
			    rtype=QTYPE[rqt], rclass=1, ttl=TTL, rdata=rdata))
    for rdata in ns_records:
	reply.add_answer(RR(rname=D, rtype=QTYPE.NS, rclass=1, ttl=TTL, rdata=rdata))
    reply.add_answer(RR(rname=D,
	rtype=QTYPE.SOA, rclass=1, ttl=TTL, rdata=soa_record))
    print "---- Reply:\n", reply
    return reply.pack()

if __name__ == '__main__':
    print "Starting nameserver..."
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind(('', PORT))
    while True:
	buf,who = s.recvfrom(8192)
	sys.stderr.write("Request %s %s: %s\n" % (who, time.asctime(), buf))
	res = dns_response(buf)
	sys.stderr.write(" response %s\n" % (res,))
	s.sendto(res, who)