public static ByteBuf createDnsQueryPayload ( DnsTransaction transaction ) { ByteBuf byteBuf = ByteBufPool . allocate ( MAX_SIZE ) ; byteBuf . writeShort ( transaction . getId ( ) ) ; // standard query flags, 1 question and 0 of other stuff byteBuf . write ( STANDARD_QUERY_HEADER ) ; // query domain name byte componentSize = 0 ; DnsQuery query = transaction . getQuery ( ) ; byte [ ] domainBytes = query . getDomainName ( ) . getBytes ( US_ASCII ) ; int pos = - 1 ; while ( ++ pos < domainBytes . length ) { if ( domainBytes [ pos ] != ' ' ) { componentSize ++ ; continue ; } byteBuf . writeByte ( componentSize ) ; byteBuf . write ( domainBytes , pos - componentSize , componentSize ) ; componentSize = 0 ; } byteBuf . writeByte ( componentSize ) ; byteBuf . write ( domainBytes , pos - componentSize , componentSize ) ; byteBuf . writeByte ( ( byte ) 0x0 ) ; // terminator byte // query record type byteBuf . writeShort ( query . getRecordType ( ) . getCode ( ) ) ; // query class: IN byteBuf . writeShort ( QueryClass . INTERNET . getCode ( ) ) ; return byteBuf ; }
Creates a bytebuf with a DNS query payload