trust_dns_proto/rr/rdata/name.rs
1/*
2 * Copyright (C) 2015 Benjamin Fry <benjaminfry@me.com>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Record type for all cname like records.
18//!
19//! A generic struct for all {*}NAME pointer RData records, CNAME, NS, and PTR. Here is the text for
20//! CNAME from RFC 1035, Domain Implementation and Specification, November 1987:
21//!
22//! [RFC 1035, DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION, November 1987](https://tools.ietf.org/html/rfc1035)
23//!
24//! ```text
25//! 3.3.1. CNAME RDATA format
26//!
27//! +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
28//! / CNAME /
29//! / /
30//! +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
31//!
32//! where:
33//!
34//! CNAME A <domain-name> which specifies the canonical or primary
35//! name for the owner. The owner name is an alias.
36//!
37//! CNAME RRs cause no additional section processing, but name servers may
38//! choose to restart the query at the canonical name in certain cases. See
39//! the description of name server logic in [RFC-1034] for details.
40//! ```
41
42use crate::error::*;
43use crate::rr::domain::Name;
44use crate::serialize::binary::*;
45
46/// Read the RData from the given Decoder
47pub fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Name> {
48 Name::read(decoder)
49}
50
51/// [RFC 4034](https://tools.ietf.org/html/rfc4034#section-6), DNSSEC Resource Records, March 2005
52///
53/// This is accurate for all currently known name records.
54///
55/// ```text
56/// 6.2. Canonical RR Form
57///
58/// For the purposes of DNS security, the canonical form of an RR is the
59/// wire format of the RR where:
60///
61/// ...
62///
63/// 3. if the type of the RR is NS, MD, MF, CNAME, SOA, MB, MG, MR, PTR,
64/// HINFO, MINFO, MX, HINFO, RP, AFSDB, RT, SIG, PX, NXT, NAPTR, KX,
65/// SRV, DNAME, A6, RRSIG, or (rfc6840 removes NSEC), all uppercase
66/// US-ASCII letters in the DNS names contained within the RDATA are replaced
67/// by the corresponding lowercase US-ASCII letters;
68/// ```
69pub fn emit(encoder: &mut BinEncoder<'_>, name_data: &Name) -> ProtoResult<()> {
70 let is_canonical_names = encoder.is_canonical_names();
71 name_data.emit_with_lowercase(encoder, is_canonical_names)?;
72 Ok(())
73}
74
75#[test]
76pub fn test() {
77 #![allow(clippy::dbg_macro, clippy::print_stdout)]
78
79 let rdata = Name::from_ascii("WWW.example.com.").unwrap();
80
81 let mut bytes = Vec::new();
82 let mut encoder: BinEncoder<'_> = BinEncoder::new(&mut bytes);
83 assert!(emit(&mut encoder, &rdata).is_ok());
84 let bytes = encoder.into_bytes();
85
86 println!("bytes: {:?}", bytes);
87
88 let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
89 let read_rdata = read(&mut decoder).expect("Decoding error");
90 assert_eq!(rdata, read_rdata);
91}