-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
270 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use core::fmt::Display; | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct ArpPacket { | ||
pub arp_type: ArpType, | ||
pub src_mac: MacAddr, | ||
pub dst_mac: MacAddr, | ||
} | ||
|
||
impl Display for ArpPacket { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
write!(f, "{} {} ARP", self.src_mac, self.dst_mac) | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub enum ArpType { | ||
Request, | ||
Reply, | ||
} | ||
|
||
impl Display for ArpType { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
Self::Request => write!(f, "Arp Request"), | ||
Self::Reply => write!(f, "Arp Reply"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
pub struct MacAddr(pub [u8; 6]); | ||
|
||
impl Display for MacAddr { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
write!( | ||
f, | ||
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", | ||
self.0[0].to_be(), | ||
self.0[1].to_be(), | ||
self.0[2].to_be(), | ||
self.0[3].to_be(), | ||
self.0[4].to_be(), | ||
self.0[5].to_be() | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use core::{fmt::Display, net::IpAddr}; | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct TcpPacket { | ||
pub dst_port: u16, | ||
pub src_port: u16, | ||
pub dst_ip: IpAddr, | ||
pub src_ip: IpAddr, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct UdpPacket { | ||
pub dst_port: u16, | ||
pub src_port: u16, | ||
pub dst_ip: IpAddr, | ||
pub src_ip: IpAddr, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub struct IcmpPacket { | ||
pub icmp_type: IcmpType, | ||
pub dst_ip: IpAddr, | ||
pub src_ip: IpAddr, | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub enum IcmpType { | ||
EchoRequest, | ||
EchoReply, | ||
DestinationUnreachable, | ||
} | ||
|
||
impl Display for IcmpType { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
IcmpType::EchoReply => { | ||
write!(f, "Echo Reply") | ||
} | ||
IcmpType::EchoRequest => { | ||
write!(f, "Echo Request") | ||
} | ||
IcmpType::DestinationUnreachable => { | ||
write!(f, "Destination Unreachable") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
#[derive(Debug, Copy, Clone)] | ||
pub enum IpPacket { | ||
Tcp(TcpPacket), | ||
Udp(UdpPacket), | ||
Icmp(IcmpPacket), | ||
} | ||
|
||
impl Display for IpPacket { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
IpPacket::Tcp(p) => { | ||
write!( | ||
f, | ||
"{} {} {} {} TCP", | ||
p.src_ip, p.src_port, p.dst_ip, p.dst_port | ||
) | ||
} | ||
IpPacket::Udp(p) => { | ||
write!( | ||
f, | ||
"{} {} {} {} UDP", | ||
p.src_ip, p.src_port, p.dst_ip, p.dst_port | ||
) | ||
} | ||
IpPacket::Icmp(p) => { | ||
write!(f, "{} {} ICMP", p.src_ip, p.dst_ip) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.