Skip to content

Commit

Permalink
update parsing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pythops committed Sep 11, 2024
1 parent 2d61340 commit 60882e9
Show file tree
Hide file tree
Showing 4 changed files with 270 additions and 163 deletions.
3 changes: 3 additions & 0 deletions oryx-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ edition.workspace = true

[lib]
path = "src/lib.rs"

[dependencies]
network-types = { git = "https://github.com/vadorovsky/network-types.git", rev = "e0ee8d5" }
47 changes: 47 additions & 0 deletions oryx-common/src/arp.rs
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()
)
}
}
83 changes: 83 additions & 0 deletions oryx-common/src/ip.rs
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)
}
}
}
}
Loading

0 comments on commit 60882e9

Please sign in to comment.