trust_dns_proto/serialize/binary/
mod.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//! Binary serialization types
18
19mod decoder;
20mod encoder;
21mod restrict;
22
23use std::net::{Ipv4Addr, Ipv6Addr};
24
25pub use self::decoder::{BinDecoder, DecodeError};
26pub use self::encoder::BinEncoder;
27pub use self::encoder::EncodeMode;
28pub use self::restrict::{Restrict, RestrictedMath, Verified};
29
30#[cfg(test)]
31pub mod bin_tests;
32
33use crate::error::*;
34
35/// A type which can be encoded into a DNS binary format
36pub trait BinEncodable {
37    /// Write the type to the stream
38    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()>;
39
40    /// Returns the object in binary form
41    fn to_bytes(&self) -> ProtoResult<Vec<u8>> {
42        let mut bytes = Vec::<u8>::new();
43        {
44            let mut encoder = BinEncoder::new(&mut bytes);
45            self.emit(&mut encoder)?;
46        }
47
48        Ok(bytes)
49    }
50}
51
52/// A trait for types which are serializable to and from DNS binary formats
53pub trait BinDecodable<'r>: Sized {
54    /// Read the type from the stream
55    fn read(decoder: &mut BinDecoder<'r>) -> ProtoResult<Self>;
56
57    /// Returns the object in binary form
58    fn from_bytes(bytes: &'r [u8]) -> ProtoResult<Self> {
59        let mut decoder = BinDecoder::new(bytes);
60        Self::read(&mut decoder)
61    }
62}
63
64impl BinEncodable for u16 {
65    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
66        encoder.emit_u16(*self)
67    }
68}
69
70impl<'r> BinDecodable<'r> for u16 {
71    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
72        decoder
73            .read_u16()
74            .map(Restrict::unverified)
75            .map_err(Into::into)
76    }
77}
78
79impl BinEncodable for i32 {
80    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
81        encoder.emit_i32(*self)
82    }
83}
84
85impl<'r> BinDecodable<'r> for i32 {
86    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
87        decoder
88            .read_i32()
89            .map(Restrict::unverified)
90            .map_err(Into::into)
91    }
92}
93
94impl BinEncodable for u32 {
95    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
96        encoder.emit_u32(*self)
97    }
98}
99
100impl<'r> BinDecodable<'r> for u32 {
101    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
102        decoder
103            .read_u32()
104            .map(Restrict::unverified)
105            .map_err(Into::into)
106    }
107}
108
109impl BinEncodable for Vec<u8> {
110    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
111        encoder.emit_vec(self)
112    }
113}
114
115impl BinEncodable for Ipv4Addr {
116    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
117        crate::rr::rdata::a::emit(encoder, *self)
118    }
119}
120
121impl<'r> BinDecodable<'r> for Ipv4Addr {
122    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
123        crate::rr::rdata::a::read(decoder)
124    }
125}
126
127impl BinEncodable for Ipv6Addr {
128    fn emit(&self, encoder: &mut BinEncoder<'_>) -> ProtoResult<()> {
129        crate::rr::rdata::aaaa::emit(encoder, self)
130    }
131}
132
133impl<'r> BinDecodable<'r> for Ipv6Addr {
134    fn read(decoder: &mut BinDecoder<'_>) -> ProtoResult<Self> {
135        crate::rr::rdata::aaaa::read(decoder)
136    }
137}