Skip to content

Commit 60882e9

Browse files
committed
update parsing logic
1 parent 2d61340 commit 60882e9

File tree

4 files changed

+270
-163
lines changed

4 files changed

+270
-163
lines changed

oryx-common/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ edition.workspace = true
1111

1212
[lib]
1313
path = "src/lib.rs"
14+
15+
[dependencies]
16+
network-types = { git = "https://github.com/vadorovsky/network-types.git", rev = "e0ee8d5" }

oryx-common/src/arp.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use core::fmt::Display;
2+
3+
#[derive(Debug, Copy, Clone)]
4+
pub struct ArpPacket {
5+
pub arp_type: ArpType,
6+
pub src_mac: MacAddr,
7+
pub dst_mac: MacAddr,
8+
}
9+
10+
impl Display for ArpPacket {
11+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12+
write!(f, "{} {} ARP", self.src_mac, self.dst_mac)
13+
}
14+
}
15+
16+
#[derive(Debug, Copy, Clone)]
17+
pub enum ArpType {
18+
Request,
19+
Reply,
20+
}
21+
22+
impl Display for ArpType {
23+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
24+
match self {
25+
Self::Request => write!(f, "Arp Request"),
26+
Self::Reply => write!(f, "Arp Reply"),
27+
}
28+
}
29+
}
30+
31+
#[derive(Debug, Copy, Clone)]
32+
pub struct MacAddr(pub [u8; 6]);
33+
34+
impl Display for MacAddr {
35+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36+
write!(
37+
f,
38+
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
39+
self.0[0].to_be(),
40+
self.0[1].to_be(),
41+
self.0[2].to_be(),
42+
self.0[3].to_be(),
43+
self.0[4].to_be(),
44+
self.0[5].to_be()
45+
)
46+
}
47+
}

oryx-common/src/ip.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use core::{fmt::Display, net::IpAddr};
2+
3+
#[repr(C)]
4+
#[derive(Debug, Copy, Clone)]
5+
pub struct TcpPacket {
6+
pub dst_port: u16,
7+
pub src_port: u16,
8+
pub dst_ip: IpAddr,
9+
pub src_ip: IpAddr,
10+
}
11+
12+
#[repr(C)]
13+
#[derive(Debug, Copy, Clone)]
14+
pub struct UdpPacket {
15+
pub dst_port: u16,
16+
pub src_port: u16,
17+
pub dst_ip: IpAddr,
18+
pub src_ip: IpAddr,
19+
}
20+
21+
#[repr(C)]
22+
#[derive(Debug, Copy, Clone)]
23+
pub struct IcmpPacket {
24+
pub icmp_type: IcmpType,
25+
pub dst_ip: IpAddr,
26+
pub src_ip: IpAddr,
27+
}
28+
29+
#[repr(C)]
30+
#[derive(Debug, Copy, Clone)]
31+
pub enum IcmpType {
32+
EchoRequest,
33+
EchoReply,
34+
DestinationUnreachable,
35+
}
36+
37+
impl Display for IcmpType {
38+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39+
match self {
40+
IcmpType::EchoReply => {
41+
write!(f, "Echo Reply")
42+
}
43+
IcmpType::EchoRequest => {
44+
write!(f, "Echo Request")
45+
}
46+
IcmpType::DestinationUnreachable => {
47+
write!(f, "Destination Unreachable")
48+
}
49+
}
50+
}
51+
}
52+
53+
#[repr(C)]
54+
#[derive(Debug, Copy, Clone)]
55+
pub enum IpPacket {
56+
Tcp(TcpPacket),
57+
Udp(UdpPacket),
58+
Icmp(IcmpPacket),
59+
}
60+
61+
impl Display for IpPacket {
62+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
63+
match self {
64+
IpPacket::Tcp(p) => {
65+
write!(
66+
f,
67+
"{} {} {} {} TCP",
68+
p.src_ip, p.src_port, p.dst_ip, p.dst_port
69+
)
70+
}
71+
IpPacket::Udp(p) => {
72+
write!(
73+
f,
74+
"{} {} {} {} UDP",
75+
p.src_ip, p.src_port, p.dst_ip, p.dst_port
76+
)
77+
}
78+
IpPacket::Icmp(p) => {
79+
write!(f, "{} {} ICMP", p.src_ip, p.dst_ip)
80+
}
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)