diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ipaddr.gemspec | 2 | ||||
-rw-r--r-- | lib/ipaddr.rb | 90 |
2 files changed, 89 insertions, 3 deletions
diff --git a/lib/ipaddr.gemspec b/lib/ipaddr.gemspec index 11ebe826ec..16df1708b0 100644 --- a/lib/ipaddr.gemspec +++ b/lib/ipaddr.gemspec @@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) Gem::Specification.new do |spec| spec.name = "ipaddr" - spec.version = "1.0.0" + spec.version = "1.2.0" spec.authors = ["Akinori MUSHA", "Hajimu UMEMOTO"] spec.email = ["knu@idaemons.org", "ume@mahoroba.org"] diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 6f70ebf773..ecf28ebb25 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -259,6 +259,50 @@ class IPAddr return @family == Socket::AF_INET6 end + # Returns true if the ipaddr is a loopback address. + def loopback? + case @family + when Socket::AF_INET + @addr & 0xff000000 == 0x7f000000 + when Socket::AF_INET6 + @addr == 1 + else + raise AddressFamilyError, "unsupported address family" + end + end + + # Returns true if the ipaddr is a private address. IPv4 addresses + # in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC + # 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC + # 4193 are considered private. + def private? + case @family + when Socket::AF_INET + @addr & 0xff000000 == 0x0a000000 || # 10.0.0.0/8 + @addr & 0xfff00000 == 0xac100000 || # 172.16.0.0/12 + @addr & 0xffff0000 == 0xc0a80000 # 192.168.0.0/16 + when Socket::AF_INET6 + @addr & 0xfe00_0000_0000_0000_0000_0000_0000_0000 == 0xfc00_0000_0000_0000_0000_0000_0000_0000 + else + raise AddressFamilyError, "unsupported address family" + end + end + + # Returns true if the ipaddr is a link-local address. IPv4 + # addresses in 169.254.0.0/16 reserved by RFC 3927 and Link-Local + # IPv6 Unicast Addresses in fe80::/10 reserved by RFC 4291 are + # considered link-local. + def link_local? + case @family + when Socket::AF_INET + @addr & 0xffff0000 == 0xa9fe0000 # 169.254.0.0/16 + when Socket::AF_INET6 + @addr & 0xffc0_0000_0000_0000_0000_0000_0000_0000 == 0xfe80_0000_0000_0000_0000_0000_0000_0000 + else + raise AddressFamilyError, "unsupported address family" + end + end + # Returns true if the ipaddr is an IPv4-mapped IPv6 address. def ipv4_mapped? return ipv6? && (@addr >> 32) == 0xffff @@ -266,6 +310,11 @@ class IPAddr # Returns true if the ipaddr is an IPv4-compatible IPv6 address. def ipv4_compat? + warn "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE + _ipv4_compat? + end + + def _ipv4_compat? if !ipv6? || (@addr >> 32) != 0 return false end @@ -273,6 +322,8 @@ class IPAddr return a != 0 && a != 1 end + private :_ipv4_compat? + # Returns a new ipaddr built by converting the native IPv4 address # into an IPv4-mapped IPv6 address. def ipv4_mapped @@ -285,6 +336,7 @@ class IPAddr # Returns a new ipaddr built by converting the native IPv4 address # into an IPv4-compatible IPv6 address. def ipv4_compat + warn "#{caller(1)[0]}: warning: IPAddr\##{__callee__} is obsolete" if $VERBOSE if !ipv4? raise InvalidAddressError, "not an IPv4 address" end @@ -295,7 +347,7 @@ class IPAddr # native IPv4 address. If the IP address is not an IPv4-mapped or # IPv4-compatible IPv6 address, returns self. def native - if !ipv4_mapped? && !ipv4_compat? + if !ipv4_mapped? && !_ipv4_compat? return self end return self.clone.set(@addr & IN4MASK, Socket::AF_INET) @@ -371,6 +423,35 @@ class IPAddr return clone.set(begin_addr, @family)..clone.set(end_addr, @family) end + # Returns the prefix length in bits for the ipaddr. + def prefix + case @family + when Socket::AF_INET + n = IN4MASK ^ @mask_addr + i = 32 + when Socket::AF_INET6 + n = IN6MASK ^ @mask_addr + i = 128 + else + raise AddressFamilyError, "unsupported address family" + end + while n.positive? + n >>= 1 + i -= 1 + end + i + end + + # Sets the prefix length in bits + def prefix=(prefix) + case prefix + when Integer + mask!(prefix) + else + raise InvalidPrefixError, "prefix must be an integer" + end + end + # Returns a string containing a human-readable representation of the # ipaddr. ("#<IPAddr: family:address/mask>") def inspect @@ -413,7 +494,8 @@ class IPAddr # Set current netmask to given mask. def mask!(mask) - if mask.kind_of?(String) + case mask + when String if mask =~ /\A\d+\z/ prefixlen = mask.to_i else @@ -422,6 +504,10 @@ class IPAddr raise InvalidPrefixError, "address family is not same" end @mask_addr = m.to_i + n = @mask_addr ^ m.instance_variable_get(:@mask_addr) + unless ((n + 1) & n).zero? + raise InvalidPrefixError, "invalid mask #{mask}" + end @addr &= @mask_addr return self end |