3 from liteeth
.common
import *
4 from liteeth
.test
.common
import *
6 from liteeth
.test
.model
import ip
9 print_with_prefix(s
, "[ICMP]")
12 class ICMPPacket(Packet
):
13 def __init__(self
, init
=[]):
14 Packet
.__init
__(self
, init
)
18 for byte
in self
[:icmp_header_len
]:
19 header
.append(self
.pop(0))
20 for k
, v
in sorted(icmp_header
.items()):
21 setattr(self
, k
, get_field_data(v
, header
))
25 for k
, v
in sorted(icmp_header
.items()):
26 value
= merge_bytes(split_bytes(getattr(self
, k
), math
.ceil(v
.width
/8)), "little")
27 header
+= (value
<< v
.offset
+(v
.byte
*8))
28 for d
in split_bytes(header
, icmp_header_len
):
33 for k
in sorted(icmp_header
.keys()):
34 r
+= k
+ " : 0x%x" %getattr
(self
,k
) + "\n"
41 def __init__(self
, ip
, ip_address
, debug
=False):
43 self
.ip_address
= ip_address
46 self
.tx_packet
= ICMPPacket()
47 self
.rx_packet
= ICMPPacket()
49 self
.ip
.set_icmp_callback(self
.callback
)
51 def send(self
, packet
):
54 print_icmp(">>>>>>>>")
56 ip_packet
= ip
.IPPacket(packet
)
57 ip_packet
.version
= 0x4
59 ip_packet
.total_length
= len(packet
) + ip_packet
.ihl
60 ip_packet
.identification
= 0
62 ip_packet
.fragment_offset
= 0
64 ip_packet
.sender_ip
= self
.ip_address
65 ip_packet
.target_ip
= 0x12345678 # XXX
66 ip_packet
.checksum
= 0
67 ip_packet
.protocol
= icmp_protocol
68 self
.ip
.send(ip_packet
)
70 def callback(self
, packet
):
71 packet
= ICMPPacket(packet
)
74 print_icmp("<<<<<<<<")
78 def process(self
, packet
):
81 if __name__
== "__main__":
82 from liteeth
.test
.model
.dumps
import *
83 from liteeth
.test
.model
.mac
import *
84 from liteeth
.test
.model
.ip
import *
87 packet
= MACPacket(ping_request
)
88 packet
.decode_remove_header()
90 packet
= IPPacket(packet
)
93 packet
= ICMPPacket(packet
)
96 errors
+= verify_packet(packet
, ping_request_infos
)
100 errors
+= verify_packet(packet
, ping_request_infos
)
102 print("icmp errors " + str(errors
))