Module: Facter::Util::Resolvers::Ffi::Hostname

Extended by:
FFI::Library
Defined in:
lib/facter/util/resolvers/ffi/hostname.rb

Overview

Since:

  • 2.0.0

Constant Summary collapse

HOST_NAME_MAX =

Since:

  • 2.0.0

64

Class Method Summary collapse

Class Method Details

.getffiaddrinfo(hostname) ⇒ Object

Since:

  • 2.0.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/facter/util/resolvers/ffi/hostname.rb', line 39

def self.getffiaddrinfo(hostname)
  ret = FFI::MemoryPointer.new(:pointer)

  hints = Facter::Util::Resolvers::Ffi::AddrInfo.new
  hints[:ai_family] = Socket::AF_UNSPEC
  hints[:ai_socketype] = Socket::SOCK_STREAM
  hints[:ai_flags] = Socket::AI_CANONNAME

  res = Hostname.getaddrinfo(hostname, '', hints.to_ptr, ret)
  log = Log.new(self)
  log.debug("FFI Getaddrinfo finished with exit status: #{res}")
  log.debug("FFI Getaddrinfo returned an AddrInfo struct at address: #{ret}")
  return unless res.zero?

  return if ret == FFI::Pointer::NULL || ret.read_pointer == FFI::Pointer::NULL

  begin
    addr = Facter::Util::Resolvers::Ffi::AddrInfo.new(ret.read_pointer)
    name = addr[:ai_canonname]
    log.debug("FFI AddrInfo struct has the fqdn: #{name}")
    return if !name || hostname == name

    name
  ensure
    Hostname.freeaddrinfo(ret.read_pointer)
  end
end

.getffihostnameObject

Since:

  • 2.0.0



30
31
32
33
34
35
36
37
# File 'lib/facter/util/resolvers/ffi/hostname.rb', line 30

def self.getffihostname
  raw_hostname = ::FFI::MemoryPointer.new(:char, HOST_NAME_MAX)

  res = Hostname.gethostname(raw_hostname, HOST_NAME_MAX)
  return unless res.zero?

  raw_hostname.read_string
end