Skip to content

Commit

Permalink
add udp for ip fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
tiannian committed Oct 27, 2022
1 parent d2ea6bb commit c43c2ed
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
5 changes: 0 additions & 5 deletions auip/src/interface/utils.rs → auip/src/interface/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,3 @@ pub fn build_and_record_arp(
Ok(Action::NoAction)
}
}

pub fn poll_ipv4(bytes: layer3::ipv4::Packet<&[u8]>) -> Result<()> {
log::debug!("Receive packet: {}", bytes);
Ok(())
}
13 changes: 8 additions & 5 deletions auip/src/interface/iface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use auip_pkt::{
layer3,
};

use crate::{interface::utils, AddrsStorage, ArpStorage, Device, InterfaceConfig, Medium, Result};
use crate::{
build_and_record_arp, poll_ipv4, AddrsStorage, ArpStorage, Device, InterfaceConfig, Medium,
Result,
};

use super::action::Action;

Expand Down Expand Up @@ -89,7 +92,7 @@ where
let dest_addr = rx_pkt.dest_addr();

if dest_addr != this_mac_addr && dest_addr != layer2::Address::BROADCAST {
log::debug!("Mac address mismatch, Drop it.");
log::debug!("Mac address {} mismatch, Drop it.", dest_addr);

return Ok(Action::NoAction);
}
Expand Down Expand Up @@ -141,7 +144,7 @@ where
let tpa = pkt.target_protocol_address()?;
let sa = rx_pkt.src_addr();

return utils::build_and_record_arp(
return build_and_record_arp(
sa,
sha,
spa,
Expand All @@ -155,7 +158,7 @@ where
layer2::Layer3Protocol::IPv4 => {
let pkt = layer3::ipv4::Packet::new_checked(rx_pkt.payload())?;

utils::poll_ipv4(pkt)?;
poll_ipv4(pkt)?;
}
layer2::Layer3Protocol::IPv6 => {}
layer2::Layer3Protocol::Unknown(_) => {}
Expand All @@ -170,7 +173,7 @@ where
let ip_pkt = layer3::IpPacket::parse(rx_bytes)?;

match ip_pkt {
layer3::IpPacket::IPv4(pkt) => utils::poll_ipv4(pkt)?,
layer3::IpPacket::IPv4(pkt) => poll_ipv4(pkt)?,
layer3::IpPacket::Ipv6 => {}
}
}
Expand Down
39 changes: 39 additions & 0 deletions auip/src/interface/ipv4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use auip_pkt::{layer3, layer4};

use crate::{Result, poll_udp};

pub fn poll_ipv4(pkt: layer3::ipv4::Packet<&[u8]>) -> Result<()> {
log::debug!("Receive packet: {}", pkt);

// Drop ttl is 0.
if pkt.ttl() == 0 {
return Ok(());
}

// Check is fragment
if pkt.dont_frag() {
// enter upper layer.
let protocol = pkt.protocol();

let payload = pkt.payload();

match protocol {
layer3::Protocol::Udp => {
let pkt = layer4::udp::Packet::new_checked(payload)?;

poll_udp(pkt)?;
}
_ => {}
}
} else {
return Ok(());
}

if pkt.more_frags() {
// save frag.
} else {
// complete frag.
}

Ok(())
}
9 changes: 8 additions & 1 deletion auip/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ pub use config::*;

pub(crate) mod action;

pub(crate) mod utils;
mod arp;
pub use arp::*;

mod ipv4;
pub use ipv4::*;

mod udp;
pub use udp::*;
8 changes: 8 additions & 0 deletions auip/src/interface/udp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use auip_pkt::layer4;

use crate::Result;

pub fn poll_udp(pkt: layer4::udp::Packet<&[u8]>) -> Result<()> {
log::debug!("Receive packet: {}", pkt);
Ok(())
}
16 changes: 16 additions & 0 deletions pkt/src/layer4/udp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::fmt::{self, Display, Formatter};

use byteorder::{ByteOrder, NetworkEndian};

use crate::{
Expand Down Expand Up @@ -34,6 +36,20 @@ impl<T> IntoInner for Packet<T> {
}
}

impl<T: AsRef<[u8]>> Display for Packet<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str("UDP Packet:")?;
f.write_fmt(format_args!(
"Src port: {}, Dst port: {}, Length: {}",
self.src_port(),
self.dst_port(),
self.len(),
))?;

Ok(())
}
}

impl<T: AsRef<[u8]>> Packet<T> {
/// Imbue a raw octet buffer with UDP packet structure.
pub fn new_unchecked(buffer: T) -> Packet<T> {
Expand Down

0 comments on commit c43c2ed

Please sign in to comment.