netaddr 2.0.1 → 2.0.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.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8baac76897c5888273f82c02b7719ac40704dde4
4
- data.tar.gz: 55829efe0e322cd809cc8113fe88c74e4597f7fb
3
+ metadata.gz: 89ce300b6a351757d48c569e132c3e1346f7b09a
4
+ data.tar.gz: 5f11179793ee8a0d5a42758524b7aa49f71392cc
5
5
  SHA512:
6
- metadata.gz: 64de30c364321364056cd9e50f1468991109203cdc58d7d3d0fa0f7626455ee8c42fd2fabe505efdaefa442b344fb03ed0354c43b39abf31470359007722bec7
7
- data.tar.gz: ff8813e6e0319932083ea4e994e4194473ac439d579df47b45faf2e2b86a72895be082b33a663ad9705a55a706815685c18c8880ac5313d59b54265dc0e599a1
6
+ metadata.gz: 36acfbba061405495f4e4d08c271f6be19c9810de6c5a44f78a2ebf407cf24637dac6b50277c82cc6048e36d00785e143aa37170e0101d40638b36f77e575c5a
7
+ data.tar.gz: c9baee3f002fb16def0f60f8e9304b44015739f0a5f844b1413f8b20d2d80c9bc73678654f81906ef14255f9928b64a3153eba120e387f4044bd558e5d6f5df3
@@ -67,6 +67,11 @@ module NetAddr
67
67
  return IPv4.new(self.addr - 1)
68
68
  end
69
69
 
70
+ # to_net returns the IPv4 as a IPv4Net
71
+ def to_net()
72
+ NetAddr::IPv4Net.new(self,nil)
73
+ end
74
+
70
75
  # to_s returns the IPv4 as a String
71
76
  def to_s()
72
77
  Util.int_to_IPv4(@addr)
@@ -43,12 +43,13 @@ module NetAddr
43
43
  return @base.to_s + " " + Util.int_to_IPv4(@m32.mask)
44
44
  end
45
45
 
46
- #cmp compares equality with another IPv4Net. Return:
47
- #* 1 if this IPv4Net is numerically greater
48
- #* 0 if the two are equal
49
- #* -1 if this IPv4Net is numerically less
46
+ # cmp compares equality with another IPv4Net. Return:
47
+ # * 1 if this IPv4Net is numerically greater
48
+ # * 0 if the two are equal
49
+ # * -1 if this IPv4Net is numerically less
50
50
  #
51
- #The comparison is initially performed on using the cmp() method of the network address, however, in cases where the network #addresses are identical then the netmasks will be compared with the cmp() method of the netmask.
51
+ # The comparison is initially performed on using the cmp() method of the network address, however, in cases where the network
52
+ # addresses are identical then the netmasks will be compared with the cmp() method of the netmask.
52
53
  def cmp(other)
53
54
  if (!other.kind_of?(IPv4Net))
54
55
  raise ArgumentError, "Expected an IPv4Net object for 'other' but got a #{other.class}."
@@ -60,6 +61,17 @@ module NetAddr
60
61
  return self.netmask.cmp(other.netmask)
61
62
  end
62
63
 
64
+ #contains returns true if the IPv4Net contains the IPv4
65
+ def contains(ip)
66
+ if (!ip.kind_of?(IPv4))
67
+ raise ArgumentError, "Expected an IPv4 object for 'ip' but got a #{ip.class}."
68
+ end
69
+ if (@base.addr == ip.addr & @m32.mask)
70
+ return true
71
+ end
72
+ return false
73
+ end
74
+
63
75
  # fill returns a copy of the given Array, stripped of any networks which are not subnets of this IPv4Net
64
76
  # and with any missing gaps filled in.
65
77
  def fill(list)
@@ -16,7 +16,7 @@ module NetAddr
16
16
  @addr = i
17
17
  end
18
18
 
19
- # parse will create an IPv6 from its string representation (ie. "192.168.1.1").
19
+ # parse will create an IPv6 from its string representation (ie. "1::").
20
20
  # Throws ValidationError on error.
21
21
  def IPv6.parse(ip)
22
22
  ip.strip!
@@ -24,10 +24,10 @@ module NetAddr
24
24
  return IPv6.new(i)
25
25
  end
26
26
 
27
- #cmp compares equality with another IPv6. Return:
28
- #* 1 if this IPv6 is numerically greater
29
- #* 0 if the two are equal
30
- #* -1 if this IPv6 is numerically less
27
+ # cmp compares equality with another IPv6. Return:
28
+ # * 1 if this IPv6 is numerically greater
29
+ # * 0 if the two are equal
30
+ # * -1 if this IPv6 is numerically less
31
31
  def cmp(other)
32
32
  if (!other.kind_of?(IPv6))
33
33
  raise ArgumentError, "Expected an IPv6 object for 'other' but got a #{other.class}."
@@ -66,6 +66,11 @@ module NetAddr
66
66
  return IPv6.new(self.addr - 1)
67
67
  end
68
68
 
69
+ # to_net returns the IPv6 as a IPv6Net
70
+ def to_net()
71
+ NetAddr::IPv6Net.new(self,nil)
72
+ end
73
+
69
74
  # to_s returns the IPv6 as a String in zero-compressed format (per rfc5952).
70
75
  def to_s()
71
76
  hexStr = ["","","","","","","",""]
@@ -24,7 +24,7 @@ module NetAddr
24
24
  @base = IPv6.new(ip.addr & m128.mask)
25
25
  end
26
26
 
27
- # parse will create an IPv6Net from its string representation. Will default to a /64 netmask if not specified.
27
+ # parse will create an IPv6Net from its string representation. A default netmask will be used if not specified.
28
28
  # Throws ValidationError on error.
29
29
  def IPv6Net.parse(net)
30
30
  m128 = nil
@@ -39,12 +39,13 @@ module NetAddr
39
39
  return IPv6Net.new(ip,m128)
40
40
  end
41
41
 
42
- #cmp compares equality with another IPv6Net. Return:
43
- #* 1 if this IPv6Net is numerically greater
44
- #* 0 if the two are equal
45
- #* -1 if this IPv6Net is numerically less
42
+ # cmp compares equality with another IPv6Net. Return:
43
+ # * 1 if this IPv6Net is numerically greater
44
+ # * 0 if the two are equal
45
+ # * -1 if this IPv6Net is numerically less
46
46
  #
47
- #The comparison is initially performed on using the cmp() method of the network address, however, in cases where the network #addresses are identical then the netmasks will be compared with the cmp() method of the netmask.
47
+ # The comparison is initially performed on using the cmp() method of the network address, however, in cases where the network
48
+ # addresses are identical then the netmasks will be compared with the cmp() method of the netmask.
48
49
  def cmp(other)
49
50
  if (!other.kind_of?(IPv6Net))
50
51
  raise ArgumentError, "Expected an IPv6Net object for 'other' but got a #{other.class}."
@@ -56,6 +57,17 @@ module NetAddr
56
57
  return self.netmask.cmp(other.netmask)
57
58
  end
58
59
 
60
+ #contains returns true if the IPv6Net contains the IPv6
61
+ def contains(ip)
62
+ if (!ip.kind_of?(IPv6))
63
+ raise ArgumentError, "Expected an IPv6 object for 'ip' but got a #{ip.class}."
64
+ end
65
+ if (@base.addr == ip.addr & @m128.mask)
66
+ return true
67
+ end
68
+ return false
69
+ end
70
+
59
71
  # fill returns a copy of the given Array, stripped of any networks which are not subnets of this IPv6Net
60
72
  # and with any missing gaps filled in.
61
73
  def fill(list)
@@ -68,6 +80,11 @@ module NetAddr
68
80
  return self.netmask.len
69
81
  end
70
82
 
83
+ # long returns the IPv6Net as a string in long (uncompressed) format
84
+ def long()
85
+ return @base.long() + @m128.to_s
86
+ end
87
+
71
88
  # netmask returns the Mask128 object representing the netmask for this network
72
89
  def netmask()
73
90
  @m128
@@ -116,13 +133,13 @@ module NetAddr
116
133
  return sub0.nth_next_sib(index)
117
134
  end
118
135
 
119
- # prev returns the previous largest consecutive IP network or nil if this is 0.0.0.0.
136
+ # prev returns the previous largest consecutive IP network or nil if this is ::.
120
137
  def prev()
121
138
  net = self.grow
122
139
  return net.prev_sib
123
140
  end
124
141
 
125
- # prev_sib returns the network immediately preceding this one or nil if this network is 0.0.0.0.
142
+ # prev_sib returns the network immediately preceding this one or nil if this network is ::.
126
143
  def prev_sib()
127
144
  if (self.network.addr == 0)
128
145
  return nil
@@ -223,7 +240,6 @@ module NetAddr
223
240
  prefix_len -= 1
224
241
  end
225
242
 
226
- newNet = IPv6Net.new(IPv6.new(addr),Mask128.new(prefix_len))
227
243
  return IPv6Net.new(IPv6.new(addr),Mask128.new(prefix_len))
228
244
  end
229
245
 
@@ -42,6 +42,21 @@ class NetAddrExamples < Test::Unit::TestCase
42
42
  puts " " + ip.to_s
43
43
  end
44
44
 
45
+ puts "\nDoes 10.0.0.7 belong to the 10.0.0.8/29 subnet?"
46
+ subnet29 = NetAddr::IPv4Net.parse("10.0.0.8/29")
47
+ if subnet29.contains(NetAddr::IPv4.parse("10.0.0.7"))
48
+ puts " yes"
49
+ else
50
+ puts " no"
51
+ end
52
+
53
+ puts "\nDoes 10.0.0.10 belong to the 10.0.0.8/29 subnet?"
54
+ if subnet29.contains(NetAddr::IPv4.parse("10.0.0.10"))
55
+ puts " yes"
56
+ else
57
+ puts " no"
58
+ end
59
+
45
60
  puts "\nGiven the 3rd /30 of 10.0.0.0/24, fill in the holes:"
46
61
  expect = ["10.0.0.0/29","10.0.0.8/30","10.0.0.12/30","10.0.0.16/28","10.0.0.32/27","10.0.0.64/26","10.0.0.128/25"]
47
62
  i = 0
@@ -82,6 +97,9 @@ class NetAddrExamples < Test::Unit::TestCase
82
97
  puts "\nRendering as a String: " + net.to_s
83
98
  assert_equal("fec0::/62", net.to_s)
84
99
 
100
+ puts "\nRendering as a String (long format): " + net.long
101
+ assert_equal("fec0:0000:0000:0000:0000:0000:0000:0000/62", net.long)
102
+
85
103
  puts "\nIterating its /64 subnets:"
86
104
  expect = ["fec0::/64","fec0:0:0:1::/64","fec0:0:0:2::/64","fec0:0:0:3::/64"]
87
105
  0.upto(net.subnet_count(64) - 1) do |i|
@@ -26,7 +26,7 @@ class TestIPv4 < Test::Unit::TestCase
26
26
  def test_cmp
27
27
  ip = NetAddr::IPv4.parse("128.0.0.1")
28
28
  ip2 = NetAddr::IPv4.parse("128.0.0.0")
29
- ip3 =NetAddr::IPv4.parse("128.0.0.2")
29
+ ip3 = NetAddr::IPv4.parse("128.0.0.2")
30
30
  ip4 = NetAddr::IPv4.parse("128.0.0.1")
31
31
  assert_equal(1, ip.cmp(ip2))
32
32
  assert_equal(-1, ip.cmp(ip3))
@@ -51,4 +51,10 @@ class TestIPv4 < Test::Unit::TestCase
51
51
  assert_equal("0.0.0.0", NetAddr::IPv4.parse("0.0.0.1").prev().to_s)
52
52
  assert_nil(NetAddr::IPv4.parse("0.0.0.0").prev())
53
53
  end
54
+
55
+ def test_to_net
56
+ ip = NetAddr::IPv4.parse("192.168.1.1")
57
+ net = NetAddr::IPv4Net.parse("192.168.1.1")
58
+ assert_equal(0, net.cmp(ip.to_net()))
59
+ end
54
60
  end
@@ -35,6 +35,17 @@ class TestIPv4Net < Test::Unit::TestCase
35
35
  assert_equal(0, net1.cmp(net5)) # eq
36
36
  end
37
37
 
38
+ def test_contains
39
+ net = NetAddr::IPv4Net.parse("1.0.0.8/29")
40
+ ip1 = NetAddr::IPv4.parse("1.0.0.15")
41
+ ip2 = NetAddr::IPv4.parse("1.0.0.16")
42
+ ip3 = NetAddr::IPv4.parse("1.0.0.7")
43
+
44
+ assert_equal(true, net.contains(ip1))
45
+ assert_equal(false, net.contains(ip2))
46
+ assert_equal(false, net.contains(ip3))
47
+ end
48
+
38
49
  def test_extended
39
50
  net = NetAddr::IPv4Net.parse("128.0.0.1/24")
40
51
  assert_equal("128.0.0.0 255.255.255.0", net.extended)
@@ -67,6 +67,12 @@ class TestIPv6 < Test::Unit::TestCase
67
67
  assert_nil(NetAddr::IPv6.parse("::").prev())
68
68
  end
69
69
 
70
+ def test_to_net
71
+ ip = NetAddr::IPv6.parse("1::")
72
+ net = NetAddr::IPv6Net.parse("1::")
73
+ assert_equal(0, net.cmp(ip.to_net()))
74
+ end
75
+
70
76
  def test_to_s
71
77
  assert_equal("::", NetAddr::IPv6.parse("0:0:0:0:0:0:0:0").to_s)
72
78
  assert_equal("1::", NetAddr::IPv6.parse("1:0:0:0:0:0:0:0").to_s)
@@ -35,6 +35,17 @@ class TestIPv6Net < Test::Unit::TestCase
35
35
  assert_equal(1, net1.cmp(net4)) # ip eq, mask greater
36
36
  assert_equal(0, net1.cmp(net5)) # eq
37
37
  end
38
+
39
+ def test_contains
40
+ net = NetAddr::IPv6Net.parse("1:8::/29")
41
+ ip1 = NetAddr::IPv6.parse("1:f::")
42
+ ip2 = NetAddr::IPv6.parse("1:10::")
43
+ ip3 = NetAddr::IPv6.parse("1:7::")
44
+
45
+ assert_equal(true, net.contains(ip1))
46
+ assert_equal(false, net.contains(ip2))
47
+ assert_equal(false, net.contains(ip3))
48
+ end
38
49
 
39
50
  def test_fill
40
51
  parent = NetAddr::IPv6Net.parse("ff00::/8")
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netaddr
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Spinhirne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-22 00:00:00.000000000 Z
11
+ date: 2018-03-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  version: '0'
62
62
  requirements: []
63
63
  rubyforge_project:
64
- rubygems_version: 2.5.1
64
+ rubygems_version: 2.5.2.1
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A Ruby library for performing calculations on IPv4 and IPv6 subnets.