module Msf
module Util
module Host
def self.normalize_host(host)
return host if defined?(::Mdm) && host.kind_of?(::Mdm::Host)
norm_host = nil
if host.kind_of?(String)
if Rex::Socket.is_ipv4?(host)
norm_host = host
elsif Rex::Socket.is_ipv6?(host)
address, _ = host.split('%', 2)
norm_host = address
else
begin
norm_host = Rex::Socket.getaddress(host, true)
rescue SocketError
end
end
elsif defined?(::Mdm) && host.kind_of?(::Mdm::Session)
norm_host = host.host
elsif host.respond_to?(:session_host)
norm_host = host.session_host
end
if (
norm_host.nil? &&
host.respond_to?(:sock) &&
host.sock.respond_to?(:peerhost) &&
host.sock.peerhost.to_s.length > 0
)
norm_host = session.sock.peerhost
end
if !norm_host
dlog("Host could not be normalized: #{host.inspect}")
end
norm_host
end
end
end
end