1 #!/local/bin/perl --*-perl-*-
3 ;# ntptrap,v 3.1 1993/07/06 01:09:15 jbj Exp
5 ;# a client for the xntp mode 6 trap mechanism
8 ;# Rainer Pruy Friedrich-Alexander Universitaet Erlangen-Nuernberg
11 ;#############################################################
12 $0 =~ s!^.*/([^/]+)$!$1!; # strip to filename
13 ;# enforce STDOUT and STDERR to be line buffered
15 select((select(STDERR
),$|=1)[$[]);
17 ;#######################################
18 ;# load utility routines and definitions
20 require('ntp.pl'); # implementation of the NTP protocol
23 #eval { require('sys/socket.ph'); require('netinet/in.ph') unless defined(&INADDR_ANY); } ||
25 #die("$0: $@") unless $[ == index($@, "Can't locate ");
27 #warn "$0: supplying some default definitions\n";
28 #eval 'sub INADDR_ANY { 0; } sub AF_INET {2;} sub SOCK_DGRAM {2;} 1;' || die "$0: $@";
30 require('getopts.pl'); # option parsing
31 require('ctime.pl'); # date/time formatting
33 ;######################################
34 ;# define some global constants
39 $REFRESH_TIME=60*15; # 15 minutes (server uses 1 hour)
40 $ntp'timeout = $FRAG_TIMEOUT; #';
43 ;######################################
44 ;# now process options
48 die("usage: $0 [-n] [-p <port>] [-l <logfile>] [host] ...\n");
51 $opt_l = "/dev/null"; # where to write debug messages to
52 $opt_p = 0; # port to use locally - (0 does mean: will be choosen by kernel)
54 &usage unless &Getopts('l
:p
:');
55 &Getopts if 0; # make -w happy
57 @Hosts = ($#ARGV < $[) ? ("localhost") : @ARGV;
59 ;# setup for debug output
61 $DEBUGFILE="&STDERR" if $DEBUGFILE eq '-';
63 open(DEBUG,">>$DEBUGFILE") || die("Cannot open \"$DEBUGFILE\": $!\n");
64 select((select(DEBUG),$|=1)[$[]);
66 ;# &log prints a single trap record (adding a (local) time stamp)
69 chop($date=&ctime(time));
70 print "$date ",@_,"\n";
78 $proto_udp = (getprotobyname('udp
'))[$[+2] ||
79 (warn("$0: Could not get protocoll number for 'udp
' using 17"), 17);
81 $ntp_port = (getservbyname('ntp
','udp
'))[$[+2] ||
82 (warn("$0: Could not get port number for service ntp/udp using 123"), 123);
85 socket(S, &AF_INET, &SOCK_DGRAM, $proto_udp) || die("Cannot open socket: $!\n");
88 bind(S, pack("S n a4 x8", &AF_INET, $opt_p, &INADDR_ANY)) ||
89 die("Cannot bind: $!\n");
91 ($my_port, $my_addr) = (unpack("S n a4 x8",getsockname(S)))[$[+1,$[+2];
92 &log(sprintf("Listening at address %d.%d.%d.%d port %d",
93 unpack("C4",$my_addr), $my_port));
95 ;# disregister with all servers in case of termination
98 &log("Aborted by signal \"$_[$[]\"") if defined($_[$[]);
102 if ( ! defined($Host{$_}) )
104 print "no info for host '$_'\n";
107 &ntp'send(S
,31,0,"",pack("Sna4x8",&AF_INET
,$ntp_port,$Host{$_})); #';
113 $SIG{'HUP'} = 'cleanup';
114 $SIG{'INT'} = 'cleanup';
115 $SIG{'QUIT'} = 'cleanup';
116 $SIG{'TERM'} = 'cleanup';
119 sub timeouts
# sort timeout id array
121 $TIMEOUTS{$a} <=> $TIMEOUTS{$b};
124 ;# a Request element looks like: pack("a4SC",addr,associd,op)
127 ;# compute requests for set trap control msgs to each host given
133 if (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/)
136 (gethostbyaddr(pack("C4",$1,$2,$3,$4),&AF_INET
))[$[,$[+4];
137 unless (defined($name))
139 $name = sprintf("[[%d.%d.%d.%d]]",$1,$2,$3,$4);
140 $addr = pack("C4",$1,$2,$3,$4);
145 ($name,$addr) = (gethostbyname($_))[$[,$[+4];
146 unless (defined($name))
148 warn "$0: unknown host \"$_\" - ignored\n";
152 next if defined($Host{$name});
153 $Host{$name} = $addr;
155 push(@Requests,pack("a4SC",$addr,0,6)); # schedule a set trap request for $name
162 return $HostName{$addr} if defined($HostName{$addr});
163 local($name) = gethostbyaddr($addr,&AF_INET
);
164 &debug
(sprintf("hostname(%d.%d.%d.%d) = \"%s\"",unpack("C4",$addr),$name))
166 defined($name) && ($HostName{$addr} = $name) && (return $name);
167 &debug
(sprintf("Failed to get name for %d.%d.%d.%d",unpack("C4",$addr)));
168 return sprintf("[%d.%d.%d.%d]",unpack("C4",$addr));
171 ;# when no hosts were given on the commandline no requests have been scheduled
172 &usage
unless (@Requests);
174 &debug
(sprintf("%d request(s) scheduled",scalar(@Requests)));
175 grep(&debug
(" - ".$_),keys(%Host));
177 ;# allocate variables;
192 if (@Requests || @TIMEOUTS) # if there is some work pending
196 ($addr,$assoc,$op) = unpack("a4SC",($req = shift(@Requests)));
197 &debug
(sprintf("Request: %s: %s(%d)",&hostname
($addr), &ntp
'cntrlop_name($op), $assoc)); #';))
198 $ret = &ntp
'send(S,$op,$assoc,"", #'(
199 pack("Sna4x8",&AF_INET
,$ntp_port,$addr));
200 &set_timeout
("retry-".unpack("H*",$req),time+$BASE_TIMEOUT,
201 sprintf("&retry(\"%s\");",unpack("H*",$req)));
203 last unless (defined($ret)); # warn called by ntp'send();
205 ;# if there are more requests just have a quick look for new messages
206 ;# otherwise grant server time for a response
207 $timeout = @Requests ?
0 : $BASE_TIMEOUT;
209 if ($timeout && @TIMEOUTS)
211 ;# ensure not to miss a timeout
212 if ($timeout + time > $TIMEOUTS{$TIMEOUTS[$[]})
214 $timeout = $TIMEOUTS{$TIMEOUTS[$[]} - time;
215 $timeout = 0 if $timeout < 0;
221 ;# no work yet - wait for some messages dropping in
222 ;# usually this will not hapen as the refresh semantic will
223 ;# always have a pending timeout
227 vec($mask="",fileno(S
),1) = 1;
228 $ret = select($mask,undef,undef,$timeout);
230 warn("$0: select: $!\n"),last if $ret < 0; # give up on error return from select
235 if (@TIMEOUTS && time > $TIMEOUTS{$TIMEOUTS[$[]})
239 (delete $TIMEOUT_PROCS{$TIMEOUTS[$[]},
240 delete $TIMEOUTS{shift(@TIMEOUTS)})[$[];
242 die "timeout eval (\"$timeout_proc\"): $@\n" if $@
;
244 ;# else: there may be something to be sent
249 $from = recv(S
,$buf,$len,0);
250 ;# give up on error return from recv
251 warn("$0: recv: $!\n"), last unless (defined($from));
253 $from = (unpack("Sna4",$from))[$[+2]; # keep host addr only
254 ;# could check for ntp_port - but who cares
255 &debug
("-Packet from ",&hostname
($from));
257 ;# stuff packet into ntp mode 6 receive machinery
258 ($ret,$data,$status,$associd,$op,$seq,$auth_keyid) =
259 &ntp
'handle_packet($buf,$from); # ';
260 &debug
(sprintf("%s uses auth_keyid %d",&hostname
($from),$auth_keyid)) if defined($auth_keyid);
261 next unless defined($ret);
266 ;# simple trap response messages have neither timeout nor retries
267 &clear_timeout
("retry-".unpack("H*",pack("a4SC",$from,$associd,$op))) unless $op == 7;
268 delete $RETRY{pack("a4SC",$from,$associd,$op)} unless $op == 7;
270 &process_response
($from,$ret,$data,$status,$associd,$op,$seq,$auth_keyid);
274 ;# some kind of error
275 &log(sprintf("%50s: %s: %s",(gethostbyaddr($from,&AF_INET
))[$[],$ret,$data));
276 if ($ret ne "TIMEOUT" && $ret ne "ERROR")
278 &clear_timeout
("retry-".unpack("H*",pack("a4SC",$from,$associd,$op)));
285 warn("$0: terminating\n");
289 ;##################################################
294 local($id,$time,$proc) = @_;
296 $TIMEOUTS{$id} = $time;
297 $TIMEOUT_PROCS{$id} = $proc;
298 @TIMEOUTS = sort timeouts
keys(%TIMEOUTS);
299 chop($date=&ctime
($time));
300 &debug
(sprintf("Schedule timeout \"%s\" for %s", $id, $date));
306 delete $TIMEOUTS{$id};
307 delete $TIMEOUT_PROCS{$id};
308 @TIMEOUTS = sort timeouts
keys(%TIMEOUTS);
309 &debug
("Clear timeout \"$id\"");
315 local($addr) = @_[$[];
316 $addr = pack("H*",$addr);
317 &debug
(sprintf("Refreshing trap for %s", &hostname
($addr)));
318 push(@Requests,pack("a4SC",$addr,0,6));
325 $tag = pack("H*",$tag);
326 $RETRY{$tag} = 0 if (!defined($RETRY{$tag}));
328 if (++$RETRY{$tag} > $MAX_TRY)
330 &debug
(sprintf("Retry failed: %s assoc %5d op %d",
331 &hostname
(substr($tag,$[,4)),
332 unpack("x4SC",$tag)));
335 &debug
(sprintf("Retrying: %s assoc %5d op %d",
336 &hostname
(substr($tag,$[,4)),
337 unpack("x4SC",$tag)));
338 push(@Requests,$tag);
343 local($from,$ret,$data,$status,$associd,$op,$seq,$auth_keyid) = @_;
346 if ($op == 7) # trap response
348 $msg .= sprintf("%40s trap#%-5d",
349 &hostname
($from),$seq);
350 &debug
(sprintf("\nTrap %d associd %d:\n%s\n===============\n",$seq,$associd,$data));
351 if ($associd == 0) # system event
354 $evnt = &ntp
'SystemEvent($status); #';
356 ;# for special cases add additional info
357 ($stratum) = ($data =~ /stratum=(\d+)/);
358 ($refid) = ($data =~ /refid=([\w\.]+)/);
359 $msg .= "stratum=$stratum refid=$refid";
360 if ($refid =~ /\[?(\d+)\.(\d+)\.(\d+)\.(\d+)/)
362 local($x) = (gethostbyaddr(pack("C4",$1,$2,$3,$4),&AF_INET
));
363 $msg .= " " . $x if defined($x)
365 if ($evnt eq "event_sync_chg")
367 $msg .= sprintf("%s %s ",
368 &ntp
'LI($status), #',
369 &ntp
'ClockSource($status) #'
372 elsif ($evnt eq "event_sync/strat_chg")
374 ($peer) = ($data =~ /peer=([0-9]+)/);
375 $msg .= " peer=$peer";
377 elsif ($evnt eq "event_clock_excptn")
379 if (($device) = ($data =~ /device=\"([^\"]+)\"/))
381 ($cstatus) = ($data =~ /refclockstatus=0?x?([\da-fA-F]+)/);
382 $Cstatus = hex($cstatus);
383 $msg .= sprintf("- %-32s",&ntp
'clock_status($Cstatus)); #');
384 ($timecode) = ($data =~ /timecode=\"([^\"]+)\"/);
385 $msg .= " \"$device\" \"$timecode\"";
389 push(@Requests,pack("a4SC",$from, $associd, 4));
395 $msg .= sprintf("peer %5d ",$associd);
396 ($srcadr) = ($data =~ /srcadr=\[?([\d\.]+)/);
397 $msg .= sprintf("%-18s %40s ", "[$srcadr]",
398 &hostname
(pack("C4",split(/\./,$srcadr))));
399 $evnt = &ntp
'PeerEvent($status); #';
401 ;# for special cases include additional info
402 if ($evnt eq "event_clock_excptn")
404 if (($device) = ($data =~ /device=\"([^\"]+)\"/))
406 ;#&debug("----\n$data\n====\n");
407 ($cstatus) = ($data =~ /refclockstatus=0?x?([\da-fA-F]+)/);
408 $Cstatus = hex($cstatus);
409 $msg .= sprintf("- %-32s",&ntp
'clock_status($Cstatus)); #');
410 ($timecode) = ($data =~ /timecode=\"([^\"]+)\"/);
411 $msg .= " \"$device\" \"$timecode\"";
415 ;# no clockvars included - post a cv request
416 push(@Requests,pack("a4SC",$from, $associd, 4));
419 elsif ($evnt eq "event_stratum_chg")
421 ($stratum) = ($data =~ /stratum=(\d+)/);
422 $msg .= "new stratum $stratum";
426 elsif ($op == 6) # set trap resonse
428 &debug
("Set trap ok from ",&hostname
($from));
429 &set_timeout
("refresh-".unpack("H*",$from),time+$REFRESH_TIME,
430 sprintf("&refresh(\"%s\");",unpack("H*",$from)));
433 elsif ($op == 4) # read clock variables response
436 $msg .= sprintf(" %40s ", &hostname
($from));
439 $msg .= "system clock status: ";
443 $msg .= sprintf("peer %5d clock",$associd);
445 $msg .= sprintf("%-32s",&ntp
'clock_status($status)); #');
446 ($device) = ($data =~ /device=\"([^\"]+)\"/);
447 ($timecode) = ($data =~ /timecode=\"([^\"]+)\"/);
448 $msg .= " \"$device\" \"$timecode\"";
450 elsif ($op == 31) # unset trap response (UNOFFICIAL op)
453 &debug
("Clear Trap ok from ",&hostname
($from));
454 &clear_timeout
("refresh-".unpack("H*",$from));
457 else # unexpected response
459 $msg .= "unexpected response to op $op assoc=$associd";
460 $msg .= sprintf(" status=%04x",$status);