Module: Louis

Extended by:
Helpers
Defined in:
lib/louis.rb,
lib/louis/const.rb,
lib/louis/helpers.rb,
lib/louis/version.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

ORIGINAL_OUI_FILE =
File.expand_path(File.join(File.dirname(__FILE__), \
'..', '..', 'data', 'mac_oui_manuf.txt'))
PARSED_DATA_FILE =
File.expand_path(File.join(File.dirname(__FILE__), \
'..', '..', 'data', 'processed_data.json'))
OUI_FORMAT_REGEX =
/^(?<prefix>[0-9a-fA-F:\-]+)(\/(?<mask>(\d+)))?\s+(?<short_vendor>\S+)(\s+# (?<long_vendor>.+))?$/
VERSION =
"2.0.1"

Class Method Summary collapse

Methods included from Helpers

calculate_mask, clean_mac, count_bits, line_parser, mac_to_num

Class Method Details

.lookup(mac) ⇒ String

Returns the name of the vendor that has the most specific prefix available in the OUI table or failing any matches will return “Unknown”.

Parameters:

  • mac (String)

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/louis.rb', line 29

def self.lookup(mac)
  match = nil

  mask_keys.each do |mask|
    prefix = mac_to_num(mac) & calculate_mask(nil, mask)
    match = lookup_table[mask.to_s][prefix.to_s]

    break if match
  end

  if match
    {'long_vendor' => match['l'], 'short_vendor' => match['s']}
  else
    {'long_vendor' => 'Unknown', 'short_vendor' => 'Unknown'}
  end
end

.lookup_tablevoid

Loads the lookup table, parsing out the uncommented non-blank lines into objects we can compare MACs against to find their vendor.



12
13
14
# File 'lib/louis.rb', line 12

def self.lookup_table
  @lookup_table ||= JSON.parse(File.read(Louis::PARSED_DATA_FILE))
end

.mask_keysvoid

Collect the recorded mask and order it appropriately from most specific to least.

Parameters:

  • (Array<Fixnum>)


20
21
22
# File 'lib/louis.rb', line 20

def self.mask_keys
  @mask_keys ||= lookup_table.keys.map(&:to_i).sort.reverse
end