package ping;

use warnings;
use strict;

use Net::Ping;
use Data::Dump qw/dump/;
use Time::HiRes;
use client;
use store;

sub host {
	my $ip = shift;
	my $status = fping($ip);
	defined $status->{$ip};
}

sub fping {
	my $p = Net::Ping->new('syn', 0.3);
	$p->hires;

	my $status;
	my %syn;

	foreach my $host ( @_ ) {
		my ($ret,$nslookup_duration,$ip) = $p->ping($host);

		if ( $ret ) {
			$syn{$host} = $ip;
		} else {
			push @{ $status->{address_not_found} }, $host;
		}
	}

	while (my ($host,$rtt,$ip) = $p->ack) {
		warn "# $host $rtt $ip\n";
		$rtt *= 1000; # ms
		client::conf( $ip => 'hostname', default => $host ) if $host ne $ip;
		$status->{$host} = { host => $host, rtt => $rtt, ip => $ip };
		store::audit( $ip, $status->{$host});
	}

	warn "# fping ",dump($status);

	return $status;

}

1;
