xref: /freebsd/contrib/ntp/scripts/monitoring/ntp.pl (revision 224ba2bd37e182b64f7d78defef8a6cacaad3415)
1224ba2bdSOllivier Robert#!/usr/bin/perl -w
2c0b746e5SOllivier Robert;#
3c0b746e5SOllivier Robert;# ntp.pl,v 3.1 1993/07/06 01:09:09 jbj Exp
4c0b746e5SOllivier Robert;#
5c0b746e5SOllivier Robert;# process loop filter statistics file and either
6c0b746e5SOllivier Robert;#     - show statistics periodically using gnuplot
7c0b746e5SOllivier Robert;#     - or print a single plot
8c0b746e5SOllivier Robert;#
9c0b746e5SOllivier Robert;#  Copyright (c) 1992
10c0b746e5SOllivier Robert;#  Rainer Pruy Friedrich-Alexander Universitaet Erlangen-Nuernberg
11c0b746e5SOllivier Robert;#
12c0b746e5SOllivier Robert;#
13c0b746e5SOllivier Robert;#############################################################
14c0b746e5SOllivier Robert
15c0b746e5SOllivier Robertpackage ntp;
16c0b746e5SOllivier Robert
17c0b746e5SOllivier Robert$NTP_version = 2;
18c0b746e5SOllivier Robert$ctrl_mode=6;
19c0b746e5SOllivier Robert
20c0b746e5SOllivier Robert$byte1 = (($NTP_version & 0x7)<< 3) & 0x34 | ($ctrl_mode & 0x7);
21c0b746e5SOllivier Robert$MAX_DATA = 468;
22c0b746e5SOllivier Robert
23c0b746e5SOllivier Robert$sequence = 0;			# initial sequence number incred before used
24c0b746e5SOllivier Robert$pad=4;
25c0b746e5SOllivier Robert$do_auth=0;			# no possibility today
26c0b746e5SOllivier Robert$keyid=0;
27c0b746e5SOllivier Robert;#list if known keys (passwords)
28c0b746e5SOllivier Robert%KEYS = ( 0, "\200\200\200\200\200\200\200\200",
29c0b746e5SOllivier Robert	 );
30c0b746e5SOllivier Robert
31c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
32c0b746e5SOllivier Robert;# access routines for ntp control packet
33c0b746e5SOllivier Robert    ;# NTP control message format
34c0b746e5SOllivier Robert    ;#  C  LI|VN|MODE  LI 2bit=00  VN 3bit=2(3) MODE 3bit=6 : $byte1
35c0b746e5SOllivier Robert    ;#  C  R|E|M|Op    R response  E error    M more   Op opcode
36c0b746e5SOllivier Robert    ;#  n  sequence
37c0b746e5SOllivier Robert    ;#  n  status
38c0b746e5SOllivier Robert    ;#  n  associd
39c0b746e5SOllivier Robert    ;#  n  offset
40c0b746e5SOllivier Robert    ;#  n  count
41c0b746e5SOllivier Robert    ;#  a+ data (+ padding)
42c0b746e5SOllivier Robert    ;#  optional authentication data
43c0b746e5SOllivier Robert    ;#  N  key
44c0b746e5SOllivier Robert    ;#  N2 checksum
45c0b746e5SOllivier Robert
46224ba2bdSOllivier Robert;# first byte of packet
47c0b746e5SOllivier Robertsub pkt_LI   { return ($_[$[] >> 6) & 0x3; }
48c0b746e5SOllivier Robertsub pkt_VN   { return ($_[$[] >> 3) & 0x7; }
49c0b746e5SOllivier Robertsub pkt_MODE { return ($_[$[]     ) & 0x7; }
50c0b746e5SOllivier Robert
51c0b746e5SOllivier Robert;# second byte of packet
52c0b746e5SOllivier Robertsub pkt_R  { return ($_[$[] & 0x80) == 0x80; }
53c0b746e5SOllivier Robertsub pkt_E  { return ($_[$[] & 0x40) == 0x40; }
54c0b746e5SOllivier Robertsub pkt_M  { return ($_[$[] & 0x20) == 0x20; }
55c0b746e5SOllivier Robertsub pkt_OP { return $_[$[] & 0x1f; }
56c0b746e5SOllivier Robert
57c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
58c0b746e5SOllivier Robert
59c0b746e5SOllivier Robertsub setkey
60c0b746e5SOllivier Robert{
61c0b746e5SOllivier Robert    local($id,$key) = @_;
62c0b746e5SOllivier Robert
63c0b746e5SOllivier Robert    $KEYS{$id} = $key if (defined($key));
64c0b746e5SOllivier Robert    if (! defined($KEYS{$id}))
65c0b746e5SOllivier Robert    {
66c0b746e5SOllivier Robert	warn "Key $id not yet specified - key not changed\n";
67c0b746e5SOllivier Robert	return undef;
68c0b746e5SOllivier Robert    }
69c0b746e5SOllivier Robert    return ($keyid,$keyid = $id)[$[];
70c0b746e5SOllivier Robert}
71c0b746e5SOllivier Robert
72c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
73c0b746e5SOllivier Robertsub numerical { $a <=> $b; }
74c0b746e5SOllivier Robert
75c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
76c0b746e5SOllivier Robert
77c0b746e5SOllivier Robertsub send	#'
78c0b746e5SOllivier Robert{
79c0b746e5SOllivier Robert    local($fh,$opcode, $associd, $data,$address) = @_;
80c0b746e5SOllivier Robert    $fh = caller(0)."'$fh";
81c0b746e5SOllivier Robert
82c0b746e5SOllivier Robert    local($junksize,$junk,$packet,$offset,$ret);
83c0b746e5SOllivier Robert    $offset = 0;
84c0b746e5SOllivier Robert
85c0b746e5SOllivier Robert    $sequence++;
86c0b746e5SOllivier Robert    while(1)
87c0b746e5SOllivier Robert    {
88c0b746e5SOllivier Robert	$junksize = length($data);
89c0b746e5SOllivier Robert	$junksize = $MAX_DATA if $junksize > $MAX_DATA;
90c0b746e5SOllivier Robert
91c0b746e5SOllivier Robert	($junk,$data) = $data =~ /^(.{$junksize})(.*)$/;
92c0b746e5SOllivier Robert	$packet
93c0b746e5SOllivier Robert	    = pack("C2n5a".(($junk eq "") ? 0 : &pad($junksize+12,$pad)-12),
94c0b746e5SOllivier Robert		   $byte1,
95c0b746e5SOllivier Robert		   ($opcode & 0x1f) | ($data ? 0x20 : 0),
96c0b746e5SOllivier Robert		   $sequence,
97c0b746e5SOllivier Robert		   0, $associd,
98c0b746e5SOllivier Robert		   $offset, $junksize, $junk);
99c0b746e5SOllivier Robert	if ($do_auth)
100c0b746e5SOllivier Robert	{
101c0b746e5SOllivier Robert	    ;# not yet
102c0b746e5SOllivier Robert	}
103c0b746e5SOllivier Robert	$offset += $junksize;
104c0b746e5SOllivier Robert
105c0b746e5SOllivier Robert	if (defined($address))
106c0b746e5SOllivier Robert	{
107c0b746e5SOllivier Robert	    $ret = send($fh, $packet, 0, $address);
108c0b746e5SOllivier Robert	}
109c0b746e5SOllivier Robert	else
110c0b746e5SOllivier Robert	{
111c0b746e5SOllivier Robert	    $ret = send($fh, $packet, 0);
112c0b746e5SOllivier Robert	}
113c0b746e5SOllivier Robert
114c0b746e5SOllivier Robert	if (! defined($ret))
115c0b746e5SOllivier Robert	{
116c0b746e5SOllivier Robert	    warn "send failed: $!\n";
117c0b746e5SOllivier Robert	    return undef;
118c0b746e5SOllivier Robert	}
119c0b746e5SOllivier Robert	elsif ($ret != length($packet))
120c0b746e5SOllivier Robert	{
121c0b746e5SOllivier Robert	    warn "send failed: sent only $ret from ".length($packet). "bytes\n";
122c0b746e5SOllivier Robert	    return undef;
123c0b746e5SOllivier Robert	}
124c0b746e5SOllivier Robert	return $sequence unless $data;
125c0b746e5SOllivier Robert    }
126c0b746e5SOllivier Robert}
127c0b746e5SOllivier Robert
128c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
129c0b746e5SOllivier Robert;# status interpretation
130c0b746e5SOllivier Robert;#
131c0b746e5SOllivier Robertsub getval
132c0b746e5SOllivier Robert{
133c0b746e5SOllivier Robert    local($val,*list) = @_;
134c0b746e5SOllivier Robert
135c0b746e5SOllivier Robert    return $list{$val} if defined($list{$val});
136c0b746e5SOllivier Robert    return sprintf("%s#%d",$list{"-"},$val) if defined($list{"-"});
137c0b746e5SOllivier Robert    return "unknown-$val";
138c0b746e5SOllivier Robert}
139c0b746e5SOllivier Robert
140c0b746e5SOllivier Robert;#---------------------------------
141c0b746e5SOllivier Robert;# system status
142c0b746e5SOllivier Robert;#
143c0b746e5SOllivier Robert;# format: |LI|CS|SECnt|SECode| LI=2bit CS=6bit SECnt=4bit SECode=4bit
144c0b746e5SOllivier Robertsub ssw_LI     { return ($_[$[] >> 14) & 0x3; }
145c0b746e5SOllivier Robertsub ssw_CS     { return ($_[$[] >> 8)  & 0x3f; }
146c0b746e5SOllivier Robertsub ssw_SECnt  { return ($_[$[] >> 4)  & 0xf; }
147c0b746e5SOllivier Robertsub ssw_SECode { return $_[$[] & 0xf; }
148c0b746e5SOllivier Robert
149c0b746e5SOllivier Robert%LI = ( 0, "leap_none",  1, "leap_add_sec", 2, "leap_del_sec", 3, "sync_alarm", "-", "leap");
150c0b746e5SOllivier Robert%ClockSource = (0, "sync_unspec",
151c0b746e5SOllivier Robert		1, "sync_lf_clock",
152c0b746e5SOllivier Robert		2, "sync_uhf_clock",
153c0b746e5SOllivier Robert		3, "sync_hf_clock",
154c0b746e5SOllivier Robert		4, "sync_local_proto",
155c0b746e5SOllivier Robert		5, "sync_ntp",
156c0b746e5SOllivier Robert		6, "sync_udp/time",
157c0b746e5SOllivier Robert		7, "sync_wristwatch",
158c0b746e5SOllivier Robert		"-", "ClockSource",
159c0b746e5SOllivier Robert		);
160c0b746e5SOllivier Robert
161c0b746e5SOllivier Robert%SystemEvent = (0, "event_unspec",
162c0b746e5SOllivier Robert		1, "event_restart",
163c0b746e5SOllivier Robert		2, "event_fault",
164c0b746e5SOllivier Robert		3, "event_sync_chg",
165c0b746e5SOllivier Robert		4, "event_sync/strat_chg",
166c0b746e5SOllivier Robert		5, "event_clock_reset",
167c0b746e5SOllivier Robert		6, "event_bad_date",
168c0b746e5SOllivier Robert		7, "event_clock_excptn",
169c0b746e5SOllivier Robert		"-", "event",
170c0b746e5SOllivier Robert		);
171c0b746e5SOllivier Robertsub LI
172c0b746e5SOllivier Robert{
173c0b746e5SOllivier Robert    &getval(&ssw_LI($_[$[]),*LI);
174c0b746e5SOllivier Robert}
175c0b746e5SOllivier Robertsub ClockSource
176c0b746e5SOllivier Robert{
177c0b746e5SOllivier Robert    &getval(&ssw_CS($_[$[]),*ClockSource);
178c0b746e5SOllivier Robert}
179c0b746e5SOllivier Robert
180c0b746e5SOllivier Robertsub SystemEvent
181c0b746e5SOllivier Robert{
182c0b746e5SOllivier Robert    &getval(&ssw_SECode($_[$[]),*SystemEvent);
183c0b746e5SOllivier Robert}
184c0b746e5SOllivier Robert
185c0b746e5SOllivier Robertsub system_status
186c0b746e5SOllivier Robert{
187c0b746e5SOllivier Robert    return sprintf("%s, %s, %d event%s, %s", &LI($_[$[]), &ClockSource($_[$[]),
188c0b746e5SOllivier Robert		   &ssw_SECnt($_[$[]), ((&ssw_SECnt($_[$[])==1) ? "" : "s"),
189c0b746e5SOllivier Robert		   &SystemEvent($_[$[]));
190c0b746e5SOllivier Robert}
191c0b746e5SOllivier Robert;#---------------------------------
192c0b746e5SOllivier Robert;# peer status
193c0b746e5SOllivier Robert;#
194c0b746e5SOllivier Robert;# format: |PStat|PSel|PCnt|PCode| Pstat=6bit PSel=2bit PCnt=4bit PCode=4bit
195c0b746e5SOllivier Robertsub psw_PStat_config     { return ($_[$[] & 0x8000) == 0x8000; }
196c0b746e5SOllivier Robertsub psw_PStat_authenable { return ($_[$[] & 0x4000) == 0x4000; }
197c0b746e5SOllivier Robertsub psw_PStat_authentic  { return ($_[$[] & 0x2000) == 0x2000; }
198c0b746e5SOllivier Robertsub psw_PStat_reach      { return ($_[$[] & 0x1000) == 0x1000; }
199c0b746e5SOllivier Robertsub psw_PStat_sane       { return ($_[$[] & 0x0800) == 0x0800; }
200c0b746e5SOllivier Robertsub psw_PStat_dispok     { return ($_[$[] & 0x0400) == 0x0400; }
201c0b746e5SOllivier Robertsub psw_PStat { return ($_[$[] >> 10) & 0x3f; }
202c0b746e5SOllivier Robertsub psw_PSel  { return ($_[$[] >> 8)  & 0x3;  }
203c0b746e5SOllivier Robertsub psw_PCnt  { return ($_[$[] >> 4)  & 0xf; }
204c0b746e5SOllivier Robertsub psw_PCode { return $_[$[] & 0xf; }
205c0b746e5SOllivier Robert
206c0b746e5SOllivier Robert%PeerSelection = (0, "sel_reject",
207c0b746e5SOllivier Robert		  1, "sel_candidate",
208c0b746e5SOllivier Robert		  2, "sel_selcand",
209c0b746e5SOllivier Robert		  3, "sel_sys.peer",
210c0b746e5SOllivier Robert		  "-", "PeerSel",
211c0b746e5SOllivier Robert		  );
212c0b746e5SOllivier Robert%PeerEvent = (0, "event_unspec",
213c0b746e5SOllivier Robert	      1, "event_ip_err",
214c0b746e5SOllivier Robert	      2, "event_authen",
215c0b746e5SOllivier Robert	      3, "event_unreach",
216c0b746e5SOllivier Robert	      4, "event_reach",
217c0b746e5SOllivier Robert	      5, "event_clock_excptn",
218c0b746e5SOllivier Robert	      6, "event_stratum_chg",
219c0b746e5SOllivier Robert	      "-", "event",
220c0b746e5SOllivier Robert	      );
221c0b746e5SOllivier Robert
222c0b746e5SOllivier Robertsub PeerSelection
223c0b746e5SOllivier Robert{
224c0b746e5SOllivier Robert    &getval(&psw_PSel($_[$[]),*PeerSelection);
225c0b746e5SOllivier Robert}
226224ba2bdSOllivier Robert
227c0b746e5SOllivier Robertsub PeerEvent
228c0b746e5SOllivier Robert{
229c0b746e5SOllivier Robert    &getval(&psw_PCode($_[$[]),*PeerEvent);
230c0b746e5SOllivier Robert}
231c0b746e5SOllivier Robert
232c0b746e5SOllivier Robertsub peer_status
233c0b746e5SOllivier Robert{
234c0b746e5SOllivier Robert    local($x) = ("");
235c0b746e5SOllivier Robert    $x .= "config,"     if &psw_PStat_config($_[$[]);
236c0b746e5SOllivier Robert    $x .= "authenable," if &psw_PStat_authenable($_[$[]);
237c0b746e5SOllivier Robert    $x .= "authentic,"  if &psw_PStat_authentic($_[$[]);
238c0b746e5SOllivier Robert    $x .= "reach,"      if &psw_PStat_reach($_[$[]);
239c0b746e5SOllivier Robert    $x .= &psw_PStat_sane($_[$[]) ? "sane," : "insane,";
240c0b746e5SOllivier Robert    $x .= "hi_disp," unless &psw_PStat_dispok($_[$[]);
241c0b746e5SOllivier Robert
242c0b746e5SOllivier Robert    $x .= sprintf(" %s, %d event%s, %s", &PeerSelection($_[$[]),
243c0b746e5SOllivier Robert		  &psw_PCnt($_[$[]), ((&psw_PCnt($_[$[]) == 1) ? "" : "s"),
244c0b746e5SOllivier Robert		  &PeerEvent($_[$[]));
245c0b746e5SOllivier Robert    return $x;
246c0b746e5SOllivier Robert}
247c0b746e5SOllivier Robert
248c0b746e5SOllivier Robert;#---------------------------------
249c0b746e5SOllivier Robert;# clock status
250c0b746e5SOllivier Robert;#
251c0b746e5SOllivier Robert;# format: |CStat|CEvnt| CStat=8bit CEvnt=8bit
252c0b746e5SOllivier Robertsub csw_CStat { return ($_[$[] >> 8) & 0xff; }
253c0b746e5SOllivier Robertsub csw_CEvnt { return $_[$[] & 0xff; }
254c0b746e5SOllivier Robert
255c0b746e5SOllivier Robert%ClockStatus = (0, "clk_nominal",
256c0b746e5SOllivier Robert		1, "clk_timeout",
257c0b746e5SOllivier Robert		2, "clk_badreply",
258c0b746e5SOllivier Robert		3, "clk_fault",
259c0b746e5SOllivier Robert		4, "clk_prop",
260c0b746e5SOllivier Robert		5, "clk_baddate",
261c0b746e5SOllivier Robert		6, "clk_badtime",
262c0b746e5SOllivier Robert		"-", "clk",
263c0b746e5SOllivier Robert	       );
264c0b746e5SOllivier Robert
265c0b746e5SOllivier Robertsub clock_status
266c0b746e5SOllivier Robert{
267c0b746e5SOllivier Robert    return sprintf("%s, last %s",
268c0b746e5SOllivier Robert		   &getval(&csw_CStat($_[$[]),*ClockStatus),
269c0b746e5SOllivier Robert		   &getval(&csw_CEvnt($_[$[]),*ClockStatus));
270c0b746e5SOllivier Robert}
271c0b746e5SOllivier Robert
272c0b746e5SOllivier Robert;#---------------------------------
273c0b746e5SOllivier Robert;# error status
274c0b746e5SOllivier Robert;#
275c0b746e5SOllivier Robert;# format: |Err|reserved|  Err=8bit
276c0b746e5SOllivier Robert;#
277c0b746e5SOllivier Robertsub esw_Err { return ($_[$[] >> 8) & 0xff; }
278c0b746e5SOllivier Robert
279c0b746e5SOllivier Robert%ErrorStatus = (0, "err_unspec",
280c0b746e5SOllivier Robert		1, "err_auth_fail",
281c0b746e5SOllivier Robert		2, "err_invalid_fmt",
282c0b746e5SOllivier Robert		3, "err_invalid_opcode",
283c0b746e5SOllivier Robert		4, "err_unknown_assoc",
284c0b746e5SOllivier Robert		5, "err_unknown_var",
285c0b746e5SOllivier Robert		6, "err_invalid_value",
286c0b746e5SOllivier Robert		7, "err_adm_prohibit",
287c0b746e5SOllivier Robert		);
288c0b746e5SOllivier Robert
289c0b746e5SOllivier Robertsub error_status
290c0b746e5SOllivier Robert{
291c0b746e5SOllivier Robert    return sprintf("%s", &getval(&esw_Err($_[$[]),*ErrorStatus));
292c0b746e5SOllivier Robert}
293c0b746e5SOllivier Robert
294c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
295c0b746e5SOllivier Robert;#
296c0b746e5SOllivier Robert;# cntrl op name translation
297c0b746e5SOllivier Robert
298c0b746e5SOllivier Robert%CntrlOpName = (1, "read_status",
299c0b746e5SOllivier Robert		2, "read_variables",
300c0b746e5SOllivier Robert		3, "write_variables",
301c0b746e5SOllivier Robert		4, "read_clock_variables",
302c0b746e5SOllivier Robert		5, "write_clock_variables",
303c0b746e5SOllivier Robert		6, "set_trap",
304c0b746e5SOllivier Robert		7, "trap_response",
305c0b746e5SOllivier Robert		31, "unset_trap", # !!! unofficial !!!
306c0b746e5SOllivier Robert		"-", "cntrlop",
307c0b746e5SOllivier Robert		);
308c0b746e5SOllivier Robert
309c0b746e5SOllivier Robertsub cntrlop_name
310c0b746e5SOllivier Robert{
311c0b746e5SOllivier Robert    return &getval($_[$[],*CntrlOpName);
312c0b746e5SOllivier Robert}
313c0b746e5SOllivier Robert
314c0b746e5SOllivier Robert;#-----------------------------------------------------------------------------
315c0b746e5SOllivier Robert
316c0b746e5SOllivier Robert$STAT_short_pkt = 0;
317c0b746e5SOllivier Robert$STAT_pkt = 0;
318c0b746e5SOllivier Robert
319c0b746e5SOllivier Robert;# process a NTP control message (response) packet
320c0b746e5SOllivier Robert;# returns a list ($ret,$data,$status,$associd,$op,$seq,$auth_keyid)
321c0b746e5SOllivier Robert;#      $ret: undef     --> not yet complete
322c0b746e5SOllivier Robert;#            ""        --> complete packet received
323c0b746e5SOllivier Robert;#            "ERROR"   --> error during receive, bad packet, ...
324c0b746e5SOllivier Robert;#          else        --> error packet - list may contain useful info
325c0b746e5SOllivier Robert
326c0b746e5SOllivier Robert
327c0b746e5SOllivier Robertsub handle_packet
328c0b746e5SOllivier Robert{
329c0b746e5SOllivier Robert    local($pkt,$from) = @_;	# parameters
330c0b746e5SOllivier Robert    local($len_pkt) = (length($pkt));
331c0b746e5SOllivier Robert;#    local(*FRAGS,*lastseen);
332c0b746e5SOllivier Robert    local($li_vn_mode,$r_e_m_op,$seq,$status,$associd,$offset,$count,$data);
333c0b746e5SOllivier Robert    local($autch_keyid,$auth_cksum);
334c0b746e5SOllivier Robert
335c0b746e5SOllivier Robert    $STAT_pkt++;
336c0b746e5SOllivier Robert    if ($len_pkt < 12)
337c0b746e5SOllivier Robert    {
338c0b746e5SOllivier Robert	$STAT_short_pkt++;
339c0b746e5SOllivier Robert	return ("ERROR","short packet received");
340c0b746e5SOllivier Robert    }
341c0b746e5SOllivier Robert
342c0b746e5SOllivier Robert    ;# now break packet apart
343c0b746e5SOllivier Robert    ($li_vn_mode,$r_e_m_op,$seq,$status,$associd,$offset,$count,$data) =
344c0b746e5SOllivier Robert	unpack("C2n5a".($len_pkt-12),$pkt);
345c0b746e5SOllivier Robert    $data=substr($data,$[,$count);
346c0b746e5SOllivier Robert    if ((($len_pkt - 12) - &pad($count,4)) >= 12)
347c0b746e5SOllivier Robert    {
348c0b746e5SOllivier Robert	;# looks like an authenticator
349c0b746e5SOllivier Robert	($auth_keyid,$auth_cksum) =
350c0b746e5SOllivier Robert	    unpack("Na8",substr($pkt,$len_pkt-12+$[,12));
351c0b746e5SOllivier Robert	$STAT_auth++;
352c0b746e5SOllivier Robert	;# no checking of auth_cksum (yet ?)
353c0b746e5SOllivier Robert    }
354c0b746e5SOllivier Robert
355c0b746e5SOllivier Robert    if (&pkt_VN($li_vn_mode) != $NTP_version)
356c0b746e5SOllivier Robert    {
357c0b746e5SOllivier Robert	$STAT_bad_version++;
358c0b746e5SOllivier Robert	return ("ERROR","version ".&pkt_VN($li_vn_mode)."packet ignored");
359c0b746e5SOllivier Robert    }
360c0b746e5SOllivier Robert
361c0b746e5SOllivier Robert    if (&pkt_MODE($li_vn_mode) != $ctrl_mode)
362c0b746e5SOllivier Robert    {
363c0b746e5SOllivier Robert	$STAT_bad_mode++;
364c0b746e5SOllivier Robert	return ("ERROR", "mode ".&pkt_MODE($li_vn_mode)." packet ignored");
365c0b746e5SOllivier Robert    }
366c0b746e5SOllivier Robert
367c0b746e5SOllivier Robert    ;# handle single fragment fast
368c0b746e5SOllivier Robert    if ($offset == 0 && &pkt_M($r_e_m_op) == 0)
369c0b746e5SOllivier Robert    {
370c0b746e5SOllivier Robert	$STAT_single_frag++;
371c0b746e5SOllivier Robert	if (&pkt_E($r_e_m_op))
372c0b746e5SOllivier Robert	{
373c0b746e5SOllivier Robert	    $STAT_err_pkt++;
374c0b746e5SOllivier Robert	    return (&error_status($status),
375c0b746e5SOllivier Robert		    $data,$status,$associd,&pkt_OP($r_e_m_op),$seq,
376c0b746e5SOllivier Robert		    $auth_keyid);
377c0b746e5SOllivier Robert	}
378c0b746e5SOllivier Robert	else
379c0b746e5SOllivier Robert	{
380c0b746e5SOllivier Robert	    return ("",
381c0b746e5SOllivier Robert		    $data,$status,$associd,&pkt_OP($r_e_m_op),$seq,
382c0b746e5SOllivier Robert		    $auth_keyid);
383c0b746e5SOllivier Robert	}
384c0b746e5SOllivier Robert    }
385c0b746e5SOllivier Robert    else
386c0b746e5SOllivier Robert    {
387c0b746e5SOllivier Robert	;# fragment - set up local name space
388c0b746e5SOllivier Robert	$id = "$from$seq".&pkt_OP($r_e_m_op);
389c0b746e5SOllivier Robert	$ID{$id} = 1;
390c0b746e5SOllivier Robert	*FRAGS = "$id FRAGS";
391c0b746e5SOllivier Robert	*lastseen = "$id lastseen";
392c0b746e5SOllivier Robert
393c0b746e5SOllivier Robert	$STAT_frag++;
394c0b746e5SOllivier Robert
395c0b746e5SOllivier Robert	$lastseen = 1 if !&pkt_M($r_e_m_op);
396c0b746e5SOllivier Robert	if (!defined(%FRAGS))
397c0b746e5SOllivier Robert	{
398224ba2bdSOllivier Robert	    print((&pkt_M($r_e_m_op) ? " more" : "")."\n");
399c0b746e5SOllivier Robert	    $FRAGS{$offset} = $data;
400c0b746e5SOllivier Robert	    ;# save other info
401c0b746e5SOllivier Robert	    @FRAGS = ($status,$associd,&pkt_OP($r_e_m_op),$seq,$auth_keyid,$r_e_m_op);
402c0b746e5SOllivier Robert	}
403c0b746e5SOllivier Robert	else
404c0b746e5SOllivier Robert	{
405224ba2bdSOllivier Robert	    print((&pkt_M($r_e_m_op) ? " more" : "")."\n");
406c0b746e5SOllivier Robert	    ;# add frag to previous - combine on the fly
407c0b746e5SOllivier Robert	    if (defined($FRAGS{$offset}))
408c0b746e5SOllivier Robert	    {
409c0b746e5SOllivier Robert		$STAT_dup_frag++;
410c0b746e5SOllivier Robert		return ("ERROR","duplicate fragment at $offset seq=$seq");
411c0b746e5SOllivier Robert	    }
412c0b746e5SOllivier Robert
413c0b746e5SOllivier Robert	    $FRAGS{$offset} = $data;
414c0b746e5SOllivier Robert
415c0b746e5SOllivier Robert	    undef($loff);
416c0b746e5SOllivier Robert	    foreach $off (sort numerical keys(%FRAGS))
417c0b746e5SOllivier Robert	    {
418c0b746e5SOllivier Robert		next unless defined($FRAGS{$off});
419c0b746e5SOllivier Robert		if (defined($loff) &&
420c0b746e5SOllivier Robert		    ($loff + length($FRAGS{$loff})) == $off)
421c0b746e5SOllivier Robert		{
422c0b746e5SOllivier Robert		    $FRAGS{$loff} .= $FRAGS{$off};
423c0b746e5SOllivier Robert		    delete $FRAGS{$off};
424c0b746e5SOllivier Robert		    last;
425c0b746e5SOllivier Robert		}
426c0b746e5SOllivier Robert		$loff = $off;
427c0b746e5SOllivier Robert	    }
428c0b746e5SOllivier Robert
429c0b746e5SOllivier Robert	    ;# return packet if all frags arrived
430c0b746e5SOllivier Robert	    ;# at most two frags with possible padding ???
431c0b746e5SOllivier Robert	    if ($lastseen && defined($FRAGS{0}) &&
432c0b746e5SOllivier Robert		(((scalar(@x=sort numerical keys(%FRAGS)) == 2) &&
433c0b746e5SOllivier Robert		  (length($FRAGS{0}) + 8) > $x[$[+1]) ||
434c0b746e5SOllivier Robert		  (scalar(@x=sort numerical keys(%FRAGS)) < 2)))
435c0b746e5SOllivier Robert	    {
436c0b746e5SOllivier Robert		@x=((&pkt_E($r_e_m_op) ? &error_status($status) : ""),
437c0b746e5SOllivier Robert		    $FRAGS{0},@FRAGS);
438c0b746e5SOllivier Robert		&pkt_E($r_e_m_op) ? $STAT_err_frag++ : $STAT_frag_all++;
439c0b746e5SOllivier Robert		undef(%FRAGS);
440c0b746e5SOllivier Robert		undef(@FRAGS);
441c0b746e5SOllivier Robert		undef($lastseen);
442c0b746e5SOllivier Robert		delete $ID{$id};
443c0b746e5SOllivier Robert		&main'clear_timeout($id);
444c0b746e5SOllivier Robert		return @x;
445c0b746e5SOllivier Robert	    }
446c0b746e5SOllivier Robert	    else
447c0b746e5SOllivier Robert	    {
448c0b746e5SOllivier Robert		&main'set_timeout($id,time+$timeout,"&ntp'handle_packet_timeout(\"".unpack("H*",$id)."\");"); #'";
449c0b746e5SOllivier Robert	    }
450c0b746e5SOllivier Robert	}
451c0b746e5SOllivier Robert	return (undef);
452c0b746e5SOllivier Robert    }
453c0b746e5SOllivier Robert}
454c0b746e5SOllivier Robert
455c0b746e5SOllivier Robertsub handle_packet_timeout
456c0b746e5SOllivier Robert{
457c0b746e5SOllivier Robert    local($id) = @_;
458c0b746e5SOllivier Robert    local($r_e_m_op,*FRAGS,*lastseen,@x) = (@FRAGS[$[+5]);
459c0b746e5SOllivier Robert
460c0b746e5SOllivier Robert    *FRAGS = "$id FRAGS";
461c0b746e5SOllivier Robert    *lastseen = "$id lastseen";
462c0b746e5SOllivier Robert
463c0b746e5SOllivier Robert    @x=((&pkt_E($r_e_m_op) ? &error_status($status) : "TIMEOUT"),
464c0b746e5SOllivier Robert	$FRAGS{0},@FRAGS[$[ .. $[+4]);
465c0b746e5SOllivier Robert    $STAT_frag_timeout++;
466c0b746e5SOllivier Robert    undef(%FRAGS);
467c0b746e5SOllivier Robert    undef(@FRAGS);
468c0b746e5SOllivier Robert    undef($lastseen);
469c0b746e5SOllivier Robert    delete $ID{$id};
470c0b746e5SOllivier Robert    return @x;
471c0b746e5SOllivier Robert}
472c0b746e5SOllivier Robert
473c0b746e5SOllivier Robert
474c0b746e5SOllivier Robertsub pad
475c0b746e5SOllivier Robert{
476c0b746e5SOllivier Robert    return $_[$[+1] * int(($_[$[] + $_[$[+1] - 1) / $_[$[+1]);
477c0b746e5SOllivier Robert}
478c0b746e5SOllivier Robert
479c0b746e5SOllivier Robert1;
480