Class: MacAddressEui48::OuiResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/mac_address_eui48/oui_resolver.rb

Instance Method Summary collapse

Constructor Details

#initializeOuiResolver

Returns a new instance of OuiResolver.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mac_address_eui48/oui_resolver.rb', line 14

def initialize()
  @oui_hash=Hash.new(nil)
  path = File.expand_path('../../../data/oui.txt', __FILE__)
  oui_file = File.open(path)
  
  oui_file.each_line do |line|
    if (line =~ /\(hex\)/) then
      prefix = line.split("\t")[0].split(" ")[0].gsub("-",":").chomp
      oui = line.split("\t")[2].chomp
      @oui_hash[prefix]=oui
    end  
  end
  @reverse_oui_hash= @oui_hash.safe_invert
  @vendor_table=@oui_hash.values
  
end

Instance Method Details

#has_oui(mac) ⇒ Object



36
37
38
# File 'lib/mac_address_eui48/oui_resolver.rb', line 36

def has_oui(mac)
  return (oui_lookup(mac) != nil) 
end

#oui_lookup(mac_addr) ⇒ Object



30
31
32
# File 'lib/mac_address_eui48/oui_resolver.rb', line 30

def oui_lookup(mac_addr)
  return @oui_hash[mac_addr[0..7].upcase]
end

#random_macObject



40
41
42
43
# File 'lib/mac_address_eui48/oui_resolver.rb', line 40

def random_mac
  i = rand(2**48)
  return MacAddressEui48::int_to_str_mac(i)
end

#random_mac_vendor(vendor) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mac_address_eui48/oui_resolver.rb', line 52

def random_mac_vendor(vendor)
  ouis = reverse_lookup(vendor)
  if (ouis.size <=0) then
    raise "OUI not found #{vendor}"
  end
  
  oui = ouis[rand(ouis.size)]
  i1 =  MacAddressEui48::str_mac_to_int( oui + ":00:00:00")
  i2 = rand(2**24)       
  i = i1 + i2
  return MacAddressEui48::int_to_str_mac(i)
end

#random_oui_macObject



44
45
46
47
48
49
50
# File 'lib/mac_address_eui48/oui_resolver.rb', line 44

def random_oui_mac 
  # Some vendors may appear more than once in the OUI table 
  # All vendors don't have the same probability to be picked
  n = @vendor_table.size
  vendor = @vendor_table[rand(n)]
  return random_mac_vendor(vendor)
end

#reverse_lookup(vendor) ⇒ Object



33
34
35
# File 'lib/mac_address_eui48/oui_resolver.rb', line 33

def reverse_lookup(vendor)
  return @reverse_oui_hash[vendor]
end