diff options
author | Camil Staps | 2016-05-25 16:11:27 +0200 |
---|---|---|
committer | Camil Staps | 2016-05-26 21:37:12 +0200 |
commit | b0f35bd7c31c0ed1188cbf325de1aead0e8f8ee9 (patch) | |
tree | 9c1ca61f3aac214488ce8ea2e69bf4189fc2c5a5 | |
parent | Caching (mostly from da97de6) (diff) |
Start zone reading
-rw-r--r-- | project2/proj2_s4498062/dns/regexes.py | 46 | ||||
-rw-r--r-- | project2/proj2_s4498062/dns/server.py | 21 | ||||
-rwxr-xr-x | project2/proj2_s4498062/dns_server.py | 11 | ||||
-rw-r--r-- | project2/proj2_s4498062/named.root | 90 |
4 files changed, 161 insertions, 7 deletions
diff --git a/project2/proj2_s4498062/dns/regexes.py b/project2/proj2_s4498062/dns/regexes.py new file mode 100644 index 0000000..ffa1770 --- /dev/null +++ b/project2/proj2_s4498062/dns/regexes.py @@ -0,0 +1,46 @@ +"""Regexes used in the DNS protocol""" + + +def grpm(regex): + """Make a matching group""" + return grp(regex, matching=True) + + +def grp(regex, matching=False): + """Make a group""" + return r'(' + (r'' if matching else r'?:') + regex + r')' + + +def opt(regex): + """Make an optional group""" + return grp(grp(regex) + r'?') + + +def regex_opt_r(*regexes): + """Make a group that matches one of the given regexes""" + return grp(r'|'.join(regexes)) + + +DIGIT = r'\d' +LETTER = r'[a-zA-Z]' +LETDIG = grp(regex_opt_r(DIGIT, LETTER)) +LETDIGHYP = grp(regex_opt_r(LETDIG, r'-')) +LDHSTR = grp(LETDIGHYP + r'+') +LABEL = grp(LETTER + opt(opt(LDHSTR) + LETDIG)) +SUBDOMAIN = grp(grpm(grp(LABEL + r'\.') + r'*') + grpm(LABEL)) +DOMAIN = regex_opt_r(SUBDOMAIN, r' ') + +# Fast, non-matching domain +_DOMAIN = r'(?:(?:[a-zA-Z](?:[a-zA-Z\d\-]*[a-zA-Z\d])?\.)*)?' + +IP = r'(?:(?:\d{1,3}\.){3}\d{1,3})' + +CLASS = regex_opt_r(r'IN', r'CH') +TYPE = regex_opt_r(r'A', r'CNAME', r'HINFO', r'MX', r'NS', r'PTR', r'SOA') +TTL = r'\d+' +RDATA = r'.*?' +RR = regex_opt_r( + grp(grpm(TTL) + r'\s+' + opt(grpm(CLASS))), + grp(grpm(CLASS) + r'\s+' + opt(grpm(TTL))) + ) + r'\s+' + grpm(TYPE) + r'\s+' + grpm(RDATA) + r'\s*(?:(?<!\\);)?\s*$' +ZONE_LINE_DOMAIN = r'^' + grpm(_DOMAIN) + r'\s+' + RR diff --git a/project2/proj2_s4498062/dns/server.py b/project2/proj2_s4498062/dns/server.py index d4e3109..f01043d 100644 --- a/project2/proj2_s4498062/dns/server.py +++ b/project2/proj2_s4498062/dns/server.py @@ -1,13 +1,16 @@ -#!/usr/bin/env python2 - """ A recursive DNS server This module provides a recursive DNS server. You will have to implement this server using the algorithm described in section 4.3.2 of RFC 1034. """ +import re from threading import Thread +import dns.regexes as rgx +from dns.classes import Class +from dns.types import Type + class RequestHandler(Thread): """ A handler for requests to the DNS server """ @@ -51,3 +54,17 @@ class Server(object): """ Shutdown the server """ self.done = True # TODO: shutdown socket + + def parse_zone_file(self, fname): + with open(fname) as zonef: + zone = zonef.read() + for match in re.finditer(rgx.ZONE_LINE_DOMAIN, zone, re.MULTILINE): + match = match.groups() + name = match[0] + ttl = int(match[1] or match[4] or ttl) + class_ = Class.from_string( + match[2] or match[3] or Class.to_string(class_)) + type_ = Type.from_string(match[5]) + data = match[6] + print match + print name, ttl, Class.to_string(class_), Type.to_string(type_), data diff --git a/project2/proj2_s4498062/dns_server.py b/project2/proj2_s4498062/dns_server.py index 4ac2ec4..6fe9a3a 100755 --- a/project2/proj2_s4498062/dns_server.py +++ b/project2/proj2_s4498062/dns_server.py @@ -26,11 +26,12 @@ def main(): # Start server server = dns.server.Server(args.port, args.caching, args.ttl) - try: - server.serve() - except KeyboardInterrupt: - server.shutdown() - print() + server.parse_zone_file('named.root') + #try: + # server.serve() + #except KeyboardInterrupt: + # server.shutdown() + # print() if __name__ == "__main__": main() diff --git a/project2/proj2_s4498062/named.root b/project2/proj2_s4498062/named.root new file mode 100644 index 0000000..3c82146 --- /dev/null +++ b/project2/proj2_s4498062/named.root @@ -0,0 +1,90 @@ +; This file holds the information on root name servers needed to +; initialize cache of Internet domain name servers +; (e.g. reference this file in the "cache . <file>" +; configuration file of BIND domain name servers). +; +; This file is made available by InterNIC +; under anonymous FTP as +; file /domain/named.cache +; on server FTP.INTERNIC.NET +; -OR- RS.INTERNIC.NET +; +; last update: March 23, 2016 +; related version of root zone: 2016032301 +; +; formerly NS.INTERNIC.NET +; +. 3600000 NS A.ROOT-SERVERS.NET. +A.ROOT-SERVERS.NET. 3600000 A 198.41.0.4 +A.ROOT-SERVERS.NET. 3600000 AAAA 2001:503:ba3e::2:30 +; +; FORMERLY NS1.ISI.EDU +; +. 3600000 NS B.ROOT-SERVERS.NET. +B.ROOT-SERVERS.NET. 3600000 A 192.228.79.201 +B.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:84::b +; +; FORMERLY C.PSI.NET +; +. 3600000 NS C.ROOT-SERVERS.NET. +C.ROOT-SERVERS.NET. 3600000 A 192.33.4.12 +C.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2::c +; +; FORMERLY TERP.UMD.EDU +; +. 3600000 NS D.ROOT-SERVERS.NET. +D.ROOT-SERVERS.NET. 3600000 A 199.7.91.13 +D.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2d::d +; +; FORMERLY NS.NASA.GOV +; +. 3600000 NS E.ROOT-SERVERS.NET. +E.ROOT-SERVERS.NET. 3600000 A 192.203.230.10 +; +; FORMERLY NS.ISC.ORG +; +. 3600000 NS F.ROOT-SERVERS.NET. +F.ROOT-SERVERS.NET. 3600000 A 192.5.5.241 +F.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:2f::f +; +; FORMERLY NS.NIC.DDN.MIL +; +. 3600000 NS G.ROOT-SERVERS.NET. +G.ROOT-SERVERS.NET. 3600000 A 192.112.36.4 +; +; FORMERLY AOS.ARL.ARMY.MIL +; +. 3600000 NS H.ROOT-SERVERS.NET. +H.ROOT-SERVERS.NET. 3600000 A 198.97.190.53 +H.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:1::53 +; +; FORMERLY NIC.NORDU.NET +; +. 3600000 NS I.ROOT-SERVERS.NET. +I.ROOT-SERVERS.NET. 3600000 A 192.36.148.17 +I.ROOT-SERVERS.NET. 3600000 AAAA 2001:7fe::53 +; +; OPERATED BY VERISIGN, INC. +; +. 3600000 NS J.ROOT-SERVERS.NET. +J.ROOT-SERVERS.NET. 3600000 A 192.58.128.30 +J.ROOT-SERVERS.NET. 3600000 AAAA 2001:503:c27::2:30 +; +; OPERATED BY RIPE NCC +; +. 3600000 NS K.ROOT-SERVERS.NET. +K.ROOT-SERVERS.NET. 3600000 A 193.0.14.129 +K.ROOT-SERVERS.NET. 3600000 AAAA 2001:7fd::1 +; +; OPERATED BY ICANN +; +. 3600000 NS L.ROOT-SERVERS.NET. +L.ROOT-SERVERS.NET. 3600000 A 199.7.83.42 +L.ROOT-SERVERS.NET. 3600000 AAAA 2001:500:9f::42 +; +; OPERATED BY WIDE +; +. 3600000 NS M.ROOT-SERVERS.NET. +M.ROOT-SERVERS.NET. 3600000 A 202.12.27.33 +M.ROOT-SERVERS.NET. 3600000 AAAA 2001:dc3::35 +; End of file |