Class | IPAddr
In: | ipaddr.rb
Parent: | Object

IPAddr provides a set of methods to manipulate an IP address. Both IPv4 and IPv6 are supported.

### Methods

& << == === >> hton include? inspect ip6_arpa ip6_int ipv4? ipv4_compat ipv4_compat? ipv4_mapped ipv4_mapped? ipv6? mask native new new_ntoh ntop reverse to_i to_s to_string | ~

### Constants

IN4MASK | = | 0xffffffff
IN6MASK | = | 0xffffffffffffffffffffffffffffffff
IN6FORMAT | = | (["%.4x"] * 8).join(':')

### Attributes

family | [R] | Returns the address family of this IP address.

### Public Class methods

new(addr = '::', family = Socket::AF_UNSPEC)

Creates a new ipaddr containing the given human readable form of an IP address. It also accepts `address/prefixlen’ and `address/mask’. When prefixlen or mask is specified, it returns a masked ipaddr. IPv6 address may beenclosed with `[’ and `]’.

Although an address family is determined automatically from a specified address, you can specify an address family explicitly by the optional second argument.

[Source]

    # File ipaddr.rb, line 403
      def initialize(addr = '::', family = Socket::AF_UNSPEC)
        if !addr.kind_of?(String)
          if family != Socket::AF_INET6 && family != Socket::AF_INET
            raise ArgumentError, "unsupported address family"
          end
          set(addr, family)
          @mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK
          return
        end
        prefix, prefixlen = addr.split('/')
        if prefix =~ /^\[(.*)\]$/i
          prefix = $1
          family = Socket::AF_INET6
        end
        # It seems AI_NUMERICHOST doesn't do the job.
        #Socket.getaddrinfo(left, nil, Socket::AF_INET6, Socket::SOCK_STREAM, nil,
        #                  Socket::AI_NUMERICHOST)
        begin
          IPSocket.getaddress(prefix)               # test if address is vaild
        rescue
          raise ArgumentError, "invalid address"
        end
        @addr = @family = nil
        if family == Socket::AF_UNSPEC || family == Socket::AF_INET
          @addr = in_addr(prefix)
          if @addr
            @family = Socket::AF_INET
          end
        end
        if !@addr && (family == Socket::AF_UNSPEC || family == Socket::AF_INET6)
          @addr = in6_addr(prefix)
          @family = Socket::AF_INET6
        end
        if family != Socket::AF_UNSPEC && @family != family
          raise ArgumentError, "address family unmatch"
        end
        if prefixlen
          mask!(prefixlen)
        else
          @mask_addr = (family == Socket::AF_INET) ? IN4MASK : IN6MASK
        end
      end

new_ntoh(addr)

Creates a new ipaddr containing the given network byte ordered string form of an IP address.

[Source]

    # File ipaddr.rb, line 91
      def IPAddr::new_ntoh(addr)
        return IPAddr.new(IPAddr::ntop(addr))
      end

ntop(addr)

Convert a network byte ordered string form of an IP address into human readable form.

[Source]

    # File ipaddr.rb, line 97
      def IPAddr::ntop(addr)
        case addr.size
        when 4
          s = addr.unpack('C4').join('.')
        when 16
          s = IN6FORMAT % addr.unpack('n8')
        else
          raise ArgumentError, "unsupported address family"
        end
        return s
      end

### Public Instance methods

&(other)

Returns a new ipaddr built by bitwise AND.

[Source]

    # File ipaddr.rb, line 110
      def &(other)
        return self.clone.set(@addr & other.to_i)
      end

<<(num)

Returns a new ipaddr built by bitwise left shift.

[Source]

    # File ipaddr.rb, line 125
      def <<(num)
        return self.clone.set(addr_mask(@addr << num))
      end

==(other)

Returns true if two ipaddr are equal.

[Source]

    # File ipaddr.rb, line 135
      def ==(other)
        if other.kind_of?(IPAddr) && @family != other.family
          return false
        end
        return (@addr == other.to_i)
      end

===(other)

Alias for include?

>>(num)

Returns a new ipaddr built by bitwise right-shift.

[Source]

    # File ipaddr.rb, line 120
      def >>(num)
        return self.clone.set(@addr >> num)
      end

hton()

Returns a network byte ordered string form of the IP address.

[Source]

    # File ipaddr.rb, line 226
      def hton
        case @family
        when Socket::AF_INET
          return [@addr].pack('N')
        when Socket::AF_INET6
          return (0..7).map { |i|
            (@addr >> (112 - 16 * i)) & 0xffff
          }.pack('n8')
        else
          raise "unsupported address family"
        end
      end

include?(other)

Returns true if the given ipaddr is in the range.

e.g.:

      require 'ipaddr'
      net1 = IPAddr.new("192.168.2.0/24")
      p net1.include?(IPAddr.new("192.168.2.0"))        #=> true
      p net1.include?(IPAddr.new("192.168.2.255"))      #=> true
      p net1.include?(IPAddr.new("192.168.3.0"))        #=> false

[Source]

    # File ipaddr.rb, line 156
      def include?(other)
        if ipv4_mapped?
          if (@mask_addr >> 32) != 0xffffffffffffffffffffffff
            return false
          end
          mask_addr = (@mask_addr & IN4MASK)
          addr = (@addr & IN4MASK)
          family = Socket::AF_INET
        else
          mask_addr = @mask_addr
          addr = @addr
          family = @family
        end
        if other.kind_of?(IPAddr)
          if other.ipv4_mapped?
            other_addr = (other.to_i & IN4MASK)
            other_family = Socket::AF_INET
          else
            other_addr = other.to_i
            other_family = other.family
          end
        else # Not IPAddr - assume integer in same family as us
          other_addr   = other.to_i
          other_family = family
        end

        if family != other_family
          return false
        end
        return ((addr & mask_addr) == (other_addr & mask_addr))
      end

inspect()

Returns a string containing a human-readable representation of the ipaddr. ("#<IPAddr: family:address/mask>")

[Source]

    # File ipaddr.rb, line 322
      def inspect
        case @family
        when Socket::AF_INET
          af = "IPv4"
        when Socket::AF_INET6
          af = "IPv6"
        else
          raise "unsupported address family"
        end
        return sprintf("#<%s: %s:%s/%s>", self.class.name,
                       af, _to_string(@addr), _to_string(@mask_addr))
      end

ip6_arpa()

Returns a string for DNS reverse lookup compatible with RFC3172.

[Source]

    # File ipaddr.rb, line 305
      def ip6_arpa
        if !ipv6?
          raise ArgumentError, "not an IPv6 address"
        end
        return _reverse + ".ip6.arpa"
      end

ip6_int()

Returns a string for DNS reverse lookup compatible with RFC1886.

[Source]

    # File ipaddr.rb, line 313
      def ip6_int
        if !ipv6?
          raise ArgumentError, "not an IPv6 address"
        end
        return _reverse + ".ip6.int"
      end

ipv4?()

Returns true if the ipaddr is an IPv4 address.

[Source]

    # File ipaddr.rb, line 240
      def ipv4?
        return @family == Socket::AF_INET
      end

ipv4_compat()

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-compatible IPv6 address.

[Source]

    # File ipaddr.rb, line 274
      def ipv4_compat
        if !ipv4?
          raise ArgumentError, "not an IPv4 address"
        end
        return self.clone.set(@addr, Socket::AF_INET6)
      end

ipv4_compat?()

Returns true if the ipaddr is an IPv4-compatible IPv6 address.

[Source]

    # File ipaddr.rb, line 255
      def ipv4_compat?
        if !ipv6? || (@addr >> 32) != 0
          return false
        end
        a = (@addr & IN4MASK)
        return a != 0 && a != 1
      end

ipv4_mapped()

Returns a new ipaddr built by converting the native IPv4 address into an IPv4-mapped IPv6 address.

[Source]

    # File ipaddr.rb, line 265
      def ipv4_mapped
        if !ipv4?
          raise ArgumentError, "not an IPv4 address"
        end
        return self.clone.set(@addr | 0xffff00000000, Socket::AF_INET6)
      end

ipv4_mapped?()

Returns true if the ipaddr is an IPv4-mapped IPv6 address.

[Source]

    # File ipaddr.rb, line 250
      def ipv4_mapped?
        return ipv6? && (@addr >> 32) == 0xffff
      end

ipv6?()

Returns true if the ipaddr is an IPv6 address.

[Source]

    # File ipaddr.rb, line 245
      def ipv6?
        return @family == Socket::AF_INET6
      end

mask(prefixlen)

Returns a new ipaddr built by masking IP address with the given prefixlen/netmask. (e.g. 8, 64, "255.255.255.0", etc.)

[Source]

    # File ipaddr.rb, line 144
      def mask(prefixlen)
        return self.clone.mask!(prefixlen)
      end

native()

Returns a new ipaddr built by converting the IPv6 address into a native IPv4 address. If the IP address is not an IPv4-mapped or IPv4-compatible IPv6 address, returns self.

[Source]

    # File ipaddr.rb, line 284
      def native
        if !ipv4_mapped? && !ipv4_compat?
          return self
        end
        return self.clone.set(@addr & IN4MASK, Socket::AF_INET)
      end

reverse()

Returns a string for DNS reverse lookup. It returns a string in RFC3172 form for an IPv6 address.

[Source]

    # File ipaddr.rb, line 293
      def reverse
        case @family
        when Socket::AF_INET
          return _reverse + ".in-addr.arpa"
        when Socket::AF_INET6
          return ip6_arpa
        else
          raise "unsupported address family"
        end
      end

to_i()

Returns the integer representation of the ipaddr.

[Source]

    # File ipaddr.rb, line 190
      def to_i
        return @addr
      end

to_s()

Returns a string containing the IP address representation.

[Source]

    # File ipaddr.rb, line 195
      def to_s
        str = to_string
        return str if ipv4?

        str.gsub!(/\b0{1,3}([\da-f]+)\b/i, '\1')
        loop do
          break if str.sub!(/\A0:0:0:0:0:0:0:0\Z/, '::')
          break if str.sub!(/\b0:0:0:0:0:0:0\b/, ':')
          break if str.sub!(/\b0:0:0:0:0:0\b/, ':')
          break if str.sub!(/\b0:0:0:0:0\b/, ':')
          break if str.sub!(/\b0:0:0:0\b/, ':')
          break if str.sub!(/\b0:0:0\b/, ':')
          break if str.sub!(/\b0:0\b/, ':')
          break
        end
        str.sub!(/:{3,}/, '::')

        if /\A::(ffff:)?([\da-f]{1,4}):([\da-f]{1,4})\Z/i =~ str
          str = sprintf('::%s%d.%d.%d.%d', $1, $2.hex / 256, $2.hex % 256, $3.hex / 256, $3.hex % 256)
        end

        str
      end

to_string()

Returns a string containing the IP address representation in canonical form.

[Source]

    # File ipaddr.rb, line 221
      def to_string
        return _to_string(@addr)
      end

|(other)

Returns a new ipaddr built by bitwise OR.

[Source]

    # File ipaddr.rb, line 115
      def |(other)
        return self.clone.set(@addr | other.to_i)
      end

~()

Returns a new ipaddr built by bitwise negation.

[Source]

    # File ipaddr.rb, line 130
      def ~
        return self.clone.set(addr_mask(~@addr))
      end

[Validate]