Class: PacketGen::Types::OUI

Inherits:
Fields
  • Object
show all
Includes:
Fieldable
Defined in:
lib/packetgen/types/oui.rb

Overview

OUI type, defined as a set of 3 bytes

oui = OUI.new
oui.from_human('00:01:02')
oui.to_human   # => "00:01:02"

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fieldable

#format_inspect, #read, #sz, #to_s, #type_name

Methods inherited from Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #initialize, #inspect, #offset_of, #optional?, #optional_fields, #present?, #read, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

This class inherits a constructor from PacketGen::Types::Fields

Instance Attribute Details

#b0Integer

Returns right-most byte.

Returns:

  • (Integer)

    right-most byte


27
# File 'lib/packetgen/types/oui.rb', line 27

define_field :b0, Types::Int8

#b1Integer

Returns center byte.

Returns:

  • (Integer)

    center byte


24
# File 'lib/packetgen/types/oui.rb', line 24

define_field :b1, Types::Int8

#b2Integer

Returns left-most byte.

Returns:

  • (Integer)

    left-most byte


21
# File 'lib/packetgen/types/oui.rb', line 21

define_field :b2, Types::Int8

Instance Method Details

#from_human(str) ⇒ OUI

Read a human-readable string to populate object

Parameters:

Returns:

  • (OUI)

    self

Raises:

  • (ArgumentError)

32
33
34
35
36
37
38
39
40
41
42
# File 'lib/packetgen/types/oui.rb', line 32

def from_human(str)
  return self if str.nil?

  bytes = str.split(':')
  raise ArgumentError, 'not a OUI' unless bytes.size == 3

  self[:b2].read(bytes[0].to_i(16))
  self[:b1].read(bytes[1].to_i(16))
  self[:b0].read(bytes[2].to_i(16))
  self
end

#to_humanString

Get OUI in human readable form (colon-separated bytes)

Returns:


46
47
48
# File 'lib/packetgen/types/oui.rb', line 46

def to_human
  fields.map { |m| '%02x' % self[m] }.join(':')
end