netaddr 1.4.0 → 1.5.3

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.

@@ -3,10 +3,6 @@ private
3
3
 
4
4
  # CIDR METHODS
5
5
 
6
- #==============================================================================#
7
- # cidr_build()
8
- #==============================================================================#
9
-
10
6
  # create either a CIDRv4 or CIDRv6 object
11
7
  #
12
8
  def cidr_build(version, ip, netmask=nil, tag={}, wildcard_mask=nil, wildcard_mask_bit_flipped=false)
@@ -15,9 +11,6 @@ def cidr_build(version, ip, netmask=nil, tag={}, wildcard_mask=nil, wildcard_mas
15
11
  end
16
12
  module_function :cidr_build
17
13
 
18
- #==============================================================================#
19
- # cidr_compare()
20
- #==============================================================================#
21
14
 
22
15
  # compare 2 CIDR objects
23
16
  #
@@ -52,10 +45,6 @@ def cidr_compare(cidr1,cidr2)
52
45
  end
53
46
  module_function :cidr_compare
54
47
 
55
- #==============================================================================#
56
- # cidr_gt_lt()
57
- #==============================================================================#
58
-
59
48
  # given a pair of CIDRs, determine if first is greater than or less than the second
60
49
  #
61
50
  # return 1 if cidr1 > cidr2
@@ -78,10 +67,6 @@ def cidr_gt_lt(cidr1,cidr2)
78
67
  end
79
68
  module_function :cidr_gt_lt
80
69
 
81
- #==============================================================================#
82
- # cidr_fill_in()
83
- #==============================================================================#
84
-
85
70
  #Given a list of subnets of supernet, return a new list with any
86
71
  #holes (missing subnets) filled in.
87
72
  #
@@ -120,10 +105,6 @@ def cidr_fill_in(supernet,list)
120
105
  end
121
106
  module_function :cidr_fill_in
122
107
 
123
- #==============================================================================#
124
- # cidr_find_in_list()
125
- #==============================================================================#
126
-
127
108
  # evaluate cidr against list of cidrs.
128
109
  #
129
110
  # return entry from list if entry is supernet of cidr (first matching entry)
@@ -159,10 +140,6 @@ def cidr_find_in_list(cidr,list)
159
140
  end
160
141
  module_function :cidr_find_in_list
161
142
 
162
- #==============================================================================#
163
- # cidr_make_subnets_from_base_and_ip_count()
164
- #==============================================================================#
165
-
166
143
  # Make CIDR addresses from a base addr and an number of ip's to encapsulate.
167
144
  #
168
145
  #===Arguments:
@@ -204,10 +181,6 @@ module_function :cidr_find_in_list
204
181
  end
205
182
  module_function :cidr_make_subnets_from_base_and_ip_count
206
183
 
207
- #==============================================================================#
208
- # cidr_sort()
209
- #==============================================================================#
210
-
211
184
  # given a list of NetAddr::CIDRs, return them as a sorted list
212
185
  #
213
186
  def cidr_sort(list, desc=false)
@@ -258,10 +231,6 @@ def cidr_sort(list, desc=false)
258
231
  end
259
232
  module_function :cidr_sort
260
233
 
261
- #==============================================================================#
262
- # cidr_summarize()
263
- #==============================================================================#
264
-
265
234
  # given a list of NetAddr::CIDRs (of the same version) summarize them
266
235
  #
267
236
  # return a hash, with the key = summary address and val = array of original cidrs
@@ -322,7 +291,7 @@ def cidr_summarize(subnet_list)
322
291
  until (index >= summarized_list.length)
323
292
  subnet = summarized_list[index]
324
293
  if (subnet && cidr_compare(supernet,subnet) == 1 )
325
- subnet_list[index] = nil
294
+ unique_list.delete(subnet.desc)
326
295
  end
327
296
  index += 1
328
297
  end
@@ -347,9 +316,30 @@ def cidr_summarize(subnet_list)
347
316
  end
348
317
  module_function :cidr_summarize
349
318
 
350
- #==============================================================================#
351
- # netmask_sort()
352
- #==============================================================================#
319
+ # given a list of NetAddr::CIDRs (of the same version), return only the 'top level' blocks (i.e. blocks not
320
+ # contained by other blocks
321
+
322
+ def cidr_supernets(subnet_list)
323
+ summary_list = []
324
+ subnet_list = netmask_sort(subnet_list)
325
+ subnet_list.each do |child|
326
+ is_parent = true
327
+ summary_list.each do |parent|
328
+ if (NetAddr.cidr_compare(parent,child) == 1)
329
+ is_parent = false
330
+ parent.tag[:Subnets].push(child)
331
+ end
332
+ end
333
+
334
+ if (is_parent)
335
+ child.tag[:Subnets] = []
336
+ summary_list.push(child)
337
+ end
338
+ end
339
+
340
+ return(summary_list)
341
+ end
342
+ module_function :cidr_supernets
353
343
 
354
344
  # given a list of NetAddr::CIDRs, return them as a sorted (by netmask) list
355
345
  #
data/lib/eui.rb CHANGED
@@ -1,9 +1,3 @@
1
- =begin rdoc
2
- Copyleft (c) 2006 Dustin Spinhirne
3
-
4
- Licensed under the same terms as Ruby, No Warranty is provided.
5
- =end
6
-
7
1
  module NetAddr
8
2
 
9
3
  #=EUI - Extended Unique Identifier
@@ -27,10 +21,6 @@ class EUI
27
21
 
28
22
  private_class_method :new
29
23
 
30
- #==============================================================================#
31
- # initialize()
32
- #==============================================================================#
33
-
34
24
  #===Synopsis
35
25
  # This method performs absolutely no error checking, and is meant to be used only by
36
26
  # other internal methods for the sake of the speedier creation of EUI objects.
@@ -71,10 +61,6 @@ private_class_method :new
71
61
 
72
62
  end
73
63
 
74
- #==============================================================================#
75
- # create()
76
- #==============================================================================#
77
-
78
64
  #===Synopsis
79
65
  #Create a new EUI48 or EUI64 object.
80
66
  #
@@ -95,7 +81,8 @@ private_class_method :new
95
81
  raise ArgumentError, "Expected String, but #{eui.class} provided."
96
82
  end
97
83
 
98
- # validate
84
+ # create local copy & validate
85
+ eui = eui.dup
99
86
  NetAddr.validate_eui(eui)
100
87
 
101
88
  # remove formatting characters
@@ -110,10 +97,6 @@ private_class_method :new
110
97
  return(eui)
111
98
  end
112
99
 
113
- #==============================================================================#
114
- # address()
115
- #==============================================================================#
116
-
117
100
  #===Synopsis
118
101
  # Returns EUI address. The default address format is xxxx.xxxx.xxxx
119
102
  #
@@ -146,30 +129,14 @@ private_class_method :new
146
129
  end
147
130
 
148
131
  if (delimiter == '-' || delimiter == ':')
149
- addr = octets.join(delimiter)
132
+ addr = octets.join(delimiter)
150
133
  elsif (delimiter == '.')
151
- toggle = 0
152
- octets.each do |x|
153
- if (!addr)
154
- addr = x
155
- toggle = 1
156
- elsif (toggle == 0)
157
- addr = addr << '.' << x
158
- toggle = 1
159
- else
160
- addr = addr << x
161
- toggle = 0
162
- end
163
- end
134
+ addr = octets.each_slice(2).to_a.map(&:join).join('.')
164
135
  end
165
136
 
166
137
  return(addr)
167
138
  end
168
139
 
169
- #==============================================================================#
170
- # ei()
171
- #==============================================================================#
172
-
173
140
  #===Synopsis
174
141
  #Returns Extended Identifier portion of an EUI address (the vendor assigned ID).
175
142
  #The default address format is xx-xx-xx
@@ -211,10 +178,6 @@ private_class_method :new
211
178
  return(ei)
212
179
  end
213
180
 
214
- #==============================================================================#
215
- # link_local()
216
- #==============================================================================#
217
-
218
181
  #===Synopsis
219
182
  # Provide an IPv6 Link Local address based on the current EUI address.
220
183
  #
@@ -231,45 +194,9 @@ private_class_method :new
231
194
  #* CIDR address String or an NetAddr::CIDR object
232
195
  #
233
196
  def link_local(options=nil)
234
- known_args = [:Short, :Objectify]
235
- objectify = false
236
- short = false
237
-
238
- if (options)
239
- if (!options.kind_of? Hash)
240
- raise ArgumentError, "Expected Hash, but #{options.class} provided."
241
- end
242
- NetAddr.validate_args(options.keys,known_args)
243
-
244
- if (options.has_key?(:Objectify) && options[:Objectify] == true)
245
- objectify = true
246
- end
247
-
248
- if (options.has_key?(:Short) && options[:Short] == true)
249
- short = true
250
- end
251
- end
252
-
253
- if (self.kind_of?(NetAddr::EUI64))
254
- link_local = 'fe80000000000000' << @eui
255
- else
256
- link_local = 'fe80000000000000' << @oui << 'fffe' << @ei
257
- end
258
-
259
- if (!objectify)
260
- link_local = NetAddr.i_to_ip(link_local.to_i(16), :Version => 6)
261
- link_local = NetAddr.shorten(link_local) if (short)
262
- else
263
- link_local = NetAddr::CIDRv6.new(link_local.to_i(16))
264
- end
265
-
266
- return(link_local)
197
+ return( self.to_ipv6('fe80::/64', options) )
267
198
  end
268
199
 
269
- #==============================================================================#
270
- # oui()
271
- #==============================================================================#
272
-
273
200
  #===Synopsis
274
201
  #Returns Organizationally Unique Identifier portion of an EUI address (the vendor ID).
275
202
  #The default address format is xx-xx-xx.
@@ -280,7 +207,7 @@ private_class_method :new
280
207
  #
281
208
  #===Arguments:
282
209
  #* options = Hash with the following fields:
283
- # :Delimiter -- delimitation character. valid values are (-, and :)
210
+ # :Delimiter -- delimitation character. valid values are (-, and :)
284
211
  #
285
212
  #===Returns:
286
213
  #* String
@@ -306,10 +233,6 @@ private_class_method :new
306
233
  return(oui)
307
234
  end
308
235
 
309
- #==============================================================================#
310
- # to_i()
311
- #==============================================================================#
312
-
313
236
  #===Synopsis
314
237
  #Returns the EUI as an Integer.
315
238
  #
@@ -327,9 +250,77 @@ private_class_method :new
327
250
  return(@eui_i)
328
251
  end
329
252
 
330
- #==============================================================================#
331
- # to_s()
332
- #==============================================================================#
253
+ #===Synopsis
254
+ # Given a valid IPv6 subnet, return an IPv6 address based on the current EUI.
255
+ #
256
+ # Example:
257
+ # addr = NetAddr::EUI.create('aabb.ccdd.eeff')
258
+ # addr.to_ipv6('3ffe::/64') => "3ffe:0000:0000:0000:a8bb:ccff:fedd:eeff"
259
+ #
260
+ #===Arguments:
261
+ #* options = Hash with the following fields:
262
+ # :Short -- if true, return IPv6 addresses in short-hand notation
263
+ # :Objectify -- if true, return CIDR objects
264
+ #
265
+ #===Returns:
266
+ #* IPv6 address String or an NetAddr::CIDRv6 object
267
+ #
268
+ def to_ipv6(cidr, options=nil)
269
+ known_args = [:Short, :Objectify]
270
+ objectify = false
271
+ short = false
272
+
273
+ if ( !cidr.kind_of?(NetAddr::CIDR) )
274
+ begin
275
+ cidr = NetAddr::CIDR.create(cidr)
276
+ rescue Exception => error
277
+ raise ArgumentError, "CIDR raised the following errors: #{error}"
278
+ end
279
+ elsif (cidr.kind_of?(NetAddr::CIDRv4) )
280
+ raise ArgumentError, "Expected CIDRv6, but #{cidr.class} provided."
281
+ end
282
+
283
+ if (cidr.bits > 64)
284
+ raise ValidationError, "Prefix length of provided CIDR must be /64 or less but was #{cidr.netmask}."
285
+ end
286
+
287
+ if (options)
288
+ if (!options.kind_of? Hash)
289
+ raise ArgumentError, "Expected Hash, but #{options.class} provided."
290
+ end
291
+ NetAddr.validate_args(options.keys,known_args)
292
+
293
+ if (options.has_key?(:Objectify) && options[:Objectify] == true)
294
+ objectify = true
295
+ end
296
+
297
+ if (options.has_key?(:Short) && options[:Short] == true)
298
+ short = true
299
+ end
300
+ end
301
+
302
+ # get integer equiv of addr. conver eui48 to eui64 if needed
303
+ if ( self.kind_of?(NetAddr::EUI48) )
304
+ eui_i = self.to_eui64.to_i
305
+ else
306
+ eui_i = self.to_i
307
+ end
308
+
309
+ # toggle u/l bit
310
+ eui_i = eui_i ^ 0x0200000000000000
311
+
312
+ # create ipv6 address
313
+ ipv6 = cidr.to_i | eui_i
314
+
315
+ if (!objectify)
316
+ ipv6 = NetAddr.i_to_ip(ipv6, :Version => 6)
317
+ ipv6 = NetAddr.shorten(ipv6) if (short)
318
+ else
319
+ ipv6 = NetAddr::CIDRv6.new(ipv6)
320
+ end
321
+
322
+ return(ipv6)
323
+ end
333
324
 
334
325
  #===Synopsis
335
326
  #Returns the EUI as an unformatted String.
@@ -350,10 +341,6 @@ private_class_method :new
350
341
 
351
342
  private
352
343
 
353
- #==============================================================================#
354
- # octets()
355
- #==============================================================================#
356
-
357
344
  #Returns array with each element representing a single octet of the eui.
358
345
  #
359
346
  def octets()
@@ -382,10 +369,6 @@ class EUI48 < EUI
382
369
 
383
370
  public_class_method :new
384
371
 
385
- #==============================================================================#
386
- # to_eui64()
387
- #==============================================================================#
388
-
389
372
  #===Synopsis
390
373
  #Return an EUI64 address based on the current EUI48 address.
391
374
  #
@@ -400,7 +383,7 @@ class EUI48 < EUI
400
383
  #* NetAddr::EUI64 object
401
384
  #
402
385
  def to_eui64()
403
- eui = @oui << 'fffe' << @ei
386
+ eui = @oui + 'fffe' + @ei
404
387
  return( NetAddr::EUI64.new(eui.to_i(16)) )
405
388
  end
406
389
 
data/lib/ip_math.rb CHANGED
@@ -4,10 +4,6 @@ private
4
4
 
5
5
  # IP MATH METHODS
6
6
 
7
- #==============================================================================#
8
- # binary_mirror()
9
- #==============================================================================#
10
-
11
7
  # given an integer and number of bits to consider, return its binary mirror
12
8
  #
13
9
  def binary_mirror(num, bit_count)
@@ -22,10 +18,6 @@ def binary_mirror(num, bit_count)
22
18
  end
23
19
  module_function :binary_mirror
24
20
 
25
- #==============================================================================#
26
- # bits_to_mask()
27
- #==============================================================================#
28
-
29
21
  # convert a netmask (in bits) to an integer mask
30
22
  #
31
23
  def bits_to_mask(netmask,version)
@@ -36,10 +28,6 @@ def bits_to_mask(netmask,version)
36
28
  end
37
29
  module_function :bits_to_mask
38
30
 
39
- #==============================================================================#
40
- # detect_ip_version()
41
- #==============================================================================#
42
-
43
31
  # determine the ip version from ip address string.
44
32
  #
45
33
  # return 4, 6, or nil
@@ -57,10 +45,6 @@ def detect_ip_version(ip)
57
45
  end
58
46
  module_function :detect_ip_version
59
47
 
60
- #==============================================================================#
61
- # ip_count_to_size()
62
- #==============================================================================#
63
-
64
48
  # given an ip count, determine the most appropriate mask (in bits)
65
49
  #
66
50
  def ip_count_to_size(ipcount,version,extended=false)
@@ -83,10 +67,6 @@ def ip_count_to_size(ipcount,version,extended=false)
83
67
  end
84
68
  module_function :ip_count_to_size
85
69
 
86
- #==============================================================================#
87
- # ip_int_to_str()
88
- #==============================================================================#
89
-
90
70
  # unpack an int into an ip address string
91
71
  #
92
72
  def ip_int_to_str(ip_int, version, ipv4_mapped=nil)
@@ -129,10 +109,6 @@ def ip_int_to_str(ip_int, version, ipv4_mapped=nil)
129
109
  end
130
110
  module_function :ip_int_to_str
131
111
 
132
- #==============================================================================#
133
- # ip_str_to_int()
134
- #==============================================================================#
135
-
136
112
  # convert an ip string into an int
137
113
  #
138
114
  def ip_str_to_int(ip,version)
@@ -197,10 +173,6 @@ def ip_str_to_int(ip,version)
197
173
  end
198
174
  module_function :ip_str_to_int
199
175
 
200
- #==============================================================================#
201
- # mask_to_bits()
202
- #==============================================================================#
203
-
204
176
  # convert integer into a cidr formatted netmask (bits)
205
177
  #
206
178
  def mask_to_bits(netmask_int)
@@ -226,10 +198,6 @@ def mask_to_bits(netmask_int)
226
198
  end
227
199
  module_function :mask_to_bits
228
200
 
229
- #==============================================================================#
230
- # netmask_str_to_int()
231
- #==============================================================================#
232
-
233
201
  # convert string into integer mask
234
202
  #
235
203
  def netmask_str_to_int(netmask,version)