ipaddr 1.0.0 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +16 -1
- data/README.md +14 -2
- data/ipaddr.gemspec +4 -4
- data/lib/ipaddr.rb +94 -6
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78c5854e872965800e79a6508cb7d8ff3cb3ea73
|
4
|
+
data.tar.gz: 0b6b8d515a8718a852beb923e7f5c24c9f65353d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3089db198e90b6c0860b09483f052fa1eb24cfb034d1473db3817e46e06b00d52afec9d5f43abaf09e01bf603324473ed9ed0de770e5bdf6de308f4e57cd556
|
7
|
+
data.tar.gz: ed8a0801bc5aa5df9ddcbcb7d8134e521ab1e77af14c19a38b0e625c1ac77bf0e48f271ef54b517a805585d4d3611a2bed848f997d0b8166cccf25747fcdee35
|
data/.travis.yml
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
3
|
rvm:
|
4
|
-
- 2.
|
4
|
+
- 2.3
|
5
|
+
- 2.4
|
6
|
+
- 2.5.0
|
5
7
|
- ruby-head
|
8
|
+
matrix:
|
9
|
+
include:
|
10
|
+
- rvm: jruby-9.1.13.0
|
11
|
+
jdk: openjdk8
|
12
|
+
- rvm: jruby-9.1.13.0
|
13
|
+
jdk: oraclejdk8
|
14
|
+
- rvm: jruby-head
|
15
|
+
jdk: openjdk8
|
16
|
+
- rvm: jruby-head
|
17
|
+
jdk: oraclejdk8
|
18
|
+
allow_failures:
|
19
|
+
- rvm: ruby-head
|
20
|
+
- rvm: jruby-head
|
6
21
|
before_install: gem install bundler
|
data/README.md
CHANGED
@@ -5,10 +5,15 @@ IPv4 and IPv6 are supported.
|
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
8
|
-
|
8
|
+
This library is part of the standard ruby distribution as default gem
|
9
|
+
and synchronized periodically, but you can explicitly depend on this
|
10
|
+
gem with version constraints as necessary, like when you need a newer
|
11
|
+
version than comes with older versions of ruby.
|
12
|
+
|
13
|
+
For example, you can add this line to your application's Gemfile:
|
9
14
|
|
10
15
|
```ruby
|
11
|
-
gem 'ipaddr'
|
16
|
+
gem 'ipaddr', '~> 1.2'
|
12
17
|
```
|
13
18
|
|
14
19
|
And then execute:
|
@@ -39,6 +44,13 @@ ipaddr3 = IPAddr.new "192.168.2.0/24"
|
|
39
44
|
p ipaddr3 #=> #<IPAddr: IPv4:192.168.2.0/255.255.255.0>
|
40
45
|
```
|
41
46
|
|
47
|
+
## Alternative
|
48
|
+
|
49
|
+
The [ipaddress](https://rubygems.org/gems/ipaddress) gem is a popular,
|
50
|
+
extensive library for manipulating IP addresses with a layer of
|
51
|
+
compatibility with `ipaddr`. If you need a richer set of features
|
52
|
+
than `ipaddr` has, try this library instead.
|
53
|
+
|
42
54
|
## Development
|
43
55
|
|
44
56
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/ipaddr.gemspec
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
# coding: utf-8
|
2
3
|
lib = File.expand_path("../lib", __FILE__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
|
5
6
|
Gem::Specification.new do |spec|
|
6
7
|
spec.name = "ipaddr"
|
7
|
-
spec.version = "1.
|
8
|
+
spec.version = "1.2.2"
|
8
9
|
spec.authors = ["Akinori MUSHA", "Hajimu UMEMOTO"]
|
9
10
|
spec.email = ["knu@idaemons.org", "ume@mahoroba.org"]
|
10
11
|
|
@@ -14,10 +15,9 @@ IPAddr provides a set of methods to manipulate an IP address.
|
|
14
15
|
Both IPv4 and IPv6 are supported.
|
15
16
|
DESCRIPTION
|
16
17
|
spec.homepage = "https://github.com/ruby/ipaddr"
|
18
|
+
spec.license = "BSD-2-Clause"
|
17
19
|
|
18
|
-
spec.files =
|
19
|
-
f.match(%r{^(test|spec|features)/})
|
20
|
-
end
|
20
|
+
spec.files = [".gitignore", ".travis.yml", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "ipaddr.gemspec", "lib/ipaddr.rb"]
|
21
21
|
spec.bindir = "exe"
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ["lib"]
|
data/lib/ipaddr.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# frozen_string_literal:
|
1
|
+
# frozen_string_literal: true
|
2
2
|
#
|
3
3
|
# ipaddr.rb - A class to manipulate an IP address
|
4
4
|
#
|
@@ -103,13 +103,13 @@ class IPAddr
|
|
103
103
|
|
104
104
|
# Creates a new ipaddr containing the given network byte ordered
|
105
105
|
# string form of an IP address.
|
106
|
-
def
|
107
|
-
return
|
106
|
+
def self.new_ntoh(addr)
|
107
|
+
return new(ntop(addr))
|
108
108
|
end
|
109
109
|
|
110
110
|
# Convert a network byte ordered string form of an IP address into
|
111
111
|
# human readable form.
|
112
|
-
def
|
112
|
+
def self.ntop(addr)
|
113
113
|
case addr.size
|
114
114
|
when 4
|
115
115
|
s = addr.unpack('C4').join('.')
|
@@ -259,6 +259,50 @@ class IPAddr
|
|
259
259
|
return @family == Socket::AF_INET6
|
260
260
|
end
|
261
261
|
|
262
|
+
# Returns true if the ipaddr is a loopback address.
|
263
|
+
def loopback?
|
264
|
+
case @family
|
265
|
+
when Socket::AF_INET
|
266
|
+
@addr & 0xff000000 == 0x7f000000
|
267
|
+
when Socket::AF_INET6
|
268
|
+
@addr == 1
|
269
|
+
else
|
270
|
+
raise AddressFamilyError, "unsupported address family"
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
274
|
+
# Returns true if the ipaddr is a private address. IPv4 addresses
|
275
|
+
# in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC
|
276
|
+
# 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC
|
277
|
+
# 4193 are considered private.
|
278
|
+
def private?
|
279
|
+
case @family
|
280
|
+
when Socket::AF_INET
|
281
|
+
@addr & 0xff000000 == 0x0a000000 || # 10.0.0.0/8
|
282
|
+
@addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12
|
283
|
+
@addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16
|
284
|
+
when Socket::AF_INET6
|
285
|
+
@addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000
|
286
|
+
else
|
287
|
+
raise AddressFamilyError, "unsupported address family"
|
288
|
+
end
|
289
|
+
end
|
290
|
+
|
291
|
+
# Returns true if the ipaddr is a link-local address. IPv4
|
292
|
+
# addresses in 169.254.0.0/16 reserved by RFC 3927 and Link-Local
|
293
|
+
# IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are
|
294
|
+
# considered link-local.
|
295
|
+
def link_local?
|
296
|
+
case @family
|
297
|
+
when Socket::AF_INET
|
298
|
+
@addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16
|
299
|
+
when Socket::AF_INET6
|
300
|
+
@addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000
|
301
|
+
else
|
302
|
+
raise AddressFamilyError, "unsupported address family"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
262
306
|
# Returns true if the ipaddr is an IPv4-mapped IPv6 address.
|
263
307
|
def ipv4_mapped?
|
264
308
|
return ipv6? && (@addr >> 32) == 0xffff
|
@@ -266,6 +310,11 @@ class IPAddr
|
|
266
310
|
|
267
311
|
# Returns true if the ipaddr is an IPv4-compatible IPv6 address.
|
268
312
|
def ipv4_compat?
|
313
|
+
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
|
314
|
+
_ipv4_compat?
|
315
|
+
end
|
316
|
+
|
317
|
+
def _ipv4_compat?
|
269
318
|
if !ipv6? || (@addr >> 32) != 0
|
270
319
|
return false
|
271
320
|
end
|
@@ -273,6 +322,8 @@ class IPAddr
|
|
273
322
|
return a != 0 && a != 1
|
274
323
|
end
|
275
324
|
|
325
|
+
private :_ipv4_compat?
|
326
|
+
|
276
327
|
# Returns a new ipaddr built by converting the native IPv4 address
|
277
328
|
# into an IPv4-mapped IPv6 address.
|
278
329
|
def ipv4_mapped
|
@@ -285,6 +336,7 @@ class IPAddr
|
|
285
336
|
# Returns a new ipaddr built by converting the native IPv4 address
|
286
337
|
# into an IPv4-compatible IPv6 address.
|
287
338
|
def ipv4_compat
|
339
|
+
warn "IPAddr\##{__callee__} is obsolete", uplevel: 1 if $VERBOSE
|
288
340
|
if !ipv4?
|
289
341
|
raise InvalidAddressError, "not an IPv4 address"
|
290
342
|
end
|
@@ -295,7 +347,7 @@ class IPAddr
|
|
295
347
|
# native IPv4 address. If the IP address is not an IPv4-mapped or
|
296
348
|
# IPv4-compatible IPv6 address, returns self.
|
297
349
|
def native
|
298
|
-
if !ipv4_mapped? && !
|
350
|
+
if !ipv4_mapped? && !_ipv4_compat?
|
299
351
|
return self
|
300
352
|
end
|
301
353
|
return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
|
@@ -371,6 +423,35 @@ class IPAddr
|
|
371
423
|
return clone.set(begin_addr, @family)..clone.set(end_addr, @family)
|
372
424
|
end
|
373
425
|
|
426
|
+
# Returns the prefix length in bits for the ipaddr.
|
427
|
+
def prefix
|
428
|
+
case @family
|
429
|
+
when Socket::AF_INET
|
430
|
+
n = IN4MASK ^ @mask_addr
|
431
|
+
i = 32
|
432
|
+
when Socket::AF_INET6
|
433
|
+
n = IN6MASK ^ @mask_addr
|
434
|
+
i = 128
|
435
|
+
else
|
436
|
+
raise AddressFamilyError, "unsupported address family"
|
437
|
+
end
|
438
|
+
while n.positive?
|
439
|
+
n >>= 1
|
440
|
+
i -= 1
|
441
|
+
end
|
442
|
+
i
|
443
|
+
end
|
444
|
+
|
445
|
+
# Sets the prefix length in bits
|
446
|
+
def prefix=(prefix)
|
447
|
+
case prefix
|
448
|
+
when Integer
|
449
|
+
mask!(prefix)
|
450
|
+
else
|
451
|
+
raise InvalidPrefixError, "prefix must be an integer"
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
374
455
|
# Returns a string containing a human-readable representation of the
|
375
456
|
# ipaddr. ("#<IPAddr: family:address/mask>")
|
376
457
|
def inspect
|
@@ -413,7 +494,8 @@ class IPAddr
|
|
413
494
|
|
414
495
|
# Set current netmask to given mask.
|
415
496
|
def mask!(mask)
|
416
|
-
|
497
|
+
case mask
|
498
|
+
when String
|
417
499
|
if mask =~ /\A\d+\z/
|
418
500
|
prefixlen = mask.to_i
|
419
501
|
else
|
@@ -422,6 +504,10 @@ class IPAddr
|
|
422
504
|
raise InvalidPrefixError, "address family is not same"
|
423
505
|
end
|
424
506
|
@mask_addr = m.to_i
|
507
|
+
n = @mask_addr ^ m.instance_variable_get(:@mask_addr)
|
508
|
+
unless ((n + 1) & n).zero?
|
509
|
+
raise InvalidPrefixError, "invalid mask #{mask}"
|
510
|
+
end
|
425
511
|
@addr &= @mask_addr
|
426
512
|
return self
|
427
513
|
end
|
@@ -508,6 +594,8 @@ class IPAddr
|
|
508
594
|
else
|
509
595
|
@mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
|
510
596
|
end
|
597
|
+
rescue InvalidAddressError => e
|
598
|
+
raise e.class, "#{e.message}: #{addr}"
|
511
599
|
end
|
512
600
|
|
513
601
|
def coerce_other(other)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ipaddr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akinori MUSHA
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -74,7 +74,8 @@ files:
|
|
74
74
|
- ipaddr.gemspec
|
75
75
|
- lib/ipaddr.rb
|
76
76
|
homepage: https://github.com/ruby/ipaddr
|
77
|
-
licenses:
|
77
|
+
licenses:
|
78
|
+
- BSD-2-Clause
|
78
79
|
metadata: {}
|
79
80
|
post_install_message:
|
80
81
|
rdoc_options: []
|
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
93
|
version: '0'
|
93
94
|
requirements: []
|
94
95
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.6.
|
96
|
+
rubygems_version: 2.6.14.1
|
96
97
|
signing_key:
|
97
98
|
specification_version: 4
|
98
99
|
summary: A class to manipulate an IP address in ruby
|