Class: NetAddr::CIDRv6

Inherits:
CIDR
  • Object
show all
Defined in:
lib/cidr.rb

Overview

IPv6 CIDR address - Inherits all methods from NetAddr::CIDR. Addresses of this class are composed of a 128-bit address space.

Instance Attribute Summary

Attributes inherited from CIDR

#all_f, #tag, #version

Instance Method Summary collapse

Methods inherited from CIDR

#arpa, #bits, #cmp, #contains?, create, #desc, #enumerate, #eql?, #fill_in, #ip, #is_contained?, #last, #matches?, #multicast_mac, #netmask, #network, #next_ip, #next_subnet, #nth, #packed_hostmask, #packed_ip, #packed_netmask, #packed_network, #packed_wildcard_mask, #range, #remainder, #resize, #resize!, #size, #subnet, #wildcard_mask

Constructor Details

#initialize(addr, options = nil) ⇒ CIDRv6

Synopsis

Return a CIDRv6 object. CIDR formatted netmasks take precedence over extended formatted ones. CIDR address defaults to a host network (/128) if netmask not provided. :PackedNetmask takes precedence over netmask given within CIDR address.

cidr6 = NetAddr::CIDRv6.new(‘fec0::/64’) cidr6 = NetAddr::CIDRv6.new(‘fec0::/64’,

:Tag => {'interface' => 'g0/1'})

cidr6_2 = NetAddr::CIDRv6.new(‘::ffff:192.168.1.1/96’) cidr6_3 = NetAddr::CIDRv6.new(‘fec0::’,

:WildcardMask => '0000:ffff::ffff')

Arguments:

  • CIDR address as a String, or a packed IP address as an Integer

  • Optional Hash with the following keys:

    :PackedNetmask -- Integer representation of an IP Netmask (optional)
    :Tag -- Custom descriptor tag - Hash, tag => value. (optional)
    :WildcardMask -- 2 element Array. First element contains a special bit mask used for
                     advanced IP pattern matching. The second element should be :inversed if this
                     bit mask is in inverse format. (optional)
    


1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
# File 'lib/cidr.rb', line 1996

def initialize(addr, options=nil)
    known_args = [:PackedNetmask, :Tag, :WildcardMask]
    @tag = {}
    @version = 6
    @max_bits = 128
    @all_f = 2**@max_bits - 1
    
    # validate addr arg
    if (addr.kind_of?(String))
        cidr = addr
    elsif (addr.kind_of?(Integer))
        packed_ip = addr
    else
        raise ArgumentError, "String or Integer expected for argument 'addr' but #{addr.class} provided."
    end
    
    # validate options
    if (options)     
        raise ArgumentError, "Hash expected for argument 'options' but " +
                             "#{options.class} provided." if (!options.kind_of?(Hash) )
        NetAddr.validate_args(options.keys,known_args)        
    
        if (options.has_key?(:PackedNetmask))
            packed_netmask = options[:PackedNetmask]
            raise ArgumentError, "Expected Integer, but #{packed_netmask.class} " +
                                 "provided for option :PackedNetmask." if (!packed_netmask.kind_of?(Integer))
        end    
    
        if (options.has_key?(:Tag))
            @tag = options[:Tag]
            if (!@tag.kind_of? Hash)
                raise ArgumentError, "Expected Hash, but #{@tag.class} provided for option :Tag."
            end
        end

        if (options.has_key?(:WildcardMask))
            if (!options[:WildcardMask].kind_of?(Array))
                raise ArgumentError, "Expected Array, but #{options[:WildcardMask].class} provided for option :WildcardMask."
            end
            wildcardmask = options[:WildcardMask][0]
            inversed = false
            inversed = true if (options[:WildcardMask][1] && options[:WildcardMask][1] == :inversed)

            if (wildcardmask.kind_of?(Integer))
               begin
                    @wildcard_mask = wildcardmask
                    NetAddr.validate_ip_addr(@wildcard_mask, :Version => @version)
                rescue NetAddr::ValidationError
                    raise NetAddr::ValidationError, ":WildcardMask must be a valid IPv#{@version} address."
                end 
            else
                begin
                    @wildcard_mask = NetAddr.pack_ip_addr(wildcardmask, :Version => @version)
                rescue NetAddr::ValidationError
                    raise NetAddr::ValidationError, ":WildcardMask must be a valid IPv#{@version} address."
                end
            end

            @wildcard_mask = @wildcard_mask ^ @all_f if (inversed)
        end
    end


    if (packed_ip)
        # validate & store packed_ip
        NetAddr.validate_ip_addr(packed_ip, :Version => @version)
        @ip = packed_ip

    else
        # if ipv6 and extended netmask was provided, then raise exception
        raise ArgumentError, "Garbage provided at end of IPv6 address." if (cidr =~ /.+\s+.+/ )

        # get ip and netmask
        if (cidr =~ /\//) 
            ip,netmask = cidr.split(/\//)
            if (!ip || !netmask)
                raise ArgumentError, "CIDR address is improperly formatted. Missing netmask after '/' character." 
            end
        else
            ip = cidr
        end

        # pack ip
        @ip = NetAddr.pack_ip_addr(ip, :Version => @version)
    end

    # if no netmask or packed_netmask, then set
    # else validate. packed_netmask takes precedence over netmask
    if (!netmask && !packed_netmask)
        @netmask = @all_f
    else
        if (packed_netmask)
            NetAddr.validate_ip_netmask(packed_netmask, :Packed => true, :Version => @version)
            @netmask = packed_netmask
        else
            NetAddr.validate_ip_netmask(netmask, :Version => @version)
            @netmask = NetAddr.pack_ip_netmask(netmask, :Version => @version)
        end
    end

    # set @network & @hostmask
    @network = (@ip & @netmask)
    @hostmask = @netmask ^ @all_f
    @wildcard_mask = @hostmask if (!@wildcard_mask)

end