Sha256: d1a6c205d1df03abac1a39961968f93b66b0cae7542ef62f26f7628adf25c587

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

# coding: utf-8
# frozen_string_literal: true

# 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.

module PacketGen
  # Unknown packet, minimaly mimicking a {Packet}.
  #
  # An unknown packet is generated when capturing packets, and a packet cannot
  # be parsed.
  # @since 3.2.0
  class UnknownPacket
    # List of headers. Always empty
    # @return [Array]
    attr_reader :headers

    def initialize
      @headers = [].freeze
      @binary_str = PacketGen.force_binary('')
    end

    # Unknown packet, so unknown protocol.
    # @return [false]
    def is?(_protocol)
      false
    end

    # Get packet body
    # @return [Types]
    def body
      @binary_str
    end
    alias to_s body

    # Set packet body
    # @param [String] str
    # @return [void]
    def body=(str)
      @binary_str = PacketGen.force_binary(str)
    end

    # Write packet to a PCapNG file on disk.
    # @param [String] filename
    # @return [Array] see return from {PcapNG::File#to_file}
    # @see File
    def to_f(filename)
      PcapNG::File.new.read_array([self]).to_f(filename)
    end
    alias write to_f

    # Read binary string
    # @param [String] binary_string
    # @return [self]
    def parse(binary_str, _first_header: nil)
      @binary_str = PacketGen.force_binary(binary_str)
      self
    end

    # @return [String]
    def inspect
      str = Inspect.dashed_line(self.class)
      str << Inspect.inspect_body(body)
    end

    # equality if {#to_s} is equal
    # @return [Boolean]
    def ==(other)
      to_s == other.to_s
    end

    # True only if +other+ is an {UnknownPacket} and +other == self+
    # @return [Boolean]
    def ===(other)
      case other
      when UnknownPacket
        self == other
      else
        false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
packetgen-3.3.1 lib/packetgen/unknown_packet.rb
packetgen-3.3.0 lib/packetgen/unknown_packet.rb
packetgen-3.2.2 lib/packetgen/unknown_packet.rb