Fix pcapng timestamp resolution + imported-session duration

- pcap.py: _parse_tsresol now pads options to a 32-bit boundary
  ((-length) % 4) and uses 2** for the MSB-set (power-of-2) case, so
  nanosecond if_tsresol is read correctly (was defaulting to µs and
  scaling timestamps 1000x)
- database/server: set imported session started/ended from the first/last
  packet timestamps (falling back to wall-clock for synthetic SPB)
- frontend: formatDuration renders hours
- tests: tsresol (ns after padded option, default µs, ns round-trip) and
  session-window/fallback (119 total)
This commit is contained in:
catarrh committed 2026-08-15 02:02:44 +03:00
1 parent 5ba55ac3a9
commit c21b655fe1
5 files changed
+81 -3

No files matched your search

+3 -1
View File
@@ -1031,7 +1031,9 @@ function formatDuration(start, end) {
const s = Math.floor(ms / 1000); const s = Math.floor(ms / 1000);
if (s < 60) return s + 's'; if (s < 60) return s + 's';
const m = Math.floor(s / 60); const m = Math.floor(s / 60);
return m + 'm ' + (s % 60) + 's'; if (m < 60) return m + 'm ' + (s % 60) + 's';
const h = Math.floor(m / 60);
return h + 'h ' + (m % 60) + 'm';
} }
function escapeHtml(str) { function escapeHtml(str) {
+2 -2
View File
@@ -378,9 +378,9 @@ def _parse_tsresol(options, endian):
if code == 9 and length == 1: if code == 9 and length == 1:
b = options[i] b = options[i]
if b & 0x80: if b & 0x80:
return 10 ** (-(b & 0x7F)) return 2 ** (-(b & 0x7F))
return 10 ** (-b) return 10 ** (-b)
i += length + (length & 1) # options are padded to 32-bit i += length + ((-length) % 4) # options are padded to a 32-bit boundary
return 1e-6 # default: microseconds return 1e-6 # default: microseconds
+11
View File
@@ -37,6 +37,11 @@ def _iso_now():
f'{time.time() % 1 * 1000:03.0f}' f'{time.time() % 1 * 1000:03.0f}'
def _iso_from_ts(ts):
return time.strftime('%Y-%m-%dT%H:%M:%S.', time.localtime(ts)) + \
f'{ts % 1 * 1000:03.0f}'
class Database: class Database:
def __init__(self, db_path=None): def __init__(self, db_path=None):
if db_path is None: if db_path is None:
@@ -64,6 +69,12 @@ class Database:
(_iso_now(), session_id)) (_iso_now(), session_id))
self._conn.commit() self._conn.commit()
def set_session_times_from_ts(self, session_id, first_ts, last_ts):
self._conn.execute(
'UPDATE sessions SET started=?, ended=? WHERE id=?',
(_iso_from_ts(first_ts), _iso_from_ts(last_ts), session_id))
self._conn.commit()
def rename_session(self, session_id, name): def rename_session(self, session_id, name):
self._conn.execute( self._conn.execute(
'UPDATE sessions SET name=? WHERE id=?', 'UPDATE sessions SET name=? WHERE id=?',
+8
View File
@@ -322,11 +322,19 @@ class RequestHandler(BaseHTTPRequestHandler):
self.db.rename_session(session_id, name) self.db.rename_session(session_id, name)
first_ts = packets[0][0] first_ts = packets[0][0]
last_ts = packets[-1][0]
rows = [] rows = []
for ts, msg_type, payload in packets: for ts, msg_type, payload in packets:
elapsed = round(ts - first_ts, 6) if ts is not None else 0.0 elapsed = round(ts - first_ts, 6) if ts is not None else 0.0
rows.append((max(0.0, elapsed), msg_type, payload, 0)) rows.append((max(0.0, elapsed), msg_type, payload, 0))
self.db.insert_messages(session_id, rows) self.db.insert_messages(session_id, rows)
# Use the packet timestamps as the session window when they are real
# (classic pcap / pcapng EPB). pcapng SPB-only files have synthetic
# 0,1,2,… timestamps, so fall back to the import wall-clock time.
_REAL_TS_MIN = 946684800 # 2000-01-01
if first_ts is not None and first_ts > _REAL_TS_MIN:
self.db.set_session_times_from_ts(session_id, first_ts, last_ts)
else:
self.db.close_session(session_id) self.db.close_session(session_id)
self._send_json({ self._send_json({
+57
View File
@@ -87,6 +87,37 @@ class TestParsePcapng(unittest.TestCase):
self.assertEqual(list(parse_pcapng(shb + idb + epb)), []) self.assertEqual(list(parse_pcapng(shb + idb + epb)), [])
class TestTsresol(unittest.TestCase):
def test_nanoseconds_after_padded_option(self):
from simtrace2_pysniff.pcap import _parse_tsresol
# if_name (code 2, len 2, "lo") then if_tsresol (code 9, len 1, 9), LE.
opts = (struct.pack('<HH', 2, 2) + b'lo' + b'\x00\x00' +
struct.pack('<HH', 9, 1) + b'\x09' + b'\x00\x00\x00' +
struct.pack('<HH', 0, 0))
self.assertEqual(_parse_tsresol(opts, '<'), 1e-9)
def test_default_microseconds(self):
from simtrace2_pysniff.pcap import _parse_tsresol
self.assertEqual(_parse_tsresol(struct.pack('<HH', 0, 0), '<'), 1e-6)
def test_parse_pcapng_nanoseconds(self):
# IDB with if_tsresol=9 → EPB timestamps in nanoseconds → seconds.
shb = _pcapng_block(0x0A0D0D0A, struct.pack('>IHHq', 0x1A2B3C4D, 1, 0, -1))
idb = _pcapng_block(0x00000001,
struct.pack('>HHI', LINKTYPE_ETHERNET, 0, 65535) +
struct.pack('>HH', 9, 1) + b'\x09' + b'\x00\x00\x00' +
struct.pack('>HH', 0, 0))
# epoch 1600000000.5 s → 1600000000500000000 ns
raw = 1600000000500000000
ts_hi, ts_lo = raw >> 32, raw & 0xFFFFFFFF
frame = wrap_gsmtap(_gsmtap_hdr(GSMTAP_SIM_ATR) + b'\x3b\x04')
epb = _pcapng_block(0x00000006,
struct.pack('>IIIII', 0, ts_hi, ts_lo, len(frame), len(frame)) + frame)
parsed = list(parse_pcapng(shb + idb + epb))
self.assertEqual(len(parsed), 1)
self.assertAlmostEqual(parsed[0][0], 1600000000.5, places=6)
class TestImportEndpoint(unittest.TestCase): class TestImportEndpoint(unittest.TestCase):
def _handler(self, body): def _handler(self, body):
from simtrace2_pysniff.server.server import RequestHandler from simtrace2_pysniff.server.server import RequestHandler
@@ -136,6 +167,32 @@ class TestImportEndpoint(unittest.TestCase):
self.assertIn('No GSMTAP data', h._json['error']) self.assertIn('No GSMTAP data', h._json['error'])
self.assertEqual(h.db.count_sessions(), 0) self.assertEqual(h.db.count_sessions(), 0)
def test_import_session_window_from_timestamps(self):
from datetime import datetime
# Two packets one hour apart in epoch time.
t0 = 1600000000.0
packets = [
(_gsmtap_hdr(GSMTAP_SIM_ATR), b'\x3b\x00', t0),
(_gsmtap_hdr(GSMTAP_SIM_APDU), b'\x80\xf2\x00\x00\x00', t0 + 3600.0),
]
h, _ = self._handler(build_pcap(packets))
h._handle_import_pcap({'name': ['window']})
session = h.db.get_session(h._json['session_id'])
started = datetime.fromisoformat(session['started'])
ended = datetime.fromisoformat(session['ended'])
self.assertEqual((ended - started).total_seconds(), 3600.0)
def test_import_synthetic_ts_falls_back(self):
# SPB-style synthetic timestamps (0, 1, …) → keep wall-clock fallback.
packets = [
(_gsmtap_hdr(GSMTAP_SIM_ATR), b'\x3b\x00', 0.0),
(_gsmtap_hdr(GSMTAP_SIM_APDU), b'\x80\xf2\x00\x00\x00', 1.0),
]
h, _ = self._handler(build_pcap(packets))
h._handle_import_pcap({'name': ['synth']})
session = h.db.get_session(h._json['session_id'])
self.assertTrue(session['ended'] is not None)
class TestVersion(unittest.TestCase): class TestVersion(unittest.TestCase):
def test_package_version(self): def test_package_version(self):