Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GNS3_unikernel_testbed
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
NGUYEN Do Duc Anh
GNS3_unikernel_testbed
Commits
c4d84d92
Commit
c4d84d92
authored
5 months ago
by
NGUYEN Do Duc Anh
Browse files
Options
Downloads
Patches
Plain Diff
update check magic_hdr in broadcast message
parent
8388279d
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
ubuntu_base/opportunistic_node.py
+17
-5
17 additions, 5 deletions
ubuntu_base/opportunistic_node.py
ubuntu_base/opportunistic_utils.py
+3
-3
3 additions, 3 deletions
ubuntu_base/opportunistic_utils.py
ubuntu_base/vc_node.py
+29
-68
29 additions, 68 deletions
ubuntu_base/vc_node.py
with
49 additions
and
76 deletions
ubuntu_base/opportunistic_node.py
+
17
−
5
View file @
c4d84d92
...
...
@@ -3,11 +3,12 @@ from opportunistic_utils import *
from
scapy.layers.inet
import
IP
import
if_utils
# event_queue_firewall = if_utils.EventDrivenQueue(setup_firewall)
interface_macs
=
{}
def
process_packet
(
new_packet
):
#
global
event_queue_firewall
global
interface_macs
inter_num
=
new_packet
.
get_mark
()
# Determine the incoming interface based on the mark
...
...
@@ -19,7 +20,14 @@ def process_packet(new_packet):
# Parse the raw data into a Scapy IP packet
pkt
=
IP
(
data
)
# Check if it's a TCP packet
if
pkt
.
haslayer
(
Ether
)
and
pkt
[
Ether
].
dst
==
"
ff:ff:ff:ff:ff:ff
"
and
pkt
.
haslayer
(
Raw
)
and
len
(
pkt
.
layers
())
==
2
:
raw_data
=
pkt
[
Raw
].
load
if
len
(
raw_data
)
==
28
:
magic_hdr_pkt
=
struct
.
unpack
(
'
!I
'
,
raw_data
[:
4
])[
0
]
if
magic_hdr_pkt
==
magic_hdr
:
new_packet
.
drop
()
return
if
pkt
.
haslayer
(
IP
):
# print(f"IP Packet: {pkt.summary()}")
# pkt.show()
...
...
@@ -59,7 +67,7 @@ def process_packet(new_packet):
new_packet
.
set_payload
(
bytes
(
pkt
))
# pkt.show() # Now this will work correctly
new_packet
.
accept
()
new_packet
.
accept
()
def
process_opportunistic_node
(
node_id
):
...
...
@@ -84,7 +92,11 @@ def process_opportunistic_node(node_id):
# print("Rule added successfully.")
def
start_process_from_queue
():
def
start_process_from_queue
(
node_interfaces
):
global
interface_macs
interface_macs
=
node_interfaces
q
=
NetfilterQueue
()
q
.
bind
(
1
,
process_packet
)
# Bind to queue number 1
...
...
This diff is collapsed.
Click to expand it.
ubuntu_base/opportunistic_utils.py
+
3
−
3
View file @
c4d84d92
...
...
@@ -7,6 +7,7 @@ import struct, ipaddress
from
scapy.layers.l2
import
Ether
MY_OPTION_TYPE
=
31
magic_hdr
=
int
(
"
0x31323334
"
,
16
)
class
Cmd
:
...
...
@@ -54,7 +55,6 @@ class CustomIPOption(IPOption):
class
FirewallOpportunistic
:
firewall_queue
=
queue
.
Queue
()
map_firewall
=
{}
magic_hdr
=
int
(
"
0x31323334
"
,
16
)
@staticmethod
def
is_firewall_ready
(
pkt
):
...
...
@@ -64,7 +64,7 @@ class FirewallOpportunistic:
raw_data
=
pkt
[
Raw
].
load
magic_hdr_pkt
,
cmd
=
struct
.
unpack
(
'
!IB
'
,
raw_data
)
# print(f"Found packet with magic_hdr_pkt={magic_hdr_pkt} and cmd={cmd} and my_magic_hdr={magic_hdr}")
if
magic_hdr_pkt
==
FirewallOpportunistic
.
magic_hdr
and
cmd
==
Cmd
.
UPDATE_RULE
:
if
magic_hdr_pkt
==
magic_hdr
and
cmd
==
Cmd
.
UPDATE_RULE
:
print
(
"
Firewall unikernel ready! Start applying the rule
"
)
return
True
return
False
...
...
@@ -80,7 +80,7 @@ class FirewallOpportunistic:
dst_mac
=
"
00:00:00:00:00:00
"
# Replace with the destination MAC address
src_mac
=
"
00:00:00:00:00:00
"
# Replace with your source MAC address
ip_bytes
=
ipaddress
.
ip_address
(
ip_att
).
packed
message
=
struct
.
pack
(
'
!IB4s
'
,
FirewallOpportunistic
.
magic_hdr
,
cmd
,
ip_bytes
)
message
=
struct
.
pack
(
'
!IB4s
'
,
magic_hdr
,
cmd
,
ip_bytes
)
sendp
(
Ether
(
src
=
src_mac
,
dst
=
dst_mac
)
/
Raw
(
load
=
message
),
iface
=
private_if
)
# Specify the interface
print
(
f
"
Update rule packet sent to
{
private_if
}
"
)
...
...
This diff is collapsed.
Click to expand it.
ubuntu_base/vc_node.py
+
29
−
68
View file @
c4d84d92
...
...
@@ -11,7 +11,7 @@ from scapy.layers.l2 import Ether
from
if_utils
import
enable_br_netfilter
,
create_bridge
,
BR_CENTER_NAME
from
opportunistic_node
import
process_opportunistic_node
,
start_process_from_queue
from
opportunistic_utils
import
update_unikernel
,
IdsOpportunistic
from
opportunistic_utils
import
update_unikernel
,
IdsOpportunistic
,
magic_hdr
# Configuration
LISTEN_PORT
=
12346
# Specify the port number to listen on
...
...
@@ -22,10 +22,6 @@ client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket
.
connect
((
'
192.168.1.10
'
,
12346
))
# stop_flag = False
# DIR = "/home/node/Desktop/gns3_unikernel_testbed"
class
State
:
INIT
=
0
COMPUTING
=
1
...
...
@@ -67,15 +63,15 @@ class NeighborNode:
map_neighbor
=
{}
node
=
VcNode
(
State
.
INIT
,
0
,
0
,
0
,
[]
)
node
=
VcNode
(
State
.
INIT
,
0
,
0
,
0
,
{}
)
# Function to broadcast an Ethernet packet
def
broadcast_packet
(
message
):
global
node
for
iface
in
node
.
interface
:
src_mac
=
get_if_hwaddr
(
iface
)
for
iface
in
node
.
interface
.
keys
()
:
src_mac
=
node
.
interface
[
iface
]
dst_mac
=
"
ff:ff:ff:ff:ff:ff
"
packet
=
Ether
(
src
=
src_mac
,
dst
=
dst_mac
)
/
Raw
(
load
=
message
)
# Broadcast MAC address
sendp
(
packet
,
iface
=
iface
)
# Specify the interface
...
...
@@ -104,25 +100,26 @@ def process_packet(iface, my_mac):
if
packet
[
Ether
].
dst
==
my_mac
or
(
packet
[
Ether
].
src
!=
my_mac
and
packet
[
Ether
].
dst
==
"
ff:ff:ff:ff:ff:ff
"
):
raw_data
=
packet
[
Raw
].
load
print
(
"
raw data from iface
"
,
iface
,
"
:
"
,
raw_data
)
cmd
=
struct
.
unpack
(
'
!I
'
,
raw_data
[:
4
])[
0
]
if
cmd
==
Cmd
.
REGISTER
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
4
:])
if
neighbor_id
not
in
map_neighbor
:
message
=
struct
.
pack
(
'
!IIIIII
'
,
Cmd
.
REPLY_REGISTER
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
send_packet
(
iface
,
packet
[
Ether
].
src
,
message
)
# update_neighbor_info_queue.put(NeighborNode(neighbor_id, neighbor_degree, neighbor_color, neighbor_color_id, neighbor_pointer, packet[Ether].src, iface))
elif
cmd
==
Cmd
.
REPLY_REGISTER
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
4
:])
if
neighbor_id
not
in
map_neighbor
:
map_neighbor
[
neighbor_id
]
=
NeighborNode
(
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
,
packet
[
Ether
].
src
,
iface
)
node
.
degree
=
len
(
map_neighbor
)
node
.
color
=
node
.
degree
print
(
f
"
My degree now is:
{
node
.
degree
}
"
)
elif
cmd
==
Cmd
.
SHARE_INFO
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
4
:])
update_neighbor_info_queue
.
put
(
NeighborNode
(
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
,
packet
[
Ether
].
src
,
iface
))
magic_hdr_pkt
=
struct
.
unpack
(
'
!I
'
,
raw_data
[:
4
])[
0
]
if
magic_hdr_pkt
==
magic_hdr
:
cmd
=
struct
.
unpack
(
'
!I
'
,
raw_data
[
4
:
8
])[
0
]
if
cmd
==
Cmd
.
REGISTER
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
8
:])
if
neighbor_id
not
in
map_neighbor
:
message
=
struct
.
pack
(
'
!IIIIIII
'
,
magic_hdr
,
Cmd
.
REPLY_REGISTER
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
send_packet
(
iface
,
packet
[
Ether
].
src
,
message
)
# update_neighbor_info_queue.put(NeighborNode(neighbor_id, neighbor_degree, neighbor_color, neighbor_color_id, neighbor_pointer, packet[Ether].src, iface))
elif
cmd
==
Cmd
.
REPLY_REGISTER
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
8
:])
if
neighbor_id
not
in
map_neighbor
:
map_neighbor
[
neighbor_id
]
=
NeighborNode
(
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
,
packet
[
Ether
].
src
,
iface
)
node
.
degree
=
len
(
map_neighbor
)
node
.
color
=
node
.
degree
print
(
f
"
My degree now is:
{
node
.
degree
}
"
)
elif
cmd
==
Cmd
.
SHARE_INFO
:
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
=
struct
.
unpack
(
'
!IIIII
'
,
raw_data
[
8
:])
update_neighbor_info_queue
.
put
(
NeighborNode
(
neighbor_id
,
neighbor_degree
,
neighbor_color
,
neighbor_color_id
,
neighbor_pointer
,
packet
[
Ether
].
src
,
iface
))
return
packet_callback
# print(f"Received packet: {packet.summary()}")
...
...
@@ -179,12 +176,6 @@ def apply_rule():
if
neighbor
.
pointer
==
node
.
myid
:
pointing_neighbor_id_set
.
append
(
neighbor_id
)
# if neighbor.color == node.color and node.color_id == node.myid and neighbor.color_id == node.myid and node.pointer == neighbor.id:
# low.append(neighbor)
# elif neighbor.color == node.color and node.color_id == neighbor.id and neighbor.color_id == neighbor.id and node.pointer == neighbor.id:
# high.append(neighbor)
# elif neighbor.pointer != 0:
else
:
# Other set is all neighbors that are not Proposer
if
neighbor
.
pointer
==
0
or
(
neighbor
.
pointer
!=
0
and
neighbor
.
color_id
!=
neighbor
.
id
):
...
...
@@ -260,19 +251,8 @@ def apply_rule():
if
is_enable
:
node
.
state
=
State
.
DECIDED_NOT_VC
print
(
"
Enable Rule 6
"
)
# else:
# is_enable = False
elif
node
.
state
==
State
.
DECIDED_NOT_VC
:
# for neighbor in map_neighbor.values():
# # neighbor.pointer != 0 and
# if neighbor.pointer != node.myid and (
# neighbor.degree < node.degree or (neighbor.degree == node.degree and neighbor.id < node.myid)):
# node.state = State.DECIDED_IN_VC
# is_enable = True
# print("Enable Rule 6")
# break
if
other
:
for
n_other
in
other
:
if
n_other
.
degree
<
node
.
degree
or
(
n_other
.
degree
==
node
.
degree
and
n_other
.
id
<
node
.
myid
):
...
...
@@ -280,9 +260,6 @@ def apply_rule():
is_enable
=
True
print
(
"
Enable Rule 6
"
)
break
# else:
# is_enable = True
# node.state = State.DECIDED_IN_VC
return
is_enable
...
...
@@ -333,7 +310,7 @@ def update_neighbor_info():
send_node_info
()
for
neighbor
in
map_neighbor
.
values
():
message
=
struct
.
pack
(
'
!IIIIII
'
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
message
=
struct
.
pack
(
'
!IIIIII
I
'
,
magic_hdr
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
send_packet
(
neighbor
.
iface
,
neighbor
.
mac
,
message
)
node
.
converge_time
=
time
.
time
()
...
...
@@ -344,7 +321,7 @@ def update_neighbor_info():
print
(
"
My current state:
"
,
node
.
state
)
for
neighbor
in
map_neighbor
.
values
():
message
=
struct
.
pack
(
'
!IIIIII
'
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
message
=
struct
.
pack
(
'
!IIIIII
I
'
,
magic_hdr
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
send_packet
(
neighbor
.
iface
,
neighbor
.
mac
,
message
)
node
.
converge_time
=
time
.
time
()
...
...
@@ -396,14 +373,14 @@ def proceed_self_stabilizing_VC():
for
iface
,
state_iface
in
check_all_interfaces
().
items
():
if
iface
!=
"
lo
"
and
iface
!=
"
ens3
"
:
# and state_iface == "up"):
node
.
interface
.
append
(
iface
)
node
.
interface
[
iface
]
=
get_if_hwaddr
(
iface
)
print
(
"
Listen to interface
"
,
iface
)
sniffer_thread
=
threading
.
Thread
(
target
=
start_sniffing
,
args
=
[
iface
])
# sniffer_thread.daemon = True
sniffer_thread
.
start
()
# Broadcast myself to update degree
message
=
struct
.
pack
(
'
!IIIIII
'
,
Cmd
.
REGISTER
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
message
=
struct
.
pack
(
'
!IIIIII
I
'
,
magic_hdr
,
Cmd
.
REGISTER
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
turn
=
3
while
turn
:
broadcast_packet
(
message
)
...
...
@@ -411,7 +388,7 @@ def proceed_self_stabilizing_VC():
time
.
sleep
(
5
)
# Trigger a round of self-stabilisation on my neighbors
message
=
struct
.
pack
(
'
!IIIIII
'
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
message
=
struct
.
pack
(
'
!IIIIII
I
'
,
magic_hdr
,
Cmd
.
SHARE_INFO
,
node
.
myid
,
node
.
degree
,
node
.
color
,
node
.
color_id
,
node
.
pointer
)
broadcast_packet
(
message
)
# My initial state is NOT in VC and start the opportunistic process for firewall deployment
...
...
@@ -435,22 +412,6 @@ def proceed_self_stabilizing_VC():
is_ids_deployed
=
False
IdsOpportunistic
.
ids_queue
.
put
((
node
.
myid
,
time
.
time
(),
is_ids_deployed
))
#
# # Continue process based on Node decision
# if node.state == State.DECIDED_IN_VC:
# process_opportunistic_monitor(node.myid)
# start_ids_unikernel()
# elif node.state == State.DECIDED_NOT_VC:
# process_opportunistic_node(node.myid)
# else:
# print("Error I cannot decide!!")
# sys.exit()
# print("My node ID is:", node.myid, "- in VC" if node.state == State.DECIDED_IN_VC else "- NOT in VC")
# start_process_from_queue()
# Exit the current script
def
main
():
wait_for_ip
(
"
ens3
"
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment