From: Ian Jackson Date: Sun, 1 Aug 2021 19:46:43 +0000 (+0100) Subject: wip get address X-Git-Tag: hippotat/1.0.0~390 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=b9319fb1dd3d6870a8855909b4f20741e3ef5ba4;p=hippotat.git wip get address Signed-off-by: Ian Jackson --- diff --git a/src/prelude.rs b/src/prelude.rs index 244d776..8375744 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -4,7 +4,7 @@ pub use std::array; pub use std::collections::{BTreeSet, HashMap}; -pub use std::convert::TryInto; +pub use std::convert::{TryFrom, TryInto}; pub use std::borrow::Cow; pub use std::cmp::{min, max}; pub use std::fs; @@ -13,7 +13,7 @@ pub use std::future::Future; pub use std::io::{self, ErrorKind, Read as _}; pub use std::iter; pub use std::mem; -pub use std::net::{IpAddr, Ipv4Addr}; +pub use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; pub use std::path::{Path, PathBuf}; pub use std::panic; pub use std::process; diff --git a/src/slip.rs b/src/slip.rs index 817357b..763f24f 100644 --- a/src/slip.rs +++ b/src/slip.rs @@ -72,6 +72,22 @@ impl From for FramesData { fn from(frames: Frames) -> FramesData { frames.frames } } +pub fn ip_packet_addrs(packet: &[u8]) -> Option<(IpAddr, IpAddr)> { + Some(match packet.get(0)? & 0xf0 { + 4 if packet.len() >= 20 => ( + Ipv4Addr::from(*<&[u8;4]>::try_from(&packet[12..16]).unwrap()).into(), + Ipv4Addr::from(*<&[u8;4]>::try_from(&packet[16..20]).unwrap()).into(), + ), + + 6 if packet.len() >= 40 => ( + Ipv6Addr::from(*<&[u8;16]>::try_from(&packet[ 8..24]).unwrap()).into(), + Ipv6Addr::from(*<&[u8;16]>::try_from(&packet[24..40]).unwrap()).into(), + ), + + _ => None?, + }) +} + #[derive(Copy,Clone,Eq,PartialEq,Ord,PartialOrd,Hash)] pub struct DumpHex<'b>(pub &'b [u8]); impl Debug for DumpHex<'_> {