Class: PacketGen::Header::ICMP

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

Overview

ICMP header class

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, known_headers

Methods included from HeaderMethods

#header_id, #ip_header, #packet, #packet=

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)
  • :sum (Integer)
  • :body (String)


19
20
21
22
23
24
# File 'lib/packetgen/header/icmp.rb', line 19

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

Instance Attribute Details

#bodyObject

Returns the value of attribute body

Returns:

  • (Object)

    the current value of body



6
7
8
# File 'lib/packetgen/header/icmp.rb', line 6

def body
  @body
end

#codeInteger

Getter for code attribute

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/icmp.rb', line 6

def code
  @code
end

#sumInteger

Getter for sum attribute

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/icmp.rb', line 6

def sum
  @sum
end

#typeInteger

Getter for type attribute

Returns:

  • (Integer)


6
7
8
# File 'lib/packetgen/header/icmp.rb', line 6

def type
  @type
end

Instance Method Details

#calc_sumInteger

Compute checksum and set sum field

Returns:

  • (Integer)


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/packetgen/header/icmp.rb', line 41

def calc_sum
  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[:sum].value = (sum == 0) ? 0xffff : sum
end

#read(str) ⇒ self

Read a ICMP header from a string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/packetgen/header/icmp.rb', line 29

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