Commit 228d23b2 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

ethernet frame: remove unwarp

parent 82e98223
...@@ -31,7 +31,10 @@ impl ControllerFrame10 for Controller { ...@@ -31,7 +31,10 @@ impl ControllerFrame10 for Controller {
*/ */
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream) { fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream) {
println!("reason {:?}", packetin.reason); println!("reason {:?}", packetin.reason);
let pkt = packetin.ether_parse(); let pkt = match packetin.ether_parse() {
Ok(pkt) => pkt,
Err(_) => return,
};
self.mac_to_port.insert(pkt.mac_src, packetin.in_port); self.mac_to_port.insert(pkt.mac_src, packetin.in_port);
let mac_dst = pkt.mac_dst; let mac_dst = pkt.mac_dst;
......
use crate::etherparser::ether_type::EtherType; use crate::etherparser::ether_type::EtherType;
use std::io::{BufRead, Cursor}; use std::io::{BufRead, Cursor, Error};
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
...@@ -19,21 +19,21 @@ pub struct EthernetFrame { ...@@ -19,21 +19,21 @@ pub struct EthernetFrame {
} }
impl EthernetFrame { impl EthernetFrame {
pub fn parse(payload: &Vec<u8>) -> EthernetFrame { pub fn parse(payload: &Vec<u8>) -> Result<EthernetFrame, Error> {
let mut bytes = Cursor::new(payload.to_vec()); let mut bytes = Cursor::new(payload.to_vec());
let mut mac_dst = [0u8; 6]; let mut mac_dst = [0u8; 6];
let mut mac_src = [0u8; 6]; let mut mac_src = [0u8; 6];
for i in 0..6 { for i in 0..6 {
mac_dst[i] = bytes.read_u8().unwrap(); mac_dst[i] = bytes.read_u8()?;
} }
for i in 0..6 { for i in 0..6 {
mac_src[i] = bytes.read_u8().unwrap(); mac_src[i] = bytes.read_u8()?;
} }
// check 802.1q tag tpid // check 802.1q tag tpid
let typ = bytes.read_u16::<BigEndian>().unwrap(); let typ = bytes.read_u16::<BigEndian>()?;
let (vlan_pcp, vlan_dei, vlan_vid, typ) = match typ { let (vlan_pcp, vlan_dei, vlan_vid, typ) = match typ {
tp if tp == EtherType::VLAN as u16 => { tp if tp == EtherType::VLAN as u16 => {
let tci = bytes.read_u16::<BigEndian>().unwrap(); let tci = bytes.read_u16::<BigEndian>()?;
let pcp = tci >> 13; let pcp = tci >> 13;
let dei = (tci & 0x1000) > 0; let dei = (tci & 0x1000) > 0;
let vid = tci & 0xfff; let vid = tci & 0xfff;
...@@ -44,23 +44,21 @@ impl EthernetFrame { ...@@ -44,23 +44,21 @@ impl EthernetFrame {
let network = match typ { let network = match typ {
tp if tp == EtherType::IP as u16 => { tp if tp == EtherType::IP as u16 => {
let ip = IP::parse(&mut bytes); let ip = IP::parse(&mut bytes);
if ip.is_some() { match ip {
Network::IP(ip.unwrap()) Some(ip) => Network::IP(ip),
} else { None => Network::Unparsable(typ, bytes.fill_buf()?.to_vec()),
Network::Unparsable(typ, bytes.fill_buf().unwrap().to_vec())
} }
} }
tp if tp == (EtherType::ARP as u16) => { tp if tp == (EtherType::ARP as u16) => {
let arp = ARP::parse(&mut bytes); let arp = ARP::parse(&mut bytes);
if arp.is_some() { match arp {
Network::ARP(arp.unwrap()) Some(arp) => Network::ARP(arp),
} else { None => Network::Unparsable(typ, bytes.fill_buf()?.to_vec()),
Network::Unparsable(typ, bytes.fill_buf().unwrap().to_vec())
} }
} }
_ => Network::Unparsable(typ, bytes.fill_buf().unwrap().to_vec()), _ => Network::Unparsable(typ, bytes.fill_buf()?.to_vec()),
}; };
EthernetFrame { Ok(EthernetFrame {
ether_type: EtherType::parse(typ), ether_type: EtherType::parse(typ),
mac_dst: mac_to_bytes(mac_dst), mac_dst: mac_to_bytes(mac_dst),
mac_src: mac_to_bytes(mac_src), mac_src: mac_to_bytes(mac_src),
...@@ -68,7 +66,7 @@ impl EthernetFrame { ...@@ -68,7 +66,7 @@ impl EthernetFrame {
vlan_dei, vlan_dei,
vlan_vid, vlan_vid,
network, network,
} })
} }
} }
......
...@@ -2,7 +2,7 @@ use crate::etherparser::ethernet::EthernetFrame; ...@@ -2,7 +2,7 @@ use crate::etherparser::ethernet::EthernetFrame;
use super::Payload; use super::Payload;
use byteorder::{BigEndian, ReadBytesExt}; use byteorder::{BigEndian, ReadBytesExt};
use std::io::{BufRead, Cursor}; use std::io::{BufRead, Cursor, Error};
#[derive(Debug)] #[derive(Debug)]
pub enum PacketInReason { pub enum PacketInReason {
...@@ -33,7 +33,7 @@ pub struct PacketInEvent { ...@@ -33,7 +33,7 @@ pub struct PacketInEvent {
} }
impl PacketInEvent { impl PacketInEvent {
pub fn ether_parse(&self) -> EthernetFrame { pub fn ether_parse(&self) -> Result<EthernetFrame, Error> {
match &self.payload { match &self.payload {
Payload::Buffered(_, p) | Payload::NoBuffered(p) => EthernetFrame::parse(&p), Payload::Buffered(_, p) | Payload::NoBuffered(p) => EthernetFrame::parse(&p),
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment