@@ -1100,7 +1100,7 @@ byte[] generateReply(Message query, Socket s)
11001100 LOG .debug ("calling addAnswer" );
11011101 byte rcode = addAnswer (response , name , type , dclass , 0 , flags );
11021102 if (rcode != Rcode .NOERROR ) {
1103- rcode = remoteLookup (response , name , type , 0 );
1103+ rcode = remoteLookup (response , name , type );
11041104 response .getHeader ().setRcode (rcode );
11051105 }
11061106 addAdditional (response , flags );
@@ -1118,55 +1118,44 @@ byte[] generateReply(Message query, Socket s)
11181118 /**
11191119 * Lookup record from upstream DNS servers.
11201120 */
1121- private byte remoteLookup (Message response , Name name , int type ,
1122- int iterations ) {
1121+ private byte remoteLookup (Message response , Name name , int type ) {
11231122 // If retrieving the root zone, query for NS record type
11241123 if (name .toString ().equals ("." )) {
11251124 type = Type .NS ;
11261125 }
11271126
1128- // Support for CNAME chaining
1129- if (type != Type .CNAME ) {
1130- Name targetName = name ;
1131- while (iterations < 6 ) {
1132- Record [] cnameAnswers = getRecords (targetName , Type .CNAME ).answers ;
1133- if (cnameAnswers == null ) {
1134- break ;
1127+ // Forward lookup to primary DNS servers
1128+ RemoteAnswer ra = getRecords (name , type );
1129+
1130+ // Support for CNAME/DNAME chaining
1131+ if (ra .aliases != null ) {
1132+ for (Name targetName : ra .aliases ) {
1133+ Record [] answers = getRecords (targetName , Type .CNAME ).answers ;
1134+ if (answers == null ) {
1135+ answers = getRecords (targetName , Type .DNAME ).answers ;
11351136 }
1136- for (Record cnameR : cnameAnswers ) {
1137- if (!response .findRecord (cnameR )) {
1138- response .addRecord (cnameR , Section .ANSWER );
1139- targetName = ((CNAMERecord ) cnameR ).getTarget ();
1137+ if (answers != null ) {
1138+ for (Record r : answers ) {
1139+ if (!response .findRecord (r )) {
1140+ response .addRecord (r , Section .ANSWER );
1141+ }
11401142 }
11411143 }
1142- iterations ++;
1143- }
1144- if (iterations < 6 && !targetName .equals (name )) {
1145- return remoteLookup (response , targetName , type , iterations + 1 );
11461144 }
11471145 }
1148-
1149- // Forward lookup to primary DNS servers
1150- RemoteAnswer ra = getRecords (name , type );
11511146 Record [] answers = ra .answers ;
11521147 // no answer
11531148 if (answers == null ) {
11541149 return (byte )ra .rcode ;
11551150 }
1156- try {
1157- for (Record r : answers ) {
1158- if (!response .findRecord (r )) {
1159- if (r .getType () == Type .SOA ) {
1160- response .addRecord (r , Section .AUTHORITY );
1161- } else {
1162- response .addRecord (r , Section .ANSWER );
1163- }
1151+ for (Record r : answers ) {
1152+ if (!response .findRecord (r )) {
1153+ if (r .getType () == Type .SOA ) {
1154+ response .addRecord (r , Section .AUTHORITY );
1155+ } else {
1156+ response .addRecord (r , Section .ANSWER );
11641157 }
11651158 }
1166- } catch (NullPointerException e ) {
1167- return Rcode .NXDOMAIN ;
1168- } catch (Throwable e ) {
1169- return Rcode .SERVFAIL ;
11701159 }
11711160 return Rcode .NOERROR ;
11721161 }
@@ -1196,12 +1185,12 @@ protected RemoteAnswer getRecords(Name name, int type) {
11961185 LOG .warn ("Unexpected result from lookup: {} type: {} error: {}" , name , Type .string (type ), lookup .getErrorString ());
11971186 break ;
11981187 }
1199- return new RemoteAnswer (lookup .getAnswers (), rcode );
1188+ return new RemoteAnswer (lookup .getAnswers (), lookup . getAliases (), rcode );
12001189 } catch (InterruptedException | ExecutionException |
12011190 TimeoutException | NullPointerException |
12021191 ExceptionInInitializerError e ) {
12031192 LOG .warn ("Failed to lookup: {} type: {}" , name , Type .string (type ), e );
1204- return new RemoteAnswer (null , Rcode .NXDOMAIN );
1193+ return new RemoteAnswer (null , null , Rcode .NXDOMAIN );
12051194 } finally {
12061195 executor .shutdown ();
12071196 }
@@ -1801,10 +1790,12 @@ public void close() {
18011790
18021791 public static class RemoteAnswer {
18031792 public Record [] answers ;
1793+ public Name [] aliases ;
18041794 public int rcode ;
18051795
1806- public RemoteAnswer (Record [] answers , int rcode ) {
1796+ public RemoteAnswer (Record [] answers , Name [] aliases , int rcode ) {
18071797 this .answers = answers ;
1798+ this .aliases = aliases ;
18081799 this .rcode = rcode ;
18091800 }
18101801 }
0 commit comments