ipaddress 0.7.0 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,11 @@
1
- == master
1
+ == ipaddress 0.7.5
2
+
3
+ CHANGED:: IPAddress::IPv4#each_host to improve speed
4
+ FIXED:: IPAddress::IPv4::summarize bug (summarization should now work properly)
5
+ NEW:: IPAddress::IPv4#include_all?
6
+ NEW:: #ipv4? and #ipv6?
7
+
8
+ == ipaddress 0.7.0
2
9
 
3
10
  NEW:: IPAddress::IPv6#include?
4
11
  NEW:: IPAddress::IPv6#network_u128
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009 Marco Ceresa
1
+ Copyright (c) 2009-2011 Marco Ceresa
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -81,7 +81,7 @@ documentation with Rake:
81
81
  ipaddress$ rake rdoc
82
82
 
83
83
  The latest documentation can be found online at
84
- {this address}[http://rubydoc.info/github/bluemonk/ipaddress/master/frames]
84
+ {this address}[http://rubydoc.info/gems/ipaddress/0.7.0/frames]
85
85
 
86
86
  == IPv4
87
87
 
@@ -108,7 +108,7 @@ or, in a easier way, using the IPAddress parse method
108
108
  which accepts and parses any kind of IP (IPv4, IPV6 and
109
109
  IPv4 IPv6 Mapped addresses).
110
110
 
111
- If you like sintactic sugar, you can use the wrapper method
111
+ If you like syntactic sugar, you can use the wrapper method
112
112
  IPAddress(), which is built around IPAddress::parse:
113
113
 
114
114
  ip = IPAddress "172.16.10.1/24"
@@ -246,7 +246,7 @@ address has all one's. For example, ip "172.16.10.1/24" has broadcast
246
246
  "172.16.10.255/24", where ip "172.16.10.1/16" has broadcast
247
247
  "172.16.255.255/16".
248
248
 
249
- Method IPv4#broadcast has the same behaviour as is #network
249
+ Method IPv4#broadcast has the same behavior as is #network
250
250
  counterpart: it creates a new IPv4 object to handle the broadcast
251
251
  address:
252
252
 
@@ -277,7 +277,7 @@ It is important to note that it doesn't matter if the original IP is a
277
277
  host IP or a network number (or a broadcast address): the #each method
278
278
  only considers the range that the original IP specifies.
279
279
 
280
- If you only want to iterate over hosts IP, use the Ipv4#each_host
280
+ If you only want to iterate over hosts IP, use the IPv4#each_host
281
281
  method:
282
282
 
283
283
  ip = IPAddress "172.16.10.1/24"
@@ -327,7 +327,7 @@ the IPv4#to_u32 method
327
327
 
328
328
  This method is the equivalent of the Unix call pton(), expressing an
329
329
  IP address in the so called +network byte order+ notation. However, if
330
- you want to trasmit your IP over a network socket, you might need to
330
+ you want to transmit your IP over a network socket, you might need to
331
331
  transform it in data format using the IPv4#data method:
332
332
 
333
333
  ip.data
@@ -382,7 +382,7 @@ You can easily check which CLASSFUL network an IPv4 object belongs:
382
382
  #=> true
383
383
 
384
384
  Remember that these methods are only checking the address portion of an IP, and are
385
- indipendent from its prefix, as classful networks have no concept of prefix.
385
+ independent from its prefix, as classful networks have no concept of prefix.
386
386
 
387
387
  For more information on CLASSFUL networks visit the
388
388
  {Wikipedia page}[http://en.wikipedia.org/wiki/Classful_network]
@@ -430,7 +430,7 @@ example:
430
430
 
431
431
  Usually, subnetting implies dividing a network to a number of subnets
432
432
  which is a power of two: in this way, you can be sure that the network
433
- will be divived evenly, and all the subnets will have the same number
433
+ will be divided evenly, and all the subnets will have the same number
434
434
  of hosts.
435
435
 
436
436
  ==== Uneven subnetting
@@ -460,7 +460,7 @@ We can go even further and divide into 11 subnets:
460
460
  "172.16.10.160/27", "172.16.10.192/26"]
461
461
 
462
462
  As you can see, most of the networks are /28, with a few /27 and one
463
- /26 to fill up the remaning space.
463
+ /26 to fill up the remaining space.
464
464
 
465
465
  ==== Summarization
466
466
 
@@ -569,7 +569,7 @@ our projects.
569
569
  IPv6 addresses are 128 bits long, in contrast with IPv4 addresses
570
570
  which are only 32 bits long. An IPv6 address is generally written as
571
571
  eight groups of four hexadecimal digits, each group representing 16
572
- bits or two octect. For example, the following is a valid IPv6
572
+ bits or two octet. For example, the following is a valid IPv6
573
573
  address:
574
574
 
575
575
  1080:0000:0000:0000:0008:0800:200c:417a
@@ -581,7 +581,7 @@ they will be converted.
581
581
  ==== Compression
582
582
 
583
583
  Since IPv6 addresses are very long to write, there are some
584
- semplifications and compressions that you can use to shorten them.
584
+ simplifications and compressions that you can use to shorten them.
585
585
 
586
586
  * Leading zeroes: all the leading zeroes within a group can be
587
587
  omitted: "0008" would become "8"
@@ -765,7 +765,7 @@ Finally, a new IPv6 address can be created from an hex string:
765
765
  === Special IPv6 addresses
766
766
 
767
767
  Some IPv6 have a special meaning and are expressed in a special form,
768
- quite different than an usual IPv6 address. IPAddress has builtin
768
+ quite different than an usual IPv6 address. IPAddress has built-in
769
769
  support for unspecified, loopback and mapped IPv6 addresses.
770
770
 
771
771
  ==== Unspecified address
@@ -921,14 +921,15 @@ Feel free to join us and tell us what you think!
921
921
 
922
922
  == Thanks to
923
923
 
924
- Thanks to Luca Russo (vargolo) and Simone Carletti (weppos) for all
925
- the support and technical review. Thanks to Marco Beri, Bryan T. Richardson,
926
- Nicolas Fevrier, jdpace, Daniele Alessandri and Steve Rawlinson for
927
- their support, feedback and bug reports.
924
+ Thanks to Luca Russo (vargolo) and Simone Carletti
925
+ (weppos) for all the support and technical review. Thanks to Marco Beri,
926
+ Bryan T. Richardson, Nicolas Fevrier, jdpace, Daniele Alessandri, jrdioko,
927
+ Ghislain Charrier, Pawel Krzesniak, Mark Sullivan, Erik Ahlström and
928
+ Steve Rawlinson for their support, feedback and bug reports.
928
929
 
929
930
  == Copyright
930
931
 
931
- Copyright (c) 2009-2010 Marco Ceresa. See LICENSE for details.
932
+ Copyright (c) 2009-2011 Marco Ceresa. See LICENSE for details.
932
933
 
933
934
 
934
935
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.7.5
@@ -22,7 +22,7 @@ module IPAddress
22
22
  NAME = "IPAddress"
23
23
  GEM = "ipaddress"
24
24
  AUTHORS = ["Marco Ceresa <ceresa@ieee.org>"]
25
-
25
+
26
26
  #
27
27
  # Parse the argument string to create a new
28
28
  # IPv4, IPv6 or Mapped IP object
@@ -50,6 +50,30 @@ module IPAddress
50
50
  end
51
51
  end
52
52
 
53
+ #
54
+ # True if the object is an IPv4 address
55
+ #
56
+ # ip = IPAddress("192.168.10.100/24")
57
+ #
58
+ # ip.ipv4?
59
+ # #-> true
60
+ #
61
+ def ipv4?
62
+ self.kind_of? IPAddress::IPv4
63
+ end
64
+
65
+ #
66
+ # True if the object is an IPv6 address
67
+ #
68
+ # ip = IPAddress("192.168.10.100/24")
69
+ #
70
+ # ip.ipv6?
71
+ # #-> false
72
+ #
73
+ def ipv6?
74
+ self.kind_of? IPAddress::IPv6
75
+ end
76
+
53
77
  #
54
78
  # Checks if the given string is a valid IP address,
55
79
  # either IPv4 or IPv6
@@ -382,7 +382,7 @@ module IPAddress;
382
382
  #
383
383
  # ip = IPAddress("10.0.0.1/29")
384
384
  #
385
- # ip.each do |i|
385
+ # ip.each_host do |i|
386
386
  # p i.to_s
387
387
  # end
388
388
  # #=> "10.0.0.1"
@@ -393,8 +393,8 @@ module IPAddress;
393
393
  # #=> "10.0.0.6"
394
394
  #
395
395
  def each_host
396
- hosts.each do |i|
397
- yield i
396
+ (network_u32+1..broadcast_u32-1).each do |i|
397
+ yield self.class.parse_u32(i, @prefix)
398
398
  end
399
399
  end
400
400
 
@@ -520,8 +520,7 @@ module IPAddress;
520
520
  #
521
521
  # Checks whether a subnet includes the given IP address.
522
522
  #
523
- # Accepts either string with the IP or and IPAddress::IPv4
524
- # object.
523
+ # Accepts an IPAddress::IPv4 object.
525
524
  #
526
525
  # ip = IPAddress("192.168.10.100/24")
527
526
  #
@@ -537,6 +536,46 @@ module IPAddress;
537
536
  @prefix <= oth.prefix and network_u32 == (oth.to_u32 & @prefix.to_u32)
538
537
  end
539
538
 
539
+ #
540
+ # Checks whether a subnet includes all the
541
+ # given IPv4 objects.
542
+ #
543
+ # ip = IPAddress("192.168.10.100/24")
544
+ #
545
+ # addr1 = IPAddress("192.168.10.102/24")
546
+ # addr2 = IPAddress("192.168.10.103/24")
547
+ #
548
+ # ip.include_all?(addr1,addr2)
549
+ # #=> true
550
+ #
551
+ def include_all?(*others)
552
+ others.all? {|oth| include?(oth)}
553
+ end
554
+
555
+ #
556
+ # True if the object is an IPv4 address
557
+ #
558
+ # ip = IPAddress("192.168.10.100/24")
559
+ #
560
+ # ip.ipv4?
561
+ # #-> true
562
+ #
563
+ # def ipv4?
564
+ # true
565
+ # end
566
+
567
+ #
568
+ # True if the object is an IPv6 address
569
+ #
570
+ # ip = IPAddress("192.168.10.100/24")
571
+ #
572
+ # ip.ipv6?
573
+ # #-> false
574
+ #
575
+ # def ipv6?
576
+ # false
577
+ # end
578
+
540
579
  #
541
580
  # Checks if an IPv4 address objects belongs
542
581
  # to a private network RFC1918
@@ -943,20 +982,15 @@ module IPAddress;
943
982
  end
944
983
 
945
984
  def aggregate(ip1,ip2)
946
- if ip1.include? ip2
947
- return [ip1]
985
+ return [ip1] if ip1.include? ip2
986
+
987
+ snet = ip1.supernet(ip1.prefix-1)
988
+ if snet.include_all?(ip1, ip2) && ((ip1.size + ip2.size) == snet.size)
989
+ return [snet]
948
990
  else
949
- snet = ip1.supernet(ip1.prefix-1)
950
- arr1 = ip1.subnet(2**(ip2.prefix-ip1.prefix)).map{|i| i.to_string}
951
- arr2 = snet.subnet(2**(ip2.prefix-snet.prefix)).map{|i| i.to_string}
952
- if (arr2 - [ip2.to_string] - arr1).empty?
953
- return [snet]
954
- else
955
- return [ip1, ip2]
956
- end
991
+ return [ip1, ip2]
957
992
  end
958
993
  end
959
-
960
994
  end # class IPv4
961
995
  end # module IPAddress
962
996
 
@@ -92,8 +92,8 @@ module IPAddress
92
92
  # #=> 24
93
93
  #
94
94
  def initialize(num)
95
- unless (1..32).include? num
96
- raise ArgumentError, "Prefix must be in range 1..32, got: #{num}"
95
+ unless (0..32).include? num
96
+ raise ArgumentError, "Prefix must be in range 0..32, got: #{num}"
97
97
  end
98
98
  super(num)
99
99
  end
@@ -121,7 +121,7 @@ module IPAddress
121
121
  # #=> "11111111111111111111111100000000"
122
122
  #
123
123
  def bits
124
- to_u32.to_s(2)
124
+ "%.32b" % to_u32
125
125
  end
126
126
 
127
127
  #
@@ -6,6 +6,7 @@ class IPv4Test < Test::Unit::TestCase
6
6
  @klass = IPAddress::IPv4
7
7
 
8
8
  @valid_ipv4 = {
9
+ "0.0.0.0/0" => ["0.0.0.0", 0],
9
10
  "10.0.0.0" => ["10.0.0.0", 32],
10
11
  "10.0.0.1" => ["10.0.0.1", 32],
11
12
  "10.0.0.1/24" => ["10.0.0.1", 24],
@@ -21,12 +22,14 @@ class IPv4Test < Test::Unit::TestCase
21
22
  "10.1-254.0.0"]
22
23
 
23
24
  @netmask_values = {
25
+ "0.0.0.0/0" => "0.0.0.0",
24
26
  "10.0.0.0/8" => "255.0.0.0",
25
27
  "172.16.0.0/16" => "255.255.0.0",
26
28
  "192.168.0.0/24" => "255.255.255.0",
27
29
  "192.168.100.4/30" => "255.255.255.252"}
28
30
 
29
31
  @decimal_values ={
32
+ "0.0.0.0/0" => 0,
30
33
  "10.0.0.0/8" => 167772160,
31
34
  "172.16.0.0/16" => 2886729728,
32
35
  "192.168.0.0/24" => 3232235520,
@@ -237,6 +240,22 @@ class IPv4Test < Test::Unit::TestCase
237
240
  assert_equal false, ip.include?(@klass.new("13.16.0.0/32"))
238
241
  end
239
242
 
243
+ def test_method_include_all?
244
+ ip = @klass.new("192.168.10.100/24")
245
+ addr1 = @klass.new("192.168.10.102/24")
246
+ addr2 = @klass.new("192.168.10.103/24")
247
+ assert_equal true, ip.include_all?(addr1,addr2)
248
+ assert_equal false, ip.include_all?(addr1, @klass.new("13.16.0.0/32"))
249
+ end
250
+
251
+ def test_method_ipv4?
252
+ assert_equal true, @ip.ipv4?
253
+ end
254
+
255
+ def test_method_ipv6?
256
+ assert_equal false, @ip.ipv6?
257
+ end
258
+
240
259
  def test_method_private?
241
260
  assert_equal true, @klass.new("192.168.10.50/24").private?
242
261
  assert_equal true, @klass.new("192.168.10.50/16").private?
@@ -453,6 +472,21 @@ class IPv4Test < Test::Unit::TestCase
453
472
  result = ["10.0.1.0/24","10.10.2.0/24","172.16.0.0/23"]
454
473
  assert_equal result, @klass.summarize(ip1,ip2,ip3,ip4).map{|i| i.to_string}
455
474
 
475
+ ips = [@klass.new("10.0.0.12/30"),
476
+ @klass.new("10.0.100.0/24")]
477
+ result = ["10.0.0.12/30", "10.0.100.0/24"]
478
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
479
+
480
+ ips = [@klass.new("172.16.0.0/31"),
481
+ @klass.new("10.10.2.1/32")]
482
+ result = ["10.10.2.1/32", "172.16.0.0/31"]
483
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
484
+
485
+ ips = [@klass.new("172.16.0.0/32"),
486
+ @klass.new("10.10.2.1/32")]
487
+ result = ["10.10.2.1/32", "172.16.0.0/32"]
488
+ assert_equal result, @klass.summarize(*ips).map{|i| i.to_string}
489
+
456
490
  end
457
491
 
458
492
  def test_classmethod_parse_data
@@ -106,6 +106,14 @@ class IPv6Test < Test::Unit::TestCase
106
106
  end
107
107
  end
108
108
 
109
+ def test_method_ipv4?
110
+ assert_equal false, @ip.ipv4?
111
+ end
112
+
113
+ def test_method_ipv6?
114
+ assert_equal true, @ip.ipv6?
115
+ end
116
+
109
117
  def test_method_network?
110
118
  assert_equal true, @network.network?
111
119
  assert_equal false, @ip.network?
@@ -197,22 +205,6 @@ class IPv6Test < Test::Unit::TestCase
197
205
  assert_not_equal compressed, @klass.compress("2001:0db8::cd3")
198
206
  end
199
207
 
200
- # def test_classmethod_create_unpecified
201
- # unspec = @klass.create_unspecified
202
- # assert_equal "::", unspec.address
203
- # assert_equal 128, unspec.prefix
204
- # assert_equal true, unspec.unspecified?
205
- # assert_instance_of @klass, unspec.class
206
- # end
207
-
208
- # def test_classmethod_create_loopback
209
- # loopb = @klass.create_loopback
210
- # assert_equal "::1", loopb.address
211
- # assert_equal 128, loopb.prefix
212
- # assert_equal true, loopb.loopback?
213
- # assert_instance_of @klass, loopb.class
214
- # end
215
-
216
208
  def test_classmethod_parse_data
217
209
  str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
218
210
  ip = @klass.parse_data str
@@ -231,7 +223,7 @@ class IPv6Test < Test::Unit::TestCase
231
223
  assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
232
224
  end
233
225
 
234
- end # class IPv4Test
226
+ end # class IPv6Test
235
227
 
236
228
  class IPv6UnspecifiedTest < Test::Unit::TestCase
237
229
 
@@ -259,6 +251,10 @@ class IPv6UnspecifiedTest < Test::Unit::TestCase
259
251
  assert_equal @string, @ip.to_string_uncompressed
260
252
  assert_equal @u128, @ip.to_u128
261
253
  end
254
+
255
+ def test_method_ipv6?
256
+ assert_equal true, @ip.ipv6?
257
+ end
262
258
 
263
259
  end # class IPv6UnspecifiedTest
264
260
 
@@ -289,6 +285,10 @@ class IPv6LoopbackTest < Test::Unit::TestCase
289
285
  assert_equal @string, @ip.to_string_uncompressed
290
286
  assert_equal @u128, @ip.to_u128
291
287
  end
288
+
289
+ def test_method_ipv6?
290
+ assert_equal true, @ip.ipv6?
291
+ end
292
292
 
293
293
  end # class IPv6LoopbackTest
294
294
 
@@ -345,6 +345,10 @@ class IPv6MappedTest < Test::Unit::TestCase
345
345
  assert_equal @u128, @ip.to_u128
346
346
  end
347
347
 
348
+ def test_method_ipv6?
349
+ assert_equal true, @ip.ipv6?
350
+ end
351
+
348
352
  def test_mapped?
349
353
  assert_equal true, @ip.mapped?
350
354
  end
@@ -3,25 +3,29 @@ require 'test_helper'
3
3
  class Prefix32Test < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
+ @netmask0 = "0.0.0.0"
6
7
  @netmask8 = "255.0.0.0"
7
8
  @netmask16 = "255.255.0.0"
8
9
  @netmask24 = "255.255.255.0"
9
10
  @netmask30 = "255.255.255.252"
10
- @netmasks = [@netmask8,@netmask16,@netmask24,@netmask30]
11
+ @netmasks = [@netmask0,@netmask8,@netmask16,@netmask24,@netmask30]
11
12
 
12
13
  @prefix_hash = {
14
+ "0.0.0.0" => 0,
13
15
  "255.0.0.0" => 8,
14
16
  "255.255.0.0" => 16,
15
17
  "255.255.255.0" => 24,
16
18
  "255.255.255.252" => 30}
17
19
 
18
20
  @octets_hash = {
21
+ [0,0,0,0] => 0,
19
22
  [255,0,0,0] => 8,
20
23
  [255,255,0,0] => 16,
21
24
  [255,255,255,0] => 24,
22
25
  [255,255,255,252] => 30}
23
26
 
24
27
  @u32_hash = {
28
+ 0 => 0,
25
29
  8 => 4278190080,
26
30
  16 => 4294901760,
27
31
  24 => 4294967040,
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ipaddress
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 9
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 0
10
- version: 0.7.0
9
+ - 5
10
+ version: 0.7.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marco Ceresa
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-08 00:00:00 +02:00
18
+ date: 2011-04-08 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -30,7 +30,6 @@ extra_rdoc_files:
30
30
  - README.rdoc
31
31
  files:
32
32
  - .document
33
- - .gitignore
34
33
  - CHANGELOG.rdoc
35
34
  - LICENSE
36
35
  - README.rdoc
@@ -52,8 +51,8 @@ homepage: http://github.com/bluemonk/ipaddress
52
51
  licenses: []
53
52
 
54
53
  post_install_message:
55
- rdoc_options:
56
- - --charset=UTF-8
54
+ rdoc_options: []
55
+
57
56
  require_paths:
58
57
  - lib
59
58
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -82,9 +81,9 @@ signing_key:
82
81
  specification_version: 3
83
82
  summary: IPv4/IPv6 addresses manipulation library
84
83
  test_files:
85
- - test/test_helper.rb
86
- - test/ipaddress_test.rb
87
- - test/ipaddress/prefix_test.rb
88
- - test/ipaddress/ipv6_test.rb
89
84
  - test/ipaddress/extensions/extensions_test.rb
90
85
  - test/ipaddress/ipv4_test.rb
86
+ - test/ipaddress/ipv6_test.rb
87
+ - test/ipaddress/prefix_test.rb
88
+ - test/ipaddress_test.rb
89
+ - test/test_helper.rb
data/.gitignore DELETED
@@ -1,7 +0,0 @@
1
- *.sw?
2
- .DS_Store
3
- coverage
4
- rdoc
5
- pkg
6
- server.rb
7
- ipaddr.html