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

Source Code for Module dpkt.icmp

  1  # $Id: icmp.py 271 2006-01-11 16:03:33Z dugsong $ 
  2   
  3  """Internet Control Message Protocol.""" 
  4   
  5  import dpkt, ip 
  6   
  7  # Types (icmp_type) and codes (icmp_code) - 
  8  # http://www.iana.org/assignments/icmp-parameters 
  9   
 10  ICMP_CODE_NONE                  = 0     # for types without codes 
 11  ICMP_ECHOREPLY          = 0     # echo reply 
 12  ICMP_UNREACH            = 3     # dest unreachable, codes: 
 13  ICMP_UNREACH_NET                = 0     # bad net 
 14  ICMP_UNREACH_HOST               = 1     # bad host 
 15  ICMP_UNREACH_PROTO              = 2     # bad protocol 
 16  ICMP_UNREACH_PORT               = 3     # bad port 
 17  ICMP_UNREACH_NEEDFRAG           = 4     # IP_DF caused drop 
 18  ICMP_UNREACH_SRCFAIL            = 5     # src route failed 
 19  ICMP_UNREACH_NET_UNKNOWN        = 6     # unknown net 
 20  ICMP_UNREACH_HOST_UNKNOWN       = 7     # unknown host 
 21  ICMP_UNREACH_ISOLATED           = 8     # src host isolated 
 22  ICMP_UNREACH_NET_PROHIB         = 9     # for crypto devs 
 23  ICMP_UNREACH_HOST_PROHIB        = 10    # ditto 
 24  ICMP_UNREACH_TOSNET             = 11    # bad tos for net 
 25  ICMP_UNREACH_TOSHOST            = 12    # bad tos for host 
 26  ICMP_UNREACH_FILTER_PROHIB      = 13    # prohibited access 
 27  ICMP_UNREACH_HOST_PRECEDENCE    = 14    # precedence error 
 28  ICMP_UNREACH_PRECEDENCE_CUTOFF  = 15    # precedence cutoff 
 29  ICMP_SRCQUENCH          = 4     # packet lost, slow down 
 30  ICMP_REDIRECT           = 5     # shorter route, codes: 
 31  ICMP_REDIRECT_NET               = 0     # for network 
 32  ICMP_REDIRECT_HOST              = 1     # for host 
 33  ICMP_REDIRECT_TOSNET            = 2     # for tos and net 
 34  ICMP_REDIRECT_TOSHOST           = 3     # for tos and host 
 35  ICMP_ALTHOSTADDR        = 6     # alternate host address 
 36  ICMP_ECHO               = 8     # echo service 
 37  ICMP_RTRADVERT          = 9     # router advertise, codes: 
 38  ICMP_RTRADVERT_NORMAL           = 0     # normal 
 39  ICMP_RTRADVERT_NOROUTE_COMMON   = 16    # selective routing 
 40  ICMP_RTRSOLICIT         = 10    # router solicitation 
 41  ICMP_TIMEXCEED          = 11    # time exceeded, code: 
 42  ICMP_TIMEXCEED_INTRANS          = 0     # ttl==0 in transit 
 43  ICMP_TIMEXCEED_REASS            = 1     # ttl==0 in reass 
 44  ICMP_PARAMPROB          = 12    # ip header bad 
 45  ICMP_PARAMPROB_ERRATPTR         = 0     # req. opt. absent 
 46  ICMP_PARAMPROB_OPTABSENT        = 1     # req. opt. absent 
 47  ICMP_PARAMPROB_LENGTH           = 2     # bad length 
 48  ICMP_TSTAMP             = 13    # timestamp request 
 49  ICMP_TSTAMPREPLY        = 14    # timestamp reply 
 50  ICMP_INFO               = 15    # information request 
 51  ICMP_INFOREPLY          = 16    # information reply 
 52  ICMP_MASK               = 17    # address mask request 
 53  ICMP_MASKREPLY          = 18    # address mask reply 
 54  ICMP_TRACEROUTE         = 30    # traceroute 
 55  ICMP_DATACONVERR        = 31    # data conversion error 
 56  ICMP_MOBILE_REDIRECT    = 32    # mobile host redirect 
 57  ICMP_IP6_WHEREAREYOU    = 33    # IPv6 where-are-you 
 58  ICMP_IP6_IAMHERE        = 34    # IPv6 i-am-here 
 59  ICMP_MOBILE_REG         = 35    # mobile registration req 
 60  ICMP_MOBILE_REGREPLY    = 36    # mobile registration reply 
 61  ICMP_DNS                = 37    # domain name request 
 62  ICMP_DNSREPLY           = 38    # domain name reply 
 63  ICMP_SKIP               = 39    # SKIP 
 64  ICMP_PHOTURIS           = 40    # Photuris 
 65  ICMP_PHOTURIS_UNKNOWN_INDEX     = 0     # unknown sec index 
 66  ICMP_PHOTURIS_AUTH_FAILED       = 1     # auth failed 
 67  ICMP_PHOTURIS_DECOMPRESS_FAILED = 2     # decompress failed 
 68  ICMP_PHOTURIS_DECRYPT_FAILED    = 3     # decrypt failed 
 69  ICMP_PHOTURIS_NEED_AUTHN        = 4     # no authentication 
 70  ICMP_PHOTURIS_NEED_AUTHZ        = 5     # no authorization 
 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):
82 __hdr__ = (('pad', 'I', 0),)
83 - def unpack(self, buf):
84 dpkt.Packet.unpack(self, buf) 85 self.data = self.ip = ip.IP(self.data)
86 - class Unreach(Quote):
87 __hdr__ = (('pad', 'H', 0), ('mtu', 'H', 0))
88 - class Quench(Quote):
89 pass
90 - class Redirect(Quote):
91 __hdr__ = (('gw', 'I', 0),)
92 - class ParamProbe(Quote):
93 __hdr__ = (('ptr', 'B', 0), ('pad1', 'B', 0), ('pad2', 'H', 0))
94 - class TimeExceed(Quote):
95 pass
96 97 _typesw = { 0:Echo, 3:Unreach, 4:Quench, 5:Redirect, 8:Echo, 98 11:TimeExceed } 99
100 - def unpack(self, buf):
101 dpkt.Packet.unpack(self, buf) 102 try: 103 self.data = self._typesw[self.type](self.data) 104 setattr(self, self.data.__class__.__name__.lower(), self.data) 105 except (KeyError, dpkt.UnpackError): 106 self.data = buf
107
108 - def __str__(self):
109 if not self.sum: 110 self.sum = dpkt.in_cksum(dpkt.Packet.__str__(self)) 111 return dpkt.Packet.__str__(self)
112