Class: IPAdmin::EUI

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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ EUI

  • Arguments:

    • Hash with the following fields:

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

    • At a minimum, EUI or PackedEUI must be provided.

    • PackedEUI takes precedence over EUI.

    • Length is only needed when using PackedEUI

Example:

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


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/eui.rb', line 36

def initialize(options)
    if (!options.kind_of? Hash)
        raise ArgumentError, "Expected Hash, but #{options.class} provided."
    end
    
    if (options.has_key?(:PackedEUI))
        packed_eui = options[:PackedEUI]
        
        if (options.has_key?(:Length) && options[:Length].kind_of?(Integer))
            @type = options[:Length]
            @type = 48 if (@type != 48 && @type != 64)
        else
            @type = 48
        end
    
        if (packed_eui.kind_of?(Integer))
            if (@type == 48)
                @oui = packed_eui >> 24
                @ei = packed_eui & 0xffffff
            else
                @oui = packed_eui >> 40
                @ei = packed_eui & 0xffffffffff
            end
        else
            raise ArgumentError, "Expected Integer, but #{eui.class} " +
                                 "provided for argument :PackedEUI."
        end
        
    elsif(options.has_key?(:EUI))
        eui = options[:EUI]
        
        if (eui.kind_of?(String))
            # validate
            IPAdmin.validate_eui(:EUI => eui)
        
            # remove formatting characters
            eui.gsub!(/[\.\:\-]/, '')           
        
            # check if eui-48 or eui-64
            if (eui.length == 12)
                @type = 48
            elsif (eui.length == 16)
                @type = 64
            else
                raise "#{eui} is invalid (address is neither EUI-48 nor EUI-64)."
            end
        
            # 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
        
    else
        raise ArgumentError, "Missing argument: [EUI|PackedEUI]."
    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

Example:

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


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/eui.rb', line 119

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

Example:

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


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/eui.rb', line 180

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:

    • String

Example:

puts addr.link_local() --> fe80:0000:0000:0000:aabb:ccff:fedd:eeff


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/eui.rb', line 222

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 (@type == 48)
        link_local = @ei | 0xfffe000000 | (@oui << 40)
    else
        link_local = @ei | (@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

Example:

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


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/eui.rb', line 279

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

#typeObject

Provide address type (EUI-48 or EUI-64).

  • Arguments:

    • none

  • Returns:

    • String

Example:

puts addr.type   --> EUI-48


319
320
321
322
323
324
325
# File 'lib/eui.rb', line 319

def type()
    if (@type == 48)
        return('EUI-48')
    else
        return('EUI-64')
    end        
end