class FFI::PCap::DataLink

Constants

SOME_DLTS

Several DLT names harvested out of the pcap-bpf.h header file. These are in alphabetical order. Their Array index does not match their pcap DLT value.

Don’t use this Array for anything except quick reference. Use the {lookup} class methods for actually resolving name to value mappings or such.

Attributes

to_i[R]

FFI::PCap datalink numeric value

value[R]

FFI::PCap datalink numeric value

Public Class Methods

describe(l) click to toggle source

@param [String, Symbol or Integer] l

The name or value to lookup. A Symbol is converted to String.
Names are case-insensitive.
# File lib/ffi/pcap/data_link.rb, line 100
def self.describe(l)
  l = l.to_s if l.kind_of?(Symbol)
  l = PCap.pcap_datalink_name_to_val(l) if l.kind_of?(String) 

  PCap.pcap_datalink_val_to_description(l)
end
lookup(l) click to toggle source

Uses the ‘pcap_datalnk_*` functions to lookup a datalink name and value pair.

@param [String, Symbol or Integer] l

The name or value to lookup. A Symbol is converted to String.
Names are case-insensitive.

@return [Array]

A 2-element array containing [value, name]. Both elements are
`nil` if the lookup failed.
# File lib/ffi/pcap/data_link.rb, line 48
def self.lookup(l)
  val, name = nil
  l = l.to_s if l.kind_of?(Symbol)

  case l
  when String
    if (v = name_to_val(l))
      name = val_to_name(v)  # get the canonical name
      val = v
    end
  when Integer
    name = val_to_name(l)
    val = l
  else
    raise(ArgumentError,"lookup takes either a String or Integer",caller)
  end
  return [val, name]
end
name_to_val(n) click to toggle source

Translates a data link type name, which is a ‘DLT_` name with the `DLT_` removed, to the corresponding data link type numeric value.

@param [String or Symbol] n

The name to lookup. Names are case-insensitive.

@return [Integer or nil]

The numeric value for the datalink name or `nil` on failure.
# File lib/ffi/pcap/data_link.rb, line 77
def self.name_to_val(n)
  n = n.to_s if n.kind_of?(Symbol)
  v = PCap.pcap_datalink_name_to_val(n)

  return v if v >= 0
end
new(arg) click to toggle source

Creates a new DataLink object with the specified value or name. The canonical name, value, and description are can be looked up on demand.

@param [String or Integer] arg

Arg can be a string or number which will be used to look up the
datalink.

@raise [UnsupportedDataLinkError]

An exception is raised if a name is supplied and a lookup for its
value fails or if the arg parameter is an invalid type.
# File lib/ffi/pcap/data_link.rb, line 123
def initialize(arg)
  case arg
  when String, Symbol
    unless (@value = self.class.name_to_val(arg.to_s))
      raise(UnsupportedDataLinkError, "Invalid DataLink: #{arg.to_s}")
    end
  when Numeric
    @value = arg
  else
    raise(UnsupportedDataLinkError,"Invalid DataLink: #{arg.inspect}",caller)
  end

  @name = self.class.val_to_name(@value)
end
val_to_name(v) click to toggle source

Translates a data link type value to the corresponding data link type name.

@return [String or nil]

The string name of the data-link or `nil` on failure.
# File lib/ffi/pcap/data_link.rb, line 91
def self.val_to_name(v)
  PCap.pcap_datalink_val_to_name(v)
end

Public Instance Methods

<=>(other) click to toggle source

Overrides the sort comparison operator to sort by DLT value.

# File lib/ffi/pcap/data_link.rb, line 160
def <=>(other)
  self.value <=> other.value
end
==(other) click to toggle source

Overrides the equality operator so that quick comparisons can be made against other DataLinks, name by String, or value by Integer.

# File lib/ffi/pcap/data_link.rb, line 142
def ==(other)
  case other
  when DataLink
    self.value == other.value
  when Numeric
    self.value == other
  when Symbol
    @value == self.class.name_to_val(other.to_s)
  when String
    @value == self.class.name_to_val(other)
  else
    false
  end
end
desc()
Alias for: description
describe()
Alias for: description
description() click to toggle source

Returns the description of the datalink.

# File lib/ffi/pcap/data_link.rb, line 167
def description
  @desc ||= self.class.describe(@value)
end
Also aliased as: desc, describe
inspect() click to toggle source

Override ‘inspect` to always provide the name for irb, pretty_print, etc.

# File lib/ffi/pcap/data_link.rb, line 185
def inspect
  "<#{self.class}:#{"0x%0.8x" % self.object_id} @value=#{@value}, @name=#{name().inspect}>"
end
name() click to toggle source

Returns the canonical String name of the DataLink object

# File lib/ffi/pcap/data_link.rb, line 177
def name
  @name
end
Also aliased as: to_s
to_s()
Alias for: name