Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
T
Tenjin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nawasan Wisitsingkhon
Tenjin
Commits
fbf4d695
Commit
fbf4d695
authored
May 06, 2024
by
Nawasan Wisitsingkhon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: move tcp connection in main to new function and use by Controller
parent
b8ceac6f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
71 deletions
+72
-71
lib.rs
src/lib.rs
+4
-2
main.rs
src/main.rs
+3
-65
controller.rs
src/openflow/controller.rs
+33
-3
header.rs
src/openflow/header.rs
+4
-1
tcp_listener.rs
src/tcp_listener.rs
+28
-0
No files found.
src/lib.rs
View file @
fbf4d695
pub
mod
openflow
;
pub
mod
etherparser
;
\ No newline at end of file
pub
mod
etherparser
;
mod
tcp_listener
;
\ No newline at end of file
src/main.rs
View file @
fbf4d695
use
std
::
io
::
Read
;
use
std
::
net
::
TcpListener
;
use
tenjin
::
openflow
::
events
::
packet_in
::
PacketInEvent
;
use
tenjin
::
openflow
::
messages
::{
OfpMsg
,
Openflow10
};
use
tenjin
::
openflow
::
traiter
::
OfpMsgEvent
;
use
tenjin
::
openflow
::{
Controller
,
OfpHeader
};
use
tenjin
::
openflow
::
messages
::
Openflow10
;
use
tenjin
::
openflow
::
Controller
;
extern
crate
byteorder
;
fn
main
()
->
Result
<
(),
std
::
io
::
Error
>
{
let
mut
controller
=
Controller
::
new
(
Openflow10
::
new
());
let
listener
=
TcpListener
::
bind
((
"127.0.0.1"
,
6633
))
.unwrap
();
let
mut
buf
=
vec!
[
0u8
;
8
];
for
stream
in
listener
.incoming
()
{
println!
(
"{:?}"
,
stream
);
match
stream
{
Ok
(
mut
stream
)
=>
{
// std::thread::spawn(move || {
println!
(
"=================== connection ======================="
);
// after tcp is connected, it will send hello message
controller
.hello
(
&
mut
stream
);
// loop for receive data
loop
{
// first receive with Openflow header 64 bit to buf
match
stream
.read
(
&
mut
buf
)
{
Ok
(
v
)
if
v
>
0
=>
{
let
packet
=
OfpHeader
::
parse
(
&
buf
);
// length_payload is var to receive payload if the packet has
// and assign size by length
let
length_payload
=
packet
.size
();
let
mut
payload
=
vec!
[
0u8
;
length_payload
];
stream
.read
(
&
mut
payload
)
?
;
let
message
=
controller
.ofp
.msg_parse
(
packet
.message
as
u16
);
match
message
{
// 0 is Hello message
OfpMsg
::
Hello
=>
{
// after get Hello, send fetureReq
controller
.fetures_req
(
packet
.xid
,
&
mut
stream
);
println!
(
"Hello event"
);
}
OfpMsg
::
PacketIn
=>
{
controller
.packet_in
(
packet
.xid
,
PacketInEvent
::
parse
(
&
payload
),
&
mut
stream
,
);
println!
(
"PacketIn event"
);
}
_
=>
{
println!
(
"others message"
);
}
}
}
Ok
(
_
)
|
Err
(
_
)
=>
break
,
}
}
println!
(
"======================================================"
);
// });
}
Err
(
_
)
=>
{
// connection failed
panic!
(
"Connection failed"
)
}
}
}
controller
.listener
(
"127.0.0.1:6633"
);
Ok
(())
}
src/openflow/controller.rs
View file @
fbf4d695
use
std
::{
collections
::
HashMap
,
io
::
Write
,
net
::
TcpStream
};
use
std
::{
collections
::
HashMap
,
io
::{
Read
,
Write
},
net
::
TcpStream
,
};
use
crate
::
tcp_listener
::
tcp_listener_handler
;
use
super
::{
events
::
PacketInEvent
,
messages
::
traiter
::{
MessageMarshal
,
OfpMsgEvent
},
messages
::{
traiter
::{
MessageMarshal
,
OfpMsgEvent
},
OfpMsg
,
},
OfpHeader
,
};
pub
struct
Controller
<
OME
:
OfpMsgEvent
>
{
...
...
@@ -23,6 +33,26 @@ impl<OME: OfpMsgEvent> Controller<OME> {
}
}
pub
fn
listener
(
&
mut
self
,
address
:
&
str
)
{
tcp_listener_handler
(
self
,
address
);
}
pub
fn
request_handler
(
&
mut
self
,
buf
:
&
mut
Vec
<
u8
>
,
stream
:
&
mut
TcpStream
)
{
let
ofp_header
=
OfpHeader
::
parse
(
&
buf
);
let
mut
payload
=
vec!
[
0u8
;
ofp_header
.pkt_size
()];
let
_
=
stream
.read
(
&
mut
payload
);
let
message
=
self
.ofp
.msg_parse
(
ofp_header
.message
as
u16
);
match
message
{
OfpMsg
::
Hello
=>
self
.send_msg
(
self
.ofp
.fetures_req
(),
ofp_header
.xid
,
stream
),
OfpMsg
::
FeaturesReq
=>
todo!
(),
OfpMsg
::
PacketIn
=>
{
self
.packet_in_handler
(
ofp_header
.xid
,
PacketInEvent
::
parse
(
&
payload
),
stream
);
}
OfpMsg
::
FlowMod
=>
todo!
(),
OfpMsg
::
NotFound
=>
todo!
(),
}
}
pub
fn
send_msg
<
T
:
MessageMarshal
>
(
&
self
,
msg
:
T
,
xid
:
u32
,
stream
:
&
mut
TcpStream
)
{
let
mut
header_bytes
:
Vec
<
u8
>
=
Vec
::
new
();
let
mut
body_bytes
:
Vec
<
u8
>
=
Vec
::
new
();
...
...
@@ -49,7 +79,7 @@ impl<OME: OfpMsgEvent> Controller<OME> {
self
.send_msg
(
fetreq_msg
,
xid
,
stream
);
}
pub
fn
packet_in
(
&
mut
self
,
xid
:
u32
,
packetin
:
PacketInEvent
,
stream
:
&
mut
TcpStream
)
{
pub
fn
packet_in
_handler
(
&
mut
self
,
xid
:
u32
,
packetin
:
PacketInEvent
,
stream
:
&
mut
TcpStream
)
{
let
ether
=
packetin
.payload
;
self
.mac_to_port
.insert
(
ether
.mac_src
,
packetin
.port
);
}
...
...
src/openflow/header.rs
View file @
fbf4d695
...
...
@@ -10,7 +10,10 @@ pub struct OfpHeader {
}
impl
OfpHeader
{
pub
fn
size
(
&
self
)
->
usize
{
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
{
...
...
src/tcp_listener.rs
0 → 100644
View file @
fbf4d695
use
std
::{
io
::
Read
,
net
::
TcpListener
};
use
crate
::
openflow
::{
traiter
::
OfpMsgEvent
,
Controller
};
pub
fn
tcp_listener_handler
<
OME
:
OfpMsgEvent
>
(
controller
:
&
mut
Controller
<
OME
>
,
address
:
&
str
)
{
let
listener
=
TcpListener
::
bind
(
address
)
.unwrap
();
/*
* buffer with 8 length that only support openflow 1.0
* I will make it support others version soon.
*/
let
mut
buffer
=
vec!
[
0u8
;
8
];
for
stream
in
listener
.incoming
()
{
match
stream
{
Ok
(
mut
stream
)
=>
{
controller
.hello
(
&
mut
stream
);
loop
{
match
stream
.read
(
&
mut
buffer
)
{
Ok
(
v
)
if
v
>
0
=>
{
controller
.request_handler
(
&
mut
buffer
,
&
mut
stream
);
}
Ok
(
_
)
|
Err
(
_
)
=>
panic!
(
"Connection failed!"
),
}
}
}
Err
(
_
)
=>
todo!
(),
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment