summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns
diff options
context:
space:
mode:
Diffstat (limited to 'project2/proj2_s4498062/dns')
-rw-r--r--project2/proj2_s4498062/dns/regexes.py46
-rw-r--r--project2/proj2_s4498062/dns/server.py21
2 files changed, 65 insertions, 2 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