]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/nsecrecords.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "dnsrecords.hh"
27 class NSECBitmapGenerator
30 NSECBitmapGenerator(DNSPacketWriter
& pw_
): pw(pw_
)
33 void set(uint16_t type
)
35 uint16_t bit
= type
% 256;
36 int window
= static_cast<int>(type
/ 256);
38 if (window
!= oldWindow
) {
40 res
[0] = static_cast<unsigned char>(oldWindow
);
41 res
[1] = static_cast<unsigned char>(len
);
42 tmp
.assign(res
, res
+len
+2);
48 res
[2+bit
/8] |= 1 << (7-(bit
%8));
54 res
[0] = static_cast<unsigned char>(oldWindow
);
55 res
[1] = static_cast<unsigned char>(len
);
57 tmp
.assign(res
, res
+len
+2);
64 /* one byte for the window,
66 then the maximum of 32 bytes */
73 void NSECBitmap::toPacket(DNSPacketWriter
& pw
)
75 NSECBitmapGenerator
nbg(pw
);
77 size_t count
= d_bitset
->count();
79 for(size_t idx
= 0; idx
< nbTypes
&& found
< count
; ++idx
){
80 if (!d_bitset
->test(idx
)) {
88 for (const auto& type
: d_set
) {
96 void NSECBitmap::fromPacket(PacketReader
& pr
)
101 // 00 06 20 00 00 00 00 03 -> NS RRSIG NSEC ( 2, 46, 47 ) counts from left
106 if(bitmap
.size() < 2) {
107 throw MOADNSException("NSEC record with impossibly small bitmap");
110 for(unsigned int n
= 0; n
+1 < bitmap
.size();) {
111 unsigned int window
=static_cast<unsigned char>(bitmap
[n
++]);
112 unsigned int blen
=static_cast<unsigned char>(bitmap
[n
++]);
114 // end if zero padding and ensure packet length
115 if(window
== 0 && blen
== 0) {
119 if(n
+ blen
> bitmap
.size()) {
120 throw MOADNSException("NSEC record with bitmap length > packet length");
123 for(unsigned int k
=0; k
< blen
; k
++) {
124 uint8_t val
=bitmap
[n
++];
125 for(int bit
= 0; bit
< 8 ; ++bit
, val
>>=1) {
127 set((7-bit
) + 8*(k
) + 256*window
);
134 string
NSECBitmap::getZoneRepresentation() const
139 size_t count
= d_bitset
->count();
141 for(size_t idx
= 0; idx
< nbTypes
&& found
< count
; ++idx
) {
142 if (!d_bitset
->test(idx
)) {
148 ret
+=DNSRecordContent::NumberToType(idx
);
152 for(const auto& type
: d_set
) {
154 ret
+=DNSRecordContent::NumberToType(type
);
161 void NSECRecordContent::report(void)
163 regist(1, 47, &make
, &make
, "NSEC");
166 std::shared_ptr
<DNSRecordContent
> NSECRecordContent::make(const string
& content
)
168 return std::make_shared
<NSECRecordContent
>(content
);
171 NSECRecordContent::NSECRecordContent(const string
& content
, const string
& zone
)
173 RecordTextReader
rtr(content
, DNSName(zone
));
183 void NSECRecordContent::toPacket(DNSPacketWriter
& pw
)
186 d_bitmap
.toPacket(pw
);
189 std::shared_ptr
<NSECRecordContent::DNSRecordContent
> NSECRecordContent::make(const DNSRecord
&dr
, PacketReader
& pr
)
191 auto ret
=std::make_shared
<NSECRecordContent
>();
192 pr
.xfrName(ret
->d_next
);
194 ret
->d_bitmap
.fromPacket(pr
);
199 string
NSECRecordContent::getZoneRepresentation(bool noDot
) const
202 RecordTextWriter
rtw(ret
);
205 return ret
+ d_bitmap
.getZoneRepresentation();
208 ////// begin of NSEC3
210 void NSEC3RecordContent::report(void)
212 regist(1, 50, &make
, &make
, "NSEC3");
215 std::shared_ptr
<DNSRecordContent
> NSEC3RecordContent::make(const string
& content
)
217 return std::make_shared
<NSEC3RecordContent
>(content
);
220 NSEC3RecordContent::NSEC3RecordContent(const string
& content
, const string
& zone
)
222 RecordTextReader
rtr(content
, DNSName(zone
));
223 rtr
.xfr8BitInt(d_algorithm
);
224 rtr
.xfr8BitInt(d_flags
);
225 rtr
.xfr16BitInt(d_iterations
);
227 rtr
.xfrHexBlob(d_salt
);
228 rtr
.xfrBase32HexBlob(d_nexthash
);
237 void NSEC3RecordContent::toPacket(DNSPacketWriter
& pw
)
239 pw
.xfr8BitInt(d_algorithm
);
240 pw
.xfr8BitInt(d_flags
);
241 pw
.xfr16BitInt(d_iterations
);
242 pw
.xfr8BitInt(d_salt
.length());
245 pw
.xfr8BitInt(d_nexthash
.length());
246 pw
.xfrBlob(d_nexthash
);
248 d_bitmap
.toPacket(pw
);
251 std::shared_ptr
<NSEC3RecordContent::DNSRecordContent
> NSEC3RecordContent::make(const DNSRecord
&dr
, PacketReader
& pr
)
253 auto ret
=std::make_shared
<NSEC3RecordContent
>();
254 pr
.xfr8BitInt(ret
->d_algorithm
);
255 pr
.xfr8BitInt(ret
->d_flags
);
256 pr
.xfr16BitInt(ret
->d_iterations
);
259 pr
.xfrBlob(ret
->d_salt
, len
);
262 pr
.xfrBlob(ret
->d_nexthash
, len
);
264 ret
->d_bitmap
.fromPacket(pr
);
268 string
NSEC3RecordContent::getZoneRepresentation(bool noDot
) const
271 RecordTextWriter
rtw(ret
);
272 rtw
.xfr8BitInt(d_algorithm
);
273 rtw
.xfr8BitInt(d_flags
);
274 rtw
.xfr16BitInt(d_iterations
);
276 rtw
.xfrHexBlob(d_salt
);
277 rtw
.xfrBase32HexBlob(d_nexthash
);
279 return ret
+ d_bitmap
.getZoneRepresentation();
283 void NSEC3PARAMRecordContent::report(void)
285 regist(1, 51, &make
, &make
, "NSEC3PARAM");
286 regist(254, 51, &make
, &make
, "NSEC3PARAM");
289 std::shared_ptr
<DNSRecordContent
> NSEC3PARAMRecordContent::make(const string
& content
)
291 return std::make_shared
<NSEC3PARAMRecordContent
>(content
);
294 NSEC3PARAMRecordContent::NSEC3PARAMRecordContent(const string
& content
, const string
& zone
)
296 RecordTextReader
rtr(content
, DNSName(zone
));
297 rtr
.xfr8BitInt(d_algorithm
);
298 rtr
.xfr8BitInt(d_flags
);
299 rtr
.xfr16BitInt(d_iterations
);
300 rtr
.xfrHexBlob(d_salt
);
303 void NSEC3PARAMRecordContent::toPacket(DNSPacketWriter
& pw
)
305 pw
.xfr8BitInt(d_algorithm
);
306 pw
.xfr8BitInt(d_flags
);
307 pw
.xfr16BitInt(d_iterations
);
308 pw
.xfr8BitInt(d_salt
.length());
309 // cerr<<"salt: '"<<makeHexDump(d_salt)<<"', "<<d_salt.length()<<endl;
313 std::shared_ptr
<NSEC3PARAMRecordContent::DNSRecordContent
> NSEC3PARAMRecordContent::make(const DNSRecord
&dr
, PacketReader
& pr
)
315 auto ret
=std::make_shared
<NSEC3PARAMRecordContent
>();
316 pr
.xfr8BitInt(ret
->d_algorithm
);
317 pr
.xfr8BitInt(ret
->d_flags
);
318 pr
.xfr16BitInt(ret
->d_iterations
);
321 pr
.xfrHexBlob(ret
->d_salt
, len
);
325 string
NSEC3PARAMRecordContent::getZoneRepresentation(bool noDot
) const
328 RecordTextWriter
rtw(ret
);
329 rtw
.xfr8BitInt(d_algorithm
);
330 rtw
.xfr8BitInt(d_flags
);
331 rtw
.xfr16BitInt(d_iterations
);
332 rtw
.xfrHexBlob(d_salt
);