Package dpkt :: Module icmp
[hide private]
[frames] | no frames]

Source Code for Module dpkt.icmp

  1  # $Id: icmp.py 45 2007-08-03 00:05:22Z jon.oberheide $ 
  2  # -*- coding: utf-8 -*- 
  3  """Internet Control Message Protocol.""" 
  4  from __future__ import print_function 
  5  from __future__ import absolute_import 
  6   
  7  from . import dpkt 
  8   
  9  # Types (icmp_type) and codes (icmp_code) - 
 10  # http://www.iana.org/assignments/icmp-parameters 
 11   
 12  ICMP_CODE_NONE = 0  # for types without codes 
 13  ICMP_ECHOREPLY = 0  # echo reply 
 14  ICMP_UNREACH = 3  # dest unreachable, codes: 
 15  ICMP_UNREACH_NET = 0  # bad net 
 16  ICMP_UNREACH_HOST = 1  # bad host 
 17  ICMP_UNREACH_PROTO = 2  # bad protocol 
 18  ICMP_UNREACH_PORT = 3  # bad port 
 19  ICMP_UNREACH_NEEDFRAG = 4  # IP_DF caused drop 
 20  ICMP_UNREACH_SRCFAIL = 5  # src route failed 
 21  ICMP_UNREACH_NET_UNKNOWN = 6  # unknown net 
 22  ICMP_UNREACH_HOST_UNKNOWN = 7  # unknown host 
 23  ICMP_UNREACH_ISOLATED = 8  # src host isolated 
 24  ICMP_UNREACH_NET_PROHIB = 9  # for crypto devs 
 25  ICMP_UNREACH_HOST_PROHIB = 10  # ditto 
 26  ICMP_UNREACH_TOSNET = 11  # bad tos for net 
 27  ICMP_UNREACH_TOSHOST = 12  # bad tos for host 
 28  ICMP_UNREACH_FILTER_PROHIB = 13  # prohibited access 
 29  ICMP_UNREACH_HOST_PRECEDENCE = 14  # precedence error 
 30  ICMP_UNREACH_PRECEDENCE_CUTOFF = 15  # precedence cutoff 
 31  ICMP_SRCQUENCH = 4  # packet lost, slow down 
 32  ICMP_REDIRECT = 5  # shorter route, codes: 
 33  ICMP_REDIRECT_NET = 0  # for network 
 34  ICMP_REDIRECT_HOST = 1  # for host 
 35  ICMP_REDIRECT_TOSNET = 2  # for tos and net 
 36  ICMP_REDIRECT_TOSHOST = 3  # for tos and host 
 37  ICMP_ALTHOSTADDR = 6  # alternate host address 
 38  ICMP_ECHO = 8  # echo service 
 39  ICMP_RTRADVERT = 9  # router advertise, codes: 
 40  ICMP_RTRADVERT_NORMAL = 0  # normal 
 41  ICMP_RTRADVERT_NOROUTE_COMMON = 16  # selective routing 
 42  ICMP_RTRSOLICIT = 10  # router solicitation 
 43  ICMP_TIMEXCEED = 11  # time exceeded, code: 
 44  ICMP_TIMEXCEED_INTRANS = 0  # ttl==0 in transit 
 45  ICMP_TIMEXCEED_REASS = 1  # ttl==0 in reass 
 46  ICMP_PARAMPROB = 12  # ip header bad 
 47  ICMP_PARAMPROB_ERRATPTR = 0  # req. opt. absent 
 48  ICMP_PARAMPROB_OPTABSENT = 1  # req. opt. absent 
 49  ICMP_PARAMPROB_LENGTH = 2  # bad length 
 50  ICMP_TSTAMP = 13  # timestamp request 
 51  ICMP_TSTAMPREPLY = 14  # timestamp reply 
 52  ICMP_INFO = 15  # information request 
 53  ICMP_INFOREPLY = 16  # information reply 
 54  ICMP_MASK = 17  # address mask request 
 55  ICMP_MASKREPLY = 18  # address mask reply 
 56  ICMP_TRACEROUTE = 30  # traceroute 
 57  ICMP_DATACONVERR = 31  # data conversion error 
 58  ICMP_MOBILE_REDIRECT = 32  # mobile host redirect 
 59  ICMP_IP6_WHEREAREYOU = 33  # IPv6 where-are-you 
 60  ICMP_IP6_IAMHERE = 34  # IPv6 i-am-here 
 61  ICMP_MOBILE_REG = 35  # mobile registration req 
 62  ICMP_MOBILE_REGREPLY = 36  # mobile registration reply 
 63  ICMP_DNS = 37  # domain name request 
 64  ICMP_DNSREPLY = 38  # domain name reply 
 65  ICMP_SKIP = 39  # SKIP 
 66  ICMP_PHOTURIS = 40  # Photuris 
 67  ICMP_PHOTURIS_UNKNOWN_INDEX = 0  # unknown sec index 
 68  ICMP_PHOTURIS_AUTH_FAILED = 1  # auth failed 
 69  ICMP_PHOTURIS_DECOMPRESS_FAILED = 2  # decompress failed 
 70  ICMP_PHOTURIS_DECRYPT_FAILED = 3  # decrypt failed 
 71  ICMP_PHOTURIS_NEED_AUTHN = 4  # no authentication 
 72  ICMP_PHOTURIS_NEED_AUTHZ = 5  # no authorization 
 73  ICMP_TYPE_MAX = 40 
 74   
 75   
76 -class ICMP(dpkt.Packet):
77 """Internet Control Message Protocol. 78 79 TODO: Longer class information.... 80 81 Attributes: 82 __hdr__: Header fields of ICMP. 83 TODO. 84 """ 85 86 __hdr__ = ( 87 ('type', 'B', 8), 88 ('code', 'B', 0), 89 ('sum', 'H', 0) 90 ) 91
92 - class Echo(dpkt.Packet):
93 __hdr__ = (('id', 'H', 0), ('seq', 'H', 0))
94
95 - class Quote(dpkt.Packet):
96 __hdr__ = (('pad', 'I', 0),) 97
98 - def unpack(self, buf):
99 dpkt.Packet.unpack(self, buf) 100 from . import ip 101 self.data = self.ip = ip.IP(self.data)
102
103 - class Unreach(Quote):
104 __hdr__ = (('pad', 'H', 0), ('mtu', 'H', 0))
105
106 - class Quench(Quote):
107 pass
108
109 - class Redirect(Quote):
110 __hdr__ = (('gw', 'I', 0),)
111
112 - class ParamProbe(Quote):
113 __hdr__ = (('ptr', 'B', 0), ('pad1', 'B', 0), ('pad2', 'H', 0))
114
115 - class TimeExceed(Quote):
116 pass
117 118 _typesw = {0: Echo, 3: Unreach, 4: Quench, 5: Redirect, 8: Echo, 11: TimeExceed} 119
120 - def unpack(self, buf):
121 dpkt.Packet.unpack(self, buf) 122 try: 123 self.data = self._typesw[self.type](self.data) 124 setattr(self, self.data.__class__.__name__.lower(), self.data) 125 except (KeyError, dpkt.UnpackError): 126 pass
127
128 - def __bytes__(self):
129 if not self.sum: 130 self.sum = dpkt.in_cksum(dpkt.Packet.__bytes__(self)) 131 return dpkt.Packet.__bytes__(self)
132 133
134 -def test_icmp():
135 s = ( 136 b'\x03\x0a\x6b\x19\x00\x00\x00\x00\x45\x00\x00\x28\x94\x1f\x00\x00\xe3\x06\x99\xb4\x23\x2b' 137 b'\x24\x00\xde\x8e\x84\x42\xab\xd1\x00\x50\x00\x35\xe1\x29\x20\xd9\x00\x00\x00\x22\x9b\xf0' 138 b'\xe2\x04\x65\x6b' 139 ) 140 r = ICMP(s) 141 assert bytes(r) == s 142 143 # construction 144 s = ( 145 b'\x00\x00\x53\x87\x00\x01\x03\xd6\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e' 146 b'\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x41\x42\x43\x44\x45\x46\x47\x48\x49' 147 ) 148 p = ICMP( 149 type=0, 150 sum=0x5387, 151 data=ICMP.Echo( 152 id=1, 153 seq=0x03d6, 154 data=b'ABCDEFGHIJKLMNOPQRSTUVWABCDEFGHI' 155 ) 156 ) 157 assert bytes(p) == s 158 159 # test checksum 160 p = ICMP( 161 type=0, 162 data=ICMP.Echo( 163 id=1, 164 seq=0x03d6, 165 data=b'ABCDEFGHIJKLMNOPQRSTUVWABCDEFGHI' 166 ) 167 ) 168 assert bytes(p) == s 169 assert p.sum == 0x5387
170 171 172 if __name__ == '__main__': 173 test_icmp() 174 print('Tests Successful...') 175