ipadmin 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/eui.rb CHANGED
@@ -1,14 +1,10 @@
1
- =begin rdoc
2
- Copyright (c) 2006 Dustin Spinhirne -
3
- Licensed under the same terms as Ruby, No Warranty is provided.
4
- =end
5
-
6
-
7
1
  module IPAdmin
2
+
3
+ # Generic EUI address. By default, it will act as an EUI48 address.
4
+ # As a general rule, it is probably better to use the EUI48 and EUI64 classes.
8
5
  class EUI
9
6
 
10
7
  # instance variables
11
- # @type - eui-48 or eui-64
12
8
  # @oui - Organizationally Unique Identifier
13
9
  # @ei - Extention Identifier
14
10
 
@@ -17,82 +13,65 @@ class EUI
17
13
  #==============================================================================#
18
14
 
19
15
  # - Arguments:
20
- # * Hash with the following fields:
21
- # - :EUI -- Extended Unique Identifier - String (optional)
16
+ # * EUI as a string, or a Hash with the following fields:
17
+ # - :EUI -- Extended Unique Identifier - String
22
18
  # - :PackedEUI -- Integer representing an Extended Unique Identifier (optional)
23
- # - :Length -- bit length of PackedEUI - Integer (optional)
24
19
  #
25
20
  # - Note:
26
- # * At a minimum, EUI or PackedEUI must be provided.
27
21
  # * PackedEUI takes precedence over EUI.
28
- # * Length is only needed when using PackedEUI
29
22
  #
30
- # Example:
31
- # addr = IPAdmin::EUI.new(:EUI => 'aa-bb-cc-dd-ee-ff')
32
- # addr = IPAdmin::EUI.new(:EUI => 'aa:bb:cc:dd:ee:ff')
33
- # addr = IPAdmin::EUI.new(:EUI => 'aabb.ccdd.eeff')
34
- # addr = IPAdmin::EUI.new(:EUI => 'aa-bb-cc-dd-ee-ff-00-01')
23
+ # Examples:
24
+ # addr = IPAdmin::EUI48.new('aa-bb-cc-dd-ee-ff')
25
+ # addr = IPAdmin::EUI48.new('aa:bb:cc:dd:ee:ff')
26
+ # addr = IPAdmin::EUI48.new('aabb.ccdd.eeff')
27
+ # addr = IPAdmin::EUI64.new('aa-bb-cc-dd-ee-ff-00-01')
35
28
  #
36
29
  def initialize(options)
37
- if (!options.kind_of? Hash)
38
- raise ArgumentError, "Expected Hash, but #{options.class} provided."
39
- end
40
-
41
- if (options.has_key?(:PackedEUI))
42
- packed_eui = options[:PackedEUI]
43
-
44
- if (options.has_key?(:Length) && options[:Length].kind_of?(Integer))
45
- @type = options[:Length]
46
- @type = 48 if (@type != 48 && @type != 64)
30
+ if (options.kind_of? String)
31
+ eui = options
32
+ elsif (options.kind_of? Hash)
33
+ if (options.has_key?(:PackedEUI))
34
+ packed_eui = options[:PackedEUI]
35
+ elsif(options.has_key?(:EUI))
36
+ eui = options[:EUI]
47
37
  else
48
- @type = 48
38
+ raise ArgumentError, "Missing argument: [EUI|PackedEUI]."
49
39
  end
50
-
40
+ else
41
+ raise ArgumentError, "Expected Hash or String, but #{options.class} provided."
42
+ end
43
+
44
+ if (packed_eui)
51
45
  if (packed_eui.kind_of?(Integer))
52
- if (@type == 48)
53
- @oui = packed_eui >> 24
54
- @ei = packed_eui & 0xffffff
55
- else
46
+ if (self.kind_of?(IPAdmin::EUI64))
56
47
  @oui = packed_eui >> 40
57
48
  @ei = packed_eui & 0xffffffffff
49
+ else
50
+ @oui = packed_eui >> 24
51
+ @ei = packed_eui & 0xffffff
58
52
  end
59
53
  else
60
54
  raise ArgumentError, "Expected Integer, but #{eui.class} " +
61
55
  "provided for argument :PackedEUI."
62
56
  end
63
-
64
- elsif(options.has_key?(:EUI))
65
- eui = options[:EUI]
66
-
57
+
58
+ elsif(eui)
67
59
  if (eui.kind_of?(String))
68
60
  # validate
69
61
  IPAdmin.validate_eui(:EUI => eui)
70
-
62
+
71
63
  # remove formatting characters
72
- eui.gsub!(/[\.\:\-]/, '')
73
-
74
- # check if eui-48 or eui-64
75
- if (eui.length == 12)
76
- @type = 48
77
- elsif (eui.length == 16)
78
- @type = 64
79
- else
80
- raise "#{eui} is invalid (address is neither EUI-48 nor EUI-64)."
81
- end
82
-
64
+ eui.gsub!(/[\.\:\-]/, '')
65
+
83
66
  # split into oui & ei, pack, and store
84
67
  @oui = eui.slice!(0..5).to_i(16)
85
68
  @ei = eui.to_i(16)
86
-
69
+
87
70
  else
88
71
  raise ArgumentError, "Expected String, but #{eui.class} " +
89
72
  "provided for argument :EUI."
90
73
  end
91
-
92
- else
93
- raise ArgumentError, "Missing argument: [EUI|PackedEUI]."
94
74
  end
95
-
96
75
  end
97
76
 
98
77
  #======================================#
@@ -113,30 +92,33 @@ class EUI
113
92
  # - Returns:
114
93
  # * String
115
94
  #
116
- # Example:
95
+ # - Notes:
96
+ # * The default address format is xxxx.xxxx.xxxx
97
+ #
98
+ # Examples:
117
99
  # puts addr.address(:Delimiter => '.') --> 'aabb.ccdd.eeff'
118
100
  #
119
101
  def address(options=nil)
120
102
  delimiter = '-'
121
-
103
+
122
104
  octets = []
123
105
  octets.concat(unpack_oui)
124
106
  octets.concat(unpack_ei)
125
-
107
+
126
108
  if (options)
127
109
  if (!options.kind_of? Hash)
128
110
  raise ArgumentError, "Expected Hash, but #{options.class} provided."
129
111
  end
130
-
112
+
131
113
  if (options.has_key?(:Delimiter))
132
114
  delimiter = options[:Delimiter]
133
115
  delimiter = '-' if (delimiter != '-' && delimiter != ':' && delimiter != '.' )
134
116
  end
135
117
  end
136
-
118
+
137
119
  if (delimiter == '-' || delimiter == ':')
138
120
  address = octets.join(delimiter)
139
- elsif (delimiter == '.')
121
+ elsif (delimiter == '.')
140
122
  toggle = 0
141
123
  octets.each do |x|
142
124
  if (!address)
@@ -152,8 +134,8 @@ class EUI
152
134
  end
153
135
 
154
136
  end
155
-
156
- return(address)
137
+
138
+ return(address)
157
139
  end
158
140
 
159
141
  #======================================#
@@ -174,18 +156,21 @@ class EUI
174
156
  # - Returns:
175
157
  # * String
176
158
  #
177
- # Example:
178
- # puts addr.ei(:Delimiter => '-') --> 'aa-bb-cc'
159
+ # - Notes:
160
+ # * The default address format is xx-xx-xx
161
+ #
162
+ # Examples:
163
+ # puts addr.ei(:Delimiter => '-')
179
164
  #
180
165
  def ei(options=nil)
181
166
  octets = unpack_ei()
182
167
  delimiter = '-'
183
-
168
+
184
169
  if (options)
185
170
  if (!options.kind_of? Hash)
186
171
  raise ArgumentError, "Expected Hash, but #{options.class} provided."
187
172
  end
188
-
173
+
189
174
  if (options.has_key?(:Delimiter))
190
175
  if (options[:Delimiter] == ':')
191
176
  delimiter = options[:Delimiter]
@@ -193,8 +178,8 @@ class EUI
193
178
  end
194
179
  end
195
180
  ei = octets.join(delimiter)
196
-
197
- return(ei)
181
+
182
+ return(ei)
198
183
  end
199
184
 
200
185
  #======================================#
@@ -214,36 +199,36 @@ class EUI
214
199
  # - :Objectify -- if true, return CIDR objects (optional)
215
200
  #
216
201
  # - Returns:
217
- # * String
202
+ # * CIDR address String or an IPAdmin::CIDR object
218
203
  #
219
- # Example:
220
- # puts addr.link_local() --> fe80:0000:0000:0000:aabb:ccff:fedd:eeff
204
+ # Examples:
205
+ # puts addr.link_local()
221
206
  #
222
- def link_local(options=nil)
207
+ def link_local(options=nil)
223
208
  objectify = false
224
209
  short = false
225
-
210
+
226
211
  if (options)
227
212
  if (!options.kind_of? Hash)
228
213
  raise ArgumentError, "Expected Hash, but #{options.class} provided."
229
214
  end
230
-
215
+
231
216
  if (options.has_key?(:Objectify) && options[:Objectify] == true)
232
217
  objectify = true
233
218
  end
234
-
219
+
235
220
  if (options.has_key?(:Short) && options[:Short] == true)
236
221
  short = true
237
222
  end
238
223
  end
239
-
240
- if (@type == 48)
241
- link_local = @ei | 0xfffe000000 | (@oui << 40)
242
- else
224
+
225
+ if (self.kind_of?(IPAdmin::EUI64))
243
226
  link_local = @ei | (@oui << 40)
227
+ else
228
+ link_local = @ei | 0xfffe000000 | (@oui << 40)
244
229
  end
245
230
  link_local = link_local | (0xfe80 << 112)
246
-
231
+
247
232
  if (!objectify)
248
233
  link_local = IPAdmin.unpack_ip_addr(:Integer => link_local, :Version => 6)
249
234
  link_local = IPAdmin.shorten(link_local) if (short)
@@ -251,7 +236,7 @@ class EUI
251
236
  link_local = IPAdmin::CIDR.new(:PackedIP => link_local,
252
237
  :Version => 6)
253
238
  end
254
-
239
+
255
240
  return(link_local)
256
241
  end
257
242
 
@@ -273,18 +258,21 @@ class EUI
273
258
  # - Returns:
274
259
  # * String
275
260
  #
276
- # Example:
277
- # puts addr.oui(:Delimiter => '-') --> 'dd-ee-ff'
261
+ # - Notes:
262
+ # * The default address format is xx-xx-xx
263
+ #
264
+ # Examples:
265
+ # puts addr.oui(:Delimiter => '-')
278
266
  #
279
267
  def oui(options=nil)
280
268
  octets = unpack_oui()
281
269
  delimiter = '-'
282
-
270
+
283
271
  if (options)
284
272
  if (!options.kind_of? Hash)
285
273
  raise ArgumentError, "Expected Hash, but #{options.class} provided."
286
274
  end
287
-
275
+
288
276
  if (options.has_key?(:Delimiter))
289
277
  if (options[:Delimiter] == ':')
290
278
  delimiter = options[:Delimiter]
@@ -292,36 +280,8 @@ class EUI
292
280
  end
293
281
  end
294
282
  oui = octets.join(delimiter)
295
-
296
- return(oui)
297
- end
298
-
299
- #======================================#
300
- #
301
- #======================================#
302
-
303
-
304
- #==============================================================================#
305
- # type()
306
- #==============================================================================#
307
283
 
308
- # Provide address type (EUI-48 or EUI-64).
309
- #
310
- # - Arguments:
311
- # * none
312
- #
313
- # - Returns:
314
- # * String
315
- #
316
- # Example:
317
- # puts addr.type --> EUI-48
318
- #
319
- def type()
320
- if (@type == 48)
321
- return('EUI-48')
322
- else
323
- return('EUI-64')
324
- end
284
+ return(oui)
325
285
  end
326
286
 
327
287
  #======================================#
@@ -340,7 +300,14 @@ private
340
300
  def unpack_ei()
341
301
  hex = @ei
342
302
  octets = []
343
- loop_count = (@type - 24)/8
303
+
304
+ if (self.kind_of?(IPAdmin::EUI64))
305
+ length = 64
306
+ else
307
+ length = 48
308
+ end
309
+
310
+ loop_count = (length - 24)/8
344
311
  loop_count.times do
345
312
  octet = (hex & 0xff).to_s(16)
346
313
  octet = '0' << octet if (octet.length != 2)
@@ -377,5 +344,15 @@ private
377
344
 
378
345
  end
379
346
 
347
+ # EUI-48 Address - Inherits all methods from IPAdmin::EUI.
348
+ # Addresses of this type have a 24-bit OUI and a 24-bit EI.
349
+ class EUI48 < EUI
350
+ end
351
+
352
+ # EUI-64 Address - Inherits all methods from IPAdmin::EUI.
353
+ # Addresses of this type have a 24-bit OUI and a 40-bit EI.
354
+ class EUI64 < EUI
355
+ end
356
+
380
357
  end # module IPAdmin
381
358
  __END__