1
2
3 """Internet Control Message Protocol."""
4
5 import dpkt, ip
6
7
8
9
10 ICMP_CODE_NONE = 0
11 ICMP_ECHOREPLY = 0
12 ICMP_UNREACH = 3
13 ICMP_UNREACH_NET = 0
14 ICMP_UNREACH_HOST = 1
15 ICMP_UNREACH_PROTO = 2
16 ICMP_UNREACH_PORT = 3
17 ICMP_UNREACH_NEEDFRAG = 4
18 ICMP_UNREACH_SRCFAIL = 5
19 ICMP_UNREACH_NET_UNKNOWN = 6
20 ICMP_UNREACH_HOST_UNKNOWN = 7
21 ICMP_UNREACH_ISOLATED = 8
22 ICMP_UNREACH_NET_PROHIB = 9
23 ICMP_UNREACH_HOST_PROHIB = 10
24 ICMP_UNREACH_TOSNET = 11
25 ICMP_UNREACH_TOSHOST = 12
26 ICMP_UNREACH_FILTER_PROHIB = 13
27 ICMP_UNREACH_HOST_PRECEDENCE = 14
28 ICMP_UNREACH_PRECEDENCE_CUTOFF = 15
29 ICMP_SRCQUENCH = 4
30 ICMP_REDIRECT = 5
31 ICMP_REDIRECT_NET = 0
32 ICMP_REDIRECT_HOST = 1
33 ICMP_REDIRECT_TOSNET = 2
34 ICMP_REDIRECT_TOSHOST = 3
35 ICMP_ALTHOSTADDR = 6
36 ICMP_ECHO = 8
37 ICMP_RTRADVERT = 9
38 ICMP_RTRADVERT_NORMAL = 0
39 ICMP_RTRADVERT_NOROUTE_COMMON = 16
40 ICMP_RTRSOLICIT = 10
41 ICMP_TIMEXCEED = 11
42 ICMP_TIMEXCEED_INTRANS = 0
43 ICMP_TIMEXCEED_REASS = 1
44 ICMP_PARAMPROB = 12
45 ICMP_PARAMPROB_ERRATPTR = 0
46 ICMP_PARAMPROB_OPTABSENT = 1
47 ICMP_PARAMPROB_LENGTH = 2
48 ICMP_TSTAMP = 13
49 ICMP_TSTAMPREPLY = 14
50 ICMP_INFO = 15
51 ICMP_INFOREPLY = 16
52 ICMP_MASK = 17
53 ICMP_MASKREPLY = 18
54 ICMP_TRACEROUTE = 30
55 ICMP_DATACONVERR = 31
56 ICMP_MOBILE_REDIRECT = 32
57 ICMP_IP6_WHEREAREYOU = 33
58 ICMP_IP6_IAMHERE = 34
59 ICMP_MOBILE_REG = 35
60 ICMP_MOBILE_REGREPLY = 36
61 ICMP_DNS = 37
62 ICMP_DNSREPLY = 38
63 ICMP_SKIP = 39
64 ICMP_PHOTURIS = 40
65 ICMP_PHOTURIS_UNKNOWN_INDEX = 0
66 ICMP_PHOTURIS_AUTH_FAILED = 1
67 ICMP_PHOTURIS_DECOMPRESS_FAILED = 2
68 ICMP_PHOTURIS_DECRYPT_FAILED = 3
69 ICMP_PHOTURIS_NEED_AUTHN = 4
70 ICMP_PHOTURIS_NEED_AUTHZ = 5
71 ICMP_TYPE_MAX = 40
72
73 -class ICMP(dpkt.Packet):
74 __hdr__ = (
75 ('type', 'B', 8),
76 ('code', 'B', 0),
77 ('sum', 'H', 0)
78 )
79 - class Echo(dpkt.Packet):
80 __hdr__ = (('id', 'H', 0), ('seq', 'H', 0))
81 - class Quote(dpkt.Packet):
87 __hdr__ = (('pad', 'H', 0), ('mtu', 'H', 0))
93 __hdr__ = (('ptr', 'B', 0), ('pad1', 'B', 0), ('pad2', 'H', 0))
96
97 _typesw = { 0:Echo, 3:Unreach, 4:Quench, 5:Redirect, 8:Echo,
98 11:TimeExceed }
99
107
112
113 if __name__ == '__main__':
114 import unittest
115
118 s = '\x03\x0a\x6b\x19\x00\x00\x00\x00\x45\x00\x00\x28\x94\x1f\x00\x00\xe3\x06\x99\xb4\x23\x2b\x24\x00\xde\x8e\x84\x42\xab\xd1\x00\x50\x00\x35\xe1\x29\x20\xd9\x00\x00\x00\x22\x9b\xf0\xe2\x04\x65\x6b'
119 icmp = ICMP(s)
120 self.failUnless(str(icmp) == s)
121
122 unittest.main()
123