def testBuildMatch(self): pkt = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8", protocol=6) + \ TCP(srcport=4000, dstport=10000) m = OpenflowMatch.build_from_packet(pkt) self.assertEqual(m.dl_src, "11:22:33:44:55:66") self.assertEqual(m.dl_dst, "aa:bb:cc:dd:ee:ff") self.assertEqual(m.dl_vlan, 0) self.assertEqual(m.dl_vlan_pcp, 0) self.assertEqual(m.nw_proto.value, 6) self.assertEqual(m.nw_tos, 0) self.assertEqual(str(m.nw_src), "1.2.3.4") self.assertEqual(str(m.nw_dst), "5.6.7.8") self.assertEqual(m.tp_src, 4000) self.assertEqual(m.tp_dst, 10000)
def testPacketMatch(self): pkt = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8", protocol=6) + \ TCP(srcport=4000, dstport=10000) m = OpenflowMatch.build_from_packet(pkt) self.assertTrue(m.matches_packet(pkt)) pkt[TCP].srcport = 42 self.assertFalse(m.matches_packet(pkt)) m.add_wildcard(OpenflowWildcard.TpSrc) self.assertTrue(m.matches_packet(pkt)) pkt[IPv4].src = "1.2.3.0" self.assertFalse(m.matches_packet(pkt)) m.nwsrc_wildcard = 8 self.assertTrue(m.matches_packet(pkt))
def _storePkt(self, ofhdr, dstport=6633): # comment the return to save pcap packets for analysis return pkt = (Ethernet(src="11:22:33:44:55:66") + IPv4() + TCP()) + ofhdr xname = pkt[OpenflowHeader].type.name dumper = PcapDumper("oftests_{}.pcap".format(xname)) pkt[Ethernet].dst = ''.join(list(reversed("11:22:33:44:55:66"))) pkt[IPv4].protocol = 6 pkt[IPv4].src = "149.43.80.25" pkt[IPv4].dst = "149.43.80.25" pkt[IPv4].ttl = 5 pkt[IPv4].ipid = 42 pkt[TCP].dstport = dstport pkt[TCP].srcport = 5555 pkt[TCP].window = 1000 pkt[TCP].seq = 42 pkt[TCP].ack = 500 pkt[TCP].PSH = 1 pkt[TCP].ACK = 1 dumper.write_packet(pkt.to_bytes(), time()) dumper.close()