@@ -810,9 +810,11 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
810
810
List <RRset > additionalSectionSets = getSectionRRsets (Section .ADDITIONAL );
811
811
List <RRset > authoritySectionSets = getSectionRRsets (Section .AUTHORITY );
812
812
813
- List <RRset > cleanedAnswerSection = new ArrayList <>();
814
- List <RRset > cleanedAuthoritySection = new ArrayList <>();
815
- List <RRset > cleanedAdditionalSection = new ArrayList <>();
813
+ @ SuppressWarnings ("unchecked" )
814
+ List <RRset >[] cleanedSection = new ArrayList [4 ];
815
+ cleanedSection [Section .ANSWER ] = new ArrayList <>();
816
+ cleanedSection [Section .AUTHORITY ] = new ArrayList <>();
817
+ cleanedSection [Section .ADDITIONAL ] = new ArrayList <>();
816
818
boolean hadNsInAuthority = false ;
817
819
818
820
// For the ANSWER section, remove all "irrelevant" records and add synthesized CNAMEs from
@@ -843,7 +845,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
843
845
// If DNAME was queried, don't attempt to synthesize CNAME
844
846
if (query .getQuestion ().getType () != Type .DNAME ) {
845
847
// The DNAME is valid, accept it
846
- cleanedAnswerSection .add (rrset );
848
+ cleanedSection [ Section . ANSWER ] .add (rrset );
847
849
848
850
// Check if the next rrset is correct CNAME, otherwise synthesize a CNAME
849
851
RRset nextRRSet = answerSectionSets .size () >= i + 2 ? answerSectionSets .get (i + 1 ) : null ;
@@ -863,7 +865,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
863
865
864
866
// Add a synthesized CNAME; TTL=0 to avoid caching
865
867
Name dnameTarget = sname .fromDNAME (dname );
866
- cleanedAnswerSection .add (
868
+ cleanedSection [ Section . ANSWER ] .add (
867
869
new RRset (new CNAMERecord (sname , dname .getDClass (), 0 , dnameTarget )));
868
870
sname = dnameTarget ;
869
871
@@ -872,7 +874,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
872
874
for (i ++; i < answerSectionSets .size (); i ++) {
873
875
rrset = answerSectionSets .get (i );
874
876
if (rrset .getName ().equals (oldSname )) {
875
- cleanedAnswerSection .add (rrset );
877
+ cleanedSection [ Section . ANSWER ] .add (rrset );
876
878
} else {
877
879
break ;
878
880
}
@@ -943,14 +945,14 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
943
945
}
944
946
945
947
sname = ((CNAMERecord ) rrset .first ()).getTarget ();
946
- cleanedAnswerSection .add (rrset );
948
+ cleanedSection [ Section . ANSWER ] .add (rrset );
947
949
948
950
// In CNAME ANY response, can have data after CNAME
949
951
if (query .getQuestion ().getType () == Type .ANY ) {
950
952
for (i ++; i < answerSectionSets .size (); i ++) {
951
953
rrset = answerSectionSets .get (i );
952
954
if (rrset .getName ().equals (oldSname )) {
953
- cleanedAnswerSection .add (rrset );
955
+ cleanedSection [ Section . ANSWER ] .add (rrset );
954
956
} else {
955
957
break ;
956
958
}
@@ -973,9 +975,9 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
973
975
}
974
976
975
977
// Mark the additional names from relevant RRset as OK
976
- cleanedAnswerSection .add (rrset );
978
+ cleanedSection [ Section . ANSWER ] .add (rrset );
977
979
if (sname .equals (rrset .getName ())) {
978
- addAdditionalRRset (rrset , additionalSectionSets , cleanedAdditionalSection );
980
+ addAdditionalRRset (rrset , additionalSectionSets , cleanedSection [ Section . ADDITIONAL ] );
979
981
}
980
982
}
981
983
@@ -1045,15 +1047,25 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
1045
1047
}
1046
1048
}
1047
1049
1048
- cleanedAuthoritySection .add (rrset );
1049
- addAdditionalRRset (rrset , additionalSectionSets , cleanedAdditionalSection );
1050
+ cleanedSection [ Section . AUTHORITY ] .add (rrset );
1051
+ addAdditionalRRset (rrset , additionalSectionSets , cleanedSection [ Section . ADDITIONAL ] );
1050
1052
}
1051
1053
1052
1054
Message cleanedMessage = new Message (this .getHeader ());
1053
1055
cleanedMessage .sections [Section .QUESTION ] = this .sections [Section .QUESTION ];
1054
- cleanedMessage .sections [Section .ANSWER ] = rrsetListToRecords (cleanedAnswerSection );
1055
- cleanedMessage .sections [Section .AUTHORITY ] = rrsetListToRecords (cleanedAuthoritySection );
1056
- cleanedMessage .sections [Section .ADDITIONAL ] = rrsetListToRecords (cleanedAdditionalSection );
1056
+ for (int section : new int [] {Section .ANSWER , Section .AUTHORITY , Section .ADDITIONAL }) {
1057
+ cleanedMessage .sections [section ] = rrsetListToRecords (cleanedSection [section ]);
1058
+
1059
+ // Fixup counts in the header
1060
+ cleanedMessage
1061
+ .getHeader ()
1062
+ .setCount (
1063
+ section ,
1064
+ cleanedMessage .sections [section ] == null
1065
+ ? 0
1066
+ : cleanedMessage .sections [section ].size ());
1067
+ }
1068
+
1057
1069
return cleanedMessage ;
1058
1070
}
1059
1071
0 commit comments