module Facter::Util::Resolvers::Networking
Constants
- IPV4_LINK_LOCAL_ADDR
- IPV6_LINK_LOCAL_ADDR
- IPV6_UNIQUE_LOCAL_ADDR
Public Class Methods
build_binding(addr, mask_length)
click to toggle source
Creates a hash with IP, netmask and network. Works for IPV4 and IPV6 @param [String] addr The IP address @param [Integer] mask_length Number of 1 bits the netmask has
@return [Hash] Hash containing ip address, netmask and network
# File lib/facter/util/resolvers/networking/networking.rb, line 15 def build_binding(addr, mask_length) return if !addr || !mask_length ip = IPAddr.new(addr) mask_helper = nil scope = nil if ip.ipv6? scope = get_scope(addr) mask_helper = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' else mask_helper = '255.255.255.255' end mask = IPAddr.new(mask_helper).mask(mask_length) result = { address: addr, netmask: mask.to_s, network: ip.mask(mask_length).to_s } result[:scope6] = scope if scope result end
calculate_mask_length(netmask)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 88 def calculate_mask_length(netmask) ipaddr = IPAddr.new(netmask) ipaddr.to_i.to_s(2).count('1') end
expand_main_bindings(networking_facts)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 34 def expand_main_bindings(networking_facts) primary = networking_facts[:primary_interface] interfaces = networking_facts[:interfaces] expand_interfaces(interfaces) unless interfaces.nil? expand_primary_interface(networking_facts, primary) unless primary.nil? || interfaces.nil? end
find_valid_binding(bindings)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 60 def find_valid_binding(bindings) bindings.each do |binding| return binding unless ignored_ip_address(binding[:address]) end bindings.empty? ? nil : bindings.first end
format_mac_address(address)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 94 def format_mac_address(address) address.split('.').map { |e| format('%<mac_address>02s', mac_address: e) }.join(':').tr(' ', '0') end
get_scope(ip)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 42 def get_scope(ip) require 'socket' scope6 = [] addrinfo = Addrinfo.new(['AF_INET6', 0, nil, ip], :INET6) scope6 << 'compat,' if IPAddr.new(ip).ipv4_compat? scope6 << if addrinfo.ipv6_linklocal? 'link' elsif addrinfo.ipv6_sitelocal? 'site' elsif addrinfo.ipv6_loopback? 'host' else 'global' end scope6.join end
ignored_ip_address(addr)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 71 def ignored_ip_address(addr) return true if addr.empty? ip = IPAddr.new(addr) return true if ip.loopback? [ IPV4_LINK_LOCAL_ADDR, IPV6_LINK_LOCAL_ADDR, IPV6_UNIQUE_LOCAL_ADDR ].each do |range| return true if range.include?(ip) end false end
Private Class Methods
expand_binding(values, bindings, ipv4_type = true)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 113 def expand_binding(values, bindings, ipv4_type = true) binding = find_valid_binding(bindings) ip_protocol_type = ipv4_type ? '' : '6' values["ip#{ip_protocol_type}".to_sym] = binding[:address] values["netmask#{ip_protocol_type}".to_sym] = binding[:netmask] values["network#{ip_protocol_type}".to_sym] = binding[:network] values[:scope6] = get_scope(binding[:address]) unless ipv4_type end
expand_interfaces(interfaces)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 100 def expand_interfaces(interfaces) interfaces.each_value do |values| expand_binding(values, values[:bindings]) if values[:bindings] expand_binding(values, values[:bindings6], false) if values[:bindings6] end end
expand_primary_interface(networking_facts, primary)
click to toggle source
# File lib/facter/util/resolvers/networking/networking.rb, line 107 def expand_primary_interface(networking_facts, primary) networking_facts[:interfaces][primary].each do |key, value| networking_facts[key] = value unless %i[bindings bindings6].include?(key) end end