3333import io .netty .handler .codec .dns .DatagramDnsResponseDecoder ;
3434import io .netty .handler .codec .dns .DnsQuestion ;
3535import io .netty .handler .codec .dns .DnsResponse ;
36- import io .netty .resolver .NameResolver ;
37- import io .netty .resolver .SimpleNameResolver ;
36+ import io .netty .resolver .InetNameResolver ;
3837import io .netty .util .NetUtil ;
3938import io .netty .util .ReferenceCountUtil ;
4039import io .netty .util .concurrent .FastThreadLocal ;
4140import io .netty .util .concurrent .Future ;
4241import io .netty .util .concurrent .Promise ;
4342import io .netty .util .internal .OneTimeTask ;
4443import io .netty .util .internal .PlatformDependent ;
45- import io .netty .util .internal .SystemPropertyUtil ;
4644import io .netty .util .internal .logging .InternalLogger ;
4745import io .netty .util .internal .logging .InternalLoggerFactory ;
4846
4947import java .net .IDN ;
5048import java .net .InetAddress ;
5149import java .net .InetSocketAddress ;
52- import java .net .SocketAddress ;
5350import java .util .ArrayList ;
5451import java .util .Arrays ;
5552import java .util .Collections ;
6259import static io .netty .util .internal .ObjectUtil .checkNotNull ;
6360
6461/**
65- * A DNS-based {@link NameResolver }.
62+ * A DNS-based {@link InetNameResolver }.
6663 */
67- public class DnsNameResolver extends SimpleNameResolver < InetSocketAddress > {
64+ public class DnsNameResolver extends InetNameResolver {
6865
6966 private static final InternalLogger logger = InternalLoggerFactory .getInstance (DnsNameResolver .class );
7067
@@ -74,7 +71,7 @@ public class DnsNameResolver extends SimpleNameResolver<InetSocketAddress> {
7471
7572 static {
7673 // Note that we did not use SystemPropertyUtil.getBoolean() here to emulate the behavior of JDK.
77- if ("true" . equalsIgnoreCase ( SystemPropertyUtil . get ( "java.net.preferIPv6Addresses" ) )) {
74+ if (Boolean . getBoolean ( "java.net.preferIPv6Addresses" )) {
7875 DEFAULT_RESOLVE_ADDRESS_TYPES [0 ] = InternetProtocolFamily .IPv6 ;
7976 DEFAULT_RESOLVE_ADDRESS_TYPES [1 ] = InternetProtocolFamily .IPv4 ;
8077 logger .debug ("-Djava.net.preferIPv6Addresses: true" );
@@ -98,7 +95,7 @@ public class DnsNameResolver extends SimpleNameResolver<InetSocketAddress> {
9895 final DnsQueryContextManager queryContextManager = new DnsQueryContextManager ();
9996
10097 /**
101- * Cache for {@link #doResolve(InetSocketAddress , Promise)} and {@link #doResolveAll(InetSocketAddress , Promise)}.
98+ * Cache for {@link #doResolve(String , Promise)} and {@link #doResolveAll(String , Promise)}.
10299 */
103100 final ConcurrentMap <String , List <DnsCacheEntry >> resolveCache = PlatformDependent .newConcurrentHashMap ();
104101
@@ -329,7 +326,7 @@ public DnsNameResolver setQueryTimeoutMillis(long queryTimeoutMillis) {
329326 }
330327
331328 /**
332- * Returns the list of the protocol families of the address resolved by {@link #resolve(SocketAddress )}
329+ * Returns the list of the protocol families of the address resolved by {@link #resolve(String )}
333330 * in the order of preference.
334331 * The default value depends on the value of the system property {@code "java.net.preferIPv6Addresses"}.
335332 *
@@ -344,7 +341,7 @@ InternetProtocolFamily[] resolveAddressTypesUnsafe() {
344341 }
345342
346343 /**
347- * Sets the list of the protocol families of the address resolved by {@link #resolve(SocketAddress )}.
344+ * Sets the list of the protocol families of the address resolved by {@link #resolve(String )}.
348345 * Usually, both {@link InternetProtocolFamily#IPv4} and {@link InternetProtocolFamily#IPv6} are specified in the
349346 * order of preference. To enforce the resolve to retrieve the address of a specific protocol family, specify
350347 * only a single {@link InternetProtocolFamily}.
@@ -382,7 +379,7 @@ public DnsNameResolver setResolveAddressTypes(InternetProtocolFamily... resolveA
382379 }
383380
384381 /**
385- * Sets the list of the protocol families of the address resolved by {@link #resolve(SocketAddress )}.
382+ * Sets the list of the protocol families of the address resolved by {@link #resolve(String )}.
386383 * Usually, both {@link InternetProtocolFamily#IPv4} and {@link InternetProtocolFamily#IPv6} are specified in the
387384 * order of preference. To enforce the resolve to retrieve the address of a specific protocol family, specify
388385 * only a single {@link InternetProtocolFamily}.
@@ -594,28 +591,22 @@ protected EventLoop executor() {
594591 }
595592
596593 @ Override
597- protected boolean doIsResolved (InetSocketAddress address ) {
598- return !address .isUnresolved ();
599- }
600-
601- @ Override
602- protected void doResolve (InetSocketAddress unresolvedAddress , Promise <InetSocketAddress > promise ) throws Exception {
603- final byte [] bytes = NetUtil .createByteArrayFromIpAddressString (unresolvedAddress .getHostName ());
594+ protected void doResolve (String inetHost , Promise <InetAddress > promise ) throws Exception {
595+ final byte [] bytes = NetUtil .createByteArrayFromIpAddressString (inetHost );
604596 if (bytes != null ) {
605- // The unresolvedAddress was created via a String that contains an ipaddress.
606- promise .setSuccess (new InetSocketAddress ( InetAddress .getByAddress (bytes ), unresolvedAddress . getPort () ));
597+ // The inetHost is actually an ipaddress.
598+ promise .setSuccess (InetAddress .getByAddress (bytes ));
607599 return ;
608600 }
609601
610- final String hostname = hostname (unresolvedAddress );
611- final int port = unresolvedAddress .getPort ();
602+ final String hostname = hostname (inetHost );
612603
613- if (!doResolveCached (hostname , port , promise )) {
614- doResolveUncached (hostname , port , promise );
604+ if (!doResolveCached (hostname , promise )) {
605+ doResolveUncached (hostname , promise );
615606 }
616607 }
617608
618- private boolean doResolveCached (String hostname , int port , Promise <InetSocketAddress > promise ) {
609+ private boolean doResolveCached (String hostname , Promise <InetAddress > promise ) {
619610 final List <DnsCacheEntry > cachedEntries = resolveCache .get (hostname );
620611 if (cachedEntries == null ) {
621612 return false ;
@@ -644,7 +635,7 @@ private boolean doResolveCached(String hostname, int port, Promise<InetSocketAdd
644635 }
645636
646637 if (address != null ) {
647- setSuccess (promise , new InetSocketAddress ( address , port ) );
638+ setSuccess (promise , address );
648639 } else if (cause != null ) {
649640 if (!promise .tryFailure (cause )) {
650641 logger .warn ("Failed to notify failure to a promise: {}" , promise , cause );
@@ -656,15 +647,15 @@ private boolean doResolveCached(String hostname, int port, Promise<InetSocketAdd
656647 return true ;
657648 }
658649
659- private static void setSuccess (Promise <InetSocketAddress > promise , InetSocketAddress result ) {
650+ private static void setSuccess (Promise <InetAddress > promise , InetAddress result ) {
660651 if (!promise .trySuccess (result )) {
661652 logger .warn ("Failed to notify success ({}) to a promise: {}" , result , promise );
662653 }
663654 }
664655
665- private void doResolveUncached (String hostname , final int port , Promise <InetSocketAddress > promise ) {
666- final DnsNameResolverContext <InetSocketAddress > ctx =
667- new DnsNameResolverContext <InetSocketAddress >(this , hostname , promise ) {
656+ private void doResolveUncached (String hostname , Promise <InetAddress > promise ) {
657+ final DnsNameResolverContext <InetAddress > ctx =
658+ new DnsNameResolverContext <InetAddress >(this , hostname , promise ) {
668659 @ Override
669660 protected boolean finishResolve (
670661 Class <? extends InetAddress > addressType , List <DnsCacheEntry > resolvedEntries ) {
@@ -673,7 +664,7 @@ protected boolean finishResolve(
673664 for (int i = 0 ; i < numEntries ; i ++) {
674665 final InetAddress a = resolvedEntries .get (i ).address ();
675666 if (addressType .isInstance (a )) {
676- setSuccess (promise (), new InetSocketAddress ( a , port ) );
667+ setSuccess (promise (), a );
677668 return true ;
678669 }
679670 }
@@ -685,32 +676,29 @@ protected boolean finishResolve(
685676 }
686677
687678 @ Override
688- protected void doResolveAll (
689- InetSocketAddress unresolvedAddress , Promise <List <InetSocketAddress >> promise ) throws Exception {
679+ protected void doResolveAll (String inetHost , Promise <List <InetAddress >> promise ) throws Exception {
690680
691- final byte [] bytes = NetUtil .createByteArrayFromIpAddressString (unresolvedAddress . getHostName () );
681+ final byte [] bytes = NetUtil .createByteArrayFromIpAddressString (inetHost );
692682 if (bytes != null ) {
693683 // The unresolvedAddress was created via a String that contains an ipaddress.
694- promise .setSuccess (Collections .singletonList (
695- new InetSocketAddress (InetAddress .getByAddress (bytes ), unresolvedAddress .getPort ())));
684+ promise .setSuccess (Collections .singletonList (InetAddress .getByAddress (bytes )));
696685 return ;
697686 }
698687
699- final String hostname = hostname (unresolvedAddress );
700- final int port = unresolvedAddress .getPort ();
688+ final String hostname = hostname (inetHost );
701689
702- if (!doResolveAllCached (hostname , port , promise )) {
703- doResolveAllUncached (hostname , port , promise );
690+ if (!doResolveAllCached (hostname , promise )) {
691+ doResolveAllUncached (hostname , promise );
704692 }
705693 }
706694
707- private boolean doResolveAllCached (String hostname , int port , Promise <List <InetSocketAddress >> promise ) {
695+ private boolean doResolveAllCached (String hostname , Promise <List <InetAddress >> promise ) {
708696 final List <DnsCacheEntry > cachedEntries = resolveCache .get (hostname );
709697 if (cachedEntries == null ) {
710698 return false ;
711699 }
712700
713- List <InetSocketAddress > result = null ;
701+ List <InetAddress > result = null ;
714702 Throwable cause = null ;
715703 synchronized (cachedEntries ) {
716704 final int numEntries = cachedEntries .size ();
@@ -724,9 +712,9 @@ private boolean doResolveAllCached(String hostname, int port, Promise<List<InetS
724712 final DnsCacheEntry e = cachedEntries .get (i );
725713 if (f .addressType ().isInstance (e .address ())) {
726714 if (result == null ) {
727- result = new ArrayList <InetSocketAddress >(numEntries );
715+ result = new ArrayList <InetAddress >(numEntries );
728716 }
729- result .add (new InetSocketAddress ( e .address (), port ));
717+ result .add (e .address ());
730718 }
731719 }
732720 }
@@ -744,23 +732,22 @@ private boolean doResolveAllCached(String hostname, int port, Promise<List<InetS
744732 return true ;
745733 }
746734
747- private void doResolveAllUncached (final String hostname , final int port ,
748- final Promise <List <InetSocketAddress >> promise ) {
749- final DnsNameResolverContext <List <InetSocketAddress >> ctx =
750- new DnsNameResolverContext <List <InetSocketAddress >>(this , hostname , promise ) {
735+ private void doResolveAllUncached (final String hostname , final Promise <List <InetAddress >> promise ) {
736+ final DnsNameResolverContext <List <InetAddress >> ctx =
737+ new DnsNameResolverContext <List <InetAddress >>(this , hostname , promise ) {
751738 @ Override
752739 protected boolean finishResolve (
753740 Class <? extends InetAddress > addressType , List <DnsCacheEntry > resolvedEntries ) {
754741
755- List <InetSocketAddress > result = null ;
742+ List <InetAddress > result = null ;
756743 final int numEntries = resolvedEntries .size ();
757744 for (int i = 0 ; i < numEntries ; i ++) {
758745 final InetAddress a = resolvedEntries .get (i ).address ();
759746 if (addressType .isInstance (a )) {
760747 if (result == null ) {
761- result = new ArrayList <InetSocketAddress >(numEntries );
748+ result = new ArrayList <InetAddress >(numEntries );
762749 }
763- result .add (new InetSocketAddress ( a , port ) );
750+ result .add (a );
764751 }
765752 }
766753
@@ -775,16 +762,8 @@ protected boolean finishResolve(
775762 ctx .resolve ();
776763 }
777764
778- private static String hostname (InetSocketAddress addr ) {
779- // InetSocketAddress.getHostString() is available since Java 7.
780- final String hostname ;
781- if (PlatformDependent .javaVersion () < 7 ) {
782- hostname = addr .getHostName ();
783- } else {
784- hostname = addr .getHostString ();
785- }
786-
787- return IDN .toASCII (hostname );
765+ private static String hostname (String inetHost ) {
766+ return IDN .toASCII (inetHost );
788767 }
789768
790769 void cache (String hostname , InetAddress address , long originalTtl ) {
0 commit comments