Module: Facter::Util::Resolvers::Networking

Defined in:
lib/facter/util/resolvers/networking.rb,
lib/facter/util/resolvers/networking/dhcp.rb,
lib/facter/util/resolvers/networking/networking.rb,
lib/facter/util/resolvers/networking/primary_interface.rb

Overview

Since:

  • 2.0.0

Defined Under Namespace

Modules: Dhcp, PrimaryInterface

Class Method Summary collapse

Class Method Details

.build_binding(addr, mask_length) ⇒ Hash

Creates a hash with IP, netmask and network. Works for IPV4 and IPV6

Parameters:

  • addr (String)

    The IP address

  • mask_length (Integer)

    Number of 1 bits the netmask has

Returns:

  • (Hash)

    Hash containing ip address, netmask and network

Since:

  • 2.0.0



15
16
17
18
19
20
21
22
23
# File 'lib/facter/util/resolvers/networking.rb', line 15

def build_binding(addr, mask_length)
  return if !addr || !mask_length

  ip = IPAddr.new(addr)
  mask_helper = ip.ipv6? ? 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' : '255.255.255.255'
  mask = IPAddr.new(mask_helper).mask(mask_length)

  { address: addr, netmask: mask.to_s, network: ip.mask(mask_length).to_s }
end

.calculate_mask_length(netmask) ⇒ Object

Since:

  • 2.0.0



71
72
73
74
75
# File 'lib/facter/util/resolvers/networking/networking.rb', line 71

def calculate_mask_length(netmask)
  ipaddr = IPAddr.new(netmask)

  ipaddr.to_i.to_s(2).count('1')
end

.expand_main_bindings(networking_facts) ⇒ Object

Since:

  • 2.0.0



25
26
27
28
29
30
31
# File 'lib/facter/util/resolvers/networking.rb', line 25

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) ⇒ Object

Since:

  • 2.0.0



51
52
53
54
55
56
# File 'lib/facter/util/resolvers/networking.rb', line 51

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) ⇒ Object

Since:

  • 2.0.0



77
78
79
# File 'lib/facter/util/resolvers/networking/networking.rb', line 77

def format_mac_address(address)
  address.split('.').map { |e| format('%<mac_address>02s', mac_address: e) }.join(':').tr(' ', '0')
end

.get_scope(ip) ⇒ Object

Since:

  • 2.0.0



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/facter/util/resolvers/networking.rb', line 33

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) ⇒ Object

Since:

  • 2.0.0



58
59
60
# File 'lib/facter/util/resolvers/networking.rb', line 58

def ignored_ip_address(addr)
  addr.empty? || addr.start_with?('127.', '169.254.') || addr.start_with?('fe80') || addr.eql?('::1')
end