class Macker::Address
MAC address class
Attributes
Get the value of name, address or iso code @return [String] content of the value
Get the value of name, address or iso code @return [String] content of the value
Get the value of name, address or iso code @return [String] content of the value
Public Class Methods
Initialize Address
object @param mac [Address,Integer,String] a MAC address @param opts [Hash] options for the method @return [Address] the initialized object
# File lib/macker/address.rb, line 19 def initialize(mac, opts = {}) case mac when Address @val = mac.to_i @name = mac.name @address = mac.address @iso_code = mac.iso_code when Integer @val = mac when String @val = cleanup(mac).to_i(16) else raise(InvalidAddress, "Incompatible type for address initialization: #{mac.class}") end raise(InvalidAddress, "Invalid MAC address: #{self}") unless valid? @name ||= opts.fetch(:name, nil) @address ||= opts.fetch(:address, nil) @iso_code ||= opts.fetch(:iso_code, nil) end
Public Instance Methods
Compare two MAC addresses @param other [Address] MAC address object @return [Boolean] true if the same, else false
# File lib/macker/address.rb, line 62 def <=>(other) @val <=> other.to_i end
Check if MAC address is a broadcast address @return [Boolean] true if broadcast, else false
# File lib/macker/address.rb, line 80 def broadcast? @val == 2**48 - 1 end
Get the full vendor address @return [String] full vendor address string
# File lib/macker/address.rb, line 125 def full_address address.join(', ') end
Check if MAC address is a global uniq address @return [Boolean] true if uniq, else false
# File lib/macker/address.rb, line 99 def global_uniq? !local_admin? end
Check if MAC address is a local address @return [Boolean] true if local, else false
# File lib/macker/address.rb, line 105 def local_admin? mask = 2 << (5 * 8) (mask & @val) != 0 end
Check if MAC address is a multicast address @return [Boolean] true if multicast, else false
# File lib/macker/address.rb, line 92 def multicast? mask = 1 << (5 * 8) (mask & @val) != 0 end
Get next MAC address from actual address @return [Adress] next MAC address
# File lib/macker/address.rb, line 112 def next Address.new((@val + 1) % 2**48) end
Check if MAC address is an OUI valid address @return [Boolean] true if valid, else false
# File lib/macker/address.rb, line 68 def oui? !@name.nil? end
Get the prefix base16 MAC address @return [Adress] MAC prefix
# File lib/macker/address.rb, line 119 def prefix to_s('')[0..5] end
Format MAC address to integer @return [Integer] integer MAC address
# File lib/macker/address.rb, line 41 def to_i @val end
Format MAC address to string @param sep [String] separator, default is ‘:’ @return [String] formatted MAC address
# File lib/macker/address.rb, line 48 def to_s(sep = ':') @val.to_s(16) .rjust(12, '0') .insert(10, sep) .insert(8, sep) .insert(6, sep) .insert(4, sep) .insert(2, sep) .upcase end
Check if MAC address is an unicast address @return [Boolean] true if unicast, else false
# File lib/macker/address.rb, line 86 def unicast? !multicast? end
Check if MAC address is a valid address @return [Boolean] true if valid, else false
# File lib/macker/address.rb, line 74 def valid? @val.between?(0, 2**48 - 1) end
Private Instance Methods
Clean up a MAC string from special characters @return [String] cleaned MAC address
# File lib/macker/address.rb, line 133 def cleanup(mac) mac.strip.upcase.gsub(/^0[xX]/, '').gsub(/[^0-9A-F]/, '').ljust(12, '0') end