ipadmin 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +166 -446
- data/lib/cidr.rb +132 -241
- data/lib/eui.rb +6 -40
- data/lib/methods.rb +28 -99
- data/lib/tree.rb +23 -144
- data/tests/cidr_test.rb +28 -17
- data/tests/tree_test.rb +2 -1
- metadata +4 -4
data/lib/cidr.rb
CHANGED
@@ -27,8 +27,8 @@ class CIDR
|
|
27
27
|
# Hash of custom tags. Should be in the format tag => value.
|
28
28
|
#
|
29
29
|
# Example:
|
30
|
-
# cidr4.tag[
|
31
|
-
# puts cidr4.tag[
|
30
|
+
# cidr4.tag[:name] = 'IPv4 CIDR'
|
31
|
+
# puts cidr4.tag[:name]
|
32
32
|
#
|
33
33
|
def tag=(new_tag)
|
34
34
|
if (!new_tag.kind_of? Hash)
|
@@ -37,35 +37,30 @@ class CIDR
|
|
37
37
|
@tag = new_tag
|
38
38
|
end
|
39
39
|
|
40
|
-
#======================================#
|
41
|
-
#
|
42
|
-
#======================================#
|
43
|
-
|
44
|
-
|
45
40
|
#==============================================================================#
|
46
41
|
# initialize()
|
47
42
|
#==============================================================================#
|
48
43
|
|
49
44
|
# - Arguments:
|
50
|
-
# * CIDR address as a
|
51
|
-
# - :CIDR --
|
52
|
-
# - :Netmask -- IP Netmask - String or Integer (optional)
|
45
|
+
# * CIDR address as a String, or a Hash with the following fields:
|
46
|
+
# - :CIDR -- CIDR address, single IP, or IP and Netmask in extended format - String (optional)
|
53
47
|
# - :PackedIP -- Integer representation of an IP address (optional)
|
54
48
|
# - :PackedNetmask -- Integer representation of an IP Netmask (optional)
|
55
49
|
# - :Version -- IP version - Integer (optional)
|
56
50
|
# - :Tag -- Custom descriptor tag - Hash, tag => value. (optional)
|
57
51
|
#
|
58
52
|
# - Notes:
|
59
|
-
# * At a minimum, either :CIDR or PackedIP must be provided.
|
60
|
-
# *
|
61
|
-
# *
|
62
|
-
# * :
|
63
|
-
# *
|
53
|
+
# * At a minimum, either :CIDR or :PackedIP must be provided.
|
54
|
+
# * CIDR formatted netmasks take precedence over extended formatted ones.
|
55
|
+
# * CIDR address defaults to a host network (/32 or /128) if netmask not provided.
|
56
|
+
# * :PackedIP takes precedence over IP given within :CIDR.
|
57
|
+
# * :PackedNetmask takes precedence over netmask given within :CIDR.
|
64
58
|
# * Version will be auto-detected if not specified
|
65
59
|
#
|
66
60
|
# Examples:
|
67
61
|
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
68
62
|
# cidr4 = IPAdmin::CIDR.new(:CIDR => '192.168.1.1/24')
|
63
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1 255.255.255.0')
|
69
64
|
# cidr4_2 = IPAdmin::CIDR.new(:PackedIP => 0x0a010001,
|
70
65
|
# :PackedNetmask => 0xffffff00
|
71
66
|
# :Version => 4)
|
@@ -75,6 +70,7 @@ class CIDR
|
|
75
70
|
#
|
76
71
|
def initialize(options)
|
77
72
|
@tag = {}
|
73
|
+
|
78
74
|
if (!options.kind_of?(Hash) && !options.kind_of?(String))
|
79
75
|
raise ArgumentError, "Expected Hash or String, but #{options.class} provided."
|
80
76
|
end
|
@@ -82,8 +78,9 @@ class CIDR
|
|
82
78
|
if (options.kind_of? String)
|
83
79
|
cidr = options
|
84
80
|
end
|
85
|
-
|
81
|
+
|
86
82
|
if (options.kind_of? Hash)
|
83
|
+
# packed_ip takes precedence over cidr
|
87
84
|
if (options.has_key?(:PackedIP))
|
88
85
|
packed_ip = options[:PackedIP]
|
89
86
|
raise ArgumentError, "Expected Integer, but #{packed_ip.class} " +
|
@@ -100,11 +97,6 @@ class CIDR
|
|
100
97
|
packed_netmask = options[:PackedNetmask]
|
101
98
|
raise ArgumentError, "Expected Integer, but #{packed_netmask.class} " +
|
102
99
|
"provided for option :PackedNetmask." if (!packed_netmask.kind_of?(Integer))
|
103
|
-
|
104
|
-
elsif (options.has_key?(:Netmask))
|
105
|
-
netmask = options[:Netmask]
|
106
|
-
raise ArgumentError, "Expected String or Integer, but #{netmask.class} " +
|
107
|
-
"provided for option :Netmask." if (!netmask.kind_of?(String) && !netmask.kind_of?(Integer))
|
108
100
|
end
|
109
101
|
|
110
102
|
if (options.has_key?(:Version))
|
@@ -123,20 +115,28 @@ class CIDR
|
|
123
115
|
end
|
124
116
|
|
125
117
|
|
126
|
-
if (packed_ip)
|
127
|
-
if
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
118
|
+
if (packed_ip)
|
119
|
+
# attempt to determine version if not provided
|
120
|
+
if (!@version)
|
121
|
+
if (packed_ip < 2**32)
|
122
|
+
@version = 4
|
123
|
+
else
|
124
|
+
@version = 6
|
125
|
+
end
|
133
126
|
end
|
134
127
|
|
135
128
|
# validate & store packed_ip
|
136
129
|
IPAdmin.validate_ip_addr(:IP => packed_ip, :Version => @version)
|
137
130
|
@ip = packed_ip
|
138
131
|
|
139
|
-
else
|
132
|
+
else
|
133
|
+
netmask = nil
|
134
|
+
|
135
|
+
# if extended netmask provided. should only apply to ipv4
|
136
|
+
if (cidr =~ /.+\s+.+/ )
|
137
|
+
cidr,netmask = cidr.split(' ')
|
138
|
+
end
|
139
|
+
|
140
140
|
# determine version if not set
|
141
141
|
if (!@version)
|
142
142
|
if (cidr =~ /\./ && cidr !~ /:/)
|
@@ -144,37 +144,26 @@ class CIDR
|
|
144
144
|
elsif (cidr =~ /:/)
|
145
145
|
@version = 6
|
146
146
|
end
|
147
|
-
end
|
148
|
-
|
149
|
-
#
|
150
|
-
|
147
|
+
end
|
148
|
+
|
149
|
+
# if ipv6 and extended netmask was provided, then raise exception
|
150
|
+
raise ArgumentError, "Garbage provided at end of IPv6 address." if (@version == 6 && netmask)
|
151
|
+
|
152
|
+
# if netmask part of cidr, then over-ride any provided extended netmask.
|
153
|
+
if (cidr =~ /\//)
|
151
154
|
ip,netmask = cidr.split(/\//)
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
elsif (@version == 6)
|
155
|
+
if (!ip || !netmask)
|
156
|
+
raise ArgumentError, "CIDR address is improperly formatted. Missing netmask after '/' character."
|
157
|
+
end
|
158
|
+
else
|
157
159
|
ip = cidr
|
158
|
-
@netmask = 2**128-1 if (!netmask && !packed_netmask)
|
159
160
|
end
|
160
|
-
|
161
|
-
# pack ip
|
162
|
-
@ip = IPAdmin.pack_ip_addr(:IP => ip, :Version => @version)
|
163
161
|
|
162
|
+
# pack ip
|
163
|
+
@ip = IPAdmin.pack_ip_addr(:IP => ip, :Version => @version)
|
164
164
|
end
|
165
165
|
|
166
166
|
|
167
|
-
# validate & store netmask || packed_netmask if needed
|
168
|
-
if (!@netmask)
|
169
|
-
if (packed_netmask)
|
170
|
-
IPAdmin.validate_ip_netmask(:Netmask => packed_netmask, :Packed => true, :Version => @version)
|
171
|
-
@netmask = packed_netmask
|
172
|
-
else
|
173
|
-
IPAdmin.validate_ip_netmask(:Netmask => netmask, :Version => @version)
|
174
|
-
@netmask = IPAdmin.pack_ip_netmask(:Netmask => netmask, :Version => @version)
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
167
|
# set vars based on version
|
179
168
|
if (@version == 4)
|
180
169
|
@max_bits = 32
|
@@ -184,17 +173,26 @@ class CIDR
|
|
184
173
|
@all_f = 2**@max_bits - 1
|
185
174
|
end
|
186
175
|
|
187
|
-
#
|
176
|
+
# if no netmask or packed_netmask, then set as /32 or /128.
|
177
|
+
# else validate. packed_netmask takes precedence over netmask
|
178
|
+
if (!netmask && !packed_netmask)
|
179
|
+
@netmask = @all_f
|
180
|
+
else
|
181
|
+
if (packed_netmask)
|
182
|
+
IPAdmin.validate_ip_netmask(:Netmask => packed_netmask, :Packed => true, :Version => @version)
|
183
|
+
@netmask = packed_netmask
|
184
|
+
else
|
185
|
+
IPAdmin.validate_ip_netmask(:Netmask => netmask, :Version => @version)
|
186
|
+
@netmask = IPAdmin.pack_ip_netmask(:Netmask => netmask, :Version => @version)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# set @network & @hostmask
|
188
191
|
@network = (@ip & @netmask)
|
189
192
|
@hostmask = @netmask ^ @all_f
|
190
193
|
|
191
194
|
end
|
192
195
|
|
193
|
-
#======================================#
|
194
|
-
#
|
195
|
-
#======================================#
|
196
|
-
|
197
|
-
|
198
196
|
#==============================================================================#
|
199
197
|
# arpa()
|
200
198
|
#==============================================================================#
|
@@ -210,7 +208,10 @@ class CIDR
|
|
210
208
|
# * String
|
211
209
|
#
|
212
210
|
# Examples:
|
213
|
-
#
|
211
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
212
|
+
# cidr6 = IPAdmin::CIDR.new(:CIDR => 'fec0::/64')
|
213
|
+
# puts "arpa for #{cidr4.desc()} is #{cidr4.arpa}"
|
214
|
+
# puts "arpa for #{cidr6.desc(:Short => true)} is #{cidr6.arpa}"
|
214
215
|
#
|
215
216
|
def arpa()
|
216
217
|
|
@@ -254,11 +255,6 @@ class CIDR
|
|
254
255
|
return(arpa)
|
255
256
|
end
|
256
257
|
|
257
|
-
#======================================#
|
258
|
-
#
|
259
|
-
#======================================#
|
260
|
-
|
261
|
-
|
262
258
|
#==============================================================================#
|
263
259
|
# bits()
|
264
260
|
#==============================================================================#
|
@@ -272,32 +268,34 @@ class CIDR
|
|
272
268
|
# * Integer.
|
273
269
|
#
|
274
270
|
# Examples:
|
275
|
-
#
|
271
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
272
|
+
# cidr6 = IPAdmin::CIDR.new(:CIDR => 'fec0::/64')
|
273
|
+
# puts "cidr4 netmask in bits #{cidr4.bits()}"
|
274
|
+
# puts "cidr6 netmask in bits #{cidr6.bits()}"
|
276
275
|
#
|
277
276
|
def bits()
|
278
277
|
return(IPAdmin.unpack_ip_netmask(:Integer => @netmask))
|
279
278
|
end
|
280
279
|
|
281
|
-
#======================================#
|
282
|
-
#
|
283
|
-
#======================================#
|
284
|
-
|
285
|
-
|
286
280
|
#==============================================================================#
|
287
281
|
# contains?()
|
288
282
|
#==============================================================================#
|
289
283
|
|
290
284
|
# Determines if this CIDR contains (is supernet of)
|
291
|
-
# the provided CIDR
|
285
|
+
# the provided CIDR address or IPAdmin::CIDR object.
|
292
286
|
#
|
293
287
|
# - Arguments:
|
294
|
-
# * CIDR
|
288
|
+
# * CIDR address or IPAdmin::CIDR object
|
295
289
|
#
|
296
290
|
# - Returns:
|
297
291
|
# * true or false
|
298
292
|
#
|
299
293
|
# Examples:
|
300
|
-
#
|
294
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
295
|
+
# cidr6 = IPAdmin::CIDR.new('fec0::/64')
|
296
|
+
# cidr6_2 = IPAdmin::CIDR.new('fec0::/96')
|
297
|
+
# puts "#{cidr4.desc} contains 192.168.1.2" if ( cidr4.contains?('192.168.1.2') )
|
298
|
+
# puts "#{cidr6.desc} contains #{cidr6_2.desc(:Short => true)}" if ( cidr6.contains?(cidr6_2) )
|
301
299
|
#
|
302
300
|
def contains?(cidr)
|
303
301
|
is_contained = false
|
@@ -335,11 +333,6 @@ class CIDR
|
|
335
333
|
return(is_contained)
|
336
334
|
end
|
337
335
|
|
338
|
-
#======================================#
|
339
|
-
#
|
340
|
-
#======================================#
|
341
|
-
|
342
|
-
|
343
336
|
#==============================================================================#
|
344
337
|
# desc()
|
345
338
|
#==============================================================================#
|
@@ -352,11 +345,15 @@ class CIDR
|
|
352
345
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
353
346
|
#
|
354
347
|
# - Returns:
|
355
|
-
# *
|
348
|
+
# * String
|
356
349
|
#
|
357
350
|
# Examples:
|
358
|
-
#
|
351
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
352
|
+
# cidr6 = IPAdmin::CIDR.new('fec0::/64')
|
359
353
|
# puts cidr4.desc(:IP => true)
|
354
|
+
# puts "cidr4 description #{cidr4.desc()}"
|
355
|
+
# puts "cidr6 description #{cidr6.desc()}"
|
356
|
+
# puts "cidr6 short-hand description #{cidr6.desc(:Short => true)}"
|
360
357
|
#
|
361
358
|
def desc(options=nil)
|
362
359
|
short = false
|
@@ -387,30 +384,29 @@ class CIDR
|
|
387
384
|
return("#{ip}/#{mask}")
|
388
385
|
end
|
389
386
|
|
390
|
-
#======================================#
|
391
|
-
#
|
392
|
-
#======================================#
|
393
|
-
|
394
|
-
|
395
387
|
#==============================================================================#
|
396
388
|
# enumerate()
|
397
389
|
#==============================================================================#
|
398
390
|
|
399
391
|
# Provide all IP addresses contained within the IP space of this CIDR.
|
400
|
-
# (warning: this can be quite large for big blocks)
|
401
392
|
#
|
402
393
|
# - Arguments:
|
403
394
|
# * Optional Hash with the following fields:
|
404
|
-
# - :Bitstep -- enumerate in X sized steps
|
395
|
+
# - :Bitstep -- enumerate in X sized steps - Integer (optional)
|
405
396
|
# - :Limit -- limit returned list to X number of items - Integer (optional)
|
406
397
|
# - :Objectify -- if true, return IPAdmin::CIDR objects (optional)
|
407
398
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
408
399
|
#
|
409
400
|
# - Returns:
|
410
|
-
# * Array of
|
401
|
+
# * Array of Strings, or Array of IPAdmin::CIDR objects
|
411
402
|
#
|
412
403
|
# Examples:
|
413
|
-
#
|
404
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
405
|
+
# cidr6 = IPAdmin::CIDR.new('fec0::/64')
|
406
|
+
# puts "first 4 cidr4 addresses (bitstep 32)"
|
407
|
+
# cidr4.enumerate(:Limit => 4, :Bitstep => 32).each {|x| puts " #{x}"}
|
408
|
+
# puts "first 4 cidr6 addresses (bitstep 32)"
|
409
|
+
# cidr6.enumerate(:Limit => 4, :Bitstep => 32, :Objectify => true).each {|x| puts " #{x.desc}"}
|
414
410
|
#
|
415
411
|
def enumerate(options=nil)
|
416
412
|
bitstep = 1
|
@@ -463,11 +459,6 @@ class CIDR
|
|
463
459
|
return(list)
|
464
460
|
end
|
465
461
|
|
466
|
-
#======================================#
|
467
|
-
#
|
468
|
-
#======================================#
|
469
|
-
|
470
|
-
|
471
462
|
#==============================================================================#
|
472
463
|
# fill_in()
|
473
464
|
#==============================================================================#
|
@@ -476,14 +467,14 @@ class CIDR
|
|
476
467
|
# holes (missing subnets) filled in.
|
477
468
|
#
|
478
469
|
# - Arguments:
|
479
|
-
# * Array of CIDR addresses
|
470
|
+
# * Array of CIDR addresses, Array of IPAdmin::CIDR objects,
|
480
471
|
# or a Hash with the following fields:
|
481
472
|
# - :List -- Array of CIDR addresses or IPAdmin::CIDR objects
|
482
473
|
# - :Objectify -- if true, return IPAdmin::CIDR objects (optional)
|
483
474
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
484
475
|
#
|
485
476
|
# - Returns:
|
486
|
-
# * Array of CIDR
|
477
|
+
# * Array of CIDR Strings or Array of IPAdmin::CIDR objects
|
487
478
|
#
|
488
479
|
# Examples:
|
489
480
|
# subnets = cidr4.fill_in(['192.168.1.0/27','192.168.1.64/26','192.168.1.128/25'])
|
@@ -584,11 +575,6 @@ class CIDR
|
|
584
575
|
end
|
585
576
|
end
|
586
577
|
|
587
|
-
#======================================#
|
588
|
-
#
|
589
|
-
#======================================#
|
590
|
-
|
591
|
-
|
592
578
|
#==============================================================================#
|
593
579
|
# hostmask_ext()
|
594
580
|
#==============================================================================#
|
@@ -614,11 +600,6 @@ class CIDR
|
|
614
600
|
return(hostmask)
|
615
601
|
end
|
616
602
|
|
617
|
-
#======================================#
|
618
|
-
#
|
619
|
-
#======================================#
|
620
|
-
|
621
|
-
|
622
603
|
#==============================================================================#
|
623
604
|
# ip()
|
624
605
|
#==============================================================================#
|
@@ -631,10 +612,11 @@ class CIDR
|
|
631
612
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
632
613
|
#
|
633
614
|
# - Returns:
|
634
|
-
# *
|
615
|
+
# * String or IPAdmin::CIDR object.
|
635
616
|
#
|
636
617
|
# Examples:
|
637
618
|
# puts cidr4.ip()
|
619
|
+
# puts cidr4.ip(:Objectify => true).desc
|
638
620
|
#
|
639
621
|
def ip(options=nil)
|
640
622
|
objectify = false
|
@@ -667,11 +649,6 @@ class CIDR
|
|
667
649
|
return(ip)
|
668
650
|
end
|
669
651
|
|
670
|
-
#======================================#
|
671
|
-
#
|
672
|
-
#======================================#
|
673
|
-
|
674
|
-
|
675
652
|
#==============================================================================#
|
676
653
|
# last()
|
677
654
|
#==============================================================================#
|
@@ -684,7 +661,7 @@ class CIDR
|
|
684
661
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
685
662
|
#
|
686
663
|
# - Returns:
|
687
|
-
# *
|
664
|
+
# * String or IPAdmin::CIDR object.
|
688
665
|
#
|
689
666
|
# - Notes:
|
690
667
|
# * The broadcast() method is aliased to this method, and thus works for
|
@@ -727,11 +704,6 @@ class CIDR
|
|
727
704
|
|
728
705
|
alias :broadcast :last
|
729
706
|
|
730
|
-
#======================================#
|
731
|
-
#
|
732
|
-
#======================================#
|
733
|
-
|
734
|
-
|
735
707
|
#==============================================================================#
|
736
708
|
# multicast_mac()
|
737
709
|
#==============================================================================#
|
@@ -740,17 +712,17 @@ class CIDR
|
|
740
712
|
# and ff00::/8 for IPv6), return its ethernet MAC address (EUI-48) mapping.
|
741
713
|
#
|
742
714
|
# - Arguments:
|
743
|
-
# * Optional
|
715
|
+
# * Optional Hash with the following fields:
|
744
716
|
# - :Objectify -- if true, return EUI objects (optional)
|
745
717
|
#
|
746
718
|
# - Returns:
|
747
|
-
# * IPAdmin::EUI48 object
|
719
|
+
# * String or IPAdmin::EUI48 object
|
748
720
|
#
|
749
721
|
# - Note:
|
750
722
|
# * MAC address is based on original IP address passed during initialization.
|
751
723
|
#
|
752
724
|
# Examples:
|
753
|
-
# mcast = IPAdmin::CIDR.new(
|
725
|
+
# mcast = IPAdmin::CIDR.new('224.0.0.6')
|
754
726
|
# puts mcast.multicast_mac.address
|
755
727
|
#
|
756
728
|
def multicast_mac(options=nil)
|
@@ -790,16 +762,11 @@ class CIDR
|
|
790
762
|
return(eui)
|
791
763
|
end
|
792
764
|
|
793
|
-
#======================================#
|
794
|
-
#
|
795
|
-
#======================================#
|
796
|
-
|
797
|
-
|
798
765
|
#==============================================================================#
|
799
766
|
# netmask()
|
800
767
|
#==============================================================================#
|
801
768
|
|
802
|
-
# Provide netmask in CIDR format.
|
769
|
+
# Provide netmask in CIDR format (/yy).
|
803
770
|
#
|
804
771
|
# - Arguments:
|
805
772
|
# * none
|
@@ -815,11 +782,6 @@ class CIDR
|
|
815
782
|
return("/#{bits}")
|
816
783
|
end
|
817
784
|
|
818
|
-
#======================================#
|
819
|
-
#
|
820
|
-
#======================================#
|
821
|
-
|
822
|
-
|
823
785
|
#==============================================================================#
|
824
786
|
# netmask_ext()
|
825
787
|
#==============================================================================#
|
@@ -846,11 +808,6 @@ class CIDR
|
|
846
808
|
return(netmask)
|
847
809
|
end
|
848
810
|
|
849
|
-
#======================================#
|
850
|
-
#
|
851
|
-
#======================================#
|
852
|
-
|
853
|
-
|
854
811
|
#==============================================================================#
|
855
812
|
# network()
|
856
813
|
#==============================================================================#
|
@@ -863,7 +820,7 @@ class CIDR
|
|
863
820
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
864
821
|
#
|
865
822
|
# - Returns:
|
866
|
-
# *
|
823
|
+
# * String or IPAdmin::CIDR object.
|
867
824
|
#
|
868
825
|
# Examples:
|
869
826
|
# puts cidr4.network()
|
@@ -901,11 +858,6 @@ class CIDR
|
|
901
858
|
alias :base :network
|
902
859
|
alias :first :network
|
903
860
|
|
904
|
-
#======================================#
|
905
|
-
#
|
906
|
-
#======================================#
|
907
|
-
|
908
|
-
|
909
861
|
#==============================================================================#
|
910
862
|
# next_ip()
|
911
863
|
#==============================================================================#
|
@@ -920,10 +872,13 @@ class CIDR
|
|
920
872
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
921
873
|
#
|
922
874
|
# - Returns:
|
923
|
-
# *
|
875
|
+
# * String or IPAdmin::CIDR object.
|
924
876
|
#
|
925
877
|
# Examples:
|
926
|
-
#
|
878
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
879
|
+
# cidr6 = IPAdmin::CIDR.new('fec0::/64')
|
880
|
+
# puts "cidr4 next subnet #{cidr4.next_subnet()}"
|
881
|
+
# puts "cidr6 next subnet #{cidr6.next_subnet(:Short => true)}"
|
927
882
|
#
|
928
883
|
def next_ip(options=nil)
|
929
884
|
bitstep = 1
|
@@ -966,11 +921,6 @@ class CIDR
|
|
966
921
|
return(next_ip)
|
967
922
|
end
|
968
923
|
|
969
|
-
#======================================#
|
970
|
-
#
|
971
|
-
#======================================#
|
972
|
-
|
973
|
-
|
974
924
|
#==============================================================================#
|
975
925
|
# next_subnet()
|
976
926
|
#==============================================================================#
|
@@ -985,10 +935,13 @@ class CIDR
|
|
985
935
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
986
936
|
#
|
987
937
|
# - Returns:
|
988
|
-
# *
|
938
|
+
# * String or IPAdmin::CIDR object.
|
989
939
|
#
|
990
940
|
# Examples:
|
991
|
-
#
|
941
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
942
|
+
# cidr6 = IPAdmin::CIDR.new('fec0::/64')
|
943
|
+
# puts "cidr4 next subnet #{cidr4.next_subnet()}"
|
944
|
+
# puts "cidr6 next subnet #{cidr6.next_subnet(:Short => true)}"
|
992
945
|
#
|
993
946
|
def next_subnet(options=nil)
|
994
947
|
bitstep = 1
|
@@ -1020,24 +973,20 @@ class CIDR
|
|
1020
973
|
if (next_sub > @all_f)
|
1021
974
|
raise "Returned subnet is out of bounds for IPv#{@version}."
|
1022
975
|
end
|
1023
|
-
|
1024
|
-
netmask = self.bits
|
976
|
+
|
1025
977
|
if (!objectify)
|
1026
978
|
next_sub = IPAdmin.unpack_ip_addr(:Integer => next_sub, :Version => @version)
|
1027
979
|
next_sub = IPAdmin.shorten(next_sub) if (short && @version == 6)
|
1028
|
-
next_sub = next_sub << "/" <<
|
980
|
+
next_sub = next_sub << "/" << self.bits.to_s
|
1029
981
|
else
|
1030
|
-
next_sub = IPAdmin::CIDR.new(:PackedIP => next_sub,
|
982
|
+
next_sub = IPAdmin::CIDR.new(:PackedIP => next_sub,
|
983
|
+
:PackedNetmask => self.packed_netmask,
|
984
|
+
:Version => @version)
|
1031
985
|
end
|
1032
986
|
|
1033
987
|
return(next_sub)
|
1034
988
|
end
|
1035
989
|
|
1036
|
-
#======================================#
|
1037
|
-
#
|
1038
|
-
#======================================#
|
1039
|
-
|
1040
|
-
|
1041
990
|
#==============================================================================#
|
1042
991
|
# nth()
|
1043
992
|
#==============================================================================#
|
@@ -1051,9 +1000,10 @@ class CIDR
|
|
1051
1000
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
1052
1001
|
#
|
1053
1002
|
# - Returns:
|
1054
|
-
# *
|
1003
|
+
# * String or IPAdmin::CIDR object.
|
1055
1004
|
#
|
1056
1005
|
# Examples:
|
1006
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
1057
1007
|
# puts cidr4.nth(1)
|
1058
1008
|
# puts cidr4.nth(:Index => 1, :Objectify => true)
|
1059
1009
|
#
|
@@ -1099,16 +1049,11 @@ class CIDR
|
|
1099
1049
|
return(my_ip)
|
1100
1050
|
end
|
1101
1051
|
|
1102
|
-
#======================================#
|
1103
|
-
#
|
1104
|
-
#======================================#
|
1105
|
-
|
1106
|
-
|
1107
1052
|
#==============================================================================#
|
1108
1053
|
# packed_hostmask()
|
1109
1054
|
#==============================================================================#
|
1110
1055
|
|
1111
|
-
# Provide an
|
1056
|
+
# Provide an Integer representation of the Hostmask of this object.
|
1112
1057
|
#
|
1113
1058
|
# - Arguments:
|
1114
1059
|
# * none
|
@@ -1123,16 +1068,11 @@ class CIDR
|
|
1123
1068
|
return(@hostmask)
|
1124
1069
|
end
|
1125
1070
|
|
1126
|
-
#======================================#
|
1127
|
-
#
|
1128
|
-
#======================================#
|
1129
|
-
|
1130
|
-
|
1131
1071
|
#==============================================================================#
|
1132
1072
|
# packed_ip()
|
1133
1073
|
#==============================================================================#
|
1134
1074
|
|
1135
|
-
# Provide an
|
1075
|
+
# Provide an Integer representation of the IP address of this object.
|
1136
1076
|
#
|
1137
1077
|
# - Arguments:
|
1138
1078
|
# * none
|
@@ -1147,16 +1087,11 @@ class CIDR
|
|
1147
1087
|
return(@ip)
|
1148
1088
|
end
|
1149
1089
|
|
1150
|
-
#======================================#
|
1151
|
-
#
|
1152
|
-
#======================================#
|
1153
|
-
|
1154
|
-
|
1155
1090
|
#==============================================================================#
|
1156
1091
|
# packed_netmask()
|
1157
1092
|
#==============================================================================#
|
1158
1093
|
|
1159
|
-
# Provide an
|
1094
|
+
# Provide an Integer representation of the Netmask of this object.
|
1160
1095
|
#
|
1161
1096
|
# - Arguments:
|
1162
1097
|
# * none
|
@@ -1171,16 +1106,11 @@ class CIDR
|
|
1171
1106
|
return(@netmask)
|
1172
1107
|
end
|
1173
1108
|
|
1174
|
-
#======================================#
|
1175
|
-
#
|
1176
|
-
#======================================#
|
1177
|
-
|
1178
|
-
|
1179
1109
|
#==============================================================================#
|
1180
1110
|
# packed_network()
|
1181
1111
|
#==============================================================================#
|
1182
1112
|
|
1183
|
-
# Provide an
|
1113
|
+
# Provide an Integer representation of the Network address of this object.
|
1184
1114
|
#
|
1185
1115
|
# - Arguments:
|
1186
1116
|
# * none
|
@@ -1195,11 +1125,6 @@ class CIDR
|
|
1195
1125
|
return(@network)
|
1196
1126
|
end
|
1197
1127
|
|
1198
|
-
#======================================#
|
1199
|
-
#
|
1200
|
-
#======================================#
|
1201
|
-
|
1202
|
-
|
1203
1128
|
#==============================================================================#
|
1204
1129
|
# range()
|
1205
1130
|
#==============================================================================#
|
@@ -1215,9 +1140,10 @@ class CIDR
|
|
1215
1140
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
1216
1141
|
#
|
1217
1142
|
# - Returns:
|
1218
|
-
# * Array
|
1143
|
+
# * Array of Strings, or Array of IPAdmin::CIDR objects
|
1219
1144
|
#
|
1220
1145
|
# Examples:
|
1146
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
1221
1147
|
# list = cidr4.range([0,1])
|
1222
1148
|
# list = cidr4.range(:Indexes => [0,1], :Objectify => true)
|
1223
1149
|
#
|
@@ -1283,11 +1209,6 @@ class CIDR
|
|
1283
1209
|
return(list)
|
1284
1210
|
end
|
1285
1211
|
|
1286
|
-
#======================================#
|
1287
|
-
#
|
1288
|
-
#======================================#
|
1289
|
-
|
1290
|
-
|
1291
1212
|
#==============================================================================#
|
1292
1213
|
# remainder()
|
1293
1214
|
#==============================================================================#
|
@@ -1298,13 +1219,13 @@ class CIDR
|
|
1298
1219
|
# and 192.168.0.128/25 will be returned as the remainders.
|
1299
1220
|
#
|
1300
1221
|
# - Arguments:
|
1301
|
-
# * CIDR address
|
1222
|
+
# * CIDR address, IPAdmin::CIDR object, or a Hash with the following fields:
|
1302
1223
|
# - :Exclude -- CIDR address or IPAdmin::CIDR object.
|
1303
1224
|
# - :Objectify -- if true, return IPAdmin::CIDR objects (optional)
|
1304
1225
|
# - :Short -- if true, return IPv6 addresses in short-hand notation (optional)
|
1305
1226
|
#
|
1306
1227
|
# - Returns:
|
1307
|
-
# * Array of
|
1228
|
+
# * Array of Strings, or Array of IPAdmin::CIDR objects
|
1308
1229
|
#
|
1309
1230
|
#
|
1310
1231
|
# Examples:
|
@@ -1381,7 +1302,7 @@ class CIDR
|
|
1381
1302
|
new_subnets.unshift("#{non_match}/#{new_mask}")
|
1382
1303
|
else
|
1383
1304
|
new_subnets.unshift(IPAdmin::CIDR.new(:PackedIP => non_match,
|
1384
|
-
:
|
1305
|
+
:PackedNetmask => IPAdmin.pack_ip_netmask(new_mask),
|
1385
1306
|
:Version => @version))
|
1386
1307
|
end
|
1387
1308
|
|
@@ -1393,11 +1314,6 @@ class CIDR
|
|
1393
1314
|
return(new_subnets)
|
1394
1315
|
end
|
1395
1316
|
|
1396
|
-
#======================================#
|
1397
|
-
#
|
1398
|
-
#======================================#
|
1399
|
-
|
1400
|
-
|
1401
1317
|
#==============================================================================#
|
1402
1318
|
# resize()
|
1403
1319
|
#==============================================================================#
|
@@ -1413,6 +1329,7 @@ class CIDR
|
|
1413
1329
|
# * IPAdmin::CIDR object
|
1414
1330
|
#
|
1415
1331
|
# Examples:
|
1332
|
+
# cidr4 = IPAdmin::CIDR.new('192.168.1.1/24')
|
1416
1333
|
# new_cidr = cidr4.resize(23)
|
1417
1334
|
# new_cidr = cidr4.resize(:Netmask => 23)
|
1418
1335
|
# puts new_cidr.desc
|
@@ -1438,11 +1355,6 @@ class CIDR
|
|
1438
1355
|
return(cidr)
|
1439
1356
|
end
|
1440
1357
|
|
1441
|
-
#======================================#
|
1442
|
-
#
|
1443
|
-
#======================================#
|
1444
|
-
|
1445
|
-
|
1446
1358
|
#==============================================================================#
|
1447
1359
|
# resize!()
|
1448
1360
|
#==============================================================================#
|
@@ -1459,10 +1371,6 @@ class CIDR
|
|
1459
1371
|
# - Notes:
|
1460
1372
|
# * If CIDR is resized such that the original IP is no longer contained within,
|
1461
1373
|
# then that IP will be reset to the base network address.
|
1462
|
-
#
|
1463
|
-
# Examples:
|
1464
|
-
# cidr4.resize!(23)
|
1465
|
-
# puts cidr4.desc
|
1466
1374
|
#
|
1467
1375
|
def resize!(options)
|
1468
1376
|
if (options.kind_of?(Hash))
|
@@ -1492,11 +1400,6 @@ class CIDR
|
|
1492
1400
|
return(true)
|
1493
1401
|
end
|
1494
1402
|
|
1495
|
-
#======================================#
|
1496
|
-
#
|
1497
|
-
#======================================#
|
1498
|
-
|
1499
|
-
|
1500
1403
|
#==============================================================================#
|
1501
1404
|
# size()
|
1502
1405
|
#==============================================================================#
|
@@ -1516,11 +1419,6 @@ class CIDR
|
|
1516
1419
|
return(@hostmask + 1)
|
1517
1420
|
end
|
1518
1421
|
|
1519
|
-
#======================================#
|
1520
|
-
#
|
1521
|
-
#======================================#
|
1522
|
-
|
1523
|
-
|
1524
1422
|
#==============================================================================#
|
1525
1423
|
# subnet()
|
1526
1424
|
#==============================================================================#
|
@@ -1530,12 +1428,12 @@ class CIDR
|
|
1530
1428
|
# * By providing the number of IP addresses needed in the new subnets in :IPCount
|
1531
1429
|
#
|
1532
1430
|
# If :Mincount is not provided, then the CIDR will be fully subnetted. Otherwise,
|
1533
|
-
#
|
1431
|
+
# :Mincount number of subnets of the requested size will be returned and
|
1534
1432
|
# the remainder of the subnets will be summarized as much as possible. If neither :Subnet
|
1535
1433
|
# or :IPCount is provided, then the current CIDR will be split in half.
|
1536
1434
|
#
|
1537
1435
|
# - Arguments:
|
1538
|
-
# * Optional
|
1436
|
+
# * Optional Hash with the following fields:
|
1539
1437
|
# - :IPCount -- Minimum number of IP's that new subnets should contain - Integer (optional)
|
1540
1438
|
# - :MinCount -- Minimum number of X sized subnets to return - Integer (optional)
|
1541
1439
|
# - :Objectify -- if true, return IPAdmin::CIDR objects (optional)
|
@@ -1543,14 +1441,16 @@ class CIDR
|
|
1543
1441
|
# - :Subnet -- Netmask (in bits) of new subnets - Integer (optional)
|
1544
1442
|
#
|
1545
1443
|
# - Returns:
|
1546
|
-
# * Array of
|
1444
|
+
# * Array of Strings, or Array of IPAdmin::CIDR objects
|
1547
1445
|
#
|
1548
1446
|
# - Notes:
|
1549
1447
|
# * :Subnet always takes precedence over :IPCount.
|
1550
1448
|
#
|
1551
1449
|
# Examples:
|
1552
|
-
# cidr_list = cidr4.subnet(:Subnet => 28, :MinCount => 3)
|
1553
|
-
# cidr_list = cidr4.subnet(:IPCount => 19)
|
1450
|
+
# cidr_list = cidr4.subnet(:Subnet => 28, :MinCount => 3).each {|x| puts " #{x}"}
|
1451
|
+
# cidr_list = cidr4.subnet(:IPCount => 19).each {|x| puts " #{x}"}
|
1452
|
+
# cidr4.subnet(:Subnet => 28).each {|x| puts " #{x}"} "
|
1453
|
+
# cidr6.subnet(:Subnet => 67, :MinCount => 4, :Short => true).each {|x| puts " #{x}"}
|
1554
1454
|
#
|
1555
1455
|
def subnet(options=nil)
|
1556
1456
|
my_network = self.packed_network
|
@@ -1647,7 +1547,7 @@ class CIDR
|
|
1647
1547
|
new_subnets.push("#{next_network}/#{next_supernet_bits}")
|
1648
1548
|
else
|
1649
1549
|
new_subnets.push(IPAdmin::CIDR.new(:PackedIP => next_supernet_ip,
|
1650
|
-
:
|
1550
|
+
:PackedNetmask => IPAdmin.pack_ip_netmask(next_supernet_bits),
|
1651
1551
|
:Version => @version))
|
1652
1552
|
end
|
1653
1553
|
|
@@ -1659,10 +1559,6 @@ class CIDR
|
|
1659
1559
|
return(new_subnets)
|
1660
1560
|
end
|
1661
1561
|
|
1662
|
-
#======================================#
|
1663
|
-
#
|
1664
|
-
#======================================#
|
1665
|
-
|
1666
1562
|
|
1667
1563
|
# PRIVATE INSTANCE METHODS
|
1668
1564
|
private
|
@@ -1713,12 +1609,7 @@ private
|
|
1713
1609
|
return(list)
|
1714
1610
|
end
|
1715
1611
|
|
1716
|
-
|
1717
|
-
#
|
1718
|
-
#======================================#
|
1719
|
-
|
1720
|
-
|
1721
|
-
end
|
1612
|
+
end # end class CIDR
|
1722
1613
|
|
1723
1614
|
end # module IPAdmin
|
1724
1615
|
__END__
|