def on_frame_received(self, frame: Frame, port: int) -> None: print(f'[{self.sim_time:>6}] {self.name:>18} received:', frame) mac_dest = from_number_to_bit_data(frame.to_mac, 16) mac_dest_str = ''.join(map(str, mac_dest)) mac_origin = from_number_to_bit_data(frame.from_mac, 16) data_s = frame.frame_data_size data = frame.data # ARPQ protocol if data_s / 8 == 8: arpq = from_str_to_bin('ARPQ') ip = ''.join(map(str, data[32:64])) if mac_dest_str == '1' * 16: arpq_data = ''.join(map(str, data[:32])) ip_values = [i.str_binary for i in self.ips.values()] if arpq_data.endswith(arpq) and \ ip in ip_values: self.respond_arpq(mac_origin, port) else: new_ip = IP.from_bin(ip) self.ip_table[str(new_ip)] = mac_origin if str(new_ip) in self.waiting_for_arpq: for data in self.waiting_for_arpq[str(new_ip)]: self.send_frame(mac_origin, data, port) self.waiting_for_arpq[str(new_ip)] = [] return valid_packet, packet = IPPacket.parse(frame.data) if valid_packet: self.on_ip_packet_received(packet, port, frame)
def __str__(self) -> str: from_mac = from_bit_data_to_hex( from_number_to_bit_data(self.from_mac, 16)) to_mac = from_bit_data_to_hex(from_number_to_bit_data(self.to_mac, 16)) data = from_bit_data_to_hex(self.data) valid, packet = IPPacket.parse(self.data) if valid: data = str(packet) return f'{from_mac} -> {to_mac} | {data} | {self.additional_info}'
def __init__(self, dest_ip: IP, orig_ip: IP, payload: List[int], ttl: int = 0, protocol: int = 0) -> None: self.to_ip = dest_ip self.from_ip = orig_ip self.payload = payload self.ttl = from_number_to_bit_data(ttl) self.protocol = from_number_to_bit_data(protocol) self.protocol_number = protocol
def enroute(self, packet: IPPacket, port: int = 1, frame: Frame = None): """ Enruta un paquete IP. Parameters ---------- packet : IPPacket Paquete a enrutar. port : int, optional Puerto por el cual sale el paquete (interfase), por defecto 1. frame : Frame, optional Frame que contiene al paquete, por defecto None. """ route = self.get_enrouting(packet.to_ip) if route is None and frame is not None: data = IPPacket.no_dest_host(packet.from_ip, self.ips[port]).bit_data super().send_frame(from_number_to_bit_data(frame.from_mac, 16), data, port) return to_ip = route.gateway if route.gateway.raw_value == 0: to_ip = packet.to_ip super().send_ip_packet(packet, route.interface, to_ip)
def on_frame_received(self, frame: Frame, port: str) -> None: frame, error = self.check_errors(frame.bit_data) frame = Frame(frame) data_from = from_bit_data_to_hex( from_number_to_bit_data(frame.from_mac)) hex_data = from_bit_data_to_hex(frame.data) r_data = [self.sim_time, data_from, hex_data] if error: r_data.append('ERROR') else: super().on_frame_received(frame, 1) self.received_data.append(r_data)
def no_dest_host(dest_ip: IP, orig_ip: IP) -> IPPacket: payload = from_number_to_bit_data(3) return IPPacket(dest_ip, orig_ip, payload, ttl=0, protocol=1)