def find_ip(family = "inet")
ips=sorted_ips(family)
return [ nil, nil ] if ips.empty?
int_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] +"_interface"
gw_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] + "_gateway"
if network[int_attr]
gw_if_ips = ips.select do |v|
v[:iface] == network[int_attr]
end
if gw_if_ips.empty?
Ohai::Log.warn("[#{family}] no ip address on #{network[int_attr]}")
elsif network[gw_attr] and
network["interfaces"][network[int_attr]] and
network["interfaces"][network[int_attr]]["addresses"]
if [ "0.0.0.0", "::", /^fe80:/ ].any? { |pat| pat === network[gw_attr] }
Ohai::Log.debug("link level default #{family} route, picking ip from #{network[gw_attr]}")
r = gw_if_ips.first
else
r = gw_if_ips.select do |v|
network_contains_address(network[gw_attr], v[:ipaddress], v[:iface])
end.first
if r.nil?
r = gw_if_ips.first
Ohai::Log.debug("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}, picking #{r[:ipaddress]}")
else
Ohai::Log.debug("[#{family}] Using default interface #{network[int_attr]} and default gateway #{network[gw_attr]} to set the default ip to #{r[:ipaddress]}")
end
end
else
r = gw_if_ips.first
end
else
r = ips.first
Ohai::Log.debug("[#{family}] no default interface, picking the first ipaddress")
end
return [ nil, nil ] if r.nil? or r.empty?
[ r[:ipaddress].to_s, r[:iface] ]
end