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

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

Overview

Ethernet MAC address, as a group of 6 bytes

Author:

  • Sylvain Daubert

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Types::Fieldable

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

Methods inherited from Types::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

#a0Integer

Returns first byte from MacAddr.

Returns:

  • (Integer)

    first byte from MacAddr



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

define_field :a0, Types::Int8

#a1Integer

Returns second byte from MacAddr.

Returns:

  • (Integer)

    second byte from MacAddr



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

define_field :a1, Types::Int8

#a2Integer

Returns third byte from MacAddr.

Returns:

  • (Integer)

    third byte from MacAddr



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

define_field :a2, Types::Int8

#a3Integer

Returns fourth byte from MacAddr.

Returns:

  • (Integer)

    fourth byte from MacAddr



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

define_field :a3, Types::Int8

#a4Integer

Returns fifth byte from MacAddr.

Returns:

  • (Integer)

    fifth byte from MacAddr



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

define_field :a4, Types::Int8

#a5Integer

Returns sixth byte from MacAddr.

Returns:

  • (Integer)

    sixth byte from MacAddr



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

define_field :a5, Types::Int8

Instance Method Details

#==(other) ⇒ Object



78
79
80
81
# File 'lib/packetgen/header/eth.rb', line 78

def ==(other)
  other.is_a?(self.class) &&
    fields.all? { |attr| self[attr].value == other[attr].value }
end

#from_human(str) ⇒ self

Read a human-readable string to populate MacAddr

Parameters:

  • str (String)

Returns:

  • (self)

Raises:

  • (ArgumentError)


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

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

  bytes = str.split(':')
  raise ArgumentError, 'not a MAC address' unless bytes.size == 6

  6.times do |i|
    self["a#{i}".to_sym].read(bytes[i].to_i(16))
  end
  self
end

#to_humanString

MacAddr in human readable form (colon format)

Returns:

  • (String)


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

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