diff options
| author | luwenpeng <[email protected]> | 2023-08-14 11:03:42 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2023-08-14 11:03:42 +0800 |
| commit | 53e184805feabca2724841dcf45f356d427ea6e6 (patch) | |
| tree | a81915365369350eb007ff3a0f645cbc84bdc4d3 | |
| parent | 85973cd021fda0d62c5f3e786e2abb8c8296a1df (diff) | |
[feature] Add Decode trait
| -rw-r--r-- | src/packet/packet.rs | 1 | ||||
| -rw-r--r-- | src/protocol/codec.rs | 10 | ||||
| -rw-r--r-- | src/protocol/dns.rs | 52 | ||||
| -rw-r--r-- | src/protocol/ethernet.rs | 6 | ||||
| -rw-r--r-- | src/protocol/http.rs | 6 | ||||
| -rw-r--r-- | src/protocol/ip.rs | 5 | ||||
| -rw-r--r-- | src/protocol/ipv4.rs | 6 | ||||
| -rw-r--r-- | src/protocol/ipv6.rs | 6 | ||||
| -rw-r--r-- | src/protocol/mod.rs | 1 | ||||
| -rw-r--r-- | src/protocol/tcp.rs | 6 | ||||
| -rw-r--r-- | src/protocol/udp.rs | 6 |
11 files changed, 46 insertions, 59 deletions
diff --git a/src/packet/packet.rs b/src/packet/packet.rs index 8980049..5ca75c9 100644 --- a/src/packet/packet.rs +++ b/src/packet/packet.rs @@ -1,4 +1,5 @@ use crate::packet::error::PacketError; +use crate::protocol::codec::Decode; use crate::protocol::dns::DNS_MESSAGE; use crate::protocol::ethernet::EtherType; use crate::protocol::ethernet::EthernetFrame; diff --git a/src/protocol/codec.rs b/src/protocol/codec.rs new file mode 100644 index 0000000..ce3d0a4 --- /dev/null +++ b/src/protocol/codec.rs @@ -0,0 +1,10 @@ +use nom::IResult; +use std::fmt::Debug; + +pub trait Decode<T: Debug> { + fn decode(input: &[u8]) -> IResult<&[u8], T>; +} + +pub trait Encode<T: Debug> { + // TODO +} diff --git a/src/protocol/dns.rs b/src/protocol/dns.rs index 27dde8a..6612320 100644 --- a/src/protocol/dns.rs +++ b/src/protocol/dns.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::bits; use nom::character; use nom::combinator; @@ -1366,8 +1367,10 @@ impl DNS_MESSAGE { ar: vec![], } } +} - pub fn decode(input: &[u8]) -> IResult<&[u8], DNS_MESSAGE> { +impl Decode<DNS_MESSAGE> for DNS_MESSAGE { + fn decode(input: &[u8]) -> IResult<&[u8], DNS_MESSAGE> { let (mut input, header) = DNS_HEADER::decode(input)?; let mut message = DNS_MESSAGE::new(header); let mut position = 12; @@ -1573,52 +1576,6 @@ fn dname_decode<'a, 'b>( * TEST ******************************************************************************/ -/* -fn main() -> io::Result<()> { - // dig @1.1.1.1 A www.kpn.com - let id = 52749u16; - let query_flags = 0b0_0000_0010_000_0000u16; - let counts = [0u8, 1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]; - // let qname = [0b00000011u8, b'w', b'w', b'w', 0b00000011u8, b'k', b'p', b'n', 0b00000011u8, b'c', b'o', b'm', 0b00000000u8]; - let qname = [0b00000011u8, b'k', b'p', b'n', 0b00000011u8, b'c', b'o', b'm', 0b00000000u8]; - // let qname = [0b00001010u8, b'q', b'u', b'a', b't', b'e', b'r', b'n', b'i', b'o', b'n', 0b00000101u8, b's', b'p', b'a', b'c', b'e', 0b00000000u8]; - let qtype = 48u16; - let qclass = 1u16; - let mut query_msg = vec![]; - query_msg.extend_from_slice(&id.to_be_bytes()); - query_msg.extend_from_slice(&query_flags.to_be_bytes()); - query_msg.extend_from_slice(&counts); - query_msg.extend_from_slice(&qname); - query_msg.extend_from_slice(&qtype.to_be_bytes()); - query_msg.extend_from_slice(&qclass.to_be_bytes()); - let socket = UdpSocket::bind("0.0.0.0:34254").expect("Failed to open UDP connection."); - socket.connect(("1.1.1.1", 53)).expect("Failed to connect to server."); - socket.send(&query_msg).expect("Failed to send query to server."); - let mut buf = [0; 512]; - let input = match socket.recv(&mut buf) { - Ok(received) => { - // println!("received {} bytes {:?}\n", received, &buf[..received]); - &buf[..received] - }, - Err(_e) => { - // println!("recv function failed: {:?}\n", e); - return Ok(()); - }, - }; - match DNS_MESSAGE::decode(input) { - Ok((_rest, message)) => { - println!("{}", message); - Ok(()) - }, - Err(err) => { - println!("err:{:?}\n", err); - Ok(()) - }, - - } -} -*/ - #[cfg(test)] mod tests { use super::character_string_decode; @@ -1636,6 +1593,7 @@ mod tests { use super::DNS_RR_DATA; use super::DNS_RR_HDR; use super::DNS_RR_SECTION; + use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[]; diff --git a/src/protocol/ethernet.rs b/src/protocol/ethernet.rs index 6b77a7d..b2888cc 100644 --- a/src/protocol/ethernet.rs +++ b/src/protocol/ethernet.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::number; use nom::IResult; @@ -140,8 +141,8 @@ impl MacAddress { } } -impl EthernetFrame { - pub fn decode(input: &[u8]) -> IResult<&[u8], EthernetFrame> { +impl Decode<EthernetFrame> for EthernetFrame { + fn decode(input: &[u8]) -> IResult<&[u8], EthernetFrame> { let (input, dest_mac) = MacAddress::decode(input)?; let (input, source_mac) = MacAddress::decode(input)?; let (input, ether_type) = EtherType::decode(input)?; @@ -164,6 +165,7 @@ impl EthernetFrame { #[cfg(test)] mod tests { use super::{EtherType, EthernetFrame, MacAddress}; + use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; #[test] diff --git a/src/protocol/http.rs b/src/protocol/http.rs index de6476f..b7abccc 100644 --- a/src/protocol/http.rs +++ b/src/protocol/http.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::IResult; #[allow(non_camel_case_types)] @@ -12,7 +13,10 @@ impl HTTP_MESSAGE { // TODO } } - pub fn decode(input: &[u8]) -> IResult<&[u8], HTTP_MESSAGE> { +} + +impl Decode<HTTP_MESSAGE> for HTTP_MESSAGE { + fn decode(input: &[u8]) -> IResult<&[u8], HTTP_MESSAGE> { let message = HTTP_MESSAGE::new(); // TODO Ok((input, message)) diff --git a/src/protocol/ip.rs b/src/protocol/ip.rs index 3a2f6bd..1b92494 100644 --- a/src/protocol/ip.rs +++ b/src/protocol/ip.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::number; use nom::IResult; @@ -62,8 +63,8 @@ impl From<u8> for IPProtocol { } } -impl IPProtocol { - pub fn decode(input: &[u8]) -> IResult<&[u8], IPProtocol> { +impl Decode<IPProtocol> for IPProtocol { + fn decode(input: &[u8]) -> IResult<&[u8], IPProtocol> { let (input, protocol) = number::streaming::be_u8(input)?; Ok((input, protocol.into())) diff --git a/src/protocol/ipv4.rs b/src/protocol/ipv4.rs index 4f07926..60ec01a 100644 --- a/src/protocol/ipv4.rs +++ b/src/protocol/ipv4.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use nom::bits; use nom::error::Error; @@ -70,8 +71,8 @@ fn address_v4_decode(input: &[u8]) -> IResult<&[u8], Ipv4Addr> { Ok((input, Ipv4Addr::from(<[u8; 4]>::try_from(ipv4).unwrap()))) } -impl IPv4Header { - pub fn decode(input: &[u8]) -> IResult<&[u8], IPv4Header> { +impl Decode<IPv4Header> for IPv4Header { + fn decode(input: &[u8]) -> IResult<&[u8], IPv4Header> { let (input, verihl) = version_hlen_decode(input)?; let (input, tos) = number::streaming::be_u8(input)?; let (input, length) = number::streaming::be_u16(input)?; @@ -112,6 +113,7 @@ impl IPv4Header { #[cfg(test)] mod tests { use super::IPv4Header; + use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use std::net::Ipv4Addr; diff --git a/src/protocol/ipv6.rs b/src/protocol/ipv6.rs index f21c4da..c334ea2 100644 --- a/src/protocol/ipv6.rs +++ b/src/protocol/ipv6.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use nom::bits; use nom::bytes; @@ -70,8 +71,8 @@ fn address_v6_decode(input: &[u8]) -> IResult<&[u8], Ipv6Addr> { Ok((input, Ipv6Addr::from(<[u8; 16]>::try_from(ipv6).unwrap()))) } -impl IPv6Header { - pub fn decode(input: &[u8]) -> IResult<&[u8], IPv6Header> { +impl Decode<IPv6Header> for IPv6Header { + fn decode(input: &[u8]) -> IResult<&[u8], IPv6Header> { let (input, ver_tc) = half_byte_decode(input)?; let (input, tc_fl) = half_byte_decode(input)?; let (input, fl): (_, u32) = @@ -108,6 +109,7 @@ impl IPv6Header { #[cfg(test)] mod tests { use super::IPv6Header; + use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use std::net::Ipv6Addr; diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index 31046cb..7d191ce 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -1,3 +1,4 @@ +pub mod codec; pub mod ethernet; pub mod ip; pub mod ipv4; diff --git a/src/protocol/tcp.rs b/src/protocol/tcp.rs index ea424b9..aaedada 100644 --- a/src/protocol/tcp.rs +++ b/src/protocol/tcp.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::bits; use nom::error::Error; use nom::error::ErrorKind; @@ -226,8 +227,10 @@ impl TcpHeader { Ok((left, options)) } +} - pub fn decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { +impl Decode<TcpHeader> for TcpHeader { + fn decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { match TcpHeader::fixed_header_decode(input) { Ok((left, mut header)) => { if header.data_offset > 20 { @@ -262,6 +265,7 @@ impl TcpHeader { #[cfg(test)] mod tests { use super::*; + use crate::protocol::codec::Decode; use crate::protocol::tcp::TcpOption::MSS; use crate::protocol::tcp::TcpOption::NOP; use crate::protocol::tcp::TcpOption::SACK; diff --git a/src/protocol/udp.rs b/src/protocol/udp.rs index c5fab65..d5302eb 100644 --- a/src/protocol/udp.rs +++ b/src/protocol/udp.rs @@ -1,3 +1,4 @@ +use crate::protocol::codec::Decode; use nom::number; use nom::IResult; @@ -31,8 +32,8 @@ pub struct UdpHeader { * API ******************************************************************************/ -impl UdpHeader { - pub fn decode(input: &[u8]) -> IResult<&[u8], UdpHeader> { +impl Decode<UdpHeader> for UdpHeader { + fn decode(input: &[u8]) -> IResult<&[u8], UdpHeader> { let (input, source_port) = number::streaming::be_u16(input)?; let (input, dest_port) = number::streaming::be_u16(input)?; let (input, length) = number::streaming::be_u16(input)?; @@ -57,6 +58,7 @@ impl UdpHeader { #[cfg(test)] mod tests { use super::UdpHeader; + use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; |
