Skip to content

Instantly share code, notes, and snippets.

@emanuele-f
Last active November 27, 2023 21:35
Show Gist options
  • Save emanuele-f/0042bd3667f6bba8c8ae843efa1a41e9 to your computer and use it in GitHub Desktop.
Save emanuele-f/0042bd3667f6bba8c8ae843efa1a41e9 to your computer and use it in GitHub Desktop.
import dgram from 'dgram';
const PCAP_HEADER_PREFIX = Buffer.from("d4c3b2a1020004000000000000000000", "hex");
let ret = { offset: 0 };
const socket = dgram.createSocket({ type: 'udp4', reuseAddr: true });
socket.bind(1234);
socket.on('message', (msg, rinfo) => {
if ((msg.length === 24) &&
(msg.slice(0, PCAP_HEADER_PREFIX.length).equals(PCAP_HEADER_PREFIX))
) {
console.log("Ignoring PCAP header (start of PCAP)");
return;
}
if (msg.length < 16) {
console.log("Ignoring bad PCAP record");
return;
}
// skip the PCAP record, go to the IP header
const data = msg.slice(16);
ret = decoders.IPV4(data, ret.offset);
console.log('ip', ret)
ret = decoders.UDP(data, ret.offset);
console.log('udp', ret)
if (ret.info.protocol === PROTOCOL.UDP) {
console.log("TODO handle UDP");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment