Class: CfFactory::IpMask

Inherits:
Object
  • Object
show all
Defined in:
lib/cf_factory/help/ip_mask.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip_mask, bits = 32) ⇒ IpMask

if one parameter specified, means it’s not a range, it’s one address



5
6
7
8
# File 'lib/cf_factory/help/ip_mask.rb', line 5

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

#bitsObject (readonly)

Returns the value of attribute bits.



3
4
5
# File 'lib/cf_factory/help/ip_mask.rb', line 3

def bits
  @bits
end

#ip_maskObject (readonly)

Returns the value of attribute ip_mask.



3
4
5
# File 'lib/cf_factory/help/ip_mask.rb', line 3

def ip_mask
  @ip_mask
end

Class Method Details

.create(ip_mask, bits = 32) ⇒ Object



10
11
12
13
# File 'lib/cf_factory/help/ip_mask.rb', line 10

def self.create(ip_mask, bits = 32)
  cleaned = IpMask.new(ip_mask, bits)
  cleaned.clean_mask()
end

.create_from_cidr(cidr) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/cf_factory/help/ip_mask.rb', line 15

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



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cf_factory/help/ip_mask.rb', line 23

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



161
162
163
164
# File 'lib/cf_factory/help/ip_mask.rb', line 161

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

Returns:

  • (Boolean)


149
150
151
152
153
154
155
156
157
158
159
# File 'lib/cf_factory/help/ip_mask.rb', line 149

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_maskObject



76
77
78
79
80
81
82
# File 'lib/cf_factory/help/ip_mask.rb', line 76

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cf_factory/help/ip_mask.rb', line 89

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cf_factory/help/ip_mask.rb', line 107

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

#freeObject



56
57
58
# File 'lib/cf_factory/help/ip_mask.rb', line 56

def free()
  (2 ** 32) / used()
end

#generate_freeObject



64
65
66
# File 'lib/cf_factory/help/ip_mask.rb', line 64

def generate_free()
  self.to_num()
end

#is_clean?Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/cf_factory/help/ip_mask.rb', line 84

def is_clean?
  comp = self.clean_mask
  return self.ip_mask != comp.ip_mask
end

#is_in_range?(ip_address) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cf_factory/help/ip_mask.rb', line 132

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_stringObject



50
51
52
53
54
# File 'lib/cf_factory/help/ip_mask.rb', line 50

def to_bit_string    
  bit_string = ("1"*@bits+"0"*(32-@bits))
  #puts "#{bit_string}"
  bit_string
end

#to_maskObject



45
46
47
48
# File 'lib/cf_factory/help/ip_mask.rb', line 45

def to_mask
  bit_string = self.to_bit_string 
  r = bit_string.to_i(2)
end

#to_numObject



35
36
37
38
39
40
41
42
43
# File 'lib/cf_factory/help/ip_mask.rb', line 35

def to_num
  sum = 0
  exp = 3
  @ip_mask.split(".").each() {|ip|
    sum += ip.to_i * (256 ** exp)
    exp -= 1
  }
  sum
end

#to_sObject



68
69
70
71
72
73
74
# File 'lib/cf_factory/help/ip_mask.rb', line 68

def to_s
  if @bits.to_i == 32
    "#{@ip_mask}"
  else
    "#{@ip_mask}/#{@bits}"
  end
end

#usedObject



60
61
62
# File 'lib/cf_factory/help/ip_mask.rb', line 60

def used()
  2 ** @bits 
end