1// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
78use std::net::SocketAddr;
910use crate::error::ProtoResult;
11use crate::op::Message;
1213/// A DNS message in serialized form, with either the target address or source address
14pub struct SerialMessage {
15// TODO: change to Bytes? this would be more compatible with some underlying libraries
16message: Vec<u8>,
17 addr: SocketAddr,
18}
1920impl SerialMessage {
21/// Construct a new SerialMessage and the source or destination address
22pub fn new(message: Vec<u8>, addr: SocketAddr) -> Self {
23Self { message, addr }
24 }
2526/// Get a reference to the bytes
27pub fn bytes(&self) -> &[u8] {
28&self.message
29 }
3031/// Get the source or destination address (context dependent)
32pub fn addr(&self) -> SocketAddr {
33self.addr
34 }
3536/// Unwrap the Bytes and address
37pub fn into_parts(self) -> (Vec<u8>, SocketAddr) {
38self.into()
39 }
4041/// Build a `SerialMessage` from some bytes and an address
42pub fn from_parts(message: Vec<u8>, addr: SocketAddr) -> Self {
43 (message, addr).into()
44 }
4546/// Deserializes the inner data into a Message
47pub fn to_message(&self) -> ProtoResult<Message> {
48 Message::from_vec(&self.message)
49 }
50}
5152impl From<(Vec<u8>, SocketAddr)> for SerialMessage {
53fn from((message, addr): (Vec<u8>, SocketAddr)) -> Self {
54Self { message, addr }
55 }
56}
5758impl From<SerialMessage> for (Vec<u8>, SocketAddr) {
59fn from(msg: SerialMessage) -> Self {
60let SerialMessage { message, addr } = msg;
61 (message, addr)
62 }
63}