Class: PacketGen::Header::ICMP

Inherits:
Base
  • Object
show all
Defined in:
lib/packetgen/header/icmp.rb

Overview

ICMP header (RFC 792)

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|     Type      |     Code      |          Checksum             |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

A ICMP header consists of:

  • a #type field (BinStruct::Int8 type),

  • a #code field (BinStruct::Int8 type),

  • a #checksum field (BinStruct::Int16 type),

  • and a #body.

Examples:

Create a ICMP header

# standalone
icmp = PacketGen::Header::ICMP.new
# in a packet
pkt = PacketGen.gen('IP').add('ICMP')
# access to ICMP header
pkt.icmp.class   # => PacketGen::Header::ICMP

ICMP attributes

icmp = PacketGen::Header::ICMP.new
icmp.code = 0
icmp.type = 200
icmp.checksum = 0x248a
icmp.body = 'this is a body'

Author:

  • Sylvain Daubert

Direct Known Subclasses

ICMPv6

Constant Summary collapse

IP_PROTOCOL =

ICMP internet protocol number

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #initialize, #ip_header, #ll_header

Methods included from PacketGen::Headerable

#added_to_packet, included, #method_name, #packet, #packet=, #parse?, #protocol_name, #read, #to_s

Constructor Details

This class inherits a constructor from PacketGen::Header::Base

Instance Attribute Details

#bodyObject

ICMP body

@return [BinStruct::String,Headerable]

57
# File 'lib/packetgen/header/icmp.rb', line 57

define_attr :body, BinStruct::String

#checksumInteger

16-bit ICMP checksum

Returns:

  • (Integer)

53
# File 'lib/packetgen/header/icmp.rb', line 53

define_attr :checksum, BinStruct::Int16

#codeInteger

8-bit ICMP code

Returns:

  • (Integer)

49
# File 'lib/packetgen/header/icmp.rb', line 49

define_attr :code, BinStruct::Int8

#typeInteger

8-bit ICMP type

Returns:

  • (Integer)

45
# File 'lib/packetgen/header/icmp.rb', line 45

define_attr :type, BinStruct::Int8

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)

61
62
63
64
# File 'lib/packetgen/header/icmp.rb', line 61

def calc_checksum
  sum = IP.sum16(self)
  self.checksum = IP.reduce_checksum(sum)
end