Class: Ethernet::Address

Inherits:
Object show all
Defined in:
lib/ethernet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil) ⇒ Address

Returns a new instance of Address.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ethernet.rb', line 55

def initialize(arg=nil)
  if arg.is_a?(String) and arg.size==6
    parse(arg)
  elsif arg.is_a?(String) and arg.size==14
    parse [arg.split('.').join].pack('H*')
  elsif arg.is_a?(String)
    @mac = arg.split(/[-:]/).collect { |n| n.to_i(16) }
  elsif arg.is_a?(String) and size==14
  elsif arg.kind_of?(self.class)
    parse arg.encode
  elsif arg.kind_of?(self.class)
    parse arg.encode
  elsif ! arg
    @mac = [0,0,0,0,0,0]
  else
    raise ArgumentError, "Argument error: #{self.class} #{arg.inspect}"
  end
end

Instance Attribute Details

#macObject (readonly)

Returns the value of attribute mac.



53
54
55
# File 'lib/ethernet.rb', line 53

def mac
  @mac
end

Instance Method Details

#encodeObject

– def to_s_oui

comp_id = Ethernet::OUI.company_id_from_arr(@mac[0..2])
if comp_id == 'Unknown'
  to_s.downcase
else
  s = []
  s << comp_id
  s <<  (format (["%02x"]*3).join(":"), *@mac[3..5])
  s.join('_')
end

end ++



97
98
99
# File 'lib/ethernet.rb', line 97

def encode
  @mac.pack('C*')
end

#to_hashObject



101
102
103
# File 'lib/ethernet.rb', line 101

def to_hash
  to_s
end

#to_s(delim = ":") ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ethernet.rb', line 74

def to_s(delim=":")
  case delim
  when ':'
    (format (["%02x"]*6).join(delim), *@mac)
  when '.'
    (format (["%04x"]*3).join(delim), *(@mac.pack('C6').unpack('n3')))
  end
end