#1800 - Fixed routing and processing of ICMP packets in the Router class
This commit is contained in:
@@ -744,14 +744,12 @@ class ICMP:
|
||||
:param frame: The Frame containing the ICMP packet to process.
|
||||
"""
|
||||
if frame.icmp.icmp_type == ICMPType.ECHO_REQUEST:
|
||||
if not is_reattempt:
|
||||
self.sys_log.info(f"Received echo request from {frame.ip.src_ip}")
|
||||
target_mac_address = self.arp.get_arp_cache_mac_address(frame.ip.src_ip)
|
||||
|
||||
src_nic = self.arp.get_arp_cache_nic(frame.ip.src_ip)
|
||||
if not src_nic:
|
||||
print(self.sys_log.hostname)
|
||||
print(frame.ip.src_ip)
|
||||
self.arp.show()
|
||||
self.arp.send_arp_request(frame.ip.src_ip)
|
||||
self.process_icmp(frame=frame, from_nic=from_nic, is_reattempt=True)
|
||||
return
|
||||
@@ -932,14 +930,13 @@ class Node(SimComponent):
|
||||
|
||||
def show(self):
|
||||
"""Prints a table of the NICs on the Node."""
|
||||
table = PrettyTable(["MAC Address", "Address", "Default Gateway", "Speed", "Status"])
|
||||
table = PrettyTable(["MAC Address", "Address", "Speed", "Status"])
|
||||
table.title = f"{self.hostname} Network Interface Cards"
|
||||
for nic in self.nics.values():
|
||||
table.add_row(
|
||||
[
|
||||
nic.mac_address,
|
||||
f"{nic.ip_address}/{nic.ip_network.prefixlen}",
|
||||
nic.gateway,
|
||||
nic.speed,
|
||||
"Enabled" if nic.enabled else "Disabled",
|
||||
]
|
||||
|
||||
@@ -367,8 +367,10 @@ class RouterICMP(ICMP):
|
||||
# determine if request is for router interface or whether it needs to be routed
|
||||
|
||||
for nic in self.router.nics.values():
|
||||
if nic.ip_address == frame.ip.dst_ip and nic.enabled:
|
||||
if nic.ip_address == frame.ip.dst_ip:
|
||||
if nic.enabled:
|
||||
# reply to the request
|
||||
if not is_reattempt:
|
||||
self.sys_log.info(f"Received echo request from {frame.ip.src_ip}")
|
||||
target_mac_address = self.arp.get_arp_cache_mac_address(frame.ip.src_ip)
|
||||
src_nic = self.arp.get_arp_cache_nic(frame.ip.src_ip)
|
||||
@@ -392,12 +394,20 @@ class RouterICMP(ICMP):
|
||||
|
||||
# Route the frame
|
||||
self.router.route_frame(frame, from_nic)
|
||||
|
||||
elif frame.icmp.icmp_type == ICMPType.ECHO_REPLY:
|
||||
for nic in self.router.nics.values():
|
||||
if nic.ip_address == frame.ip.dst_ip:
|
||||
if nic.enabled:
|
||||
self.sys_log.info(f"Received echo reply from {frame.ip.src_ip}")
|
||||
if not self.request_replies.get(frame.icmp.identifier):
|
||||
self.request_replies[frame.icmp.identifier] = 0
|
||||
self.request_replies[frame.icmp.identifier] += 1
|
||||
|
||||
return
|
||||
# Route the frame
|
||||
self.router.route_frame(frame, from_nic)
|
||||
|
||||
|
||||
class Router(Node):
|
||||
num_ports: int
|
||||
@@ -436,15 +446,23 @@ class Router(Node):
|
||||
pass
|
||||
|
||||
def route_frame(self, frame: Frame, from_nic: NIC, re_attempt: bool = False) -> None:
|
||||
if not re_attempt:
|
||||
# Check if src ip is on network of one of the NICs
|
||||
nic = self.arp.get_arp_cache_nic(frame.ip.dst_ip)
|
||||
target_mac = self.arp.get_arp_cache_mac_address(frame.ip.dst_ip)
|
||||
|
||||
if re_attempt and not nic:
|
||||
self.sys_log.info(f"Destination {frame.ip.dst_ip} is unreachable")
|
||||
return
|
||||
|
||||
if not nic:
|
||||
self.arp.send_arp_request(frame.ip.dst_ip)
|
||||
return self.route_frame(frame=frame, from_nic=from_nic, re_attempt=True)
|
||||
for nic in self.nics.values():
|
||||
if nic.enabled and frame.ip.dst_ip in nic.ip_network:
|
||||
|
||||
if not nic.enabled:
|
||||
# TODO: Add sys_log here
|
||||
return
|
||||
|
||||
if frame.ip.dst_ip in nic.ip_network:
|
||||
from_port = self._get_port_of_nic(from_nic)
|
||||
to_port = self._get_port_of_nic(nic)
|
||||
self.sys_log.info(f"Routing frame to internally from port {from_port} to port {to_port}")
|
||||
@@ -454,7 +472,8 @@ class Router(Node):
|
||||
nic.send_frame(frame)
|
||||
return
|
||||
else:
|
||||
self.sys_log.info(f"Destination {frame.ip.dst_ip} is unreachable")
|
||||
pass
|
||||
# TODO: Deal with routing from route tables
|
||||
|
||||
def receive_frame(self, frame: Frame, from_nic: NIC):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user