From a01e8ffad311862e2ab3af61560dc74e42ae337d Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 22 Sep 2023 15:30:04 +0800 Subject: [refactor] Packet Decode: Protocol names are named in capital letters --- src/main.rs | 34 +- src/packet/packet.rs | 666 +++++++-------- src/plugin/example.rs | 2 +- src/protocol/dns.rs | 2014 +++++++++++++++++++--------------------------- src/protocol/ethernet.rs | 36 +- src/protocol/grev0.rs | 36 +- src/protocol/grev1.rs | 28 +- src/protocol/gtpv1.rs | 46 +- src/protocol/http.rs | 16 +- src/protocol/icmp.rs | 62 +- src/protocol/icmpv6.rs | 106 +-- src/protocol/ipv4.rs | 18 +- src/protocol/ipv6.rs | 58 +- src/protocol/l2tp.rs | 296 +++---- src/protocol/mpls.rs | 32 +- src/protocol/ppp.rs | 54 +- src/protocol/pptp.rs | 176 ++-- src/protocol/tcp.rs | 98 +-- src/protocol/udp.rs | 18 +- src/protocol/vlan.rs | 30 +- 20 files changed, 1743 insertions(+), 2083 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c02490..012db40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,52 +21,52 @@ fn trigger_packet_event( let num = packet.encapsulation.len(); for i in 0..num { match packet.encapsulation[i] { - Encapsulation::Eth(_, _) => { + Encapsulation::ETH(_, _) => { // TODO } - Encapsulation::Vlan(_, _) => { + Encapsulation::VLAN(_, _) => { // TODO } - Encapsulation::Mpls(_, _) => { + Encapsulation::MPLS(_, _) => { // TODO } - Encapsulation::PwEth(_, _) => { + Encapsulation::PWETH(_, _) => { // TODO } - Encapsulation::Ipv4(_, _) => { + Encapsulation::IPv4(_, _) => { queue.add(Event::Ipv4Event, session.clone()); } - Encapsulation::Ipv6(_, _) => { + Encapsulation::IPv6(_, _) => { queue.add(Event::Ipv6Event, session.clone()); } - Encapsulation::Grev0(_, _) => { + Encapsulation::GREv0(_, _) => { // TODO } - Encapsulation::Grev1(_, _) => { + Encapsulation::GREv1(_, _) => { // TODO } - Encapsulation::Tcp(_, _) => { + Encapsulation::TCP(_, _) => { queue.add(Event::TcpEvent, session.clone()); } - Encapsulation::Udp(_, _) => { + Encapsulation::UDP(_, _) => { queue.add(Event::UdpEvent, session.clone()); } - Encapsulation::Icmp(_, _) => { + Encapsulation::ICMP(_, _) => { // TODO } - Encapsulation::Icmpv6(_, _) => { + Encapsulation::ICMPv6(_, _) => { // TODO } - Encapsulation::Gtpv1(_, _) => { + Encapsulation::GTPv1(_, _) => { // TODO } - Encapsulation::L2tp(_, _) => { + Encapsulation::L2TP(_, _) => { // TODO } - Encapsulation::Pptp(_, _) => { + Encapsulation::PPTP(_, _) => { // TODO } - Encapsulation::Ppp(_, _) => { + Encapsulation::PPP(_, _) => { // TODO } } @@ -139,7 +139,7 @@ fn handle_one_packet(data: &[u8], len: u32, ctx: Rc>) { } } - if packet.get_inner_tuple().is_some() { + if packet.get_inner_most_tuple().is_some() { let flow_id = packet.get_flow_id().unwrap(); let session = session_mgr.borrow_mut().update(flow_id); trigger_packet_event(&packet, Some(session.clone()), &mut queue); diff --git a/src/packet/packet.rs b/src/packet/packet.rs index e42dd2f..0576f70 100644 --- a/src/packet/packet.rs +++ b/src/packet/packet.rs @@ -1,49 +1,49 @@ use crate::packet::error::PacketError; use crate::protocol::codec::Decode; -use crate::protocol::ethernet::EtherType; -use crate::protocol::ethernet::EthernetFrame; -use crate::protocol::grev0::Grev0Header; -use crate::protocol::grev1::Grev1Header; -use crate::protocol::gtpv1::Gtpv1Header; -use crate::protocol::icmp::IcmpHeader; -use crate::protocol::icmpv6::Icmpv6Header; +use crate::protocol::ethernet::EthHeader; +use crate::protocol::ethernet::EthType; +use crate::protocol::grev0::GREv0Header; +use crate::protocol::grev1::GREv1Header; +use crate::protocol::gtpv1::GTPv1Header; +use crate::protocol::icmp::ICMPHeader; +use crate::protocol::icmpv6::ICMPv6Header; use crate::protocol::ip::IPProtocol; -use crate::protocol::ipv4::Ipv4Header; -use crate::protocol::ipv6::Ipv6Header; -use crate::protocol::l2tp::L2tpHeader; -use crate::protocol::l2tp::L2tpType; -use crate::protocol::mpls::MplsHeader; -use crate::protocol::mpls::PwEthHeader; -use crate::protocol::ppp::PppHeader; -use crate::protocol::ppp::PppProtocol; -use crate::protocol::pptp::PptpHeader; -use crate::protocol::tcp::TcpHeader; -use crate::protocol::udp::UdpHeader; -use crate::protocol::vlan::VlanHeader; +use crate::protocol::ipv4::IPv4Header; +use crate::protocol::ipv6::IPv6Header; +use crate::protocol::l2tp::L2TPHeader; +use crate::protocol::l2tp::L2TPType; +use crate::protocol::mpls::MPLSHeader; +use crate::protocol::mpls::PWEthHeader; +use crate::protocol::ppp::PPPHeader; +use crate::protocol::ppp::PPPProtocol; +use crate::protocol::pptp::PPTPHeader; +use crate::protocol::tcp::TCPHeader; +use crate::protocol::udp::UDPHeader; +use crate::protocol::vlan::VLANHeader; use nom::Err::Incomplete; #[derive(Clone, Debug, PartialEq)] pub enum Encapsulation<'a> { - Eth(EthernetFrame, &'a [u8]), - Vlan(VlanHeader, &'a [u8]), - Mpls(MplsHeader, &'a [u8]), - PwEth(PwEthHeader, &'a [u8]), - - Ipv4(Ipv4Header, &'a [u8]), - Ipv6(Ipv6Header, &'a [u8]), - - Grev0(Grev0Header, &'a [u8]), - Grev1(Grev1Header, &'a [u8]), - - Tcp(TcpHeader, &'a [u8]), - Udp(UdpHeader, &'a [u8]), - Icmp(IcmpHeader, &'a [u8]), - Icmpv6(Icmpv6Header, &'a [u8]), - - Gtpv1(Gtpv1Header, &'a [u8]), - L2tp(L2tpHeader, &'a [u8]), - Pptp(PptpHeader, &'a [u8]), - Ppp(PppHeader, &'a [u8]), + ETH(EthHeader, &'a [u8]), + VLAN(VLANHeader, &'a [u8]), + MPLS(MPLSHeader, &'a [u8]), + PWETH(PWEthHeader, &'a [u8]), + + IPv4(IPv4Header, &'a [u8]), + IPv6(IPv6Header, &'a [u8]), + + GREv0(GREv0Header, &'a [u8]), + GREv1(GREv1Header, &'a [u8]), + + TCP(TCPHeader, &'a [u8]), + UDP(UDPHeader, &'a [u8]), + ICMP(ICMPHeader, &'a [u8]), + ICMPv6(ICMPv6Header, &'a [u8]), + + GTPv1(GTPv1Header, &'a [u8]), + L2TP(L2TPHeader, &'a [u8]), + PPTP(PPTPHeader, &'a [u8]), + PPP(PPPHeader, &'a [u8]), } #[derive(Debug)] @@ -69,14 +69,14 @@ impl Packet<'_> { return handle_eth(self, self.orig_data); } - pub fn get_outer_l3_layer(&self) -> Option { + pub fn get_outer_most_l3_layer(&self) -> Option { let num = self.encapsulation.len(); for i in 0..num { match self.encapsulation[i] { - Encapsulation::Ipv4(_, _) => { + Encapsulation::IPv4(_, _) => { return Some(self.encapsulation[i].clone()); } - Encapsulation::Ipv6(_, _) => { + Encapsulation::IPv6(_, _) => { return Some(self.encapsulation[i].clone()); } _ => continue, @@ -86,14 +86,14 @@ impl Packet<'_> { return None; } - pub fn get_inner_l3_layer(&self) -> Option { + pub fn get_inner_most_l3_layer(&self) -> Option { let num = self.encapsulation.len(); for i in (0..num).rev() { match self.encapsulation[i] { - Encapsulation::Ipv4(_, _) => { + Encapsulation::IPv4(_, _) => { return Some(self.encapsulation[i].clone()); } - Encapsulation::Ipv6(_, _) => { + Encapsulation::IPv6(_, _) => { return Some(self.encapsulation[i].clone()); } _ => continue, @@ -103,14 +103,14 @@ impl Packet<'_> { return None; } - pub fn get_outer_l4_layer(&self) -> Option { + pub fn get_outer_most_l4_layer(&self) -> Option { let num = self.encapsulation.len(); for i in 0..num { match self.encapsulation[i] { - Encapsulation::Tcp(_, _) => { + Encapsulation::TCP(_, _) => { return Some(self.encapsulation[i].clone()); } - Encapsulation::Udp(_, _) => { + Encapsulation::UDP(_, _) => { return Some(self.encapsulation[i].clone()); } _ => continue, @@ -120,14 +120,14 @@ impl Packet<'_> { return None; } - pub fn get_inner_l4_layer(&self) -> Option { + pub fn get_inner_most_l4_layer(&self) -> Option { let num = self.encapsulation.len(); for i in (0..num).rev() { match self.encapsulation[i] { - Encapsulation::Tcp(_, _) => { + Encapsulation::TCP(_, _) => { return Some(self.encapsulation[i].clone()); } - Encapsulation::Udp(_, _) => { + Encapsulation::UDP(_, _) => { return Some(self.encapsulation[i].clone()); } _ => continue, @@ -137,17 +137,17 @@ impl Packet<'_> { return None; } - pub fn get_outer_address(&self) -> Option<(String, String)> { + pub fn get_outer_most_address(&self) -> Option<(String, String)> { let num = self.encapsulation.len(); for i in 0..num { match self.encapsulation[i] { - Encapsulation::Ipv4(ref header, _) => { + Encapsulation::IPv4(ref header, _) => { return Some(( header.source_address.to_string(), header.dest_address.to_string(), )); } - Encapsulation::Ipv6(ref header, _) => { + Encapsulation::IPv6(ref header, _) => { return Some(( header.source_address.to_string(), header.dest_address.to_string(), @@ -160,17 +160,17 @@ impl Packet<'_> { return None; } - pub fn get_inner_address(&self) -> Option<(String, String)> { + pub fn get_inner_most_address(&self) -> Option<(String, String)> { let num = self.encapsulation.len(); for i in (0..num).rev() { match self.encapsulation[i] { - Encapsulation::Ipv4(ref header, _) => { + Encapsulation::IPv4(ref header, _) => { return Some(( header.source_address.to_string(), header.dest_address.to_string(), )); } - Encapsulation::Ipv6(ref header, _) => { + Encapsulation::IPv6(ref header, _) => { return Some(( header.source_address.to_string(), header.dest_address.to_string(), @@ -183,14 +183,14 @@ impl Packet<'_> { return None; } - pub fn get_outer_port(&self) -> Option<(u16, u16)> { + pub fn get_outer_most_port(&self) -> Option<(u16, u16)> { let num = self.encapsulation.len(); for i in 0..num { match self.encapsulation[i] { - Encapsulation::Tcp(ref header, _) => { + Encapsulation::TCP(ref header, _) => { return Some((header.source_port, header.dest_port)); } - Encapsulation::Udp(ref header, _) => { + Encapsulation::UDP(ref header, _) => { return Some((header.source_port, header.dest_port)); } _ => continue, @@ -200,14 +200,14 @@ impl Packet<'_> { return None; } - pub fn get_inner_port(&self) -> Option<(u16, u16)> { + pub fn get_inner_most_port(&self) -> Option<(u16, u16)> { let num = self.encapsulation.len(); for i in (0..num).rev() { match self.encapsulation[i] { - Encapsulation::Tcp(ref header, _) => { + Encapsulation::TCP(ref header, _) => { return Some((header.source_port, header.dest_port)); } - Encapsulation::Udp(ref header, _) => { + Encapsulation::UDP(ref header, _) => { return Some((header.source_port, header.dest_port)); } _ => continue, @@ -217,15 +217,15 @@ impl Packet<'_> { return None; } - pub fn get_outer_tuple(&self) -> Option<(String, u16, String, u16)> { + pub fn get_outer_most_tuple(&self) -> Option<(String, u16, String, u16)> { let num = self.encapsulation.len(); if num < 2 { return None; } for i in 0..num - 1 { match self.encapsulation[i] { - Encapsulation::Ipv4(ref l3_header, _) => match self.encapsulation[i + 1] { - Encapsulation::Tcp(ref l4_header, _) => { + Encapsulation::IPv4(ref l3_header, _) => match self.encapsulation[i + 1] { + Encapsulation::TCP(ref l4_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -233,7 +233,7 @@ impl Packet<'_> { l4_header.dest_port, )); } - Encapsulation::Udp(ref l4_header, _) => { + Encapsulation::UDP(ref l4_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -243,8 +243,8 @@ impl Packet<'_> { } _ => continue, }, - Encapsulation::Ipv6(ref l3_header, _) => match self.encapsulation[i + 1] { - Encapsulation::Tcp(ref l4_header, _) => { + Encapsulation::IPv6(ref l3_header, _) => match self.encapsulation[i + 1] { + Encapsulation::TCP(ref l4_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -252,7 +252,7 @@ impl Packet<'_> { l4_header.dest_port, )); } - Encapsulation::Udp(ref l4_header, _) => { + Encapsulation::UDP(ref l4_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -269,15 +269,15 @@ impl Packet<'_> { return None; } - pub fn get_inner_tuple(&self) -> Option<(String, u16, String, u16)> { + pub fn get_inner_most_tuple(&self) -> Option<(String, u16, String, u16)> { let num = self.encapsulation.len(); if num < 2 { return None; } for i in (1..num).rev() { match self.encapsulation[i] { - Encapsulation::Tcp(ref l4_header, _) => match self.encapsulation[i - 1] { - Encapsulation::Ipv4(ref l3_header, _) => { + Encapsulation::TCP(ref l4_header, _) => match self.encapsulation[i - 1] { + Encapsulation::IPv4(ref l3_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -285,7 +285,7 @@ impl Packet<'_> { l4_header.dest_port, )); } - Encapsulation::Ipv6(ref l3_header, _) => { + Encapsulation::IPv6(ref l3_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -295,8 +295,8 @@ impl Packet<'_> { } _ => continue, }, - Encapsulation::Udp(ref l4_header, _) => match self.encapsulation[i - 1] { - Encapsulation::Ipv4(ref l3_header, _) => { + Encapsulation::UDP(ref l4_header, _) => match self.encapsulation[i - 1] { + Encapsulation::IPv4(ref l3_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -304,7 +304,7 @@ impl Packet<'_> { l4_header.dest_port, )); } - Encapsulation::Ipv6(ref l3_header, _) => { + Encapsulation::IPv6(ref l3_header, _) => { return Some(( l3_header.source_address.to_string(), l4_header.source_port, @@ -326,26 +326,26 @@ impl Packet<'_> { let mut flow_id = String::new(); for i in 0..num { match self.encapsulation[i] { - Encapsulation::Ipv4(ref l3_header, _) => { + Encapsulation::IPv4(ref l3_header, _) => { flow_id.push_str(&l3_header.source_address.to_string()); flow_id.push_str("->"); flow_id.push_str(&l3_header.dest_address.to_string()); flow_id.push_str(";"); } - Encapsulation::Ipv6(ref l3_header, _) => { + Encapsulation::IPv6(ref l3_header, _) => { flow_id.push_str(&l3_header.source_address.to_string()); flow_id.push_str("->"); flow_id.push_str(&l3_header.dest_address.to_string()); flow_id.push_str(";"); } - Encapsulation::Tcp(ref l4_header, _) => { + Encapsulation::TCP(ref l4_header, _) => { flow_id.push_str("TCP->TCP;"); flow_id.push_str(&l4_header.source_port.to_string()); flow_id.push_str("->"); flow_id.push_str(&l4_header.dest_port.to_string()); flow_id.push_str(";"); } - Encapsulation::Udp(ref l4_header, _) => { + Encapsulation::UDP(ref l4_header, _) => { flow_id.push_str("UDP->UDP;"); flow_id.push_str(&l4_header.source_port.to_string()); flow_id.push_str("->"); @@ -361,14 +361,14 @@ impl Packet<'_> { } fn handle_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = EthernetFrame::decode(input); + let result = EthHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.ether_type; packet .encapsulation - .push(Encapsulation::Eth(header, payload)); + .push(Encapsulation::ETH(header, payload)); return handle_l3(packet, payload, next_proto); } else { @@ -377,14 +377,14 @@ fn handle_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet } fn handle_vlan<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = VlanHeader::decode(input); + let result = VLANHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.ether_type; packet .encapsulation - .push(Encapsulation::Vlan(header, payload)); + .push(Encapsulation::VLAN(header, payload)); return handle_l3(packet, payload, next_proto); } else { @@ -393,14 +393,14 @@ fn handle_vlan<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_mpls<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = MplsHeader::decode(input); + let result = MPLSHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let bottom_of_stack = header.bottom_of_stack; packet .encapsulation - .push(Encapsulation::Mpls(header, payload)); + .push(Encapsulation::MPLS(header, payload)); if bottom_of_stack { if payload.len() < 1 { @@ -423,13 +423,13 @@ fn handle_mpls<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_pw_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = PwEthHeader::decode(input); + let result = PWEthHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); packet .encapsulation - .push(Encapsulation::PwEth(header, payload)); + .push(Encapsulation::PWETH(header, payload)); return handle_eth(packet, payload); } else { @@ -438,14 +438,14 @@ fn handle_pw_eth<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Pac } fn handle_ipv4<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = Ipv4Header::decode(input); + let result = IPv4Header::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.protocol; packet .encapsulation - .push(Encapsulation::Ipv4(header, payload)); + .push(Encapsulation::IPv4(header, payload)); // TODO IPv4 Fragment @@ -456,7 +456,7 @@ fn handle_ipv4<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_ipv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = Ipv6Header::decode(input); + let result = IPv6Header::decode(input); if let Ok((payload, header)) = result { dbg!(&header); @@ -471,7 +471,7 @@ fn handle_ipv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe packet .encapsulation - .push(Encapsulation::Ipv6(header, payload)); + .push(Encapsulation::IPv6(header, payload)); return handle_l4(packet, payload, next_proto); } else { @@ -480,7 +480,7 @@ fn handle_ipv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_tcp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = TcpHeader::decode(input); + let result = TCPHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); @@ -488,7 +488,7 @@ fn handle_tcp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet let dest_port = header.dest_port; packet .encapsulation - .push(Encapsulation::Tcp(header, payload)); + .push(Encapsulation::TCP(header, payload)); // TODO TCP Reassembly @@ -507,14 +507,14 @@ fn handle_tcp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet } fn handle_udp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = UdpHeader::decode(input); + let result = UDPHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let dest_port = header.dest_port; packet .encapsulation - .push(Encapsulation::Udp(header, payload)); + .push(Encapsulation::UDP(header, payload)); match dest_port { // GTP-U @@ -529,13 +529,13 @@ fn handle_udp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet } fn handle_icmp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = IcmpHeader::decode(input); + let result = ICMPHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); packet .encapsulation - .push(Encapsulation::Icmp(header, payload)); + .push(Encapsulation::ICMP(header, payload)); return Ok(()); } else { @@ -544,13 +544,13 @@ fn handle_icmp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_icmpv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = Icmpv6Header::decode(input); + let result = ICMPv6Header::decode(input); if let Ok((payload, header)) = result { dbg!(&header); packet .encapsulation - .push(Encapsulation::Icmpv6(header, payload)); + .push(Encapsulation::ICMPv6(header, payload)); return Ok(()); } else { @@ -559,14 +559,14 @@ fn handle_icmpv6<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Pac } fn handle_gtpv1<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = Gtpv1Header::decode(input); + let result = GTPv1Header::decode(input); match result { Ok((payload, header)) => { dbg!(&header); packet .encapsulation - .push(Encapsulation::Gtpv1(header, payload)); + .push(Encapsulation::GTPv1(header, payload)); if payload.len() < 1 { return Ok(()); @@ -589,7 +589,7 @@ fn handle_gtpv1<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Pack } fn handle_l2tp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = L2tpHeader::decode(input); + let result = L2TPHeader::decode(input); match result { Ok((payload, header)) => { dbg!(&header); @@ -597,13 +597,13 @@ fn handle_l2tp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe let l2tp_type = header.flag_type; packet .encapsulation - .push(Encapsulation::L2tp(header, payload)); + .push(Encapsulation::L2TP(header, payload)); match l2tp_type { - L2tpType::Control => { + L2TPType::Control => { return Ok(()); } - L2tpType::Data => { + L2TPType::Data => { // TODO handle PPP return Ok(()); } @@ -626,14 +626,14 @@ fn handle_gre<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet let version = input[1] & 0x07; match version { 0 => { - let result = Grev0Header::decode(input); + let result = GREv0Header::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.protocol_type; packet .encapsulation - .push(Encapsulation::Grev0(header, payload)); + .push(Encapsulation::GREv0(header, payload)); return handle_l3(packet, payload, next_proto); } else { @@ -641,14 +641,14 @@ fn handle_gre<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet } } 1 => { - let result = Grev1Header::decode(input); + let result = GREv1Header::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.protocol_type; packet .encapsulation - .push(Encapsulation::Grev1(header, payload)); + .push(Encapsulation::GREv1(header, payload)); return handle_l3(packet, payload, next_proto); } else { @@ -662,13 +662,13 @@ fn handle_gre<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet } fn handle_pptp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = PptpHeader::decode(input); + let result = PPTPHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); packet .encapsulation - .push(Encapsulation::Pptp(header, payload)); + .push(Encapsulation::PPTP(header, payload)); return Ok(()); } else { @@ -677,24 +677,24 @@ fn handle_pptp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packe } fn handle_ppp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), PacketError> { - let result = PppHeader::decode(input); + let result = PPPHeader::decode(input); if let Ok((payload, header)) = result { dbg!(&header); let next_proto = header.protocol; packet .encapsulation - .push(Encapsulation::Ppp(header, payload)); + .push(Encapsulation::PPP(header, payload)); match next_proto { - // PppProtocol::PAD => handle_pad(packet, payload), - PppProtocol::IPv4 => handle_ipv4(packet, payload), - PppProtocol::IPv6 => handle_ipv6(packet, payload), - // PppProtocol::IPCP => handle_ipcp(packet, payload), - // PppProtocol::CCP => handle_ccp(packet, payload), - // PppProtocol::LCP => handle_lcp(packet, payload), - // PppProtocol::PAP => handle_pap(packet, payload), - // PppProtocol::CHAP => handle_chap(packet, payload), + // PPPProtocol::PAD => handle_pad(packet, payload), + PPPProtocol::IPv4 => handle_ipv4(packet, payload), + PPPProtocol::IPv6 => handle_ipv6(packet, payload), + // PPPProtocol::IPCP => handle_ipcp(packet, payload), + // PPPProtocol::CCP => handle_ccp(packet, payload), + // PPPProtocol::LCP => handle_lcp(packet, payload), + // PPPProtocol::PAP => handle_pap(packet, payload), + // PPPProtocol::CHAP => handle_chap(packet, payload), _ => Ok(()), } } else { @@ -705,15 +705,15 @@ fn handle_ppp<'a>(packet: &mut Packet<'a>, input: &'a [u8]) -> Result<(), Packet fn handle_l3<'a>( packet: &mut Packet<'a>, input: &'a [u8], - next_proto: EtherType, + next_proto: EthType, ) -> Result<(), PacketError> { match next_proto { - EtherType::PPP => handle_ppp(packet, input), - EtherType::MPLSuni => handle_mpls(packet, input), - EtherType::QinQ => handle_vlan(packet, input), - EtherType::VLAN => handle_vlan(packet, input), - EtherType::IPv4 => handle_ipv4(packet, input), - EtherType::IPv6 => handle_ipv6(packet, input), + EthType::PPP => handle_ppp(packet, input), + EthType::MPLSuni => handle_mpls(packet, input), + EthType::QinQ => handle_vlan(packet, input), + EthType::VLAN => handle_vlan(packet, input), + EthType::IPv4 => handle_ipv4(packet, input), + EthType::IPv6 => handle_ipv6(packet, input), _e => { return Err(PacketError::UnsupportEthernetType); } @@ -747,37 +747,37 @@ fn handle_l4<'a>( mod tests { use super::Encapsulation; use super::Packet; - use crate::protocol::ethernet::EtherType; - use crate::protocol::ethernet::EthernetFrame; + use crate::protocol::ethernet::EthHeader; + use crate::protocol::ethernet::EthType; use crate::protocol::ethernet::MacAddress; - use crate::protocol::grev0::Grev0Header; - use crate::protocol::grev1::Grev1Header; - use crate::protocol::gtpv1::Gtpv1ExtensionHeader; - use crate::protocol::gtpv1::Gtpv1Header; - use crate::protocol::gtpv1::Gtpv1Option; - use crate::protocol::icmp::IcmpHeader; - use crate::protocol::icmp::IcmpType; + use crate::protocol::grev0::GREv0Header; + use crate::protocol::grev1::GREv1Header; + use crate::protocol::gtpv1::GTPv1ExtensionHeader; + use crate::protocol::gtpv1::GTPv1Header; + use crate::protocol::gtpv1::GTPv1Option; + use crate::protocol::icmp::ICMPHeader; + use crate::protocol::icmp::ICMPType; use crate::protocol::ip::IPProtocol; - use crate::protocol::ipv4::Ipv4Header; - use crate::protocol::ipv6::Ipv6Header; - use crate::protocol::mpls::MplsHeader; - use crate::protocol::mpls::PwEthHeader; - use crate::protocol::ppp::PppHeader; - use crate::protocol::ppp::PppProtocol; - use crate::protocol::pptp::PptpControlMessageType; - use crate::protocol::pptp::PptpHeader; - use crate::protocol::pptp::PptpMessageType; - use crate::protocol::tcp::TcpHeader; - use crate::protocol::tcp::TcpOption; - use crate::protocol::udp::UdpHeader; - use crate::protocol::vlan::VlanHeader; + use crate::protocol::ipv4::IPv4Header; + use crate::protocol::ipv6::IPv6Header; + use crate::protocol::mpls::MPLSHeader; + use crate::protocol::mpls::PWEthHeader; + use crate::protocol::ppp::PPPHeader; + use crate::protocol::ppp::PPPProtocol; + use crate::protocol::pptp::PPTPControlMessageType; + use crate::protocol::pptp::PPTPHeader; + use crate::protocol::pptp::PPTPMessageType; + use crate::protocol::tcp::TCPHeader; + use crate::protocol::tcp::TCPOption; + use crate::protocol::udp::UDPHeader; + use crate::protocol::vlan::VLANHeader; use std::net::Ipv4Addr; use std::net::Ipv6Addr; #[test] fn test_packet_api() { let mut packet = Packet::new(b"0", 1 as u32); - let ipv4_hdr = Ipv4Header { + let ipv4_hdr = IPv4Header { version: 4, ihl: 20, tos: 0, @@ -791,7 +791,7 @@ mod tests { source_address: Ipv4Addr::new(192, 168, 0, 101), dest_address: Ipv4Addr::new(121, 14, 154, 93), }; - let ipv6_hdr = Ipv6Header { + let ipv6_hdr = IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -807,7 +807,7 @@ mod tests { ), extensions: Vec::new(), }; - let tcp_hdr = TcpHeader { + let tcp_hdr = TCPHeader { source_port: 50081, dest_port: 443, seq_num: 1522577104, @@ -825,7 +825,7 @@ mod tests { urgent_ptr: 0, options: None, }; - let udp_hdr = UdpHeader { + let udp_hdr = UDPHeader { source_port: 9993, dest_port: 9994, length: 145, @@ -834,34 +834,34 @@ mod tests { packet .encapsulation - .push(Encapsulation::Ipv4(ipv4_hdr.clone(), b"1")); + .push(Encapsulation::IPv4(ipv4_hdr.clone(), b"1")); packet .encapsulation - .push(Encapsulation::Tcp(tcp_hdr.clone(), b"2")); + .push(Encapsulation::TCP(tcp_hdr.clone(), b"2")); packet .encapsulation - .push(Encapsulation::Ipv6(ipv6_hdr.clone(), b"3")); + .push(Encapsulation::IPv6(ipv6_hdr.clone(), b"3")); packet .encapsulation - .push(Encapsulation::Udp(udp_hdr.clone(), b"4")); + .push(Encapsulation::UDP(udp_hdr.clone(), b"4")); assert_eq!( - packet.get_outer_address(), + packet.get_outer_most_address(), Some(("192.168.0.101".to_string(), "121.14.154.93".to_string())) ); assert_eq!( - packet.get_inner_address(), + packet.get_inner_most_address(), Some(( "2409:8034:4025::50:a31".to_string(), "2409:8034:4040:5301::204".to_string() )) ); - assert_eq!(packet.get_outer_port(), Some((50081, 443))); - assert_eq!(packet.get_inner_port(), Some((9993, 9994))); + assert_eq!(packet.get_outer_most_port(), Some((50081, 443))); + assert_eq!(packet.get_inner_most_port(), Some((9993, 9994))); assert_eq!( - packet.get_outer_tuple(), + packet.get_outer_most_tuple(), Some(( "192.168.0.101".to_string(), 50081, @@ -870,7 +870,7 @@ mod tests { )) ); assert_eq!( - packet.get_inner_tuple(), + packet.get_inner_most_tuple(), Some(( "2409:8034:4025::50:a31".to_string(), 9993, @@ -880,20 +880,20 @@ mod tests { ); assert_eq!( - packet.get_outer_l3_layer(), - Some(Encapsulation::Ipv4(ipv4_hdr, b"1")) + packet.get_outer_most_l3_layer(), + Some(Encapsulation::IPv4(ipv4_hdr, b"1")) ); assert_eq!( - packet.get_inner_l3_layer(), - Some(Encapsulation::Ipv6(ipv6_hdr, b"3")) + packet.get_inner_most_l3_layer(), + Some(Encapsulation::IPv6(ipv6_hdr, b"3")) ); assert_eq!( - packet.get_outer_l4_layer(), - Some(Encapsulation::Tcp(tcp_hdr, b"2")) + packet.get_outer_most_l4_layer(), + Some(Encapsulation::TCP(tcp_hdr, b"2")) ); assert_eq!( - packet.get_inner_l4_layer(), - Some(Encapsulation::Udp(udp_hdr, b"4")) + packet.get_inner_most_l4_layer(), + Some(Encapsulation::UDP(udp_hdr, b"4")) ); assert_eq!(packet.get_flow_id(), Some("192.168.0.101->121.14.154.93;TCP->TCP;50081->443;2409:8034:4025::50:a31->2409:8034:4040:5301::204;UDP->UDP;9993->9994;".to_string())); @@ -1020,43 +1020,43 @@ mod tests { assert_eq!(packet.encapsulation.len(), 5); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x13, 0xc3, 0xdf, 0xae, 0x18]), dest_mac: MacAddress([0x00, 0x1b, 0xd4, 0x1b, 0xa4, 0xd8]), - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 0, drop_eligible_indicator: false, vlan_identifier: 118, - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[18..] ) ); assert_eq!( packet.encapsulation[2], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 0, drop_eligible_indicator: false, vlan_identifier: 10, - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[22..] ) ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0, @@ -1075,8 +1075,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 2048, dest_port: 52912, seq_num: 196611, @@ -1219,43 +1219,43 @@ mod tests { assert_eq!(packet.encapsulation.len(), 5); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0xa2, 0xc1, 0x12, 0x03, 0x02, 0x03]), dest_mac: MacAddress([0xa2, 0xc1, 0x12, 0x03, 0x01, 0x64]), - ether_type: EtherType::QinQ, + ether_type: EthType::QinQ, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 0, drop_eligible_indicator: false, vlan_identifier: 1, - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[18..] ) ); assert_eq!( packet.encapsulation[2], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 0, drop_eligible_indicator: false, vlan_identifier: 1, - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[22..] ) ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0, @@ -1274,8 +1274,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 10000, dest_port: 80, seq_num: 2180528890, @@ -1472,19 +1472,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 4); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x22, 0x46, 0x36, 0x51, 0x3c]), dest_mac: MacAddress([0x00, 0x22, 0x46, 0x36, 0x51, 0x38]), - ether_type: EtherType::IPv6, + ether_type: EthType::IPv6, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv6( - Ipv6Header { + Encapsulation::IPv6( + IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -1501,8 +1501,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0, @@ -1521,8 +1521,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 57639, dest_port: 22, seq_num: 1508621024, @@ -1675,19 +1675,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 4); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x2c, 0x6b, 0xf5, 0x45, 0x88, 0x29]), dest_mac: MacAddress([0x5c, 0x5e, 0xab, 0x2a, 0xa2, 0x00]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0, @@ -1706,8 +1706,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Ipv6( - Ipv6Header { + Encapsulation::IPv6( + IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -1728,8 +1728,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 52556, dest_port: 80, seq_num: 2172673240, @@ -1842,19 +1842,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 4); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), dest_mac: MacAddress([0xff, 0xff, 0xff, 0xff, 0xff, 0xff]), - ether_type: EtherType::IPv6, + ether_type: EthType::IPv6, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv6( - Ipv6Header { + Encapsulation::IPv6( + IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -1875,8 +1875,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Ipv6( - Ipv6Header { + Encapsulation::IPv6( + IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -1897,8 +1897,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Udp( - UdpHeader { + Encapsulation::UDP( + UDPHeader { source_port: 30000, dest_port: 13000, length: 12, @@ -2030,43 +2030,43 @@ mod tests { assert_eq!(packet.encapsulation.len(), 6); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0xa4, 0xc6, 0x4f, 0x3b, 0xb3, 0x9a]), dest_mac: MacAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x04]), - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 3, drop_eligible_indicator: false, vlan_identifier: 1624, - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[18..] ) ); assert_eq!( packet.encapsulation[2], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 3, drop_eligible_indicator: false, vlan_identifier: 505, - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[22..] ) ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0xb8, @@ -2085,8 +2085,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0xb8, @@ -2105,8 +2105,8 @@ mod tests { ); assert_eq!( packet.encapsulation[5], - Encapsulation::Udp( - UdpHeader { + Encapsulation::UDP( + UDPHeader { source_port: 62367, dest_port: 17000, length: 108, @@ -2237,19 +2237,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 5); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x30, 0x96, 0x05, 0x28, 0x38]), dest_mac: MacAddress([0x00, 0x30, 0x96, 0xe6, 0xfc, 0x39]), - ether_type: EtherType::MPLSuni, + ether_type: EthType::MPLSuni, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Mpls( - MplsHeader { + Encapsulation::MPLS( + MPLSHeader { label: 18, experimental: 5, bottom_of_stack: false, @@ -2260,8 +2260,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Mpls( - MplsHeader { + Encapsulation::MPLS( + MPLSHeader { label: 16, experimental: 5, bottom_of_stack: true, @@ -2272,8 +2272,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0xb0, @@ -2292,8 +2292,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 11001, dest_port: 23, seq_num: 3481568569, @@ -2309,7 +2309,7 @@ mod tests { window: 4128, checksum: 0xf791, urgent_ptr: 0, - options: Some(vec![TcpOption::MSS { + options: Some(vec![TCPOption::MSS { length: 4, mss: 536 }]), @@ -2429,19 +2429,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 7); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0xcc, 0x01, 0x0d, 0x5c, 0x00, 0x10]), dest_mac: MacAddress([0xcc, 0x00, 0x0d, 0x5c, 0x00, 0x10]), - ether_type: EtherType::MPLSuni, + ether_type: EthType::MPLSuni, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Mpls( - MplsHeader { + Encapsulation::MPLS( + MPLSHeader { label: 19, experimental: 0, bottom_of_stack: false, @@ -2452,8 +2452,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Mpls( - MplsHeader { + Encapsulation::MPLS( + MPLSHeader { label: 16, experimental: 0, bottom_of_stack: true, @@ -2464,23 +2464,23 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::PwEth(PwEthHeader { control_word: 0 }, &bytes[26..]) + Encapsulation::PWETH(PWEthHeader { control_word: 0 }, &bytes[26..]) ); assert_eq!( packet.encapsulation[4], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x50, 0x79, 0x66, 0x68, 0x00]), dest_mac: MacAddress([0x00, 0x50, 0x79, 0x66, 0x68, 0x01]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[40..] ) ); assert_eq!( packet.encapsulation[5], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -2499,9 +2499,9 @@ mod tests { ); assert_eq!( packet.encapsulation[6], - Encapsulation::Icmp( - IcmpHeader { - icmp_type: IcmpType::EchoRequest, + Encapsulation::ICMP( + ICMPHeader { + icmp_type: ICMPType::EchoRequest, icmp_code: 0, icmp_checksum: 0x529b, icmp_extended: vec![0xcc, 0x70, 0x01, 0x00], @@ -2666,31 +2666,31 @@ mod tests { assert_eq!(packet.encapsulation.len(), 7); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0xac, 0xb3, 0xb5, 0x40, 0xe9, 0xc3]), dest_mac: MacAddress([0x74, 0x4a, 0xa4, 0x0e, 0xf5, 0x14]), - ether_type: EtherType::VLAN, + ether_type: EthType::VLAN, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Vlan( - VlanHeader { + Encapsulation::VLAN( + VLANHeader { priority_code_point: 2, drop_eligible_indicator: false, vlan_identifier: 504, - ether_type: EtherType::IPv6, + ether_type: EthType::IPv6, }, &bytes[18..] ) ); assert_eq!( packet.encapsulation[2], - Encapsulation::Ipv6( - Ipv6Header { + Encapsulation::IPv6( + IPv6Header { version: 6, dsc: 18, ecn: 0, @@ -2707,8 +2707,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Udp( - UdpHeader { + Encapsulation::UDP( + UDPHeader { source_port: 2152, dest_port: 2152, length: 64, @@ -2719,8 +2719,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Gtpv1( - Gtpv1Header { + Encapsulation::GTPv1( + GTPv1Header { version: 1, protocol_type: 1, reserved: 0, @@ -2730,12 +2730,12 @@ mod tests { message_type: 0xff, message_length: 48, teid: 476419836, - options: Some(Gtpv1Option { + options: Some(GTPv1Option { sequence_number: 26425, npdu_number: 0, next_header_type: 0x85, }), - extensions: vec![Gtpv1ExtensionHeader { + extensions: vec![GTPv1ExtensionHeader { length: 1, contents: vec![0x10, 0x01], next_header_type: 0x00, @@ -2746,8 +2746,8 @@ mod tests { ); assert_eq!( packet.encapsulation[5], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -2766,8 +2766,8 @@ mod tests { ); assert_eq!( packet.encapsulation[6], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 47892, dest_port: 80, seq_num: 2480465049, @@ -2940,19 +2940,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 7); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x0f, 0xfe, 0xdd, 0x22, 0x42]), dest_mac: MacAddress([0x00, 0x1b, 0xd5, 0xff, 0x54, 0xd9]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -2971,8 +2971,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Grev0( - Grev0Header { + Encapsulation::GREv0( + GREv0Header { flag_checksum: false, flag_routing: false, flag_key: false, @@ -2981,7 +2981,7 @@ mod tests { recursion_control: 0, flags: 0, version: 0, - protocol_type: EtherType::IPv4, + protocol_type: EthType::IPv4, checksum: None, offset: None, key: None, @@ -2993,8 +2993,8 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -3013,8 +3013,8 @@ mod tests { ); assert_eq!( packet.encapsulation[4], - Encapsulation::Grev0( - Grev0Header { + Encapsulation::GREv0( + GREv0Header { flag_checksum: false, flag_routing: false, flag_key: false, @@ -3023,7 +3023,7 @@ mod tests { recursion_control: 0, flags: 0, version: 0, - protocol_type: EtherType::IPv4, + protocol_type: EthType::IPv4, checksum: None, offset: None, key: None, @@ -3035,8 +3035,8 @@ mod tests { ); assert_eq!( packet.encapsulation[5], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -3055,9 +3055,9 @@ mod tests { ); assert_eq!( packet.encapsulation[6], - Encapsulation::Icmp( - IcmpHeader { - icmp_type: IcmpType::EchoRequest, + Encapsulation::ICMP( + ICMPHeader { + icmp_type: ICMPType::EchoRequest, icmp_code: 0, icmp_checksum: 0xcbca, icmp_extended: vec![0x00, 0x17, 0x1d, 0xf2], @@ -3188,19 +3188,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 4); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x28, 0xd2, 0x44, 0x43, 0x38, 0x37]), dest_mac: MacAddress([0xc0, 0x00, 0x14, 0x8c, 0x00, 0x00]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -3219,8 +3219,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Tcp( - TcpHeader { + Encapsulation::TCP( + TCPHeader { source_port: 50112, dest_port: 1723, seq_num: 2945311102, @@ -3243,12 +3243,12 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Pptp( - PptpHeader { + Encapsulation::PPTP( + PPTPHeader { length: 24, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::SetLinkInfo, + control_message_type: PPTPControlMessageType::SetLinkInfo, reserved0: 0, payload: vec![ 0x00, 0x03, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, @@ -3384,19 +3384,19 @@ mod tests { assert_eq!(packet.encapsulation.len(), 6); assert_eq!( packet.encapsulation[0], - Encapsulation::Eth( - EthernetFrame { + Encapsulation::ETH( + EthHeader { source_mac: MacAddress([0x00, 0x14, 0x00, 0x00, 0x02, 0x00]), dest_mac: MacAddress([0x00, 0x09, 0xe9, 0x55, 0xc0, 0x1c]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }, &bytes[14..] ) ); assert_eq!( packet.encapsulation[1], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -3415,8 +3415,8 @@ mod tests { ); assert_eq!( packet.encapsulation[2], - Encapsulation::Grev1( - Grev1Header { + Encapsulation::GREv1( + GREv1Header { flag_checksum: false, flag_routing: false, flag_key: true, @@ -3426,7 +3426,7 @@ mod tests { flag_acknowledgment: false, flags: 0, version: 1, - protocol_type: EtherType::PPP, + protocol_type: EthType::PPP, key_payload_length: 88, key_call_id: 24, sequence_number: Some(7), @@ -3437,19 +3437,19 @@ mod tests { ); assert_eq!( packet.encapsulation[3], - Encapsulation::Ppp( - PppHeader { + Encapsulation::PPP( + PPPHeader { address: 0xff, control: 0x03, - protocol: PppProtocol::IPv4, + protocol: PPPProtocol::IPv4, }, &bytes[50..] ) ); assert_eq!( packet.encapsulation[4], - Encapsulation::Ipv4( - Ipv4Header { + Encapsulation::IPv4( + IPv4Header { version: 4, ihl: 20, tos: 0x00, @@ -3468,9 +3468,9 @@ mod tests { ); assert_eq!( packet.encapsulation[5], - Encapsulation::Icmp( - IcmpHeader { - icmp_type: IcmpType::EchoRequest, + Encapsulation::ICMP( + ICMPHeader { + icmp_type: ICMPType::EchoRequest, icmp_code: 0, icmp_checksum: 0x4500, icmp_extended: vec![0x18, 0x2b, 0x00, 0x01,], diff --git a/src/plugin/example.rs b/src/plugin/example.rs index b453660..d1f6b46 100644 --- a/src/plugin/example.rs +++ b/src/plugin/example.rs @@ -53,7 +53,7 @@ impl EventHandle for ExamplePulgin { match event { Event::TcpOpeningEvent => { println!("{} handle TcpOpeningEvent: {:?}", self.plugin_name, session); - let (src_port, dst_port) = packet.unwrap().get_inner_port().unwrap(); + let (src_port, dst_port) = packet.unwrap().get_inner_most_port().unwrap(); if src_port == 80 || dst_port == 80 { println!("{} add HttpRequestEvent", self.plugin_name); queue.add(Event::HttpRequestEvent, Some(session)); diff --git a/src/protocol/dns.rs b/src/protocol/dns.rs index 135bd11..3b39af0 100644 --- a/src/protocol/dns.rs +++ b/src/protocol/dns.rs @@ -10,14 +10,8 @@ use nom::sequence; use nom::Err; use nom::IResult; use nom::Needed; -use std::fmt; -use std::fmt::Display; use std::str; -/****************************************************************************** - * DNS Header Definitions - ******************************************************************************/ - /* * DNS Header Definitions * 1 1 1 1 1 1 @@ -37,42 +31,215 @@ use std::str; * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ */ -/****************************** DNS_HDR_QR ******************************/ +/* + * DNS Question Section Definitions + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | | + * / QNAME / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | QTYPE | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | QCLASS | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + */ + +/* + * DNS Resource Record Definitions + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | | + * / / + * / NAME / + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | TYPE | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | CLASS | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | TTL | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | RDLENGTH | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| + * / RDATA / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + */ + +/* + * A RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | ADDRESS | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * ADDRESS : A 32 bit Internet address + */ + +/* + * AAAA RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | ADDRESS | + * | | + * | | + * | | + * | | + * | | + * | | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * ADDRESS : A 128 bit IPv6 address is encoded in the data portion of an AAAA + * resource record in network byte order (high-order byte first). + */ + +/* + * CNAME RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / CNAME / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * CNAME : A which specifies the canonical or primary + * name for the owner. The owner name is an alias. + */ + +/* + * HINFO RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / CPU / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / OS / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * CPU : A which specifies the CPU type. + * OS : A which specifies the operating system type. + */ + +/* + * MX RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | PREFERENCE | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / EXCHANGE / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * PREFERENCE : A 16 bit integer which specifies the preference given to + * this RR among others at the same owner. Lower values are preferred. + * EXCHANGE : A which specifies a host willing to act as a mail exchange for the owner name. + */ + +/* + * NS RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / NSDNAME / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * NSDNAME : A which specifies a host which should be + * authoritative for the specified class and domain. + */ + +/* + * PTR RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / PTRDNAME / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * PTRDNAME : A which points to some location in the domain name space. + */ + +/* + * SOA RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / MNAME / + * / / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / RNAME / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | SERIAL | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | REFRESH | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | RETRY | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | EXPIRE | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * | MINIMUM | + * | | + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * MNAME : The of the name server that was the original or primary source of data for this zone. + * RNAME : A which specifies the mailbox of the person responsible for this zone. + * SERIAL : The unsigned 32 bit version number of the original copy of the zone. + * Zone transfers preserve this value. This value wraps and should be + * compared using sequence space arithmetic. + * REFRESH : A 32 bit time interval before the zone should be refreshed. + * RETRY : A 32 bit time interval that should elapse before a failed refresh should be retried. + * EXPIRE : A 32 bit time value that specifies the upper limit on + * the time interval that can elapse before the zone is no longer authoritative. + * MINIMUM : The unsigned 32 bit minimum TTL field that should beexported with any RR from this zone. + */ + +/* + * TXT RDATA format + * 1 1 1 1 1 1 + * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * / TXT-DATA / + * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ + * + * TXT-DATA : One or more s. + */ + +/****************************************************************************** + * DNS Header + ******************************************************************************/ -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum DNS_HDR_QR { +pub enum DNSHeaderQR { Query = 0, Response = 1, Other = 2, } -impl DNS_HDR_QR { - fn from_u8(n: u8) -> DNS_HDR_QR { +impl DNSHeaderQR { + fn from_u8(n: u8) -> DNSHeaderQR { match n { - 0 => DNS_HDR_QR::Query, - 1 => DNS_HDR_QR::Response, - _ => DNS_HDR_QR::Other, - } - } -} - -impl Display for DNS_HDR_QR { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DNS_HDR_QR::Query => write!(f, "QUERY"), - DNS_HDR_QR::Response => write!(f, "RESPONSE"), - DNS_HDR_QR::Other => write!(f, "OTHER"), + 0 => DNSHeaderQR::Query, + 1 => DNSHeaderQR::Response, + _ => DNSHeaderQR::Other, } } } -/****************************** DNS_HDR_OPCODE ******************************/ - // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5 -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum DNS_HDR_OPCODE { +pub enum DNSHeaderOpcode { Query, IQuery, Status, @@ -82,41 +249,24 @@ pub enum DNS_HDR_OPCODE { Other(u8), } -impl DNS_HDR_OPCODE { - fn from_u8(n: u8) -> DNS_HDR_OPCODE { +impl DNSHeaderOpcode { + fn from_u8(n: u8) -> DNSHeaderOpcode { match n { - 0 => DNS_HDR_OPCODE::Query, - 1 => DNS_HDR_OPCODE::IQuery, - 2 => DNS_HDR_OPCODE::Status, + 0 => DNSHeaderOpcode::Query, + 1 => DNSHeaderOpcode::IQuery, + 2 => DNSHeaderOpcode::Status, // 3 => Unassigned - 4 => DNS_HDR_OPCODE::Notify, - 5 => DNS_HDR_OPCODE::Update, - 6 => DNS_HDR_OPCODE::DSO, - n => DNS_HDR_OPCODE::Other(n), - } - } -} - -impl Display for DNS_HDR_OPCODE { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DNS_HDR_OPCODE::Query => write!(f, "QUERY"), - DNS_HDR_OPCODE::IQuery => write!(f, "IQUERY"), - DNS_HDR_OPCODE::Status => write!(f, "STATUS"), - DNS_HDR_OPCODE::Notify => write!(f, "NOTIFY"), - DNS_HDR_OPCODE::Update => write!(f, "UPDATE"), - DNS_HDR_OPCODE::DSO => write!(f, "DSO"), - DNS_HDR_OPCODE::Other(n) => write!(f, "OTHER {}", n), + 4 => DNSHeaderOpcode::Notify, + 5 => DNSHeaderOpcode::Update, + 6 => DNSHeaderOpcode::DSO, + n => DNSHeaderOpcode::Other(n), } } } -/****************************** DNS_HDR_RCODE ******************************/ - // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum DNS_HDR_RCODE { +pub enum DNSHeaderRcode { NoError, // No Error FormErr, // Format Error ServFail, // Server Failure @@ -140,98 +290,67 @@ pub enum DNS_HDR_RCODE { Other(u8), } -impl DNS_HDR_RCODE { - fn from_u8(n: u8) -> DNS_HDR_RCODE { +impl DNSHeaderRcode { + fn from_u8(n: u8) -> DNSHeaderRcode { match n { - 0 => DNS_HDR_RCODE::NoError, - 1 => DNS_HDR_RCODE::FormErr, - 2 => DNS_HDR_RCODE::ServFail, - 3 => DNS_HDR_RCODE::NXDomain, - 4 => DNS_HDR_RCODE::NotImp, - 5 => DNS_HDR_RCODE::Refused, - 6 => DNS_HDR_RCODE::YXDomain, - 7 => DNS_HDR_RCODE::YXRRSet, - 8 => DNS_HDR_RCODE::NXRRSet, - 9 => DNS_HDR_RCODE::NotAuth, - 10 => DNS_HDR_RCODE::NotZone, - 11 => DNS_HDR_RCODE::DSOTYPENI, + 0 => DNSHeaderRcode::NoError, + 1 => DNSHeaderRcode::FormErr, + 2 => DNSHeaderRcode::ServFail, + 3 => DNSHeaderRcode::NXDomain, + 4 => DNSHeaderRcode::NotImp, + 5 => DNSHeaderRcode::Refused, + 6 => DNSHeaderRcode::YXDomain, + 7 => DNSHeaderRcode::YXRRSet, + 8 => DNSHeaderRcode::NXRRSet, + 9 => DNSHeaderRcode::NotAuth, + 10 => DNSHeaderRcode::NotZone, + 11 => DNSHeaderRcode::DSOTYPENI, // hole - 16 => DNS_HDR_RCODE::BADSIG, - 17 => DNS_HDR_RCODE::BADKEY, - 18 => DNS_HDR_RCODE::BADTIME, - 19 => DNS_HDR_RCODE::BADMODE, - 20 => DNS_HDR_RCODE::BADNAME, - 21 => DNS_HDR_RCODE::BADALG, - 22 => DNS_HDR_RCODE::BADTRUNC, - 23 => DNS_HDR_RCODE::BADCOOKIE, - n => DNS_HDR_RCODE::Other(n), + 16 => DNSHeaderRcode::BADSIG, + 17 => DNSHeaderRcode::BADKEY, + 18 => DNSHeaderRcode::BADTIME, + 19 => DNSHeaderRcode::BADMODE, + 20 => DNSHeaderRcode::BADNAME, + 21 => DNSHeaderRcode::BADALG, + 22 => DNSHeaderRcode::BADTRUNC, + 23 => DNSHeaderRcode::BADCOOKIE, + n => DNSHeaderRcode::Other(n), } } } -impl Display for DNS_HDR_RCODE { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DNS_HDR_RCODE::NoError => write!(f, "NOERROR"), - DNS_HDR_RCODE::FormErr => write!(f, "FORMERR"), - DNS_HDR_RCODE::ServFail => write!(f, "SERVFAIL"), - DNS_HDR_RCODE::NXDomain => write!(f, "NXDOMAIN"), - DNS_HDR_RCODE::NotImp => write!(f, "NOTIMP"), - DNS_HDR_RCODE::Refused => write!(f, "REFUSED"), - DNS_HDR_RCODE::YXDomain => write!(f, "YXDOMAIN"), - DNS_HDR_RCODE::YXRRSet => write!(f, "YXRRSET"), - DNS_HDR_RCODE::NXRRSet => write!(f, "NXRRSET"), - DNS_HDR_RCODE::NotAuth => write!(f, "NOTAUTH"), - DNS_HDR_RCODE::NotZone => write!(f, "NOTZONE"), - DNS_HDR_RCODE::DSOTYPENI => write!(f, "DSOTYPENI"), - DNS_HDR_RCODE::BADSIG => write!(f, "BADSIG"), - DNS_HDR_RCODE::BADKEY => write!(f, "BADKEY"), - DNS_HDR_RCODE::BADTIME => write!(f, "BADTIME"), - DNS_HDR_RCODE::BADMODE => write!(f, "BADMODE"), - DNS_HDR_RCODE::BADNAME => write!(f, "BADNAME"), - DNS_HDR_RCODE::BADALG => write!(f, "BADALG"), - DNS_HDR_RCODE::BADTRUNC => write!(f, "BADTRUNC"), - DNS_HDR_RCODE::BADCOOKIE => write!(f, "BADCOOKIE"), - DNS_HDR_RCODE::Other(n) => write!(f, "OTHER {}", n), - } - } -} - -/****************************** DNS_HEADER ******************************/ - -#[allow(non_camel_case_types)] -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub struct DNS_HEADER { +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DNSHeader { pub id: u16, - pub qr: DNS_HDR_QR, - pub op_code: DNS_HDR_OPCODE, - pub aa: bool, // Authoritative Answer - pub tc: bool, // TrunCation - pub rd: bool, // Recursion Desired - pub ra: bool, // Recursion Available - pub ad: bool, // TODO ??? - pub cd: bool, // TODO ??? - pub r_code: DNS_HDR_RCODE, // Response Code + pub qr: DNSHeaderQR, + pub op_code: DNSHeaderOpcode, + pub aa: bool, // Authoritative Answer + pub tc: bool, // TrunCation + pub rd: bool, // Recursion Desired + pub ra: bool, // Recursion Available + pub ad: bool, // TODO ??? + pub cd: bool, // TODO ??? + pub r_code: DNSHeaderRcode, // Response Code pub qd_count: u16, pub an_count: u16, pub ns_count: u16, pub ar_count: u16, } -fn hdr_bits_decode( +fn bit_decode( input: &[u8], ) -> IResult< &[u8], ( - DNS_HDR_QR, - DNS_HDR_OPCODE, + DNSHeaderQR, + DNSHeaderOpcode, bool, bool, bool, bool, bool, bool, - DNS_HDR_RCODE, + DNSHeaderRcode, ), > { bits::<_, _, Error<(&[u8], usize)>, _, _>(sequence::tuple(( @@ -251,26 +370,26 @@ fn hdr_bits_decode( ( rest, ( - DNS_HDR_QR::from_u8(res.0), - DNS_HDR_OPCODE::from_u8(res.1), + DNSHeaderQR::from_u8(res.0), + DNSHeaderOpcode::from_u8(res.1), res.2 == 1, res.3 == 1, res.4 == 1, res.5 == 1, res.7 == 1, res.8 == 1, - DNS_HDR_RCODE::from_u8(res.9), + DNSHeaderRcode::from_u8(res.9), ), ) }, ) } -impl DNS_HEADER { - fn decode(input: &[u8]) -> IResult<&[u8], DNS_HEADER> { +impl DNSHeader { + fn decode(input: &[u8]) -> IResult<&[u8], DNSHeader> { sequence::tuple(( number::streaming::be_u16, - hdr_bits_decode, + bit_decode, number::streaming::be_u16, number::streaming::be_u16, number::streaming::be_u16, @@ -279,7 +398,7 @@ impl DNS_HEADER { .map(|(rest, res)| { ( rest, - DNS_HEADER { + DNSHeader { id: res.0, qr: res.1 .0, op_code: res.1 .1, @@ -300,67 +419,13 @@ impl DNS_HEADER { } } -impl Display for DNS_HEADER { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - ";; ->>HEADER<<- opcode: {}, status: {}, id: {}\n;; flags:", - self.op_code, self.r_code, self.id, - )?; - if self.qr == DNS_HDR_QR::Response { - write!(f, " qr")?; - } - if self.aa { - write!(f, " aa")?; - } - if self.tc { - write!(f, " tc")?; - } - if self.rd { - write!(f, " rd")?; - } - if self.ra { - write!(f, " ra")?; - } - if self.ad { - write!(f, " ad")?; - } - if self.cd { - write!(f, " cd")?; - } - write!( - f, - "; QUERY: {}, ANSWER: {}, AUTHORITY: {}, ADDITIONAL: {}\n", - self.qd_count, self.an_count, self.ns_count, self.ar_count - ) - } -} - /****************************************************************************** - * DNS Question Section Definitions + * DNS Question Section ******************************************************************************/ -/* - * DNS Question Section Definitions - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | | - * / QNAME / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | QTYPE | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | QCLASS | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - */ - -/****************************** DNS_QTYPE ******************************/ - // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4 -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum DNS_QTYPE { +pub enum DNSQtype { A, // a host address NS, // an authoritative name server MD, // a mail destination (OBSOLETE - use MX) @@ -453,331 +518,183 @@ pub enum DNS_QTYPE { Other(u16), } -impl DNS_QTYPE { - fn from_u16(n: u16) -> DNS_QTYPE { +impl DNSQtype { + fn from_u16(n: u16) -> DNSQtype { match n { - 1 => DNS_QTYPE::A, - 2 => DNS_QTYPE::NS, - 3 => DNS_QTYPE::MD, - 4 => DNS_QTYPE::MF, - 5 => DNS_QTYPE::CNAME, - 6 => DNS_QTYPE::SOA, - 7 => DNS_QTYPE::MB, - 8 => DNS_QTYPE::MG, - 9 => DNS_QTYPE::MR, - 10 => DNS_QTYPE::NULL, - 11 => DNS_QTYPE::WKS, - 12 => DNS_QTYPE::PTR, - 13 => DNS_QTYPE::HINFO, - 14 => DNS_QTYPE::MINFO, - 15 => DNS_QTYPE::MX, - 16 => DNS_QTYPE::TXT, - 17 => DNS_QTYPE::RP, - 18 => DNS_QTYPE::AFSDB, - 19 => DNS_QTYPE::X25, - 20 => DNS_QTYPE::ISDN, - 21 => DNS_QTYPE::RT, - 22 => DNS_QTYPE::NSAP, - 23 => DNS_QTYPE::NSAPPTR, - 24 => DNS_QTYPE::SIG, - 25 => DNS_QTYPE::KEY, - 26 => DNS_QTYPE::PX, - 27 => DNS_QTYPE::GPOS, - 28 => DNS_QTYPE::AAAA, - 29 => DNS_QTYPE::LOC, - 30 => DNS_QTYPE::NXT, - 31 => DNS_QTYPE::EID, - 32 => DNS_QTYPE::NIMLOC, - 33 => DNS_QTYPE::SRV, - 34 => DNS_QTYPE::ATMA, - 35 => DNS_QTYPE::NAPTR, - 36 => DNS_QTYPE::KX, - 37 => DNS_QTYPE::CERT, - 38 => DNS_QTYPE::A6, - 39 => DNS_QTYPE::DNAME, - 40 => DNS_QTYPE::SINK, - 41 => DNS_QTYPE::OPT, - 42 => DNS_QTYPE::APL, - 43 => DNS_QTYPE::DS, - 44 => DNS_QTYPE::SSHFP, - 45 => DNS_QTYPE::IPSECKEY, - 46 => DNS_QTYPE::RRSIG, - 47 => DNS_QTYPE::NSEC, - 48 => DNS_QTYPE::DNSKEY, - 49 => DNS_QTYPE::DHCID, - 50 => DNS_QTYPE::NSEC3, - 51 => DNS_QTYPE::NSEC3PARAM, - 52 => DNS_QTYPE::TLSA, - 53 => DNS_QTYPE::SMIMEA, + 1 => DNSQtype::A, + 2 => DNSQtype::NS, + 3 => DNSQtype::MD, + 4 => DNSQtype::MF, + 5 => DNSQtype::CNAME, + 6 => DNSQtype::SOA, + 7 => DNSQtype::MB, + 8 => DNSQtype::MG, + 9 => DNSQtype::MR, + 10 => DNSQtype::NULL, + 11 => DNSQtype::WKS, + 12 => DNSQtype::PTR, + 13 => DNSQtype::HINFO, + 14 => DNSQtype::MINFO, + 15 => DNSQtype::MX, + 16 => DNSQtype::TXT, + 17 => DNSQtype::RP, + 18 => DNSQtype::AFSDB, + 19 => DNSQtype::X25, + 20 => DNSQtype::ISDN, + 21 => DNSQtype::RT, + 22 => DNSQtype::NSAP, + 23 => DNSQtype::NSAPPTR, + 24 => DNSQtype::SIG, + 25 => DNSQtype::KEY, + 26 => DNSQtype::PX, + 27 => DNSQtype::GPOS, + 28 => DNSQtype::AAAA, + 29 => DNSQtype::LOC, + 30 => DNSQtype::NXT, + 31 => DNSQtype::EID, + 32 => DNSQtype::NIMLOC, + 33 => DNSQtype::SRV, + 34 => DNSQtype::ATMA, + 35 => DNSQtype::NAPTR, + 36 => DNSQtype::KX, + 37 => DNSQtype::CERT, + 38 => DNSQtype::A6, + 39 => DNSQtype::DNAME, + 40 => DNSQtype::SINK, + 41 => DNSQtype::OPT, + 42 => DNSQtype::APL, + 43 => DNSQtype::DS, + 44 => DNSQtype::SSHFP, + 45 => DNSQtype::IPSECKEY, + 46 => DNSQtype::RRSIG, + 47 => DNSQtype::NSEC, + 48 => DNSQtype::DNSKEY, + 49 => DNSQtype::DHCID, + 50 => DNSQtype::NSEC3, + 51 => DNSQtype::NSEC3PARAM, + 52 => DNSQtype::TLSA, + 53 => DNSQtype::SMIMEA, // hole - 55 => DNS_QTYPE::HIP, - 56 => DNS_QTYPE::NINFO, - 57 => DNS_QTYPE::RKEY, - 58 => DNS_QTYPE::TALINK, - 59 => DNS_QTYPE::CDS, - 60 => DNS_QTYPE::CDNSKEY, - 61 => DNS_QTYPE::OPENPGPKEY, - 62 => DNS_QTYPE::CSYNC, - 63 => DNS_QTYPE::ZONEMD, - 64 => DNS_QTYPE::SVCB, - 65 => DNS_QTYPE::HTTPS, + 55 => DNSQtype::HIP, + 56 => DNSQtype::NINFO, + 57 => DNSQtype::RKEY, + 58 => DNSQtype::TALINK, + 59 => DNSQtype::CDS, + 60 => DNSQtype::CDNSKEY, + 61 => DNSQtype::OPENPGPKEY, + 62 => DNSQtype::CSYNC, + 63 => DNSQtype::ZONEMD, + 64 => DNSQtype::SVCB, + 65 => DNSQtype::HTTPS, // hole - 99 => DNS_QTYPE::SPF, - 100 => DNS_QTYPE::UINFO, - 101 => DNS_QTYPE::UID, - 102 => DNS_QTYPE::GID, - 103 => DNS_QTYPE::UNSPEC, - 104 => DNS_QTYPE::NID, - 105 => DNS_QTYPE::L32, - 106 => DNS_QTYPE::L64, - 107 => DNS_QTYPE::LP, - 108 => DNS_QTYPE::EUI48, - 109 => DNS_QTYPE::EUI64, + 99 => DNSQtype::SPF, + 100 => DNSQtype::UINFO, + 101 => DNSQtype::UID, + 102 => DNSQtype::GID, + 103 => DNSQtype::UNSPEC, + 104 => DNSQtype::NID, + 105 => DNSQtype::L32, + 106 => DNSQtype::L64, + 107 => DNSQtype::LP, + 108 => DNSQtype::EUI48, + 109 => DNSQtype::EUI64, // hole - 249 => DNS_QTYPE::TKEY, - 250 => DNS_QTYPE::TSIG, - 251 => DNS_QTYPE::IXFR, - 252 => DNS_QTYPE::AXFR, - 253 => DNS_QTYPE::MAILB, - 254 => DNS_QTYPE::MAILA, - 255 => DNS_QTYPE::ANY, - 256 => DNS_QTYPE::URI, - 257 => DNS_QTYPE::CAA, - 258 => DNS_QTYPE::AVC, - 259 => DNS_QTYPE::DOA, - 260 => DNS_QTYPE::AMTRELAY, + 249 => DNSQtype::TKEY, + 250 => DNSQtype::TSIG, + 251 => DNSQtype::IXFR, + 252 => DNSQtype::AXFR, + 253 => DNSQtype::MAILB, + 254 => DNSQtype::MAILA, + 255 => DNSQtype::ANY, + 256 => DNSQtype::URI, + 257 => DNSQtype::CAA, + 258 => DNSQtype::AVC, + 259 => DNSQtype::DOA, + 260 => DNSQtype::AMTRELAY, // hole - 32768 => DNS_QTYPE::TA, - 32769 => DNS_QTYPE::DLV, - n => DNS_QTYPE::Other(n), + 32768 => DNSQtype::TA, + 32769 => DNSQtype::DLV, + n => DNSQtype::Other(n), } } } -impl Display for DNS_QTYPE { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DNS_QTYPE::A => write!(f, "A"), - DNS_QTYPE::NS => write!(f, "NS"), - DNS_QTYPE::MD => write!(f, "MD"), - DNS_QTYPE::MF => write!(f, "MF"), - DNS_QTYPE::CNAME => write!(f, "CNAME"), - DNS_QTYPE::SOA => write!(f, "SOA"), - DNS_QTYPE::MB => write!(f, "MB"), - DNS_QTYPE::MG => write!(f, "MG"), - DNS_QTYPE::MR => write!(f, "MR"), - DNS_QTYPE::NULL => write!(f, "NULL"), - DNS_QTYPE::WKS => write!(f, "WKS"), - DNS_QTYPE::PTR => write!(f, "PTR"), - DNS_QTYPE::HINFO => write!(f, "HINFO"), - DNS_QTYPE::MINFO => write!(f, "MINFO"), - DNS_QTYPE::MX => write!(f, "MX"), - DNS_QTYPE::TXT => write!(f, "TXT"), - DNS_QTYPE::RP => write!(f, "RP"), - DNS_QTYPE::AFSDB => write!(f, "AFSDB"), - DNS_QTYPE::X25 => write!(f, "X25"), - DNS_QTYPE::ISDN => write!(f, "ISDN"), - DNS_QTYPE::RT => write!(f, "RT"), - DNS_QTYPE::NSAP => write!(f, "NSAP"), - DNS_QTYPE::NSAPPTR => write!(f, "NSAPPTR"), - DNS_QTYPE::SIG => write!(f, "SIG"), - DNS_QTYPE::KEY => write!(f, "KEY"), - DNS_QTYPE::PX => write!(f, "PX"), - DNS_QTYPE::GPOS => write!(f, "GPOS"), - DNS_QTYPE::AAAA => write!(f, "AAAA"), - DNS_QTYPE::LOC => write!(f, "LOC"), - DNS_QTYPE::NXT => write!(f, "NXT"), - DNS_QTYPE::EID => write!(f, "EID"), - DNS_QTYPE::NIMLOC => write!(f, "NIMLOC"), - DNS_QTYPE::SRV => write!(f, "SRV"), - DNS_QTYPE::ATMA => write!(f, "ATMA"), - DNS_QTYPE::NAPTR => write!(f, "NAPTR"), - DNS_QTYPE::KX => write!(f, "KX"), - DNS_QTYPE::CERT => write!(f, "CERT"), - DNS_QTYPE::A6 => write!(f, "A6"), - DNS_QTYPE::DNAME => write!(f, "DNAME"), - DNS_QTYPE::SINK => write!(f, "SINK"), - DNS_QTYPE::OPT => write!(f, "OPT"), - DNS_QTYPE::APL => write!(f, "APL"), - DNS_QTYPE::DS => write!(f, "DS"), - DNS_QTYPE::SSHFP => write!(f, "SSHFP"), - DNS_QTYPE::IPSECKEY => write!(f, "IPSECKEY"), - DNS_QTYPE::RRSIG => write!(f, "RRSIG"), - DNS_QTYPE::NSEC => write!(f, "NSEC"), - DNS_QTYPE::DNSKEY => write!(f, "DNSKEY"), - DNS_QTYPE::DHCID => write!(f, "DHCID"), - DNS_QTYPE::NSEC3 => write!(f, "NSEC3"), - DNS_QTYPE::NSEC3PARAM => write!(f, "NSEC3PARAM"), - DNS_QTYPE::TLSA => write!(f, "TLSA"), - DNS_QTYPE::SMIMEA => write!(f, "SMIMEA"), - DNS_QTYPE::HIP => write!(f, "HIP"), - DNS_QTYPE::NINFO => write!(f, "NINFO"), - DNS_QTYPE::RKEY => write!(f, "RKEY"), - DNS_QTYPE::TALINK => write!(f, "TALINK"), - DNS_QTYPE::CDS => write!(f, "CDS"), - DNS_QTYPE::CDNSKEY => write!(f, "CDNSKEY"), - DNS_QTYPE::OPENPGPKEY => write!(f, "OPENPGPKEY"), - DNS_QTYPE::CSYNC => write!(f, "CSYNC"), - DNS_QTYPE::ZONEMD => write!(f, "ZONEMD"), - DNS_QTYPE::SVCB => write!(f, "SVCB"), - DNS_QTYPE::HTTPS => write!(f, "HTTPS"), - DNS_QTYPE::SPF => write!(f, "SPF"), - DNS_QTYPE::UINFO => write!(f, "UINFO"), - DNS_QTYPE::UID => write!(f, "UID"), - DNS_QTYPE::GID => write!(f, "GID"), - DNS_QTYPE::UNSPEC => write!(f, "UNSPEC"), - DNS_QTYPE::NID => write!(f, "NID"), - DNS_QTYPE::L32 => write!(f, "L32"), - DNS_QTYPE::L64 => write!(f, "L64"), - DNS_QTYPE::LP => write!(f, "LP"), - DNS_QTYPE::EUI48 => write!(f, "EUI48"), - DNS_QTYPE::EUI64 => write!(f, "EUI64"), - DNS_QTYPE::TKEY => write!(f, "TKEY"), - DNS_QTYPE::TSIG => write!(f, "TSIG"), - DNS_QTYPE::IXFR => write!(f, "IXFR"), - DNS_QTYPE::AXFR => write!(f, "AXFR"), - DNS_QTYPE::MAILB => write!(f, "MAILB"), - DNS_QTYPE::MAILA => write!(f, "MAILA"), - DNS_QTYPE::ANY => write!(f, "ANY"), - DNS_QTYPE::URI => write!(f, "URI"), - DNS_QTYPE::CAA => write!(f, "CAA"), - DNS_QTYPE::AVC => write!(f, "AVC"), - DNS_QTYPE::DOA => write!(f, "DOA"), - DNS_QTYPE::AMTRELAY => write!(f, "AMTRELAY"), - DNS_QTYPE::TA => write!(f, "TA"), - DNS_QTYPE::DLV => write!(f, "DLV"), - DNS_QTYPE::Other(n) => write!(f, "TYPE{}", n), - } - } -} - -/****************************** DNS_QCLASS ******************************/ - // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-2 -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum DNS_QCLASS { +pub enum DNSQclass { Internet, Unassigned, Chaos, Hesiod, - QCLASS_NONE, - QCLASS_ANY, + None, + Any, Other(u16), } -impl DNS_QCLASS { - fn from_u16(n: u16) -> DNS_QCLASS { +impl DNSQclass { + fn from_u16(n: u16) -> DNSQclass { match n { - 1 => DNS_QCLASS::Internet, - 2 => DNS_QCLASS::Unassigned, - 3 => DNS_QCLASS::Chaos, - 4 => DNS_QCLASS::Hesiod, - 254 => DNS_QCLASS::QCLASS_NONE, - 255 => DNS_QCLASS::QCLASS_ANY, - n => DNS_QCLASS::Other(n), + 1 => DNSQclass::Internet, + 2 => DNSQclass::Unassigned, + 3 => DNSQclass::Chaos, + 4 => DNSQclass::Hesiod, + 254 => DNSQclass::None, + 255 => DNSQclass::Any, + n => DNSQclass::Other(n), } } } -impl Display for DNS_QCLASS { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - DNS_QCLASS::Internet => write!(f, "Internet"), - DNS_QCLASS::Unassigned => write!(f, "Unassigned"), - DNS_QCLASS::Chaos => write!(f, "Chaos"), - DNS_QCLASS::Hesiod => write!(f, "Hesiod"), - DNS_QCLASS::QCLASS_NONE => write!(f, "QCLASS_NONE"), - DNS_QCLASS::QCLASS_ANY => write!(f, "QCLASS_ANY"), - DNS_QCLASS::Other(n) => write!(f, "CLASS{}", n), - } - } -} - -/****************************** DNS_QUESTION_SECTION ******************************/ - -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct DNS_QUESTION_SECTION { +pub struct DNSQuestionSection { pub qname: String, - pub qtype: DNS_QTYPE, - pub qclass: DNS_QCLASS, - size: usize, // DNS 原始数据中 DNS_QUESTION_SECTION 占多少字节 + pub qtype: DNSQtype, + pub qclass: DNSQclass, + size: usize, // DNS 原始数据中 DNSQuestionSection 占多少字节 } -impl DNS_QUESTION_SECTION { +impl DNSQuestionSection { fn decode<'a, 'b>( input: &'a [u8], - labels: &'b mut DNS_LABEL_TABLE, + labels: &'b mut DNSLabelTable, position: usize, - ) -> IResult<&'a [u8], DNS_QUESTION_SECTION> { + ) -> IResult<&'a [u8], DNSQuestionSection> { let (input, (name, size)) = dname_decode(input, labels, position)?; let (input, qtype) = number::streaming::be_u16(input)?; let (input, qclass) = number::streaming::be_u16(input)?; Ok(( input, - DNS_QUESTION_SECTION { + DNSQuestionSection { qname: name.to_string(), - qtype: DNS_QTYPE::from_u16(qtype), - qclass: DNS_QCLASS::from_u16(qclass), + qtype: DNSQtype::from_u16(qtype), + qclass: DNSQclass::from_u16(qclass), size: 4 + size, }, )) } } -impl Display for DNS_QUESTION_SECTION { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, ";{}\t{}\t{}\n", self.qname, self.qclass, self.qtype) - } -} - /****************************************************************************** - * DNS Resource Record Definitions + * DNS Resource Record Section ******************************************************************************/ -/* - * DNS Resource Record Definitions - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | | - * / / - * / NAME / - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | TYPE | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | CLASS | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | TTL | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | RDLENGTH | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--| - * / RDATA / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - */ - -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct DNS_RR_HDR { +pub struct DNSResourceRecordHeader { pub qname: String, - pub rr_type: DNS_QTYPE, - pub rr_class: DNS_QCLASS, + pub rr_type: DNSQtype, + pub rr_class: DNSQclass, pub ttl: i32, pub rd_length: u16, - size: usize, // DNS 原始数据中 DNS_RR_HDR 占多少字节 + size: usize, // DNS 原始数据中 DNSResourceRecordHeader 占多少字节 } -impl DNS_RR_HDR { +impl DNSResourceRecordHeader { fn decode<'a>( input: &'a [u8], - labels: &mut DNS_LABEL_TABLE, + labels: &mut DNSLabelTable, position: usize, - ) -> IResult<&'a [u8], DNS_RR_HDR> { + ) -> IResult<&'a [u8], DNSResourceRecordHeader> { let (input, (name, size)) = dname_decode(input, labels, position)?; let (input, rr_type) = number::streaming::be_u16(input)?; let (input, rr_class) = number::streaming::be_u16(input)?; @@ -786,10 +703,10 @@ impl DNS_RR_HDR { Ok(( input, - DNS_RR_HDR { + DNSResourceRecordHeader { qname: name.to_string(), - rr_type: DNS_QTYPE::from_u16(rr_type), - rr_class: DNS_QCLASS::from_u16(rr_class), + rr_type: DNSQtype::from_u16(rr_type), + rr_class: DNSQclass::from_u16(rr_class), ttl: rr_ttl, rd_length: rr_rdlength, size: 10 + size, @@ -798,155 +715,8 @@ impl DNS_RR_HDR { } } -/* - * A RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | ADDRESS | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * ADDRESS : A 32 bit Internet address - */ - -/* - * AAAA RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | ADDRESS | - * | | - * | | - * | | - * | | - * | | - * | | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * ADDRESS : A 128 bit IPv6 address is encoded in the data portion of an AAAA - * resource record in network byte order (high-order byte first). - */ - -/* - * CNAME RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / CNAME / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * CNAME : A which specifies the canonical or primary - * name for the owner. The owner name is an alias. - */ - -/* - * HINFO RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / CPU / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / OS / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * CPU : A which specifies the CPU type. - * OS : A which specifies the operating system type. - */ - -/* - * MX RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | PREFERENCE | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / EXCHANGE / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * PREFERENCE : A 16 bit integer which specifies the preference given to - * this RR among others at the same owner. Lower values are preferred. - * EXCHANGE : A which specifies a host willing to act as a mail exchange for the owner name. - */ - -/* - * NS RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / NSDNAME / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * NSDNAME : A which specifies a host which should be - * authoritative for the specified class and domain. - */ - -/* - * PTR RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / PTRDNAME / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * PTRDNAME : A which points to some location in the domain name space. - */ - -/* - * SOA RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / MNAME / - * / / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / RNAME / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | SERIAL | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | REFRESH | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | RETRY | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | EXPIRE | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * | MINIMUM | - * | | - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * MNAME : The of the name server that was the original or primary source of data for this zone. - * RNAME : A which specifies the mailbox of the person responsible for this zone. - * SERIAL : The unsigned 32 bit version number of the original copy of the zone. - * Zone transfers preserve this value. This value wraps and should be - * compared using sequence space arithmetic. - * REFRESH : A 32 bit time interval before the zone should be refreshed. - * RETRY : A 32 bit time interval that should elapse before a failed refresh should be retried. - * EXPIRE : A 32 bit time value that specifies the upper limit on - * the time interval that can elapse before the zone is no longer authoritative. - * MINIMUM : The unsigned 32 bit minimum TTL field that should beexported with any RR from this zone. - */ - -/* - * TXT RDATA format - * 1 1 1 1 1 1 - * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * / TXT-DATA / - * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ - * - * TXT-DATA : One or more s. - */ - -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub enum DNS_RR_DATA { +pub enum DNSResourceRecordData { A { address: String, }, @@ -983,87 +753,87 @@ pub enum DNS_RR_DATA { txtdata: Vec, }, Other { - qtype: DNS_QTYPE, + qtype: DNSQtype, data: Vec, }, } -impl DNS_RR_DATA { +impl DNSResourceRecordData { fn decode<'a>( input: &'a [u8], - labels: &mut DNS_LABEL_TABLE, + labels: &mut DNSLabelTable, position: usize, - rr_hdr: &DNS_RR_HDR, - ) -> IResult<&'a [u8], DNS_RR_DATA> { + rr_hdr: &DNSResourceRecordHeader, + ) -> IResult<&'a [u8], DNSResourceRecordData> { match (rr_hdr.rr_class, rr_hdr.rr_type) { - (DNS_QCLASS::Internet, DNS_QTYPE::A) => { + (DNSQclass::Internet, DNSQtype::A) => { let (input, num) = number::streaming::be_u32(input)?; Ok(( input, - DNS_RR_DATA::A { + DNSResourceRecordData::A { address: address_v4_decode(num), }, )) } - (DNS_QCLASS::Internet, DNS_QTYPE::AAAA) => { + (DNSQclass::Internet, DNSQtype::AAAA) => { let (input, num) = number::streaming::be_u128(input)?; Ok(( input, - DNS_RR_DATA::AAAA { + DNSResourceRecordData::AAAA { address: address_v6_decode(num), }, )) } - (_, DNS_QTYPE::CNAME) => { + (_, DNSQtype::CNAME) => { let (input, (name, _size)) = dname_decode(input, labels, position)?; Ok(( input, - DNS_RR_DATA::CNAME { + DNSResourceRecordData::CNAME { cname: name.to_string(), }, )) } - (_, DNS_QTYPE::HINFO) => { + (_, DNSQtype::HINFO) => { let (input, _cpu) = multi::length_data(number::streaming::be_u8)(input)?; let (input, _os) = multi::length_data(number::streaming::be_u8)(input)?; Ok(( input, - DNS_RR_DATA::HINFO { + DNSResourceRecordData::HINFO { cpu: _cpu.to_vec(), os: _os.to_vec(), }, )) } - (_, DNS_QTYPE::MX) => { + (_, DNSQtype::MX) => { let (input, _preference) = number::streaming::be_u16(input)?; let (input, (name, _size)) = dname_decode(input, labels, position + 2)?; Ok(( input, - DNS_RR_DATA::MX { + DNSResourceRecordData::MX { preference: _preference, exchange: name.to_string(), }, )) } - (_, DNS_QTYPE::NS) => { + (_, DNSQtype::NS) => { let (input, (name, _size)) = dname_decode(input, labels, position)?; Ok(( input, - DNS_RR_DATA::NS { + DNSResourceRecordData::NS { nsdname: name.to_string(), }, )) } - (_, DNS_QTYPE::PTR) => { + (_, DNSQtype::PTR) => { let (input, (name, _size)) = dname_decode(input, labels, position)?; Ok(( input, - DNS_RR_DATA::PTR { + DNSResourceRecordData::PTR { ptrdnmae: name.to_string(), }, )) } - (_, DNS_QTYPE::SOA) => { + (_, DNSQtype::SOA) => { let (input, (_mname, mname_size)) = dname_decode(input, labels, position)?; let mname_string = _mname.to_string(); let (input, (_rname, _rname_size)) = @@ -1075,7 +845,7 @@ impl DNS_RR_DATA { let (input, _minimum) = number::streaming::be_i32(input)?; Ok(( input, - DNS_RR_DATA::SOA { + DNSResourceRecordData::SOA { mname: mname_string, rname: _rname.to_string(), serial: _serial, @@ -1086,7 +856,7 @@ impl DNS_RR_DATA { }, )) } - (_, DNS_QTYPE::TXT) => { + (_, DNSQtype::TXT) => { let mut input = input; let mut nleft = rr_hdr.rd_length; let mut ret = Vec::new(); @@ -1101,14 +871,14 @@ impl DNS_RR_DATA { break; } } - Ok((input, DNS_RR_DATA::TXT { txtdata: ret })) + Ok((input, DNSResourceRecordData::TXT { txtdata: ret })) } (_, _) => { let slen: usize = rr_hdr.rd_length.into(); let rdata = &input[0..slen]; Ok(( &input[slen..], - DNS_RR_DATA::Other { + DNSResourceRecordData::Other { qtype: rr_hdr.rr_type, data: rdata.to_vec(), }, @@ -1118,27 +888,25 @@ impl DNS_RR_DATA { } } -/****************************** DNS_RR_SECTION ******************************/ - -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct DNS_RR_SECTION { - pub hdr: DNS_RR_HDR, - pub data: DNS_RR_DATA, +pub struct DNSResourceRecordSection { + pub hdr: DNSResourceRecordHeader, + pub data: DNSResourceRecordData, } -impl DNS_RR_SECTION { +impl DNSResourceRecordSection { fn decode<'a, 'b>( input: &'a [u8], - labels: &'b mut DNS_LABEL_TABLE, + labels: &'b mut DNSLabelTable, position: usize, - ) -> IResult<&'a [u8], DNS_RR_SECTION> { - let (left, rr_hdr) = DNS_RR_HDR::decode(input, labels, position)?; - let (left, rr_data) = DNS_RR_DATA::decode(left, labels, position + rr_hdr.size, &rr_hdr)?; + ) -> IResult<&'a [u8], DNSResourceRecordSection> { + let (left, rr_hdr) = DNSResourceRecordHeader::decode(input, labels, position)?; + let (left, rr_data) = + DNSResourceRecordData::decode(left, labels, position + rr_hdr.size, &rr_hdr)?; Ok(( left, - DNS_RR_SECTION { + DNSResourceRecordSection { hdr: rr_hdr, data: rr_data, }, @@ -1146,107 +914,27 @@ impl DNS_RR_SECTION { } } -impl Display for DNS_RR_SECTION { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}\t\t{}\t{}\t{}\t", - self.hdr.qname, self.hdr.ttl, self.hdr.rr_class, self.hdr.rr_type - )?; - match &self.data { - DNS_RR_DATA::A { address: _address } => { - write!(f, "{}\n", _address) - } - DNS_RR_DATA::AAAA { address: _address } => { - write!(f, "{}\n", _address) - } - DNS_RR_DATA::CNAME { cname: _cname } => { - write!(f, "{}\n", _cname) - } - DNS_RR_DATA::HINFO { cpu: _cpu, os: _os } => { - write!(f, "\\# ")?; - for i in &*_cpu { - write!(f, "{:02X}", i)?; - } - write!(f, "\n\\# ")?; - for i in &*_os { - write!(f, "{:02X}", i)?; - } - write!(f, "\n") - } - DNS_RR_DATA::MX { - preference: _preference, - exchange: _exchange, - } => { - write!(f, "{} {}\n", _preference, _exchange) - } - DNS_RR_DATA::NS { nsdname: _nsdname } => { - write!(f, "{}\n", _nsdname) - } - DNS_RR_DATA::PTR { - ptrdnmae: _ptrdnmae, - } => { - write!(f, "{}\n", _ptrdnmae) - } - DNS_RR_DATA::SOA { - mname: _mname, - rname: _rname, - serial: _serial, - refresh: _refresh, - retry: _retry, - expire: _expire, - minimum: _minimum, - } => { - write!( - f, - "{} {} {} {} {} {} {}\n", - _mname, _rname, _serial, _refresh, _retry, _expire, _minimum, - ) - } - DNS_RR_DATA::TXT { txtdata: _txtdata } => { - for i in _txtdata { - write!(f, "{}", i)?; - } - Ok(()) - } - DNS_RR_DATA::Other { - qtype: _qtype, - data: _data, - } => { - write!(f, "\\# ")?; - for i in &*_data { - write!(f, "{:02X}", i)?; - } - write!(f, "\n") - } - } - } -} - /****************************************************************************** - * DNS Message + * DNS Label ******************************************************************************/ -/****************************** DNS_LABEL_TYPE ******************************/ - // https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-10 -#[allow(non_camel_case_types)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum DNS_LABEL_TYPE { +enum DNSLabelType { Normal(usize), // Normal label lower 6 bits is the length of the label Pointer(usize), // Compressed label the lower 6 bits and the 8 bits from next octet form a pointer to the compression target End, Other, } -impl DNS_LABEL_TYPE { +impl DNSLabelType { fn peek(input: &[u8]) -> IResult<&[u8], (u8, u8)> { combinator::peek(bits::<_, _, Error<(&[u8], usize)>, _, _>(sequence::tuple( (bits::streaming::take(1usize), bits::streaming::take(1usize)), )))(input) } - fn decode(input: &[u8]) -> IResult<&[u8], DNS_LABEL_TYPE> { + fn decode(input: &[u8]) -> IResult<&[u8], DNSLabelType> { match Self::peek(input) { // 域名压缩 Ok((_, (1u8, 1u8))) => bits::<_, _, Error<(&[u8], usize)>, _, _>(sequence::tuple(( @@ -1258,7 +946,7 @@ impl DNS_LABEL_TYPE { .map(|(input, (_, _, left, right)): (&[u8], (u8, u8, u8, u8))| { ( input, - DNS_LABEL_TYPE::Pointer((((left as u16) << 8) | right as u16) as usize), + DNSLabelType::Pointer((((left as u16) << 8) | right as u16) as usize), ) }), // 域名未压缩 @@ -1271,27 +959,24 @@ impl DNS_LABEL_TYPE { if size == 0 { // 域名未压缩时,需要在域名最后加 0x00 表示名域名结束 // 域名压缩时,就不用在最后加 0x00 了 - return (input, DNS_LABEL_TYPE::End); + return (input, DNSLabelType::End); } else { - return (input, DNS_LABEL_TYPE::Normal(size as usize)); + return (input, DNSLabelType::Normal(size as usize)); } }), - _ => Ok((input, DNS_LABEL_TYPE::Other)), + _ => Ok((input, DNSLabelType::Other)), } } } -/****************************** DNS_LABEL_TABLE ******************************/ - -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct DNS_LABEL_TABLE { +pub struct DNSLabelTable { pub labels: Vec<((usize, usize), String)>, } -impl DNS_LABEL_TABLE { - fn new() -> DNS_LABEL_TABLE { - DNS_LABEL_TABLE { labels: vec![] } +impl DNSLabelTable { + fn new() -> DNSLabelTable { + DNSLabelTable { labels: vec![] } } /* @@ -1343,24 +1028,25 @@ impl DNS_LABEL_TABLE { } } -/****************************** DNS_MESSAGE ******************************/ +/****************************************************************************** + * DNS Message + ******************************************************************************/ -#[allow(non_camel_case_types)] #[derive(Clone, Debug, PartialEq, Eq)] -pub struct DNS_MESSAGE { - pub header: DNS_HEADER, - labels: DNS_LABEL_TABLE, - pub qd: Vec, - pub an: Vec, - pub ns: Vec, - pub ar: Vec, +pub struct DNSMessage { + pub header: DNSHeader, + labels: DNSLabelTable, + pub qd: Vec, + pub an: Vec, + pub ns: Vec, + pub ar: Vec, } -impl DNS_MESSAGE { - fn new(header: DNS_HEADER) -> DNS_MESSAGE { - DNS_MESSAGE { +impl DNSMessage { + fn new(header: DNSHeader) -> DNSMessage { + DNSMessage { header: header, - labels: DNS_LABEL_TABLE::new(), + labels: DNSLabelTable::new(), qd: vec![], an: vec![], ns: vec![], @@ -1369,24 +1055,25 @@ impl DNS_MESSAGE { } } -impl Decode for DNS_MESSAGE { - type Iterm = 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); +impl Decode for DNSMessage { + type Iterm = DNSMessage; + fn decode(input: &[u8]) -> IResult<&[u8], DNSMessage> { + let (mut input, header) = DNSHeader::decode(input)?; + let mut message = DNSMessage::new(header); let mut position = 12; // QDCOUNT - for _ in 0..header.qd_count { - let (left, qd) = DNS_QUESTION_SECTION::decode(input, &mut message.labels, position)?; + for _ in 0..message.header.qd_count { + let (left, qd) = DNSQuestionSection::decode(input, &mut message.labels, position)?; input = left; position += qd.size; message.qd.push(qd); } // ANCOUNT - for _ in 0..header.an_count { - let (left, rr) = DNS_RR_SECTION::decode(input, &mut message.labels, position)?; + for _ in 0..message.header.an_count { + let (left, rr) = + DNSResourceRecordSection::decode(input, &mut message.labels, position)?; position += rr.hdr.size + rr.hdr.rd_length as usize; message.an.push(rr); @@ -1394,8 +1081,9 @@ impl Decode for DNS_MESSAGE { } // NSCOUNT - for _ in 0..header.ns_count { - let (left, rr) = DNS_RR_SECTION::decode(input, &mut message.labels, position)?; + for _ in 0..message.header.ns_count { + let (left, rr) = + DNSResourceRecordSection::decode(input, &mut message.labels, position)?; position += rr.hdr.size + rr.hdr.rd_length as usize; message.ns.push(rr); @@ -1403,8 +1091,9 @@ impl Decode for DNS_MESSAGE { } // ARCOUNT - for _ in 0..header.ar_count { - let (left, rr) = DNS_RR_SECTION::decode(input, &mut message.labels, position)?; + for _ in 0..message.header.ar_count { + let (left, rr) = + DNSResourceRecordSection::decode(input, &mut message.labels, position)?; position += rr.hdr.size + rr.hdr.rd_length as usize; message.ar.push(rr); @@ -1415,37 +1104,6 @@ impl Decode for DNS_MESSAGE { } } -impl Display for DNS_MESSAGE { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{}\n", self.header)?; - if self.qd.len() > 0 { - write!(f, ";; QUESTION SECTION:\n")?; - for qde in &self.qd { - write!(f, "{}\n", qde)?; - } - } - if self.an.len() > 0 { - write!(f, ";; ANSWER SECTION:\n")?; - for ane in &self.an { - ane.fmt(f)?; - } - } - if self.ns.len() > 0 { - write!(f, ";; AUTHORITY SECTION:\n")?; - for nse in &self.ns { - nse.fmt(f)?; - } - } - if self.ar.len() > 0 { - write!(f, ";; ADDITIONAL SECTION:\n")?; - for are in &self.ar { - are.fmt(f)?; - } - } - Ok(()) - } -} - /****************************************************************************** * Utils ******************************************************************************/ @@ -1518,17 +1176,17 @@ fn label_decode<'a>( */ fn dname_decode<'a, 'b>( data: &'a [u8], - labels: &'b mut DNS_LABEL_TABLE, + labels: &'b mut DNSLabelTable, position: usize, ) -> IResult<&'a [u8], (&'b str, usize)> { let mut input = data; let mut total_size = 0usize; let mut name = String::new(); loop { - match DNS_LABEL_TYPE::decode(input) { + match DNSLabelType::decode(input) { Ok((inner_input, label_type)) => match label_type { // 域名未压缩 - DNS_LABEL_TYPE::Normal(size) => { + DNSLabelType::Normal(size) => { total_size += 1 + size; if name.len() > 0 { name.push('.'); @@ -1538,7 +1196,7 @@ fn dname_decode<'a, 'b>( input = left; } // 域名压缩 - DNS_LABEL_TYPE::Pointer(_position) => { + DNSLabelType::Pointer(_position) => { total_size += 2; if name.len() > 0 { name.push('.'); @@ -1555,14 +1213,14 @@ fn dname_decode<'a, 'b>( return Ok((inner_input, (n, total_size))); } // 域名未压缩 - DNS_LABEL_TYPE::End => { + DNSLabelType::End => { total_size += 1; name.push('.'); let n = labels.set(position, total_size, &name); return Ok((inner_input, (n, total_size))); } - DNS_LABEL_TYPE::Other => { + DNSLabelType::Other => { return Err(Err::Failure(Error::new(inner_input, ErrorKind::Fail))); } }, @@ -1582,18 +1240,18 @@ mod tests { use super::character_string_decode; use super::dname_decode; use super::label_decode; - use super::DNS_HDR_OPCODE; - use super::DNS_HDR_QR; - use super::DNS_HDR_RCODE; - use super::DNS_HEADER; - use super::DNS_LABEL_TABLE; - use super::DNS_MESSAGE; - use super::DNS_QCLASS; - use super::DNS_QTYPE; - use super::DNS_QUESTION_SECTION; - use super::DNS_RR_DATA; - use super::DNS_RR_HDR; - use super::DNS_RR_SECTION; + use super::DNSHeader; + use super::DNSHeaderOpcode; + use super::DNSHeaderQR; + use super::DNSHeaderRcode; + use super::DNSLabelTable; + use super::DNSMessage; + use super::DNSQclass; + use super::DNSQtype; + use super::DNSQuestionSection; + use super::DNSResourceRecordData; + use super::DNSResourceRecordHeader; + use super::DNSResourceRecordSection; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[]; @@ -1618,7 +1276,7 @@ mod tests { #[test] fn test_label_table() { - let mut table = DNS_LABEL_TABLE::new(); + let mut table = DNSLabelTable::new(); table.set(30, 16, "www.example.com."); assert_eq!(table.get(29), None); @@ -1681,7 +1339,7 @@ mod tests { #[test] fn test_dname_decode() { - let mut table = DNS_LABEL_TABLE::new(); + let mut table = DNSLabelTable::new(); // uncompress name: www.example.com let input1 = [ @@ -1769,58 +1427,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xa59a, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 15), "www.baidu.com.".to_string()), ((31, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "www.baidu.com.".to_string(), - qtype: DNS_QTYPE::A, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::A, + qclass: DNSQclass::Internet, size: 19, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -1890,58 +1548,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xf086, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 15), "www.baidu.com.".to_string()), ((31, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "www.baidu.com.".to_string(), - qtype: DNS_QTYPE::AAAA, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::AAAA, + qclass: DNSQclass::Internet, size: 19, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -2011,58 +1669,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0x61a5, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 15), "www.baidu.com.".to_string()), ((31, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "www.baidu.com.".to_string(), - qtype: DNS_QTYPE::CNAME, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::CNAME, + qclass: DNSQclass::Internet, size: 19, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -2135,58 +1793,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xd35c, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 9), "163.com.".to_string()), ((25, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "163.com.".to_string(), - qtype: DNS_QTYPE::MX, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::MX, + qclass: DNSQclass::Internet, size: 13, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -2264,58 +1922,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xb613, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 15), "www.baidu.com.".to_string()), ((31, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "www.baidu.com.".to_string(), - qtype: DNS_QTYPE::SOA, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::SOA, + qclass: DNSQclass::Internet, size: 19, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -2386,58 +2044,58 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0x6721, - qr: DNS_HDR_QR::Query, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Query, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: false, ad: true, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 0, ns_count: 0, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 11), "baidu.com.".to_string()), ((27, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "baidu.com.".to_string(), - qtype: DNS_QTYPE::TXT, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::TXT, + qclass: DNSQclass::Internet, size: 15, }], an: vec![], ns: vec![], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -2720,24 +2378,24 @@ mod tests { 0x00, 0x00, ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xd35c, - qr: DNS_HDR_QR::Response, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Response, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: true, ad: false, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 4, ns_count: 7, ar_count: 13, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 9), "163.com.".to_string()), ((25, 2), "163.com.".to_string()), @@ -2777,340 +2435,340 @@ mod tests { ((467, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "163.com.".to_string(), - qtype: DNS_QTYPE::MX, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::MX, + qclass: DNSQclass::Internet, size: 13, }], an: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::MX, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::MX, + rr_class: DNSQclass::Internet, ttl: 12492, rd_length: 27, size: 12, }, - data: DNS_RR_DATA::MX { + data: DNSResourceRecordData::MX { preference: 10, exchange: "163mx02.mxmail.netease.com.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::MX, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::MX, + rr_class: DNSQclass::Internet, ttl: 12492, rd_length: 12, size: 12, }, - data: DNS_RR_DATA::MX { + data: DNSResourceRecordData::MX { preference: 50, exchange: "163mx00.mxmail.netease.com.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::MX, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::MX, + rr_class: DNSQclass::Internet, ttl: 12492, rd_length: 12, size: 12, }, - data: DNS_RR_DATA::MX { + data: DNSResourceRecordData::MX { preference: 10, exchange: "163mx03.mxmail.netease.com.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::MX, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::MX, + rr_class: DNSQclass::Internet, ttl: 12492, rd_length: 12, size: 12, }, - data: DNS_RR_DATA::MX { + data: DNSResourceRecordData::MX { preference: 10, exchange: "163mx01.mxmail.netease.com.".to_string(), }, }), ], ns: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 15, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns5.nease.net.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 6, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns3.nease.net.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 6, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns1.nease.net.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 10, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns8.166.com.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 6, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns4.nease.net.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 6, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns2.166.com.".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 34967, rd_length: 6, size: 12, }, - data: DNS_RR_DATA::NS { + data: DNSResourceRecordData::NS { nsdname: "ns6.nease.net.".to_string(), }, }), ], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163mx03.mxmail.netease.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 442, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "220.181.12.119".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163mx01.mxmail.netease.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 442, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "220.181.12.117".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163mx02.mxmail.netease.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 442, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "220.181.12.118".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "163mx00.mxmail.netease.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 442, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "220.181.12.180".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns6.nease.net.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 5583, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "54.228.156.72".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns1.nease.net.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 32644, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "42.186.35.222".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns4.nease.net.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 51974, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "103.72.16.81".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns3.nease.net.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 41374, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "220.181.36.234".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns2.166.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 73500, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "103.71.201.3".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns5.nease.net.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 34614, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "121.195.179.18".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns8.166.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 64949, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "18.182.82.158".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns8.166.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 64949, rd_length: 4, size: 12, }, - data: DNS_RR_DATA::A { + data: DNSResourceRecordData::A { address: "44.228.163.69".to_string(), }, }), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -3243,24 +2901,24 @@ mod tests { 0x00, 0x00, /* Data length: 0 */ ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0xb613, - qr: DNS_HDR_QR::Response, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Response, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: true, ad: false, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 1, ns_count: 1, ar_count: 1, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 15), "www.baidu.com.".to_string()), ((31, 2), "www.baidu.com.".to_string()), @@ -3271,38 +2929,38 @@ mod tests { ((115, 1), ".".to_string()), ], }, - qd: vec![DNS_QUESTION_SECTION { + qd: vec![DNSQuestionSection { qname: "www.baidu.com.".to_string(), - qtype: DNS_QTYPE::SOA, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::SOA, + qclass: DNSQclass::Internet, size: 19, }], an: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "www.baidu.com.".to_string(), - rr_type: DNS_QTYPE::CNAME, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::CNAME, + rr_class: DNSQclass::Internet, ttl: 221, rd_length: 15, size: 12, }, - data: DNS_RR_DATA::CNAME { + data: DNSResourceRecordData::CNAME { cname: "www.a.shifen.com.".to_string(), }, }), ], ns: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "a.shifen.com.".to_string(), - rr_type: DNS_QTYPE::SOA, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::SOA, + rr_class: DNSQclass::Internet, ttl: 296, rd_length: 45, size: 12, }, - data: DNS_RR_DATA::SOA { + data: DNSResourceRecordData::SOA { mname: "ns1.a.shifen.com.".to_string(), rname: "baidu_dns_master.baidu.com.".to_string(), serial: 2211240019, @@ -3314,26 +2972,26 @@ mod tests { }), ], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11, }, - data: DNS_RR_DATA::Other { - qtype: DNS_QTYPE::OPT, + data: DNSResourceRecordData::Other { + qtype: DNSQtype::OPT, data: vec![], }, }), ], }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } @@ -3554,24 +3212,24 @@ mod tests { 0x00, ]; - let expectation = DNS_MESSAGE { - header: DNS_HEADER { + let expectation = DNSMessage { + header: DNSHeader { id: 0x6721, - qr: DNS_HDR_QR::Response, - op_code: DNS_HDR_OPCODE::Query, + qr: DNSHeaderQR::Response, + op_code: DNSHeaderOpcode::Query, aa: false, tc: false, rd: true, ra: true, ad: false, cd: false, - r_code: DNS_HDR_RCODE::NoError, + r_code: DNSHeaderRcode::NoError, qd_count: 1, an_count: 3, ns_count: 5, ar_count: 10, }, - labels: DNS_LABEL_TABLE { + labels: DNSLabelTable { labels: vec![ ((12, 11), "baidu.com.".to_string()), ((27, 2), "baidu.com.".to_string()), @@ -3600,231 +3258,231 @@ mod tests { ] }, qd: vec![ - DNS_QUESTION_SECTION { + DNSQuestionSection { qname: "baidu.com.".to_string(), - qtype: DNS_QTYPE::TXT, - qclass: DNS_QCLASS::Internet, + qtype: DNSQtype::TXT, + qclass: DNSQclass::Internet, size: 15 } ], an: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::TXT, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::TXT, + rr_class: DNSQclass::Internet, ttl: 7200, rd_length: 75, size: 12 }, - data:DNS_RR_DATA::TXT{ txtdata:vec!["_globalsign-domain-verification=qjb28W2jJSrWj04NHpB0CvgK9tle5JkOq-EcyWBgnE".to_string()]},} + data:DNSResourceRecordData::TXT{ txtdata:vec!["_globalsign-domain-verification=qjb28W2jJSrWj04NHpB0CvgK9tle5JkOq-EcyWBgnE".to_string()]},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::TXT, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::TXT, + rr_class: DNSQclass::Internet, ttl: 7200, rd_length: 69, size: 12 }, - data:DNS_RR_DATA::TXT{ txtdata:vec!["google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM".to_string()]},} + data:DNSResourceRecordData::TXT{ txtdata:vec!["google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM".to_string()]},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::TXT, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::TXT, + rr_class: DNSQclass::Internet, ttl: 7200, rd_length: 113, size: 12 }, - data: DNS_RR_DATA::TXT{ txtdata:vec!["v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com include:spf4.baidu.com a mx ptr -all".to_string()]}, } + data: DNSResourceRecordData::TXT{ txtdata:vec!["v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com include:spf4.baidu.com a mx ptr -all".to_string()]}, } )], ns: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 53898, rd_length: 6, size: 12 }, - data: DNS_RR_DATA::NS{nsdname: "ns7.baidu.com.".to_string()},} + data: DNSResourceRecordData::NS{nsdname: "ns7.baidu.com.".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 53898, rd_length: 6, size: 12 }, - data: DNS_RR_DATA::NS{nsdname: "dns.baidu.com.".to_string()},} + data: DNSResourceRecordData::NS{nsdname: "dns.baidu.com.".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 53898, rd_length: 6, size: 12 }, - data: DNS_RR_DATA::NS{nsdname: "ns3.baidu.com.".to_string()},} + data: DNSResourceRecordData::NS{nsdname: "ns3.baidu.com.".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 53898, rd_length: 6, size: 12 }, - data: DNS_RR_DATA:: NS{ nsdname: "ns2.baidu.com.".to_string()},} + data: DNSResourceRecordData:: NS{ nsdname: "ns2.baidu.com.".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "baidu.com.".to_string(), - rr_type: DNS_QTYPE::NS, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::NS, + rr_class: DNSQclass::Internet, ttl: 53898, rd_length: 6, size: 12 }, - data: DNS_RR_DATA::NS{nsdname: "ns4.baidu.com.".to_string()},} + data: DNSResourceRecordData::NS{nsdname: "ns4.baidu.com.".to_string()},} ) ], ar: vec![ - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns3.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 25922, rd_length: 4, size: 12 }, - data: DNS_RR_DATA::A{address: "36.152.45.193".to_string()}, + data: DNSResourceRecordData::A{address: "36.152.45.193".to_string()}, } ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns3.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 25922, rd_length: 4, size: 12 }, - data: DNS_RR_DATA::A{address: "112.80.248.64".to_string()}, + data: DNSResourceRecordData::A{address: "112.80.248.64".to_string()}, } ), ( - DNS_RR_SECTION { - hdr: DNS_RR_HDR { + DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns2.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 23967, rd_length: 4, size: 12 }, - data: DNS_RR_DATA::A{address: "220.181.33.31".to_string()}, + data: DNSResourceRecordData::A{address: "220.181.33.31".to_string()}, } ), ( - DNS_RR_SECTION { - hdr: DNS_RR_HDR { + DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns7.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 50794, rd_length: 4, size: 12 }, - data: DNS_RR_DATA::A{address: "180.76.76.92".to_string()}, + data: DNSResourceRecordData::A{address: "180.76.76.92".to_string()}, } ), ( - DNS_RR_SECTION { - hdr: DNS_RR_HDR { + DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns4.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 58399, rd_length: 4, size: 12 }, - data:DNS_RR_DATA::A{address: "14.215.178.80".to_string()}, + data:DNSResourceRecordData::A{address: "14.215.178.80".to_string()}, } ), ( - DNS_RR_SECTION { - hdr: DNS_RR_HDR { + DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns4.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 58399, rd_length: 4, size: 12 }, - data:DNS_RR_DATA::A{address: "111.45.3.226".to_string()},} + data:DNSResourceRecordData::A{address: "111.45.3.226".to_string()},} ), ( - DNS_RR_SECTION { - hdr: DNS_RR_HDR { + DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "dns.baidu.com.".to_string(), - rr_type: DNS_QTYPE::A, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::A, + rr_class: DNSQclass::Internet, ttl: 114889, rd_length: 4, size: 12 }, - data: DNS_RR_DATA::A{address: "110.242.68.134".to_string()},} + data: DNSResourceRecordData::A{address: "110.242.68.134".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns7.baidu.com.".to_string(), - rr_type: DNS_QTYPE::AAAA, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::AAAA, + rr_class: DNSQclass::Internet, ttl: 80536, rd_length: 16, size: 12 }, - data: DNS_RR_DATA::AAAA{ address : "240E:0940:0603:0004:0000:00FF:B01B:589A".to_string()},} + data: DNSResourceRecordData::AAAA{ address : "240E:0940:0603:0004:0000:00FF:B01B:589A".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: "ns7.baidu.com.".to_string(), - rr_type: DNS_QTYPE::AAAA, - rr_class: DNS_QCLASS::Internet, + rr_type: DNSQtype::AAAA, + rr_class: DNSQclass::Internet, ttl: 80536, rd_length: 16, size: 12 }, - data: DNS_RR_DATA::AAAA{ address: "240E:00BF:B801:1002:0000:00FF:B024:26DE".to_string()},} + data: DNSResourceRecordData::AAAA{ address: "240E:00BF:B801:1002:0000:00FF:B024:26DE".to_string()},} ), - (DNS_RR_SECTION { - hdr: DNS_RR_HDR { + (DNSResourceRecordSection { + hdr: DNSResourceRecordHeader { qname: ".".to_string(), - rr_type: DNS_QTYPE::OPT, - rr_class: DNS_QCLASS::Other(4096), + rr_type: DNSQtype::OPT, + rr_class: DNSQclass::Other(4096), ttl: 0, rd_length: 0, size: 11 }, - data:DNS_RR_DATA::Other{ qtype: DNS_QTYPE::OPT,data:vec![]},} + data:DNSResourceRecordData::Other{ qtype: DNSQtype::OPT,data:vec![]},} ) ] }; - assert_eq!(DNS_MESSAGE::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(DNSMessage::decode(&bytes), Ok((LAST_SLICE, expectation))); - match DNS_MESSAGE::decode(&bytes) { + match DNSMessage::decode(&bytes) { Ok((left, message)) => { println!("{:?}; left {:?}", message, left); } diff --git a/src/protocol/ethernet.rs b/src/protocol/ethernet.rs index 6c42c2d..d3b9fc0 100644 --- a/src/protocol/ethernet.rs +++ b/src/protocol/ethernet.rs @@ -10,7 +10,7 @@ use nom::IResult; pub struct MacAddress(pub [u8; 6]); #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum EtherType { +pub enum EthType { LANMIN, // 802.3 Min data length LANMAX, // 802.3 Max data length IPv4, // Internet Protocol version 4 (IPv4) [RFC7042] @@ -62,17 +62,17 @@ pub enum EtherType { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct EthernetFrame { +pub struct EthHeader { pub source_mac: MacAddress, pub dest_mac: MacAddress, - pub ether_type: EtherType, + pub ether_type: EthType, } /****************************************************************************** * API ******************************************************************************/ -impl From for EtherType { +impl From for EthType { fn from(raw: u16) -> Self { match raw { 0x002E => Self::LANMIN, @@ -127,15 +127,17 @@ impl From for EtherType { } } -impl EtherType { - pub fn decode(input: &[u8]) -> IResult<&[u8], EtherType> { +impl Decode for EthType { + type Iterm = EthType; + fn decode(input: &[u8]) -> IResult<&[u8], EthType> { let (input, ether_type) = number::streaming::be_u16(input)?; Ok((input, ether_type.into())) } } -impl MacAddress { +impl Decode for MacAddress { + type Iterm = MacAddress; fn decode(input: &[u8]) -> IResult<&[u8], MacAddress> { let (input, mac_address) = nom::bytes::streaming::take(6u8)(input)?; @@ -143,16 +145,16 @@ impl MacAddress { } } -impl Decode for EthernetFrame { - type Iterm = EthernetFrame; - fn decode(input: &[u8]) -> IResult<&[u8], EthernetFrame> { +impl Decode for EthHeader { + type Iterm = EthHeader; + fn decode(input: &[u8]) -> IResult<&[u8], EthHeader> { let (input, dest_mac) = MacAddress::decode(input)?; let (input, source_mac) = MacAddress::decode(input)?; - let (input, ether_type) = EtherType::decode(input)?; + let (input, ether_type) = EthType::decode(input)?; Ok(( input, - EthernetFrame { + EthHeader { source_mac, dest_mac, ether_type, @@ -167,7 +169,7 @@ impl Decode for EthernetFrame { #[cfg(test)] mod tests { - use super::{EtherType, EthernetFrame, MacAddress}; + use super::{EthHeader, EthType, MacAddress}; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -192,16 +194,16 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = EthernetFrame { + let expectation = EthHeader { source_mac: MacAddress([0x3c, 0xa6, 0xf6, 0x0a, 0xc5, 0xea]), dest_mac: MacAddress([0x4c, 0xbc, 0x98, 0x08, 0x02, 0xbe]), - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }; - assert_eq!(EthernetFrame::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(EthHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let ethernet = EthernetFrame::decode(&bytes); + let ethernet = EthHeader::decode(&bytes); if let Ok((payload, header)) = ethernet { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/grev0.rs b/src/protocol/grev0.rs index fd368b9..8ef5a20 100644 --- a/src/protocol/grev0.rs +++ b/src/protocol/grev0.rs @@ -1,5 +1,5 @@ use crate::protocol::codec::Decode; -use crate::protocol::ethernet::EtherType; +use crate::protocol::ethernet::EthType; use nom::bits; use nom::bytes; use nom::error::Error; @@ -49,7 +49,7 @@ pub struct SourceRouteEntry { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Grev0Header { +pub struct GREv0Header { pub flag_checksum: bool, pub flag_routing: bool, pub flag_key: bool, @@ -58,7 +58,7 @@ pub struct Grev0Header { pub recursion_control: u8, pub flags: u8, pub version: u8, - pub protocol_type: EtherType, + pub protocol_type: EthType, pub checksum: Option, pub offset: Option, pub key: Option, @@ -93,9 +93,9 @@ fn source_route_entry_decode(input: &[u8]) -> IResult<&[u8], SourceRouteEntry> { )) } -impl Decode for Grev0Header { - type Iterm = Grev0Header; - fn decode(input: &[u8]) -> IResult<&[u8], Grev0Header> { +impl Decode for GREv0Header { + type Iterm = GREv0Header; + fn decode(input: &[u8]) -> IResult<&[u8], GREv0Header> { let ( input, ( @@ -126,7 +126,7 @@ impl Decode for Grev0Header { ))); } - let (input, protocol_type) = EtherType::decode(input)?; + let (input, protocol_type) = EthType::decode(input)?; let (input, checksum) = match (flag_checksum, flag_routing) { (0, 0) => (input, None), (_, _) => number::streaming::be_u16(input).map(|(i, l)| (i, Some(l)))?, @@ -163,7 +163,7 @@ impl Decode for Grev0Header { Ok(( input, - Grev0Header { + GREv0Header { flag_checksum: flag_checksum == 1, flag_routing: flag_routing == 1, flag_key: flag_key == 1, @@ -189,10 +189,10 @@ impl Decode for Grev0Header { #[cfg(test)] mod tests { - use super::Grev0Header; + use super::GREv0Header; use super::SourceRouteEntry; use crate::protocol::codec::Decode; - use crate::protocol::ethernet::EtherType; + use crate::protocol::ethernet::EthType; const LAST_SLICE: &'static [u8] = &[0xff]; #[test] @@ -217,7 +217,7 @@ mod tests { 0x20, 0x00, 0x08, 0x00, 0x00, 0x00, 0x03, 0x84, 0xff, /* Payload */ ]; - let expectation = Grev0Header { + let expectation = GREv0Header { flag_checksum: false, flag_routing: false, flag_key: true, @@ -226,7 +226,7 @@ mod tests { recursion_control: 0, flags: 0, version: 0, - protocol_type: EtherType::IPv4, + protocol_type: EthType::IPv4, checksum: None, offset: None, key: Some(0x384), @@ -234,10 +234,10 @@ mod tests { routing: None, }; - assert_eq!(Grev0Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(GREv0Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Grev0Header::decode(&bytes); + let result = GREv0Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -286,7 +286,7 @@ mod tests { 0x00, 0x00, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = Grev0Header { + let expectation = GREv0Header { flag_checksum: true, flag_routing: true, flag_key: false, @@ -295,7 +295,7 @@ mod tests { recursion_control: 0, flags: 0, version: 0, - protocol_type: EtherType::IPv4, + protocol_type: EthType::IPv4, checksum: Some(0x0000), offset: Some(44), key: None, @@ -321,10 +321,10 @@ mod tests { ]), }; - assert_eq!(Grev0Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(GREv0Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Grev0Header::decode(&bytes); + let result = GREv0Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/grev1.rs b/src/protocol/grev1.rs index 4bf551f..a16bab6 100644 --- a/src/protocol/grev1.rs +++ b/src/protocol/grev1.rs @@ -1,5 +1,5 @@ use crate::protocol::codec::Decode; -use crate::protocol::ethernet::EtherType; +use crate::protocol::ethernet::EthType; use nom::bits; use nom::error::Error; use nom::number; @@ -29,7 +29,7 @@ use nom::IResult; */ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Grev1Header { +pub struct GREv1Header { pub flag_checksum: bool, pub flag_routing: bool, pub flag_key: bool, @@ -39,7 +39,7 @@ pub struct Grev1Header { pub flag_acknowledgment: bool, pub flags: u8, pub version: u8, - pub protocol_type: EtherType, + pub protocol_type: EthType, pub key_payload_length: u16, pub key_call_id: u16, pub sequence_number: Option, @@ -50,9 +50,9 @@ pub struct Grev1Header { * API ******************************************************************************/ -impl Decode for Grev1Header { - type Iterm = Grev1Header; - fn decode(input: &[u8]) -> IResult<&[u8], Grev1Header> { +impl Decode for GREv1Header { + type Iterm = GREv1Header; + fn decode(input: &[u8]) -> IResult<&[u8], GREv1Header> { let ( input, ( @@ -85,7 +85,7 @@ impl Decode for Grev1Header { ))); } - let (input, protocol_type) = EtherType::decode(input)?; + let (input, protocol_type) = EthType::decode(input)?; let (input, key_payload_length) = number::streaming::be_u16(input)?; let (input, key_call_id) = number::streaming::be_u16(input)?; let (input, sequence_number) = match flag_sequence { @@ -99,7 +99,7 @@ impl Decode for Grev1Header { Ok(( input, - Grev1Header { + GREv1Header { flag_checksum: flag_checksum == 1, flag_routing: flag_routing == 1, flag_key: flag_key == 1, @@ -125,9 +125,9 @@ impl Decode for Grev1Header { #[cfg(test)] mod tests { - use super::Grev1Header; + use super::GREv1Header; use crate::protocol::codec::Decode; - use crate::protocol::ethernet::EtherType; + use crate::protocol::ethernet::EthType; const LAST_SLICE: &'static [u8] = &[0xff]; #[test] @@ -156,7 +156,7 @@ mod tests { 0x3a, 0x76, 0xff, /* Payload */ ]; - let expectation = Grev1Header { + let expectation = GREv1Header { flag_checksum: false, flag_routing: false, flag_key: true, @@ -166,17 +166,17 @@ mod tests { flag_acknowledgment: true, flags: 0, version: 1, - protocol_type: EtherType::PPP, + protocol_type: EthType::PPP, key_payload_length: 103, key_call_id: 6016, sequence_number: Some(430001), acknowledgment_number: Some(539254), }; - assert_eq!(Grev1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(GREv1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Grev1Header::decode(&bytes); + let result = GREv1Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/gtpv1.rs b/src/protocol/gtpv1.rs index a38da21..9acfc0f 100644 --- a/src/protocol/gtpv1.rs +++ b/src/protocol/gtpv1.rs @@ -11,21 +11,21 @@ use nom::IResult; ******************************************************************************/ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Gtpv1Option { +pub struct GTPv1Option { pub sequence_number: u16, // 16bit pub npdu_number: u8, // 8bit pub next_header_type: u8, // 8bit } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Gtpv1ExtensionHeader { +pub struct GTPv1ExtensionHeader { pub length: u8, // 8bit (单位4字节,包括长度/内容/下一扩展消息头字段) pub contents: Vec, pub next_header_type: u8, // 8bit } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Gtpv1Header { +pub struct GTPv1Header { pub version: u8, // 3bit 1: GTPv1 pub protocol_type: u8, // 1bit pub reserved: u8, // 1bit @@ -38,8 +38,8 @@ pub struct Gtpv1Header { pub teid: u32, // 32bit // extension_header_flag/sequence_number_flag/npdu_number_flag任意一个取值为1时options字段才存在 - pub options: Option, - pub extensions: Vec, + pub options: Option, + pub extensions: Vec, } /****************************************************************************** @@ -57,14 +57,14 @@ fn bit_decode(input: &[u8]) -> IResult<&[u8], (u8, u8, u8, u8, u8, u8)> { )))(input) } -fn option_decode(input: &[u8]) -> IResult<&[u8], Gtpv1Option> { +fn option_decode(input: &[u8]) -> IResult<&[u8], GTPv1Option> { let (input, sequence_number) = number::streaming::be_u16(input)?; let (input, npdu_number) = number::streaming::be_u8(input)?; let (input, next_header_type) = number::streaming::be_u8(input)?; Ok(( input, - Gtpv1Option { + GTPv1Option { sequence_number, npdu_number, next_header_type, @@ -72,7 +72,7 @@ fn option_decode(input: &[u8]) -> IResult<&[u8], Gtpv1Option> { )) } -fn extension_decode(input: &[u8]) -> IResult<&[u8], Gtpv1ExtensionHeader> { +fn extension_decode(input: &[u8]) -> IResult<&[u8], GTPv1ExtensionHeader> { let (input, length) = number::streaming::be_u8(input)?; if length * 4 < 2 { return Err(nom::Err::Incomplete(nom::Needed::new( @@ -83,7 +83,7 @@ fn extension_decode(input: &[u8]) -> IResult<&[u8], Gtpv1ExtensionHeader> { let (input, next_header_type) = number::streaming::be_u8(input)?; Ok(( input, - Gtpv1ExtensionHeader { + GTPv1ExtensionHeader { length, contents: contents.to_vec(), next_header_type, @@ -91,9 +91,9 @@ fn extension_decode(input: &[u8]) -> IResult<&[u8], Gtpv1ExtensionHeader> { )) } -impl Decode for Gtpv1Header { - type Iterm = Gtpv1Header; - fn decode(input: &[u8]) -> IResult<&[u8], Gtpv1Header> { +impl Decode for GTPv1Header { + type Iterm = GTPv1Header; + fn decode(input: &[u8]) -> IResult<&[u8], GTPv1Header> { let ( input, ( @@ -140,7 +140,7 @@ impl Decode for Gtpv1Header { Ok(( remain, - Gtpv1Header { + GTPv1Header { version, protocol_type, reserved, @@ -163,8 +163,8 @@ impl Decode for Gtpv1Header { #[cfg(test)] mod tests { - use super::Gtpv1ExtensionHeader; - use super::Gtpv1Header; + use super::GTPv1ExtensionHeader; + use super::GTPv1Header; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -188,7 +188,7 @@ mod tests { 0x30, 0xff, 0x00, 0x40, 0x1f, 0x54, 0xd4, 0xb5, 0xff, /* Payload */ ]; - let expectation = Gtpv1Header { + let expectation = GTPv1Header { version: 1, protocol_type: 1, reserved: 0, @@ -202,10 +202,10 @@ mod tests { extensions: vec![], }; - assert_eq!(Gtpv1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(GTPv1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Gtpv1Header::decode(&bytes); + let result = GTPv1Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -246,7 +246,7 @@ mod tests { 0x01, 0x00, 0xff, /* Payload */ ]; - let expectation = Gtpv1Header { + let expectation = GTPv1Header { version: 1, protocol_type: 1, reserved: 0, @@ -256,22 +256,22 @@ mod tests { message_type: 0xff, message_length: 48, teid: 476419836, - options: Some(super::Gtpv1Option { + options: Some(super::GTPv1Option { sequence_number: 26425, npdu_number: 0, next_header_type: 0x85, }), - extensions: vec![Gtpv1ExtensionHeader { + extensions: vec![GTPv1ExtensionHeader { length: 1, contents: vec![0x10, 0x01], next_header_type: 0x00, }], }; - assert_eq!(Gtpv1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(GTPv1Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Gtpv1Header::decode(&bytes); + let result = GTPv1Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/http.rs b/src/protocol/http.rs index e8d33f0..d05c59e 100644 --- a/src/protocol/http.rs +++ b/src/protocol/http.rs @@ -2,22 +2,22 @@ use crate::protocol::codec::Decode; use nom::IResult; #[derive(Clone, Debug, PartialEq, Eq)] -pub struct HttpMessage { +pub struct HTTPMessage { // TODO } -impl HttpMessage { - pub fn new() -> HttpMessage { - HttpMessage { +impl HTTPMessage { + pub fn new() -> HTTPMessage { + HTTPMessage { // TODO } } } -impl Decode for HttpMessage { - type Iterm = HttpMessage; - fn decode(input: &[u8]) -> IResult<&[u8], HttpMessage> { - let message = HttpMessage::new(); +impl Decode for HTTPMessage { + type Iterm = HTTPMessage; + fn decode(input: &[u8]) -> IResult<&[u8], HTTPMessage> { + let message = HTTPMessage::new(); // TODO Ok((input, message)) } diff --git a/src/protocol/icmp.rs b/src/protocol/icmp.rs index 314c3d7..fee8749 100644 --- a/src/protocol/icmp.rs +++ b/src/protocol/icmp.rs @@ -7,7 +7,7 @@ use nom::IResult; ******************************************************************************/ #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum IcmpType { +pub enum ICMPType { EchoReply, DestinationUnreachable, SourceQuench, @@ -28,8 +28,8 @@ pub enum IcmpType { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct IcmpHeader { - pub icmp_type: IcmpType, +pub struct ICMPHeader { + pub icmp_type: ICMPType, pub icmp_code: u8, pub icmp_checksum: u16, pub icmp_extended: Vec, @@ -39,33 +39,33 @@ pub struct IcmpHeader { * API ******************************************************************************/ -impl From for IcmpType { +impl From for ICMPType { fn from(raw: u8) -> Self { match raw { - 0 => IcmpType::EchoReply, - 3 => IcmpType::DestinationUnreachable, - 4 => IcmpType::SourceQuench, - 5 => IcmpType::Redirect, - 8 => IcmpType::EchoRequest, - 9 => IcmpType::RouterAdvertisement, - 10 => IcmpType::RouterSolicitation, - 11 => IcmpType::TimeExceeded, - 12 => IcmpType::ParameterProblem, - 13 => IcmpType::Timestamp, - 14 => IcmpType::TimestampReply, - 15 => IcmpType::InformationRequest, - 16 => IcmpType::InformationReply, - 17 => IcmpType::AddressMaskRequest, - 18 => IcmpType::AddressMaskReply, - 30 => IcmpType::Traceroute, - other => IcmpType::Other(other), + 0 => ICMPType::EchoReply, + 3 => ICMPType::DestinationUnreachable, + 4 => ICMPType::SourceQuench, + 5 => ICMPType::Redirect, + 8 => ICMPType::EchoRequest, + 9 => ICMPType::RouterAdvertisement, + 10 => ICMPType::RouterSolicitation, + 11 => ICMPType::TimeExceeded, + 12 => ICMPType::ParameterProblem, + 13 => ICMPType::Timestamp, + 14 => ICMPType::TimestampReply, + 15 => ICMPType::InformationRequest, + 16 => ICMPType::InformationReply, + 17 => ICMPType::AddressMaskRequest, + 18 => ICMPType::AddressMaskReply, + 30 => ICMPType::Traceroute, + other => ICMPType::Other(other), } } } -impl Decode for IcmpHeader { - type Iterm = IcmpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], IcmpHeader> { +impl Decode for ICMPHeader { + type Iterm = ICMPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], ICMPHeader> { let (input, icmp_type) = number::streaming::be_u8(input)?; let (input, icmp_code) = number::streaming::be_u8(input)?; let (input, icmp_checksum) = number::streaming::be_u16(input)?; @@ -73,7 +73,7 @@ impl Decode for IcmpHeader { Ok(( input, - IcmpHeader { + ICMPHeader { icmp_type: icmp_type.into(), icmp_code, icmp_checksum, @@ -89,8 +89,8 @@ impl Decode for IcmpHeader { #[cfg(test)] mod tests { - use super::IcmpHeader; - use crate::protocol::{codec::Decode, icmp::IcmpType}; + use super::ICMPHeader; + use crate::protocol::{codec::Decode, icmp::ICMPType}; const LAST_SLICE: &'static [u8] = &[ 0x96, 0xb5, 0xe9, 0x5e, 0x00, 0x00, 0x00, 0x00, 0xac, 0xe6, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, @@ -125,17 +125,17 @@ mod tests { 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, ]; - let expectation = IcmpHeader { - icmp_type: IcmpType::EchoRequest, + let expectation = ICMPHeader { + icmp_type: ICMPType::EchoRequest, icmp_code: 0, icmp_checksum: 0xab05, icmp_extended: vec![0x5f, 0x2b, 0x00, 0x01], }; - assert_eq!(IcmpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(ICMPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = IcmpHeader::decode(&bytes); + let result = ICMPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/icmpv6.rs b/src/protocol/icmpv6.rs index 837f2e6..26728f4 100644 --- a/src/protocol/icmpv6.rs +++ b/src/protocol/icmpv6.rs @@ -7,7 +7,7 @@ use nom::IResult; ******************************************************************************/ #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Icmpv6Type { +pub enum ICMPv6Type { DestinationUnreachable, PacketTooBig, TimeExceeded, @@ -49,8 +49,8 @@ pub enum Icmpv6Type { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Icmpv6Header { - pub icmp_type: Icmpv6Type, +pub struct ICMPv6Header { + pub icmp_type: ICMPv6Type, pub icmp_code: u8, pub checksum: u16, } @@ -59,61 +59,61 @@ pub struct Icmpv6Header { * API ******************************************************************************/ -impl From for Icmpv6Type { +impl From for ICMPv6Type { fn from(raw: u8) -> Self { match raw { - 1 => Icmpv6Type::DestinationUnreachable, - 2 => Icmpv6Type::PacketTooBig, - 3 => Icmpv6Type::TimeExceeded, - 4 => Icmpv6Type::ParameterProblem, - 128 => Icmpv6Type::EchoRequest, - 129 => Icmpv6Type::EchoReply, - 130 => Icmpv6Type::MulticastListenerQuery, - 131 => Icmpv6Type::MulticastListenerReport, - 132 => Icmpv6Type::MulticastListenerDone, - 133 => Icmpv6Type::RouterSolicitation, - 134 => Icmpv6Type::RouterAdvertisement, - 135 => Icmpv6Type::NeighborSolicitation, - 136 => Icmpv6Type::NeighborAdvertisement, - 137 => Icmpv6Type::RedirectMessage, - 138 => Icmpv6Type::RouterRenumbering, - 139 => Icmpv6Type::NodeInformationQuery, - 140 => Icmpv6Type::NodeInformationResponse, - 141 => Icmpv6Type::InverseNeighborDiscoverySolicitation, - 142 => Icmpv6Type::InverseNeighborDiscoveryAdvertisement, - 143 => Icmpv6Type::Version2MulticastListenerReport, - 144 => Icmpv6Type::HomeAgentAddressDiscoveryRequest, - 145 => Icmpv6Type::HomeAgentAddressDiscoveryReply, - 146 => Icmpv6Type::MobilePrefixSolicitation, - 147 => Icmpv6Type::MobilePrefixAdvertisement, - 148 => Icmpv6Type::CertificationPathSolicitation, - 149 => Icmpv6Type::CertificationPathAdvertisement, - 151 => Icmpv6Type::MulticastRouterAdvertisement, - 152 => Icmpv6Type::MulticastRouterSolicitation, - 153 => Icmpv6Type::MulticastRouterTermination, - 154 => Icmpv6Type::Fmipv6Messages, - 155 => Icmpv6Type::RplControlMessage, - 156 => Icmpv6Type::Ilnpv6LocatorUpdateMessage, - 157 => Icmpv6Type::DuplicateAddressRequest, - 158 => Icmpv6Type::DuplicateAddressConfirmation, - 159 => Icmpv6Type::MplControlMessage, - 160 => Icmpv6Type::ExtendedEchoRequest, - 161 => Icmpv6Type::ExtendedEchoReply, - other => Icmpv6Type::Other(other), + 1 => ICMPv6Type::DestinationUnreachable, + 2 => ICMPv6Type::PacketTooBig, + 3 => ICMPv6Type::TimeExceeded, + 4 => ICMPv6Type::ParameterProblem, + 128 => ICMPv6Type::EchoRequest, + 129 => ICMPv6Type::EchoReply, + 130 => ICMPv6Type::MulticastListenerQuery, + 131 => ICMPv6Type::MulticastListenerReport, + 132 => ICMPv6Type::MulticastListenerDone, + 133 => ICMPv6Type::RouterSolicitation, + 134 => ICMPv6Type::RouterAdvertisement, + 135 => ICMPv6Type::NeighborSolicitation, + 136 => ICMPv6Type::NeighborAdvertisement, + 137 => ICMPv6Type::RedirectMessage, + 138 => ICMPv6Type::RouterRenumbering, + 139 => ICMPv6Type::NodeInformationQuery, + 140 => ICMPv6Type::NodeInformationResponse, + 141 => ICMPv6Type::InverseNeighborDiscoverySolicitation, + 142 => ICMPv6Type::InverseNeighborDiscoveryAdvertisement, + 143 => ICMPv6Type::Version2MulticastListenerReport, + 144 => ICMPv6Type::HomeAgentAddressDiscoveryRequest, + 145 => ICMPv6Type::HomeAgentAddressDiscoveryReply, + 146 => ICMPv6Type::MobilePrefixSolicitation, + 147 => ICMPv6Type::MobilePrefixAdvertisement, + 148 => ICMPv6Type::CertificationPathSolicitation, + 149 => ICMPv6Type::CertificationPathAdvertisement, + 151 => ICMPv6Type::MulticastRouterAdvertisement, + 152 => ICMPv6Type::MulticastRouterSolicitation, + 153 => ICMPv6Type::MulticastRouterTermination, + 154 => ICMPv6Type::Fmipv6Messages, + 155 => ICMPv6Type::RplControlMessage, + 156 => ICMPv6Type::Ilnpv6LocatorUpdateMessage, + 157 => ICMPv6Type::DuplicateAddressRequest, + 158 => ICMPv6Type::DuplicateAddressConfirmation, + 159 => ICMPv6Type::MplControlMessage, + 160 => ICMPv6Type::ExtendedEchoRequest, + 161 => ICMPv6Type::ExtendedEchoReply, + other => ICMPv6Type::Other(other), } } } -impl Decode for Icmpv6Header { - type Iterm = Icmpv6Header; - fn decode(input: &[u8]) -> IResult<&[u8], Icmpv6Header> { +impl Decode for ICMPv6Header { + type Iterm = ICMPv6Header; + fn decode(input: &[u8]) -> IResult<&[u8], ICMPv6Header> { let (input, icmp_type) = number::streaming::be_u8(input)?; let (input, icmp_code) = number::streaming::be_u8(input)?; let (input, checksum) = number::streaming::be_u16(input)?; Ok(( input, - Icmpv6Header { - icmp_type: Icmpv6Type::from(icmp_type), + ICMPv6Header { + icmp_type: ICMPv6Type::from(icmp_type), icmp_code, checksum, }, @@ -127,8 +127,8 @@ impl Decode for Icmpv6Header { #[cfg(test)] mod tests { - use super::Icmpv6Header; - use super::Icmpv6Type; + use super::ICMPv6Header; + use super::ICMPv6Type; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[ 0x11, 0x0d, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, @@ -160,16 +160,16 @@ mod tests { 0x30, 0x31, 0x32, 0x33, ]; - let expectation = Icmpv6Header { - icmp_type: Icmpv6Type::EchoRequest, + let expectation = ICMPv6Header { + icmp_type: ICMPv6Type::EchoRequest, icmp_code: 0, checksum: 0x863c, }; - assert_eq!(Icmpv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(ICMPv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Icmpv6Header::decode(&bytes); + let result = ICMPv6Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/ipv4.rs b/src/protocol/ipv4.rs index a869031..61dce29 100644 --- a/src/protocol/ipv4.rs +++ b/src/protocol/ipv4.rs @@ -32,7 +32,7 @@ use std::net::Ipv4Addr; */ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Ipv4Header { +pub struct IPv4Header { pub version: u8, // 4 bit pub ihl: u8, // 4 bit pub tos: u8, @@ -71,9 +71,9 @@ fn address_v4_decode(input: &[u8]) -> IResult<&[u8], Ipv4Addr> { Ok((input, Ipv4Addr::from(<[u8; 4]>::try_from(ipv4).unwrap()))) } -impl Decode for Ipv4Header { - type Iterm = Ipv4Header; - fn decode(input: &[u8]) -> IResult<&[u8], Ipv4Header> { +impl Decode for IPv4Header { + type Iterm = 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)?; @@ -89,7 +89,7 @@ impl Decode for Ipv4Header { Ok(( input, - Ipv4Header { + IPv4Header { version: verihl.0, ihl: verihl.1 * 4, // verihl.1 * 32 / 8 tos, @@ -113,7 +113,7 @@ impl Decode for Ipv4Header { #[cfg(test)] mod tests { - use super::Ipv4Header; + use super::IPv4Header; use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use std::net::Ipv4Addr; @@ -160,7 +160,7 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = Ipv4Header { + let expectation = IPv4Header { version: 4, ihl: 20, tos: 0, @@ -175,10 +175,10 @@ mod tests { dest_address: Ipv4Addr::new(121, 14, 154, 93), }; - assert_eq!(Ipv4Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(IPv4Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Ipv4Header::decode(&bytes); + let result = IPv4Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/ipv6.rs b/src/protocol/ipv6.rs index c32d21b..639c023 100644 --- a/src/protocol/ipv6.rs +++ b/src/protocol/ipv6.rs @@ -60,14 +60,14 @@ use std::net::Ipv6Addr; */ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Ipv6Extension { +pub struct IPv6Extension { pub next_header: IPProtocol, pub ext_length: u8, // Extension total Length pub data: Vec, // Extension data length (ext_length - 2) } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Ipv6Header { +pub struct IPv6Header { pub version: u8, // 4 bit pub dsc: u8, // Differentiated Services Codepoint: 6 bit pub ecn: u8, // Explicit Congestion Notification: 2 bit @@ -77,7 +77,7 @@ pub struct Ipv6Header { pub hop_limit: u8, pub source_address: Ipv6Addr, pub dest_address: Ipv6Addr, - pub extensions: Vec, + pub extensions: Vec, } /****************************************************************************** @@ -97,7 +97,7 @@ fn address_v6_decode(input: &[u8]) -> IResult<&[u8], Ipv6Addr> { Ok((input, Ipv6Addr::from(<[u8; 16]>::try_from(ipv6).unwrap()))) } -fn extension_decode(input: &[u8], curr_proto: IPProtocol) -> IResult<&[u8], Ipv6Extension> { +fn extension_decode(input: &[u8], curr_proto: IPProtocol) -> IResult<&[u8], IPv6Extension> { let (input, next_header) = IPProtocol::decode(input)?; let (input, mut ext_length) = number::streaming::be_u8(input)?; @@ -117,7 +117,7 @@ fn extension_decode(input: &[u8], curr_proto: IPProtocol) -> IResult<&[u8], Ipv6 Ok(( input, - Ipv6Extension { + IPv6Extension { next_header, ext_length, data: data.to_vec(), @@ -125,9 +125,9 @@ fn extension_decode(input: &[u8], curr_proto: IPProtocol) -> IResult<&[u8], Ipv6 )) } -impl Decode for Ipv6Header { - type Iterm = Ipv6Header; - fn decode(input: &[u8]) -> IResult<&[u8], Ipv6Header> { +impl Decode for IPv6Header { + type Iterm = 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) = @@ -150,7 +150,7 @@ impl Decode for Ipv6Header { Ok(( remain, - Ipv6Header { + IPv6Header { version: ver_tc.0, dsc: (ver_tc.1 << 2) + ((tc_fl.0 & 0b1100) >> 2), ecn: tc_fl.0 & 0b11, @@ -172,8 +172,8 @@ impl Decode for Ipv6Header { #[cfg(test)] mod tests { - use super::Ipv6Extension; - use super::Ipv6Header; + use super::IPv6Extension; + use super::IPv6Header; use crate::protocol::codec::Decode; use crate::protocol::ip::IPProtocol; use std::net::Ipv6Addr; @@ -210,7 +210,7 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = Ipv6Header { + let expectation = IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -227,10 +227,10 @@ mod tests { extensions: Vec::new(), }; - assert_eq!(Ipv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(IPv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Ipv6Header::decode(&bytes); + let result = IPv6Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -280,7 +280,7 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = Ipv6Header { + let expectation = IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -294,17 +294,17 @@ mod tests { dest_address: Ipv6Addr::new( 0xff02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0016, ), - extensions: vec![Ipv6Extension { + extensions: vec![IPv6Extension { next_header: IPProtocol::ICMP6, ext_length: 8, data: vec![0x05, 0x02, 0x00, 0x00, 0x01, 0x00], }], }; - assert_eq!(Ipv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(IPv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Ipv6Header::decode(&bytes); + let result = IPv6Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -350,7 +350,7 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = Ipv6Header { + let expectation = IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -364,7 +364,7 @@ mod tests { dest_address: Ipv6Addr::new( 0x2200, 0x0000, 0x0000, 0x0240, 0x0002, 0x0000, 0x0000, 0x0004, ), - extensions: vec![Ipv6Extension { + extensions: vec![IPv6Extension { next_header: IPProtocol::ICMP6, ext_length: 24, data: vec![ @@ -374,10 +374,10 @@ mod tests { }], }; - assert_eq!(Ipv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(IPv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Ipv6Header::decode(&bytes); + let result = IPv6Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -458,7 +458,7 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = Ipv6Header { + let expectation = IPv6Header { version: 6, dsc: 0, ecn: 0, @@ -473,22 +473,22 @@ mod tests { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0003, ), extensions: vec![ - Ipv6Extension { + IPv6Extension { next_header: IPProtocol::IPV6DEST, ext_length: 8, data: vec![0x01, 0x04, 0x00, 0x00, 0x00, 0x00], }, - Ipv6Extension { + IPv6Extension { next_header: IPProtocol::IPV6ROUTING, ext_length: 8, data: vec![0x01, 0x04, 0x00, 0x00, 0x00, 0x00], }, - Ipv6Extension { + IPv6Extension { next_header: IPProtocol::IPV6FRAGMENT, ext_length: 8, data: vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00], }, - Ipv6Extension { + IPv6Extension { next_header: IPProtocol::Other(59), ext_length: 8, data: vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00], @@ -496,10 +496,10 @@ mod tests { ], }; - assert_eq!(Ipv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(IPv6Header::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = Ipv6Header::decode(&bytes); + let result = IPv6Header::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/l2tp.rs b/src/protocol/l2tp.rs index 0496f2b..ebed837 100644 --- a/src/protocol/l2tp.rs +++ b/src/protocol/l2tp.rs @@ -43,7 +43,7 @@ use nom::IResult; */ #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum AvpType { +pub enum L2TPAVPType { Message, Result, ProtocolVersion, @@ -138,26 +138,26 @@ pub enum AvpType { } #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum L2tpType { +pub enum L2TPType { Data, Control, } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct AvpHeader { +pub struct L2TPAVPHeader { pub mandatory: bool, pub hidden: bool, pub reserved: u8, pub length: u16, pub vendor_id: u16, - pub attribute_type: AvpType, + pub attribute_type: L2TPAVPType, pub attribute_value: Vec, } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct L2tpHeader { +pub struct L2TPHeader { pub flags: u16, - pub flag_type: L2tpType, + pub flag_type: L2TPType, pub flag_length: bool, pub flag_sequence: bool, pub flag_offset: bool, @@ -171,121 +171,121 @@ pub struct L2tpHeader { pub nr: Option, pub offset_size: Option, pub offset_pad: Option>, - pub avps: Option>, + pub avps: Option>, } /****************************************************************************** * API ******************************************************************************/ -impl From for L2tpType { +impl From for L2TPType { fn from(raw: bool) -> Self { match raw { - false => L2tpType::Data, - true => L2tpType::Control, + false => L2TPType::Data, + true => L2TPType::Control, } } } -impl From for AvpType { +impl From for L2TPAVPType { fn from(raw: u16) -> Self { match raw { - 0 => AvpType::Message, - 1 => AvpType::Result, - 2 => AvpType::ProtocolVersion, - 3 => AvpType::FramingCapabilites, - 4 => AvpType::BearerCapabilites, - 5 => AvpType::TieBreaker, - 6 => AvpType::FirmwareRevision, - 7 => AvpType::HostName, - 8 => AvpType::VendorName, - 9 => AvpType::AssignedTunnelId, - 10 => AvpType::ReceiveWindowSize, - 11 => AvpType::Challenge, - 12 => AvpType::CauseCode, - 13 => AvpType::ChallengeResponse, - 14 => AvpType::AssignedSessionId, - 15 => AvpType::CallSerialNumber, - 16 => AvpType::MinimumBps, - 17 => AvpType::MaximumBps, - 18 => AvpType::BearerType, - 19 => AvpType::FramingType, - 20 => AvpType::CalledNumber, - 21 => AvpType::CallingNumber, - 22 => AvpType::SubAddress, - 23 => AvpType::TxConnectSpeed, - 24 => AvpType::PhysicalChannelId, - 25 => AvpType::InitialReceivedLcpConfReq, - 26 => AvpType::LastSentLcpConfReq, - 27 => AvpType::LastReceivedLcpConfReq, - 28 => AvpType::ProxyAuthenType, - 29 => AvpType::ProxyAuthenName, - 30 => AvpType::ProxyAuthenChallenge, - 31 => AvpType::ProxyAuthenID, - 32 => AvpType::ProxyAuthenResponse, - 33 => AvpType::CallErrors, - 34 => AvpType::AcctAuthenType, - 35 => AvpType::AcctAuthenName, - 36 => AvpType::AcctAuthenChallenge, - 37 => AvpType::AcctAuthenID, - 38 => AvpType::AcctAuthenResponse, - 39 => AvpType::ChallengeControl, - 40 => AvpType::AcctEapType, - 41 => AvpType::AcctEapId, - 42 => AvpType::AcctEapResponse, - 44 => AvpType::CalledStationId, - 45 => AvpType::CallingStationId, - 46 => AvpType::NasIdentifier, - 47 => AvpType::ProxyState, - 48 => AvpType::LoginLatService, - 49 => AvpType::LoginLatNode, - 50 => AvpType::LoginLatGroup, - 51 => AvpType::FramedAppleTalkLink, - 52 => AvpType::FramedAppleTalkNetwork, - 53 => AvpType::FramedAppleTalkZone, - 61 => AvpType::AcctStatusType, - 62 => AvpType::AcctDelayTime, - 63 => AvpType::AcctInputOctets, - 64 => AvpType::AcctOutputOctets, - 65 => AvpType::AcctSessionId, - 66 => AvpType::AcctAuthentic, - 67 => AvpType::AcctSessionTime, - 68 => AvpType::AcctInputPackets, - 69 => AvpType::AcctOutputPackets, - 70 => AvpType::AcctTerminateCause, - 71 => AvpType::AcctMultiSessionId, - 72 => AvpType::AcctLinkCount, - 73 => AvpType::AcctEventTimeStamp, - 74 => AvpType::AcctTunnelConnection, - 75 => AvpType::AcctTunnelPacketsLost, - 87 => AvpType::NasPortId, - 88 => AvpType::FramedInterfaceId, - 97 => AvpType::FramedIpv6Prefix, - 98 => AvpType::LoginIpv6Host, - 99 => AvpType::FramedIpv6Route, - 100 => AvpType::FramedIpv6Pool, - 101 => AvpType::ErrorCause, - 102 => AvpType::EapMessage, - 103 => AvpType::MessageAuthenticator, - 104 => AvpType::TunnelPassword, - 105 => AvpType::ArapPassword, - 106 => AvpType::ArapFeatures, - 107 => AvpType::ArapZoneAccess, - 108 => AvpType::ArapSecurity, - 109 => AvpType::ArapSecurityData, - 110 => AvpType::PasswordRetry, - 111 => AvpType::Prompt, - 112 => AvpType::ConnectInfo, - 113 => AvpType::ConfigurationToken, - 114 => AvpType::EapMessage2, - 115 => AvpType::MessageAuthenticator2, - 116 => AvpType::ArapChallenge, - other => AvpType::Other(other), + 0 => L2TPAVPType::Message, + 1 => L2TPAVPType::Result, + 2 => L2TPAVPType::ProtocolVersion, + 3 => L2TPAVPType::FramingCapabilites, + 4 => L2TPAVPType::BearerCapabilites, + 5 => L2TPAVPType::TieBreaker, + 6 => L2TPAVPType::FirmwareRevision, + 7 => L2TPAVPType::HostName, + 8 => L2TPAVPType::VendorName, + 9 => L2TPAVPType::AssignedTunnelId, + 10 => L2TPAVPType::ReceiveWindowSize, + 11 => L2TPAVPType::Challenge, + 12 => L2TPAVPType::CauseCode, + 13 => L2TPAVPType::ChallengeResponse, + 14 => L2TPAVPType::AssignedSessionId, + 15 => L2TPAVPType::CallSerialNumber, + 16 => L2TPAVPType::MinimumBps, + 17 => L2TPAVPType::MaximumBps, + 18 => L2TPAVPType::BearerType, + 19 => L2TPAVPType::FramingType, + 20 => L2TPAVPType::CalledNumber, + 21 => L2TPAVPType::CallingNumber, + 22 => L2TPAVPType::SubAddress, + 23 => L2TPAVPType::TxConnectSpeed, + 24 => L2TPAVPType::PhysicalChannelId, + 25 => L2TPAVPType::InitialReceivedLcpConfReq, + 26 => L2TPAVPType::LastSentLcpConfReq, + 27 => L2TPAVPType::LastReceivedLcpConfReq, + 28 => L2TPAVPType::ProxyAuthenType, + 29 => L2TPAVPType::ProxyAuthenName, + 30 => L2TPAVPType::ProxyAuthenChallenge, + 31 => L2TPAVPType::ProxyAuthenID, + 32 => L2TPAVPType::ProxyAuthenResponse, + 33 => L2TPAVPType::CallErrors, + 34 => L2TPAVPType::AcctAuthenType, + 35 => L2TPAVPType::AcctAuthenName, + 36 => L2TPAVPType::AcctAuthenChallenge, + 37 => L2TPAVPType::AcctAuthenID, + 38 => L2TPAVPType::AcctAuthenResponse, + 39 => L2TPAVPType::ChallengeControl, + 40 => L2TPAVPType::AcctEapType, + 41 => L2TPAVPType::AcctEapId, + 42 => L2TPAVPType::AcctEapResponse, + 44 => L2TPAVPType::CalledStationId, + 45 => L2TPAVPType::CallingStationId, + 46 => L2TPAVPType::NasIdentifier, + 47 => L2TPAVPType::ProxyState, + 48 => L2TPAVPType::LoginLatService, + 49 => L2TPAVPType::LoginLatNode, + 50 => L2TPAVPType::LoginLatGroup, + 51 => L2TPAVPType::FramedAppleTalkLink, + 52 => L2TPAVPType::FramedAppleTalkNetwork, + 53 => L2TPAVPType::FramedAppleTalkZone, + 61 => L2TPAVPType::AcctStatusType, + 62 => L2TPAVPType::AcctDelayTime, + 63 => L2TPAVPType::AcctInputOctets, + 64 => L2TPAVPType::AcctOutputOctets, + 65 => L2TPAVPType::AcctSessionId, + 66 => L2TPAVPType::AcctAuthentic, + 67 => L2TPAVPType::AcctSessionTime, + 68 => L2TPAVPType::AcctInputPackets, + 69 => L2TPAVPType::AcctOutputPackets, + 70 => L2TPAVPType::AcctTerminateCause, + 71 => L2TPAVPType::AcctMultiSessionId, + 72 => L2TPAVPType::AcctLinkCount, + 73 => L2TPAVPType::AcctEventTimeStamp, + 74 => L2TPAVPType::AcctTunnelConnection, + 75 => L2TPAVPType::AcctTunnelPacketsLost, + 87 => L2TPAVPType::NasPortId, + 88 => L2TPAVPType::FramedInterfaceId, + 97 => L2TPAVPType::FramedIpv6Prefix, + 98 => L2TPAVPType::LoginIpv6Host, + 99 => L2TPAVPType::FramedIpv6Route, + 100 => L2TPAVPType::FramedIpv6Pool, + 101 => L2TPAVPType::ErrorCause, + 102 => L2TPAVPType::EapMessage, + 103 => L2TPAVPType::MessageAuthenticator, + 104 => L2TPAVPType::TunnelPassword, + 105 => L2TPAVPType::ArapPassword, + 106 => L2TPAVPType::ArapFeatures, + 107 => L2TPAVPType::ArapZoneAccess, + 108 => L2TPAVPType::ArapSecurity, + 109 => L2TPAVPType::ArapSecurityData, + 110 => L2TPAVPType::PasswordRetry, + 111 => L2TPAVPType::Prompt, + 112 => L2TPAVPType::ConnectInfo, + 113 => L2TPAVPType::ConfigurationToken, + 114 => L2TPAVPType::EapMessage2, + 115 => L2TPAVPType::MessageAuthenticator2, + 116 => L2TPAVPType::ArapChallenge, + other => L2TPAVPType::Other(other), } } } -fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { +fn avp_decode(input: &[u8]) -> IResult<&[u8], L2TPAVPHeader> { let (input, (mandatory, hidden, reserved, length)): (&[u8], (u8, u8, u8, u16)) = bits::bits::<_, _, Error<_>, _, _>(sequence::tuple(( bits::streaming::take(1u8), @@ -310,7 +310,7 @@ fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { Ok(( input, - AvpHeader { + L2TPAVPHeader { mandatory: mandatory != 0, hidden: hidden != 0, reserved, @@ -322,9 +322,9 @@ fn avp_decode(input: &[u8]) -> IResult<&[u8], AvpHeader> { )) } -impl Decode for L2tpHeader { - type Iterm = L2tpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], L2tpHeader> { +impl Decode for L2TPHeader { + type Iterm = L2TPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], L2TPHeader> { let (input, (flags, ver)) = bits::bits::<_, _, Error<_>, _, _>(sequence::tuple(( bits::streaming::take(12u8), bits::streaming::take(4u8), @@ -367,8 +367,8 @@ impl Decode for L2tpHeader { false => (input, None), }; let (input, avps) = match flag_type { - L2tpType::Data => (input, None), - L2tpType::Control => { + L2TPType::Data => (input, None), + L2TPType::Control => { let mut avps = Vec::new(); let mut input = input; loop { @@ -385,7 +385,7 @@ impl Decode for L2tpHeader { Ok(( input, - L2tpHeader { + L2TPHeader { flags, flag_type, flag_length, @@ -412,10 +412,10 @@ impl Decode for L2tpHeader { #[cfg(test)] mod tests { - use super::AvpHeader; - use super::AvpType; - use super::L2tpHeader; - use super::L2tpType; + use super::L2TPAVPHeader; + use super::L2TPAVPType; + use super::L2TPHeader; + use super::L2TPType; use crate::protocol::codec::Decode; use std::vec; const EMPTY_SLICE: &'static [u8] = &[]; @@ -442,9 +442,9 @@ mod tests { 0x40, 0x02, 0x00, 0x4e, 0x71, 0x46, 0x00, 0x02, 0xff, /* Payload */ ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x4002 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: true, flag_sequence: false, flag_offset: false, @@ -460,10 +460,10 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); + let result = L2TPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -491,9 +491,9 @@ mod tests { let bytes = [0x00, 0x02, 0x00, 0x0d, 0x00, 0x01, 0xff /* Payload */]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x0002 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: false, flag_sequence: false, flag_offset: false, @@ -509,10 +509,10 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); + let result = L2TPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -543,9 +543,9 @@ mod tests { 0x02, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0x0202 >> 4, - flag_type: L2tpType::Data, + flag_type: L2TPType::Data, flag_length: false, flag_sequence: false, flag_offset: true, @@ -561,10 +561,10 @@ mod tests { avps: None, }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); + let result = L2TPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -670,9 +670,9 @@ mod tests { 0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x08, ]; - let expectation = L2tpHeader { + let expectation = L2TPHeader { flags: 0xc802 >> 4, - flag_type: L2tpType::Control, + flag_type: L2TPType::Control, flag_length: true, flag_sequence: true, flag_offset: false, @@ -686,96 +686,96 @@ mod tests { offset_size: None, offset_pad: None, avps: Some(vec![ - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::Message, + attribute_type: L2TPAVPType::Message, attribute_value: vec![0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::ProtocolVersion, + attribute_type: L2TPAVPType::ProtocolVersion, attribute_value: vec![0x01, 0x00], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 10, vendor_id: 0, - attribute_type: AvpType::FramingCapabilites, + attribute_type: L2TPAVPType::FramingCapabilites, attribute_value: vec![0x00, 0x00, 0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 10, vendor_id: 0, - attribute_type: AvpType::BearerCapabilites, + attribute_type: L2TPAVPType::BearerCapabilites, attribute_value: vec![0x00, 0x00, 0x00, 0x00], }, - AvpHeader { + L2TPAVPHeader { mandatory: false, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::FirmwareRevision, + attribute_type: L2TPAVPType::FirmwareRevision, attribute_value: vec![0x06, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 18, vendor_id: 0, - attribute_type: AvpType::HostName, + attribute_type: L2TPAVPType::HostName, attribute_value: vec![ 0x49, 0x49, 0x45, 0x2d, 0x53, 0x4d, 0x2d, 0x54, 0x48, 0x49, 0x4e, 0x4b, ], }, - AvpHeader { + L2TPAVPHeader { mandatory: false, hidden: false, reserved: 0, length: 15, vendor_id: 0, - attribute_type: AvpType::VendorName, + attribute_type: L2TPAVPType::VendorName, attribute_value: vec![0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::AssignedTunnelId, + attribute_type: L2TPAVPType::AssignedTunnelId, attribute_value: vec![0x00, 0x01], }, - AvpHeader { + L2TPAVPHeader { mandatory: true, hidden: false, reserved: 0, length: 8, vendor_id: 0, - attribute_type: AvpType::ReceiveWindowSize, + attribute_type: L2TPAVPType::ReceiveWindowSize, attribute_value: vec![0x00, 0x08], }, ]), }; - assert_eq!(L2tpHeader::decode(&bytes), Ok((EMPTY_SLICE, expectation))); + assert_eq!(L2TPHeader::decode(&bytes), Ok((EMPTY_SLICE, expectation))); // example - let result = L2tpHeader::decode(&bytes); + let result = L2TPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/mpls.rs b/src/protocol/mpls.rs index e6a7a37..add686d 100644 --- a/src/protocol/mpls.rs +++ b/src/protocol/mpls.rs @@ -10,7 +10,7 @@ use nom::IResult; ******************************************************************************/ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct MplsHeader { +pub struct MPLSHeader { pub label: u32, pub experimental: u8, pub bottom_of_stack: bool, @@ -19,7 +19,7 @@ pub struct MplsHeader { #[derive(Clone, Debug, PartialEq, Eq)] // Ethernet pseudowire (PW) https://tools.ietf.org/html/rfc4448#section-3.1 -pub struct PwEthHeader { +pub struct PWEthHeader { pub control_word: u32, } @@ -36,14 +36,14 @@ fn bit_decode(input: &[u8]) -> IResult<&[u8], (u32, u8, u8, u8)> { )))(input) } -impl Decode for MplsHeader { - type Iterm = MplsHeader; - fn decode(input: &[u8]) -> IResult<&[u8], MplsHeader> { +impl Decode for MPLSHeader { + type Iterm = MPLSHeader; + fn decode(input: &[u8]) -> IResult<&[u8], MPLSHeader> { let (input, (label, experimental, bottom_of_stack, ttl)) = bit_decode(input)?; Ok(( input, - MplsHeader { + MPLSHeader { label, experimental, bottom_of_stack: bottom_of_stack == 1, @@ -53,12 +53,12 @@ impl Decode for MplsHeader { } } -impl Decode for PwEthHeader { - type Iterm = PwEthHeader; - fn decode(input: &[u8]) -> IResult<&[u8], PwEthHeader> { +impl Decode for PWEthHeader { + type Iterm = PWEthHeader; + fn decode(input: &[u8]) -> IResult<&[u8], PWEthHeader> { let (input, control_word) = number::streaming::be_u32(input)?; - Ok((input, PwEthHeader { control_word })) + Ok((input, PWEthHeader { control_word })) } } @@ -68,7 +68,7 @@ impl Decode for PwEthHeader { #[cfg(test)] mod tests { - use super::MplsHeader; + use super::MPLSHeader; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -89,28 +89,28 @@ mod tests { let bytes = [0x00, 0x01, 0x2a, 0xff, 0x00, 0x01, 0x0b, 0xff, 0xff]; - let expectation1 = MplsHeader { + let expectation1 = MPLSHeader { label: 18, experimental: 5, bottom_of_stack: false, ttl: 255, }; - let expectation2 = MplsHeader { + let expectation2 = MPLSHeader { label: 16, experimental: 5, bottom_of_stack: true, ttl: 255, }; - assert_eq!(MplsHeader::decode(&bytes), Ok((&bytes[4..], expectation1))); + assert_eq!(MPLSHeader::decode(&bytes), Ok((&bytes[4..], expectation1))); assert_eq!( - MplsHeader::decode(&bytes[4..]), + MPLSHeader::decode(&bytes[4..]), Ok((LAST_SLICE, expectation2)) ); // example let mut payload = &bytes[..]; - while let Ok((remain, header)) = MplsHeader::decode(payload) { + while let Ok((remain, header)) = MPLSHeader::decode(payload) { println!("return: {:?}, payload: {}", header, remain.len()); payload = remain; if header.bottom_of_stack { diff --git a/src/protocol/ppp.rs b/src/protocol/ppp.rs index e625cb6..558e9a6 100644 --- a/src/protocol/ppp.rs +++ b/src/protocol/ppp.rs @@ -8,7 +8,7 @@ use nom::IResult; // https://www.iana.org/assignments/ppp-numbers/ppp-numbers.xhtml #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum PppProtocol { +pub enum PPPProtocol { PAD, // Padding Protocol IPv4, // Internet Protocol version 4 (IPv4) IPv6, // Internet Protocol version 6 (IPv6) @@ -22,50 +22,50 @@ pub enum PppProtocol { // https://www.rfc-editor.org/rfc/rfc1661.html #[derive(Clone, Debug, PartialEq, Eq)] -pub struct PppHeader { +pub struct PPPHeader { pub address: u8, pub control: u8, - pub protocol: PppProtocol, + pub protocol: PPPProtocol, } /****************************************************************************** * API ******************************************************************************/ -impl From for PppProtocol { +impl From for PPPProtocol { fn from(raw: u16) -> Self { match raw { - 0x0001 => PppProtocol::PAD, - 0x0021 => PppProtocol::IPv4, - 0x0057 => PppProtocol::IPv6, - 0x8021 => PppProtocol::IPCP, - 0x80FD => PppProtocol::CCP, - 0xC021 => PppProtocol::LCP, - 0xC023 => PppProtocol::PAP, - 0xC223 => PppProtocol::CHAP, - other => PppProtocol::Other(other), + 0x0001 => PPPProtocol::PAD, + 0x0021 => PPPProtocol::IPv4, + 0x0057 => PPPProtocol::IPv6, + 0x8021 => PPPProtocol::IPCP, + 0x80FD => PPPProtocol::CCP, + 0xC021 => PPPProtocol::LCP, + 0xC023 => PPPProtocol::PAP, + 0xC223 => PPPProtocol::CHAP, + other => PPPProtocol::Other(other), } } } -impl Decode for PppProtocol { - type Iterm = PppProtocol; - fn decode(input: &[u8]) -> IResult<&[u8], PppProtocol> { +impl Decode for PPPProtocol { + type Iterm = PPPProtocol; + fn decode(input: &[u8]) -> IResult<&[u8], PPPProtocol> { let (input, protocol) = number::streaming::be_u16(input)?; Ok((input, protocol.into())) } } -impl Decode for PppHeader { - type Iterm = PppHeader; - fn decode(input: &[u8]) -> IResult<&[u8], PppHeader> { +impl Decode for PPPHeader { + type Iterm = PPPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], PPPHeader> { let (input, address) = number::streaming::be_u8(input)?; let (input, control) = number::streaming::be_u8(input)?; - let (input, protocol) = PppProtocol::decode(input)?; + let (input, protocol) = PPPProtocol::decode(input)?; Ok(( input, - PppHeader { + PPPHeader { address, control, protocol, @@ -80,8 +80,8 @@ impl Decode for PppHeader { #[cfg(test)] mod tests { - use super::PppHeader; - use super::PppProtocol; + use super::PPPHeader; + use super::PPPProtocol; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -96,16 +96,16 @@ mod tests { let bytes = [0xff, 0x03, 0xc0, 0x21, 0xff /* Payload */]; - let expectation = PppHeader { + let expectation = PPPHeader { address: 0xff, control: 0x03, - protocol: PppProtocol::LCP, + protocol: PPPProtocol::LCP, }; - assert_eq!(PppHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PppHeader::decode(&bytes); + let result = PPPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/pptp.rs b/src/protocol/pptp.rs index 3c4f80a..ce8bd58 100644 --- a/src/protocol/pptp.rs +++ b/src/protocol/pptp.rs @@ -30,14 +30,14 @@ use nom::Needed; // https://datatracker.ietf.org/doc/html/rfc2637 #[derive(Clone, Debug, PartialEq, Eq)] -pub enum PptpMessageType { +pub enum PPTPMessageType { ControlMessage, ManagementMessage, UnknownMessage(u16), } #[derive(Clone, Debug, PartialEq, Eq)] -pub enum PptpControlMessageType { +pub enum PPTPControlMessageType { StartControlConnectionRequest, StartControlConnectionReply, StopControlConnectionRequest, @@ -57,11 +57,11 @@ pub enum PptpControlMessageType { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct PptpHeader { +pub struct PPTPHeader { pub length: u16, - pub message_type: PptpMessageType, + pub message_type: PPTPMessageType, pub magic_cookie: u32, // The Magic Cookie is always sent as the constant 0x1A2B3C4D - pub control_message_type: PptpControlMessageType, + pub control_message_type: PPTPControlMessageType, pub reserved0: u16, pub payload: Vec, } @@ -70,42 +70,42 @@ pub struct PptpHeader { * API ******************************************************************************/ -impl From for PptpMessageType { +impl From for PPTPMessageType { fn from(value: u16) -> Self { match value { - 1 => PptpMessageType::ControlMessage, - 2 => PptpMessageType::ManagementMessage, - other => PptpMessageType::UnknownMessage(other), + 1 => PPTPMessageType::ControlMessage, + 2 => PPTPMessageType::ManagementMessage, + other => PPTPMessageType::UnknownMessage(other), } } } -impl From for PptpControlMessageType { +impl From for PPTPControlMessageType { fn from(value: u16) -> Self { match value { - 1 => PptpControlMessageType::StartControlConnectionRequest, - 2 => PptpControlMessageType::StartControlConnectionReply, - 3 => PptpControlMessageType::StopControlConnectionRequest, - 4 => PptpControlMessageType::StopControlConnectionReply, - 5 => PptpControlMessageType::EchoRequest, - 6 => PptpControlMessageType::EchoReply, - 7 => PptpControlMessageType::OutgoingCallRequest, - 8 => PptpControlMessageType::OutgoingCallReply, - 9 => PptpControlMessageType::IncomingCallRequest, - 10 => PptpControlMessageType::IncomingCallReply, - 11 => PptpControlMessageType::IncomingCallConnected, - 12 => PptpControlMessageType::CallClearRequest, - 13 => PptpControlMessageType::CallDisconnectNotify, - 14 => PptpControlMessageType::WanErrorNotify, - 15 => PptpControlMessageType::SetLinkInfo, - other => PptpControlMessageType::Unknown(other), + 1 => PPTPControlMessageType::StartControlConnectionRequest, + 2 => PPTPControlMessageType::StartControlConnectionReply, + 3 => PPTPControlMessageType::StopControlConnectionRequest, + 4 => PPTPControlMessageType::StopControlConnectionReply, + 5 => PPTPControlMessageType::EchoRequest, + 6 => PPTPControlMessageType::EchoReply, + 7 => PPTPControlMessageType::OutgoingCallRequest, + 8 => PPTPControlMessageType::OutgoingCallReply, + 9 => PPTPControlMessageType::IncomingCallRequest, + 10 => PPTPControlMessageType::IncomingCallReply, + 11 => PPTPControlMessageType::IncomingCallConnected, + 12 => PPTPControlMessageType::CallClearRequest, + 13 => PPTPControlMessageType::CallDisconnectNotify, + 14 => PPTPControlMessageType::WanErrorNotify, + 15 => PPTPControlMessageType::SetLinkInfo, + other => PPTPControlMessageType::Unknown(other), } } } -impl Decode for PptpHeader { - type Iterm = PptpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], PptpHeader> { +impl Decode for PPTPHeader { + type Iterm = PPTPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], PPTPHeader> { let (input, length) = number::streaming::be_u16(input)?; let (input, message_type) = number::streaming::be_u16(input)?; let (input, magic_cookie) = number::streaming::be_u32(input)?; @@ -119,7 +119,7 @@ impl Decode for PptpHeader { false => return Err(Err::Incomplete(Needed::new(need.into()))), }; - let header = PptpHeader { + let header = PPTPHeader { length, message_type: message_type.into(), magic_cookie, @@ -138,9 +138,9 @@ impl Decode for PptpHeader { #[cfg(test)] mod tests { - use super::PptpControlMessageType; - use super::PptpHeader; - use super::PptpMessageType; + use super::PPTPControlMessageType; + use super::PPTPHeader; + use super::PPTPMessageType; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -179,11 +179,11 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 156, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::StartControlConnectionRequest, + control_message_type: PPTPControlMessageType::StartControlConnectionRequest, reserved0: 0, payload: vec![ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, @@ -200,10 +200,10 @@ mod tests { ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -249,11 +249,11 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 156, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::StartControlConnectionReply, + control_message_type: PPTPControlMessageType::StartControlConnectionReply, reserved0: 0, payload: vec![ 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, @@ -270,10 +270,10 @@ mod tests { ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -303,19 +303,19 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 16, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::StopControlConnectionRequest, + control_message_type: PPTPControlMessageType::StopControlConnectionRequest, reserved0: 0, payload: vec![0x00, 0x00, 0x00, 0x00], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -345,19 +345,19 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 16, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::StopControlConnectionReply, + control_message_type: PPTPControlMessageType::StopControlConnectionReply, reserved0: 0, payload: vec![0x01, 0x00, 0x00, 0x00], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -385,19 +385,19 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 16, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::EchoRequest, + control_message_type: PPTPControlMessageType::EchoRequest, reserved0: 0, payload: vec![0x05, 0x00, 0x00, 0x00], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -428,19 +428,19 @@ mod tests { 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 20, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::EchoReply, + control_message_type: PPTPControlMessageType::EchoReply, reserved0: 0, payload: vec![0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -490,11 +490,11 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 168, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::OutgoingCallRequest, + control_message_type: PPTPControlMessageType::OutgoingCallRequest, reserved0: 0, payload: vec![ 0x7d, 0x14, 0x00, 0x0c, 0x00, 0x00, 0x01, 0x2c, 0x05, 0xf5, 0xe1, 0x00, 0x00, 0x00, @@ -512,10 +512,10 @@ mod tests { ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -552,11 +552,11 @@ mod tests { 0x00, 0x00, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 32, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::OutgoingCallReply, + control_message_type: PPTPControlMessageType::OutgoingCallReply, reserved0: 0, payload: vec![ 0x00, 0x03, 0x7d, 0x14, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfa, 0x00, 0x00, 0x10, @@ -564,10 +564,10 @@ mod tests { ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -626,19 +626,19 @@ mod tests { 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 16, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::CallClearRequest, + control_message_type: PPTPControlMessageType::CallClearRequest, reserved0: 0, payload: vec![0x7d, 0x14, 0x00, 0x00], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -680,11 +680,11 @@ mod tests { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 148, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::CallDisconnectNotify, + control_message_type: PPTPControlMessageType::CallDisconnectNotify, reserved0: 0, payload: vec![ 0x00, 0x03, 0x01, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -700,10 +700,10 @@ mod tests { ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { @@ -744,21 +744,21 @@ mod tests { 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* Payload */ ]; - let expectation = PptpHeader { + let expectation = PPTPHeader { length: 24, - message_type: PptpMessageType::ControlMessage, + message_type: PPTPMessageType::ControlMessage, magic_cookie: 0x1a2b3c4d, - control_message_type: PptpControlMessageType::SetLinkInfo, + control_message_type: PPTPControlMessageType::SetLinkInfo, reserved0: 0, payload: vec![ 0x00, 0x03, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, ], }; - assert_eq!(PptpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(PPTPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = PptpHeader::decode(&bytes); + let result = PPTPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/tcp.rs b/src/protocol/tcp.rs index 5a88b25..5ab7dde 100644 --- a/src/protocol/tcp.rs +++ b/src/protocol/tcp.rs @@ -45,7 +45,7 @@ const TCP_OPTION_SACK: u8 = 5; // Selective acknowledgment const TCP_OPTION_TIMESTAMPS: u8 = 8; // Timestamps #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum TcpOption { +pub enum TCPOption { EOL, NOP, MSS { @@ -70,7 +70,7 @@ pub enum TcpOption { } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct TcpHeader { +pub struct TCPHeader { pub source_port: u16, pub dest_port: u16, @@ -90,7 +90,7 @@ pub struct TcpHeader { pub window: u16, pub checksum: u16, pub urgent_ptr: u16, - pub options: Option>, + pub options: Option>, } /****************************************************************************** @@ -105,37 +105,37 @@ fn offset_res_flags_decode(input: &[u8]) -> IResult<&[u8], (u8, u8, u8)> { )))(input) } -impl TcpOption { - fn mss_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { +impl TCPOption { + fn mss_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, mss) = number::streaming::be_u16(input)?; - Ok((input, TcpOption::MSS { length, mss })) + Ok((input, TCPOption::MSS { length, mss })) } - fn wscale_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn wscale_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, shift_count) = number::streaming::be_u8(input)?; Ok(( input, - TcpOption::WSCALE { + TCPOption::WSCALE { length, shift_count, }, )) } - fn sack_permitted_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn sack_permitted_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, _length) = number::streaming::be_u8(input)?; - Ok((input, TcpOption::SACKPERMITTED)) + Ok((input, TCPOption::SACKPERMITTED)) } - fn sack_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn sack_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, left_edge) = number::streaming::be_u32(input)?; let (input, right_edge) = number::streaming::be_u32(input)?; Ok(( input, - TcpOption::SACK { + TCPOption::SACK { length, left_edge, right_edge, @@ -143,13 +143,13 @@ impl TcpOption { )) } - fn timestamp_decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn timestamp_decode(input: &[u8]) -> IResult<&[u8], TCPOption> { let (input, length) = number::streaming::be_u8(input)?; let (input, ts_value) = number::streaming::be_u32(input)?; let (input, ts_reply) = number::streaming::be_u32(input)?; Ok(( input, - TcpOption::TIMESTAMPS { + TCPOption::TIMESTAMPS { length, ts_value, ts_reply, @@ -157,22 +157,22 @@ impl TcpOption { )) } - fn decode(input: &[u8]) -> IResult<&[u8], TcpOption> { + fn decode(input: &[u8]) -> IResult<&[u8], TCPOption> { match number::streaming::be_u8(input)? { - (input, TCP_OPTION_EOL) => Ok((input, TcpOption::EOL)), - (input, TCP_OPTION_NOP) => Ok((input, TcpOption::NOP)), - (input, TCP_OPTION_MSS) => TcpOption::mss_decode(input), - (input, TCP_OPTION_WSCALE) => TcpOption::wscale_decode(input), - (input, TCP_OPTION_SACK_PERMITTED) => TcpOption::sack_permitted_decode(input), - (input, TCP_OPTION_SACK) => TcpOption::sack_decode(input), - (input, TCP_OPTION_TIMESTAMPS) => TcpOption::timestamp_decode(input), + (input, TCP_OPTION_EOL) => Ok((input, TCPOption::EOL)), + (input, TCP_OPTION_NOP) => Ok((input, TCPOption::NOP)), + (input, TCP_OPTION_MSS) => TCPOption::mss_decode(input), + (input, TCP_OPTION_WSCALE) => TCPOption::wscale_decode(input), + (input, TCP_OPTION_SACK_PERMITTED) => TCPOption::sack_permitted_decode(input), + (input, TCP_OPTION_SACK) => TCPOption::sack_decode(input), + (input, TCP_OPTION_TIMESTAMPS) => TCPOption::timestamp_decode(input), (input, _other) => Err(Err::Failure(Error::new(input, ErrorKind::Switch))), } } } -impl TcpHeader { - fn fixed_header_decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { +impl TCPHeader { + fn fixed_header_decode(input: &[u8]) -> IResult<&[u8], TCPHeader> { let (input, source_port) = number::streaming::be_u16(input)?; let (input, dest_port) = number::streaming::be_u16(input)?; let (input, seq_num) = number::streaming::be_u32(input)?; @@ -184,7 +184,7 @@ impl TcpHeader { Ok(( input, - TcpHeader { + TCPHeader { source_port, dest_port, seq_num, @@ -205,11 +205,11 @@ impl TcpHeader { )) } - fn options_decode(input: &[u8]) -> IResult<&[u8], Vec> { + fn options_decode(input: &[u8]) -> IResult<&[u8], Vec> { let mut left = input; - let mut options: Vec = vec![]; + let mut options: Vec = vec![]; loop { - match TcpOption::decode(left) { + match TCPOption::decode(left) { Ok((l, opt)) => { left = l; options.push(opt); @@ -217,7 +217,7 @@ impl TcpHeader { if left.len() <= 0 { break; } - if let TcpOption::EOL = opt { + if let TCPOption::EOL = opt { break; } } @@ -229,16 +229,16 @@ impl TcpHeader { } } -impl Decode for TcpHeader { - type Iterm = TcpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], TcpHeader> { - match TcpHeader::fixed_header_decode(input) { +impl Decode for TCPHeader { + type Iterm = TCPHeader; + fn decode(input: &[u8]) -> IResult<&[u8], TCPHeader> { + match TCPHeader::fixed_header_decode(input) { Ok((left, mut header)) => { if header.data_offset > 20 { let options_length = (header.data_offset - 20) as usize; if options_length <= left.len() { if let Ok((__left, options)) = - TcpHeader::options_decode(&left[0..options_length]) + TCPHeader::options_decode(&left[0..options_length]) { header.options = Some(options); Ok((&left[options_length..], header)) @@ -267,12 +267,12 @@ impl Decode for TcpHeader { 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; - use crate::protocol::tcp::TcpOption::SACKPERMITTED; - use crate::protocol::tcp::TcpOption::TIMESTAMPS; - use crate::protocol::tcp::TcpOption::WSCALE; + use crate::protocol::tcp::TCPOption::MSS; + use crate::protocol::tcp::TCPOption::NOP; + use crate::protocol::tcp::TCPOption::SACK; + use crate::protocol::tcp::TCPOption::SACKPERMITTED; + use crate::protocol::tcp::TCPOption::TIMESTAMPS; + use crate::protocol::tcp::TCPOption::WSCALE; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -341,7 +341,7 @@ mod tests { 0xff, /* Payload */ ]; - let expect_options: Vec = vec![ + let expect_options: Vec = vec![ NOP, NOP, TIMESTAMPS { @@ -351,7 +351,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 50081, dest_port: 443, seq_num: 1522577104, @@ -370,7 +370,7 @@ mod tests { options: Some(expect_options), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } #[test] @@ -449,7 +449,7 @@ mod tests { 0xff, /* Payload */ ]; - let expect_options: Vec = vec![ + let expect_options: Vec = vec![ MSS { length: 4, mss: 1460, @@ -467,7 +467,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 58816, dest_port: 80, seq_num: 3851697578, @@ -486,7 +486,7 @@ mod tests { options: Some(expect_options), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } #[test] @@ -568,7 +568,7 @@ mod tests { 0xff, /* Payload */ ]; - let opts: Vec = vec![ + let opts: Vec = vec![ NOP, NOP, TIMESTAMPS { @@ -585,7 +585,7 @@ mod tests { }, ]; - let expectation = TcpHeader { + let expectation = TCPHeader { source_port: 58816, dest_port: 80, seq_num: 3851698039, @@ -604,6 +604,6 @@ mod tests { options: Some(opts), }; - assert_eq!(TcpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(TCPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); } } diff --git a/src/protocol/udp.rs b/src/protocol/udp.rs index 43cf75c..625c9e9 100644 --- a/src/protocol/udp.rs +++ b/src/protocol/udp.rs @@ -21,7 +21,7 @@ use nom::IResult; */ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct UdpHeader { +pub struct UDPHeader { pub source_port: u16, pub dest_port: u16, pub length: u16, @@ -32,9 +32,9 @@ pub struct UdpHeader { * API ******************************************************************************/ -impl Decode for UdpHeader { - type Iterm = UdpHeader; - fn decode(input: &[u8]) -> IResult<&[u8], UdpHeader> { +impl Decode for UDPHeader { + type Iterm = 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)?; @@ -42,7 +42,7 @@ impl Decode for UdpHeader { Ok(( input, - UdpHeader { + UDPHeader { source_port, dest_port, length, @@ -58,7 +58,7 @@ impl Decode for UdpHeader { #[cfg(test)] mod tests { - use super::UdpHeader; + use super::UDPHeader; use crate::protocol::codec::Decode; const LAST_SLICE: &'static [u8] = &[0xff]; @@ -89,17 +89,17 @@ mod tests { 0xff, /* Payload */ ]; - let expectation = UdpHeader { + let expectation = UDPHeader { source_port: 9993, dest_port: 9993, length: 145, checksum: 0x57d3, }; - assert_eq!(UdpHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(UDPHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = UdpHeader::decode(&bytes); + let result = UDPHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { diff --git a/src/protocol/vlan.rs b/src/protocol/vlan.rs index 7533e41..17a82e1 100644 --- a/src/protocol/vlan.rs +++ b/src/protocol/vlan.rs @@ -1,5 +1,5 @@ use crate::protocol::codec::Decode; -use crate::protocol::ethernet::EtherType; +use crate::protocol::ethernet::EthType; use nom::bits; use nom::error::Error; use nom::sequence; @@ -10,15 +10,15 @@ use nom::IResult; ******************************************************************************/ #[derive(Clone, Debug, PartialEq, Eq)] -pub struct VlanHeader { +pub struct VLANHeader { // A 3 bit number which refers to the IEEE 802.1p class of service and maps to the frame priority level. pub priority_code_point: u8, // Indicate that the frame may be dropped under the presence of congestion. pub drop_eligible_indicator: bool, // 12 bits vland identifier. pub vlan_identifier: u16, - // "Tag protocol identifier": Type id of content after this header. Refer to the "EtherType" for a list of possible supported values. - pub ether_type: EtherType, + // "Tag protocol identifier": Type id of content after this header. Refer to the "EthType" for a list of possible supported values. + pub ether_type: EthType, } /****************************************************************************** @@ -33,15 +33,15 @@ fn bit_decode(input: &[u8]) -> IResult<&[u8], (u8, u8, u16)> { )))(input) } -impl Decode for VlanHeader { - type Iterm = VlanHeader; - fn decode(input: &[u8]) -> IResult<&[u8], VlanHeader> { +impl Decode for VLANHeader { + type Iterm = VLANHeader; + fn decode(input: &[u8]) -> IResult<&[u8], VLANHeader> { let (input, (priority_code_point, drop_eligible_indicator, vlan_identifier)) = bit_decode(input)?; - let (input, ether_type) = EtherType::decode(input)?; + let (input, ether_type) = EthType::decode(input)?; Ok(( input, - VlanHeader { + VLANHeader { priority_code_point, drop_eligible_indicator: drop_eligible_indicator == 1, vlan_identifier, @@ -57,9 +57,9 @@ impl Decode for VlanHeader { #[cfg(test)] mod tests { - use super::VlanHeader; + use super::VLANHeader; use crate::protocol::codec::Decode; - use crate::protocol::ethernet::EtherType; + use crate::protocol::ethernet::EthType; const LAST_SLICE: &'static [u8] = &[0xff]; #[test] @@ -73,17 +73,17 @@ mod tests { */ let bytes = [0x00, 0x20, 0x08, 0x00, 0xff /* Payload */]; - let expectation = VlanHeader { + let expectation = VLANHeader { priority_code_point: 0, drop_eligible_indicator: false, vlan_identifier: 32, - ether_type: EtherType::IPv4, + ether_type: EthType::IPv4, }; - assert_eq!(VlanHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); + assert_eq!(VLANHeader::decode(&bytes), Ok((LAST_SLICE, expectation))); // example - let result = VlanHeader::decode(&bytes); + let result = VLANHeader::decode(&bytes); if let Ok((payload, header)) = result { println!("return: {:?}, payload: {}", header, payload.len()); } else { -- cgit v1.2.3