blackwinter-ipaddress 0.8.0 → 0.8.0.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/Rakefile +2 -1
- data/VERSION +1 -1
- data/lib/ipaddress.rb +56 -26
- data/lib/ipaddress/ipv4.rb +18 -14
- data/lib/ipaddress/ipv6.rb +18 -14
- data/lib/ipaddress/prefix.rb +18 -20
- metadata +18 -14
- data/ipaddress.gemspec +0 -57
- data/lib/ipaddress/lazy.rb +0 -19
data/Rakefile
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
require 'rake'
|
|
2
2
|
require 'rake/clean'
|
|
3
3
|
require 'rake/testtask'
|
|
4
|
-
require '
|
|
4
|
+
require 'rdoc/task'
|
|
5
5
|
|
|
6
6
|
$:.unshift(File.expand_path('../lib', __FILE__))
|
|
7
7
|
require 'ipaddress'
|
|
@@ -22,6 +22,7 @@ begin
|
|
|
22
22
|
a layer of compatibility with Ruby's own IPAddr, while
|
|
23
23
|
addressing many of its issues.
|
|
24
24
|
EOD
|
|
25
|
+
gem.add_dependency 'ruby-nuggets', '>= 0.8.9'
|
|
25
26
|
end
|
|
26
27
|
rescue LoadError
|
|
27
28
|
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.8.0
|
|
1
|
+
0.8.0.1
|
data/lib/ipaddress.rb
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
#++
|
|
14
14
|
|
|
15
15
|
require 'ipaddress/conversions'
|
|
16
|
-
require '
|
|
16
|
+
require 'nuggets/util/lazy_attr'
|
|
17
17
|
|
|
18
18
|
class IPAddress
|
|
19
19
|
|
|
@@ -23,12 +23,12 @@ class IPAddress
|
|
|
23
23
|
include Conversions
|
|
24
24
|
extend Conversions
|
|
25
25
|
|
|
26
|
-
include
|
|
26
|
+
include Util::LazyAttr
|
|
27
27
|
|
|
28
|
-
VERSION = '0.8.0'
|
|
28
|
+
VERSION = '0.8.0.1'
|
|
29
29
|
NAME = 'IPAddress'
|
|
30
|
-
GEM = 'ipaddress'
|
|
31
|
-
AUTHORS = ['Marco Ceresa <ceresa@ieee.org>']
|
|
30
|
+
GEM = 'blackwinter-ipaddress'
|
|
31
|
+
AUTHORS = ['Marco Ceresa <ceresa@ieee.org>', 'Jens Wille <jens.wille@uni-koeln.de>']
|
|
32
32
|
|
|
33
33
|
class << self
|
|
34
34
|
|
|
@@ -309,7 +309,7 @@ class IPAddress
|
|
|
309
309
|
# #=> "0010000000000001000011011011100000 [...] "
|
|
310
310
|
#
|
|
311
311
|
def bits
|
|
312
|
-
|
|
312
|
+
lazy_attr(:bits) { data2bits(data) }
|
|
313
313
|
end
|
|
314
314
|
|
|
315
315
|
#
|
|
@@ -336,7 +336,7 @@ class IPAddress
|
|
|
336
336
|
# #=> true
|
|
337
337
|
#
|
|
338
338
|
def network?
|
|
339
|
-
|
|
339
|
+
lazy_attr(:network_p) { i = prefix.to_i; to_i | i == i }
|
|
340
340
|
end
|
|
341
341
|
|
|
342
342
|
#
|
|
@@ -354,7 +354,7 @@ class IPAddress
|
|
|
354
354
|
# #=> "2001:db8::/32"
|
|
355
355
|
#
|
|
356
356
|
def network
|
|
357
|
-
|
|
357
|
+
lazy_attr(:network, false) { network? ? self : at(0) }
|
|
358
358
|
end
|
|
359
359
|
|
|
360
360
|
#
|
|
@@ -371,7 +371,7 @@ class IPAddress
|
|
|
371
371
|
# #=> 42540766411282592856903984951653826560
|
|
372
372
|
#
|
|
373
373
|
def network_i
|
|
374
|
-
|
|
374
|
+
lazy_attr(:network_i) { to_i & prefix.to_i }
|
|
375
375
|
end
|
|
376
376
|
|
|
377
377
|
#
|
|
@@ -383,7 +383,7 @@ class IPAddress
|
|
|
383
383
|
# #=> "172.16.10.255"
|
|
384
384
|
#
|
|
385
385
|
def broadcast
|
|
386
|
-
|
|
386
|
+
lazy_attr(:broadcast, false) { at(-1) }
|
|
387
387
|
end
|
|
388
388
|
|
|
389
389
|
#
|
|
@@ -404,7 +404,7 @@ class IPAddress
|
|
|
404
404
|
# a helper to other functions.
|
|
405
405
|
#
|
|
406
406
|
def broadcast_i
|
|
407
|
-
|
|
407
|
+
lazy_attr(:broadcast_i) { network_i + size - 1 }
|
|
408
408
|
end
|
|
409
409
|
|
|
410
410
|
#
|
|
@@ -423,7 +423,7 @@ class IPAddress
|
|
|
423
423
|
# #=> 18446744073709551616
|
|
424
424
|
#
|
|
425
425
|
def size
|
|
426
|
-
|
|
426
|
+
lazy_attr(:size) { 2 ** prefix.host_prefix }
|
|
427
427
|
end
|
|
428
428
|
|
|
429
429
|
#
|
|
@@ -595,8 +595,9 @@ class IPAddress
|
|
|
595
595
|
#
|
|
596
596
|
# If +new_prefix+ is less than 1, returns 0.0.0.0/0
|
|
597
597
|
#
|
|
598
|
-
def supernet(num)
|
|
599
|
-
|
|
598
|
+
def supernet(num, relax = false)
|
|
599
|
+
superprefix = prefix.superprefix(num, relax)
|
|
600
|
+
new_prefix(superprefix).network if superprefix
|
|
600
601
|
end
|
|
601
602
|
|
|
602
603
|
def exact_supernet(*other)
|
|
@@ -685,16 +686,41 @@ class IPAddress
|
|
|
685
686
|
|
|
686
687
|
alias_method :+, :summarize
|
|
687
688
|
|
|
688
|
-
def range
|
|
689
|
-
|
|
689
|
+
def range(other)
|
|
690
|
+
raise TypeError, 'must be same version' if other.version != version
|
|
691
|
+
|
|
692
|
+
return [self] unless other > self
|
|
693
|
+
|
|
694
|
+
nets, n, l, m = [], to_i, other.to_i, self.class::MAX_PREFIX
|
|
695
|
+
|
|
696
|
+
until n > l
|
|
697
|
+
i = self.class.parse_i(n, m)
|
|
698
|
+
f = i.bits.rindex('1') || -1
|
|
699
|
+
|
|
700
|
+
while s = i.supernet(f += 1, true)
|
|
701
|
+
unless s.broadcast_i > l
|
|
702
|
+
i = s
|
|
703
|
+
break
|
|
704
|
+
end
|
|
705
|
+
end
|
|
706
|
+
|
|
707
|
+
nets << i
|
|
708
|
+
n = i.broadcast_i + 1
|
|
709
|
+
end
|
|
710
|
+
|
|
711
|
+
nets
|
|
690
712
|
end
|
|
691
713
|
|
|
692
|
-
def
|
|
693
|
-
|
|
714
|
+
def span
|
|
715
|
+
lazy_attr(:span) { network_i..broadcast_i }
|
|
716
|
+
end
|
|
717
|
+
|
|
718
|
+
def span_i
|
|
719
|
+
lazy_attr(:span_i) { span.to_a }
|
|
694
720
|
end
|
|
695
721
|
|
|
696
722
|
def boundaries
|
|
697
|
-
|
|
723
|
+
lazy_attr(:boundaries) { [(s = span).first, s.last] }
|
|
698
724
|
end
|
|
699
725
|
|
|
700
726
|
def each_i(first = nil, last = nil)
|
|
@@ -768,10 +794,10 @@ class IPAddress
|
|
|
768
794
|
end
|
|
769
795
|
|
|
770
796
|
def at(index)
|
|
771
|
-
|
|
797
|
+
s = span
|
|
772
798
|
|
|
773
|
-
index += index < 0 ?
|
|
774
|
-
each(index) { |i| return i } if
|
|
799
|
+
index += index < 0 ? s.last + 1 : s.first
|
|
800
|
+
each(index) { |i| return i } if s.include?(index)
|
|
775
801
|
|
|
776
802
|
nil
|
|
777
803
|
end
|
|
@@ -797,7 +823,7 @@ class IPAddress
|
|
|
797
823
|
# #=> "192.168.100.1"
|
|
798
824
|
#
|
|
799
825
|
def first
|
|
800
|
-
|
|
826
|
+
lazy_attr(:first, false) { at(1) }
|
|
801
827
|
end
|
|
802
828
|
|
|
803
829
|
#
|
|
@@ -822,7 +848,11 @@ class IPAddress
|
|
|
822
848
|
# #=> "192.168.100.254"
|
|
823
849
|
#
|
|
824
850
|
def last
|
|
825
|
-
|
|
851
|
+
lazy_attr(:last, false) { at(-2) }
|
|
852
|
+
end
|
|
853
|
+
|
|
854
|
+
def succ
|
|
855
|
+
lazy_attr(:succ, false) { self.class.parse_i(to_i + 1, prefix) }
|
|
826
856
|
end
|
|
827
857
|
|
|
828
858
|
#
|
|
@@ -840,7 +870,7 @@ class IPAddress
|
|
|
840
870
|
# #=> "10.0.0.6"]
|
|
841
871
|
#
|
|
842
872
|
def hosts
|
|
843
|
-
|
|
873
|
+
lazy_attr(:hosts) { hosts = []; each_host { |i| hosts << i }; hosts }
|
|
844
874
|
end
|
|
845
875
|
|
|
846
876
|
def mapped
|
|
@@ -900,7 +930,7 @@ class IPAddress
|
|
|
900
930
|
end
|
|
901
931
|
|
|
902
932
|
def hash
|
|
903
|
-
|
|
933
|
+
lazy_attr(:hash) { [to_i, prefix.hash].hash }
|
|
904
934
|
end
|
|
905
935
|
|
|
906
936
|
alias_method :eql?, :==
|
data/lib/ipaddress/ipv4.rb
CHANGED
|
@@ -27,7 +27,7 @@ class IPAddress
|
|
|
27
27
|
24 => /\A110/ # Class C, D and E, from 192.0.0.0 to 255.255.255.254
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
part = %r{(?: 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]\d | \d )}x
|
|
30
|
+
part = %r{ (?: 25[0-5] | 2[0-4]\d | 1\d\d | [1-9]\d | \d ) }x
|
|
31
31
|
|
|
32
32
|
INTERNAL_RE = %r{ (?: #{part} \. ){3} #{part} }xo
|
|
33
33
|
|
|
@@ -40,7 +40,7 @@ class IPAddress
|
|
|
40
40
|
|
|
41
41
|
MAX_PREFIX = Prefix::MAX
|
|
42
42
|
|
|
43
|
-
PREFIX_RE = %r{\A(?:[12]?\d|3[0-2])\z}
|
|
43
|
+
PREFIX_RE = %r{ \A (?: [12]?\d | 3[0-2] ) \z }x
|
|
44
44
|
|
|
45
45
|
class << self
|
|
46
46
|
|
|
@@ -181,6 +181,10 @@ class IPAddress
|
|
|
181
181
|
@address, @octets = ip, addr2ary(ip)
|
|
182
182
|
end
|
|
183
183
|
|
|
184
|
+
def version
|
|
185
|
+
4
|
|
186
|
+
end
|
|
187
|
+
|
|
184
188
|
#
|
|
185
189
|
# Returns the address portion of the IPv4 object
|
|
186
190
|
# as a string.
|
|
@@ -191,7 +195,7 @@ class IPAddress
|
|
|
191
195
|
# #=> "172.16.100.4"
|
|
192
196
|
#
|
|
193
197
|
def address
|
|
194
|
-
|
|
198
|
+
lazy_attr(:address) { ary2addr(octets) }
|
|
195
199
|
end
|
|
196
200
|
|
|
197
201
|
#
|
|
@@ -207,7 +211,7 @@ class IPAddress
|
|
|
207
211
|
# #=> IPAddress::Prefix32
|
|
208
212
|
#
|
|
209
213
|
def prefix
|
|
210
|
-
|
|
214
|
+
lazy_attr(:prefix, false) { Prefix.new(@_prefix) }
|
|
211
215
|
end
|
|
212
216
|
|
|
213
217
|
#
|
|
@@ -219,7 +223,7 @@ class IPAddress
|
|
|
219
223
|
# #=> "255.255.252.0"
|
|
220
224
|
#
|
|
221
225
|
def netmask
|
|
222
|
-
|
|
226
|
+
lazy_attr(:netmask) { prefix.to_ip }
|
|
223
227
|
end
|
|
224
228
|
|
|
225
229
|
#
|
|
@@ -250,7 +254,7 @@ class IPAddress
|
|
|
250
254
|
# #=> [172, 16, 100, 4]
|
|
251
255
|
#
|
|
252
256
|
def octets
|
|
253
|
-
|
|
257
|
+
lazy_attr(:octets) { int2ary(to_i) }
|
|
254
258
|
end
|
|
255
259
|
|
|
256
260
|
alias_method :groups, :octets
|
|
@@ -282,7 +286,7 @@ class IPAddress
|
|
|
282
286
|
# #=> 167772160
|
|
283
287
|
#
|
|
284
288
|
def to_i
|
|
285
|
-
|
|
289
|
+
lazy_attr(:int) { ary2int(octets) }
|
|
286
290
|
end
|
|
287
291
|
|
|
288
292
|
alias_method :u32, :to_i
|
|
@@ -308,7 +312,7 @@ class IPAddress
|
|
|
308
312
|
# a.puts binary_data
|
|
309
313
|
#
|
|
310
314
|
def data
|
|
311
|
-
|
|
315
|
+
lazy_attr(:data) { int2data(to_i) }
|
|
312
316
|
end
|
|
313
317
|
|
|
314
318
|
#
|
|
@@ -346,7 +350,7 @@ class IPAddress
|
|
|
346
350
|
# #=> "50.100.16.172.in-addr.arpa"
|
|
347
351
|
#
|
|
348
352
|
def reverse
|
|
349
|
-
|
|
353
|
+
lazy_attr(:reverse) { "#{ary2addr(octets.reverse)}.in-addr.arpa" }
|
|
350
354
|
end
|
|
351
355
|
|
|
352
356
|
alias_method :arpa, :reverse
|
|
@@ -362,7 +366,7 @@ class IPAddress
|
|
|
362
366
|
# #=> true
|
|
363
367
|
#
|
|
364
368
|
def private?
|
|
365
|
-
|
|
369
|
+
lazy_attr(:private_p) { self.class.private_nets.any? { |i| i.include?(self) } }
|
|
366
370
|
end
|
|
367
371
|
|
|
368
372
|
#
|
|
@@ -378,7 +382,7 @@ class IPAddress
|
|
|
378
382
|
# #=> true
|
|
379
383
|
#
|
|
380
384
|
def a?
|
|
381
|
-
|
|
385
|
+
lazy_attr(:a_p) { CLASSFUL[8] === bits }
|
|
382
386
|
end
|
|
383
387
|
|
|
384
388
|
#
|
|
@@ -394,7 +398,7 @@ class IPAddress
|
|
|
394
398
|
# #=> true
|
|
395
399
|
#
|
|
396
400
|
def b?
|
|
397
|
-
|
|
401
|
+
lazy_attr(:b_p) { CLASSFUL[16] === bits }
|
|
398
402
|
end
|
|
399
403
|
|
|
400
404
|
#
|
|
@@ -410,7 +414,7 @@ class IPAddress
|
|
|
410
414
|
# #=> true
|
|
411
415
|
#
|
|
412
416
|
def c?
|
|
413
|
-
|
|
417
|
+
lazy_attr(:c_p) { CLASSFUL[24] === bits }
|
|
414
418
|
end
|
|
415
419
|
|
|
416
420
|
#
|
|
@@ -425,7 +429,7 @@ class IPAddress
|
|
|
425
429
|
# #=> "ac10:0a01"
|
|
426
430
|
#
|
|
427
431
|
def to_ipv6
|
|
428
|
-
|
|
432
|
+
lazy_attr(:to_ipv6) { '%.4x:%.4x' % int2data(to_i).unpack('n2') }
|
|
429
433
|
end
|
|
430
434
|
|
|
431
435
|
end
|
data/lib/ipaddress/ipv6.rb
CHANGED
|
@@ -75,7 +75,7 @@ class IPAddress
|
|
|
75
75
|
#
|
|
76
76
|
# Format string to pretty print IPv6 addresses
|
|
77
77
|
#
|
|
78
|
-
IN6FORMAT = ('%.4x:'
|
|
78
|
+
IN6FORMAT = Array.new(8, '%.4x').join(':')
|
|
79
79
|
|
|
80
80
|
Prefix = Prefix128
|
|
81
81
|
|
|
@@ -205,6 +205,10 @@ class IPAddress
|
|
|
205
205
|
end
|
|
206
206
|
end
|
|
207
207
|
|
|
208
|
+
def version
|
|
209
|
+
6
|
|
210
|
+
end
|
|
211
|
+
|
|
208
212
|
#
|
|
209
213
|
# Returns the IPv6 address in uncompressed form:
|
|
210
214
|
#
|
|
@@ -214,7 +218,7 @@ class IPAddress
|
|
|
214
218
|
# #=> "2001:0db8:0000:0000:0008:0800:200c:417a"
|
|
215
219
|
#
|
|
216
220
|
def address
|
|
217
|
-
|
|
221
|
+
lazy_attr(:address) { IN6FORMAT % groups }
|
|
218
222
|
end
|
|
219
223
|
|
|
220
224
|
#
|
|
@@ -226,7 +230,7 @@ class IPAddress
|
|
|
226
230
|
# #=> "2001:db8::8:800:200c:417a"
|
|
227
231
|
#
|
|
228
232
|
def compressed
|
|
229
|
-
|
|
233
|
+
lazy_attr(:compressed) {
|
|
230
234
|
r1, r2, q = /\b0(?::0)+\b/, /:{3,}/, '::'
|
|
231
235
|
|
|
232
236
|
a, b = [s = groups.map { |i| i.to_s(16) }.join(':'), s.reverse].map! { |t|
|
|
@@ -246,7 +250,7 @@ class IPAddress
|
|
|
246
250
|
# #=> 64
|
|
247
251
|
#
|
|
248
252
|
def prefix
|
|
249
|
-
|
|
253
|
+
lazy_attr(:prefix, false) { Prefix.new(@netmask) }
|
|
250
254
|
end
|
|
251
255
|
|
|
252
256
|
#
|
|
@@ -259,7 +263,7 @@ class IPAddress
|
|
|
259
263
|
# #=> [8193, 3512, 0, 0, 8, 2048, 8204, 16762]
|
|
260
264
|
#
|
|
261
265
|
def groups
|
|
262
|
-
|
|
266
|
+
lazy_attr(:groups) { self.class.groups(@ip) }
|
|
263
267
|
end
|
|
264
268
|
|
|
265
269
|
#
|
|
@@ -274,7 +278,7 @@ class IPAddress
|
|
|
274
278
|
# Not to be confused with the similar IPv6#to_hex method.
|
|
275
279
|
#
|
|
276
280
|
def hexs
|
|
277
|
-
|
|
281
|
+
lazy_attr(:hexs) { address.split(':') }
|
|
278
282
|
end
|
|
279
283
|
|
|
280
284
|
#
|
|
@@ -287,7 +291,7 @@ class IPAddress
|
|
|
287
291
|
# #=> "20010db80000000000080800200c417a"
|
|
288
292
|
#
|
|
289
293
|
def to_hex
|
|
290
|
-
|
|
294
|
+
lazy_attr(:to_hex) { hexs.join('') }
|
|
291
295
|
end
|
|
292
296
|
|
|
293
297
|
#
|
|
@@ -326,7 +330,7 @@ class IPAddress
|
|
|
326
330
|
# #=> 42540766411282592856906245548098208122
|
|
327
331
|
#
|
|
328
332
|
def to_i
|
|
329
|
-
|
|
333
|
+
lazy_attr(:int) { to_hex.hex }
|
|
330
334
|
end
|
|
331
335
|
|
|
332
336
|
alias_method :u128, :to_i
|
|
@@ -351,7 +355,7 @@ class IPAddress
|
|
|
351
355
|
# a.puts binary_data
|
|
352
356
|
#
|
|
353
357
|
def data
|
|
354
|
-
|
|
358
|
+
lazy_attr(:data) { groups.pack('n8') }
|
|
355
359
|
end
|
|
356
360
|
|
|
357
361
|
#
|
|
@@ -388,7 +392,7 @@ class IPAddress
|
|
|
388
392
|
# #=> "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa"
|
|
389
393
|
#
|
|
390
394
|
def reverse
|
|
391
|
-
|
|
395
|
+
lazy_attr(:reverse) { "#{to_hex.gsub(/(?=.)/, '.').reverse}ip6.arpa" }
|
|
392
396
|
end
|
|
393
397
|
|
|
394
398
|
alias_method :arpa, :reverse
|
|
@@ -402,7 +406,7 @@ class IPAddress
|
|
|
402
406
|
# #=> "2001-0db8-0000-0000-0008-0800-200c-417a.ipv6-literal.net"
|
|
403
407
|
#
|
|
404
408
|
def literal
|
|
405
|
-
|
|
409
|
+
lazy_attr(:literal) { "#{address.tr(':', '-')}.ipv6-literal.net" }
|
|
406
410
|
end
|
|
407
411
|
|
|
408
412
|
#
|
|
@@ -411,7 +415,7 @@ class IPAddress
|
|
|
411
415
|
# See IPAddress::IPv6::Unspecified for more information
|
|
412
416
|
#
|
|
413
417
|
def unspecified?
|
|
414
|
-
|
|
418
|
+
lazy_attr(:unspecified_p) { prefix.max? && compressed == '::' }
|
|
415
419
|
end
|
|
416
420
|
|
|
417
421
|
#
|
|
@@ -420,7 +424,7 @@ class IPAddress
|
|
|
420
424
|
# See IPAddress::IPv6::Loopback for more information
|
|
421
425
|
#
|
|
422
426
|
def loopback?
|
|
423
|
-
|
|
427
|
+
lazy_attr(:loopback_p) { prefix.max? && compressed == '::1' }
|
|
424
428
|
end
|
|
425
429
|
|
|
426
430
|
#
|
|
@@ -429,7 +433,7 @@ class IPAddress
|
|
|
429
433
|
# See IPAddress::IPv6::Mapped for more information
|
|
430
434
|
#
|
|
431
435
|
def mapped?
|
|
432
|
-
|
|
436
|
+
lazy_attr(:mapped_p) { to_i >> 32 == 0xffff }
|
|
433
437
|
end
|
|
434
438
|
|
|
435
439
|
#
|
data/lib/ipaddress/prefix.rb
CHANGED
|
@@ -25,7 +25,7 @@ class IPAddress
|
|
|
25
25
|
include Conversions
|
|
26
26
|
extend Conversions
|
|
27
27
|
|
|
28
|
-
include
|
|
28
|
+
include Util::LazyAttr
|
|
29
29
|
|
|
30
30
|
#
|
|
31
31
|
# Creates a new prefix object for 32 bits IPv4 addresses /
|
|
@@ -74,7 +74,7 @@ class IPAddress
|
|
|
74
74
|
# "0000000000000000000000000000000000000000000000000000000000000000"
|
|
75
75
|
#
|
|
76
76
|
def bits
|
|
77
|
-
|
|
77
|
+
lazy_attr(:bits) { '1' * prefix << '0' * host_prefix }
|
|
78
78
|
end
|
|
79
79
|
|
|
80
80
|
#
|
|
@@ -92,7 +92,7 @@ class IPAddress
|
|
|
92
92
|
# #=> 340282366920938463444927863358058659840
|
|
93
93
|
#
|
|
94
94
|
def to_i
|
|
95
|
-
|
|
95
|
+
lazy_attr(:int) { bits.to_i(2) }
|
|
96
96
|
end
|
|
97
97
|
|
|
98
98
|
#
|
|
@@ -140,39 +140,37 @@ class IPAddress
|
|
|
140
140
|
# #=> 32
|
|
141
141
|
#
|
|
142
142
|
def host_prefix
|
|
143
|
-
|
|
143
|
+
lazy_attr(:host_prefix) { max - prefix }
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
def max
|
|
147
|
-
|
|
147
|
+
lazy_attr(:max) { self.class::MAX }
|
|
148
148
|
end
|
|
149
149
|
|
|
150
150
|
def max?
|
|
151
|
-
|
|
151
|
+
lazy_attr(:max_p) { prefix == max }
|
|
152
152
|
end
|
|
153
153
|
|
|
154
154
|
def prev
|
|
155
|
-
|
|
155
|
+
lazy_attr(:prev) { prefix - 1 }
|
|
156
156
|
end
|
|
157
157
|
|
|
158
158
|
def next
|
|
159
|
-
|
|
159
|
+
lazy_attr(:next) { prefix + 1 unless max? }
|
|
160
160
|
end
|
|
161
161
|
|
|
162
|
-
def superprefix(num)
|
|
163
|
-
validate_prefix(
|
|
164
|
-
num
|
|
162
|
+
def superprefix(num, relax = false)
|
|
163
|
+
validate_prefix([0, num].max, nil, prev, relax) or return
|
|
165
164
|
end
|
|
166
165
|
|
|
167
|
-
def subprefix(num)
|
|
168
|
-
validate_prefix(num, prefix)
|
|
166
|
+
def subprefix(num, relax = false)
|
|
167
|
+
validate_prefix(num, prefix, relax) or return
|
|
169
168
|
[2 ** (max - num), 2 ** (num - prefix)]
|
|
170
169
|
end
|
|
171
170
|
|
|
172
|
-
def validate_prefix(num, first = nil, last = nil)
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
end
|
|
171
|
+
def validate_prefix(num, first = nil, last = nil, relax = false)
|
|
172
|
+
return num if (range = (first || 0)..(last || max)).include?(num)
|
|
173
|
+
raise ArgumentError, "Prefix must be in range #{range}, got: #{num}" unless relax
|
|
176
174
|
end
|
|
177
175
|
|
|
178
176
|
end
|
|
@@ -209,7 +207,7 @@ class IPAddress
|
|
|
209
207
|
# #=> "255.255.255.0"
|
|
210
208
|
#
|
|
211
209
|
def to_ip
|
|
212
|
-
|
|
210
|
+
lazy_attr(:ip) { bits2addr(bits) }
|
|
213
211
|
end
|
|
214
212
|
|
|
215
213
|
#
|
|
@@ -222,7 +220,7 @@ class IPAddress
|
|
|
222
220
|
# #=> [255, 255, 255, 0]
|
|
223
221
|
#
|
|
224
222
|
def octets
|
|
225
|
-
|
|
223
|
+
lazy_attr(:octets) { addr2ary(to_ip) }
|
|
226
224
|
end
|
|
227
225
|
|
|
228
226
|
#
|
|
@@ -251,7 +249,7 @@ class IPAddress
|
|
|
251
249
|
# #=> "0.0.0.255"
|
|
252
250
|
#
|
|
253
251
|
def hostmask
|
|
254
|
-
|
|
252
|
+
lazy_attr(:hostmask) { int2addr(~to_i) }
|
|
255
253
|
end
|
|
256
254
|
|
|
257
255
|
end
|
metadata
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: blackwinter-ipaddress
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.8.0
|
|
4
|
+
version: 0.8.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marco Ceresa
|
|
8
|
-
- Jens Wille
|
|
9
8
|
autorequire:
|
|
10
9
|
bindir: bin
|
|
11
10
|
cert_chain: []
|
|
12
11
|
|
|
13
|
-
date: 2012-
|
|
12
|
+
date: 2012-11-30 00:00:00 +01:00
|
|
14
13
|
default_executable:
|
|
15
|
-
dependencies:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
dependencies:
|
|
15
|
+
- !ruby/object:Gem::Dependency
|
|
16
|
+
name: ruby-nuggets
|
|
17
|
+
type: :runtime
|
|
18
|
+
version_requirement:
|
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
20
|
+
requirements:
|
|
21
|
+
- - ">="
|
|
22
|
+
- !ruby/object:Gem::Version
|
|
23
|
+
version: 0.8.9
|
|
24
|
+
version:
|
|
25
|
+
description: " IPAddress is a Ruby library designed to make manipulation\n of IPv4 and IPv6 addresses both powerful and simple. It mantains\n a layer of compatibility with Ruby's own IPAddr, while\n addressing many of its issues.\n"
|
|
26
|
+
email: ceresa@gmail.com
|
|
21
27
|
executables: []
|
|
22
28
|
|
|
23
29
|
extensions: []
|
|
@@ -32,20 +38,18 @@ files:
|
|
|
32
38
|
- README.rdoc
|
|
33
39
|
- Rakefile
|
|
34
40
|
- VERSION
|
|
35
|
-
- ipaddress.gemspec
|
|
36
41
|
- lib/ipaddress.rb
|
|
42
|
+
- lib/ipaddress/conversions.rb
|
|
37
43
|
- lib/ipaddress/ipv4.rb
|
|
38
44
|
- lib/ipaddress/ipv6.rb
|
|
39
45
|
- lib/ipaddress/prefix.rb
|
|
40
|
-
- lib/ipaddress/conversions.rb
|
|
41
|
-
- lib/ipaddress/lazy.rb
|
|
42
46
|
- test/ipaddress/ipv4_test.rb
|
|
43
47
|
- test/ipaddress/ipv6_test.rb
|
|
44
48
|
- test/ipaddress/prefix_test.rb
|
|
45
49
|
- test/ipaddress_test.rb
|
|
46
50
|
- test/test_helper.rb
|
|
47
51
|
has_rdoc: true
|
|
48
|
-
homepage:
|
|
52
|
+
homepage: http://github.com/bluemonk/ipaddress
|
|
49
53
|
licenses: []
|
|
50
54
|
|
|
51
55
|
post_install_message:
|
|
@@ -71,6 +75,6 @@ rubyforge_project:
|
|
|
71
75
|
rubygems_version: 1.3.5
|
|
72
76
|
signing_key:
|
|
73
77
|
specification_version: 3
|
|
74
|
-
summary: IPv4/IPv6 addresses manipulation library
|
|
78
|
+
summary: IPv4/IPv6 addresses manipulation library
|
|
75
79
|
test_files: []
|
|
76
80
|
|
data/ipaddress.gemspec
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
# Generated by jeweler
|
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
|
-
# -*- encoding: utf-8 -*-
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |s|
|
|
7
|
-
s.name = %q{blackwinter-ipaddress}
|
|
8
|
-
s.version = "0.8.0"
|
|
9
|
-
|
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
|
-
s.authors = ['Marco Ceresa', 'Jens Wille']
|
|
12
|
-
s.date = %q{2012-07-20}
|
|
13
|
-
s.description = %q{ IPAddress is a Ruby library designed to make manipulation
|
|
14
|
-
of IPv4 and IPv6 addresses both powerful and simple. It mantains
|
|
15
|
-
a layer of compatibility with Ruby's own IPAddr, while
|
|
16
|
-
addressing many of its issues.
|
|
17
|
-
}
|
|
18
|
-
s.email = %w[ceresa@gmail.com jens.wille@uni-koeln.de]
|
|
19
|
-
s.extra_rdoc_files = [
|
|
20
|
-
"LICENSE",
|
|
21
|
-
"README.rdoc"
|
|
22
|
-
]
|
|
23
|
-
s.files = [
|
|
24
|
-
".document",
|
|
25
|
-
"CHANGELOG.rdoc",
|
|
26
|
-
"LICENSE",
|
|
27
|
-
"README.rdoc",
|
|
28
|
-
"Rakefile",
|
|
29
|
-
"VERSION",
|
|
30
|
-
"ipaddress.gemspec",
|
|
31
|
-
"lib/ipaddress.rb",
|
|
32
|
-
"lib/ipaddress/ipv4.rb",
|
|
33
|
-
"lib/ipaddress/ipv6.rb",
|
|
34
|
-
"lib/ipaddress/prefix.rb",
|
|
35
|
-
"lib/ipaddress/conversions.rb",
|
|
36
|
-
"lib/ipaddress/lazy.rb",
|
|
37
|
-
"test/ipaddress/ipv4_test.rb",
|
|
38
|
-
"test/ipaddress/ipv6_test.rb",
|
|
39
|
-
"test/ipaddress/prefix_test.rb",
|
|
40
|
-
"test/ipaddress_test.rb",
|
|
41
|
-
"test/test_helper.rb"
|
|
42
|
-
]
|
|
43
|
-
s.homepage = %q{https://github.com/blackwinter/ipaddress}
|
|
44
|
-
s.require_paths = ["lib"]
|
|
45
|
-
s.rubygems_version = %q{1.6.2}
|
|
46
|
-
s.summary = %q{IPv4/IPv6 addresses manipulation library [Incompatible fork]}
|
|
47
|
-
|
|
48
|
-
if s.respond_to? :specification_version then
|
|
49
|
-
s.specification_version = 3
|
|
50
|
-
|
|
51
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
52
|
-
else
|
|
53
|
-
end
|
|
54
|
-
else
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
data/lib/ipaddress/lazy.rb
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
class IPAddress
|
|
2
|
-
|
|
3
|
-
module Lazy
|
|
4
|
-
|
|
5
|
-
private
|
|
6
|
-
|
|
7
|
-
def lazy(attr, freeze = true)
|
|
8
|
-
class << self; self; end.class_eval { attr_reader attr }
|
|
9
|
-
|
|
10
|
-
value =
|
|
11
|
-
instance_variable_get(name = "@#{attr}") ||
|
|
12
|
-
instance_variable_set(name, yield)
|
|
13
|
-
|
|
14
|
-
freeze ? value.freeze : value
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
end
|