<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w
;#
;# ntp.pl,v 3.1 1993/07/06 01:09:09 jbj Exp
;#
;# process loop filter statistics file and either
;#     - show statistics periodically using gnuplot
;#     - or print a single plot
;#
;#  Copyright (c) 1992 
;#  Rainer Pruy Friedrich-Alexander Universitaet Erlangen-Nuernberg
;#
;#
;#############################################################

package ntp;

$NTP_version = 2;
$ctrl_mode=6;

$byte1 = (($NTP_version &amp; 0x7)&lt;&lt; 3) &amp; 0x34 | ($ctrl_mode &amp; 0x7);
$MAX_DATA = 468;

$sequence = 0;			# initial sequence number incred before used
$pad=4;
$do_auth=0;			# no possibility today
$keyid=0;
;#list if known keys (passwords)
%KEYS = ( 0, "\200\200\200\200\200\200\200\200",
	 );

;#-----------------------------------------------------------------------------
;# access routines for ntp control packet
    ;# NTP control message format
    ;#  C  LI|VN|MODE  LI 2bit=00  VN 3bit=2(3) MODE 3bit=6 : $byte1
    ;#  C  R|E|M|Op    R response  E error    M more   Op opcode
    ;#  n  sequence
    ;#  n  status
    ;#  n  associd
    ;#  n  offset
    ;#  n  count
    ;#  a+ data (+ padding)
    ;#  optional authentication data
    ;#  N  key
    ;#  N2 checksum
    
;# first byte of packet
sub pkt_LI   { return ($_[$[] &gt;&gt; 6) &amp; 0x3; }
sub pkt_VN   { return ($_[$[] &gt;&gt; 3) &amp; 0x7; }
sub pkt_MODE { return ($_[$[]     ) &amp; 0x7; }

;# second byte of packet
sub pkt_R  { return ($_[$[] &amp; 0x80) == 0x80; }
sub pkt_E  { return ($_[$[] &amp; 0x40) == 0x40; }
sub pkt_M  { return ($_[$[] &amp; 0x20) == 0x20; }
sub pkt_OP { return $_[$[] &amp; 0x1f; }

;#-----------------------------------------------------------------------------

sub setkey
{
    local($id,$key) = @_;

    $KEYS{$id} = $key if (defined($key));
    if (! defined($KEYS{$id}))
    {
	warn "Key $id not yet specified - key not changed\n";
	return undef;
    }
    return ($keyid,$keyid = $id)[$[];
}

;#-----------------------------------------------------------------------------
sub numerical { $a &lt;=&gt; $b; }

;#-----------------------------------------------------------------------------

sub send	#'
{
    local($fh,$opcode, $associd, $data,$address) = @_;
    $fh = caller(0)."'$fh";

    local($junksize,$junk,$packet,$offset,$ret);
    $offset = 0;

    $sequence++;
    while(1)
    {
	$junksize = length($data);
	$junksize = $MAX_DATA if $junksize &gt; $MAX_DATA;
	
	($junk,$data) = $data =~ /^(.{$junksize})(.*)$/;
	$packet
	    = pack("C2n5a".(($junk eq "") ? 0 : &amp;pad($junksize+12,$pad)-12),
		   $byte1,
		   ($opcode &amp; 0x1f) | ($data ? 0x20 : 0),
		   $sequence,
		   0, $associd,
		   $offset, $junksize, $junk);
	if ($do_auth)
	{
	    ;# not yet
	}
	$offset += $junksize;

	if (defined($address))
	{
	    $ret = send($fh, $packet, 0, $address);
	}
	else
	{
	    $ret = send($fh, $packet, 0);
	}

	if (! defined($ret))
	{
	    warn "send failed: $!\n";
	    return undef;
	}
	elsif ($ret != length($packet))
	{
	    warn "send failed: sent only $ret from ".length($packet). "bytes\n";
	    return undef;
	}
	return $sequence unless $data;
    }
}

;#-----------------------------------------------------------------------------
;# status interpretation
;#
sub getval
{
    local($val,*list) = @_;
    
    return $list{$val} if defined($list{$val});
    return sprintf("%s#%d",$list{"-"},$val) if defined($list{"-"});
    return "unknown-$val";
}

;#---------------------------------
;# system status
;#
;# format: |LI|CS|SECnt|SECode| LI=2bit CS=6bit SECnt=4bit SECode=4bit
sub ssw_LI     { return ($_[$[] &gt;&gt; 14) &amp; 0x3; }
sub ssw_CS     { return ($_[$[] &gt;&gt; 8)  &amp; 0x3f; }
sub ssw_SECnt  { return ($_[$[] &gt;&gt; 4)  &amp; 0xf; }
sub ssw_SECode { return $_[$[] &amp; 0xf; }

%LI = ( 0, "leap_none",  1, "leap_add_sec", 2, "leap_del_sec", 3, "sync_alarm", "-", "leap");
%ClockSource = (0, "sync_unspec",
		1, "sync_lf_clock",
		2, "sync_uhf_clock",
		3, "sync_hf_clock",
		4, "sync_local_proto",
		5, "sync_ntp",
		6, "sync_udp/time",
		7, "sync_wristwatch",
		"-", "ClockSource",
		);

%SystemEvent = (0, "event_unspec",
		1, "event_restart",
		2, "event_fault",
		3, "event_sync_chg",
		4, "event_sync/strat_chg",
		5, "event_clock_reset",
		6, "event_bad_date",
		7, "event_clock_excptn",
		"-", "event",
		);
sub LI
{
    &amp;getval(&amp;ssw_LI($_[$[]),*LI);
}
sub ClockSource
{
    &amp;getval(&amp;ssw_CS($_[$[]),*ClockSource);
}

sub SystemEvent
{
    &amp;getval(&amp;ssw_SECode($_[$[]),*SystemEvent);
}

sub system_status
{
    return sprintf("%s, %s, %d event%s, %s", &amp;LI($_[$[]), &amp;ClockSource($_[$[]),
		   &amp;ssw_SECnt($_[$[]), ((&amp;ssw_SECnt($_[$[])==1) ? "" : "s"),
		   &amp;SystemEvent($_[$[]));
}
;#---------------------------------
;# peer status
;#
;# format: |PStat|PSel|PCnt|PCode| Pstat=6bit PSel=2bit PCnt=4bit PCode=4bit
sub psw_PStat_config     { return ($_[$[] &amp; 0x8000) == 0x8000; }
sub psw_PStat_authenable { return ($_[$[] &amp; 0x4000) == 0x4000; }
sub psw_PStat_authentic  { return ($_[$[] &amp; 0x2000) == 0x2000; }
sub psw_PStat_reach      { return ($_[$[] &amp; 0x1000) == 0x1000; }
sub psw_PStat_sane       { return ($_[$[] &amp; 0x0800) == 0x0800; }
sub psw_PStat_dispok     { return ($_[$[] &amp; 0x0400) == 0x0400; }
sub psw_PStat { return ($_[$[] &gt;&gt; 10) &amp; 0x3f; }
sub psw_PSel  { return ($_[$[] &gt;&gt; 8)  &amp; 0x3;  }
sub psw_PCnt  { return ($_[$[] &gt;&gt; 4)  &amp; 0xf; }
sub psw_PCode { return $_[$[] &amp; 0xf; }

%PeerSelection = (0, "sel_reject",
		  1, "sel_candidate",
		  2, "sel_selcand",
		  3, "sel_sys.peer",
		  "-", "PeerSel",
		  );
%PeerEvent = (0, "event_unspec",
	      1, "event_ip_err",
	      2, "event_authen",
	      3, "event_unreach",
	      4, "event_reach",
	      5, "event_clock_excptn",
	      6, "event_stratum_chg",
	      "-", "event",
	      );

sub PeerSelection
{
    &amp;getval(&amp;psw_PSel($_[$[]),*PeerSelection);
}

sub PeerEvent
{
    &amp;getval(&amp;psw_PCode($_[$[]),*PeerEvent);
}

sub peer_status
{
    local($x) = ("");
    $x .= "config,"     if &amp;psw_PStat_config($_[$[]);
    $x .= "authenable," if &amp;psw_PStat_authenable($_[$[]);
    $x .= "authentic,"  if &amp;psw_PStat_authentic($_[$[]);
    $x .= "reach,"      if &amp;psw_PStat_reach($_[$[]);
    $x .= &amp;psw_PStat_sane($_[$[]) ? "sane," : "insane,";
    $x .= "hi_disp," unless &amp;psw_PStat_dispok($_[$[]);

    $x .= sprintf(" %s, %d event%s, %s", &amp;PeerSelection($_[$[]),
		  &amp;psw_PCnt($_[$[]), ((&amp;psw_PCnt($_[$[]) == 1) ? "" : "s"),
		  &amp;PeerEvent($_[$[]));
    return $x;
}

;#---------------------------------
;# clock status
;#
;# format: |CStat|CEvnt| CStat=8bit CEvnt=8bit
sub csw_CStat { return ($_[$[] &gt;&gt; 8) &amp; 0xff; }
sub csw_CEvnt { return $_[$[] &amp; 0xff; }

%ClockStatus = (0, "clk_nominal",
		1, "clk_timeout",
		2, "clk_badreply",
		3, "clk_fault",
		4, "clk_prop",
		5, "clk_baddate",
		6, "clk_badtime",
		"-", "clk",
	       );

sub clock_status
{
    return sprintf("%s, last %s",
		   &amp;getval(&amp;csw_CStat($_[$[]),*ClockStatus),
		   &amp;getval(&amp;csw_CEvnt($_[$[]),*ClockStatus));
}

;#---------------------------------
;# error status
;#
;# format: |Err|reserved|  Err=8bit
;#
sub esw_Err { return ($_[$[] &gt;&gt; 8) &amp; 0xff; }

%ErrorStatus = (0, "err_unspec",
		1, "err_auth_fail",
		2, "err_invalid_fmt",
		3, "err_invalid_opcode",
		4, "err_unknown_assoc",
		5, "err_unknown_var",
		6, "err_invalid_value",
		7, "err_adm_prohibit",
		);

sub error_status
{
    return sprintf("%s", &amp;getval(&amp;esw_Err($_[$[]),*ErrorStatus));
}

;#-----------------------------------------------------------------------------
;#
;# cntrl op name translation

%CntrlOpName = (1, "read_status",
		2, "read_variables",
		3, "write_variables",
		4, "read_clock_variables",
		5, "write_clock_variables",
		6, "set_trap",
		7, "trap_response",
		31, "unset_trap", # !!! unofficial !!!
		"-", "cntrlop",
		);

sub cntrlop_name
{
    return &amp;getval($_[$[],*CntrlOpName);
}

;#-----------------------------------------------------------------------------

$STAT_short_pkt = 0;
$STAT_pkt = 0;

;# process a NTP control message (response) packet
;# returns a list ($ret,$data,$status,$associd,$op,$seq,$auth_keyid)
;#      $ret: undef     --&gt; not yet complete
;#            ""        --&gt; complete packet received
;#            "ERROR"   --&gt; error during receive, bad packet, ...
;#          else        --&gt; error packet - list may contain useful info


sub handle_packet
{
    local($pkt,$from) = @_;	# parameters
    local($len_pkt) = (length($pkt));
;#    local(*FRAGS,*lastseen);
    local($li_vn_mode,$r_e_m_op,$seq,$status,$associd,$offset,$count,$data);
    local($autch_keyid,$auth_cksum);

    $STAT_pkt++;
    if ($len_pkt &lt; 12)
    {
	$STAT_short_pkt++;
	return ("ERROR","short packet received");
    }

    ;# now break packet apart
    ($li_vn_mode,$r_e_m_op,$seq,$status,$associd,$offset,$count,$data) =
	unpack("C2n5a".($len_pkt-12),$pkt);
    $data=substr($data,$[,$count);
    if ((($len_pkt - 12) - &amp;pad($count,4)) &gt;= 12)
    {
	;# looks like an authenticator
	($auth_keyid,$auth_cksum) =
	    unpack("Na8",substr($pkt,$len_pkt-12+$[,12));
	$STAT_auth++;
	;# no checking of auth_cksum (yet ?)
    }

    if (&amp;pkt_VN($li_vn_mode) != $NTP_version)
    {
	$STAT_bad_version++;
	return ("ERROR","version ".&amp;pkt_VN($li_vn_mode)."packet ignored");
    }

    if (&amp;pkt_MODE($li_vn_mode) != $ctrl_mode)
    {
	$STAT_bad_mode++;
	return ("ERROR", "mode ".&amp;pkt_MODE($li_vn_mode)." packet ignored");
    }
    
    ;# handle single fragment fast
    if ($offset == 0 &amp;&amp; &amp;pkt_M($r_e_m_op) == 0)
    {
	$STAT_single_frag++;
	if (&amp;pkt_E($r_e_m_op))
	{
	    $STAT_err_pkt++;
	    return (&amp;error_status($status),
		    $data,$status,$associd,&amp;pkt_OP($r_e_m_op),$seq,
		    $auth_keyid);
	}
	else
	{
	    return ("",
		    $data,$status,$associd,&amp;pkt_OP($r_e_m_op),$seq,
		    $auth_keyid);
	}
    }
    else
    {
	;# fragment - set up local name space
	$id = "$from$seq".&amp;pkt_OP($r_e_m_op);
	$ID{$id} = 1;
	*FRAGS = "$id FRAGS";
	*lastseen = "$id lastseen";
	
	$STAT_frag++;
	
	$lastseen = 1 if !&amp;pkt_M($r_e_m_op);
	if (!defined(%FRAGS))
	{
	    print((&amp;pkt_M($r_e_m_op) ? " more" : "")."\n");
	    $FRAGS{$offset} = $data;
	    ;# save other info
	    @FRAGS = ($status,$associd,&amp;pkt_OP($r_e_m_op),$seq,$auth_keyid,$r_e_m_op);
	}
	else
	{
	    print((&amp;pkt_M($r_e_m_op) ? " more" : "")."\n");
	    ;# add frag to previous - combine on the fly
	    if (defined($FRAGS{$offset}))
	    {
		$STAT_dup_frag++;
		return ("ERROR","duplicate fragment at $offset seq=$seq");
	    }
	    
	    $FRAGS{$offset} = $data;
	    
	    undef($loff);
	    foreach $off (sort numerical keys(%FRAGS))
	    {
		next unless defined($FRAGS{$off});
		if (defined($loff) &amp;&amp;
		    ($loff + length($FRAGS{$loff})) == $off)
		{
		    $FRAGS{$loff} .= $FRAGS{$off};
		    delete $FRAGS{$off};
		    last;
		}
		$loff = $off;
	    }

	    ;# return packet if all frags arrived
	    ;# at most two frags with possible padding ???
	    if ($lastseen &amp;&amp; defined($FRAGS{0}) &amp;&amp;
		(((scalar(@x=sort numerical keys(%FRAGS)) == 2) &amp;&amp;
		  (length($FRAGS{0}) + 8) &gt; $x[$[+1]) ||
		  (scalar(@x=sort numerical keys(%FRAGS)) &lt; 2)))
	    {
		@x=((&amp;pkt_E($r_e_m_op) ? &amp;error_status($status) : ""),
		    $FRAGS{0},@FRAGS);
		&amp;pkt_E($r_e_m_op) ? $STAT_err_frag++ : $STAT_frag_all++;
		undef(%FRAGS);
		undef(@FRAGS);
		undef($lastseen);
		delete $ID{$id};
		&amp;main'clear_timeout($id);
		return @x;
	    }
	    else
	    {
		&amp;main'set_timeout($id,time+$timeout,"&amp;ntp'handle_packet_timeout(\"".unpack("H*",$id)."\");"); #'";
	    }
	}
	return (undef);
    }
}

sub handle_packet_timeout
{
    local($id) = @_;
    local($r_e_m_op,*FRAGS,*lastseen,@x) = (@FRAGS[$[+5]);
    
    *FRAGS = "$id FRAGS";
    *lastseen = "$id lastseen";
    
    @x=((&amp;pkt_E($r_e_m_op) ? &amp;error_status($status) : "TIMEOUT"),
	$FRAGS{0},@FRAGS[$[ .. $[+4]);
    $STAT_frag_timeout++;
    undef(%FRAGS);
    undef(@FRAGS);
    undef($lastseen);
    delete $ID{$id};
    return @x;
}


sub pad
{
    return $_[$[+1] * int(($_[$[] + $_[$[+1] - 1) / $_[$[+1]);
}

1;
</pre></body></html>