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

Defined in:
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

Constant Summary collapse

IPAddr.new('169.254.0.0/16').freeze
IPAddr.new('fe80::/10').freeze
IPV6_UNIQUE_LOCAL_ADDR =

RFC4193

Since:

  • 2.0.0

IPAddr.new('fc00::/7').freeze

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
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object

Since:

  • 2.0.0



91
92
93
94
95
# File 'lib/facter/util/resolvers/networking/networking.rb', line 91

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



34
35
36
37
38
39
40
41
42
# 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?
  return if primary.nil? || interfaces.nil? || networking_facts.nil?

  expand_primary_interface(networking_facts, primary)
end

.find_valid_binding(bindings) ⇒ Object

Since:

  • 2.0.0



63
64
65
66
67
68
# File 'lib/facter/util/resolvers/networking/networking.rb', line 63

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



97
98
99
# File 'lib/facter/util/resolvers/networking/networking.rb', line 97

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/facter/util/resolvers/networking/networking.rb', line 44

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/facter/util/resolvers/networking/networking.rb', line 74

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