summaryrefslogtreecommitdiff
path: root/project2/proj2_s4498062/dns/domainname.py
diff options
context:
space:
mode:
Diffstat (limited to 'project2/proj2_s4498062/dns/domainname.py')
-rw-r--r--project2/proj2_s4498062/dns/domainname.py29
1 files changed, 14 insertions, 15 deletions
diff --git a/project2/proj2_s4498062/dns/domainname.py b/project2/proj2_s4498062/dns/domainname.py
index 85e14bf..6dbb039 100644
--- a/project2/proj2_s4498062/dns/domainname.py
+++ b/project2/proj2_s4498062/dns/domainname.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python2
-
""" Parsing and composing domain names
This module contains two classes for converting domain names to and from bytes.
@@ -14,20 +12,20 @@ import struct
class Composer(object):
def __init__(self):
self.offsets = dict()
-
+
def to_bytes(self, offset, dnames):
# Convert each domain name in to bytes
result = b""
- for i, dname in enumerate(dnames):
+ for _, dname in enumerate(dnames):
# Split domain name into labels
labels = dname.split(".")
-
+
# Determine keys of subdomains in offset dict
keys = []
for label in reversed(labels):
name = label
if keys:
- name += "." + keys[-1]
+ name += "." + keys[-1]
keys.append(name)
keys.reverse()
@@ -43,9 +41,10 @@ class Composer(object):
break
else:
self.offsets[keys[j]] = offset
- result += struct.pack("!B{}s".format(len(label)),
- len(label),
- label)
+ result += struct.pack(
+ "!B{}s".format(len(label)),
+ len(label),
+ label)
offset += 1 + len(label)
# Add null character at end
@@ -61,11 +60,10 @@ class Parser(object):
self.labels = dict()
def from_bytes(self, packet, offset, num):
- begin_offset = offset
dnames = []
# Read the domain names
- for i in range(num):
+ for _ in range(num):
# Read a new domain name
dname = ""
prev_offsets = []
@@ -78,7 +76,7 @@ class Parser(object):
if llength == 0:
offset += 1
break
-
+
# Compression label
elif (llength >> 6) == 3:
new_offset = offset + 2
@@ -90,8 +88,9 @@ class Parser(object):
# Normal label
else:
new_offset = offset + llength + 1
- label = struct.unpack_from("{}s".format(llength),
- packet, offset+1)[0]
+ label = struct.unpack_from(
+ "{}s".format(llength),
+ packet, offset+1)[0]
# Add label to dictionary
self.labels[offset] = label
@@ -109,5 +108,5 @@ class Parser(object):
# Append domain name to list
dnames.append(dname)
-
+
return dnames, offset