Skip to content

Commit

Permalink
Add flag to ignore trailing junk in message.
Browse files Browse the repository at this point in the history
  • Loading branch information
shanehuntley committed Jan 30, 2012
1 parent d5dc023 commit 419e0a4
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions dns/message.py
Expand Up @@ -571,20 +571,23 @@ class _WireReader(object):
@type updating: bool
@ivar one_rr_per_rrset: Put each RR into its own RRset?
@type one_rr_per_rrset: bool
@ivar ignore_trailing: Ignore trailing junk at end of request?
@type ignore_trailing: bool
@ivar zone_rdclass: The class of the zone in messages which are
DNS dynamic updates.
@type zone_rdclass: int
"""

def __init__(self, wire, message, question_only=False,
one_rr_per_rrset=False):
one_rr_per_rrset=False, ignore_trailing=False):
self.wire = dns.wiredata.maybe_wrap(wire)
self.message = message
self.current = 0
self.updating = False
self.zone_rdclass = dns.rdataclass.IN
self.question_only = question_only
self.one_rr_per_rrset = one_rr_per_rrset
self.ignore_trailing = ignore_trailing

def _get_question(self, qcount):
"""Read the next I{qcount} records from the wire data and add them to
Expand Down Expand Up @@ -722,7 +725,7 @@ def read(self):
self._get_section(self.message.answer, ancount)
self._get_section(self.message.authority, aucount)
self._get_section(self.message.additional, adcount)
if self.current != l:
if not self.ignore_trailing and self.current != l:
raise TrailingJunk
if self.message.multi and self.message.tsig_ctx and \
not self.message.had_tsig:
Expand All @@ -731,7 +734,8 @@ def read(self):

def from_wire(wire, keyring=None, request_mac='', xfr=False, origin=None,
tsig_ctx = None, multi = False, first = True,
question_only = False, one_rr_per_rrset = False):
question_only = False, one_rr_per_rrset = False,
ignore_trailing = False):
"""Convert a DNS wire format message into a message
object.
Expand All @@ -757,6 +761,8 @@ def from_wire(wire, keyring=None, request_mac='', xfr=False, origin=None,
@type question_only: bool
@param one_rr_per_rrset: Put each RR into its own RRset
@type one_rr_per_rrset: bool
@param ignore_trailing: Ignore trailing junk at end of request?
@type ignore_trailing: bool
@raises ShortHeader: The message is less than 12 octets long.
@raises TrailingJunk: There were octets in the message past the end
of the proper DNS message.
Expand All @@ -775,7 +781,8 @@ def from_wire(wire, keyring=None, request_mac='', xfr=False, origin=None,
m.multi = multi
m.first = first

reader = _WireReader(wire, m, question_only, one_rr_per_rrset)
reader = _WireReader(wire, m, question_only, one_rr_per_rrset,
ignore_trailing)
reader.read()

return m
Expand Down

0 comments on commit 419e0a4

Please sign in to comment.