// Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use std::{net::SocketAddr, ops::Deref}; use crate::{hex_with_len, IpTos}; #[derive(Clone, PartialEq, Eq)] pub struct Datagram { src: SocketAddr, dst: SocketAddr, tos: IpTos, ttl: Option, d: Vec, } impl Datagram { pub fn new>>( src: SocketAddr, dst: SocketAddr, tos: IpTos, ttl: Option, d: V, ) -> Self { Self { src, dst, tos, ttl, d: d.into(), } } #[must_use] pub fn source(&self) -> SocketAddr { self.src } #[must_use] pub fn destination(&self) -> SocketAddr { self.dst } #[must_use] pub fn tos(&self) -> IpTos { self.tos } #[must_use] pub fn ttl(&self) -> Option { self.ttl } #[cfg(feature = "udp")] #[must_use] pub(crate) fn into_data(self) -> Vec { self.d } } impl Deref for Datagram { type Target = Vec; #[must_use] fn deref(&self) -> &Self::Target { &self.d } } impl std::fmt::Debug for Datagram { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!( f, "Datagram {:?} TTL {:?} {:?}->{:?}: {}", self.tos, self.ttl, self.src, self.dst, hex_with_len(&self.d) ) } } #[cfg(test)] use test_fixture::datagram; #[test] fn fmt_datagram() { let d = datagram([0; 1].to_vec()); assert_eq!( format!("{d:?}"), "Datagram IpTos(Cs0, NotEct) TTL Some(128) [fe80::1]:443->[fe80::1]:443: [1]: 00" .to_string() ); }