ipaddr 1.1.0 → 1.2.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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/ipaddr.gemspec +1 -1
- data/lib/ipaddr.rb +57 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 409feb75e6b948e43ff047dfab95e15d71cb744a
|
4
|
+
data.tar.gz: 7c4860ccfb49a0a60c896dbe992949f4e6414933
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f8c85dec1cd6721254f2696b665b552864454288caea21f794d652fcce9c7e8783e816fbf0902699c99cf859b197cef1ddd1bda9fd89b417fa6d19c5a151a5b
|
7
|
+
data.tar.gz: fd661af42fbe9c53c5a2cf386790b91d32e4d78b49373f2ad14ac2f5bc28ddab4f01efc4b3e3bf8736d701fe1db8ba1bfab3664d487eec2a23f212b10df3cc94
|
data/README.md
CHANGED
data/ipaddr.gemspec
CHANGED
@@ -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.
|
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
|
|
data/lib/ipaddr.rb
CHANGED
@@ -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? && !
|
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
|