Skip to content

Commit

Permalink
backends: impleent simple USBMon backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ktemkin committed Jun 20, 2019
1 parent 10c406a commit 1c5c126
Show file tree
Hide file tree
Showing 5 changed files with 561 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,5 +1,5 @@
# Byte-compiled / optimized / DLL files
__pycache__/
__pycache__/G
*.py[cod]
*$py.class

Expand Down Expand Up @@ -125,3 +125,5 @@ dmypy.json

__pycache__
.vscode

.ccls-cache
52 changes: 49 additions & 3 deletions viewsb/backend.py
Expand Up @@ -2,12 +2,10 @@
ViewSB backend class defintions -- defines the abstract base for things that capture USB data.
"""

import io
import multiprocessing





class ViewSBBackendProcess:
""" Class that controls and communicates with a VSB backend running in another process. """

Expand Down Expand Up @@ -124,6 +122,54 @@ def handle_termination(self):



class FileBackend(ViewSBBackend):
""" Generic class for mass parsing packets from files. """

# Provided for the default implementation of run_capture.
# Specifies the amount of data that should be read from the file at once.
# If none, we'll try to read all of the data available at once. :)
# Defaults to a sane value for reading regular files, per python.
# Not (directly) used if `next_read_size()` is overridden.
READ_CHUNK_SIZE = io.DEFAULT_BUFFER_SIZE

def __init__(self, target_filename):

# Open the relevant file for reading.
self.target_file = open(target_filename, 'rb')


def next_read_size(self):
""" Returns the amount of data that should be read in the next read. """
return self.READ_CHUNK_SIZE


def read(self, length):
"""
Read handler that the subclass can call to perform a manual read.
Useful for grabbing data payloads following a header captured by `capture_data`.
"""
return self.target_file.read(length)


def run_capture(self):
"""
Primary capture function: reads a single chunk from the file, and passes
it to `handle_data` for conversion into ViewSB packets.
"""

# Attempt to read a chunk from the given file.
data = self.target_file.read(self.READ_CHUNK_SIZE)

# If we have data, handle it.
if data:
self.handle_data(data)

#TODO: handle EOF


def handle_data(self, data):
""" Handle chunks of data read from the relevant file. """
raise NotImplementedError("subclass must implement handle_data()")



Expand Down

0 comments on commit 1c5c126

Please sign in to comment.