summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062
diff options
context:
space:
mode:
Diffstat (limited to 'project2/proj2_s4498062')
-rw-r--r--project2/proj2_s4498062/dns/rcodes.py1
-rw-r--r--project2/proj2_s4498062/dns/regexes.py6
-rw-r--r--project2/proj2_s4498062/dns/resolver.py13
-rw-r--r--project2/proj2_s4498062/dns/resource.py13
4 files changed, 18 insertions, 15 deletions
diff --git a/project2/proj2_s4498062/dns/rcodes.py b/project2/proj2_s4498062/dns/rcodes.py
index cff7a8f..8f63b90 100644
--- a/project2/proj2_s4498062/dns/rcodes.py
+++ b/project2/proj2_s4498062/dns/rcodes.py
@@ -4,6 +4,7 @@ This module contains an Enum of RCODE values. See section 4.1.4 of RFC 1035 for
more info.
"""
+
class RCode(object):
""" Enum of RCODE values
diff --git a/project2/proj2_s4498062/dns/regexes.py b/project2/proj2_s4498062/dns/regexes.py
index 48c28c9..07c1eed 100644
--- a/project2/proj2_s4498062/dns/regexes.py
+++ b/project2/proj2_s4498062/dns/regexes.py
@@ -1,21 +1,26 @@
"""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))
@@ -26,4 +31,3 @@ SUBDOMAIN = grp(grpm(grp(LABEL + r'\.') + r'*') + grpm(LABEL))
DOMAIN = regex_opt_r(SUBDOMAIN, r' ')
IP = r'(?:(?:\d{1,3}\.){3}\d{1,3})'
-
diff --git a/project2/proj2_s4498062/dns/resolver.py b/project2/proj2_s4498062/dns/resolver.py
index a046d5d..b57e044 100644
--- a/project2/proj2_s4498062/dns/resolver.py
+++ b/project2/proj2_s4498062/dns/resolver.py
@@ -1,8 +1,8 @@
""" DNS Resolver
-This module contains a class for resolving hostnames. You will have to implement
-things in this module. This resolver will be both used by the DNS client and the
-DNS server, but with a different list of servers.
+This module contains a class for resolving hostnames. You will have to
+implement things in this module. This resolver will be both used by the DNS
+client and the DNS server, but with a different list of servers.
"""
import re
@@ -13,6 +13,7 @@ from dns.message import Message, Header, Question
from dns.types import Type
import dns.regexes as rgx
+
class Resolver(object):
""" DNS resolver """
@@ -47,7 +48,7 @@ class Resolver(object):
def do_query(self, query, using):
"""Send a query to a list of name servers"""
for hint in using:
- if re.match(rgx.IP, hint) == None:
+ if re.match(rgx.IP, hint) is None:
continue
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(self.timeout)
@@ -68,7 +69,7 @@ class Resolver(object):
using += self.nameservers
domains = re.match(rgx.DOMAIN, domain)
- if domains == None:
+ if domains is None:
return None
sub, dom = domains.groups()
if parent != '':
@@ -89,7 +90,7 @@ class Resolver(object):
result = self.get_hints(
sub, dom, new_hints + using, in_recursion=True)
- if result != None:
+ if result is not None:
return result
return []
diff --git a/project2/proj2_s4498062/dns/resource.py b/project2/proj2_s4498062/dns/resource.py
index 71f09be..adce3e7 100644
--- a/project2/proj2_s4498062/dns/resource.py
+++ b/project2/proj2_s4498062/dns/resource.py
@@ -30,15 +30,12 @@ class ResourceRecord(object):
def to_bytes(self, offset, composer):
""" Convert ResourceRecord to bytes """
- name = composer.to_bytes(offset, [self.name])
- offset += len(name)
+ record = composer.to_bytes(offset, [self.name])
+ record += struct.pack("!HHI", self.type_, self.class_, self.ttl)
+ offset += len(record) + 2
rdata = self.rdata.to_bytes(offset, composer)
- return (name + struct.pack(
- "!HHIH",
- self.type_,
- self.class_,
- self.ttl,
- len(rdata)) + rdata)
+ record += struct.pack("!H", len(rdata)) + rdata
+ return record
@classmethod
def from_bytes(cls, packet, offset, parser):