import assert from "assert" ; import net from "net" ; import { DNSLabelCoder } from "../DNSLabelCoder" ; import { DecodedData , RType } from "../DNSPacket" ; import { RecordRepresentation , ResourceRecord } from "../ResourceRecord" ; export class ARecord extends ResourceRecord { public static readonly DEFAULT_TTL = 120 ; readonly ipAddress : string ; constructor ( name , ipAddress , flushFlag ? , ttl ? ) ; constructor ( header , ipAddress ) ; constructor ( name , ipAddress , flushFlag ? , ttl ? ) { if ( typeof name === "string" ) { super ( name , RType . A , ttl || ARecord . RR_DEFAULT_TTL_SHORT , flushFlag ) ; } else { assert ( name . type === RType . A ) ; super ( name ) ; } assert ( net . isIPv4 ( ipAddress ) , "IP address is not in v4 format!" ) ; this . ipAddress = ipAddress ; } protected getRDataEncodingLength ( ) { return 4 ; } protected encodeRData ( coder , buffer , offset ) { const oldOffset = offset ; const bytes = this . ipAddress . split ( "." ) ; assert ( bytes . length === 4 , "invalid ip address" ) ; for ( const byte of bytes ) { const number = parseInt ( byte , 10 ) ; buffer . writeUInt8 ( number , offset ++ ) ; } return offset - oldOffset ; } public static decodeData ( coder , header , buffer , offset ) { const oldOffset = offset ; const ipBytes = new Array ( 4 ) ; for ( let i = 0 ; i < 4 ; i ++ ) { const byte = buffer . readUInt8 ( offset ++ ) ; ipBytes [ i ] = byte . toString ( 10 ) ; } const ipAddress = ipBytes . join ( "." ) ; return { data : new ARecord ( header , ipAddress ) , readBytes : offset - oldOffset , } ; } public clone ( ) { return new ARecord ( this . getRecordRepresentation ( ) , this . ipAddress ) ; } public dataAsString ( ) { return this . ipAddress ; } public dataEquals ( record ) { return this . ipAddress === record . ipAddress ; } }