Commit 35d61ff6 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

rewrite header to split version to support others version in future, and create…

rewrite header to split version to support others version in future, and create header struct to be interface for using
parent 3a1c73b2
......@@ -21,17 +21,25 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
tcp_listener_handler::<OME>(address, ofp.version() as u8);
}
fn handle_header(&mut self, buf: &mut Vec<u8>) -> (u8, usize, u32) {
let ofp_header = self.get_ofp().header_parse(&buf);
(
ofp_header.message(),
ofp_header.pkt_size(),
ofp_header.xid(),
)
}
fn request_handler(&mut self, buf: &mut Vec<u8>, stream: &mut TcpStream) {
let ofp = self.get_ofp();
let ofp_header = ofp.header_parse(&buf);
let mut payload = vec![0u8; ofp_header.pkt_size()];
let (message, pkt_size, xid) = self.handle_header(buf);
let mut payload = vec![0u8; pkt_size];
let _ = stream.read(&mut payload);
let message = ofp.msg_parse(ofp_header.message as u16);
let message = self.get_ofp().msg_parse(message as u16);
match message {
OfpMsg::Hello => self.send_msg(ofp.fetures_req(), ofp_header.xid, stream),
OfpMsg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
OfpMsg::FeaturesReq => todo!(),
OfpMsg::PacketIn => {
self.packet_in_handler(ofp_header.xid, PacketInEvent::parse(&payload), stream);
self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
}
OfpMsg::FlowMod => todo!(),
OfpMsg::NotFound => todo!(),
......
pub mod header;
pub use header::OfpHeader;
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod controller_frame;
......
pub trait OpenflowHeader {
fn version(&self) -> usize;
fn message(&self) -> u8;
fn length(&self) -> usize;
fn xid(&self) -> u32;
fn pkt_size(&self) -> usize;
fn new(message: u8, length: usize, xid: usize) -> Self;
fn header_size(&self) -> usize;
fn parse(buf: &Vec<u8>) -> Self;
fn marshal(&self, bytes: &mut Vec<u8>);
}
pub mod header_traiter;
pub use header_traiter::OpenflowHeader;
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod ofp_header_v1_0;
pub use ofp_header_v1_0::OfpHeader10;
use super::OpenflowHeader;
pub struct OfpHeader<OFH: OpenflowHeader> {
ofp_header: OFH,
}
impl<OFH: OpenflowHeader> OfpHeader<OFH> {
pub fn new(ofp_header: OFH) -> Self {
Self { ofp_header }
}
pub fn version(&self) -> usize {
self.ofp_header.version()
}
pub fn message(&self) -> u8 {
self.ofp_header.message()
}
pub fn length(&self) -> usize {
self.ofp_header.length()
}
pub fn xid(&self) -> u32 {
self.ofp_header.xid()
}
pub fn header_size(&self) -> usize {
self.ofp_header.header_size()
}
pub fn pkt_size(&self) -> usize {
self.ofp_header.pkt_size()
}
pub fn marshal(&self, bytes: &mut Vec<u8>) {
self.ofp_header.marshal(bytes);
}
}
use std::{io::Cursor, mem::size_of};
use super::OpenflowHeader;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::{io::Cursor, mem::size_of};
pub struct OfpHeader {
pub struct OfpHeader10 {
pub version: u8,
pub message: u8,
pub length: u16,
pub xid: u32,
}
impl OfpHeader {
pub fn header_size(&self) -> usize {
size_of::<OfpHeader>()
}
pub fn pkt_size(&self) -> usize {
return self.length as usize - size_of::<OfpHeader>();
}
pub fn new(version: u8, message: u8, length: u16, xid: u32) -> Self {
impl OpenflowHeader for OfpHeader10 {
fn new(message: u8, length: usize, xid: usize) -> Self {
Self {
version,
version: 1,
message,
length: length + 8,
xid,
length: (size_of::<OfpHeader10>() + length) as u16,
xid: xid as u32,
}
}
pub fn parse(buf: &Vec<u8>) -> Self {
fn version(&self) -> usize {
1
}
fn message(&self) -> u8 {
self.message
}
fn length(&self) -> usize {
self.length as usize
}
fn xid(&self) -> u32 {
self.xid
}
fn header_size(&self) -> usize {
size_of::<OfpHeader10>()
}
fn pkt_size(&self) -> usize {
return self.length as usize - size_of::<OfpHeader10>();
}
fn parse(buf: &Vec<u8>) -> Self {
let mut buf_cursor = Cursor::new(buf);
// split data from header
let version = buf_cursor.read_u8().unwrap();
let message = buf_cursor.read_u8().unwrap();
// size is size of packet
let length = buf_cursor.read_u16::<BigEndian>().unwrap();
let xid = buf_cursor.read_u32::<BigEndian>().unwrap();
Self {
......@@ -39,7 +50,7 @@ impl OfpHeader {
xid,
}
}
pub fn marshal(&self, bytes: &mut Vec<u8>) {
fn marshal(&self, bytes: &mut Vec<u8>) {
bytes.write_u8(self.version).unwrap();
bytes.write_u8(self.message).unwrap();
bytes.write_u16::<BigEndian>(self.length).unwrap();
......
use crate::openflow::{
events::{FeaturesReq, HelloEvent},
ofp_header::{OfpHeader10, OpenflowHeader},
OfpHeader,
};
......@@ -15,8 +16,8 @@ impl Openflow10 {
}
impl OfpMsgEvent for Openflow10 {
fn header_parse(&self, bytes:&Vec<u8>) -> OfpHeader {
OfpHeader::parse(bytes)
fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader> {
OfpHeader::new(OfpHeader10::parse(bytes))
}
fn header_size(&self) -> usize {
8
......@@ -35,13 +36,8 @@ impl OfpMsgEvent for Openflow10 {
1
}
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader {
OfpHeader {
version: 1,
message,
length,
xid,
}
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader> {
OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize))
}
fn msg_parse(&self, msg: u16) -> OfpMsg {
......
use crate::openflow::{
events::{FeaturesReq, HelloEvent},
ofp_header::OpenflowHeader,
OfpHeader,
};
......@@ -20,11 +21,11 @@ pub trait MessageMarshal {
* for works with controller to create OfpMsgEvent
*/
pub trait OfpMsgEvent {
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader;
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader>;
fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader>;
fn version(&self) -> usize;
fn ofp_version() -> usize;
fn header_size(&self) -> usize;
fn header_parse(&self, bytes:&Vec<u8>) -> OfpHeader;
fn msg_usize(&self, msg: OfpMsg) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg;
......
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