Sha256: cb6eea83eaa4ae0b9ba2408f6150c3d97190009e0d7800375e34db185c77738a

Contents?: true

Size: 1.34 KB

Versions: 17

Compression:

Stored size: 1.34 KB

Contents

require 'bindata'
require 'pio/ethernet_header'
require 'pio/ipv4_header'

module Pio
  class Icmp
    # Icmp parser.
    class Format < BinData::Record
      MINIMUM_IP_PACKET_LENGTH = 50

      include EthernetHeader
      include IPv4Header

      endian :big

      ethernet_header ether_type: EtherType::IPV4
      ipv4_header ip_protocol: ProtocolNumber::ICMP
      uint8 :icmp_type
      uint8 :icmp_code, initial_value: 0
      uint16 :icmp_checksum, value: :calculate_icmp_checksum
      uint16 :icmp_identifier
      uint16 :icmp_sequence_number
      string :echo_data, read_length: :echo_data_read_length
      string :padding, read_length: 0, initial_value: :icmp_padding_length

      def message_type
        icmp_type
      end

      alias_method :to_binary, :to_binary_s

      private

      def calculate_icmp_checksum
        sum = [icmp_type * 0x100 + icmp_code,
               icmp_identifier,
               icmp_sequence_number,
               *echo_data.unpack('n*')].inject(:+)
        ~((sum & 0xffff) + (sum >> 16)) & 0xffff
      end

      def echo_data_read_length
        ip_total_length - (ip_header_length * 4) - 8
      end

      def icmp_padding_length
        length = MINIMUM_IP_PACKET_LENGTH -
                 (ip_header_length * 4) - 8 - echo_data.length
        length > 0 ? "\x00" * length : ''
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
pio-0.26.0 lib/pio/icmp/format.rb
pio-0.25.0 lib/pio/icmp/format.rb
pio-0.24.2 lib/pio/icmp/format.rb
pio-0.24.1 lib/pio/icmp/format.rb
pio-0.24.0 lib/pio/icmp/format.rb
pio-0.23.1 lib/pio/icmp/format.rb
pio-0.23.0 lib/pio/icmp/format.rb
pio-0.22.0 lib/pio/icmp/format.rb
pio-0.21.1 lib/pio/icmp/format.rb
pio-0.21.0 lib/pio/icmp/format.rb
pio-0.20.1 lib/pio/icmp/format.rb
pio-0.20.0 lib/pio/icmp/format.rb
pio-0.19.0 lib/pio/icmp/format.rb
pio-0.18.2 lib/pio/icmp/format.rb
pio-0.18.1 lib/pio/icmp/format.rb
pio-0.18.0 lib/pio/icmp/format.rb
pio-0.17.0 lib/pio/icmp/format.rb