Sha256: 07d7f6e1b5031e83abb04d1a2acb0b7c8b1c00b5c6337852749b5eb93968f3c4

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

# This file is part of PacketGen
# See https://github.com/sdaubert/packetgen for more informations
# Copyright (C) 2016 Sylvain Daubert <sylvain.daubert@laposte.net>
# This program is published under MIT license.

# frozen_string_literal: true

module PacketGen
  module Header
    # A ICMP header consists of:
    # * a {#type} field ({Types::Int8} type),
    # * a {#code} field ({Types::Int8} type),
    # * a {#checksum} field ({Types::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'
    # @author Sylvain Daubert
    class ICMP < Base
      # ICMP internet protocol number
      IP_PROTOCOL = 1

      # @!attribute type
      #  8-bit ICMP type
      #  @return [Integer]
      define_field :type, Types::Int8
      # @!attribute code
      #  8-bit ICMP code
      #  @return [Integer]
      define_field :code, Types::Int8
      # @!attribute checksum
      #  16-bit ICMP checksum
      #  @return [Integer]
      define_field :checksum, Types::Int16
      # @!attribute body
      #  @return [Types::String,Header::Base]
      define_field :body, Types::String

      # Compute checksum and set +checksum+ field
      # @return [Integer]
      def calc_checksum
        sum = IP.sum16(self)
        self.checksum = IP.reduce_checksum(sum)
      end
    end

    self.add_class ICMP

    IP.bind ICMP, protocol: ICMP::IP_PROTOCOL
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
packetgen-2.8.7 lib/packetgen/header/icmp.rb
packetgen-2.8.6 lib/packetgen/header/icmp.rb
packetgen-2.8.5 lib/packetgen/header/icmp.rb
packetgen-2.8.4 lib/packetgen/header/icmp.rb
packetgen-2.8.3 lib/packetgen/header/icmp.rb
packetgen-2.8.1 lib/packetgen/header/icmp.rb
packetgen-2.8.0 lib/packetgen/header/icmp.rb
packetgen-2.7.0 lib/packetgen/header/icmp.rb