<commit_before>module FFI
  module PCap

    # An abstract base wrapper class with features common to all pcap
    # wrapper types. Do not use this directly. Instead refer to Live, 
    # Dead, or Offline class for open_live, open_dead, or open_file 
    # respectively.
    class CommonWrapper

      attr_accessor :pcap

      def initialize(pcap, opts={})
        @pcap = pcap
        @closed = false
        @errbuf = ErrorBuffer.create

        trap('INT') {|s| stop(); close(); raise(SignalException, s)}
        trap('TERM') {|s| stop(); close(); raise(SignalException, s)}

        if block_given?
          yield(self)
          self.close()
        end
      end


      # Returns the DataLink for the pcap device.
      def datalink
        @datalink ||= DataLink.new(PCap.pcap_datalink(_pcap))
      end


      # Returns an array of supported DataLinks for the pcap device.
      def supported_datalinks
        dlt_lst = FFI::MemoryPointer.new(:pointer)
        if (cnt=PCap.pcap_list_datalinks(_pcap, dlt_lst)) < 0
          raise(LibError, "pcap_list_datalinks(): #{geterr()}")
        end
        # extract datalink values 
        p = dlt_lst.get_pointer(0)
        ret = p.get_array_of_int(0, cnt).map {|dlt| DataLink.new(dlt) }
        PCap.free(p)
        return ret
      end

      # Indicates whether the pcap interface is already closed.
      def closed?
        @closed == true
      end

      # Closes the pcap interface using libpcap.
      def close
        unless @closed
          @closed = true
          PCap.pcap_close(_pcap)
        end
      end

      # Returns the pcap interface pointer.
      #
      # @return [FFI::Pointer]
      #   Internal pointer to a pcap_t handle.
      #
      def to_ptr
        _check_pcap()
      end
      
      # Gets the snapshot length.
      #
      # @return [Integer]
      #  Snapshot length for the pcap interface.
      def snaplen
        PCap.pcap_snapshot(_pcap)
      end

      # Used to specify a pcap filter for the pcap interface. This method 
      # compiles a filter expression and applies it on the wrapped pcap 
      # interface.
      #
      # @param [String] expression
      #   A pcap filter expression. See pcap-filter(7) manpage for syntax.
      #
      # @options [Hash] opts
      #   Compile options. See compile()
      #
      # @raise [LibError]
      #   On failure, an exception is raised with the relevant error message 
      #   from libpcap.
      #
      def set_filter(expression, opts={})
        code = compile(expression, opts)
        ret = PCap.pcap_setfilter(_pcap, code)
        code.free!  # done with this, we can free it
        raise(LibError, "pcap_setfilter(): #{geterr()}") if ret < 0
        return expression
      end

      alias setfilter set_filter
      alias filter= set_filter


      # Compiles a pcap filter but does not apply it to the pcap interface.
      #
      # @param [String] expression
      #   A pcap filter expression. See pcap-filter(7) manpage for syntax.
      #
      # @options [Hash] opts
      #   Additional options for compile
      #
      # @option opts [optional, Integer] :optimize
      #   Optimization flag. 0 means don't optimize. Defaults to 1.
      #
      # @option opts [optional, Integer] :netmask
      #   A 32-bit number representing the IPv4 netmask of the network on which
      #   packets are being captured. It is only used when checking for IPv4
      #   broadcast addresses in the filter program. Default: 0 (unspecified <commit_msg>fix doc<commit_after>module FFI
  module PCap

    # An abstract base wrapper class with features common to all pcap
    # wrapper types. Do not use this directly. Instead refer to Live, 
    # Dead, or Offline class for open_live, open_dead, or open_file 
    # respectively.
    class CommonWrapper

      attr_accessor :pcap

      def initialize(pcap, opts={})
        @pcap = pcap
        @closed = false
        @errbuf = ErrorBuffer.create

        trap('INT') {|s| stop(); close(); raise(SignalException, s)}
        trap('TERM') {|s| stop(); close(); raise(SignalException, s)}

        if block_given?
          yield(self)
          self.close()
        end
      end


      # Returns the DataLink for the pcap device.
      def datalink
        @datalink ||= DataLink.new(PCap.pcap_datalink(_pcap))
      end


      # Returns an array of supported DataLinks for the pcap device.
      def supported_datalinks
        dlt_lst = FFI::MemoryPointer.new(:pointer)
        if (cnt=PCap.pcap_list_datalinks(_pcap, dlt_lst)) < 0
          raise(LibError, "pcap_list_datalinks(): #{geterr()}")
        end
        # extract datalink values 
        p = dlt_lst.get_pointer(0)
        ret = p.get_array_of_int(0, cnt).map {|dlt| DataLink.new(dlt) }
        PCap.free(p)
        return ret
      end

      # Indicates whether the pcap interface is already closed.
      def closed?
        @closed == true
      end

      # Closes the pcap interface using libpcap.
      def close
        unless @closed
          @closed = true
          PCap.pcap_close(_pcap)
        end
      end

      # Returns the pcap interface pointer.
      #
      # @return [FFI::Pointer]
      #   Internal pointer to a pcap_t handle.
      #
      def to_ptr
        _check_pcap()
      end
      
      # Gets the snapshot length.
      #
      # @return [Integer]
      #  Snapshot length for the pcap interface.
      def snaplen
        PCap.pcap_snapshot(_pcap)
      end

      # Used to specify a pcap filter for the pcap interface. This method 
      # compiles a filter expression and applies it on the wrapped pcap 
      # interface.
      #
      # @param [String] expression
      #   A pcap filter expression. See pcap-filter(7) manpage for syntax.
      #
      # @param [Hash] opts
      #   Compile options. See compile()
      #
      # @raise [LibError]
      #   On failure, an exception is raised with the relevant error message 
      #   from libpcap.
      #
      def set_filter(expression, opts={})
        code = compile(expression, opts)
        ret = PCap.pcap_setfilter(_pcap, code)
        code.free!  # done with this, we can free it
        raise(LibError, "pcap_setfilter(): #{geterr()}") if ret < 0
        return expression
      end

      alias setfilter set_filter
      alias filter= set_filter


      # Compiles a pcap filter but does not apply it to the pcap interface.
      #
      # @param [String] expression
      #   A pcap filter expression. See pcap-filter(7) manpage for syntax.
      #
      # @param [Hash] opts
      #   Additional options for compile
      #
      # @option opts [optional, Integer] :optimize
      #   Optimization flag. 0 means don't optimize. Defaults to 1.
      #
      # @option opts [optional, Integer] :netmask
      #   A 32-bit number representing the IPv4 netmask of the network on which
      #   packets are being captured. It is only used when checking for IPv4
      #   broadcast addresses in the filter program. Default: 0 (unspecified 
{'fraction_non_alphanumeric': 0.08129619101762366, 'fraction_numerical': 0.003695281409891984, 'mean_word_length': 2.0463203463203463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6413725', 'n_tokens_mistral': 2107, 'n_tokens_neox': 2052, 'n_words': 701}