trust_dns_proto/rr/rdata/
aaaa.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//! IPv6 address record data
18//!
19//! [RFC 3596, DNS Extensions to Support IPv6, October 2003](https://tools.ietf.org/html/rfc3596)
20//!
21//! ```text
22//! 2.1 AAAA record type
23//!
24//!   The AAAA resource record type is a record specific to the Internet
25//!   class that stores a single IPv6 address.
26//!
27//!   The IANA assigned value of the type is 28 (decimal).
28//!
29//! 2.2 AAAA data format
30//!
31//!   A 128 bit IPv6 address is encoded in the data portion of an AAAA
32//!   resource record in network byte order (high-order byte first).
33//! ```
34
35use std::net::Ipv6Addr;
36
37use crate::error::*;
38use crate::serialize::binary::*;
39
40/// Read the RData from the given Decoder
41#[allow(clippy::many_single_char_names)]
42pub fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Ipv6Addr> {
43    let a: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
44    let b: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
45    let c: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
46    let d: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
47    let e: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
48    let f: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
49    let g: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
50    let h: u16 = decoder.read_u16()?.unverified(/*valid as any u16*/);
51
52    Ok(Ipv6Addr::new(a, b, c, d, e, f, g, h))
53}
54
55/// Write the RData from the given Decoder
56pub fn emit(encoder: &mut BinEncoder<'_>, address: &Ipv6Addr) -> ProtoResult<()> {
57    let segments = address.segments();
58
59    encoder.emit_u16(segments[0])?;
60    encoder.emit_u16(segments[1])?;
61    encoder.emit_u16(segments[2])?;
62    encoder.emit_u16(segments[3])?;
63    encoder.emit_u16(segments[4])?;
64    encoder.emit_u16(segments[5])?;
65    encoder.emit_u16(segments[6])?;
66    encoder.emit_u16(segments[7])?;
67    Ok(())
68}
69
70#[cfg(test)]
71mod tests {
72    use std::net::Ipv6Addr;
73    use std::str::FromStr;
74
75    use super::*;
76    use crate::serialize::binary::bin_tests::{test_emit_data_set, test_read_data_set};
77
78    fn get_data() -> Vec<(Ipv6Addr, Vec<u8>)> {
79        vec![
80            (
81                Ipv6Addr::from_str("::").unwrap(),
82                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
83            ), // base case
84            (
85                Ipv6Addr::from_str("1::").unwrap(),
86                vec![0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
87            ),
88            (
89                Ipv6Addr::from_str("0:1::").unwrap(),
90                vec![0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
91            ),
92            (
93                Ipv6Addr::from_str("0:0:1::").unwrap(),
94                vec![0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
95            ),
96            (
97                Ipv6Addr::from_str("0:0:0:1::").unwrap(),
98                vec![0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
99            ),
100            (
101                Ipv6Addr::from_str("::1:0:0:0").unwrap(),
102                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
103            ),
104            (
105                Ipv6Addr::from_str("::1:0:0").unwrap(),
106                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
107            ),
108            (
109                Ipv6Addr::from_str("::1:0").unwrap(),
110                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
111            ),
112            (
113                Ipv6Addr::from_str("::1").unwrap(),
114                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
115            ),
116            (
117                Ipv6Addr::from_str("::127.0.0.1").unwrap(),
118                vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 127, 0, 0, 1],
119            ),
120            (
121                Ipv6Addr::from_str("FF00::192.168.64.32").unwrap(),
122                vec![255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 168, 64, 32],
123            ),
124        ]
125    }
126
127    #[test]
128    fn test_read() {
129        test_read_data_set(get_data(), |ref mut d| read(d));
130    }
131
132    #[test]
133    fn test_emit() {
134        test_emit_data_set(get_data(), |e, d| emit(e, &d));
135    }
136}