ipadmin 0.1.0 → 0.1.1

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/README CHANGED
@@ -103,8 +103,8 @@
103
103
  puts "cidr4 size is #{cidr4.size()}"
104
104
  puts "cidr6 size is #{cidr6.size()}"
105
105
 
106
- cidr4.enumerate(:Limit => 4, :BitStep => 32) {|addr| puts addr}
107
- cidr6.enumerate(:Limit => 4, :BitStep => 32, :Objectify => 1) {|addr| puts addr.desc()}
106
+ cidr4.enumerate(:Limit => 4, :Bitstep => 32) {|addr| puts addr}
107
+ cidr6.enumerate(:Limit => 4, :Bitstep => 32, :Objectify => 1) {|addr| puts addr.desc()}
108
108
 
109
109
  cidr4.subnet(:Subnet => 28, :MinCount => 3) {|cidr| puts cidr.desc()}
110
110
  cidr6.subnet(:Subnet => 66, :MinCount => 4) {|cidr| puts cidr.desc()}
@@ -216,6 +216,17 @@
216
216
 
217
217
  puts "192.168.1.0/24 and 192.168.0.0/24 merge into #{IPAdmin.merge_cidr([cidr4_1,cidr4_2]).desc}"
218
218
  puts "fec0::0/128 and fec0::1/128 merge into #{IPAdmin.merge_cidr([cidr6_1,cidr6_2]).desc}"
219
+
220
+ ip1 = IPAdmin::IPAddr.new(:IPAddr => '192.168.1.0')
221
+ ip2 = IPAdmin::IPAddr.new(:IPAddr => '192.168.1.50')
222
+
223
+
224
+ list = IPAdmin.range(:Boundaries => [ip1,ip2], :Bitstep => 20 )
225
+ puts "ip's between #{ip1.desc} and #{ip2.desc}"
226
+ list.each do |x|
227
+ puts " #{x}"
228
+ end
229
+
219
230
 
220
231
  print "\n\n\n"
221
232
  #=====================================#
data/lib/ip_admin.rb CHANGED
@@ -392,6 +392,139 @@ module_function :pack_ipv6_netmask
392
392
 
393
393
 
394
394
 
395
+ #============================================================================#
396
+ # range()
397
+ #============================================================================#
398
+
399
+ # Given two IPAdmin::IPAddr objects of the same version, return all IP
400
+ # addresses between them (non-inclusive).
401
+ #
402
+ # - Arguments:
403
+ # * Hash with the following fields
404
+ # - :Bitstep -- enumerate in X sized steps
405
+ # - :Boundaries -- array of (2) IPAdmin::IPAddr objects
406
+ # - :Limit -- limit returned list to X number of items
407
+ # - :Objectify -- return IPAdmin::IPAddr objects
408
+ #
409
+ # - Returns:
410
+ # * array of IP addresses or IPAdmin::IPAddr objects
411
+ #
412
+ def range(options)
413
+ list = []
414
+ bitstep = 1
415
+ objectify = nil
416
+ limit = nil
417
+ v4_all_f = (2**32)-1
418
+ v6_all_f = (2**128)-1
419
+
420
+ # check options
421
+ if (options)
422
+ unless ( options.has_key?(:Boundaries) )
423
+ raise ArgumentError, "Missing arguments: Boundaries."
424
+ end
425
+
426
+ if (options[:Boundaries].length == 2)
427
+ (ip1,ip2) = options[:Boundaries]
428
+ else
429
+ raise ArgumentError, "Two IPAdmin::IPAddr ojects are required. " +
430
+ "as Boundaries."
431
+ end
432
+
433
+ if( options.has_key?(:Bitstep) )
434
+ bitstep = options[:Bitstep]
435
+ end
436
+
437
+ if( options.has_key?(:Objectify) )
438
+ objectify = 1
439
+ end
440
+
441
+ if( options.has_key?(:Limit) )
442
+ limit = options[:Limit]
443
+ end
444
+ end
445
+
446
+
447
+
448
+ # check our ipaddr objects
449
+ unless ((ip1.kind_of? IPAdmin::IPAddr) && (ip2.kind_of? IPAdmin::IPAddr))
450
+ raise "One or more values provided under :Boundaries "+
451
+ "is not a valid IPAdmin::IPAddr object."
452
+ end
453
+
454
+ if (ip1.version == ip2.version)
455
+ version = ip1.version
456
+ boundaries = [ip1.packed_ip, ip2.packed_ip]
457
+ boundaries.sort
458
+ else
459
+ raise "Provided IPAdmin::IPAddr objects are of different versions."
460
+ end
461
+
462
+
463
+
464
+ # dump our range
465
+ if ( (version == 4) && !objectify)
466
+ my_ip = boundaries[0] + 1
467
+
468
+ until (my_ip >= boundaries[1])
469
+ list.push( IPAdmin.unpack_ipv4_addr(my_ip) )
470
+ my_ip = my_ip + bitstep
471
+ if (limit)
472
+ limit = limit -1
473
+ break if (limit == 0)
474
+ end
475
+ end
476
+
477
+ elsif ( (version == 4) && objectify)
478
+ my_ip = boundaries[0] + 1
479
+
480
+ until (my_ip >= boundaries[1])
481
+ my_ip_s = IPAdmin.unpack_ipv4_addr(my_ip)
482
+ list.push( IPAdmin::IPAddr.new(:IPAddr => my_ip_s) )
483
+ my_ip = my_ip + bitstep
484
+ if (limit)
485
+ limit = limit -1
486
+ break if (limit == 0)
487
+ end
488
+ end
489
+
490
+ elsif ( (version >= 6) && !objectify)
491
+ my_ip = boundaries[0] + 1
492
+
493
+ until (my_ip == boundaries[1])
494
+ list.push( IPAdmin.unpack_ipv6_addr(my_ip) )
495
+ my_ip = my_ip + bitstep
496
+ if (limit)
497
+ limit = limit -1
498
+ break if (limit == 0)
499
+ end
500
+ end
501
+
502
+ elsif ( (version >= 6) && objectify)
503
+ my_ip = boundaries[0] + 1
504
+
505
+ until (my_ip == boundaries[1])
506
+ my_ip_s = IPAdmin.unpack_ipv6_addr(my_ip)
507
+ list.push( IPAdmin::IPAddr.new(:IPAddr => my_ip_s) )
508
+ my_ip = my_ip + bitstep
509
+ if (limit)
510
+ limit = limit -1
511
+ break if (limit == 0)
512
+ end
513
+ end
514
+
515
+ end
516
+
517
+
518
+ return(list)
519
+ end
520
+ module_function :range
521
+
522
+ #=====================================#
523
+ #
524
+ #=====================================#
525
+
526
+
527
+
395
528
  #============================================================================#
396
529
  # unpack_ipv4_addr()
397
530
  #============================================================================#
@@ -1014,7 +1147,7 @@ class CIDR
1014
1147
  #
1015
1148
  # - Arguments:
1016
1149
  # * Hash with the following fields
1017
- # - :BitStep -- enumerate in X sized steps
1150
+ # - :Bitstep -- enumerate in X sized steps
1018
1151
  # - :Objectify -- return IPAdmin::IPAddr objects
1019
1152
  # - :Limit -- limit returned list to X number of items
1020
1153
  #
@@ -1029,8 +1162,8 @@ class CIDR
1029
1162
  v6_all_f = (2**128)-1
1030
1163
 
1031
1164
  if (options)
1032
- if( options.has_key?(:BitStep) )
1033
- bitstep = options[:BitStep]
1165
+ if( options.has_key?(:Bitstep) )
1166
+ bitstep = options[:Bitstep]
1034
1167
  end
1035
1168
 
1036
1169
  if( options.has_key?(:Objectify) )
@@ -1455,7 +1588,7 @@ class CIDR
1455
1588
 
1456
1589
  # list all 'subnet' sized subnets of this cidr block
1457
1590
  bitstep = 2**(max_bits - subnet)
1458
- subs = self.enumerate(:BitStep => bitstep)
1591
+ subs = self.enumerate(:Bitstep => bitstep)
1459
1592
  subs.each do |sub|
1460
1593
  cidr = "#{sub}/#{subnet}"
1461
1594
  new_subnets.push(IPAdmin::CIDR.new(:CIDR => cidr))
data/tests/cidr_test.rb CHANGED
@@ -54,8 +54,8 @@ class TestCIDR < Test::Unit::TestCase
54
54
  assert_equal(['fec0:0000:0000:0000:0000:0000:0000:0000'],cidr6.enumerate(:Limit => 1) )
55
55
 
56
56
 
57
- enums4 = cidr4.enumerate(:Limit => 2, :BitStep => 5)
58
- enums6 = cidr6.enumerate(:Limit => 2, :BitStep => 5)
57
+ enums4 = cidr4.enumerate(:Limit => 2, :Bitstep => 5)
58
+ enums6 = cidr6.enumerate(:Limit => 2, :Bitstep => 5)
59
59
  assert_equal('192.168.1.5', enums4[1] )
60
60
  assert_equal('fec0:0000:0000:0000:0000:0000:0000:0005', enums6[1] )
61
61
 
@@ -105,6 +105,7 @@ class TestIPAdmin < Test::Unit::TestCase
105
105
  cidr4_2 = IPAdmin::CIDR.new(:CIDR => '192.168.1.0/25')
106
106
  cidr4_3 = IPAdmin::CIDR.new(:CIDR => '192.168.2.0/24')
107
107
  cidr4_4 = IPAdmin::CIDR.new(:CIDR => '192.168.2.64/26')
108
+ cidr4_5 = IPAdmin::CIDR.new(:CIDR => '192.168.1.128/25')
108
109
 
109
110
  cidr6_1 = IPAdmin::CIDR.new(:CIDR => 'fec0::0/10')
110
111
  cidr6_2 = IPAdmin::CIDR.new(:CIDR => 'fec0::0/64')
@@ -114,12 +115,12 @@ class TestIPAdmin < Test::Unit::TestCase
114
115
  comp1 = IPAdmin.compare_cidr(cidr4_1,cidr4_2)
115
116
  comp2 = IPAdmin.compare_cidr(cidr4_4,cidr4_3)
116
117
  comp3 = IPAdmin.compare_cidr(cidr4_1,cidr4_1)
117
- comp4 = IPAdmin.compare_cidr(cidr4_1,cidr4_3)
118
-
118
+ comp4 = IPAdmin.compare_cidr(cidr4_1,cidr4_3)
119
119
  comp5 = IPAdmin.compare_cidr(cidr6_1,cidr6_2)
120
120
  comp6 = IPAdmin.compare_cidr(cidr6_4,cidr6_3)
121
121
  comp7 = IPAdmin.compare_cidr(cidr6_1,cidr6_1)
122
- comp8 = IPAdmin.compare_cidr(cidr6_1,cidr6_3)
122
+ comp8 = IPAdmin.compare_cidr(cidr6_1,cidr6_3)
123
+ comp9 = IPAdmin.compare_cidr(cidr4_2,cidr4_5)
123
124
 
124
125
  assert_equal([cidr4_1,cidr4_2],comp1)
125
126
  assert_equal([cidr4_3,cidr4_4],comp2)
@@ -129,6 +130,7 @@ class TestIPAdmin < Test::Unit::TestCase
129
130
  assert_equal([cidr6_3,cidr6_4],comp6)
130
131
  assert_equal(1,comp7)
131
132
  assert_nil(comp8)
133
+ assert_nil(comp9)
132
134
  end
133
135
 
134
136
 
@@ -147,6 +149,24 @@ class TestIPAdmin < Test::Unit::TestCase
147
149
  assert_raise(RuntimeError){ IPAdmin.merge_cidr([cidr6_1,cidr6_3]) }
148
150
  end
149
151
 
152
+ def test_range
153
+ ip4_1 = IPAdmin::IPAddr.new(:IPAddr => '192.168.1.0')
154
+ ip4_2 = IPAdmin::IPAddr.new(:IPAddr => '192.168.1.50')
155
+ ip6_1 = IPAdmin::IPAddr.new(:IPAddr => 'fec0::0')
156
+ ip6_2 = IPAdmin::IPAddr.new(:IPAddr => 'fec0::32')
157
+
158
+ assert_equal(['192.168.1.1'], IPAdmin.range(:Boundaries => [ip4_1,ip4_2], :Limit => 1) )
159
+ assert_equal(['fec0:0000:0000:0000:0000:0000:0000:0001'], IPAdmin.range(:Boundaries => [ip6_1,ip6_2], :Limit => 1) )
160
+
161
+ list = IPAdmin.range(:Boundaries => [ip4_1,ip4_2], :Bitstep => 2)
162
+
163
+ assert_equal(25, list.length)
164
+ assert_equal('192.168.1.49', list[24])
165
+
166
+ assert_raise(ArgumentError){ IPAdmin.range(:Limit => 1) }
167
+ assert_raise(RuntimeError){ IPAdmin.range(:Boundaries => [1,2]) }
168
+ end
169
+
150
170
  end
151
171
 
152
172
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ipadmin
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.0
7
- date: 2006-01-17 00:00:00 -06:00
6
+ version: 0.1.1
7
+ date: 2006-01-22 00:00:00 -06:00
8
8
  summary: A package for manipulating IPv4/IPv6 address space.
9
9
  require_paths:
10
10
  - lib