1use std::net::Ipv6Addr;
36
37use crate::error::*;
38use crate::serialize::binary::*;
39
40#[allow(clippy::many_single_char_names)]
42pub fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Ipv6Addr> {
43 let a: u16 = decoder.read_u16()?.unverified();
44 let b: u16 = decoder.read_u16()?.unverified();
45 let c: u16 = decoder.read_u16()?.unverified();
46 let d: u16 = decoder.read_u16()?.unverified();
47 let e: u16 = decoder.read_u16()?.unverified();
48 let f: u16 = decoder.read_u16()?.unverified();
49 let g: u16 = decoder.read_u16()?.unverified();
50 let h: u16 = decoder.read_u16()?.unverified();
51
52 Ok(Ipv6Addr::new(a, b, c, d, e, f, g, h))
53}
54
55pub 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 ), (
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}