Skip to content

Commit

Permalink
rename and add l3ps
Browse files Browse the repository at this point in the history
  • Loading branch information
tiannian committed Oct 19, 2022
1 parent 22e3065 commit 164b20b
Show file tree
Hide file tree
Showing 34 changed files with 385 additions and 205 deletions.
5 changes: 5 additions & 0 deletions auip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ edition = "2018"
# byteorder = {version = "1", default-features = false}
log = "0.4"
auip-pkt = {path = "../pkt"}

[features]
default = ["alloc"]
alloc = []

15 changes: 0 additions & 15 deletions auip/src/device/addrs_storage.rs

This file was deleted.

9 changes: 0 additions & 9 deletions auip/src/device/device.rs

This file was deleted.

7 changes: 2 additions & 5 deletions auip/src/device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
mod medium;
pub use medium::*;

mod device;
pub use device::*;

mod addrs_storage;
pub use addrs_storage::*;
mod prelude;
pub use prelude::*;
31 changes: 31 additions & 0 deletions auip/src/device/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use auip_pkt::{layer2, layer3};

use crate::{Medium, Result};

pub trait Device {
fn send(&mut self, buffer: &[u8]) -> Result<()>;

fn recv(&mut self) -> Result<Option<&[u8]>>;

fn medium(&self) -> Medium;
}

pub trait AddrsStorage {
fn set_mac_addr(&mut self, addr: layer2::Address);

fn mac_addr(&self) -> &layer2::Address;

fn add_ip_addr(&mut self, addr: layer3::Cidr) -> Result<()>;

fn del_ip_addr(&mut self, addr: layer3::Cidr);

fn ip_addrs(&self) -> &[layer3::Cidr];
}

pub trait Layer3PacketStorage {
type Layer3PacketBytes;

fn get(&self, idx: usize) -> Option<&layer3::Packet<Self::Layer3PacketBytes>>;

fn get_mut(&mut self, idx: usize) -> Option<&mut layer3::Packet<Self::Layer3PacketBytes>>;
}
101 changes: 81 additions & 20 deletions auip/src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,86 @@
use auip_pkt::{
ip,
mac::{ethernet, VlanId},
layer2::{self, ethernet, VlanId},
layer3,
};

use crate::{AddrsStorage, Device, Medium, Result};
use crate::{AddrsStorage, Device, Layer3PacketStorage, Medium, Result};

pub struct Interface<D, A> {
pub struct Interface<D, AS, L3PS> {
device: D,
addrs_storage: AS,
layer3_packet_storage: L3PS,
medium: Medium,
addrs_storage: A,
vlanid: VlanId,

vlanid0: Option<VlanId>,
vlanid1: Option<VlanId>,
}

impl<D: Device, A: AddrsStorage> Interface<D, A> {
pub fn new(device: D, addrs_storage: A, vlanid: VlanId) -> Self {
impl<D, AS, L3PS> Interface<D, AS, L3PS>
where
D: Device,
AS: AddrsStorage,
L3PS: Layer3PacketStorage,
{
pub fn new(device: D, addrs_storage: AS, layer3_packet_storage: L3PS) -> Self {
let medium = device.medium();

Self {
device,
medium,
addrs_storage,
vlanid,
layer3_packet_storage,
vlanid0: None,
vlanid1: None,
}
}

pub(crate) fn poll_ethernet(&mut self) -> Result<()> {
let rx_bytes = self.device.recv()?;
let rx_pkt = ethernet::Packet::new_checked(rx_bytes)?;
if let Some(rx_bytes) = self.device.recv()? {
let rx_pkt = ethernet::Packet::new_checked(rx_bytes)?;

// TODO: Hook
// TODO: Hook

let dest_addr = rx_pkt.dest_addr();
let dest_addr = rx_pkt.dest_addr();

if &dest_addr == self.addrs_storage.mac_addr() {
// Got vlan id and math
// Process ip packet
}
if &dest_addr == self.addrs_storage.mac_addr() {
let protocol = rx_pkt.protocol();

match protocol {
layer2::Protocol::Layer3Protocol(l3) => poll_layer3(l3, rx_pkt.payload())?,
layer2::Protocol::IEEE8021Q(vlanid, l3) => {
if Some(vlanid) == self.vlanid0 {
poll_layer3(l3, rx_pkt.payload())?;
}
}
layer2::Protocol::QinQ(vlanid, vlanid1, l3) => {
if Some(vlanid) == self.vlanid0 && Some(vlanid1) == self.vlanid1 {
poll_layer3(l3, rx_pkt.payload())?;
}
}

// TODO: process IEEE802.3 packet.
layer2::Protocol::Length(_) => {}

// Skip
layer2::Protocol::Unknown(_) => {}
}
}

// Select mac address and send packet
// Select mac address and send packet
}

Ok(())
}

pub(crate) fn poll_ip(&mut self) -> Result<()> {
let rx_bytes = self.device.recv()?;
// let rx_pkt = ip::Packet::new_checked(rx_bytes)?;
if let Some(rx_bytes) = self.device.recv()? {
let ip_pkt = layer3::IpPacket::parse(rx_bytes)?;

match ip_pkt {
layer3::IpPacket::IPv4(pkt) => poll_ipv4(pkt)?,
layer3::IpPacket::Ipv6 => {}
}
}

Ok(())
}
Expand All @@ -58,3 +94,28 @@ impl<D: Device, A: AddrsStorage> Interface<D, A> {
Ok(())
}
}

pub(crate) fn poll_layer3(protocol: layer2::Layer3Protocol, bytes: &[u8]) -> Result<()> {
match protocol {
layer2::Layer3Protocol::IPv4 => {
let pkt = layer3::ipv4::Packet::new_checked(bytes)?;

poll_ipv4(pkt)?;
}
layer2::Layer3Protocol::IPv6 => {}
layer2::Layer3Protocol::ARP => {}
layer2::Layer3Protocol::Unknown(_) => {}
}

Ok(())
}

pub(crate) fn poll_arp(bytes: &[u8]) -> Result<()> {
let pkt = layer3::arp::Packet::new_checked(bytes)?;

Ok(())
}

pub(crate) fn poll_ipv4(pkt: layer3::ipv4::Packet<&[u8]>) -> Result<()> {
Ok(())
}
File renamed without changes.
18 changes: 9 additions & 9 deletions auip/src/storage/fixed_addrs.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use auip_pkt::{ip, mac};
use auip_pkt::{layer2, layer3};

use crate::{AddrsStorage, Error, Result};

pub struct FixedAddrsStorage<const IP_ADDR_NUM: usize> {
pub mac_addr: mac::Address,
pub ip_addrs: [ip::Cidr; IP_ADDR_NUM],
pub mac_addr: layer2::Address,
pub ip_addrs: [layer3::Cidr; IP_ADDR_NUM],
}

impl<const IP_ADDR_NUM: usize> Default for FixedAddrsStorage<IP_ADDR_NUM> {
Expand All @@ -19,19 +19,19 @@ impl<const IP_ADDR_NUM: usize> Default for FixedAddrsStorage<IP_ADDR_NUM> {
}

impl<const IP_ADDR_NUM: usize> AddrsStorage for FixedAddrsStorage<IP_ADDR_NUM> {
fn mac_addr(&self) -> &mac::Address {
fn mac_addr(&self) -> &layer2::Address {
&self.mac_addr
}

fn set_mac_addr(&mut self, addr: mac::Address) {
fn set_mac_addr(&mut self, addr: layer2::Address) {
self.mac_addr = addr;
}

fn add_ip_addr(&mut self, addr: ip::Cidr) -> Result<()> {
fn add_ip_addr(&mut self, addr: layer3::Cidr) -> Result<()> {
let mut setted = false;

for it in &mut self.ip_addrs {
if it.address() == &ip::Address::Unspecified {
if it.address() == &layer3::Address::Unspecified {
*it = addr;
setted = true;
}
Expand All @@ -44,15 +44,15 @@ impl<const IP_ADDR_NUM: usize> AddrsStorage for FixedAddrsStorage<IP_ADDR_NUM> {
}
}

fn del_ip_addr(&mut self, addr: ip::Cidr) {
fn del_ip_addr(&mut self, addr: layer3::Cidr) {
for it in &mut self.ip_addrs {
if it == &addr {
*it = Default::default()
}
}
}

fn ip_addrs(&self) -> &[ip::Cidr] {
fn ip_addrs(&self) -> &[layer3::Cidr] {
&self.ip_addrs
}
}
29 changes: 29 additions & 0 deletions auip/src/storage/fixed_l3ps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use auip_pkt::layer3;

use crate::{FixedBytes, Layer3PacketStorage};

pub struct FixedLayer3PacketStorage<const MTU: usize, const LEN: usize> {
pub packets: [layer3::Packet<FixedBytes<MTU>>; LEN],
}

impl<const MTU: usize, const LEN: usize> Layer3PacketStorage
for FixedLayer3PacketStorage<MTU, LEN>
{
type Layer3PacketBytes = FixedBytes<MTU>;

fn get(&self, idx: usize) -> Option<&layer3::Packet<Self::Layer3PacketBytes>> {
if self.packets.len() < idx {
Some(&self.packets[idx])
} else {
None
}
}

fn get_mut(&mut self, idx: usize) -> Option<&mut layer3::Packet<Self::Layer3PacketBytes>> {
if self.packets.len() < idx {
Some(&mut self.packets[idx])
} else {
None
}
}
}
8 changes: 8 additions & 0 deletions auip/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
mod fixed_addrs;
pub use fixed_addrs::*;

#[cfg(feature = "alloc")]
mod dynamic_addrs;
#[cfg(feature = "alloc")]
pub use dynamic_addrs::*;

mod fixed_l3ps;
pub use fixed_l3ps::*;
12 changes: 12 additions & 0 deletions auip/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
pub struct FixedBytes<const LEN: usize>(pub [u8; LEN]);

impl<const LEN: usize> AsRef<[u8]> for FixedBytes<LEN> {
fn as_ref(&self) -> &[u8] {
&self.0
}
}

impl<const LEN: usize> AsMut<[u8]> for FixedBytes<LEN> {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0
}
}
5 changes: 2 additions & 3 deletions pkt/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
/// Error's for packet.
#[derive(Clone, Debug)]
pub enum Error {
Illegal,
Malformed,
Unrecognized,
WrongLengthForEthernetAddress,
WrongLengthForIpv4Address,
WrongLengthForArpPacket,
WrongLengthForIpv4Packet,
WrongLengthForEthernetPacket,
UnknownIpVersionNumber,
IllegalNetmask,
}

/// Result for packet.
Expand Down
10 changes: 0 additions & 10 deletions pkt/src/ip/ipv4/mod.rs

This file was deleted.

17 changes: 0 additions & 17 deletions pkt/src/ip/mod.rs

This file was deleted.

15 changes: 0 additions & 15 deletions pkt/src/ip/packet.rs

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 164b20b

Please sign in to comment.