import assert from "assert" ; import { dnsLowerCase } from "../../util/dns-equal" ; import { DNSLabelCoder } from "../DNSLabelCoder" ; import { DecodedData , RType } from "../DNSPacket" ; import { RecordRepresentation , ResourceRecord } from "../ResourceRecord" ; export class SRVRecord extends ResourceRecord { public static readonly DEFAULT_TTL = 120 ; readonly hostname : string ; private lowerCasedHostname ? : string ; readonly port : number ; private readonly priority : number ; private readonly weight : number ; constructor ( name , hostname , port , flushFlag ? , ttl ? ) ; constructor ( header , hostname , port ) ; constructor ( name , hostname , port , flushFlag ? , ttl ? ) { if ( typeof name === "string" ) { super ( name , RType . SRV , ttl || SRVRecord . RR_DEFAULT_TTL_SHORT , flushFlag ) ; } else { assert ( name . type === RType . SRV ) ; super ( name ) ; } if ( ! hostname . endsWith ( "." ) ) { this . hostname = hostname + "." ; } else { this . hostname = hostname ; } this . port = port ; this . priority = 0 ; this . weight = 0 ; } public getLowerCasedHostname ( ) { return this . lowerCasedHostname || ( this . lowerCasedHostname = dnsLowerCase ( this . hostname ) ) ; } protected getRDataEncodingLength ( coder ) { return 6 + ( coder . legacyUnicastEncoding ? coder . getUncompressedNameLength ( this . hostname ) : coder . getNameLength ( this . hostname ) ) ; } protected encodeRData ( coder , buffer , offset ) { const oldOffset = offset ; buffer . writeUInt16BE ( this . priority , offset ) ; offset += 2 ; buffer . writeUInt16BE ( this . weight , offset ) ; offset += 2 ; buffer . writeUInt16BE ( this . port , offset ) ; offset += 2 ; const hostnameLength = coder . legacyUnicastEncoding ? coder . encodeUncompressedName ( this . hostname , offset ) : coder . encodeName ( this . hostname , offset ) ; offset += hostnameLength ; return offset - oldOffset ; } public static decodeData ( coder , header , buffer , offset ) { const oldOffset = offset ; offset += 2 ; offset += 2 ; const port = buffer . readUInt16BE ( offset ) ; offset += 2 ; const decodedHostname = coder . decodeName ( offset ) ; offset += decodedHostname . readBytes ; return { data : new SRVRecord ( header , decodedHostname . data , port ) , readBytes : offset - oldOffset , } ; } public clone ( ) { return new SRVRecord ( this . getRecordRepresentation ( ) , this . hostname , this . port ) ; } public dataAsString ( ) { return `${ this . hostname } ${ this . port } ${ this . priority } ${ this . weight }` ; } public dataEquals ( record ) { return this . getLowerCasedHostname ( ) === record . getLowerCasedHostname ( ) && this . port === record . port && this . weight === record . weight && this . priority === record . priority ; } }