1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
// Copyright 2015-2019 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.
use std::iter::Chain;
use std::slice::Iter;
use std::vec;
use tracing::{info, warn};
use crate::rr::{DNSClass, Name, RData, Record, RecordType};
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
use crate::rr::dnssec::SupportedAlgorithms;
/// Set of resource records associated to a name and type
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RecordSet {
name: Name,
record_type: RecordType,
dns_class: DNSClass,
ttl: u32,
records: Vec<Record>,
rrsigs: Vec<Record>,
serial: u32, // serial number at which this record was modified
}
impl RecordSet {
/// Creates a new Resource Record Set.
///
/// # Arguments
///
/// * `name` - The label for the `RecordSet`
/// * `record_type` - `RecordType` of this `RecordSet`, all records in the `RecordSet` must be of the
/// specified `RecordType`.
/// * `serial` - current serial number of the `SOA` record, this is to be used for `IXFR` and
/// signing for DNSSec after updates.
///
/// # Return value
///
/// The newly created Resource Record Set
/// TODO: make all cloned params pass by value
pub fn new(name: &Name, record_type: RecordType, serial: u32) -> Self {
Self {
name: name.clone(),
record_type,
dns_class: DNSClass::IN,
ttl: 0,
records: Vec::new(),
rrsigs: Vec::new(),
serial,
}
}
/// Creates a new Resource Record Set.
///
/// # Arguments
///
/// * `name` - The label for the `RecordSet`
/// * `record_type` - `RecordType` of this `RecordSet`, all records in the `RecordSet` must be of the
/// specified `RecordType`.
/// * `ttl` - time-to-live for the `RecordSet` in seconds.
///
/// # Return value
///
/// The newly created Resource Record Set
/// TODO: make all cloned params pass by value
pub fn with_ttl(name: Name, record_type: RecordType, ttl: u32) -> Self {
Self {
name,
record_type,
dns_class: DNSClass::IN,
ttl,
records: Vec::new(),
rrsigs: Vec::new(),
serial: 0,
}
}
/// # Return value
///
/// Label of the Resource Record Set
pub fn name(&self) -> &Name {
&self.name
}
/// # Return value
///
/// `RecordType` of the Resource Record Set
pub fn record_type(&self) -> RecordType {
self.record_type
}
/// Sets the DNSClass to the specified value
///
/// This will traverse every record and associate with it the specified dns_class
pub fn set_dns_class(&mut self, dns_class: DNSClass) {
self.dns_class = dns_class;
for r in &mut self.records {
r.set_dns_class(dns_class);
}
}
/// Returns the `DNSClass` of the RecordSet
pub fn dns_class(&self) -> DNSClass {
self.dns_class
}
/// Sets the TTL, in seconds, to the specified value
///
/// This will traverse every record and associate with it the specified ttl
pub fn set_ttl(&mut self, ttl: u32) {
self.ttl = ttl;
for r in &mut self.records {
r.set_ttl(ttl);
}
}
/// Returns the time-to-live for the record.
///
/// # Return value
///
/// TTL, time-to-live, of the Resource Record Set, this is the maximum length of time that an
/// RecordSet should be cached.
pub fn ttl(&self) -> u32 {
self.ttl
}
/// Returns a Vec of all records in the set.
///
/// # Arguments
///
/// * `and_rrsigs` - if true, RRSIGs will be returned if they exist
/// * `supported_algorithms` - the RRSIGs will be filtered by the set of supported_algorithms,
/// and then only the maximal RRSIG algorithm will be returned.
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
pub fn records(
&self,
and_rrsigs: bool,
supported_algorithms: SupportedAlgorithms,
) -> RrsetRecords<'_> {
if and_rrsigs {
self.records_with_rrsigs(supported_algorithms)
} else {
self.records_without_rrsigs()
}
}
/// Returns a Vec of all records in the set, with RRSIGs, if present.
///
/// # Arguments
///
/// * `supported_algorithms` - the RRSIGs will be filtered by the set of supported_algorithms,
/// and then only the maximal RRSIG algorithm will be returned.
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
pub fn records_with_rrsigs(
&self,
supported_algorithms: SupportedAlgorithms,
) -> RrsetRecords<'_> {
if self.records.is_empty() {
RrsetRecords::Empty
} else {
let rrsigs = RrsigsByAlgorithms {
rrsigs: self.rrsigs.iter(),
supported_algorithms,
};
RrsetRecords::RecordsAndRrsigs(RecordsAndRrsigsIter(self.records.iter().chain(rrsigs)))
}
}
/// Returns a Vec of all records in the set, without any RRSIGs.
pub fn records_without_rrsigs(&self) -> RrsetRecords<'_> {
if self.records.is_empty() {
RrsetRecords::Empty
} else {
RrsetRecords::RecordsOnly(self.records.iter())
}
}
/// Returns an iterator over the records in the set
#[deprecated(note = "see `records_without_rrsigs`")]
pub fn iter(&self) -> Iter<'_, Record> {
self.records.iter()
}
/// Returns true if there are no records in this set
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
/// Returns the serial number at which the record was updated.
pub fn serial(&self) -> u32 {
self.serial
}
/// Returns a slice of all the Records signatures in the RecordSet
pub fn rrsigs(&self) -> &[Record] {
&self.rrsigs
}
/// Inserts a Signature for the Record set
///
/// Many can be associated with the RecordSet. Once added, the RecordSet should not be changed
///
/// # Arguments
///
/// * `rrsig` - A signature which covers the RecordSet.
pub fn insert_rrsig(&mut self, rrsig: Record) {
self.rrsigs.push(rrsig)
}
/// Useful for clearing all signatures when the RecordSet is updated, or keys are rotated.
pub fn clear_rrsigs(&mut self) {
self.rrsigs.clear()
}
fn updated(&mut self, serial: u32) {
self.serial = serial;
self.rrsigs.clear(); // on updates, the rrsigs are invalid
}
/// creates a new Record as part of this RecordSet, adding the associated RData
///
/// this interface may be deprecated in the future.
pub fn new_record(&mut self, rdata: &RData) -> &Record {
self.add_rdata(rdata.clone());
self.records
.iter()
.find(|r| r.data().map(|r| r == rdata).unwrap_or(false))
.expect("insert failed")
}
/// creates a new Record as part of this RecordSet, adding the associated RData
pub fn add_rdata(&mut self, rdata: RData) -> bool {
debug_assert_eq!(self.record_type, rdata.to_record_type());
let mut record = Record::with(self.name.clone(), self.record_type, self.ttl);
record.set_data(Some(rdata));
self.insert(record, 0)
}
/// Inserts a new Resource Record into the Set.
///
/// If the record is inserted, the ttl for the most recent record will be used for the ttl of
/// the entire resource record set.
///
/// This abides by the following restrictions in RFC 2136, April 1997:
///
/// ```text
/// 1.1.5. The following RR types cannot be appended to an RRset. If the
/// following comparison rules are met, then an attempt to add the new RR
/// will result in the replacement of the previous RR:
///
/// SOA compare only NAME, CLASS and TYPE -- it is not possible to
/// have more than one SOA per zone, even if any of the data
/// fields differ.
///
/// CNAME compare only NAME, CLASS, and TYPE -- it is not possible
/// to have more than one CNAME RR, even if their data fields
/// differ.
/// ```
///
/// # Arguments
///
/// * `record` - `Record` asserts that the `name` and `record_type` match the `RecordSet`.
/// * `serial` - current serial number of the `SOA` record, this is to be used for `IXFR` and
/// signing for DNSSec after updates. The serial will only be updated if the
/// record was added.
///
/// # Return value
///
/// True if the record was inserted.
///
/// TODO: make a default add without serial number for basic usage
pub fn insert(&mut self, record: Record, serial: u32) -> bool {
assert_eq!(record.name(), &self.name);
assert_eq!(record.rr_type(), self.record_type);
// RFC 2136 DNS Update April 1997
//
// 1.1.5. The following RR types cannot be appended to an RRset. If the
// following comparison rules are met, then an attempt to add the new RR
// will result in the replacement of the previous RR:
match record.rr_type() {
// SOA compare only NAME, CLASS and TYPE -- it is not possible to
// have more than one SOA per zone, even if any of the data
// fields differ.
RecordType::SOA => {
assert!(self.records.len() <= 1);
if let Some(soa_record) = self.records.get(0) {
match soa_record.data() {
Some(RData::SOA(ref existing_soa)) => {
if let Some(RData::SOA(ref new_soa)) = record.data() {
if new_soa.serial() <= existing_soa.serial() {
info!(
"update ignored serial out of data: {:?} <= {:?}",
new_soa, existing_soa
);
return false;
}
} else {
// not panicking here, b/c this is a bad record from the client or something, ignore
info!("wrong rdata for SOA update: {:?}", record.data());
return false;
}
}
rdata => {
warn!("wrong rdata: {:?}, expected SOA", rdata);
return false;
}
}
}
// if we got here, we're updating...
self.records.clear();
}
// RFC 1034/1035
// CNAME compare only NAME, CLASS, and TYPE -- it is not possible
// to have more than one CNAME RR, even if their data fields
// differ.
//
// ANAME https://tools.ietf.org/html/draft-ietf-dnsop-aname-02
// 2.2. Coexistence with other types
//
// Only one ANAME <target> can be defined per <owner>. An ANAME RRset
// MUST NOT contain more than one resource record.
//
// An ANAME's sibling address records are under the control of ANAME
// processing (see Section 5) and are not first-class records in their
// own right. They MAY exist in zone files, but they can subsequently
// be altered by ANAME processing.
//
// ANAME records MAY freely coexist at the same owner name with other RR
// types, except they MUST NOT coexist with CNAME or any other RR type
// that restricts the types with which it can itself coexist.
//
// Like other types, ANAME records can coexist with DNAME records at the
// same owner name; in fact, the two can be used cooperatively to
// redirect both the owner name address records (via ANAME) and
// everything under it (via DNAME).
RecordType::CNAME | RecordType::ANAME => {
assert!(self.records.len() <= 1);
self.records.clear();
}
_ => (),
}
// collect any records to update based on rdata
let to_replace: Vec<usize> = self
.records
.iter()
.enumerate()
.filter(|&(_, rr)| rr.data() == record.data())
.map(|(i, _)| i)
.collect::<Vec<usize>>();
// if the Records are identical, ignore the update, update all that are not (ttl, etc.)
let mut replaced = false;
for i in to_replace {
if self.records[i] == record {
return false;
}
// TODO: this shouldn't really need a clone since there should only be one...
self.records.push(record.clone());
self.records.swap_remove(i);
self.ttl = record.ttl();
self.updated(serial);
replaced = true;
}
if !replaced {
self.ttl = record.ttl();
self.updated(serial);
self.records.push(record);
true
} else {
replaced
}
}
/// Removes the Resource Record if it exists.
///
/// # Arguments
///
/// * `record` - `Record` asserts that the `name` and `record_type` match the `RecordSet`. Removes
/// any `record` if the record data, `RData`, match.
/// * `serial` - current serial number of the `SOA` record, this is to be used for `IXFR` and
/// signing for DNSSec after updates. The serial will only be updated if the
/// record was added.
///
/// # Return value
///
/// True if a record was removed.
pub fn remove(&mut self, record: &Record, serial: u32) -> bool {
assert_eq!(record.name(), &self.name);
assert!(record.rr_type() == self.record_type || record.rr_type() == RecordType::ANY);
match record.rr_type() {
// never delete the last NS record
RecordType::NS => {
if self.records.len() <= 1 {
info!("ignoring delete of last NS record: {:?}", record);
return false;
}
}
// never delete SOA
RecordType::SOA => {
info!("ignored delete of SOA");
return false;
}
_ => (), // move on to the delete
}
// remove the records, first collect all the indexes, then remove the records
let to_remove: Vec<usize> = self
.records
.iter()
.enumerate()
.filter(|&(_, rr)| rr.data() == record.data())
.map(|(i, _)| i)
.collect::<Vec<usize>>();
let mut removed = false;
for i in to_remove {
self.records.remove(i);
removed = true;
self.updated(serial);
}
removed
}
/// Consumes `RecordSet` and returns its components
pub fn into_parts(self) -> RecordSetParts {
self.into()
}
}
/// Consumes `RecordSet` giving public access to fields of `RecordSet` so they can
/// be destructured and taken by value
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RecordSetParts {
pub name: Name,
pub record_type: RecordType,
pub dns_class: DNSClass,
pub ttl: u32,
pub records: Vec<Record>,
pub rrsigs: Vec<Record>,
pub serial: u32, // serial number at which this record was modifie,
}
impl From<RecordSet> for RecordSetParts {
fn from(rset: RecordSet) -> Self {
let RecordSet {
name,
record_type,
dns_class,
ttl,
records,
rrsigs,
serial,
} = rset;
Self {
name,
record_type,
dns_class,
ttl,
records,
rrsigs,
serial,
}
}
}
impl From<Record> for RecordSet {
fn from(record: Record) -> Self {
Self {
name: record.name().clone(),
record_type: record.rr_type(),
dns_class: record.dns_class(),
ttl: record.ttl(),
records: vec![record],
rrsigs: vec![],
serial: 0,
}
}
}
/// Types which implement this can be converted into a RecordSet
#[deprecated(note = "use From/Into")]
pub trait IntoRecordSet: Sized {
/// Performs the conversion to a RecordSet
fn into_record_set(self) -> RecordSet;
}
#[allow(deprecated)]
impl IntoRecordSet for RecordSet {
fn into_record_set(self) -> Self {
self
}
}
impl IntoIterator for RecordSet {
type Item = Record;
type IntoIter = Chain<vec::IntoIter<Record>, vec::IntoIter<Record>>;
fn into_iter(self) -> Self::IntoIter {
self.records.into_iter().chain(self.rrsigs.into_iter())
}
}
/// An iterator over all the records and their signatures
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
#[derive(Debug)]
pub struct RecordsAndRrsigsIter<'r>(Chain<Iter<'r, Record>, RrsigsByAlgorithms<'r>>);
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
impl<'r> Iterator for RecordsAndRrsigsIter<'r> {
type Item = &'r Record;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
/// An iterator that limits the record signatures by SupportedAlgorithms
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
#[derive(Debug)]
pub(crate) struct RrsigsByAlgorithms<'r> {
rrsigs: Iter<'r, Record>,
supported_algorithms: SupportedAlgorithms,
}
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
impl<'r> Iterator for RrsigsByAlgorithms<'r> {
type Item = &'r Record;
fn next(&mut self) -> Option<Self::Item> {
use crate::rr::dnssec::rdata::DNSSECRData;
use crate::rr::dnssec::Algorithm;
let supported_algorithms = self.supported_algorithms;
// disable rfc 6975 when no supported_algorithms specified
if supported_algorithms.is_empty() {
self.rrsigs.next()
} else {
self.rrsigs
.by_ref()
.filter(|record| {
if let Some(RData::DNSSEC(DNSSECRData::SIG(ref rrsig))) = record.data() {
supported_algorithms.has(rrsig.algorithm())
} else {
false
}
})
.max_by_key(|record| {
if let Some(RData::DNSSEC(DNSSECRData::SIG(ref rrsig))) = record.data() {
rrsig.algorithm()
} else {
#[allow(deprecated)]
Algorithm::RSASHA1
}
})
}
}
}
/// An iterator over the RecordSet data
#[derive(Debug)]
pub enum RrsetRecords<'r> {
/// There are no records in the record set
Empty,
/// The records associated with the record set
RecordsOnly(Iter<'r, Record>),
/// The records along with their signatures in the record set
#[cfg(feature = "dnssec")]
#[cfg_attr(docsrs, doc(cfg(feature = "dnssec")))]
RecordsAndRrsigs(RecordsAndRrsigsIter<'r>),
}
impl<'r> RrsetRecords<'r> {
/// This is a best effort emptyness check
pub fn is_empty(&self) -> bool {
matches!(*self, RrsetRecords::Empty)
}
}
impl<'r> Iterator for RrsetRecords<'r> {
type Item = &'r Record;
fn next(&mut self) -> Option<Self::Item> {
match self {
RrsetRecords::Empty => None,
RrsetRecords::RecordsOnly(i) => i.next(),
#[cfg(feature = "dnssec")]
RrsetRecords::RecordsAndRrsigs(i) => i.next(),
}
}
}
#[cfg(test)]
mod test {
use std::net::Ipv4Addr;
use std::str::FromStr;
use crate::rr::rdata::SOA;
use crate::rr::*;
#[test]
fn test_insert() {
let name = Name::from_str("www.example.com.").unwrap();
let record_type = RecordType::A;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let insert = Record::new()
.set_name(name.clone())
.set_ttl(86400)
.set_rr_type(record_type)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 24))))
.clone();
assert!(rr_set.insert(insert.clone(), 0));
assert_eq!(rr_set.records_without_rrsigs().count(), 1);
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
// dups ignored
assert!(!rr_set.insert(insert.clone(), 0));
assert_eq!(rr_set.records_without_rrsigs().count(), 1);
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
// add one
let insert1 = Record::new()
.set_name(name)
.set_ttl(86400)
.set_rr_type(record_type)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 25))))
.clone();
assert!(rr_set.insert(insert1.clone(), 0));
assert_eq!(rr_set.records_without_rrsigs().count(), 2);
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert1));
}
#[test]
#[allow(clippy::unreadable_literal)]
fn test_insert_soa() {
let name = Name::from_str("example.com.").unwrap();
let record_type = RecordType::SOA;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let insert = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::SOA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::SOA(SOA::new(
Name::from_str("sns.dns.icann.org.").unwrap(),
Name::from_str("noc.dns.icann.org.").unwrap(),
2015082403,
7200,
3600,
1209600,
3600,
))))
.clone();
let same_serial = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::SOA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::SOA(SOA::new(
Name::from_str("sns.dns.icann.net.").unwrap(),
Name::from_str("noc.dns.icann.net.").unwrap(),
2015082403,
7200,
3600,
1209600,
3600,
))))
.clone();
let new_serial = Record::new()
.set_name(name)
.set_ttl(3600)
.set_rr_type(RecordType::SOA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::SOA(SOA::new(
Name::from_str("sns.dns.icann.net.").unwrap(),
Name::from_str("noc.dns.icann.net.").unwrap(),
2015082404,
7200,
3600,
1209600,
3600,
))))
.clone();
assert!(rr_set.insert(insert.clone(), 0));
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
// same serial number
assert!(!rr_set.insert(same_serial.clone(), 0));
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
assert!(!rr_set
.records_without_rrsigs()
.any(|ref x| x == &&same_serial));
assert!(rr_set.insert(new_serial.clone(), 0));
assert!(!rr_set.insert(same_serial.clone(), 0));
assert!(!rr_set.insert(insert.clone(), 0));
assert!(rr_set
.records_without_rrsigs()
.any(|ref x| x == &&new_serial));
assert!(!rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
assert!(!rr_set
.records_without_rrsigs()
.any(|ref x| x == &&same_serial));
}
#[test]
fn test_insert_cname() {
let name = Name::from_str("web.example.com.").unwrap();
let cname = Name::from_str("www.example.com.").unwrap();
let new_cname = Name::from_str("w2.example.com.").unwrap();
let record_type = RecordType::CNAME;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let insert = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::CNAME)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::CNAME(cname)))
.clone();
let new_record = Record::new()
.set_name(name)
.set_ttl(3600)
.set_rr_type(RecordType::CNAME)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::CNAME(new_cname)))
.clone();
assert!(rr_set.insert(insert.clone(), 0));
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
// update the record
assert!(rr_set.insert(new_record.clone(), 0));
assert!(!rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
assert!(rr_set
.records_without_rrsigs()
.any(|ref x| x == &&new_record));
}
#[test]
fn test_remove() {
let name = Name::from_str("www.example.com.").unwrap();
let record_type = RecordType::A;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let insert = Record::new()
.set_name(name.clone())
.set_ttl(86400)
.set_rr_type(record_type)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 24))))
.clone();
let insert1 = Record::new()
.set_name(name)
.set_ttl(86400)
.set_rr_type(record_type)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 25))))
.clone();
assert!(rr_set.insert(insert.clone(), 0));
assert!(rr_set.insert(insert1.clone(), 0));
assert!(rr_set.remove(&insert, 0));
assert!(!rr_set.remove(&insert, 0));
assert!(rr_set.remove(&insert1, 0));
assert!(!rr_set.remove(&insert1, 0));
}
#[test]
#[allow(clippy::unreadable_literal)]
fn test_remove_soa() {
let name = Name::from_str("www.example.com.").unwrap();
let record_type = RecordType::SOA;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let insert = Record::new()
.set_name(name)
.set_ttl(3600)
.set_rr_type(RecordType::SOA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::SOA(SOA::new(
Name::from_str("sns.dns.icann.org.").unwrap(),
Name::from_str("noc.dns.icann.org.").unwrap(),
2015082403,
7200,
3600,
1209600,
3600,
))))
.clone();
assert!(rr_set.insert(insert.clone(), 0));
assert!(!rr_set.remove(&insert, 0));
assert!(rr_set.records_without_rrsigs().any(|ref x| x == &&insert));
}
#[test]
fn test_remove_ns() {
let name = Name::from_str("example.com.").unwrap();
let record_type = RecordType::NS;
let mut rr_set = RecordSet::new(&name, record_type, 0);
let ns1 = Record::new()
.set_name(name.clone())
.set_ttl(86400)
.set_rr_type(RecordType::NS)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::NS(
Name::from_str("a.iana-servers.net.").unwrap(),
)))
.clone();
let ns2 = Record::new()
.set_name(name)
.set_ttl(86400)
.set_rr_type(RecordType::NS)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::NS(
Name::from_str("b.iana-servers.net.").unwrap(),
)))
.clone();
assert!(rr_set.insert(ns1.clone(), 0));
assert!(rr_set.insert(ns2.clone(), 0));
// ok to remove one, but not two...
assert!(rr_set.remove(&ns1, 0));
assert!(!rr_set.remove(&ns2, 0));
// check that we can swap which ones are removed
assert!(rr_set.insert(ns1.clone(), 0));
assert!(rr_set.remove(&ns2, 0));
assert!(!rr_set.remove(&ns1, 0));
}
#[test]
#[cfg(feature = "dnssec")] // This tests RFC 6975, a DNSSEC-specific feature.
#[allow(clippy::blocks_in_if_conditions)]
fn test_get_filter() {
use crate::rr::dnssec::rdata::DNSSECRData;
use crate::rr::dnssec::rdata::SIG;
use crate::rr::dnssec::{Algorithm, SupportedAlgorithms};
let name = Name::root();
let rsasha256 = SIG::new(
RecordType::A,
Algorithm::RSASHA256,
0,
0,
0,
0,
0,
Name::root(),
vec![],
);
let ecp256 = SIG::new(
RecordType::A,
Algorithm::ECDSAP256SHA256,
0,
0,
0,
0,
0,
Name::root(),
vec![],
);
let ecp384 = SIG::new(
RecordType::A,
Algorithm::ECDSAP384SHA384,
0,
0,
0,
0,
0,
Name::root(),
vec![],
);
let ed25519 = SIG::new(
RecordType::A,
Algorithm::ED25519,
0,
0,
0,
0,
0,
Name::root(),
vec![],
);
let rrsig_rsa = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::RRSIG)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::DNSSEC(DNSSECRData::SIG(rsasha256))))
.clone();
let rrsig_ecp256 = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::RRSIG)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::DNSSEC(DNSSECRData::SIG(ecp256))))
.clone();
let rrsig_ecp384 = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::RRSIG)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::DNSSEC(DNSSECRData::SIG(ecp384))))
.clone();
let rrsig_ed25519 = Record::new()
.set_name(name.clone())
.set_ttl(3600)
.set_rr_type(RecordType::RRSIG)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::DNSSEC(DNSSECRData::SIG(ed25519))))
.clone();
let a = Record::new()
.set_name(name)
.set_ttl(3600)
.set_rr_type(RecordType::A)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 24))))
.clone();
let mut rrset = RecordSet::from(a);
rrset.insert_rrsig(rrsig_rsa);
rrset.insert_rrsig(rrsig_ecp256);
rrset.insert_rrsig(rrsig_ecp384);
rrset.insert_rrsig(rrsig_ed25519);
assert!(rrset
.records_with_rrsigs(SupportedAlgorithms::all(),)
.any(
|r| if let Some(RData::DNSSEC(DNSSECRData::SIG(ref sig))) = r.data() {
sig.algorithm() == Algorithm::ED25519
} else {
false
},
));
let mut supported_algorithms = SupportedAlgorithms::new();
supported_algorithms.set(Algorithm::ECDSAP384SHA384);
assert!(rrset.records_with_rrsigs(supported_algorithms).any(|r| {
if let Some(RData::DNSSEC(DNSSECRData::SIG(ref sig))) = r.data() {
sig.algorithm() == Algorithm::ECDSAP384SHA384
} else {
false
}
}));
let mut supported_algorithms = SupportedAlgorithms::new();
supported_algorithms.set(Algorithm::ED25519);
assert!(rrset.records_with_rrsigs(supported_algorithms).any(|r| {
if let Some(RData::DNSSEC(DNSSECRData::SIG(ref sig))) = r.data() {
sig.algorithm() == Algorithm::ED25519
} else {
false
}
}));
}
}