<commit_before>require 'ipaddr'

# Fact: defaultroute
#
# Purpose: Return the default route for a host.
#
# Resolution:
#   Runs netstat, and returns the gateway associated with the destination
#   "default" or "0.0.0.0".
#
# Caveats:
#

Facter.add(:defaultroute) do
  confine :kernel => Facter::Util::NetStat.supported_platforms
  setcode do
    Facter::Util::NetStat.get_route_value('default', 'gw') ||
    Facter::Util::NetStat.get_route_value('0.0.0.0', 'gw')
  end
end

# Fact: defaultroute_interface
#
# Purpose: Return the interface uses for the host's default route.
#
# Resolution:
#   Runs netstat, and returns the interface associated with the route for the
#   destination "default" or "0.0.0.0".
#
#   If the default route listing only includes the gateway and not the
#   interface (as is the case on Solaris), return the first interface whose
#   network range includes the default gateway.
#
# Caveats:
#

Facter.add(:defaultroute_interface) do
  confine :kernel => Facter::Util::NetStat.supported_platforms
  setcode do
    Facter::Util::NetStat.get_route_value('default', 'iface') ||
    Facter::Util::NetStat.get_route_value('0.0.0.0', 'iface')
  end
end

Facter.add(:defaultroute_interface) do
  confine :kernel => Facter::Util::IP.supported_platforms
  setcode do
    return nil unless defaultroute = Facter.value(:defaultroute)
    gw = IPAddr.new(defaultroute)

    Facter::Util::IP.get_interfaces.collect { |i| Facter::Util::IP.alphafy(i) }.
    detect do |i| 
      range = Facter.value('network_' + i) +
              '/' +
              Facter.value('netmask_' + i)
      IPAddr.new(range).include?(gw)
    end
  end
end
<commit_msg>Fix defaultroute.db<commit_after>require 'ipaddr'

begin
  require 'facter/util/netstat.rb'
rescue LoadError => e
  # puppet apply does not add module lib directories to the $LOAD_PATH (See
  # #4248). It should (in the future) but for the time being we need to be
  # defensive which is what this rescue block is doing.
  rb_file = File.join(File.dirname(__FILE__), 'util', 'netstat.rb')
  load rb_file if File.exists?(rb_file) or raise e
end

# Fact: defaultroute
#
# Purpose: Return the default route for a host.
#
# Resolution:
#   Runs netstat, and returns the gateway associated with the destination
#   "default" or "0.0.0.0".
#
# Caveats:
#

Facter.add(:defaultroute) do
  confine :kernel => Facter::Util::NetStat.supported_platforms
  setcode do
    Facter::Util::NetStat.get_route_value('default', 'gw') ||
    Facter::Util::NetStat.get_route_value('0.0.0.0', 'gw')
  end
end

# Fact: defaultroute_interface
#
# Purpose: Return the interface uses for the host's default route.
#
# Resolution:
#   Runs netstat, and returns the interface associated with the route for the
#   destination "default" or "0.0.0.0".
#
#   If the default route listing only includes the gateway and not the
#   interface (as is the case on Solaris), return the first interface whose
#   network range includes the default gateway.
#
# Caveats:
#

Facter.add(:defaultroute_interface) do
  confine :kernel => Facter::Util::IP.supported_platforms
  setcode do
    defaultroute = Facter.value(:defaultroute)
    if defaultroute
      gw = IPAddr.new(defaultroute)
      Facter::Util::IP.get_interfaces.collect { |i| Facter::Util::IP.alphafy(i) }.
      detect do |i| 
        range = Facter.value('network_' + i) +
                '/' +
                Facter.value('netmask_' + i)
        IPAddr.new(range).include?(gw)
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.11912943871706758, 'fraction_numerical': 0.009163802978235968, 'mean_word_length': 3.7330623306233064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3133756', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1142, 'n_words': 326}