Skip to content

Commit

Permalink
Allow PcapNG reader to have multiple linktypes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Martin committed Sep 1, 2017
1 parent 1544658 commit 2a5b59e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions scapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ def __init__(self, filep):
self.filep.seek(0, 0)
self.endian = '<'
self.tsresol = 6
self.LLcls = []
self.linktype = None

def __iter__(self):
Expand Down Expand Up @@ -704,10 +705,10 @@ def read_interface_description(self):
elif not i & (0b1 << 15):
warning("PcapNGReader: Unparsed option %d/#%x in enhanced packet block" % (i, i))
try:
self.LLcls = conf.l2types[self.linktype]
self.LLcls.append(conf.l2types[self.linktype])
except KeyError:
warning("RawPcapReader: unknown LL type [%i]/[%#x]. Using Raw packets" % (self.linktype,self.linktype))
self.LLcls = conf.raw_layer
self.LLcls.append(conf.raw_layer)

self._check_length(block_length)

Expand All @@ -724,7 +725,7 @@ def read_enhanced_packet(self, size = MTU):
if not i & (0b1 << 15):
warning("PcapNGReader: Unparsed option %d/#%x in enhanced packet block" % (i, i))
self._check_length(block_length)
return pkt[:MTU], (self.parse_sec(timestamp), self.parse_usec(timestamp), wirelen)
return pkt[:MTU], interface, (self.parse_sec(timestamp), self.parse_usec(timestamp), wirelen)

def parse_sec(self, t):
if self.tsresol & 0b10000000:
Expand Down Expand Up @@ -771,10 +772,10 @@ def __init__(self, filep, endianness):

self.linktype = linktype
try:
self.LLcls = conf.l2types[self.linktype]
self.LLcls = [conf.l2types[self.linktype]]
except KeyError:
warning("RawPcapReader: unknown LL type [%i]/[%#x]. Using Raw packets" % (self.linktype,self.linktype))
self.LLcls = conf.raw_layer
self.LLcls = [conf.raw_layer]

def __iter__(self):
return self
Expand All @@ -798,7 +799,7 @@ def read_packet(self, size=MTU):
return None
sec,usec,caplen,wirelen = struct.unpack(self.endian+"IIII", hdr)
s = self.f.read(caplen)[:MTU]
return s,(sec,usec,wirelen) # caplen = len(s)
return s, 0, (sec,usec,wirelen) # caplen = len(s)


class PcapReader(RawPcapReader):
Expand All @@ -818,11 +819,11 @@ def read_packet(self, size=MTU):
rp = RawPcapReader.read_packet(self,size)
if rp is None:
return None
s,(sec,usec,wirelen) = rp
s, i, (sec,usec,wirelen) = rp


try:
p = self.reader.LLcls(s)
p = self.reader.LLcls[i](s)
except KeyboardInterrupt:
raise
except:
Expand Down

0 comments on commit 2a5b59e

Please sign in to comment.