Method: NetAddr::CIDR#enumerate

Defined in:
lib/cidr.rb

#enumerate(options = nil) ⇒ Object

Synopsis

Provide all IP addresses contained within the IP space of this CIDR.

Example: cidr4 = NetAddr::CIDR.create(‘192.168.1.1/24’) cidr6 = NetAddr::CIDR.create(‘fec0::/64’) cidr4.enumerate(:Limit => 4, :Bitstep => 32) cidr6.enumerate(:Limit => 4, :Bitstep => 32, :Objectify => true)

Arguments:

  • options = Hash with the following keys:

    :Bitstep -- enumerate in X sized steps - Integer
    :Limit -- limit returned list to X number of items - Integer
    :Objectify -- if true, return NetAddr::CIDR objects
    :Short -- if true, return IPv6 addresses in short-hand notation
    

Returns:

  • Array of Strings, or Array of NetAddr::CIDR objects



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# File 'lib/cidr.rb', line 769

def enumerate(options=nil)
    known_args = [:Bitstep, :Limit, :Objectify, :Short]
    bitstep = 1
    objectify = false
    limit = nil
    short = false

    if (options)
        if (!options.kind_of? Hash)
            raise ArgumentError, "Expected Hash, but #{options.class} provided."
        end
        NetAddr.validate_args(options.keys,known_args)

        if( options.has_key?(:Bitstep) )
            bitstep = options[:Bitstep]
        end

        if( options.has_key?(:Objectify) && options[:Objectify] == true )
            objectify = true
        end

        if( options.has_key?(:Limit) )
            limit = options[:Limit]
        end

        if( options.has_key?(:Short) && options[:Short] == true )
            short = true
        end
    end

    list = []
    my_ip = @network
    change_mask = @hostmask | my_ip

    until ( change_mask != (@hostmask | @network) ) 
        if (!objectify)
            my_ip_s = NetAddr.ip_int_to_str(my_ip, @version)
            my_ip_s = NetAddr.shorten(my_ip_s) if (short && @version == 6)
            list.push( my_ip_s )
        else
            list.push( NetAddr.cidr_build(@version,my_ip) )
        end
        my_ip = my_ip + bitstep
        change_mask = @hostmask | my_ip
        if (limit)
            limit = limit -1
            break if (limit == 0)
        end
    end

    return(list)
end