Class: MacVendor

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

Constant Summary collapse

OUI_FULL_URL =
'http://standards.ieee.org/develop/regauth/oui/oui.txt'
OUI_SINGLE_URL =
'http://standards.ieee.org/cgi-bin/ouisearch?'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MacVendor

Returns a new instance of MacVendor.



7
8
9
10
11
12
# File 'lib/mac_vendor.rb', line 7

def initialize(opts={})
  @prefix_cache = {}
  @preloaded = false
  @network_hits = 0 if opts[:enable_network_tracking]
  preload_cache_via_local_data if opts[:use_local]
end

Instance Method Details

#fetch_single(prefix) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mac_vendor.rb', line 35

def fetch_single(prefix)
  single_txt = get_url OUI_SINGLE_URL + hyphen_prefix(prefix)
  
  if single_txt =~ /The public OUI listing contains no match for the query/
    return @prefix_cache[prefix] = nil
  end
  
  if single_txt.gsub(/[\r\n]/,"\t") =~ /([0-9A-F]+)\s+\(base 16\)(.*?)pre/
    mac_prefix = normalize_prefix $1
    lines = $2.strip.split("\t")
    company_name = lines[0]
    company_address = lines[1..-2].reject {|x| x.strip == ''}

    return @prefix_cache[prefix] = {:name => company_name, :address => company_address}
  end
  nil
end

#get_url(url) ⇒ Object



30
31
32
33
# File 'lib/mac_vendor.rb', line 30

def get_url(url)
  @network_hits +=1 unless @network_hits.nil?
  Net::HTTP.get URI(url)
end

#hyphen_prefix(p) ⇒ Object

Converts a normalized prefix into something with hyphens, e.g.: AA-BB-CC



26
27
28
# File 'lib/mac_vendor.rb', line 26

def hyphen_prefix(p)
  "#{p[0..1]}-#{p[2..3]}-#{p[4..5]}"
end

#lookup(mac) ⇒ Object



14
15
16
17
18
# File 'lib/mac_vendor.rb', line 14

def lookup(mac)
  prefix = self.normalize_prefix mac
  return @prefix_cache[prefix] if @preloaded or @prefix_cache.has_key?(prefix)
  @prefix_cache[prefix] = fetch_single(prefix)
end

#network_hitsObject



86
87
88
# File 'lib/mac_vendor.rb', line 86

def network_hits
  @network_hits
end

#normalize_prefix(mac) ⇒ Object

Attempts to turn anything MAC-like into the first six digits, e.g.: AABBCC



21
22
23
# File 'lib/mac_vendor.rb', line 21

def normalize_prefix(mac)
  mac.strip.upcase.gsub(/^0[xX]/,'').gsub(/[^0-9A-F]/,'')[0..5]
end

#preload_cacheObject



82
83
84
# File 'lib/mac_vendor.rb', line 82

def preload_cache
  preload_cache_via_string get_url(OUI_FULL_URL)
end

#preload_cache_via_local_dataObject



77
78
79
80
# File 'lib/mac_vendor.rb', line 77

def preload_cache_via_local_data
  local_path = File.expand_path(File.dirname(__FILE__) + "/../data/oui.txt.gz")
  preload_cache_via_string Zlib::GzipReader.open(local_path).read
end

#preload_cache_via_string(oui_txt) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mac_vendor.rb', line 53

def preload_cache_via_string(oui_txt)
  # First entry is a header
  entries = oui_txt.split("\n\n")

  entries[1..-1].each do |entry|
    base16_fields = entry.strip.split("\n")[1].split("\t")
    mac_prefix = base16_fields[0].strip[0..5]
    company_name = base16_fields[-1]
    company_address = entry.strip.gsub("\t",'').split("\n")[2..-1].map {|x| x.strip}

    # This actually happens three times in the current dataset!
    unless @prefix_cache[mac_prefix].nil?
      #puts "MAC PREFIX COLLISION: #{mac_prefix}"
      #puts "CURRENT = #{@prefix_cache[mac_prefix].inspect}"
      #puts "NEW = #{{:name => company_name, :address => company_address}.inspect}"
      #raise "MAC prefix key collision: #{mac_prefix}"
      next
    end
  
    @prefix_cache[mac_prefix] = {:name => company_name, :address => company_address}
  end
  @preloaded = true
end