ipaddr 1.2.9 → 1.3.0

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/ipaddr.rb +56 -16
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d7f44f2a3874755d2b38a4cf417bc3b86f1298a92a47ce0df1c6da780982f10
4
- data.tar.gz: d7b75a507f7345cfdb5fa1e7141748031b102e843982a5d627fb3d2f9ff9c302
3
+ metadata.gz: d1c0830348465e01e309792b106380c16a97cfe9f5ba003ec99d3e78d2d602bd
4
+ data.tar.gz: 59a62a5f79ce024610365be4a599fa8be0afe413d184edb7e4c9517435fb4678
5
5
  SHA512:
6
- metadata.gz: 34b061ae03573b58b00b867655c94edb42970eccf6297ac29915fb5e9cec430479eadb0cae90c9857b395ed9d220cd9eaa596a791f0225237113ab010b6dcba4
7
- data.tar.gz: 7347ead0e35fa0bad997db445806eda0bdc6d8ddcffba1b8ff6d8d36f3c3febfa429e59bf36a0b5381690c3881163b070b858dc92cea1d8eca887657a4085f96
6
+ metadata.gz: 13aaefd68e369c3591cfe576dcf4d905cda02a7ec4b9de3b7e161817a9c676bd667e1edf4c1b4ee5816b9cac45aa80bab492095b5d46af571fca537ada32b643
7
+ data.tar.gz: 3ddb2b27e66dfa04ed44a2a2617d02501fcaa6975458fdf8378a22dc6031096e05e3034f6a6542856a069fa7cf5b183bec131a13a90be7486415da41bae85924
data/README.md CHANGED
@@ -15,7 +15,7 @@ version than comes with older versions of ruby.
15
15
  For example, you can add this line to your application's Gemfile:
16
16
 
17
17
  ```ruby
18
- gem 'ipaddr', '~> 1.2'
18
+ gem 'ipaddr'
19
19
  ```
20
20
 
21
21
  And then execute:
data/lib/ipaddr.rb CHANGED
@@ -41,7 +41,7 @@ require 'socket'
41
41
 
42
42
  class IPAddr
43
43
  # The version string
44
- VERSION = "1.2.9"
44
+ VERSION = "1.3.0"
45
45
 
46
46
  # 32 bit mask for IPv4
47
47
  IN4MASK = 0xffffffff
@@ -207,7 +207,7 @@ class IPAddr
207
207
 
208
208
  # Returns a string containing the IP address representation.
209
209
  def to_s
210
- str = to_string
210
+ str = _to_string(@addr)
211
211
  return str if ipv4?
212
212
 
213
213
  str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
@@ -227,6 +227,7 @@ class IPAddr
227
227
  str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
228
228
  end
229
229
 
230
+ str << @zone_id if @zone_id
230
231
  str
231
232
  end
232
233
 
@@ -294,11 +295,11 @@ class IPAddr
294
295
  def loopback?
295
296
  case @family
296
297
  when Socket::AF_INET
297
- @addr & 0xff000000 == 0x7f000000 # 127.0.0.1/8
298
+ (@addr & 0xff000000 == 0x7f000000) && prefix >= 8 # 127.0.0.1/8
298
299
  when Socket::AF_INET6
299
300
  @addr == 1 || # ::1
300
301
  (@addr >> 32 == 0xffff && (
301
- @addr & 0xff000000 == 0x7f000000 # ::ffff:127.0.0.1/8
302
+ (@addr & 0xff000000 == 0x7f000000) && prefix >= 104 # ::ffff:127.0.0.0/104 (127.0.0.1/8)
302
303
  ))
303
304
  else
304
305
  raise AddressFamilyError, "unsupported address family"
@@ -313,22 +314,22 @@ class IPAddr
313
314
  def private?
314
315
  case @family
315
316
  when Socket::AF_INET
316
- @addr & 0xff000000 == 0x0a000000 || # 10.0.0.0/8
317
- @addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12
318
- @addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16
317
+ (@addr & 0xff000000 == 0x0a000000) && prefix >= 8 || # 10.0.0.0/8
318
+ (@addr & 0xfff00000 == 0xac100000) && prefix >= 12 || # 172.16.0.0/12
319
+ (@addr & 0xffff0000 == 0xc0a80000) && prefix >= 16 # 192.168.0.0/16
319
320
  when Socket::AF_INET6
320
- @addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000 ||
321
+ (@addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000) && prefix >= 7 || # fc00::/7
321
322
  (@addr >> 32 == 0xffff && (
322
- @addr & 0xff000000 == 0x0a000000 || # ::ffff:10.0.0.0/8
323
- @addr & 0xfff00000 == 0xac100000 || # ::ffff:172.16.0.0/12
324
- @addr & 0xffff0000 == 0xc0a80000 # ::ffff:192.168.0.0/16
323
+ (@addr & 0xff000000 == 0x0a000000) && prefix >= 104 || # ::ffff:10.0.0.0/104 (10.0.0.0/8)
324
+ (@addr & 0xfff00000 == 0xac100000) && prefix >= 108 || # ::ffff:172.16.0.0/108 (172.16.0.0/12)
325
+ (@addr & 0xffff0000 == 0xc0a80000) && prefix >= 112 # ::ffff:192.168.0.0/112 (192.168.0.0/16)
325
326
  ))
326
327
  else
327
328
  raise AddressFamilyError, "unsupported address family"
328
329
  end
329
330
  end
330
331
 
331
- # Returns true if the ipaddr is a link-local address. IPv4
332
+ # Returns true if the ipaddr is a link-local unicast address. IPv4
332
333
  # addresses in 169.254.0.0/16 reserved by RFC 3927 and link-local
333
334
  # IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are
334
335
  # considered link-local. Link-local IPv4 addresses in the
@@ -336,11 +337,49 @@ class IPAddr
336
337
  def link_local?
337
338
  case @family
338
339
  when Socket::AF_INET
339
- @addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16
340
+ (@addr & 0xffff0000 == 0xa9fe0000) && prefix >= 16 # 169.254.0.0/16
341
+ when Socket::AF_INET6
342
+ (@addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000) && prefix >= 10 || # fe80::/10
343
+ (@addr >> 32 == 0xffff && (
344
+ (@addr & 0xffff0000 == 0xa9fe0000) && prefix >= 112 # ::ffff:169.254.0.0/112 (169.254.0.0/16)
345
+ ))
346
+ else
347
+ raise AddressFamilyError, "unsupported address family"
348
+ end
349
+ end
350
+
351
+ alias link_local_unicast? link_local?
352
+
353
+ # Returns true if the ipaddr is a multicast address. IPv4
354
+ # addresses in 224.0.0.0/4 and IPv6 multicast addresses
355
+ # in ff00::/8 are considered multicast. Multicast IPv4 addresses in the
356
+ # IPv4-mapped IPv6 address range are also considered multicast.
357
+ def multicast?
358
+ case @family
359
+ when Socket::AF_INET
360
+ (@addr & 0xf0000000 == 0xe0000000) && prefix >= 4 # 224.0.0.0/4
361
+ when Socket::AF_INET6
362
+ (@addr & 0xff00_0000_0000_0000_0000_0000_0000_0000 == 0xff00_0000_0000_0000_0000_0000_0000_0000) && prefix >= 8 || # ff00::/8
363
+ (@addr >> 32 == 0xffff &&
364
+ (@addr & 0xf0000000 == 0xe0000000) && prefix >= 100 # ::ffff:224.0.0.0/100 (224.0.0.0/4)
365
+ )
366
+ else
367
+ raise AddressFamilyError, "unsupported address family"
368
+ end
369
+ end
370
+
371
+ # Returns true if the ipaddr is a link-local multicast address. IPv4
372
+ # addresses in 224.0.0.0/24 and link-local IPv6 Multicast Addresses in ff02::/16
373
+ # are considered link-local multicast. Link-local multicast IPv4 addresses in the
374
+ # IPv4-mapped IPv6 address range are also considered link-local multicast.
375
+ def link_local_multicast?
376
+ case @family
377
+ when Socket::AF_INET
378
+ (@addr & 0xffffff00 == 0xe0000000) && prefix >= 24 # 224.0.0.0/24 Local Network Control Block
340
379
  when Socket::AF_INET6
341
- @addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000 || # fe80::/10
380
+ (@addr & 0xffff_0000_0000_0000_0000_0000_0000_0000 == 0xff02_0000_0000_0000_0000_0000_0000_0000) && prefix >= 16 || # ff02::/16
342
381
  (@addr >> 32 == 0xffff && (
343
- @addr & 0xffff0000 == 0xa9fe0000 # ::ffff:169.254.0.0/16
382
+ (@addr & 0xffffff00 == 0xe0000000) && prefix >= 120 # ::ffff:224.0.0.0/120 (224.0.0.0/24)
344
383
  ))
345
384
  else
346
385
  raise AddressFamilyError, "unsupported address family"
@@ -673,6 +712,7 @@ class IPAddr
673
712
  raise AddressFamilyError, "unsupported address family: #{family}"
674
713
  end
675
714
  end
715
+ requested_family = family
676
716
  prefix, prefixlen = addr.split('/', 2)
677
717
  if prefix =~ /\A\[(.*)\]\z/i
678
718
  prefix = $1
@@ -698,7 +738,7 @@ class IPAddr
698
738
  @family = Socket::AF_INET6
699
739
  end
700
740
  @zone_id = zone_id
701
- if family != Socket::AF_UNSPEC && @family != family
741
+ if requested_family != Socket::AF_UNSPEC && @family != requested_family
702
742
  raise AddressFamilyError, "address family mismatch"
703
743
  end
704
744
  if prefixlen
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.2.9
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinori MUSHA
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
42
42
  - !ruby/object:Gem::Version
43
43
  version: '0'
44
44
  requirements: []
45
- rubygems_version: 4.0.6
45
+ rubygems_version: 4.0.20
46
46
  specification_version: 4
47
47
  summary: A class to manipulate an IP address in ruby
48
48
  test_files: []