2020import io .netty .buffer .ByteBufHolder ;
2121import io .netty .channel .AddressedEnvelope ;
2222import io .netty .channel .socket .InternetProtocolFamily ;
23+ import io .netty .handler .codec .CorruptedFrameException ;
2324import io .netty .handler .codec .dns .DefaultDnsQuestion ;
2425import io .netty .handler .codec .dns .DefaultDnsRecordDecoder ;
2526import io .netty .handler .codec .dns .DnsResponseCode ;
2930import io .netty .handler .codec .dns .DnsRecord ;
3031import io .netty .handler .codec .dns .DnsRecordType ;
3132import io .netty .handler .codec .dns .DnsResponse ;
32- import io .netty .util .CharsetUtil ;
3333import io .netty .util .ReferenceCountUtil ;
3434import io .netty .util .concurrent .Future ;
3535import io .netty .util .concurrent .FutureListener ;
@@ -412,25 +412,23 @@ private void finishResolve() {
412412 final int tries = maxAllowedQueries - allowedQueries ;
413413 final StringBuilder buf = new StringBuilder (64 );
414414
415- buf .append ("failed to resolve " );
416- buf .append (hostname );
417-
415+ buf .append ("failed to resolve '" )
416+ .append (hostname ).append ('\'' );
418417 if (tries > 1 ) {
419- buf .append (" after " );
420- buf .append (tries );
421- if (trace != null ) {
422- buf .append (" queries:" );
423- buf .append (trace );
418+ if (tries < maxAllowedQueries ) {
419+ buf .append (" after " )
420+ .append (tries )
421+ .append (" queries " );
424422 } else {
425- buf .append (" queries" );
426- }
427- } else {
428- if (trace != null ) {
429- buf .append (':' );
430- buf .append (trace );
423+ buf .append (". Exceeded max queries per resolve " )
424+ .append (maxAllowedQueries )
425+ .append (' ' );
431426 }
432427 }
433-
428+ if (trace != null ) {
429+ buf .append (':' )
430+ .append (trace );
431+ }
434432 final UnknownHostException cause = new UnknownHostException (buf .toString ());
435433
436434 resolveCache .cache (hostname , cause , parent .ch .eventLoop ());
@@ -440,53 +438,15 @@ private void finishResolve() {
440438 protected abstract boolean finishResolve (
441439 InternetProtocolFamily f , List <DnsCacheEntry > resolvedEntries );
442440
443- /**
444- * Adapted from {@link DefaultDnsRecordDecoder#decodeName(ByteBuf)}.
445- */
446- static String decodeDomainName (ByteBuf buf ) {
447- buf .markReaderIndex ();
441+ static String decodeDomainName (ByteBuf in ) {
442+ in .markReaderIndex ();
448443 try {
449- int position = -1 ;
450- int checked = 0 ;
451- final int end = buf .writerIndex ();
452- final StringBuilder name = new StringBuilder (buf .readableBytes () << 1 );
453- for (int len = buf .readUnsignedByte (); buf .isReadable () && len != 0 ; len = buf .readUnsignedByte ()) {
454- boolean pointer = (len & 0xc0 ) == 0xc0 ;
455- if (pointer ) {
456- if (position == -1 ) {
457- position = buf .readerIndex () + 1 ;
458- }
459-
460- final int next = (len & 0x3f ) << 8 | buf .readUnsignedByte ();
461- if (next >= end ) {
462- // Should not happen.
463- return null ;
464- }
465- buf .readerIndex (next );
466-
467- // check for loops
468- checked += 2 ;
469- if (checked >= end ) {
470- // Name contains a loop; give up.
471- return null ;
472- }
473- } else {
474- name .append (buf .toString (buf .readerIndex (), len , CharsetUtil .UTF_8 )).append ('.' );
475- buf .skipBytes (len );
476- }
477- }
478-
479- if (position != -1 ) {
480- buf .readerIndex (position );
481- }
482-
483- if (name .length () == 0 ) {
484- return null ;
485- }
486-
487- return name .substring (0 , name .length () - 1 );
444+ return DefaultDnsRecordDecoder .decodeName (in );
445+ } catch (CorruptedFrameException e ) {
446+ // In this case we just return null.
447+ return null ;
488448 } finally {
489- buf .resetReaderIndex ();
449+ in .resetReaderIndex ();
490450 }
491451 }
492452
0 commit comments