tc full packet test
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 10s
All checks were successful
Build and Deploy MITM Webserver / build (push) Successful in 10s
This commit is contained in:
46
backend/src/utilities/packet_mark.py
Normal file
46
backend/src/utilities/packet_mark.py
Normal file
@@ -0,0 +1,46 @@
|
||||
"""Helpers for the shared skb mark layout used for packet correlation and verdict hints."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
|
||||
PACKET_ID_MASK = 0x0FFFFFFF
|
||||
VERDICT_MASK = 0xF0000000
|
||||
VERDICT_FLAG_DROP = 0x10000000
|
||||
VERDICT_FLAG_REJECT = 0x20000000
|
||||
|
||||
|
||||
def normalize_skb_mark(value: object) -> Optional[int]:
|
||||
"""Return a positive integer skb mark or `None` when the value is empty."""
|
||||
if value in (None, "", 0, "0"):
|
||||
return None
|
||||
try:
|
||||
mark = int(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
if mark < 0:
|
||||
mark &= 0xFFFFFFFF
|
||||
return mark or None
|
||||
|
||||
|
||||
def packet_id_from_mark(value: object) -> Optional[str]:
|
||||
"""Extract the packet correlation identifier from the shared skb mark layout."""
|
||||
mark = normalize_skb_mark(value)
|
||||
if mark is None:
|
||||
return None
|
||||
packet_id = mark & PACKET_ID_MASK
|
||||
return str(packet_id) if packet_id else None
|
||||
|
||||
|
||||
def verdict_from_mark(value: object) -> Optional[str]:
|
||||
"""Return a verdict hint encoded into the upper mark bits, if any."""
|
||||
mark = normalize_skb_mark(value)
|
||||
if mark is None:
|
||||
return None
|
||||
verdict_bits = mark & VERDICT_MASK
|
||||
if verdict_bits & VERDICT_FLAG_REJECT:
|
||||
return "reject"
|
||||
if verdict_bits & VERDICT_FLAG_DROP:
|
||||
return "drop"
|
||||
return None
|
||||
Reference in New Issue
Block a user