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

Function writeDNSPacket

test/common/dns.js:194–307  ·  view source on GitHub ↗
(parsed)

Source from the content-addressed store, hash-verified

192}
193
194function writeDNSPacket(parsed) {
195 const buffers = [];
196 const kStandardResponseFlags = 0x8180;
197
198 buffers.push(new Uint16Array([
199 parsed.id,
200 parsed.flags ?? kStandardResponseFlags,
201 parsed.questions?.length,
202 parsed.answers?.length,
203 parsed.authorityAnswers?.length,
204 parsed.additionalRecords?.length,
205 ]));
206
207 for (const q of parsed.questions) {
208 assert(types[q.type]);
209 buffers.push(writeDomainName(q.domain));
210 buffers.push(new Uint16Array([
211 types[q.type],
212 q.cls === undefined ? classes.IN : q.cls,
213 ]));
214 }
215
216 for (const rr of [].concat(parsed.answers,
217 parsed.authorityAnswers,
218 parsed.additionalRecords)) {
219 if (!rr) continue;
220
221 assert(types[rr.type]);
222 buffers.push(writeDomainName(rr.domain));
223 buffers.push(new Uint16Array([
224 types[rr.type],
225 rr.cls === undefined ? classes.IN : rr.cls,
226 ]));
227 buffers.push(new Int32Array([rr.ttl]));
228
229 const rdLengthBuf = new Uint16Array(1);
230 buffers.push(rdLengthBuf);
231
232 switch (rr.type) {
233 case 'A':
234 rdLengthBuf[0] = 4;
235 buffers.push(new Uint8Array(rr.address.split('.')));
236 break;
237 case 'AAAA':
238 rdLengthBuf[0] = 16;
239 buffers.push(writeIPv6(rr.address));
240 break;
241 case 'TXT': {
242 const total = rr.entries.map((s) => s.length).reduce((a, b) => a + b);
243 // Total length of all strings + 1 byte each for their lengths.
244 rdLengthBuf[0] = rr.entries.length + total;
245 for (const txt of rr.entries) {
246 buffers.push(new Uint8Array([Buffer.byteLength(txt)]));
247 buffers.push(Buffer.from(txt));
248 }
249 break;
250 }
251 case 'MX':

Callers 2

createDnsServerFunction · 0.85
createDnsServerFunction · 0.85

Calls 11

writeDomainNameFunction · 0.85
writeIPv6Function · 0.85
concatMethod · 0.80
reduceMethod · 0.80
swap16Method · 0.80
swap32Method · 0.80
mapMethod · 0.65
assertFunction · 0.50
pushMethod · 0.45
splitMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…