(buffer)
| 50 | } |
| 51 | |
| 52 | function parseDNSPacket(buffer) { |
| 53 | assert.ok(buffer.length > 12); |
| 54 | |
| 55 | const parsed = { |
| 56 | id: buffer.readUInt16BE(0), |
| 57 | flags: buffer.readUInt16BE(2), |
| 58 | }; |
| 59 | |
| 60 | const counts = [ |
| 61 | ['questions', buffer.readUInt16BE(4)], |
| 62 | ['answers', buffer.readUInt16BE(6)], |
| 63 | ['authorityAnswers', buffer.readUInt16BE(8)], |
| 64 | ['additionalRecords', buffer.readUInt16BE(10)], |
| 65 | ]; |
| 66 | |
| 67 | let offset = 12; |
| 68 | for (const [ sectionName, count ] of counts) { |
| 69 | parsed[sectionName] = []; |
| 70 | for (let i = 0; i < count; ++i) { |
| 71 | const { nread, domain } = readDomainFromPacket(buffer, offset); |
| 72 | offset += nread; |
| 73 | |
| 74 | const type = buffer.readUInt16BE(offset); |
| 75 | |
| 76 | const rr = { |
| 77 | domain, |
| 78 | cls: buffer.readUInt16BE(offset + 2), |
| 79 | }; |
| 80 | offset += 4; |
| 81 | |
| 82 | for (const name in types) { |
| 83 | if (types[name] === type) |
| 84 | rr.type = name; |
| 85 | } |
| 86 | |
| 87 | if (sectionName !== 'questions') { |
| 88 | rr.ttl = buffer.readInt32BE(offset); |
| 89 | const dataLength = buffer.readUInt16BE(offset); |
| 90 | offset += 6; |
| 91 | |
| 92 | switch (type) { |
| 93 | case types.A: |
| 94 | assert.strictEqual(dataLength, 4); |
| 95 | rr.address = `${buffer[offset + 0]}.${buffer[offset + 1]}.` + |
| 96 | `${buffer[offset + 2]}.${buffer[offset + 3]}`; |
| 97 | break; |
| 98 | case types.AAAA: |
| 99 | assert.strictEqual(dataLength, 16); |
| 100 | rr.address = buffer.toString('hex', offset, offset + 16) |
| 101 | .replace(/(.{4}(?!$))/g, '$1:'); |
| 102 | break; |
| 103 | case types.TXT: |
| 104 | { |
| 105 | let position = offset; |
| 106 | rr.entries = []; |
| 107 | while (position < offset + dataLength) { |
| 108 | const txtLength = buffer[offset]; |
| 109 | rr.entries.push(buffer.toString('utf8', |
no test coverage detected
searching dependent graphs…