Class: Icmphdr
- Inherits:
-
Object
- Object
- Icmphdr
- Defined in:
- lib/emtraceroute/icmp.rb
Instance Attribute Summary collapse
-
#cksum ⇒ Object
This represents an ICMP packet header.
-
#code ⇒ Object
This represents an ICMP packet header.
-
#data ⇒ Object
This represents an ICMP packet header.
-
#id ⇒ Object
This represents an ICMP packet header.
-
#sequence ⇒ Object
This represents an ICMP packet header.
-
#type ⇒ Object
This represents an ICMP packet header.
Class Method Summary collapse
Instance Method Summary collapse
- #assemble ⇒ Object
-
#initialize(data = "") ⇒ Icmphdr
constructor
A new instance of Icmphdr.
Constructor Details
#initialize(data = "") ⇒ Icmphdr
9 10 11 12 13 14 15 16 |
# File 'lib/emtraceroute/icmp.rb', line 9 def initialize data="" @type = 8 @code = 0 @cksum = 0 @id = $$ @sequence = 0 @data = data end |
Instance Attribute Details
#cksum ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def cksum @cksum end |
#code ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def code @code end |
#data ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def data @data end |
#id ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def id @id end |
#sequence ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def sequence @sequence end |
#type ⇒ Object
This represents an ICMP packet header.
Icmphdr#assemble packages the packet Imphdr.checsum calc checksum Icmphdr.disassemble disassembles the packet
8 9 10 |
# File 'lib/emtraceroute/icmp.rb', line 8 def type @type end |
Class Method Details
.checksum(data) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/emtraceroute/icmp.rb', line 26 def self.checksum data data += '\0' if data.size & 1 == 1 cksum = data.unpack("n*")[0..(data.size >> 1)].inject(&:+) cksum = (cksum >> 16) + (cksum & 0xffff) cksum += (cksum >> 16) cksum = (cksum & 0xffff) ^ 0xffff cksum end |
.disassemble(data) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/emtraceroute/icmp.rb', line 35 def self.disassemble data icmp = Icmphdr.new pkt = data.unpack('CCnnn') icmp.type, icmp.code, icmp.cksum, icmp.id, icmp.sequence = pkt icmp end |
Instance Method Details
#assemble ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/emtraceroute/icmp.rb', line 18 def assemble part1 = [@type, @code].pack('C*') part2 = [@id, @sequence].pack('n*') cksum = Icmphdr.checksum(part1 + "\000\000" + part2 + @data) cksum = [cksum].pack('n') part1 + cksum + part2 + @data end |