diff options
author | Camil Staps | 2016-05-02 16:12:13 +0200 |
---|---|---|
committer | Camil Staps | 2016-05-02 16:12:13 +0200 |
commit | ce38b1cc4cd55e3acbc40e664e4ee473afa35b18 (patch) | |
tree | e13341cfee008f1c465b52abe441b7ade31a4505 /project2/proj2_s4498062/dns/message.py | |
parent | dos2unix (diff) |
pylint project 2
Diffstat (limited to 'project2/proj2_s4498062/dns/message.py')
-rw-r--r-- | project2/proj2_s4498062/dns/message.py | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/project2/proj2_s4498062/dns/message.py b/project2/proj2_s4498062/dns/message.py index 3c86443..9f8ead4 100644 --- a/project2/proj2_s4498062/dns/message.py +++ b/project2/proj2_s4498062/dns/message.py @@ -1,26 +1,23 @@ -#!/usr/bin/env python2 - """ DNS messages This module contains classes for DNS messages, their header section and question fields. See section 4 of RFC 1035 for more info. """ -import socket import struct -from dns.classes import Class from dns.domainname import Parser, Composer from dns.resource import ResourceRecord -from dns.types import Type class Message(object): """ DNS message """ - def __init__(self, header, questions=[], answers=[], authorities=[], additionals=[]): + def __init__( + self, header, questions=[], answers=[], authorities=[], + additionals=[]): """ Create a new DNS message - + Args: header (Header): the header section questions ([Question]): the question section @@ -82,40 +79,40 @@ class Message(object): # Parse questions questions = [] - for i in range(header.qd_count): + for _ in range(header.qd_count): question, offset = Question.from_bytes(packet, offset, parser) questions.append(question) # Parse answers answers = [] - for i in range(header.an_count): + for _ in range(header.an_count): answer, offset = ResourceRecord.from_bytes(packet, offset, parser) answers.append(answer) # Parse authorities authorities = [] - for i in range(header.ns_count): - authority, offset = ResourceRecord.from_bytes(packet, offset, parser) - authorities.append(authority) + for _ in range(header.ns_count): + auth, offset = ResourceRecord.from_bytes(packet, offset, parser) + authorities.append(auth) # Parse additionals additionals = [] - for i in range(header.ar_count): - additional, offset = ResourceRecord.from_bytes(packet, offset, parser) - additionals.append(additional) + for _ in range(header.ar_count): + add, offset = ResourceRecord.from_bytes(packet, offset, parser) + additionals.append(add) return cls(header, questions, answers, authorities, additionals) class Header(object): """ The header section of a DNS message - + Contains a number of properties which are accessible as normal member variables. See section 4.1.1 of RFC 1035 for their meaning. """ - + def __init__(self, ident, flags, qd_count, an_count, ns_count, ar_count): """ Create a new Header object @@ -135,13 +132,14 @@ class Header(object): def to_bytes(self): """ Convert header to bytes """ - return struct.pack("!6H", - self.ident, - self._flags, - self.qd_count, - self.an_count, - self.ns_count, - self.ar_count) + return struct.pack( + "!6H", + self.ident, + self._flags, + self.qd_count, + self.an_count, + self.ns_count, + self.ar_count) @classmethod def from_bytes(cls, packet): @@ -149,21 +147,27 @@ class Header(object): if len(packet) < 12: raise ShortHeader return cls(*struct.unpack_from("!6H", packet)) - + @property def flags(self): + """The flags of the header""" return self._flags + @flags.setter def flags(self, value): + """Set the flags of the header""" if value >= (1 << 16): raise ValueError("value too big for flags") self._flags = value @property def qr(self): + """The QR flag""" return self._flags & (1 << 15) + @qr.setter def qr(self, value): + """Set the QR flag""" if value: self._flags |= (1 << 15) else: @@ -171,9 +175,12 @@ class Header(object): @property def opcode(self): + """The opcode of the header""" return (self._flags & (((1 << 4) - 1) << 11)) >> 11 + @opcode.setter def opcode(self, value): + """Set the opcode""" if value > 0b1111: raise ValueError("invalid opcode") self._flags &= ~(((1 << 4) - 1) << 11) @@ -181,9 +188,12 @@ class Header(object): @property def aa(self): + """The AA flag""" return self._flags & (1 << 10) + @aa.setter def aa(self, value): + """Set the AA flag""" if value: self._flags |= (1 << 10) else: @@ -191,9 +201,12 @@ class Header(object): @property def tc(self): + """The TC flag""" return self._flags & (1 << 9) + @tc.setter def tc(self, value): + """Set the TC flag""" if value: self._flags |= (1 << 9) else: @@ -201,9 +214,12 @@ class Header(object): @property def rd(self): + """The RD flag""" return self._flags & (1 << 8) + @rd.setter def rd(self, value): + """Set the RD flag""" if value: self._flags |= (1 << 8) else: @@ -211,9 +227,12 @@ class Header(object): @property def ra(self): + """The RA flag""" return self._flags & (1 << 7) + @ra.setter def ra(self, value): + """Set the RA flag""" if value: self._flags |= (1 << 7) else: @@ -221,17 +240,23 @@ class Header(object): @property def z(self): - return (self._flags & (((1 << 3) - 1) << 4) >> 4) + """The Z flag""" + return self._flags & (((1 << 3) - 1) << 4) >> 4 + @z.setter def z(self, value): + """Set the Z flag""" if value: raise ValueError("non-zero zero flag") @property def rcode(self): + """The return code""" return self._flags & ((1 << 4) - 1) + @rcode.setter def rcode(self, value): + """Set the return code""" if value > 0b1111: raise ValueError("invalid return code") self._flags &= ~((1 << 4) - 1) @@ -245,8 +270,8 @@ class Question(object): """ def __init__(self, qname, qtype, qclass): - """ Create a new entry in the question section - + """ Create a new entry in the question section + Args: qname (str): QNAME qtype (Type): QTYPE |