This repository was archived by the owner on May 29, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpcap.ts
More file actions
59 lines (50 loc) · 1.75 KB
/
Copy pathpcap.ts
File metadata and controls
59 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { appendFileSync, writeFileSync } from "fs";
/**
* Simple PCAP writer for Meshtastic packets
* Uses LINKTYPE_USER0 (147) for custom protocol data
* Format: https://wiki.wireshark.org/Development/LibpcapFileFormat
*/
export class PcapWriter {
private path: string;
private initialized = false;
constructor(path: string) {
this.path = path;
}
/**
* Write pcap global header
* Only call once at the start
*/
private writeHeader() {
if (this.initialized) return;
const header = Buffer.alloc(24);
header.writeUInt32LE(0xa1b2c3d4, 0); // Magic number (little endian)
header.writeUInt16LE(2, 4); // Major version
header.writeUInt16LE(4, 6); // Minor version
header.writeInt32LE(0, 8); // Timezone offset (GMT)
header.writeUInt32LE(0, 12); // Timestamp accuracy
header.writeUInt32LE(65535, 16); // Max packet length
header.writeUInt32LE(147, 20); // Link type: USER0 (custom)
writeFileSync(this.path, header);
this.initialized = true;
}
/**
* Write a packet to the pcap file
* @param data Raw packet bytes
* @param timestamp Packet timestamp
*/
writePacket(data: Uint8Array, timestamp: Date = new Date()) {
if (!this.initialized) {
this.writeHeader();
}
const ts = timestamp.getTime();
const tsSec = Math.floor(ts / 1000);
const tsUsec = (ts % 1000) * 1000;
const packetHeader = Buffer.alloc(16);
packetHeader.writeUInt32LE(tsSec, 0); // Timestamp seconds
packetHeader.writeUInt32LE(tsUsec, 4); // Timestamp microseconds
packetHeader.writeUInt32LE(data.byteLength, 8); // Captured length
packetHeader.writeUInt32LE(data.byteLength, 12); // Original length
appendFileSync(this.path, packetHeader);
appendFileSync(this.path, data);
}
}