Class: NetworkUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/resolvers/windows/ffi/network_utils.rb

Class Method Summary collapse

Class Method Details

.address_to_string(addr) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/facter/resolvers/windows/ffi/network_utils.rb', line 6

def address_to_string(addr)
  return if addr[:lpSockaddr] == FFI::Pointer::NULL

  size = FFI::MemoryPointer.new(NetworkingFFI::INET6_ADDRSTRLEN + 1)
  buffer = FFI::MemoryPointer.new(:wchar, NetworkingFFI::INET6_ADDRSTRLEN + 1)
  error = nil
  3.times do
    error = NetworkingFFI::WSAAddressToStringW(addr[:lpSockaddr], addr[:iSockaddrLength],
                                               FFI::Pointer::NULL, buffer, size)
    break if error.zero?
  end
  unless error.zero?
    @log.debug 'address to string translation failed!'
    return
  end
  extract_address(buffer)
end

.extract_address(addr) ⇒ Object



24
25
26
# File 'lib/facter/resolvers/windows/ffi/network_utils.rb', line 24

def extract_address(addr)
  addr.read_wide_string_without_length.split('%').first
end

.find_mac_address(adapter) ⇒ Object



32
33
34
35
# File 'lib/facter/resolvers/windows/ffi/network_utils.rb', line 32

def find_mac_address(adapter)
  adapter[:PhysicalAddress].first(adapter[:PhysicalAddressLength])
                           .map { |e| format('%<mac_address>02x', mac_address: e.to_i) }.join(':').upcase
end

.ignored_ip_address(addr) ⇒ Object



28
29
30
# File 'lib/facter/resolvers/windows/ffi/network_utils.rb', line 28

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