MCPcopy Index your code
hub / github.com/nodejs/node / readDomainFromPacket

Function readDomainFromPacket

test/common/dns.js:27–50  ·  view source on GitHub ↗
(buffer, offset)

Source from the content-addressed store, hash-verified

25// Naïve DNS parser/serializer.
26
27function readDomainFromPacket(buffer, offset) {
28 assert.ok(offset < buffer.length);
29 const length = buffer[offset];
30 if (length === 0) {
31 return { nread: 1, domain: '' };
32 } else if ((length & 0xC0) === 0) {
33 offset += 1;
34 const chunk = buffer.toString('ascii', offset, offset + length);
35 // Read the rest of the domain.
36 const { nread, domain } = readDomainFromPacket(buffer, offset + length);
37 return {
38 nread: 1 + length + nread,
39 domain: domain ? `${chunk}.${domain}` : chunk,
40 };
41 }
42 // Pointer to another part of the packet.
43 assert.strictEqual(length & 0xC0, 0xC0);
44 // eslint-disable-next-line @stylistic/js/space-infix-ops, @stylistic/js/space-unary-ops
45 const pointeeOffset = buffer.readUInt16BE(offset) &~ 0xC000;
46 return {
47 nread: 2,
48 domain: readDomainFromPacket(buffer, pointeeOffset),
49 };
50}
51
52function parseDNSPacket(buffer) {
53 assert.ok(buffer.length > 12);

Callers 1

parseDNSPacketFunction · 0.85

Calls 2

okMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…