Class: Macker::Address

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/macker/address.rb

Overview

MAC address class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mac, opts = {}) ⇒ Address

Initialize Address object

Parameters:

  • mac (Address, Integer, String)

    a MAC address

  • opts (Hash) (defaults to: {})

    options for the method

Raises:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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.to_s}") unless valid?
  @name     ||= opts.fetch(:name, nil)
  @address  ||= opts.fetch(:address, nil)
  @iso_code ||= opts.fetch(:iso_code, nil)
end

Instance Attribute Details

#addressString (readonly)

Get the value of name, address or iso code

Returns:

  • (String)

    content of the value


13
14
15
# File 'lib/macker/address.rb', line 13

def address
  @address
end

#iso_codeString (readonly)

Get the value of name, address or iso code

Returns:

  • (String)

    content of the value


13
14
15
# File 'lib/macker/address.rb', line 13

def iso_code
  @iso_code
end

#nameString (readonly)

Get the value of name, address or iso code

Returns:

  • (String)

    content of the value


13
14
15
# File 'lib/macker/address.rb', line 13

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Boolean

Compare two MAC addresses

Parameters:

  • other (Address)

    MAC address object

Returns:

  • (Boolean)

    true if the same, else false


62
63
64
# File 'lib/macker/address.rb', line 62

def <=>(other)
  @val <=> other.to_i
end

#broadcast?Boolean

Check if MAC address is a broadcast address

Returns:

  • (Boolean)

    true if broadcast, else false


80
81
82
# File 'lib/macker/address.rb', line 80

def broadcast?
  @val == 2**48 - 1
end

#full_addressString

Get the full vendor address

Returns:

  • (String)

    full vendor address string


125
126
127
# File 'lib/macker/address.rb', line 125

def full_address
  address.join(', ')
end

#global_uniq?Boolean

Check if MAC address is a global uniq address

Returns:

  • (Boolean)

    true if uniq, else false


99
100
101
# File 'lib/macker/address.rb', line 99

def global_uniq?
  !local_admin?
end

#local_admin?Boolean

Check if MAC address is a local address

Returns:

  • (Boolean)

    true if local, else false


105
106
107
108
# File 'lib/macker/address.rb', line 105

def local_admin?
  mask = 2 << (5 * 8)
  (mask & @val) != 0
end

#multicast?Boolean

Check if MAC address is a multicast address

Returns:

  • (Boolean)

    true if multicast, else false


92
93
94
95
# File 'lib/macker/address.rb', line 92

def multicast?
  mask = 1 << (5 * 8)
  (mask & @val) != 0
end

#nextAdress Also known as: succ

Get next MAC address from actual address

Returns:

  • (Adress)

    next MAC address


112
113
114
# File 'lib/macker/address.rb', line 112

def next
  Address.new((@val + 1) % 2**48)
end

#oui?Boolean

Check if MAC address is an OUI valid address

Returns:

  • (Boolean)

    true if valid, else false


68
69
70
# File 'lib/macker/address.rb', line 68

def oui?
  !@name.nil?
end

#prefixAdress

Get the prefix base16 MAC address

Returns:

  • (Adress)

    MAC prefix


119
120
121
# File 'lib/macker/address.rb', line 119

def prefix
  to_s('')[0..5]
end

#to_iInteger

Format MAC address to integer

Returns:

  • (Integer)

    integer MAC address


41
42
43
# File 'lib/macker/address.rb', line 41

def to_i
  @val
end

#to_s(sep = ':') ⇒ String

Format MAC address to string

Parameters:

  • sep (String) (defaults to: ':')

    separator, default is ':'

Returns:

  • (String)

    formatted MAC address


48
49
50
51
52
53
54
55
56
57
# 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

#unicast?Boolean

Check if MAC address is an unicast address

Returns:

  • (Boolean)

    true if unicast, else false


86
87
88
# File 'lib/macker/address.rb', line 86

def unicast?
  !multicast?
end

#valid?Boolean

Check if MAC address is a valid address

Returns:

  • (Boolean)

    true if valid, else false


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

def valid?
  @val.between?(0, 2**48 - 1)
end