trust_dns_proto/rr/rdata/null.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//! null record type, generally not used except as an internal tool for representing null data
18use std::fmt;
19
20#[cfg(feature = "serde-config")]
21use serde::{Deserialize, Serialize};
22
23use crate::error::*;
24use crate::serialize::binary::*;
25
26/// [RFC 1035, DOMAIN NAMES - IMPLEMENTATION AND SPECIFICATION, November 1987](https://tools.ietf.org/html/rfc1035)
27///
28/// ```text
29/// 3.3.10. NULL RDATA format (EXPERIMENTAL)
30///
31/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
32/// / <anything> /
33/// / /
34/// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
35///
36/// Anything at all may be in the RDATA field so long as it is 65535 octets
37/// or less.
38///
39/// NULL records cause no additional section processing. NULL RRs are not
40/// allowed in Zone Files. NULLs are used as placeholders in some
41/// experimental extensions of the DNS.
42/// ```
43#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
44#[derive(Default, Debug, PartialEq, Eq, Hash, Clone)]
45pub struct NULL {
46 anything: Vec<u8>,
47}
48
49impl NULL {
50 /// Construct a new NULL RData
51 pub const fn new() -> Self {
52 Self {
53 anything: Vec::new(),
54 }
55 }
56
57 /// Constructs a new NULL RData with the associated data
58 pub fn with(anything: Vec<u8>) -> Self {
59 // FIXME: we don't want empty data for NULL's, should be Option in the Record
60 debug_assert!(!anything.is_empty());
61
62 Self { anything }
63 }
64
65 /// Returns the buffer stored in the NULL
66 pub fn anything(&self) -> &[u8] {
67 &self.anything
68 }
69}
70
71/// Read the RData from the given Decoder
72pub fn read(decoder: &mut BinDecoder<'_>, rdata_length: Restrict<u16>) -> ProtoResult<NULL> {
73 let rdata_length = rdata_length.map(|u| u as usize).unverified(/*any u16 is valid*/);
74 if rdata_length > 0 {
75 let anything = decoder.read_vec(rdata_length)?.unverified(/*any byte array is good*/);
76 Ok(NULL::with(anything))
77 } else {
78 Ok(NULL::new())
79 }
80}
81
82/// Write the RData from the given Decoder
83pub fn emit(encoder: &mut BinEncoder<'_>, nil: &NULL) -> ProtoResult<()> {
84 for b in nil.anything() {
85 encoder.emit(*b)?;
86 }
87
88 Ok(())
89}
90
91impl fmt::Display for NULL {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
93 f.write_str(&data_encoding::BASE64.encode(&self.anything))
94 }
95}
96
97#[cfg(test)]
98mod tests {
99 #![allow(clippy::dbg_macro, clippy::print_stdout)]
100
101 use super::*;
102
103 #[test]
104 fn test() {
105 let rdata = NULL::with(vec![0, 1, 2, 3, 4, 5, 6, 7]);
106
107 let mut bytes = Vec::new();
108 let mut encoder: BinEncoder<'_> = BinEncoder::new(&mut bytes);
109 assert!(emit(&mut encoder, &rdata).is_ok());
110 let bytes = encoder.into_bytes();
111
112 println!("bytes: {:?}", bytes);
113
114 let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
115 let restrict = Restrict::new(bytes.len() as u16);
116 let read_rdata = read(&mut decoder, restrict).expect("Decoding error");
117 assert_eq!(rdata, read_rdata);
118 }
119}