Minor fixes

Add a field for the raw ipv4 address of an A record (encoded in an int)
Rename "remaining data" to "rdata" since that's what the dns spec calls it
Update Srv record rdata block to be handled the same as other records
Fix A record endOffset to have the correct value
This commit is contained in:
2025-01-02 15:20:02 -06:00
parent 6d8258313d
commit 74a91c2e13

37
dns.ts
View File

@ -45,7 +45,8 @@ export interface SrvRecord extends Record {
export interface ARecord extends Record {
interface: "a";
address: string;
address: number;
addressStr: string;
}
class dnsAnswerParseResult {
@ -114,7 +115,7 @@ export function GetDNSAnswer(
offset += 2; // don't care about "class" of answer
const ttlSeconds = msg.readUInt32BE(offset);
offset += 4;
const remainingDataLength = msg.readUInt16BE(offset);
const rDataLength = msg.readUInt16BE(offset);
offset += 2;
switch (type) {
@ -125,7 +126,7 @@ export function GetDNSAnswer(
type: type,
ttlSeconds: ttlSeconds,
name: parsedResult.name,
endOffset: offset + remainingDataLength,
endOffset: offset + rDataLength,
domain: domainResult.name,
};
return ret;
@ -138,7 +139,7 @@ export function GetDNSAnswer(
type: type,
ttlSeconds: ttlSeconds,
name: parsedResult.name,
endOffset: offset + remainingDataLength,
endOffset: offset + rDataLength,
text: textResult.name,
};
return ret;
@ -146,20 +147,16 @@ export function GetDNSAnswer(
case TypeSrv: {
const priority = msg.readUInt16BE(offset);
offset += 2;
const weight = msg.readUInt16BE(offset);
offset += 2;
const port = msg.readUInt16BE(offset);
offset += 2;
const targetResult = parseDnsName(msg, offset);
offset = targetResult.endOffset;
const weight = msg.readUInt16BE(offset + 2);
const port = msg.readUInt16BE(offset + 4);
const targetResult = parseDnsName(msg, offset + 6);
const ret: SrvRecord = {
interface: "srv",
type: type,
ttlSeconds: ttlSeconds,
name: parsedResult.name,
endOffset: offset,
endOffset: offset + rDataLength,
priority: priority,
weight: weight,
port: port,
@ -170,22 +167,20 @@ export function GetDNSAnswer(
case TypeA: {
const o1 = msg.readUInt8(offset);
offset++;
const o2 = msg.readUInt8(offset);
offset++;
const o3 = msg.readUInt8(offset);
offset++;
const o4 = msg.readUInt8(offset);
offset++;
const address = `${o1.toString()}.${o2.toString()}.${o3.toString()}.${o4.toString()}`;
const o2 = msg.readUInt8(offset + 1);
const o3 = msg.readUInt8(offset + 2);
const o4 = msg.readUInt8(offset + 3);
const address = (o1 << 24) | (o2 << 16) | (o3 << 8) | (o4 << 0);
const addressStr = `${o1.toString()}.${o2.toString()}.${o3.toString()}.${o4.toString()}`;
const ret: ARecord = {
interface: "a",
type: type,
ttlSeconds: ttlSeconds,
name: parsedResult.name,
endOffset: offset + remainingDataLength,
endOffset: offset + rDataLength,
address: address,
addressStr: addressStr,
};
return ret;
}