import { AddressInfo } from "net" ; import { dnsLowerCase } from "../util/dns-equal" ; import { DNSLabelCoder } from "./DNSLabelCoder" ; import { DecodedData , DNSRecord , QClass , QType } from "./DNSPacket" ; export class Question implements DNSRecord { private static readonly QU_MASK = 0x8000 ; private static readonly NOT_QU_MASK = 0x7FFF ; readonly name : string ; private lowerCasedName ? : string ; readonly type : QType ; readonly class : QClass ; unicastResponseFlag = false ; constructor ( name , type , unicastResponseFlag = false , clazz = QClass . IN ) { if ( ! name . endsWith ( "." ) ) { name += "." ; } this . name = name ; this . type = type ; this . class = clazz ; this . unicastResponseFlag = unicastResponseFlag ; } public getLowerCasedName ( ) { return this . lowerCasedName || ( this . lowerCasedName = dnsLowerCase ( this . name ) ) ; } public getEncodingLength ( coder ) { return coder . getNameLength ( this . name ) + 4 ; } public encode ( coder , buffer , offset ) { const oldOffset = offset ; const nameLength = coder . encodeName ( this . name , offset ) ; offset += nameLength ; buffer . writeUInt16BE ( this . type , offset ) ; offset += 2 ; let qClass = this . class ; if ( this . unicastResponseFlag ) { qClass |= Question . QU_MASK ; } buffer . writeUInt16BE ( qClass , offset ) ; offset += 2 ; return offset - oldOffset ; } public clone ( ) { return new Question ( this . name , this . type , this . unicastResponseFlag , this . class ) ; } public asString ( ) { return `Q ${ this . name } ${ this . type } ${ this . class }` ; } public static decode ( context , coder , buffer , offset ) { const oldOffset = offset ; const decodedName = coder . decodeName ( offset ) ; offset += decodedName . readBytes ; const type = buffer . readUInt16BE ( offset ) as QType ; offset += 2 ; const qClass = buffer . readUInt16BE ( offset ) ; offset += 2 ; const clazz = ( qClass & this . NOT_QU_MASK ) as QClass ; const quFlag = ! ! ( qClass & this . QU_MASK ) ; const question = new Question ( decodedName . data , type , quFlag , clazz ) ; return { data : question , readBytes : offset - oldOffset , } ; } }