ipaddr 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ipaddr.gemspec +1 -1
  4. data/lib/ipaddr.rb +57 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cb27d00fb843e1943f1a7640504fc2ea8970a91
4
- data.tar.gz: ab0c6306a7f5b36fce8e5f6c472864cf52598d0e
3
+ metadata.gz: 409feb75e6b948e43ff047dfab95e15d71cb744a
4
+ data.tar.gz: 7c4860ccfb49a0a60c896dbe992949f4e6414933
5
5
  SHA512:
6
- metadata.gz: '0197f4cad3d5497974b2669b49d66c658dc21ce8f5f17df574c16badea57fd0cac83c776cf3f646555049789cd9c0184f226ac9ec90a32285f3d46bc4809aab6'
7
- data.tar.gz: 37ac683429dd8c03202a50feb3c1b1155ebe1ff8e4f42e6707d8be5dae6ba5a21990a7b0e06c145c8e449f64deb446c664b90970ef2e3cb575dc0efe47255371
6
+ metadata.gz: 0f8c85dec1cd6721254f2696b665b552864454288caea21f794d652fcce9c7e8783e816fbf0902699c99cf859b197cef1ddd1bda9fd89b417fa6d19c5a151a5b
7
+ data.tar.gz: fd661af42fbe9c53c5a2cf386790b91d32e4d78b49373f2ad14ac2f5bc28ddab4f01efc4b3e3bf8736d701fe1db8ba1bfab3664d487eec2a23f212b10df3cc94
data/README.md CHANGED
@@ -13,7 +13,7 @@ version than comes with older versions of ruby.
13
13
  For example, you can add this line to your application's Gemfile:
14
14
 
15
15
  ```ruby
16
- gem 'ipaddr', '~> 1.1'
16
+ gem 'ipaddr', '~> 1.2'
17
17
  ```
18
18
 
19
19
  And then execute:
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "ipaddr"
7
- spec.version = "1.1.0"
7
+ spec.version = "1.2.0"
8
8
  spec.authors = ["Akinori MUSHA", "Hajimu UMEMOTO"]
9
9
  spec.email = ["knu@idaemons.org", "ume@mahoroba.org"]
10
10
 
@@ -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 "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" 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 "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" 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? && !ipv4_compat?
350
+ if !ipv4_mapped? && !_ipv4_compat?
299
351
  return self
300
352
  end
301
353
  return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
@@ -452,6 +504,10 @@ class IPAddr
452
504
  raise InvalidPrefixError, "address family is not same"
453
505
  end
454
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
455
511
  @addr &= @mask_addr
456
512
  return self
457
513
  end
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akinori MUSHA