Class: IpMask
- Inherits:
-
Object
- Object
- IpMask
- Defined in:
- lib/cf_factory/help/ip_mask.rb
Instance Attribute Summary collapse
-
#bits ⇒ Object
readonly
Returns the value of attribute bits.
-
#ip_mask ⇒ Object
readonly
Returns the value of attribute ip_mask.
Class Method Summary collapse
- .create(ip_mask, bits = 32) ⇒ Object
- .create_from_cidr(cidr) ⇒ Object
- .create_from_num(number, bits) ⇒ Object
Instance Method Summary collapse
- #==(comp) ⇒ Object
- #are_all_in_range?(ip_mask) ⇒ Boolean
- #clean_mask ⇒ Object
- #divide(number_of_addresses) ⇒ Object
-
#divide_individually(array_with_number_of_addresses) ⇒ Object
Takes an array of IP-Address-Numbers into account and allocates corresponding IP address ranges.
- #free ⇒ Object
- #generate_free ⇒ Object
-
#initialize(ip_mask, bits = 32) ⇒ IpMask
constructor
if one parameter specified, means it’s not a range, it’s one address.
- #is_clean? ⇒ Boolean
- #is_in_range?(ip_address) ⇒ Boolean
- #to_bit_string ⇒ Object
- #to_mask ⇒ Object
- #to_num ⇒ Object
- #to_s ⇒ Object
- #used ⇒ Object
Constructor Details
#initialize(ip_mask, bits = 32) ⇒ IpMask
if one parameter specified, means it’s not a range, it’s one address
4 5 6 7 |
# File 'lib/cf_factory/help/ip_mask.rb', line 4 def initialize(ip_mask, bits = 32) #if one parameter specified, means it's not a range, it's one address @ip_mask = ip_mask @bits = bits.to_i end |
Instance Attribute Details
#bits ⇒ Object (readonly)
Returns the value of attribute bits.
2 3 4 |
# File 'lib/cf_factory/help/ip_mask.rb', line 2 def bits @bits end |
#ip_mask ⇒ Object (readonly)
Returns the value of attribute ip_mask.
2 3 4 |
# File 'lib/cf_factory/help/ip_mask.rb', line 2 def ip_mask @ip_mask end |
Class Method Details
.create(ip_mask, bits = 32) ⇒ Object
9 10 11 12 |
# File 'lib/cf_factory/help/ip_mask.rb', line 9 def self.create(ip_mask, bits = 32) cleaned = IpMask.new(ip_mask, bits) cleaned.clean_mask() end |
.create_from_cidr(cidr) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/cf_factory/help/ip_mask.rb', line 14 def self.create_from_cidr(cidr) #ip = cidr.split("/")[0].split(".") #bits = cidr.split("/")[1].to_i ip = cidr.split("/")[0] bits = cidr.split("/")[1] IpMask.new(ip, bits) end |
.create_from_num(number, bits) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cf_factory/help/ip_mask.rb', line 22 def self.create_from_num(number, bits) ip_bytes = [] 3.downto(0) {|i| div = 256 ** i b = number/div.to_i ip_bytes << b.to_i number = number % div } ip_bytes IpMask.new(ip_bytes.join("."), bits) end |
Instance Method Details
#==(comp) ⇒ Object
160 161 162 163 |
# File 'lib/cf_factory/help/ip_mask.rb', line 160 def ==(comp) puts "comp = #{comp.class} #{comp.inspect}" self.ip_mask == comp.ip_mask && self.bits == comp.bits end |
#are_all_in_range?(ip_mask) ⇒ Boolean
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/cf_factory/help/ip_mask.rb', line 148 def are_all_in_range?(ip_mask) #puts "check for #{ip_mask}" return false if ip_mask.bits < self.bits # comp_ip = ip_mask.to_num ip_base_num = self.to_num ip_bits_num = self.to_mask ip_clean = ip_base_num & ip_bits_num #puts "comp = #{(comp_ip & ip_bits_num)} ip_clean = #{ip_clean} (ip_bits_num = #{ip_bits_num})" (comp_ip & ip_bits_num) == ip_clean end |
#clean_mask ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/cf_factory/help/ip_mask.rb', line 75 def clean_mask ip_base_num = self.to_num ip_bits_num = self.to_mask() clean_mask = ip_base_num & ip_bits_num #puts "clean_mask = #{clean_mask}" IpMask.create_from_num(clean_mask,@bits) end |
#divide(number_of_addresses) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cf_factory/help/ip_mask.rb', line 88 def divide(number_of_addresses) possible_ranges = [] bits_to_move = (Math.log(number_of_addresses+1)/Math.log(2)).to_i puts "asked to allocate #{number_of_addresses}; that corresponds to #{bits_to_move} bits" puts "#{self.free()} are free" max_subnets = self.free()/number_of_addresses puts "given that every subnet should have #{number_of_addresses} addresses, there is currently space for #{max_subnets}" 0.upto(max_subnets-1) {|i| num = self.to_num() num += i*number_of_addresses possible_range = IpMask.create_from_num(num, 32 - bits_to_move) possible_ranges << possible_range puts "possible range: #{possible_range}" } possible_ranges end |
#divide_individually(array_with_number_of_addresses) ⇒ Object
Takes an array of IP-Address-Numbers into account and allocates corresponding IP address ranges
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/cf_factory/help/ip_mask.rb', line 106 def divide_individually(array_with_number_of_addresses) possible_ranges = [] num = self.to_num() bits_to_move = 32 - @bits remaining_addresses = self.free array_with_number_of_addresses.each() {|number_of_addresses_for_subnet| if (2 ** bits_to_move) < number_of_addresses_for_subnet puts "WARNING: could not allocate #{number_of_addresses_for_subnet} anymore (max #{(2 ** bits_to_move)})" next end bits_to_move = [(Math.log(number_of_addresses_for_subnet+1)/Math.log(2)).to_i, bits_to_move].min possible_range = IpMask.create_from_num(num, 32 - bits_to_move) unless self.are_all_in_range?(possible_range) puts "WARNING: the selected range '#{possible_range}' is outside the base range" next end num += number_of_addresses_for_subnet puts "[alloc #{number_of_addresses_for_subnet}] \tpossible range: #{possible_range}" possible_ranges << possible_range remaining_addresses -= (2 ** bits_to_move) #puts "[to allocate = #{number_of_addresses_for_subnet}] => free = #{possible_range.free}" } possible_ranges end |
#free ⇒ Object
55 56 57 |
# File 'lib/cf_factory/help/ip_mask.rb', line 55 def free() (2 ** 32) / used() end |
#generate_free ⇒ Object
63 64 65 |
# File 'lib/cf_factory/help/ip_mask.rb', line 63 def generate_free() self.to_num() end |
#is_clean? ⇒ Boolean
83 84 85 86 |
# File 'lib/cf_factory/help/ip_mask.rb', line 83 def is_clean? comp = self.clean_mask return self.ip_mask != comp.ip_mask end |
#is_in_range?(ip_address) ⇒ Boolean
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/cf_factory/help/ip_mask.rb', line 131 def is_in_range?(ip_address) comp_mask = IpMask.new(ip_address) # transform ip address string to numerical values for bitwise operations comp_ip = comp_mask.to_num #puts "ip_address checked = #{comp_ip.to_s(2)}" ip_base_num = self.to_num #puts "range_mask = #{ip_base_num.to_s(2)}" ip_bits_num = self.to_mask #puts "bit_mask = #{ip_bits_num.to_s(2)}" # perform an AND operation to get rid of the bits in the mask that don't count clean_mask = ip_base_num & ip_bits_num #puts "cleaned range_mask = #{ip_base_num.to_s(2)}" # the ip address belongs to the range, when an AND with the bitmask equals the cleaned mask #puts "(ip_address&bits = #{(comp_ip & ip_bits_num).to_s(2)}" (comp_ip & ip_bits_num) == clean_mask end |
#to_bit_string ⇒ Object
49 50 51 52 53 |
# File 'lib/cf_factory/help/ip_mask.rb', line 49 def to_bit_string bit_string = ("1"*@bits+"0"*(32-@bits)) #puts "#{bit_string}" bit_string end |
#to_mask ⇒ Object
44 45 46 47 |
# File 'lib/cf_factory/help/ip_mask.rb', line 44 def to_mask bit_string = self.to_bit_string r = bit_string.to_i(2) end |
#to_num ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/cf_factory/help/ip_mask.rb', line 34 def to_num sum = 0 exp = 3 @ip_mask.split(".").each() {|ip| sum += ip.to_i * (256 ** exp) exp -= 1 } sum end |
#to_s ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/cf_factory/help/ip_mask.rb', line 67 def to_s if @bits.to_i == 32 "#{@ip_mask}" else "#{@ip_mask}/#{@bits}" end end |
#used ⇒ Object
59 60 61 |
# File 'lib/cf_factory/help/ip_mask.rb', line 59 def used() 2 ** @bits end |