Class: PacketGen::Header::ICMP

Inherits:
Struct
  • Object
show all
Extended by:
HeaderClassMethods
Includes:
HeaderMethods, StructFu
Defined in:
lib/packetgen/header/icmp.rb

Overview

A ICMP header consists of:

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     # => PacketGen::Header::ICMP

ICMP attributes

icmp.code = 0
icmp.type = 200
icmp.checksum = 0x248a
icmp.body.read '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 included from HeaderClassMethods

bind_header, define_bit_fields_on, known_headers

Methods included from HeaderMethods

#header_id, #inspect, #ip_header, #packet, #packet=, #protocol_name

Methods included from StructFu

#clone, #set_endianness, #sz, #to_s, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ ICMP

Returns a new instance of ICMP.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :type (Integer)
  • :code (Integer)
  • :checksum (Integer)
  • :body (String)


42
43
44
45
46
47
# File 'lib/packetgen/header/icmp.rb', line 42

def initialize(options={})
  super Int8.new(options[:type]),
        Int8.new(options[:code]),
        Int16.new(options[:checksum]),
        StructFu::String.new.read(options[:body])
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



29
30
31
# File 'lib/packetgen/header/icmp.rb', line 29

def body
  @body
end

#checksumInteger

Getter for checksum attribute

Returns:

  • (Integer)


29
30
31
# File 'lib/packetgen/header/icmp.rb', line 29

def checksum
  @checksum
end

#codeInteger

Getter for code attribute

Returns:

  • (Integer)


29
30
31
# File 'lib/packetgen/header/icmp.rb', line 29

def code
  @code
end

#typeInteger

Getter for type attribute

Returns:

  • (Integer)


29
30
31
# File 'lib/packetgen/header/icmp.rb', line 29

def type
  @type
end

Instance Method Details

#calc_checksumInteger

Compute checksum and set checksum field

Returns:

  • (Integer)


64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/packetgen/header/icmp.rb', line 64

def calc_checksum
  sum = (type << 8) | code

  payload = body.to_s
  payload << "\x00" unless payload.size % 2 == 0
  payload.unpack('n*').each { |x| sum += x }

  while sum > 0xffff do
    sum = (sum & 0xffff) + (sum >> 16)
  end
  sum = ~sum & 0xffff
  self[:checksum].value = (sum == 0) ? 0xffff : sum
end

#read(str) ⇒ self

Read a ICMP header from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



52
53
54
55
56
57
58
59
60
# File 'lib/packetgen/header/icmp.rb', line 52

def read(str)
  return self if str.nil?
  raise ParseError, 'string too short for ICMP' if str.size < self.sz
  force_binary str
  self[:type].read str[0, 1]
  self[:code].read str[1, 1]
  self[:checksum].read str[2, 2]
  self[:body].read str[4..-1]
end