Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
lldp_increment_step, lldp_increment_count = self._check_increment(lldp_sa_increment, "lldp_sa_increment")
udf_id = len(self.udf_dict) + 1
lldp_tlv = hex_data[28:34]
lldp_mac_initval = hex_data[38:46]
if lldp_tlv == '020704':
self.udf_dict["lldp_sa"] = {"udf_id": udf_id, "initval": lldp_mac_initval, "offset": 19, "counter_type": 'c32', "step": lldp_increment_step,
"count": lldp_increment_count, "continuous": continuous}
tcl_commands.extend(self._set_ixia_udf_field(**self.udf_dict["lldp_sa"]))
else:
raise TypeError("lldp tlv is not '\\x02\\x07\\x04'.")
# Set ether type increment
if eth_type_increment is not None:
eth_type_increment_step, eth_type_increment_count = self._check_increment(eth_type_increment, "eth_type_increment")
udf_id = len(self.udf_dict) + 1
eth_type = packet[pypacker.layer12.ethernet.Ethernet].type
eth_type_initval = str(hex(eth_type))[2:].zfill(4)
self.udf_dict["eth_type"] = {"udf_id": udf_id, "initval": eth_type_initval, "offset": 12, "counter_type": 'c16', "step": eth_type_increment_step,
"count": eth_type_increment_count, "continuous": continuous}
tcl_commands.extend(self._set_ixia_udf_field(**self.udf_dict["eth_type"]))
# Set dhcp siaddr increment
if dhcp_si_increment is not None:
dhcp_si_increment_step, dhcp_si_increment_count = self._check_increment(dhcp_si_increment, "dhcp_si_increment")
udf_id = len(self.udf_dict) + 1
dhcp_si = packet.getlayer(pypacker.BOOTP).siaddr.split('.') # pylint: disable=no-member
dhcp_si_initval = str(hex(int(dhcp_si[0])))[2:].zfill(2) + str(hex(int(dhcp_si[1])))[2:].zfill(2) + \
str(hex(int(dhcp_si[2])))[2:].zfill(2) + str(hex(int(dhcp_si[3])))[2:].zfill(2)
if packet.vlan:
offset = 66
else:
offset = 62
def test_socket(self):
print_header("Sockets")
packet_eth = ethernet.Ethernet() + ip.IP(src_s="192.168.178.27", dst_s="173.194.113.183") + tcp.TCP(
dport=80)
packet_ip = ip.IP(src_s="192.168.178.27", dst_s="173.194.113.183") + tcp.TCP(dport=80)
# Layer 2 Socket
socket = SocketHndl(iface_name="eth1", mode=SocketHndl.MODE_LAYER_2)
# socket.send(packet_eth.bin())
packets = socket.sr(packet_eth)
for p in packets:
print(">>> %s" % p)
socket.close()
# Layer 3 Socket
socket = SocketHndl(iface_name="eth1", mode=SocketHndl.MODE_LAYER_3)
# socket.send(packet_ip.bin())
packets = socket.sr(packet_ip)
for p in packets:
def test_bgp(self):
print_header("BGP")
packet_bytes = get_pcap("tests/packets_bgp.pcap")
# parsing
bgp1_bytes = packet_bytes[0]
bgp1 = ethernet.Ethernet(bgp1_bytes)
bgp2_bytes = packet_bytes[1]
bgp2 = ethernet.Ethernet(bgp2_bytes)
bgp3_bytes = packet_bytes[2]
bgp3 = ethernet.Ethernet(bgp3_bytes)
self.assertEqual(bgp1.bin(), bgp1_bytes)
self.assertEqual(bgp2.bin(), bgp2_bytes)
self.assertEqual(bgp3.bin(), bgp3_bytes)
def test_dissectfail(self):
print_header("Dissectfail")
tcp_bytes_fail = b"\x00" * 16
pkt1 = ethernet.Ethernet() + ip.IP() + tcp_bytes_fail
pkt1_bts = pkt1.bin()
self.assertTrue(pkt1_bts.endswith(tcp_bytes_fail))
pkt1 = ethernet.Ethernet(pkt1_bts)
pkt_tcp = pkt1.upper_layer.upper_layer
print("TCP for dissectfail #1: %r" % pkt_tcp)
self.assertIsNone(pkt_tcp)
pkt1 = ethernet.Ethernet(pkt1_bts)
pkt_tcp = pkt1.ip.tcp
print("TCP for dissectfail #2: %r" % pkt_tcp)
self.assertIsNone(pkt_tcp)
# retrieving body type on failed dissect only works 1st time (returns None)
# 2nd time raises Exception
# pkt_tcp = pkt1.ip.tcp
self.assertRaises(Exception, lambda: pkt1.ip.tcp)
def test_repr(self):
# TODO: activate
print_header("__repr__")
bts_list = get_pcap("tests/packets_ssl.pcap")
for bts in bts_list:
eth = ethernet.Ethernet(bts)
print("%r" % eth)
eth1 = ethernet.Ethernet(bts)
eth1[tcp.TCP].body_bytes = b"qwertz"
eth1.bin()
tcp_sum_original = eth1[tcp.TCP].sum
eth1[tcp.TCP].body_bytes = b"asdfgh"
# ip checksum should be recalculated
tmp = "%r" % eth1
self.assertNotEqual(tcp_sum_original, eth1[tcp.TCP].sum)
# original checksum value should be calculated
eth1[tcp.TCP].body_bytes = b"qwertz"
tmp = "%r" % eth1
self.assertEqual(tcp_sum_original, eth1[tcp.TCP].sum)
print_header("Keyword creation")
eth = ethernet.Ethernet()
# print(str(eth))
self.assertEqual(eth.bin(), b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x08\x00")
eth = ethernet.Ethernet(dst=b"\x00\x01\x02\x03\x04\x05", src=b"\x06\x07\x08\x09\x0A\x0B", type=2048)
print(str(eth))
print(eth.bin())
self.assertEqual(eth.bin(), b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x08\x00")
# test packet creation (default, keyword, bytes + keyword)
bts = get_pcap("tests/packets_ether.pcap")[0]
eth = ethernet.Ethernet()
self.assertEqual(eth.src_s, "FF:FF:FF:FF:FF:FF")
eth = ethernet.Ethernet(src=b"\xAA\xAA\xAA\xAA\xAA\xAA")
self.assertEqual(eth.src_s, "AA:AA:AA:AA:AA:AA")
eth = ethernet.Ethernet(dst=b"\xAA\xAA\xAA\xAA\xAA\xAA")
self.assertEqual(eth.dst_s, "AA:AA:AA:AA:AA:AA")
def test_pause(self):
print_header("FLOW CONTROL PAUSE")
# Pause frame
raw_pkt = b"\x01\x80\xc2\x00\x00\x01\x00\x00\x00\x00\x00\xaa\x88\x08\x00\x01\x00\x03"
pkt = ethernet.Ethernet(raw_pkt)
# parsing
self.assertEqual(pkt.bin(), raw_pkt)
self.assertEqual(pkt.dst_s, "01:80:C2:00:00:01")
self.assertEqual(pkt[flow_control.FlowControl].opcode, flow_control.PAUSE_OPCODE)
self.assertEqual(type(pkt[flow_control.FlowControl].pause).__name__, "Pause")
self.assertEqual(pkt[flow_control.FlowControl].pause.ptime, 3)
def test_len(self):
print_header("Length")
bts_list = get_pcap("tests/packets_ssl.pcap")
for bts in bts_list:
eth = ethernet.Ethernet(bts)
print("%d = %d" % (len(bts), len(eth)))
self.assertEqual(len(bts), len(eth))
def test_handlerid_update(self):
print_header("Auto update of handler id")
# auto type-id setting for Ethernet
print("Ethernet...")
eth_1 = ethernet.Ethernet(type=0)
ip_1 = ip.IP()
pkt = eth_1 + ip_1
self.assertEqual(pkt.type, 0)
pkt.bin()
self.assertEqual(pkt.type, ethernet.ETH_TYPE_IP)
# auto type-id setting for IP
print("IP...")
ip_2 = ip.IP(p=0)
tcp_2 = tcp.TCP()
pkt = ip_2 + tcp_2
self.assertEqual(pkt.p, 0)
pkt.bin()
self.assertEqual(pkt.p, ip.IP_PROTO_TCP)
# auto type-id setting for TCP
print(udp1)
udp_bin = udp1.bin()
print(udp1)
print(udp1.ulen)
print(udp_bin)
print(udp1.sum)
print("sum 1: %X" % udp1.sum)
self.assertEqual(udp1.sum, 0xf6eb)
# print("setting new port")
udp1.dport = 1234
udp1.bin()
print("sum 2: %X" % udp1.sum)
self.assertEqual(udp1.sum, 0xf24e)
udp2 = ethernet.Ethernet() + ip.IP() + udp.UDP()
udp2[udp.UDP].body_bytes = b"A" * 10
udp2.bin()
self.assertEqual(udp2[udp.UDP].sum, 0xDAD6)
udp2[udp.UDP].body_bytes = b"A" * 11
udp2.bin()
self.assertEqual(udp2[udp.UDP].sum, 0x99D4)