Skip to content
Snippets Groups Projects
Commit c4d84d92 authored by NGUYEN Do Duc Anh's avatar NGUYEN Do Duc Anh
Browse files

update check magic_hdr in broadcast message

parent 8388279d
Branches
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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}")
......
......@@ -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('!IIIIIII', 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('!IIIIIII', 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('!IIIIIII', 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('!IIIIIII', 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")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment