(buffer, offset)
| 25 | // Naïve DNS parser/serializer. |
| 26 | |
| 27 | function 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 | |
| 52 | function parseDNSPacket(buffer) { |
| 53 | assert.ok(buffer.length > 12); |
no test coverage detected
searching dependent graphs…