20
20
import io .netty .buffer .ByteBufHolder ;
21
21
import io .netty .channel .AddressedEnvelope ;
22
22
import io .netty .channel .socket .InternetProtocolFamily ;
23
+ import io .netty .handler .codec .CorruptedFrameException ;
23
24
import io .netty .handler .codec .dns .DefaultDnsQuestion ;
24
25
import io .netty .handler .codec .dns .DefaultDnsRecordDecoder ;
25
26
import io .netty .handler .codec .dns .DnsResponseCode ;
29
30
import io .netty .handler .codec .dns .DnsRecord ;
30
31
import io .netty .handler .codec .dns .DnsRecordType ;
31
32
import io .netty .handler .codec .dns .DnsResponse ;
32
- import io .netty .util .CharsetUtil ;
33
33
import io .netty .util .ReferenceCountUtil ;
34
34
import io .netty .util .concurrent .Future ;
35
35
import io .netty .util .concurrent .FutureListener ;
@@ -412,25 +412,23 @@ private void finishResolve() {
412
412
final int tries = maxAllowedQueries - allowedQueries ;
413
413
final StringBuilder buf = new StringBuilder (64 );
414
414
415
- buf .append ("failed to resolve " );
416
- buf .append (hostname );
417
-
415
+ buf .append ("failed to resolve '" )
416
+ .append (hostname ).append ('\'' );
418
417
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 " );
424
422
} 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 (' ' );
431
426
}
432
427
}
433
-
428
+ if (trace != null ) {
429
+ buf .append (':' )
430
+ .append (trace );
431
+ }
434
432
final UnknownHostException cause = new UnknownHostException (buf .toString ());
435
433
436
434
resolveCache .cache (hostname , cause , parent .ch .eventLoop ());
@@ -440,53 +438,15 @@ private void finishResolve() {
440
438
protected abstract boolean finishResolve (
441
439
InternetProtocolFamily f , List <DnsCacheEntry > resolvedEntries );
442
440
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 ();
448
443
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 ;
488
448
} finally {
489
- buf .resetReaderIndex ();
449
+ in .resetReaderIndex ();
490
450
}
491
451
}
492
452
0 commit comments