Class: PacketGen::Header::Eth::MacAddr

Inherits:
Types::Fields show all
Defined in:
lib/packetgen/header/eth.rb

Overview

Ethernet MAC address, as a group of 6 bytes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Types::Fields

#[], #[]=, #bits_on, #body=, define_bit_fields_on, define_field, define_field_after, define_field_before, delete_field, fields, #fields, #force_binary, inherited, #initialize, #inspect, #is_optional?, #is_present?, #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

#a0Integer



38
# File 'lib/packetgen/header/eth.rb', line 38

define_field :a0, Types::Int8

#a1Integer



41
# File 'lib/packetgen/header/eth.rb', line 41

define_field :a1, Types::Int8

#a2Integer



44
# File 'lib/packetgen/header/eth.rb', line 44

define_field :a2, Types::Int8

#a3Integer



47
# File 'lib/packetgen/header/eth.rb', line 47

define_field :a3, Types::Int8

#a4Integer



50
# File 'lib/packetgen/header/eth.rb', line 50

define_field :a4, Types::Int8

#a5Integer



53
# File 'lib/packetgen/header/eth.rb', line 53

define_field :a5, Types::Int8

Instance Method Details

#from_human(str) ⇒ self

Read a human-readable string to populate MacAddr

Raises:

  • (ArgumentError)


58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/packetgen/header/eth.rb', line 58

def from_human(str)
  return self if str.nil?
  bytes = str.split(/:/)
  raise ArgumentError, 'not a MAC address' unless bytes.size == 6
  self[:a0].read(bytes[0].to_i(16))
  self[:a1].read(bytes[1].to_i(16))
  self[:a2].read(bytes[2].to_i(16))
  self[:a3].read(bytes[3].to_i(16))
  self[:a4].read(bytes[4].to_i(16))
  self[:a5].read(bytes[5].to_i(16))
  self
end

#to_humanString

MacAddr in human readable form (colon format)



73
74
75
# File 'lib/packetgen/header/eth.rb', line 73

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