Class: PacketGen::Header::ICMP
- Extended by:
- HeaderClassMethods
- Includes:
- HeaderMethods, StructFu
- Defined in:
- lib/packetgen/header/icmp.rb
Overview
A ICMP header consists of:
-
a #type field (StructFu::Int8 type),
-
a #code field (StructFu::Int8 type),
-
a #checksum field (StructFu::Int16 type),
-
and a #body.
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'
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.
-
#checksum ⇒ Integer
Getter for checksum attribute.
-
#code ⇒ Integer
Getter for code attribute.
-
#type ⇒ Integer
Getter for type attribute.
Instance Method Summary collapse
-
#calc_checksum ⇒ Integer
Compute checksum and set
checksumfield. -
#initialize(options = {}) ⇒ ICMP
constructor
A new instance of ICMP.
-
#read(str) ⇒ self
Read a ICMP header from a string.
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
Constructor Details
#initialize(options = {}) ⇒ ICMP
Returns a new instance of ICMP.
42 43 44 45 46 47 |
# File 'lib/packetgen/header/icmp.rb', line 42 def initialize(={}) super Int8.new([:type]), Int8.new([:code]), Int16.new([:checksum]), StructFu::String.new.read([:body]) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body
29 30 31 |
# File 'lib/packetgen/header/icmp.rb', line 29 def body @body end |
#checksum ⇒ Integer
Getter for checksum attribute
29 30 31 |
# File 'lib/packetgen/header/icmp.rb', line 29 def checksum @checksum end |
#code ⇒ Integer
Getter for code attribute
29 30 31 |
# File 'lib/packetgen/header/icmp.rb', line 29 def code @code end |
#type ⇒ Integer
Getter for type attribute
29 30 31 |
# File 'lib/packetgen/header/icmp.rb', line 29 def type @type end |
Instance Method Details
#calc_checksum ⇒ Integer
Compute checksum and set checksum field
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
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 |