Class: PacketGen::Header::ICMP
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/icmp.rb
Overview
ICMP header class
Direct Known Subclasses
Constant Summary collapse
- IP_PROTOCOL =
ICMP internet protocol number
1
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#code ⇒ Integer
Getter for code attribute.
-
#sum ⇒ Integer
Getter for sum attribute.
-
#type ⇒ Integer
Getter for type attribute.
Instance Method Summary collapse
-
#calc_sum ⇒ Integer
Compute checksum and set
sumfield. -
#initialize(options = {}) ⇒ ICMP
constructor
A new instance of ICMP.
-
#read(str) ⇒ self
Read a ICMP header from a string.
Methods included from HeaderClassMethods
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
Constructor Details
#initialize(options = {}) ⇒ ICMP
Returns a new instance of ICMP.
19 20 21 22 23 24 |
# File 'lib/packetgen/header/icmp.rb', line 19 def initialize(={}) super Int8.new([:type]), Int8.new([:code]), Int16.new([:sum]), StructFu::String.new.read([:body]) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
6 7 8 |
# File 'lib/packetgen/header/icmp.rb', line 6 def body @body end |
#code ⇒ Integer
Getter for code attribute
6 7 8 |
# File 'lib/packetgen/header/icmp.rb', line 6 def code @code end |
#sum ⇒ Integer
Getter for sum attribute
6 7 8 |
# File 'lib/packetgen/header/icmp.rb', line 6 def sum @sum end |
#type ⇒ Integer
Getter for type attribute
6 7 8 |
# File 'lib/packetgen/header/icmp.rb', line 6 def type @type end |
Instance Method Details
#calc_sum ⇒ Integer
Compute checksum and set sum field
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
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 |