Module: Proxy::DHCP::Infoblox::IpAddressArithmetic

Included in:
CommonCRUD, NetworkAddressesRegularExpressionGenerator, Provider
Defined in:
lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb

Instance Method Summary collapse

Instance Method Details

#cidr_to_bitmask(prefix_length) ⇒ Object



16
17
18
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 16

def cidr_to_bitmask(prefix_length)
  0xFFFFFFFF ^ (2**(32 - prefix_length) - 1)
end

#cidr_to_i(an_address_with_cidr) ⇒ Object



20
21
22
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 20

def cidr_to_i(an_address_with_cidr)
  an_address_with_cidr.split("/").last.to_i
end

#cidr_to_ip_mask(prefix_length) ⇒ Object



3
4
5
6
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 3

def cidr_to_ip_mask(prefix_length)
  bitmask = 0xFFFFFFFF ^ (2**(32 - prefix_length) - 1)
  (0..3).map { |i| (bitmask >> i * 8) & 0xFF }.reverse.join('.')
end

#i_to_ipv4(i) ⇒ Object



12
13
14
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 12

def i_to_ipv4(i)
  (0..3).inject([]) { |a, c| a.push((i >> (c * 8)) & 0xFF) }.reverse.join('.')
end

#ipv4_to_i(an_address) ⇒ Object



8
9
10
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 8

def ipv4_to_i(an_address)
  an_address.split('.').inject(0) { |a, c| (a << 8) + c.to_i }
end

#network_cidr_to_range(network_and_cidr) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/smart_proxy_dhcp_infoblox/ip_address_arithmetic.rb', line 24

def network_cidr_to_range(network_and_cidr)
  network_addr, cidr = network_and_cidr.split('/')
  mask = cidr_to_bitmask(cidr.to_i)

  range_start = ipv4_to_i(network_addr) & mask
  range_end = range_start | (0xFFFFFFFF ^ mask)

  [i_to_ipv4(range_start), i_to_ipv4(range_end)]
end