ntptrace.in revision 275970
15059Sgtb#! @PATH_PERL@ -w
25059Sgtb# John Hay -- John.Hay@icomtek.csir.co.za / jhay@FreeBSD.org
35059Sgtb
45059Sgtbpackage ntptrace;
55059Sgtbuse 5.006_000;
65059Sgtbuse strict;
75059Sgtbuse lib "@PERLLIBDIR@";
85059Sgtbuse NTP::Util qw(ntp_read_vars do_dns);
95059Sgtb
105059Sgtbexit run(@ARGV) unless caller;
115059Sgtb
125059Sgtbsub run {
135059Sgtb    my $opts;
145059Sgtb    if (!processOptions(\@_, $opts)) {
155059Sgtb        usage(1);
165059Sgtb    };
175059Sgtb
185059Sgtb    my $dodns     = $opts->{numeric} ? 0 : 1;
195059Sgtb    my $max_hosts = $opts->{'max-hosts'};
205059Sgtb    my $host      = shift || $opts->{host};
210Sstevel@tonic-gate    my $nb_host   = 0;
225053Sgtb
230Sstevel@tonic-gate    for (;;) {
240Sstevel@tonic-gate        $nb_host++;
250Sstevel@tonic-gate
260Sstevel@tonic-gate        my %info = get_info($host);
270Sstevel@tonic-gate        last if not %info;
280Sstevel@tonic-gate
290Sstevel@tonic-gate        my $dhost = $host;
300Sstevel@tonic-gate        if ($dodns) {
310Sstevel@tonic-gate            my $name = do_dns($host);
320Sstevel@tonic-gate            $dhost = $name if defined $name;
330Sstevel@tonic-gate        }
340Sstevel@tonic-gate
350Sstevel@tonic-gate        printf "%s: stratum %d, offset %f, synch distance %f",
360Sstevel@tonic-gate            $dhost, $info{stratum}, $info{offset}, $info{syncdistance};
370Sstevel@tonic-gate        printf ", refid '%s'", $info{refid} if $info{stratum} == 1;
380Sstevel@tonic-gate        print "\n";
390Sstevel@tonic-gate
400Sstevel@tonic-gate        last if $info{stratum} == 0 || $info{stratum} == 1 || 
410Sstevel@tonic-gate                $info{stratum} == 16;
420Sstevel@tonic-gate        last if $info{refid} =~ /^127\.127\.\d{1,3}\.\d{1,3}$/;
430Sstevel@tonic-gate        last if $nb_host == $max_hosts;
440Sstevel@tonic-gate
450Sstevel@tonic-gate        my $next_host = get_next_host($info{peer}, $host);
460Sstevel@tonic-gate        last if $next_host eq '';
470Sstevel@tonic-gate        last if $next_host  =~ /^127\.127\.\d{1,3}\.\d{1,3}$/;
480Sstevel@tonic-gate
490Sstevel@tonic-gate        $host = $next_host;
500Sstevel@tonic-gate    }
510Sstevel@tonic-gate    return 0;
520Sstevel@tonic-gate}
530Sstevel@tonic-gate
540Sstevel@tonic-gatesub get_info {
550Sstevel@tonic-gate    my ($host) = @_;
565053Sgtb    my ($rootdelay, $rootdisp, $info) = (0, 0);
570Sstevel@tonic-gate
580Sstevel@tonic-gate    $info = ntp_read_vars(0, [], $host);
590Sstevel@tonic-gate    return if not defined $info;
600Sstevel@tonic-gate    return if not exists $info->{stratum};
615053Sgtb
620Sstevel@tonic-gate    $info->{offset} /= 1000;
630Sstevel@tonic-gate    $info->{syncdistance} = ($info->{rootdisp} + ($info->{rootdelay} / 2)) / 1000;
640Sstevel@tonic-gate
650Sstevel@tonic-gate    return %$info;
660Sstevel@tonic-gate}
670Sstevel@tonic-gate
680Sstevel@tonic-gate
690Sstevel@tonic-gatesub get_next_host {
700Sstevel@tonic-gate    my ($peer, $host) = @_;
710Sstevel@tonic-gate
725053Sgtb    my $info = ntp_read_vars($peer, [qw(srcadr)], $host);
735053Sgtb    return if not defined $info;
745053Sgtb    return $info->{srcadr};
755059Sgtb}
765053Sgtb
775053Sgtb@ntptrace_opts@
780Sstevel@tonic-gate
790Sstevel@tonic-gate1;
805053Sgtb__END__
815053Sgtb