mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 15:33:56 +08:00
refactor(pcap): share DNS wire diagnostic text (#17320)
This commit is contained in:
+14
-32
@@ -4,6 +4,11 @@ import { err, ok, type Result } from "../domain/result.js";
|
||||
import { byteToHex } from "../kernel/byteToHex.js";
|
||||
import { isRecord as object } from "../kernel/isRecord.js";
|
||||
|
||||
const INVALID_NAME_ENCODING =
|
||||
"DNS name encoding or compression pointer is invalid";
|
||||
const INVALID_MESSAGE_FRAMING =
|
||||
"DNS raw bytes, message framing or header coordinates are unavailable or invalid";
|
||||
|
||||
interface DnsWireFailure {
|
||||
readonly kind: "invalid_dns_wire";
|
||||
readonly reason: string;
|
||||
@@ -110,33 +115,20 @@ const decodeName = (
|
||||
const length = message[at]!;
|
||||
if ((length & 0xc0) === 0xc0) {
|
||||
if (at + 2 > (end === null ? boundary : message.length))
|
||||
return invalidWire(
|
||||
"DNS name encoding or compression pointer is invalid",
|
||||
);
|
||||
return invalidWire(INVALID_NAME_ENCODING);
|
||||
const pointer = ((length & 0x3f) << 8) | message[at + 1]!;
|
||||
if (pointer >= at)
|
||||
return invalidWire(
|
||||
"DNS name encoding or compression pointer is invalid",
|
||||
);
|
||||
if (pointer >= at) return invalidWire(INVALID_NAME_ENCODING);
|
||||
end ??= at + 2;
|
||||
at = pointer;
|
||||
} else {
|
||||
if ((length & 0xc0) !== 0)
|
||||
return invalidWire(
|
||||
"DNS name encoding or compression pointer is invalid",
|
||||
);
|
||||
if ((length & 0xc0) !== 0) return invalidWire(INVALID_NAME_ENCODING);
|
||||
if (at + 1 + length > (end === null ? boundary : message.length))
|
||||
return invalidWire(
|
||||
"DNS name encoding or compression pointer is invalid",
|
||||
);
|
||||
return invalidWire(INVALID_NAME_ENCODING);
|
||||
at += 1;
|
||||
if (length === 0)
|
||||
return ok({ labels, key: folded.join("."), end: end ?? at });
|
||||
expanded += length + 1;
|
||||
if (expanded > 255)
|
||||
return invalidWire(
|
||||
"DNS name encoding or compression pointer is invalid",
|
||||
);
|
||||
if (expanded > 255) return invalidWire(INVALID_NAME_ENCODING);
|
||||
const label = message.subarray(at, at + length);
|
||||
const hex = label.toString("hex");
|
||||
labels.push(hex);
|
||||
@@ -189,24 +181,14 @@ const decodeMessage = (
|
||||
!Array.isArray(ids[0]) ||
|
||||
!Number.isSafeInteger(ids[0][1])
|
||||
)
|
||||
return invalidWire(
|
||||
"DNS raw bytes, message framing or header coordinates are unavailable or invalid",
|
||||
);
|
||||
return invalidWire(INVALID_MESSAGE_FRAMING);
|
||||
const header = (ids[0][1] as number) - (raw[1] as number);
|
||||
if (header !== 0 && header !== 2)
|
||||
return invalidWire(
|
||||
"DNS raw bytes, message framing or header coordinates are unavailable or invalid",
|
||||
);
|
||||
if (header !== 0 && header !== 2) return invalidWire(INVALID_MESSAGE_FRAMING);
|
||||
const bytes = Buffer.from(raw[0], "hex");
|
||||
if (bytes.length < header + 12)
|
||||
return invalidWire(
|
||||
"DNS raw bytes, message framing or header coordinates are unavailable or invalid",
|
||||
);
|
||||
if (bytes.length < header + 12) return invalidWire(INVALID_MESSAGE_FRAMING);
|
||||
const wire = new BinaryReader(bytes, false, "dns");
|
||||
if (header === 2 && wire.u16(0) !== bytes.length - 2)
|
||||
return invalidWire(
|
||||
"DNS raw bytes, message framing or header coordinates are unavailable or invalid",
|
||||
);
|
||||
return invalidWire(INVALID_MESSAGE_FRAMING);
|
||||
const reader =
|
||||
header === 0
|
||||
? wire
|
||||
|
||||
Reference in New Issue
Block a user