class MacVendor

Constants

OUI_FULL_URL
OUI_SINGLE_URL

Public Class Methods

new(opts={}) click to toggle source
# 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

Public Instance Methods

fetch_single(prefix) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# File lib/mac_vendor.rb, line 26
def hyphen_prefix(p)
  "#{p[0..1]}-#{p[2..3]}-#{p[4..5]}"
end
lookup(mac) click to toggle source
# 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_hits() click to toggle source
# File lib/mac_vendor.rb, line 86
def network_hits
  @network_hits
end
normalize_prefix(mac) click to toggle source

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

# 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_cache() click to toggle source
# File lib/mac_vendor.rb, line 82
def preload_cache
  preload_cache_via_string get_url(OUI_FULL_URL)
end
preload_cache_via_local_data() click to toggle source
# 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) click to toggle source
# 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