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
Defined Under Namespace
Modules: Dhcp, PrimaryInterface
Constant Summary
collapse
- IPV4_LINK_LOCAL_ADDR =
IPAddr.new('169.254.0.0/16').freeze
- IPV6_LINK_LOCAL_ADDR =
IPAddr.new('fe80::/10').freeze
- IPV6_UNIQUE_LOCAL_ADDR =
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
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
88
89
90
91
92
|
# 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) ⇒ Object
34
35
36
37
38
39
40
|
# 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) ⇒ Object
60
61
62
63
64
65
|
# 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
|
94
95
96
|
# 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) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# 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) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# 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
|