Class: IPAdmin::EUI

Inherits:
Object
  • Object
show all
Defined in:
lib/eui.rb

Overview

Generic EUI address. By default, it will act as an EUI48 address. As a general rule, it is probably better to use the EUI48 and EUI64 classes.

Direct Known Subclasses

EUI48, EUI64

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EUI

  • Arguments:

    • EUI as a String, or a Hash with the following fields:

      - :EUI -- Extended Unique Identifier - String
      - :PackedEUI -- Integer representing an Extended Unique Identifier (optional)
      
  • Note:

    • PackedEUI takes precedence over EUI.

Examples:

addr = IPAdmin::EUI48.new('aa-bb-cc-dd-ee-ff')
addr = IPAdmin::EUI48.new('aa:bb:cc:dd:ee:ff')
addr = IPAdmin::EUI48.new('aabb.ccdd.eeff')
addr = IPAdmin::EUI64.new('aa-bb-cc-dd-ee-ff-00-01')


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/eui.rb', line 29

def initialize(options)
    if (options.kind_of? String)
        eui = options
    elsif (options.kind_of? Hash)
        if (options.has_key?(:PackedEUI))
            packed_eui = options[:PackedEUI]
        elsif(options.has_key?(:EUI))
            eui = options[:EUI]
        else
            raise ArgumentError, "Missing argument: [EUI|PackedEUI]."
        end
    else
        raise ArgumentError, "Expected Hash or String, but #{options.class} provided."
    end

    if (packed_eui)
        if (packed_eui.kind_of?(Integer))
            if (self.kind_of?(IPAdmin::EUI64))
                @oui = packed_eui >> 40
                @ei = packed_eui & 0xffffffffff
            else
                @oui = packed_eui >> 24
                @ei = packed_eui & 0xffffff
            end
        else
            raise ArgumentError, "Expected Integer, but #{eui.class} " +
                                 "provided for argument :PackedEUI."
        end

    elsif(eui)
        if (eui.kind_of?(String))
            # validate
            IPAdmin.validate_eui(:EUI => eui)

            # remove formatting characters
            eui.gsub!(/[\.\:\-]/, '')

            # split into oui & ei, pack, and store
            @oui = eui.slice!(0..5).to_i(16)
            @ei = eui.to_i(16)

        else
            raise ArgumentError, "Expected String, but #{eui.class} " +
                                 "provided for argument :EUI."
        end
    end
end

Instance Method Details

#address(options = nil) ⇒ Object

Returns EUI address.

  • Arguments:

    • Optional Hash with the following fields:

      - :Delimiter -- delimitation character. valid values are (-,:,and .) (optional)
      
  • Returns:

    • String

  • Notes:

    • The default address format is xxxx.xxxx.xxxx

Examples:

puts addr.address(:Delimiter => '.')   --> 'aabb.ccdd.eeff'


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/eui.rb', line 96

def address(options=nil)
    delimiter = '-'

    octets = []
    octets.concat(unpack_oui)
    octets.concat(unpack_ei)

    if (options)
        if (!options.kind_of? Hash)
            raise ArgumentError, "Expected Hash, but #{options.class} provided."
        end

        if (options.has_key?(:Delimiter))
            delimiter = options[:Delimiter]
            delimiter = '-' if (delimiter != '-' && delimiter != ':' && delimiter != '.' )
        end
    end

    if (delimiter == '-' || delimiter == ':')
                address = octets.join(delimiter)
    elsif (delimiter == '.')
            toggle = 0
            octets.each do |x|
                if (!address)
                    address = x
                    toggle = 1
                elsif (toggle == 0)
                    address = address  << '.' << x
                    toggle = 1
                else
                    address = address << x
                    toggle = 0
                end
            end 

    end

    return(address)
end

#ei(options = nil) ⇒ Object

Returns Extended Identifier portion of an EUI address (the vendor assigned ID).

  • Arguments:

    • Optional Hash with the following fields:

      - :Delimiter -- delimitation character. valid values are (-, and :) (optional)
      
  • Returns:

    • String

  • Notes:

    • The default address format is xx-xx-xx

Examples:

puts addr.ei(:Delimiter => '-')


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/eui.rb', line 155

def ei(options=nil)
    octets = unpack_ei()
    delimiter = '-'

    if (options)
        if (!options.kind_of? Hash)
            raise ArgumentError, "Expected Hash, but #{options.class} provided."
        end

        if (options.has_key?(:Delimiter))
            if (options[:Delimiter] == ':')
                delimiter = options[:Delimiter]
            end
        end
    end
    ei = octets.join(delimiter)

    return(ei)
end

Provide an IPv6 Link Local address based on the current EUI address.

  • Arguments:

    • Optional Hash with the following fields:

      - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
      - :Objectify -- if true, return CIDR objects (optional)
      
  • Returns:

    • CIDR address String or an IPAdmin::CIDR object

Examples:

puts addr.link_local()


192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/eui.rb', line 192

def link_local(options=nil)
    objectify = false
    short = false

    if (options)
        if (!options.kind_of? Hash)
            raise ArgumentError, "Expected Hash, but #{options.class} provided."
        end

        if (options.has_key?(:Objectify) && options[:Objectify] == true)
            objectify = true
        end

        if (options.has_key?(:Short) && options[:Short] == true)
            short = true
        end
    end

    if (self.kind_of?(IPAdmin::EUI64))
        link_local = @ei | (@oui << 40)
    else
        link_local = @ei | 0xfffe000000 | (@oui << 40)
    end
    link_local = link_local | (0xfe80 << 112)

    if (!objectify)
        link_local = IPAdmin.unpack_ip_addr(:Integer => link_local, :Version => 6)
        link_local = IPAdmin.shorten(link_local) if (short)
    else
        link_local = IPAdmin::CIDR.new(:PackedIP => link_local,  
                                       :Version => 6)
    end

    return(link_local)
end

#oui(options = nil) ⇒ Object

Returns Organizationally Unique Identifier portion of an EUI address (the vendor ID).

  • Arguments:

    • Optional Hash with the following fields:

      - :Delimiter -- delimitation character. valid values are (-, and :) (optional)
      
  • Returns:

    • String

  • Notes:

    • The default address format is xx-xx-xx

Examples:

puts addr.oui(:Delimiter => '-')


247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/eui.rb', line 247

def oui(options=nil)
    octets = unpack_oui()
    delimiter = '-'

    if (options)
        if (!options.kind_of? Hash)
            raise ArgumentError, "Expected Hash, but #{options.class} provided."
        end

        if (options.has_key?(:Delimiter))
            if (options[:Delimiter] == ':')
                delimiter = options[:Delimiter]
            end
        end
    end
    oui = octets.join(delimiter)

    return(oui)
end