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

Inherits:
Struct
  • Object
show all
Includes:
StructFu
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 StructFu

#body=, #clone, #set_endianness, #sz, #to_s, #typecast

Methods inherited from Struct

#force_binary

Constructor Details

#initialize(options = {}) ⇒ MacAddr

Returns a new instance of MacAddr.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :a0 (Integer)
  • :a1 (Integer)
  • :a2 (Integer)
  • :a3 (Integer)
  • :a4 (Integer)
  • :a5 (Integer)


48
49
50
51
52
53
54
55
56
# File 'lib/packetgen/header/eth.rb', line 48

def initialize(options={})
  super Int8.new(options[:a0]),
        Int8.new(options[:a1]),
        Int8.new(options[:a2]),
        Int8.new(options[:a3]),
        Int8.new(options[:a4]),
        Int8.new(options[:a5])

end

Instance Attribute Details

#a0Object

Returns the value of attribute a0

Returns:

  • (Object)

    the current value of a0



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

def a0
  @a0
end

#a1Object

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



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

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



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

def a2
  @a2
end

#a3Object

Returns the value of attribute a3

Returns:

  • (Object)

    the current value of a3



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

def a3
  @a3
end

#a4Object

Returns the value of attribute a4

Returns:

  • (Object)

    the current value of a4



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

def a4
  @a4
end

#a5Object

Returns the value of attribute a5

Returns:

  • (Object)

    the current value of a5



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

def a5
  @a5
end

Instance Method Details

#from_human(str) ⇒ self

Read a human-readable string to populate MacAddr

Parameters:

Returns:

  • (self)


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

def from_human(str)
  return self if str.nil?
  bytes = str.split(/:/)
  unless bytes.size == 6
    raise ArgumentError, 'not a MAC address'
  end
  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

#read(str) ⇒ self

Read a MacAddr from a binary string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



79
80
81
82
83
84
85
86
# File 'lib/packetgen/header/eth.rb', line 79

def read(str)
  return self if str.nil?
  raise ParseError, 'string too short for Eth' if str.size < self.sz
  force_binary str
  [:a0, :a1, :a2, :a3, :a4, :a5].each_with_index do |byte, i|
    self[byte].read str[i, 1]
  end
end

#to_humanString

MacAddr in human readable form (colon format)

Returns:



95
96
97
# File 'lib/packetgen/header/eth.rb', line 95

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