Skip to content

Commit

Permalink
Add support for ARP protocol (#10)
Browse files Browse the repository at this point in the history
big thanks to my friend @adgaultier 

Co-authored-by: adrien gaultier <adrien.gaultier@protonmail.com>
  • Loading branch information
pythops and adgaultier authored Sep 12, 2024
1 parent 2289c92 commit 648cde1
Show file tree
Hide file tree
Showing 22 changed files with 976 additions and 662 deletions.
36 changes: 22 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ show interface:

# Run oryx
run:
cargo xtask run
cargo xtask run --release

# Build oryx
build:
Expand Down
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" }
52 changes: 52 additions & 0 deletions oryx-common/src/arp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 {
//FIX: workaround for the moment
if self.0.iter().all(|x| *x == 0) {
write!(f, "ff:ff:ff:ff:ff:ff",)
} else {
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()
)
}
}
}
93 changes: 93 additions & 0 deletions oryx-common/src/ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use core::{fmt::Display, net::IpAddr};

use network_types::{icmp::IcmpHdr, tcp::TcpHdr, udp::UdpHdr};

#[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),
}

#[repr(C)]
#[derive(Copy, Clone)]
pub enum ProtoHdr {
Tcp(TcpHdr),
Udp(UdpHdr),
Icmp(IcmpHdr),
}

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 648cde1

Please # to comment.