### Home Classes Methods

### In Files

- lib/packetfu/protos/icmp/header.rb

### Parent

Struct.new(:icmp_type, :icmp_code, :icmp_sum, :body)

### Methods

- ::new
- #icmp_calc_sum
- #icmp_code
- #icmp_code=
- #icmp_recalc
- #icmp_sum
- #icmp_sum=
- #icmp_sum_readable
- #icmp_type
- #icmp_type=
- #read
- #to_s

### Included Modules

- StructFu

### Files

- .document
- README.rdoc

### Class/Module Index

Quicksearch

- PacketFu
- PacketFu::ARPHeader
- PacketFu::ARPHeaderMixin
- PacketFu::ARPPacket
- PacketFu::AddrIpv6
- PacketFu::Capture
- PacketFu::Config
- PacketFu::EthHeader
- PacketFu::EthHeaderMixin
- PacketFu::EthMac
- PacketFu::EthNic
- PacketFu::EthOui
- PacketFu::EthPacket
- PacketFu::HSRPHeader
- PacketFu::HSRPHeaderMixin
- PacketFu::HSRPPacket
- PacketFu::ICMPHeader
- PacketFu::ICMPHeaderMixin
- PacketFu::ICMPPacket
- PacketFu::IPHeader
- PacketFu::IPHeaderMixin
- PacketFu::IPPacket
- PacketFu::IPv6Header
- PacketFu::IPv6HeaderMixin
- PacketFu::IPv6Packet
- PacketFu::Inject
- PacketFu::InvalidHeader
- PacketFu::InvalidPacket
- PacketFu::LLDPHeader
- PacketFu::LLDPHeaderMixin
- PacketFu::LLDPPacket
- PacketFu::Octets
- PacketFu::Packet
- PacketFu::PcapFile
- PacketFu::PcapHeader
- PacketFu::PcapPacket
- PacketFu::PcapPackets
- PacketFu::Read
- PacketFu::TCPHeader
- PacketFu::TCPHeaderMixin
- PacketFu::TCPPacket
- PacketFu::TcpEcn
- PacketFu::TcpFlags
- PacketFu::TcpHlen
- PacketFu::TcpOption
- PacketFu::TcpOption::ECHO
- PacketFu::TcpOption::ECHOREPLY
- PacketFu::TcpOption::EOL
- PacketFu::TcpOption::MSS
- PacketFu::TcpOption::NOP
- PacketFu::TcpOption::SACK
- PacketFu::TcpOption::SACKOK
- PacketFu::TcpOption::TS
- PacketFu::TcpOption::WS
- PacketFu::TcpOptions
- PacketFu::TcpReserved
- PacketFu::Timestamp
- PacketFu::UDPHeader
- PacketFu::UDPHeaderMixin
- PacketFu::UDPPacket
- PacketFu::Utils
- PacketFu::Write
- StructFu
- StructFu::Int
- StructFu::Int16
- StructFu::Int16be
- StructFu::Int16le
- StructFu::Int32
- StructFu::Int32be
- StructFu::Int32le
- StructFu::Int8
- StructFu::IntString
- StructFu::String
- Struct

No matching classes.

# PacketFu::ICMPHeader

ICMPHeader is a complete ICMP struct, used in ICMPPacket. ICMP is typically used for network administration and connectivity testing.

For more on ICMP packets, see www.networksorcery.com/enp/protocol/icmp.htm

#### Header Definition

    Int8    :icmp_type                        # Type
    Int8    :icmp_code                        # Code
    Int16   :icmp_sum    Default: calculated  # Checksum
    String  :body

### Public Class Methods

new(args={}) click to toggle source

    # File lib/packetfu/protos/icmp/header.rb, line 18
    def initialize(args={})
            super(
                    Int8.new(args[:icmp_type]),
                    Int8.new(args[:icmp_code]),
                    Int16.new(args[:icmp_sum] || icmp_calc_sum),
                    StructFu::String.new.read(args[:body])
            )
    end

### Public Instance Methods

icmp_calc_sum() click to toggle source

Calculates and sets the checksum for the object.

    # File lib/packetfu/protos/icmp/header.rb, line 58
    def icmp_calc_sum
            checksum = (icmp_type.to_i << 8)     + icmp_code.to_i
            chk_body = (body.to_s.size % 2 == 0 ? body.to_s : body.to_s + "\x00")
            if 1.respond_to? :ord
                    chk_body.scan(/../).map { |x| (x[0].ord << 8) + x[1].ord }.each { |y| checksum += y }
            else
                    chk_body.scan(/../).map { |x| (x[0] << 8) + x[1] }.each { |y| checksum += y }
            end
            checksum = checksum % 0xffff
            checksum = 0xffff - checksum
            checksum == 0 ? 0xffff : checksum
    end

icmp_code() click to toggle source

Getter for the code.

    # File lib/packetfu/protos/icmp/header.rb, line 50
    def icmp_code; self[:icmp_code].to_i; end

icmp_code=(i) click to toggle source

Setter for the code.

    # File lib/packetfu/protos/icmp/header.rb, line 48
    def icmp_code=(i); typecast i; end

icmp_recalc(arg=:all) click to toggle source

Recalculates the calculatable fields for ICMP.

    # File lib/packetfu/protos/icmp/header.rb, line 72
    def icmp_recalc(arg=:all)
            # How silly is this, you can't intern a symbol in ruby 1.8.7pl72?
            # I'm this close to monkey patching Symbol so you can force it...
            arg = arg.intern if arg.respond_to? :intern
            case arg
            when :icmp_sum
                    self.icmp_sum=icmp_calc_sum
            when :all
                    self.icmp_sum=icmp_calc_sum
            else
                    raise ArgumentError, "No such field `#{arg}'"
            end
    end

icmp_sum() click to toggle source

Getter for the checksum.

    # File lib/packetfu/protos/icmp/header.rb, line 55
    def icmp_sum; self[:icmp_sum].to_i; end

icmp_sum=(i) click to toggle source

Setter for the checksum. Note, this is calculated automatically with icmp_calc_sum.

    # File lib/packetfu/protos/icmp/header.rb, line 53
    def icmp_sum=(i); typecast i; end

icmp_sum_readable() click to toggle source

Readability aliases

    # File lib/packetfu/protos/icmp/header.rb, line 88
    def icmp_sum_readable
            "0x%04x" % icmp_sum
    end

icmp_type() click to toggle source

Getter for the type.

    # File lib/packetfu/protos/icmp/header.rb, line 46
    def icmp_type; self[:icmp_type].to_i; end

icmp_type=(i) click to toggle source

Setter for the type.

    # File lib/packetfu/protos/icmp/header.rb, line 44
    def icmp_type=(i); typecast i; end

read(str) click to toggle source

Reads a string to populate the object.

    # File lib/packetfu/protos/icmp/header.rb, line 33
    def read(str)
            force_binary(str)
            return self if str.nil?
            self[:icmp_type].read(str[0,1])
            self[:icmp_code].read(str[1,1])
            self[:icmp_sum].read(str[2,2])
            self[:body].read(str[4,str.size])
            self
    end

to_s() click to toggle source

Returns the object in string form.

    # File lib/packetfu/protos/icmp/header.rb, line 28
    def to_s
            self.to_a.map {|x| x.to_s}.join
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.