Skip to content

Commit

Permalink
Add support for non-seekable file handles in a bit of awkard way. It …
Browse files Browse the repository at this point in the history
…is only possible to determine seekability with the io.open API, however the Array API doesn't support it.
  • Loading branch information
flupzor committed Jun 21, 2015
1 parent a616825 commit f03bbcb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
14 changes: 9 additions & 5 deletions packetparser/pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ class PcapFile(PacketContainer):
time
"""

def __init__(self, data, file_handle):
def __init__(self, data, file_handle, seekable=True):
self.file_handle = file_handle
self.seekable = seekable

new = {}
new.update(PcapHeaderStructure.defaults())
Expand Down Expand Up @@ -289,8 +290,9 @@ def write_frame(self, frame):
pass

@classmethod
def parse_header(cls, file_handle):
file_handle.seek(0)
def parse_header(cls, file_handle, seekable=True):
if seekable:
file_handle.seek(0)

header_frame_array = array.array('B')
header_frame_array.fromfile(
Expand All @@ -301,11 +303,13 @@ def parse_header(cls, file_handle):
pcap_header_struct = PcapHeaderStructure.unpack(header_frame_array)
data = pcap_header_struct.data

return cls(data, file_handle)
return cls(data, file_handle, seekable=seekable)

def frames(self):
# Start parsing right after the PCAP header.
self.file_handle.seek(PcapHeaderStructure.struct.size)

if self.seekable:
self.file_handle.seek(PcapHeaderStructure.struct.size)

# TODO: For now we support 127, 80211 RadioTap only.
extra = {
Expand Down
10 changes: 8 additions & 2 deletions pcap_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,13 @@ def print_pcap_file_info(pcap_file):
sys.exit(1) # EXIT_FAILURE

for filename in sys.argv[1:]:
file_handle = open(filename, 'rb')
pcap_file = PcapFile.parse_header(file_handle)
if filename == '-':
file_handle = sys.stdin
seekable = False
else:
file_handle = open(filename, 'rb')
seekable = True

pcap_file = PcapFile.parse_header(file_handle, seekable=seekable)

print_pcap_file_info(pcap_file)

0 comments on commit f03bbcb

Please sign in to comment.