@@ -1101,7 +1101,7 @@ byte[] generateReply(Message query, Socket s)
11011101 LOG .debug ("calling addAnswer" );
11021102 byte rcode = addAnswer (response , name , type , dclass , 0 , flags );
11031103 if (rcode != Rcode .NOERROR ) {
1104- rcode = remoteLookup (response , name , type , 0 );
1104+ rcode = remoteLookup (response , name , type );
11051105 response .getHeader ().setRcode (rcode );
11061106 }
11071107 addAdditional (response , flags );
@@ -1119,55 +1119,44 @@ byte[] generateReply(Message query, Socket s)
11191119 /**
11201120 * Lookup record from upstream DNS servers.
11211121 */
1122- private byte remoteLookup (Message response , Name name , int type ,
1123- int iterations ) {
1122+ private byte remoteLookup (Message response , Name name , int type ) {
11241123 // If retrieving the root zone, query for NS record type
11251124 if (name .toString ().equals ("." )) {
11261125 type = Type .NS ;
11271126 }
11281127
1129- // Support for CNAME chaining
1130- if (type != Type .CNAME ) {
1131- Name targetName = name ;
1132- while (iterations < 6 ) {
1133- Record [] cnameAnswers = getRecords (targetName , Type .CNAME ).answers ;
1134- if (cnameAnswers == null ) {
1135- break ;
1128+ // Forward lookup to primary DNS servers
1129+ RemoteAnswer ra = getRecords (name , type );
1130+
1131+ // Support for CNAME/DNAME chaining
1132+ if (ra .aliases != null ) {
1133+ for (Name targetName : ra .aliases ) {
1134+ Record [] answers = getRecords (targetName , Type .CNAME ).answers ;
1135+ if (answers == null ) {
1136+ answers = getRecords (targetName , Type .DNAME ).answers ;
11361137 }
1137- for (Record cnameR : cnameAnswers ) {
1138- if (!response .findRecord (cnameR )) {
1139- response .addRecord (cnameR , Section .ANSWER );
1140- targetName = ((CNAMERecord ) cnameR ).getTarget ();
1138+ if (answers != null ) {
1139+ for (Record r : answers ) {
1140+ if (!response .findRecord (r )) {
1141+ response .addRecord (r , Section .ANSWER );
1142+ }
11411143 }
11421144 }
1143- iterations ++;
1144- }
1145- if (iterations < 6 && !targetName .equals (name )) {
1146- return remoteLookup (response , targetName , type , iterations + 1 );
11471145 }
11481146 }
1149-
1150- // Forward lookup to primary DNS servers
1151- RemoteAnswer ra = getRecords (name , type );
11521147 Record [] answers = ra .answers ;
11531148 // no answer
11541149 if (answers == null ) {
11551150 return (byte )ra .rcode ;
11561151 }
1157- try {
1158- for (Record r : answers ) {
1159- if (!response .findRecord (r )) {
1160- if (r .getType () == Type .SOA ) {
1161- response .addRecord (r , Section .AUTHORITY );
1162- } else {
1163- response .addRecord (r , Section .ANSWER );
1164- }
1152+ for (Record r : answers ) {
1153+ if (!response .findRecord (r )) {
1154+ if (r .getType () == Type .SOA ) {
1155+ response .addRecord (r , Section .AUTHORITY );
1156+ } else {
1157+ response .addRecord (r , Section .ANSWER );
11651158 }
11661159 }
1167- } catch (NullPointerException e ) {
1168- return Rcode .NXDOMAIN ;
1169- } catch (Throwable e ) {
1170- return Rcode .SERVFAIL ;
11711160 }
11721161 return Rcode .NOERROR ;
11731162 }
@@ -1197,12 +1186,12 @@ protected RemoteAnswer getRecords(Name name, int type) {
11971186 LOG .warn ("Unexpected result from lookup: {} type: {} error: {}" , name , Type .string (type ), lookup .getErrorString ());
11981187 break ;
11991188 }
1200- return new RemoteAnswer (lookup .getAnswers (), rcode );
1189+ return new RemoteAnswer (lookup .getAnswers (), lookup . getAliases (), rcode );
12011190 } catch (InterruptedException | ExecutionException |
12021191 TimeoutException | NullPointerException |
12031192 ExceptionInInitializerError e ) {
12041193 LOG .warn ("Failed to lookup: {} type: {}" , name , Type .string (type ), e );
1205- return new RemoteAnswer (null , Rcode .NXDOMAIN );
1194+ return new RemoteAnswer (null , null , Rcode .NXDOMAIN );
12061195 } finally {
12071196 executor .shutdown ();
12081197 }
@@ -1802,10 +1791,12 @@ public void close() {
18021791
18031792 public static class RemoteAnswer {
18041793 public Record [] answers ;
1794+ public Name [] aliases ;
18051795 public int rcode ;
18061796
1807- public RemoteAnswer (Record [] answers , int rcode ) {
1797+ public RemoteAnswer (Record [] answers , Name [] aliases , int rcode ) {
18081798 this .answers = answers ;
1799+ this .aliases = aliases ;
18091800 this .rcode = rcode ;
18101801 }
18111802 }
0 commit comments