Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions marine/encap_consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""
Encapsulation consts
For more values look at wireshark's wiretap/wtap.h
"""

ENCAP_ETHERNET = 1

"""Your friendly neighbourhood wifi"""
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

ENCAP_IEEE_802_11_RADIOTAP = 23
15 changes: 11 additions & 4 deletions marine/marine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from io import StringIO
from typing import Optional, List, Dict

from . import encap_consts


class MarineResult(Structure):
_fields_ = [("output", c_char_p), ("result", c_int)]
Expand Down Expand Up @@ -47,6 +49,7 @@ def filter_and_parse(
bpf: Optional[str] = None,
display_filter: Optional[str] = None,
fields: Optional[list] = None,
encapsulation_type: int = encap_consts.ENCAP_ETHERNET,
) -> (bool, Dict[str, str]):
if bpf is None and display_filter is None and fields is None:
raise ValueError(
Expand All @@ -69,12 +72,13 @@ def filter_and_parse(
bpf,
display_filter,
tuple(encoded_fields) if fields is not None else None,
encapsulation_type,
)
if filter_key in self._filters_cache:
filter_id = self._filters_cache[filter_key]
else:
filter_id, err = self._add_or_get_filter(
bpf, display_filter, encoded_fields
bpf, display_filter, encoded_fields, encapsulation_type
)
if filter_id < 0:
raise ValueError(
Expand All @@ -97,9 +101,11 @@ def filter_and_parse(
self._marine.marine_free(marine_result)
return success, result

def validate_bpf(self, bpf: str) -> bool:
def validate_bpf(
self, bpf: str, encapsulation_type: int = encap_consts.ENCAP_ETHERNET
) -> bool:
bpf = bpf.encode("utf-8")
return bool(self._marine.validate_bpf(bpf))
return bool(self._marine.validate_bpf(bpf, encapsulation_type))

def validate_display_filter(self, display_filter: str) -> bool:
display_filter = display_filter.encode("utf-8")
Expand Down Expand Up @@ -127,6 +133,7 @@ def _add_or_get_filter(
bpf: Optional[bytes] = None,
display_filter: Optional[bytes] = None,
fields: Optional[List[bytes]] = None,
encapsulation_type: int = encap_consts.ENCAP_ETHERNET,
) -> (int, bytes):
if fields is not None:
fields_len = len(fields)
Expand All @@ -136,7 +143,7 @@ def _add_or_get_filter(
fields_c_arr = None
err_msg = pointer(POINTER(c_char)())
filter_id = self._marine.marine_add_filter(
bpf, display_filter, fields_c_arr, fields_len, err_msg
bpf, display_filter, fields_c_arr, fields_len, encapsulation_type, err_msg
)
if err_msg.contents:
err_msg_value = string_at(err_msg.contents)
Expand Down
13 changes: 11 additions & 2 deletions marine/marine_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import List, Dict, Optional, Tuple, ClassVar

from marine import Marine
from . import encap_consts


class MarinePool:
Expand Down Expand Up @@ -33,14 +34,21 @@ def filter_and_parse(
bpf: Optional[str] = None,
display_filter: Optional[str] = None,
fields: Optional[List[str]] = None,
encapsulation_type: int = encap_consts.ENCAP_ETHERNET,
) -> List[Tuple[bool, Dict[str, str]]]:
if len(packets) == 0:
return []

chunk_size = int(math.ceil(len(packets) / float(self._process_count)))
return self.pool.starmap(
self._filter_and_parse,
zip(packets, repeat(bpf), repeat(display_filter), repeat(fields)),
zip(
packets,
repeat(bpf),
repeat(display_filter),
repeat(fields),
repeat(encapsulation_type),
),
chunksize=chunk_size,
)

Expand All @@ -55,9 +63,10 @@ def _filter_and_parse(
bpf: Optional[str] = None,
display_filter: Optional[str] = None,
fields: Optional[list] = None,
encapsulation_type: int = encap_consts.ENCAP_ETHERNET,
) -> (bool, Dict[str, str]):
return cls._marine_instance.filter_and_parse(
packet, bpf, display_filter, fields
packet, bpf, display_filter, fields, encapsulation_type
)

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down
Loading