ntptrace.in revision 1.1.1.1.14.2 1 1.1.1.1.14.2 snj #! @PATH_PERL@ -w
2 1.1.1.1.14.2 snj # John Hay -- John.Hay (at] icomtek.csir.co.za / jhay (at] FreeBSD.org
3 1.1.1.1.14.2 snj
4 1.1.1.1.14.2 snj package ntptrace;
5 1.1.1.1.14.2 snj use 5.006_000;
6 1.1.1.1.14.2 snj use strict;
7 1.1.1.1.14.2 snj use lib "@PERLLIBDIR@";
8 1.1.1.1.14.2 snj use NTP::Util qw(ntp_read_vars do_dns);
9 1.1.1.1.14.2 snj
10 1.1.1.1.14.2 snj exit run(@ARGV) unless caller;
11 1.1.1.1.14.2 snj
12 1.1.1.1.14.2 snj sub run {
13 1.1.1.1.14.2 snj my $opts;
14 1.1.1.1.14.2 snj if (!processOptions(\@_, $opts)) {
15 1.1.1.1.14.2 snj usage(1);
16 1.1.1.1.14.2 snj };
17 1.1.1.1.14.2 snj
18 1.1.1.1.14.2 snj my $dodns = $opts->{numeric} ? 0 : 1;
19 1.1.1.1.14.2 snj my $max_hosts = $opts->{'max-hosts'};
20 1.1.1.1.14.2 snj my $host = shift || $opts->{host};
21 1.1.1.1.14.2 snj my $nb_host = 0;
22 1.1.1.1.14.2 snj
23 1.1.1.1.14.2 snj for (;;) {
24 1.1.1.1.14.2 snj $nb_host++;
25 1.1.1.1.14.2 snj
26 1.1.1.1.14.2 snj my %info = get_info($host);
27 1.1.1.1.14.2 snj last if not %info;
28 1.1.1.1.14.2 snj
29 1.1.1.1.14.2 snj my $dhost = $host;
30 1.1.1.1.14.2 snj if ($dodns) {
31 1.1.1.1.14.2 snj my $name = do_dns($host);
32 1.1.1.1.14.2 snj $dhost = $name if defined $name;
33 1.1.1.1.14.2 snj }
34 1.1.1.1.14.2 snj
35 1.1.1.1.14.2 snj printf "%s: stratum %d, offset %f, synch distance %f",
36 1.1.1.1.14.2 snj $dhost, $info{stratum}, $info{offset}, $info{syncdistance};
37 1.1.1.1.14.2 snj printf ", refid '%s'", $info{refid} if $info{stratum} == 1;
38 1.1.1.1.14.2 snj print "\n";
39 1.1.1.1.14.2 snj
40 1.1.1.1.14.2 snj last if $info{stratum} == 0 || $info{stratum} == 1 ||
41 1.1.1.1.14.2 snj $info{stratum} == 16;
42 1.1.1.1.14.2 snj last if $info{refid} =~ /^127\.127\.\d{1,3}\.\d{1,3}$/;
43 1.1.1.1.14.2 snj last if $nb_host == $max_hosts;
44 1.1.1.1.14.2 snj
45 1.1.1.1.14.2 snj my $next_host = get_next_host($info{peer}, $host);
46 1.1.1.1.14.2 snj last if $next_host eq '';
47 1.1.1.1.14.2 snj last if $next_host =~ /^127\.127\.\d{1,3}\.\d{1,3}$/;
48 1.1.1.1.14.2 snj
49 1.1.1.1.14.2 snj $host = $next_host;
50 1.1.1.1.14.2 snj }
51 1.1.1.1.14.2 snj return 0;
52 1.1.1.1.14.2 snj }
53 1.1.1.1.14.2 snj
54 1.1.1.1.14.2 snj sub get_info {
55 1.1.1.1.14.2 snj my ($host) = @_;
56 1.1.1.1.14.2 snj my ($rootdelay, $rootdisp, $info) = (0, 0);
57 1.1.1.1.14.2 snj
58 1.1.1.1.14.2 snj $info = ntp_read_vars(0, [], $host);
59 1.1.1.1.14.2 snj return if not defined $info;
60 1.1.1.1.14.2 snj return if not exists $info->{stratum};
61 1.1.1.1.14.2 snj
62 1.1.1.1.14.2 snj $info->{offset} /= 1000;
63 1.1.1.1.14.2 snj $info->{syncdistance} = ($info->{rootdisp} + ($info->{rootdelay} / 2)) / 1000;
64 1.1.1.1.14.2 snj
65 1.1.1.1.14.2 snj return %$info;
66 1.1.1.1.14.2 snj }
67 1.1.1.1.14.2 snj
68 1.1.1.1.14.2 snj
69 1.1.1.1.14.2 snj sub get_next_host {
70 1.1.1.1.14.2 snj my ($peer, $host) = @_;
71 1.1.1.1.14.2 snj
72 1.1.1.1.14.2 snj my $info = ntp_read_vars($peer, [qw(srcadr)], $host);
73 1.1.1.1.14.2 snj return if not defined $info;
74 1.1.1.1.14.2 snj return $info->{srcadr};
75 1.1.1.1.14.2 snj }
76 1.1.1.1.14.2 snj
77 1.1.1.1.14.2 snj @ntptrace_opts@
78 1.1.1.1.14.2 snj
79 1.1.1.1.14.2 snj 1;
80 1.1.1.1.14.2 snj __END__
81