netaddr 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 89ce300b6a351757d48c569e132c3e1346f7b09a
4
- data.tar.gz: 5f11179793ee8a0d5a42758524b7aa49f71392cc
2
+ SHA256:
3
+ metadata.gz: de26482bcd44b80695dd256c99f45c9f5ceb4d7bec3cfc57c60a0f59a860188d
4
+ data.tar.gz: f86ab761fd9ab76f1de6f1fb26d4f89fa85390ab772c3144de18d07b5202dc0e
5
5
  SHA512:
6
- metadata.gz: 36acfbba061405495f4e4d08c271f6be19c9810de6c5a44f78a2ebf407cf24637dac6b50277c82cc6048e36d00785e143aa37170e0101d40638b36f77e575c5a
7
- data.tar.gz: c9baee3f002fb16def0f60f8e9304b44015739f0a5f844b1413f8b20d2d80c9bc73678654f81906ef14255f9928b64a3153eba120e387f4044bd558e5d6f5df3
6
+ metadata.gz: e5afb658aa8805f286b273ada3a86fae37b6fd8c4e6ef441845e78fa70e351c7c9b2df2b84f8e919a5cea032ad801b0f1fe36494e13b8e9b3afcf7ecd2ac869b
7
+ data.tar.gz: 339ba4279b2f90223362c40ca5753922b889bf2cc2870c4a7a9ddd774668ad7dd419144a9773bd371a5ca16e6d77b7465756fcf5f46bf2410effbbc3a1dc0e55
data/LICENSE CHANGED
File without changes
data/README.md CHANGED
File without changes
File without changes
File without changes
@@ -19,7 +19,7 @@ module NetAddr
19
19
  # parse will create an IPv4 from its string representation (ie. "192.168.1.1").
20
20
  # Throws ValidationError on error.
21
21
  def IPv4.parse(ip)
22
- ip.strip!
22
+ ip = ip.strip
23
23
  i = Util.parse_IPv4(ip)
24
24
  return IPv4.new(i)
25
25
  end
@@ -77,6 +77,11 @@ module NetAddr
77
77
  Util.int_to_IPv4(@addr)
78
78
  end
79
79
 
80
+ # version returns "4" for IPv4
81
+ def version()
82
+ return 4
83
+ end
84
+
80
85
  end # end class IPv4
81
86
 
82
87
  end # end module
@@ -23,7 +23,7 @@ module NetAddr
23
23
  # parse will create an IPv4Net from its string representation. Will default to a /32 netmask if not specified.
24
24
  # Throws ValidationError on error.
25
25
  def IPv4Net.parse(net)
26
- net.strip!
26
+ net = net.strip
27
27
  m32 = nil
28
28
  if (net.include?("/")) # cidr format
29
29
  addr,mask = net.split("/")
@@ -220,6 +220,11 @@ module NetAddr
220
220
  return @base.to_s + @m32.to_s
221
221
  end
222
222
 
223
+ # version returns "4" for IPv4
224
+ def version()
225
+ return 4
226
+ end
227
+
223
228
 
224
229
  protected
225
230
 
@@ -19,7 +19,7 @@ module NetAddr
19
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
- ip.strip!
22
+ ip = ip.strip
23
23
  i = Util.parse_IPv6(ip)
24
24
  return IPv6.new(i)
25
25
  end
@@ -110,6 +110,11 @@ module NetAddr
110
110
  return hexStr.join(":")
111
111
  end
112
112
 
113
+ # version returns "6" for IPv6
114
+ def version()
115
+ return 6
116
+ end
117
+
113
118
  end # end class IPv6
114
119
 
115
120
  end # end module
@@ -28,7 +28,7 @@ module NetAddr
28
28
  # Throws ValidationError on error.
29
29
  def IPv6Net.parse(net)
30
30
  m128 = nil
31
- net.strip!
31
+ net = net.strip
32
32
  if (net.include?("/")) # cidr format
33
33
  addr,mask = net.split("/")
34
34
  m128 = Mask128.parse(mask)
@@ -224,6 +224,11 @@ module NetAddr
224
224
  return @base.to_s + @m128.to_s
225
225
  end
226
226
 
227
+ # version returns "6" for IPv6
228
+ def version()
229
+ return 6
230
+ end
231
+
227
232
 
228
233
  protected
229
234
 
@@ -26,11 +26,13 @@ module NetAddr
26
26
  #
27
27
  # Throws ValidationError on error.
28
28
  def Mask128.parse(mask)
29
- mask.strip!
29
+ mask = mask.strip
30
30
  if (mask.start_with?("/")) # cidr format
31
31
  mask = mask[1..-1] # remove "/"
32
32
  end
33
- return Mask128.new(mask.to_i)
33
+ return Mask128.new(Integer(mask))
34
+ rescue ArgumentError
35
+ raise ValidationError, "#{mask} is not valid integer."
34
36
  end
35
37
 
36
38
  #cmp compares equality with another Mask128. Return:
@@ -26,11 +26,17 @@ module NetAddr
26
26
  #
27
27
  # Throws ValidationError on error.
28
28
  def Mask32.parse(mask)
29
- mask.strip!
29
+ mask = mask.strip
30
30
  if (mask.start_with?("/")) # cidr format
31
- return Mask32.new(mask[1..-1].to_i) # remove "/"
32
- elsif (!mask.include?("."))
33
- return Mask32.new(mask.to_i)
31
+ mask = mask[1..-1] # remove "/"
32
+ end
33
+
34
+ if (!mask.include?("."))
35
+ begin
36
+ return Mask32.new(Integer(mask))
37
+ rescue ArgumentError
38
+ raise ValidationError, "#{mask} is not valid integer."
39
+ end
34
40
  end
35
41
 
36
42
  # for extended netmask
@@ -62,7 +68,7 @@ module NetAddr
62
68
 
63
69
  # extended returns the Mask32 in extended format (eg. x.x.x.x)
64
70
  def extended()
65
- Util.intToMask32(@mask)
71
+ Util.int_to_IPv4(@mask)
66
72
  end
67
73
 
68
74
  #cmp compares equality with another Mask32. Return:
@@ -37,6 +37,24 @@ module NetAddr
37
37
  end
38
38
  module_function :ipv4_prefix_len
39
39
 
40
+ ## parse_ip parses a string into an IPv4 or IPv6
41
+ def parse_ip(ip)
42
+ if (ip.include?(".")) # ipv4
43
+ return IPv4.parse(ip)
44
+ end
45
+ return IPv6.parse(ip)
46
+ end
47
+ module_function :parse_ip
48
+
49
+ ## parse_net parses a string into an IPv4Net or IPv6Net
50
+ def parse_net(net)
51
+ if (net.include?(".")) # ipv4
52
+ return IPv4Net.parse(net)
53
+ end
54
+ return IPv6Net.parse(net)
55
+ end
56
+ module_function :parse_net
57
+
40
58
  # sort_IPv4 sorts a list of IPv4 objects in ascending order.
41
59
  # It will return a new list with any non IPv4 objects removed.
42
60
  def sort_IPv4(list)
@@ -23,29 +23,27 @@ module NetAddr
23
23
 
24
24
  # discard_subnets returns a copy of the IPv4NetList with any entries which are subnets of other entries removed.
25
25
  def Util.discard_subnets(list)
26
- unrelated = []
27
- supernets = []
26
+ keepers = []
28
27
  last = list[list.length-1]
28
+ keep_last = true
29
29
  list.each do |net|
30
30
  rel = last.rel(net)
31
- if (!rel)
32
- unrelated.push(net)
33
- elsif (rel == -1) # last is subnet of net
34
- supernets.push(net)
31
+ if (!rel) # keep unrelated nets
32
+ keepers.push(net)
33
+ elsif (rel == -1) # keep supernets, but do not keep last
34
+ keepers.push(net)
35
+ keep_last = false
35
36
  end
36
37
  end
37
38
 
38
- cleaned = []
39
- if (supernets.length > 0)
40
- cleaned = discard_subnets(supernets)
41
- else
42
- cleaned.push(last)
39
+ # recursively clean up keepers
40
+ if (keepers.length > 0)
41
+ keepers = discard_subnets(keepers)
43
42
  end
44
-
45
- if (unrelated.length > 0)
46
- cleaned.concat( discard_subnets(unrelated) )
43
+ if keep_last
44
+ keepers.unshift(last)
47
45
  end
48
- return cleaned
46
+ return keepers
49
47
  end
50
48
 
51
49
  # fill returns a copy of the given Array, stripped of any networks which are not subnets of ipnet
@@ -166,8 +164,7 @@ module NetAddr
166
164
  raise ValidationError, "#{ip} contains invalid characters."
167
165
  end
168
166
 
169
- ip.strip!
170
- octets = ip.split('.')
167
+ octets = ip.strip.split('.')
171
168
  if (octets.length != 4)
172
169
  raise ValidationError, "IPv4 requires (4) octets."
173
170
  end
@@ -192,7 +189,7 @@ module NetAddr
192
189
  raise ValidationError, "#{ip} contains invalid characters."
193
190
  end
194
191
 
195
- ip.strip!
192
+ ip = ip.strip
196
193
  if (ip == "::")
197
194
  return 0 # zero address
198
195
  end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -34,6 +34,11 @@ class TestMask32 < Test::Unit::TestCase
34
34
  assert_equal(0, m.cmp(m4))
35
35
  end
36
36
 
37
+ def test_extended
38
+ m32 = NetAddr::Mask32.new(24)
39
+ assert_equal("255.255.255.0", m32.extended)
40
+ end
41
+
37
42
  def test_len
38
43
  m = NetAddr::Mask32.new(24)
39
44
  assert_equal(256, m.len())
@@ -5,6 +5,16 @@ require 'test/unit'
5
5
 
6
6
  class TestNetAddr < Test::Unit::TestCase
7
7
 
8
+ def test_parse_ip
9
+ assert_equal("128.0.0.1", NetAddr.parse_ip("128.0.0.1").to_s)
10
+ assert_equal("1::1", NetAddr.parse_ip("1::1").to_s)
11
+ end
12
+
13
+ def test_parse_net
14
+ assert_equal("128.0.0.1/32", NetAddr.parse_net("128.0.0.1/32").to_s)
15
+ assert_equal("1::/24", NetAddr.parse_net("1::1/24").to_s)
16
+ end
17
+
8
18
  def test_ipv4_prefix_len
9
19
  assert_equal(32,NetAddr.ipv4_prefix_len(1))
10
20
  assert_equal(27,NetAddr.ipv4_prefix_len(30))
@@ -67,6 +77,17 @@ class TestNetAddr < Test::Unit::TestCase
67
77
  assert_equal(expect[i],net.to_s)
68
78
  i += 1
69
79
  end
80
+
81
+ nets = []
82
+ ["10.0.0.0/26","10.0.0.64/26","10.0.0.0/24","10.0.0.192/26","10.0.0.128/26"].each do |net| # test out of order
83
+ nets.push(NetAddr::IPv4Net.parse(net))
84
+ end
85
+ expect = ["10.0.0.0/24"]
86
+ i = 0
87
+ NetAddr.summ_IPv4Net(nets).each do |net|
88
+ assert_equal(expect[i],net.to_s)
89
+ i += 1
90
+ end
70
91
  end
71
92
 
72
93
  def test_sort_IPv6
File without changes
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.3
4
+ version: 2.0.4
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-03-23 00:00:00.000000000 Z
11
+ date: 2019-10-03 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.2.1
64
+ rubygems_version: 2.7.6.2
65
65
  signing_key:
66
66
  specification_version: 4
67
67
  summary: A Ruby library for performing calculations on IPv4 and IPv6 subnets.