Class: Caper::DataLink

Inherits:
Object
  • Object
show all
Defined in:
lib/caper/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.



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/caper/data_link.rb', line 109

def initialize(arg)
  if arg.kind_of? String or arg.kind_of? Symbol
    unless @value = self.class.name_to_val(arg.to_s)
      raise(UnsupportedDataLinkError, "Invalid DataLink: #{arg.to_s}")
    end
  elsif arg.kind_of? Numeric
    @value = arg
  else
    raise(UnsupportedDataLinkError, "Invalid DataLink: #{arg.inspect}")
  end
  @name = self.class.val_to_name(@value)
end

Instance Attribute Details

#valueObject (readonly) Also known as: to_i

Caper datalink numeric value



95
96
97
# File 'lib/caper/data_link.rb', line 95

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.



88
89
90
91
92
# File 'lib/caper/data_link.rb', line 88

def self.describe(l)
  l = l.to_s if l.kind_of?(Symbol)
  l = Caper.pcap_datalink_name_to_val(l) if l.kind_of?(String) 
  Caper.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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/caper/data_link.rb', line 41

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")
  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.



68
69
70
71
72
73
# File 'lib/caper/data_link.rb', line 68

def self.name_to_val(n)
  n = n.to_s if n.kind_of?(Symbol)
  if (v=Caper.pcap_datalink_name_to_val(n)) >= 0
    return v
  end
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.



81
82
83
# File 'lib/caper/data_link.rb', line 81

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

Instance Method Details

#<=>(other) ⇒ Object

Overrides the sort comparison operator to sort by DLT value.



140
141
142
# File 'lib/caper/data_link.rb', line 140

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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/caper/data_link.rb', line 124

def ==(other)
  case other
  when DataLink
    return (self.value == other.value)
  when Numeric
    return (self.value == other)
  when Symbol
    return (@value == self.class.name_to_val(other.to_s))
  when String
    return (@value == self.class.name_to_val(other))
  else
    return false
  end
end

#descriptionObject Also known as: desc, describe

Returns the description of the datalink.



145
146
147
# File 'lib/caper/data_link.rb', line 145

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

#inspectObject

Override ‘inspect’ we’ll to always provide the name for irb, pretty_print, etc.



159
160
161
# File 'lib/caper/data_link.rb', line 159

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



153
154
155
# File 'lib/caper/data_link.rb', line 153

def name
  @name
end