Sha256: e929d89ecc325494378d5211ee5303c64460c180978ac494090b9e7b1d2bd6cd

Contents?: true

Size: 1.77 KB

Versions: 7

Compression:

Stored size: 1.77 KB

Contents

require 'json'

require 'louis/const'
require 'louis/helpers'
require 'louis/version'

module Louis
  # This masks out both the 'universal/local' bit as well as the
  # 'unicast/multicast' bit which is the first and second least significant bit
  # of the first byte in a vendor prefix.
  IGNORED_BITS_MASK = 0xfcffffffffff

  # Loads the lookup table, parsing out the uncommented non-blank lines into
  # objects we can compare MACs against to find their vendor.
  LOOKUP_TABLE = JSON.parse(File.read(Louis::PARSED_DATA_FILE))

  # Collect the recorded mask and order it appropriately from most specific to
  # least.
  #
  # @param [Array<Fixnum>]
  def self.mask_keys
    @mask_keys ||= LOOKUP_TABLE.keys.map(&:to_i).sort.reverse
  end

  # Returns the name of the vendor that has the most specific prefix
  # available in the OUI table or failing any matches will return "Unknown".
  #
  # @param [String] mac
  # @return [String]
  def self.lookup(mac)
    numeric_mac = Louis::Helpers.mac_to_num(mac)
    masked_mac = numeric_mac & IGNORED_BITS_MASK

    if (vendor = search_table(masked_mac))
      return {
        'long_vendor' => vendor['l'],
        'short_vendor' => vendor['s']
      }.compact
    end

    # Try again, but this time don't ignore any bits (Looking at you
    # Google... with your 'da' prefix...)
    if (vendor = search_table(numeric_mac))
      return {
        'long_vendor' => vendor['l'],
        'short_vendor' => vendor['s']
      }.compact
    end

    {'long_vendor' => 'Unknown', 'short_vendor' => 'Unknown'}
  end

  def self.search_table(encoded_mac)
    mask_keys.each do |mask|
      table = LOOKUP_TABLE[mask.to_s]
      prefix = (encoded_mac & Louis::Helpers.calculate_mask(nil, mask)).to_s
      return table[prefix] if table.include?(prefix)
    end

    nil
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
louis-2.2.6 lib/louis.rb
louis-2.2.5 lib/louis.rb
louis-2.2.4 lib/louis.rb
louis-2.2.3 lib/louis.rb
louis-2.2.2 lib/louis.rb
louis-2.2.1 lib/louis.rb
louis-2.2.0 lib/louis.rb