def test_ethernet_packet_packs(self): expected_packed_message = bytes.fromhex( "0180c2000003001906eab88c888e0100000501010005010000") message = EthernetPacket(dst_mac=MacAddress.from_string("01:80:c2:00:00:03"), src_mac=MacAddress.from_string("00:19:06:ea:b8:8c"), ethertype=0x888e, data=bytes.fromhex("0100000501010005010000")) packed_message = message.pack() self.assertEqual(expected_packed_message, packed_message)
def ethernet_pack(message, src_mac, dst_mac): """Packs a ethernet packet. Args: message: EAP payload src_mac (MacAddress): dst_mac (MacAddress): Returns: packed ethernet packet (bytes) """ data = MessagePacker.pack(message) ethernet_packet = EthernetPacket(dst_mac=dst_mac, src_mac=src_mac, ethertype=0x888e, data=data) return ethernet_packet.pack()
def test_ethernet_packet_parses(self): packed_message = bytes.fromhex("0180c2000003001906eab88c888e0100000501010005010000") message = EthernetPacket.parse(packed_message) self.assertEqual(message.src_mac, MacAddress.from_string("00:19:06:ea:b8:8c")) self.assertEqual(message.dst_mac, MacAddress.from_string("01:80:c2:00:00:03")) self.assertEqual(message.ethertype, 0x888e) self.assertEqual(message.data, bytes.fromhex("0100000501010005010000"))
def receive_eth_packet(self): """Receive Ethernet Frame and send to State Machine""" rad_queue_size = self.radius_output_queue.qsize() message = EthernetPacket(self.PORT_ID_MAC, str(self.src_mac), 0x888e, "") self.sm.event(EventMessageReceived(message, self.PORT_ID_MAC)) self.assertEqual(self.radius_output_queue.qsize(), rad_queue_size + 1)
def send_eth_to_state_machine(self, packed_message): """Send an ethernet frame to MAB State Machine""" ethernet_packet = EthernetPacket.parse(packed_message) port_id = ethernet_packet.dst_mac src_mac = ethernet_packet.src_mac self.logger.info("Sending MAC to MAB State Machine: %s", src_mac) message_id = -2 state_machine = self.get_state_machine(src_mac, port_id, message_id) event = EventMessageReceived(ethernet_packet, port_id) state_machine.event(event)
def ethernet_parse(packed_message): """Parses the ethernet header part, and payload Args: packed_message: Returns: ***Message & destination mac address. Raises: MessageParseError: the packed_message cannot be parsed.""" ethernet_packet = EthernetPacket.parse(packed_message) if ethernet_packet.ethertype != 0x888e: raise MessageParseError( "Ethernet packet with bad ethertype received: %s" % ethernet_packet) return MessageParser.one_x_parse(ethernet_packet.data, ethernet_packet.src_mac), \ ethernet_packet.dst_mac