Module: PacketGen::PCAPRUBWrapper Private

Defined in:
lib/packetgen/pcaprub_wrapper.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Wrapper around PCAPRUB

Author:

  • Sylvain Daubert

Since:

  • 3.1.4

Constant Summary collapse

TIMEOUT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

timeout for PCAPRUB

Since:

  • 3.1.4

1
DEFAULT_SNAPLEN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default snaplen to use

Since:

  • 3.1.4

0xffff
DEFAULT_PROMISC =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default promisc value to use

Since:

  • 3.1.4

false

Class Method Summary collapse

Class Method Details

.capture(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC, filter: nil, monitor: nil) {|packet_data| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Capture packets from a network interface

Parameters:

  • iface (String)

    interface name

  • snaplen (Integer) (defaults to: DEFAULT_SNAPLEN)
  • promisc (Boolean) (defaults to: DEFAULT_PROMISC)
  • filter (String) (defaults to: nil)

    BPF filter

  • monitor (Boolean) (defaults to: nil)

Yield Parameters:

  • packet_data (String)

    packet data

Author:

  • Sylvain Daubert

  • optix2000 - add support for setting monitor mode

Since:

  • 3.1.5 add monitor argument



52
53
54
55
56
# File 'lib/packetgen/pcaprub_wrapper.rb', line 52

def self.capture(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC, filter: nil, monitor: nil, &block)
  pcap = self.open_iface(iface: iface, snaplen: snaplen, promisc: promisc, monitor: monitor)
  pcap.setfilter filter unless filter.nil?
  pcap.each_packet(&block)
end

.inject(iface:, data:) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Inject given data onto wire

Parameters:

  • iface (String)

    interface name

  • data (String)

    to inject

Since:

  • 3.1.4



62
63
64
65
66
# File 'lib/packetgen/pcaprub_wrapper.rb', line 62

def self.inject(iface:, data:)
  pcap = self.open_iface(iface: iface)
  pcap.inject(data)
  pcap.close
end

.open_iface(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC, monitor: nil) ⇒ PCAPRUB::Pcap

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Open an interface for capturing

Parameters:

  • iface (String)

    interface name

  • snaplen (Integer) (defaults to: DEFAULT_SNAPLEN)
  • promisc (Boolean) (defaults to: DEFAULT_PROMISC)
  • monitor (Boolean) (defaults to: nil)

Returns:

  • (PCAPRUB::Pcap)

Author:

  • Sylvain Daubert

  • optix2000 - add support for setting monitor mode

Since:

  • 3.1.5 add monitor argument



31
32
33
34
35
36
37
38
39
# File 'lib/packetgen/pcaprub_wrapper.rb', line 31

def self.open_iface(iface:, snaplen: DEFAULT_SNAPLEN, promisc: DEFAULT_PROMISC, monitor: nil)
  pcap = PCAPRUB::Pcap.create(iface)
  pcap.setsnaplen(snaplen)
  pcap.setpromisc(promisc)
  pcap.settimeout(TIMEOUT)
  # Monitor MUST be set before pcap is activated
  pcap.setmonitor monitor unless monitor.nil?
  pcap.activate
end

.read_pcap(filename:) {|data| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Read a PCAP file

Parameters:

  • filename (String)

Yield Parameters:

  • data (String)

    binary packet data

Author:

  • Kent Gruber

Since:

  • 3.1.4



73
74
75
# File 'lib/packetgen/pcaprub_wrapper.rb', line 73

def self.read_pcap(filename:, &block)
  PCAPRUB::Pcap.open_offline(filename).each_packet(&block)
end