Class: FFI::PCap::DataLink

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/pcap/data_link.rb

Constant Summary collapse

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.

%w[
  A429 A653_ICM AIRONET_HEADER APPLE_IP_OVER_IEEE1394 
  ARCNET ARCNET_LINUX ATM_CLIP ATM_RFC1483 AURORA AX25 BACNET_MS_TP 
  BLUETOOTH_HCI_H4 BLUETOOTH_HCI_H4_WITH_PHDR CAN20B CHAOS CHDLC
  CISCO_IOS C_HDLC DOCSIS ECONET EN10MB EN3MB ENC ERF ERF_ETH ERF_POS
  FDDI FRELAY GCOM_SERIAL GCOM_T1E1 GPF_F GPF_T GPRS_LLC HHDLC IBM_SN
  IBM_SP IEEE802 IEEE802_11 IEEE802_11_RADIO IEEE802_11_RADIO_AVS
  IEEE802_15_4 IEEE802_15_4_LINUX IEEE802_16_MAC_CPS
  IEEE802_16_MAC_CPS_RADIO IPFILTER IPMB IP_OVER_FC JUNIPER_ATM1
  JUNIPER_ATM2 JUNIPER_CHDLC JUNIPER_ES JUNIPER_ETHER JUNIPER_FRELAY
  JUNIPER_GGSN JUNIPER_ISM JUNIPER_MFR JUNIPER_MLFR JUNIPER_MLPPP
  JUNIPER_MONITOR JUNIPER_PIC_PEER JUNIPER_PPP JUNIPER_PPPOE
  JUNIPER_PPPOE_ATM JUNIPER_SERVICES JUNIPER_ST JUNIPER_VP LINUX_IRDA
  LINUX_LAPD LINUX_PPP_WITHDIRECTION LINUX_SLL LOOP LTALK MFR MTP2
  MTP2_WITH_PHDR MTP3 NULL OLD_PFLOG PCI_EXP PFLOG PFSYNC PPI PPP
  PPP_BSDOS PPP_ETHER PPP_PPPD PPP_SERIAL PPP_WITH_DIRECTION
  PRISM_HEADER PRONET RAIF1 RAW REDBACK_SMARTEDGE RIO SCCP SITA SLIP
  SLIP_BSDOS SUNATM SYMANTEC_FIREWALL TZSP USB USB_LINUX USER0 USER1
  USER10 USER11 USER12 USER13 USER14 USER15 USER2 USER3 USER4 USER5
  USER6 USER7 USER8 USER9
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ DataLink

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

Parameters:

  • arg (String or Integer)

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

Raises:

  • (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.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

Instance Attribute Details

#valueObject (readonly) Also known as: to_i

FFI::PCap datalink numeric value



108
109
110
# File 'lib/ffi/pcap/data_link.rb', line 108

def value
  @value
end

Class Method Details

.describe(l) ⇒ Object

Parameters:

  • l (String, Symbol or Integer)

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



100
101
102
103
104
105
# 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) ⇒ Array

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

Parameters:

  • l (String, Symbol or Integer)

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

Returns:

  • (Array)

    A 2-element array containing [value, name]. Both elements are nil if the lookup failed.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Integer or nil

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

Parameters:

  • n (String or Symbol)

    The name to lookup. Names are case-insensitive.

Returns:

  • (Integer or nil)

    The numeric value for the datalink name or nil on failure.



77
78
79
80
81
82
# 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

.val_to_name(v) ⇒ String or nil

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

Returns:

  • (String or nil)

    The string name of the data-link or nil on failure.



91
92
93
# File 'lib/ffi/pcap/data_link.rb', line 91

def self.val_to_name(v)
  PCap.pcap_datalink_val_to_name(v)
end

Instance Method Details

#<=>(other) ⇒ Object

Overrides the sort comparison operator to sort by DLT value.



160
161
162
# File 'lib/ffi/pcap/data_link.rb', line 160

def <=>(other)
  self.value <=> other.value
end

#==(other) ⇒ Object

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# 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

#descriptionObject Also known as: desc, describe

Returns the description of the datalink.



167
168
169
# File 'lib/ffi/pcap/data_link.rb', line 167

def description
  @desc ||= self.class.describe(@value)
end

#inspectObject

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



185
186
187
# 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

#nameObject Also known as: to_s

Returns the canonical String name of the DataLink object



177
178
179
# File 'lib/ffi/pcap/data_link.rb', line 177

def name
  @name
end