Sha256: 1de6a4db0c4e0cf929a63b32b113533e4bdf14dfc8402ca73e0c6bebc6368104

Contents?: true

Size: 1.19 KB

Versions: 36

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

class NetworkUtils
  @log = Facter::Log.new(self)
  class << self
    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

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

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

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

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
facter-4.10.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.9.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.8.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.7.1 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.7.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.6.1 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.6.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.5.2 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.5.1 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.5.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.4.3 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.4.2 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.4.1 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.4.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.3.1 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.3.0 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.2.14 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.2.13 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.2.12 lib/facter/resolvers/windows/ffi/network_utils.rb
facter-4.2.11 lib/facter/resolvers/windows/ffi/network_utils.rb