netaddr 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of netaddr might be problematic. Click here for more details.

@@ -29,21 +29,21 @@ module_function :cidr_build
29
29
  #
30
30
  def cidr_compare(cidr1,cidr2)
31
31
  comparasin = nil
32
- if ( (cidr1.packed_network == cidr2.packed_network))
32
+ if ( cidr1.to_i(:network) == cidr2.to_i(:network) )
33
33
  # same network, check netmask
34
- if (cidr1.packed_netmask == cidr2.packed_netmask)
34
+ if (cidr1.to_i(:netmask) == cidr2.to_i(:netmask) )
35
35
  comparasin = 0
36
- elsif(cidr1.packed_netmask < cidr2.packed_netmask)
36
+ elsif(cidr1.to_i(:netmask) < cidr2.to_i(:netmask))
37
37
  comparasin = 1
38
- elsif(cidr1.packed_netmask > cidr2.packed_netmask)
38
+ elsif(cidr1.to_i(:netmask) > cidr2.to_i(:netmask))
39
39
  comparasin = -1
40
40
  end
41
41
 
42
- elsif( (cidr2.packed_network | cidr1.packed_hostmask) == (cidr1.packed_network | cidr1.packed_hostmask) )
42
+ elsif( (cidr2.to_i(:network) | cidr1.to_i(:hostmask)) == (cidr1.to_i(:network) | cidr1.to_i(:hostmask)) )
43
43
  # cidr1 contains cidr2
44
44
  comparasin = 1
45
45
 
46
- elsif( (cidr2.packed_network | cidr2.packed_hostmask) == (cidr1.packed_network | cidr2.packed_hostmask) )
46
+ elsif( (cidr2.to_i(:network) | cidr2.to_i(:hostmask)) == (cidr1.to_i(:network) | cidr2.to_i(:hostmask)) )
47
47
  # cidr2 contains cidr1
48
48
  comparasin = -1
49
49
  end
@@ -64,12 +64,12 @@ module_function :cidr_compare
64
64
  #
65
65
  def cidr_gt_lt(cidr1,cidr2)
66
66
  gt_lt = 1
67
- if(cidr1.packed_network < cidr2.packed_network)
67
+ if(cidr1.to_i(:network) < cidr2.to_i(:network))
68
68
  gt_lt = -1
69
- elsif (cidr1.packed_network == cidr2.packed_network)
70
- if (cidr1.packed_netmask < cidr2.packed_netmask)
69
+ elsif (cidr1.to_i(:network) == cidr2.to_i(:network))
70
+ if (cidr1.to_i(:netmask) < cidr2.to_i(:netmask))
71
71
  gt_lt = -1
72
- elsif (cidr1.packed_netmask == cidr2.packed_netmask)
72
+ elsif (cidr1.to_i(:netmask) == cidr2.to_i(:netmask))
73
73
  gt_lt = 0
74
74
  end
75
75
  end
@@ -88,15 +88,14 @@ module_function :cidr_gt_lt
88
88
  def cidr_fill_in(supernet,list)
89
89
  # sort our cidr's and see what is missing
90
90
  complete_list = []
91
- expected = supernet.packed_network
91
+ expected = supernet.to_i(:network)
92
92
  all_f = supernet.all_f
93
- version = supernet.version
94
93
 
95
94
  NetAddr.cidr_sort(list).each do |cidr|
96
- network = cidr.packed_network
97
- bitstep = (all_f + 1) - cidr.packed_netmask
95
+ network = cidr.to_i(:network)
96
+ bitstep = (all_f + 1) - cidr.to_i(:netmask)
98
97
 
99
- if (network > expected)
98
+ if (network > expected) # missing space at beginning of supernet, so fill in the hole
100
99
  num_ips_missing = network - expected
101
100
  sub_list = cidr_make_subnets_from_base_and_ip_count(supernet,expected,num_ips_missing)
102
101
  complete_list.concat(sub_list)
@@ -104,13 +103,13 @@ def cidr_fill_in(supernet,list)
104
103
  next
105
104
  end
106
105
 
107
- complete_list.push(NetAddr.cidr_build(version,network,cidr.packed_netmask))
106
+ complete_list.push(cidr)
108
107
  expected = network + bitstep
109
108
  end
110
109
 
111
- # if expected is not the next subnet, then we're missing subnets
110
+ # if expected is not the next subnet, then we're missing subnets
112
111
  # at the end of the cidr
113
- next_sub = supernet.next_subnet(:Objectify => true).packed_network
112
+ next_sub = supernet.next_subnet(:Objectify => true).to_i(:network)
114
113
  if (expected != next_sub)
115
114
  num_ips_missing = next_sub - expected
116
115
  sub_list = cidr_make_subnets_from_base_and_ip_count(supernet,expected,num_ips_missing)
@@ -167,7 +166,8 @@ module_function :cidr_find_in_list
167
166
  # Make CIDR addresses from a base addr and an number of ip's to encapsulate.
168
167
  #
169
168
  #===Arguments:
170
- # * base ip as packed integer
169
+ # * cidr
170
+ # * base ip as integer
171
171
  # * number of ip's required
172
172
  #
173
173
  #===Returns:
@@ -259,7 +259,7 @@ def cidr_summarize(subnet_list)
259
259
  summarized_list = []
260
260
  until (sorted_list.length == 0)
261
261
  cidr = sorted_list.shift
262
- network, netmask = cidr.packed_network, cidr.packed_netmask
262
+ network, netmask = cidr.to_i(:network), cidr.to_i(:netmask)
263
263
  supermask = (netmask << 1) & all_f
264
264
  supernet = supermask & network
265
265
 
@@ -268,7 +268,7 @@ def cidr_summarize(subnet_list)
268
268
  bitstep = (all_f + 1) - netmask
269
269
  expected = network + bitstep
270
270
  next_cidr = sorted_list.shift
271
- next_network, next_netmask = next_cidr.packed_network, next_cidr.packed_netmask
271
+ next_network, next_netmask = next_cidr.to_i(:network), next_cidr.to_i(:netmask)
272
272
 
273
273
  if ( (next_network == expected) && (next_netmask == netmask) )
274
274
  # we do indeed have the upper half. store new supernet.
data/lib/eui.rb CHANGED
@@ -1,23 +1,23 @@
1
1
  =begin rdoc
2
- Copyleft (c) 2006 Dustin Spinhirne (www.spinhirne.com)
3
-
2
+ Copyleft (c) 2006 Dustin Spinhirne
3
+
4
4
  Licensed under the same terms as Ruby, No Warranty is provided.
5
5
  =end
6
6
 
7
7
  module NetAddr
8
8
 
9
9
  #=EUI - Extended Unique Identifier
10
- #
10
+ #
11
11
  #A class & series of methods for creating and manipulating Extended Unique Identifier
12
12
  #(EUI) addresses. Two types of address formats are supported EUI-48 and EUI-64. The
13
13
  #most common use for this class will be to manipulate MAC addresses (which are essentially
14
- #a type of EUI-48 address).
15
- #
14
+ #a type of EUI-48 address).
15
+ #
16
16
  #EUI addresses are separated into two parts, the
17
17
  #Organizationally Unique Identifier (OUI) and the Extended Identifier (EI). The OUI
18
18
  #is assigned by the IEEE and is used to identify a particular hardware manufacturer.
19
19
  #The EI is assigned by the hardware manufacturer as a per device unique address.
20
- #
20
+ #
21
21
  #Probably the most useful feature of this class, and thus the reason it was created,
22
22
  #is to help automate certain address assignments within IP. For example, IPv6
23
23
  #Link Local addresses use MAC addresses for IP auto-assignment and multicast MAC addresses
@@ -27,6 +27,50 @@ class EUI
27
27
 
28
28
  private_class_method :new
29
29
 
30
+ #==============================================================================#
31
+ # initialize()
32
+ #==============================================================================#
33
+
34
+ #===Synopsis
35
+ # This method performs absolutely no error checking, and is meant to be used only by
36
+ # other internal methods for the sake of the speedier creation of EUI objects.
37
+ # Please consider using #create unless you know what you are doing with 100% certainty.
38
+ #
39
+ # Example:
40
+ # NetAddr::EUI48.new('aabbccddeeff')
41
+ #
42
+ #===Arguments:
43
+ #* EUI as a String or Integer. Strings should contain no formatting characters.
44
+ #
45
+ def initialize(eui)
46
+
47
+ if (eui.kind_of?(Integer))
48
+ @eui_i = eui
49
+ @eui = eui.to_s(16)
50
+ if ( self.kind_of?(NetAddr::EUI48) )
51
+ @eui = '0' * (12 - @eui.length) << @eui if (@eui.length < 12)
52
+ else
53
+ @eui = '0' * (16 - @eui.length) << @eui if (@eui.length < 16)
54
+ end
55
+
56
+ elsif(eui.kind_of?(String))
57
+ @eui = eui
58
+ @eui_i = eui.to_i(16)
59
+ else
60
+ raise ArgumentError, "Expected String or Integer, but #{eui.class} provided."
61
+ end
62
+
63
+ # set ei & oui
64
+ if ( self.kind_of?(NetAddr::EUI48) )
65
+ @ei = @eui.slice(6..11)
66
+ else
67
+ @ei = @eui.slice(6..15)
68
+ end
69
+
70
+ @oui = @eui.slice(0..5)
71
+
72
+ end
73
+
30
74
  #==============================================================================#
31
75
  # create()
32
76
  #==============================================================================#
@@ -34,10 +78,11 @@ private_class_method :new
34
78
  #===Synopsis
35
79
  #Create a new EUI48 or EUI64 object.
36
80
  #
37
- # addr = NetAddr::EUI.new('aa-bb-cc-dd-ee-ff')
38
- # addr = NetAddr::EUI.new('aa:bb:cc:dd:ee:ff')
39
- # addr = NetAddr::EUI.new('aabb.ccdd.eeff')
40
- # addr = NetAddr::EUI.new('aa-bb-cc-dd-ee-ff-00-01')
81
+ # Example:
82
+ # addr = NetAddr::EUI.create('aa-bb-cc-dd-ee-ff')
83
+ # addr = NetAddr::EUI.create('aa:bb:cc:dd:ee:ff')
84
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
85
+ # addr = NetAddr::EUI.create('aa-bb-cc-dd-ee-ff-00-01')
41
86
  #
42
87
  #===Arguments
43
88
  #* eui = EUI as a String
@@ -56,11 +101,10 @@ private_class_method :new
56
101
  # remove formatting characters
57
102
  eui.gsub!(/[\.\:\-]/, '')
58
103
 
59
- # split into oui & ei, pack, and store
60
104
  if (eui.length == 12)
61
- eui = NetAddr::EUI48.new(eui.to_i(16))
105
+ eui = NetAddr::EUI48.new(eui)
62
106
  else
63
- eui = NetAddr::EUI64.new(eui.to_i(16))
107
+ eui = NetAddr::EUI64.new(eui)
64
108
  end
65
109
 
66
110
  return(eui)
@@ -73,11 +117,14 @@ private_class_method :new
73
117
  #===Synopsis
74
118
  # Returns EUI address. The default address format is xxxx.xxxx.xxxx
75
119
  #
76
- # puts addr.address(:Delimiter => '.') --> 'aabb.ccdd.eeff'
120
+ # Example:
121
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
122
+ # addr.address(:Delimiter => '-') => "aa-bb-cc-dd-ee-ff"
123
+ # addr.address(:Delimiter => ':') => "aa:bb:cc:dd:ee:ff"
77
124
  #
78
125
  #===Arguments:
79
126
  #* options = Hash with the following fields:
80
- # :Delimiter -- delimitation character. valid values are (-,:,and .)
127
+ # :Delimiter -- delimitation character. valid values are (- : .)
81
128
  #
82
129
  #===Returns:
83
130
  #* String
@@ -86,10 +133,6 @@ private_class_method :new
86
133
  known_args = [:Delimiter]
87
134
  delimiter = '-'
88
135
 
89
- octets = []
90
- octets.concat(unpack_oui)
91
- octets.concat(unpack_ei)
92
-
93
136
  if (options)
94
137
  if (!options.kind_of? Hash)
95
138
  raise ArgumentError, "Expected Hash, but #{options.class} provided."
@@ -98,30 +141,29 @@ private_class_method :new
98
141
 
99
142
  if (options.has_key?(:Delimiter))
100
143
  delimiter = options[:Delimiter]
101
- delimiter = '-' if (delimiter != '-' && delimiter != ':' && delimiter != '.' )
144
+ delimiter = '-' if (delimiter != ':' && delimiter != '.')
102
145
  end
103
146
  end
104
147
 
105
148
  if (delimiter == '-' || delimiter == ':')
106
- address = octets.join(delimiter)
149
+ addr = octets.join(delimiter)
107
150
  elsif (delimiter == '.')
108
151
  toggle = 0
109
152
  octets.each do |x|
110
- if (!address)
111
- address = x
153
+ if (!addr)
154
+ addr = x
112
155
  toggle = 1
113
156
  elsif (toggle == 0)
114
- address = address << '.' << x
157
+ addr = addr << '.' << x
115
158
  toggle = 1
116
159
  else
117
- address = address << x
160
+ addr = addr << x
118
161
  toggle = 0
119
162
  end
120
- end
121
-
163
+ end
122
164
  end
123
165
 
124
- return(address)
166
+ return(addr)
125
167
  end
126
168
 
127
169
  #==============================================================================#
@@ -132,7 +174,9 @@ private_class_method :new
132
174
  #Returns Extended Identifier portion of an EUI address (the vendor assigned ID).
133
175
  #The default address format is xx-xx-xx
134
176
  #
135
- # puts addr.ei(:Delimiter => '-')
177
+ # Example:
178
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
179
+ # addr.ei(:Delimiter => '-') => "dd-ee-ff"
136
180
  #
137
181
  #===Arguments:
138
182
  #* options = Hash with the following fields:
@@ -143,7 +187,6 @@ private_class_method :new
143
187
  #
144
188
  def ei(options=nil)
145
189
  known_args = [:Delimiter]
146
- octets = unpack_ei()
147
190
  delimiter = '-'
148
191
 
149
192
  if (options)
@@ -158,7 +201,12 @@ private_class_method :new
158
201
  end
159
202
  end
160
203
  end
161
- ei = octets.join(delimiter)
204
+
205
+ if ( self.kind_of?(NetAddr::EUI48) )
206
+ ei = octets[3..5].join(delimiter)
207
+ else
208
+ ei = octets[3..7].join(delimiter)
209
+ end
162
210
 
163
211
  return(ei)
164
212
  end
@@ -170,7 +218,9 @@ private_class_method :new
170
218
  #===Synopsis
171
219
  # Provide an IPv6 Link Local address based on the current EUI address.
172
220
  #
173
- # puts addr.link_local()
221
+ # Example:
222
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
223
+ # addr.link_local() => "fe80:0000:0000:0000:aabb:ccff:fedd:eeff"
174
224
  #
175
225
  #===Arguments:
176
226
  #* options = Hash with the following fields:
@@ -201,17 +251,16 @@ private_class_method :new
201
251
  end
202
252
 
203
253
  if (self.kind_of?(NetAddr::EUI64))
204
- link_local = @ei | (@oui << 40)
254
+ link_local = 'fe80000000000000' << @eui
205
255
  else
206
- link_local = @ei | 0xfffe000000 | (@oui << 40)
256
+ link_local = 'fe80000000000000' << @oui << 'fffe' << @ei
207
257
  end
208
- link_local = link_local | (0xfe80 << 112)
209
258
 
210
259
  if (!objectify)
211
- link_local = NetAddr.unpack_ip_addr(link_local, :Version => 6)
260
+ link_local = NetAddr.i_to_ip(link_local.to_i(16), :Version => 6)
212
261
  link_local = NetAddr.shorten(link_local) if (short)
213
262
  else
214
- link_local = NetAddr::CIDR.create(link_local, :Version => 6)
263
+ link_local = NetAddr::CIDRv6.new(link_local.to_i(16))
215
264
  end
216
265
 
217
266
  return(link_local)
@@ -225,7 +274,9 @@ private_class_method :new
225
274
  #Returns Organizationally Unique Identifier portion of an EUI address (the vendor ID).
226
275
  #The default address format is xx-xx-xx.
227
276
  #
228
- # puts addr.oui(:Delimiter => '-')
277
+ # Example:
278
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
279
+ # addr.oui(:Delimiter => '-') => "aa-bb-cc"
229
280
  #
230
281
  #===Arguments:
231
282
  #* options = Hash with the following fields:
@@ -236,7 +287,6 @@ private_class_method :new
236
287
  #
237
288
  def oui(options=nil)
238
289
  known_args = [:Delimiter]
239
- octets = unpack_oui()
240
290
  delimiter = '-'
241
291
 
242
292
  if (options)
@@ -251,140 +301,119 @@ private_class_method :new
251
301
  end
252
302
  end
253
303
  end
254
- oui = octets.join(delimiter)
304
+ oui = octets[0..2].join(delimiter)
255
305
 
256
306
  return(oui)
257
307
  end
258
308
 
259
-
260
- # PRIVATE METHODS
261
- private
262
-
263
- #==============================================================================#
264
- # unpack_ei()
265
- #==============================================================================#
266
-
267
- def unpack_ei()
268
- hex = @ei
269
- octets = []
270
-
271
- if (self.kind_of?(NetAddr::EUI64))
272
- length = 64
273
- else
274
- length = 48
275
- end
276
-
277
- loop_count = (length - 24)/8
278
- loop_count.times do
279
- octet = (hex & 0xff).to_s(16)
280
- octet = '0' << octet if (octet.length != 2)
281
- octets.unshift(octet)
282
- hex = hex >> 8
283
- end
284
- return(octets)
285
- end
286
-
287
309
  #==============================================================================#
288
- # unpack_oui()
310
+ # to_i()
289
311
  #==============================================================================#
290
312
 
291
- def unpack_oui()
292
- hex = @oui
293
- octets = []
294
- 3.times do
295
- octet = (hex & 0xff).to_s(16)
296
- octet = '0' << octet if (octet.length != 2)
297
- octets.unshift(octet)
298
- hex = hex >> 8
299
- end
300
- return(octets)
313
+ #===Synopsis
314
+ #Returns the EUI as an Integer.
315
+ #
316
+ # Example:
317
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
318
+ # addr.to_i => 187723572702975
319
+ #
320
+ #===Arguments:
321
+ #* none
322
+ #
323
+ #===Returns:
324
+ #* Integer
325
+ #
326
+ def to_i()
327
+ return(@eui_i)
301
328
  end
302
329
 
303
- end # class EUI
304
-
305
- # EUI-48 Address - Inherits all methods from NetAddr::EUI.
306
- # Addresses of this class have a 24-bit OUI and a 24-bit EI.
307
- class EUI48 < EUI
308
-
309
- public_class_method :new
310
-
311
330
  #==============================================================================#
312
- # initialize()
331
+ # to_s()
313
332
  #==============================================================================#
314
333
 
315
334
  #===Synopsis
316
- #Return an EUI48 object. PackedEUI takes precedence over EUI.
335
+ #Returns the EUI as an unformatted String.
317
336
  #
318
- # addr = NetAddr::EUI48.new('aa-bb-cc-dd-ee-ff')
319
- # addr = NetAddr::EUI48.new('aa:bb:cc:dd:ee:ff')
320
- # addr = NetAddr::EUI48.new('aabb.ccdd.eeff')
337
+ # Example:
338
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
339
+ # addr.to_s => "aabbccddeeff"
321
340
  #
322
341
  #===Arguments:
323
- #* EUI as a String or Integer
342
+ #* none
324
343
  #
325
- def initialize(eui)
326
-
327
- if (eui.kind_of?(Integer))
328
- @oui = eui >> 24
329
- @ei = eui & 0xffffff
344
+ #===Returns:
345
+ #* String
346
+ #
347
+ def to_s()
348
+ return(@eui)
349
+ end
330
350
 
331
- elsif(eui.kind_of?(String))
332
- # validate
333
- NetAddr.validate_eui(eui)
351
+ private
334
352
 
335
- # remove formatting characters
336
- eui.gsub!(/[\.\:\-]/, '')
353
+ #==============================================================================#
354
+ # octets()
355
+ #==============================================================================#
337
356
 
338
- # split into oui & ei, pack, and store
339
- @oui = eui.slice!(0..5).to_i(16)
340
- @ei = eui.to_i(16)
341
- else
342
- raise ArgumentError, "Expected String or Integer, but #{eui.class} provided."
357
+ #Returns array with each element representing a single octet of the eui.
358
+ #
359
+ def octets()
360
+ return(@octets) if (@octets)
361
+
362
+ @octets = []
363
+ str = ''
364
+ @eui.each_byte do |chr|
365
+ str = str << chr
366
+ if (str.length == 2)
367
+ @octets.push(str)
368
+ str = ''
369
+ end
343
370
  end
371
+
372
+ return(@octets)
344
373
  end
345
374
 
346
375
  end
347
376
 
348
- # EUI-64 Address - Inherits all methods from NetAddr::EUI.
349
- # Addresses of this class have a 24-bit OUI and a 40-bit EI.
350
- class EUI64 < EUI
351
377
 
352
- public_class_method :new
378
+
379
+ # EUI-48 Address - Inherits all methods from NetAddr::EUI.
380
+ # Addresses of this class have a 24-bit OUI and a 24-bit EI.
381
+ class EUI48 < EUI
382
+
383
+ public_class_method :new
353
384
 
354
385
  #==============================================================================#
355
- # initialize()
386
+ # to_eui64()
356
387
  #==============================================================================#
357
388
 
358
389
  #===Synopsis
359
- #Return an EUI object. PackedEUI takes precedence over EUI.
390
+ #Return an EUI64 address based on the current EUI48 address.
360
391
  #
361
- # addr = NetAddr::EUI64.new('aa-bb-cc-dd-ee-ff-00-01')
392
+ # Example:
393
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
394
+ # addr.to_eui64 => NetAddr::EUI64
362
395
  #
363
396
  #===Arguments:
364
- #* eui = EUI as a String or Integer
397
+ #* none
365
398
  #
366
- def initialize(eui)
367
-
368
- if (eui.kind_of?(Integer))
369
- @oui = eui >> 40
370
- @ei = eui & 0xffffffffffd
399
+ #===Returns:
400
+ #* NetAddr::EUI64 object
401
+ #
402
+ def to_eui64()
403
+ eui = @oui << 'fffe' << @ei
404
+ return( NetAddr::EUI64.new(eui.to_i(16)) )
405
+ end
371
406
 
372
- elsif(eui.kind_of?(String))
373
- # validate
374
- NetAddr.validate_eui(eui)
407
+ end
375
408
 
376
- # remove formatting characters
377
- eui.gsub!(/[\.\:\-]/, '')
378
409
 
379
- # split into oui & ei, pack, and store
380
- @oui = eui.slice!(0..5).to_i(16)
381
- @ei = eui.to_i(16)
382
- else
383
- raise ArgumentError, "Expected String or Integer, but #{eui.class} provided."
384
- end
385
- end
386
410
 
411
+ # EUI-64 Address - Inherits all methods from NetAddr::EUI.
412
+ # Addresses of this class have a 24-bit OUI and a 40-bit EI.
413
+ class EUI64 < EUI
414
+ public_class_method :new
387
415
  end
388
416
 
417
+
389
418
  end # module NetAddr
390
419
  __END__