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
FFI::PCap
datalink numeric value
FFI::PCap
datalink numeric value
Public Class Methods
@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
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
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
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
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
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
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
Returns the description of the datalink.
# File lib/ffi/pcap/data_link.rb, line 167 def description @desc ||= self.class.describe(@value) end
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