Module: OUI

Extended by:
OUI
Included in:
OUI
Defined in:
lib/oui.rb

Overview

Organizationally Unique Identifier

Constant Summary collapse

@@local_db =
LOCAL_DB_DEFAULT
@@debugging =
DEBUGGING_DEFAULT
@@import_local_txt_file =
IMPORT_LOCAL_TXT_FILE_DEFAULT
@@in_memory_only =
IN_MEMORY_ONLY_DEFAULT
@@db =
nil

Instance Method Summary collapse

Instance Method Details

#clear_tableObject



138
139
140
141
# File 'lib/oui.rb', line 138

def clear_table
  debug 'clear_table'
  table.delete_sql
end

#close_dbObject

Release backend resources



128
129
130
131
132
133
134
135
136
# File 'lib/oui.rb', line 128

def close_db
  semaphore.synchronize do
    debug 'Closing database'
    if @@db 
      @@db.disconnect
      @@db = nil
    end
  end
end

#debuggingObject



59
60
61
# File 'lib/oui.rb', line 59

def debugging
  @@debugging
end

#debugging=(v) ⇒ Object



63
64
65
# File 'lib/oui.rb', line 63

def debugging=(v)
  @@debugging = (v.nil?) ? DEBUGGING_DEFAULT : v
end

#find(oui) ⇒ Hash?

Parameters:

  • oui (String, Integer)

    hex or numeric OUI to find

Returns:

  • (Hash, nil)


94
95
96
97
98
99
100
101
# File 'lib/oui.rb', line 94

def find(oui)
  semaphore.synchronize do
    update_db unless table? && table.count > 0
    r = table.where(id: self.to_i(oui)).first
    r.delete :create_table if r # not sure why this is here, but nuking it
    r
  end
end

#import_local_txt_fileObject



70
71
72
# File 'lib/oui.rb', line 70

def import_local_txt_file
  @@import_local_txt_file
end

#import_local_txt_file=(v) ⇒ Object



74
75
76
# File 'lib/oui.rb', line 74

def import_local_txt_file=(v)
  @@import_local_txt_file = (v.nil?) ? IMPORT_LOCAL_TXT_FILE_DEFAULT : v
end

#in_memory_onlyObject



81
82
83
# File 'lib/oui.rb', line 81

def in_memory_only
  @@in_memory_only
end

#in_memory_only=(v) ⇒ Object



85
86
87
88
89
90
# File 'lib/oui.rb', line 85

def in_memory_only=(v)
  if v != @@in_memory_only
    @@in_memory_only = (v.nil?) ? IN_MEMORY_ONLY_DEFAULT : v
    close_db
  end
end

#local_dbObject



48
49
50
# File 'lib/oui.rb', line 48

def local_db
  @@local_db
end

#local_db=(v) ⇒ Object



52
53
54
# File 'lib/oui.rb', line 52

def local_db=(v)
  @@local_db = v || LOCAL_DB_DEFAULT
end

#to_i(oui) ⇒ Integer

Converts an OUI string to an integer of equal value

Parameters:

  • oui (String, Integer)

    MAC OUI in hexadecimal formats hhhh.hh, hh:hh:hh, hh-hh-hh or hhhhhh

Returns:

  • (Integer)

    numeric representation of oui



107
108
109
110
111
112
113
# File 'lib/oui.rb', line 107

def to_i(oui)
  return oui if oui.is_a? Integer
  oui = oui.strip.gsub(/[:\- .]/, '')
  if oui =~ /([[:xdigit:]]{6})/
    $1.to_i(16)
  end
end

#to_s(id, sep = '-') ⇒ String

Convert an id to OUI

Parameters:

  • oui (String, nil)

    string to place between pairs of hex digits, nil for none

Returns:

  • (String)

    hexadecimal format of id



118
119
120
121
122
123
124
# File 'lib/oui.rb', line 118

def to_s(id, sep = '-')
  return id if id.is_a? String
  unless id >= 0x000000 && id <= 0xFFFFFF
    raise ArgumentError, "#{id} is not a valid 24-bit OUI"
  end
  format('%06x', id).scan(/../).join(sep)
end

#update_db(local = nil, db_file = nil) ⇒ Integer

Update database from fetched URL

Parameters:

  • whether (Boolean)

    to connect to the network or not

Returns:

  • (Integer)

    number of unique records loaded



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/oui.rb', line 146

def update_db(local = nil, db_file = nil)
  semaphore.synchronize do
    debug "update_db(local = #{local}, db_file = #{db_file})"
    close_db
    old_import_local_txt_file = self.import_local_txt_file
    self.import_local_txt_file = local
    old_local_db = self.local_db
    self.local_db = db_file
    ## Sequel
    debug '--- close db ---'
    debug '--- close db ---'
    debug '--- drop table ---'
    drop_table
#      debug '--- drop table ---'
#      debug '--- create table ---'
    create_table
#      debug '--- create table ---'
    db.transaction do
      debug '--- clear table ---'
      clear_table
      debug '--- clear table ---'
      debug '--- install manual ---'
      install_manual
      debug '--- install manual ---'
      debug '--- install updates ---'
      install_updates
      debug '--- install updates ---'
    end
    debug '--- close db ---'
    close_db
    debug '--- close db ---'

    self.local_db = old_local_db
    self.import_local_txt_file = old_import_local_txt_file
    ## AR
    # self.transaction do
    #   self.delete_all
    #   self.install_manual
    #   self.install_updates
    # end
    debug "update_db(local = #{local}, db_file = #{db_file}) finish"
  end
end