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)


23
24
25
26
27
28
29
30
31
# File 'lib/packetgen/header/eth.rb', line 23

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



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a0
  @a0
end

#a1Object

Returns the value of attribute a1

Returns:

  • (Object)

    the current value of a1



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a1
  @a1
end

#a2Object

Returns the value of attribute a2

Returns:

  • (Object)

    the current value of a2



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a2
  @a2
end

#a3Object

Returns the value of attribute a3

Returns:

  • (Object)

    the current value of a3



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a3
  @a3
end

#a4Object

Returns the value of attribute a4

Returns:

  • (Object)

    the current value of a4



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a4
  @a4
end

#a5Object

Returns the value of attribute a5

Returns:

  • (Object)

    the current value of a5



13
14
15
# File 'lib/packetgen/header/eth.rb', line 13

def a5
  @a5
end

Instance Method Details

#parse(str) ⇒ self

Parse a string to populate MacAddr

Parameters:

Returns:

  • (self)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/packetgen/header/eth.rb', line 36

def parse(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 string

Parameters:

  • str (String)

    binary string

Returns:

  • (self)

Raises:



54
55
56
57
58
59
60
61
# File 'lib/packetgen/header/eth.rb', line 54

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_xString

Addr in human readable form (dotted format)

Returns:



70
71
72
# File 'lib/packetgen/header/eth.rb', line 70

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