Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / ntpdc / ntpdc_ops.c
blob6d9985543bda9a57c2f1a3c3245c033c402427b2
1 /* $NetBSD$ */
3 /*
4 * ntpdc_ops.c - subroutines which are called to perform operations by
5 * ntpdc
6 */
8 #ifdef HAVE_CONFIG_H
9 # include <config.h>
10 #endif
12 #include <stdio.h>
13 #include <stddef.h>
15 #include "ntpdc.h"
16 #include "ntp_net.h"
17 #include "ntp_control.h"
18 #include "ntp_refclock.h"
19 #include "ntp_stdlib.h"
21 #include <ctype.h>
22 #ifdef HAVE_SYS_TIMEX_H
23 # include <sys/timex.h>
24 #endif
25 #if !defined(__bsdi__) && !defined(apollo)
26 #ifdef HAVE_NETINET_IN_H
27 #include <netinet/in.h>
28 #endif
29 #endif
31 #include <arpa/inet.h>
34 * utility functions
36 static int checkitems (int, FILE *);
37 static int checkitemsize (int, int);
38 static int check1item (int, FILE *);
41 * Declarations for command handlers in here
43 static void peerlist (struct parse *, FILE *);
44 static void peers (struct parse *, FILE *);
45 static void doconfig (struct parse *pcmd, FILE *fp, int mode, int refc);
46 static void dmpeers (struct parse *, FILE *);
47 static void dopeers (struct parse *, FILE *, int);
48 static void printpeer (struct info_peer *, FILE *);
49 static void showpeer (struct parse *, FILE *);
50 static void peerstats (struct parse *, FILE *);
51 static void loopinfo (struct parse *, FILE *);
52 static void sysinfo (struct parse *, FILE *);
53 static void sysstats (struct parse *, FILE *);
54 static void iostats (struct parse *, FILE *);
55 static void memstats (struct parse *, FILE *);
56 static void timerstats (struct parse *, FILE *);
57 static void addpeer (struct parse *, FILE *);
58 static void addserver (struct parse *, FILE *);
59 static void addrefclock (struct parse *, FILE *);
60 static void broadcast (struct parse *, FILE *);
61 static void doconfig (struct parse *, FILE *, int, int);
62 static void unconfig (struct parse *, FILE *);
63 static void set (struct parse *, FILE *);
64 static void sys_clear (struct parse *, FILE *);
65 static void doset (struct parse *, FILE *, int);
66 static void reslist (struct parse *, FILE *);
67 static void new_restrict (struct parse *, FILE *);
68 static void unrestrict (struct parse *, FILE *);
69 static void delrestrict (struct parse *, FILE *);
70 static void do_restrict (struct parse *, FILE *, int);
71 static void monlist (struct parse *, FILE *);
72 static void reset (struct parse *, FILE *);
73 static void preset (struct parse *, FILE *);
74 static void readkeys (struct parse *, FILE *);
75 static void trustkey (struct parse *, FILE *);
76 static void untrustkey (struct parse *, FILE *);
77 static void do_trustkey (struct parse *, FILE *, int);
78 static void authinfo (struct parse *, FILE *);
79 static void traps (struct parse *, FILE *);
80 static void addtrap (struct parse *, FILE *);
81 static void clrtrap (struct parse *, FILE *);
82 static void do_addclr_trap (struct parse *, FILE *, int);
83 static void requestkey (struct parse *, FILE *);
84 static void controlkey (struct parse *, FILE *);
85 static void do_changekey (struct parse *, FILE *, int);
86 static void ctlstats (struct parse *, FILE *);
87 static void clockstat (struct parse *, FILE *);
88 static void fudge (struct parse *, FILE *);
89 static void clkbug (struct parse *, FILE *);
90 static void kerninfo (struct parse *, FILE *);
91 static void get_if_stats (struct parse *, FILE *);
92 static void do_if_reload (struct parse *, FILE *);
95 * Commands we understand. Ntpdc imports this.
97 struct xcmd opcmds[] = {
98 { "listpeers", peerlist, { OPT|IP_VERSION, NO, NO, NO },
99 { "-4|-6", "", "", "" },
100 "display list of peers the server knows about [IP Version]" },
101 { "peers", peers, { OPT|IP_VERSION, NO, NO, NO },
102 { "-4|-6", "", "", "" },
103 "display peer summary information [IP Version]" },
104 { "dmpeers", dmpeers, { OPT|IP_VERSION, NO, NO, NO },
105 { "-4|-6", "", "", "" },
106 "display peer summary info the way Dave Mills likes it (IP Version)" },
107 { "showpeer", showpeer, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD},
108 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
109 "display detailed information for one or more peers" },
110 { "pstats", peerstats, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
111 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
112 "display statistical information for one or more peers" },
113 { "loopinfo", loopinfo, { OPT|NTP_STR, NO, NO, NO },
114 { "oneline|multiline", "", "", "" },
115 "display loop filter information" },
116 { "sysinfo", sysinfo, { NO, NO, NO, NO },
117 { "", "", "", "" },
118 "display local server information" },
119 { "sysstats", sysstats, { NO, NO, NO, NO },
120 { "", "", "", "" },
121 "display local server statistics" },
122 { "memstats", memstats, { NO, NO, NO, NO },
123 { "", "", "", "" },
124 "display peer memory usage statistics" },
125 { "iostats", iostats, { NO, NO, NO, NO },
126 { "", "", "", "" },
127 "display I/O subsystem statistics" },
128 { "timerstats", timerstats, { NO, NO, NO, NO },
129 { "", "", "", "" },
130 "display event timer subsystem statistics" },
131 { "addpeer", addpeer, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
132 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
133 "configure a new peer association" },
134 { "addserver", addserver, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
135 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
136 "configure a new server" },
137 { "addrefclock",addrefclock, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_STR, OPT|NTP_STR },
138 { "addr", "mode", "minpoll|prefer", "minpoll|prefer" },
139 "configure a new server" },
140 { "broadcast", broadcast, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
141 { "addr", "keyid", "version", "minpoll" },
142 "configure broadcasting time service" },
143 { "unconfig", unconfig, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
144 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
145 "unconfigure existing peer assocations" },
146 { "enable", set, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
147 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
148 "set a system flag (auth, bclient, monitor, pll, kernel, stats)" },
149 { "disable", sys_clear, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
150 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
151 "clear a system flag (auth, bclient, monitor, pll, kernel, stats)" },
152 { "reslist", reslist, {OPT|IP_VERSION, NO, NO, NO },
153 { "-4|-6", "", "", "" },
154 "display the server's restrict list" },
155 { "restrict", new_restrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
156 { "address", "mask",
157 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
158 "..." },
159 "create restrict entry/add flags to entry" },
160 { "unrestrict", unrestrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
161 { "address", "mask",
162 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
163 "..." },
164 "remove flags from a restrict entry" },
165 { "delrestrict", delrestrict, { NTP_ADD, NTP_ADD, OPT|NTP_STR, NO },
166 { "address", "mask", "ntpport", "" },
167 "delete a restrict entry" },
168 { "monlist", monlist, { OPT|NTP_INT, NO, NO, NO },
169 { "version", "", "", "" },
170 "display data the server's monitor routines have collected" },
171 { "reset", reset, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
172 { "io|sys|mem|timer|auth|allpeers", "...", "...", "..." },
173 "reset various subsystem statistics counters" },
174 { "preset", preset, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
175 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
176 "reset stat counters associated with particular peer(s)" },
177 { "readkeys", readkeys, { NO, NO, NO, NO },
178 { "", "", "", "" },
179 "request a reread of the keys file and re-init of system keys" },
180 { "trustedkey", trustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
181 { "keyid", "keyid", "keyid", "keyid" },
182 "add one or more key ID's to the trusted list" },
183 { "untrustedkey", untrustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
184 { "keyid", "keyid", "keyid", "keyid" },
185 "remove one or more key ID's from the trusted list" },
186 { "authinfo", authinfo, { NO, NO, NO, NO },
187 { "", "", "", "" },
188 "display the state of the authentication code" },
189 { "traps", traps, { NO, NO, NO, NO },
190 { "", "", "", "" },
191 "display the traps set in the server" },
192 { "addtrap", addtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
193 { "address", "port", "interface", "" },
194 "configure a trap in the server" },
195 { "clrtrap", clrtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
196 { "address", "port", "interface", "" },
197 "remove a trap (configured or otherwise) from the server" },
198 { "requestkey", requestkey, { NTP_UINT, NO, NO, NO },
199 { "keyid", "", "", "" },
200 "change the keyid the server uses to authenticate requests" },
201 { "controlkey", controlkey, { NTP_UINT, NO, NO, NO },
202 { "keyid", "", "", "" },
203 "change the keyid the server uses to authenticate control messages" },
204 { "ctlstats", ctlstats, { NO, NO, NO, NO },
205 { "", "", "", "" },
206 "display packet count statistics from the control module" },
207 { "clockstat", clockstat, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
208 { "address", "address", "address", "address" },
209 "display clock status information" },
210 { "fudge", fudge, { NTP_ADD, NTP_STR, NTP_STR, NO },
211 { "address", "time1|time2|val1|val2|flags", "value", "" },
212 "set/change one of a clock's fudge factors" },
213 { "clkbug", clkbug, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
214 { "address", "address", "address", "address" },
215 "display clock debugging information" },
216 { "kerninfo", kerninfo, { NO, NO, NO, NO },
217 { "", "", "", "" },
218 "display the kernel pll/pps variables" },
219 { "ifstats", get_if_stats, { NO, NO, NO, NO },
220 { "", "", "", "" },
221 "list interface statistics" },
222 { "ifreload", do_if_reload, { NO, NO, NO, NO },
223 { "", "", "", "" },
224 "reload interface configuration" },
225 { 0, 0, { NO, NO, NO, NO },
226 { "", "", "", "" }, "" }
230 * For quick string comparisons
232 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
235 * SET_SS_LEN_IF_PRESENT - used by SET_ADDR, SET_ADDRS macros
238 #ifdef ISC_PLATFORM_HAVESALEN
239 #define SET_SS_LEN_IF_PRESENT(psau) \
240 do { \
241 (psau)->sas.ss_len = SOCKLEN(psau); \
242 } while (0)
243 #else
244 #define SET_SS_LEN_IF_PRESENT(psau) do { } while (0)
245 #endif
248 * SET_ADDR - setup address for v4/v6 as needed
250 #define SET_ADDR(address, v6flag, v4addr, v6addr) \
251 do { \
252 memset(&(address), 0, sizeof(address)); \
253 if (v6flag) { \
254 AF(&(address)) = AF_INET6; \
255 SOCK_ADDR6(&(address)) = (v6addr); \
256 } else { \
257 AF(&(address)) = AF_INET; \
258 NSRCADR(&(address)) = (v4addr); \
260 SET_SS_LEN_IF_PRESENT(&(address)); \
261 } while (0)
265 * SET_ADDRS - setup source and destination addresses for
266 * v4/v6 as needed
268 #define SET_ADDRS(a1, a2, info, a1prefix, a2prefix) \
269 do { \
270 memset(&(a1), 0, sizeof(a1)); \
271 memset(&(a2), 0, sizeof(a2)); \
272 if ((info)->v6_flag) { \
273 AF(&(a1)) = AF_INET6; \
274 AF(&(a2)) = AF_INET6; \
275 SOCK_ADDR6(&(a1)) = (info)->a1prefix##6; \
276 SOCK_ADDR6(&(a2)) = (info)->a2prefix##6; \
277 } else { \
278 AF(&(a1)) = AF_INET; \
279 AF(&(a2)) = AF_INET; \
280 NSRCADR(&(a1)) = (info)->a1prefix; \
281 NSRCADR(&(a2)) = (info)->a2prefix; \
283 SET_SS_LEN_IF_PRESENT(&(a1)); \
284 SET_SS_LEN_IF_PRESENT(&(a2)); \
285 } while (0)
289 * SET_ADDRS - setup source and destination addresses for
290 * v4/v6 as needed
292 #if 0
293 #define SET_ADDR_MASK(address, addrmask, info) \
294 do { \
295 memset(&(address), 0, sizeof(address)); \
296 memset(&(mask), 0, sizeof(mask)); \
297 if ((info)->v6_flag) { \
298 AF(&(address)) = AF_INET6; \
299 AF(&(addrmask)) = AF_INET6; \
300 SOCK_ADDR6(&(address)) = (info)->addr6; \
301 SOCK_ADDR6(&(addrmask)) = (info)->mask6; \
302 } else { \
303 AF(&(address)) = AF_INET; \
304 AF(&(addrmask)) = AF_INET; \
305 NSRCADR(&(address)) = (info)->addr; \
306 NSRCADR(&(addrmask)) = (info)->mask; \
308 SET_SS_LEN_IF_PRESENT(&(address)); \
309 SET_SS_LEN_IF_PRESENT(&(addrmask)); \
310 } while (0)
311 #endif
314 * checkitems - utility to print a message if no items were returned
316 static int
317 checkitems(
318 int items,
319 FILE *fp
322 if (items == 0) {
323 (void) fprintf(fp, "No data returned in response to query\n");
324 return 0;
326 return 1;
331 * checkitemsize - utility to print a message if the item size is wrong
333 static int
334 checkitemsize(
335 int itemsize,
336 int expected
339 if (itemsize != expected) {
340 (void) fprintf(stderr,
341 "***Incorrect item size returned by remote host (%d should be %d)\n",
342 itemsize, expected);
343 return 0;
345 return 1;
350 * check1item - check to make sure we have exactly one item
352 static int
353 check1item(
354 int items,
355 FILE *fp
358 if (items == 0) {
359 (void) fprintf(fp, "No data returned in response to query\n");
360 return 0;
362 if (items > 1) {
363 (void) fprintf(fp, "Expected one item in response, got %d\n",
364 items);
365 return 0;
367 return 1;
372 * peerlist - get a short list of peers
374 /*ARGSUSED*/
375 static void
376 peerlist(
377 struct parse *pcmd,
378 FILE *fp
381 struct info_peer_list *plist;
382 sockaddr_u paddr;
383 int items;
384 int itemsize;
385 int res;
387 again:
388 res = doquery(impl_ver, REQ_PEER_LIST, 0, 0, 0, (char *)NULL, &items,
389 &itemsize, (void *)&plist, 0,
390 sizeof(struct info_peer_list));
392 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
393 impl_ver = IMPL_XNTPD_OLD;
394 goto again;
397 if (res != 0)
398 return;
400 if (!checkitems(items, fp))
401 return;
403 if (!checkitemsize(itemsize, sizeof(struct info_peer_list)) &&
404 !checkitemsize(itemsize, v4sizeof(struct info_peer_list)))
405 return;
407 while (items > 0) {
408 SET_ADDR(paddr, plist->v6_flag, plist->addr, plist->addr6);
409 if ((pcmd->nargs == 0) ||
410 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
411 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
412 (void) fprintf(fp, "%-9s %s\n",
413 modetoa(plist->hmode),
414 nntohost(&paddr));
415 plist++;
416 items--;
422 * peers - show peer summary
424 static void
425 peers(
426 struct parse *pcmd,
427 FILE *fp
430 dopeers(pcmd, fp, 0);
434 * dmpeers - show peer summary, Dave Mills style
436 static void
437 dmpeers(
438 struct parse *pcmd,
439 FILE *fp
442 dopeers(pcmd, fp, 1);
447 * peers - show peer summary
449 /*ARGSUSED*/
450 static void
451 dopeers(
452 struct parse *pcmd,
453 FILE *fp,
454 int dmstyle
457 struct info_peer_summary *plist;
458 sockaddr_u dstadr;
459 sockaddr_u srcadr;
460 int items;
461 int itemsize;
462 int ntp_poll;
463 int res;
464 int c;
465 l_fp tempts;
467 again:
468 res = doquery(impl_ver, REQ_PEER_LIST_SUM, 0, 0, 0, (char *)NULL,
469 &items, &itemsize, (void *)&plist, 0,
470 sizeof(struct info_peer_summary));
472 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
473 impl_ver = IMPL_XNTPD_OLD;
474 goto again;
477 if (res != 0)
478 return;
480 if (!checkitems(items, fp))
481 return;
483 if (!checkitemsize(itemsize, sizeof(struct info_peer_summary)) &&
484 !checkitemsize(itemsize, v4sizeof(struct info_peer_summary)))
485 return;
487 (void) fprintf(fp,
488 " remote local st poll reach delay offset disp\n");
489 (void) fprintf(fp,
490 "=======================================================================\n");
491 while (items > 0) {
492 if (!dmstyle) {
493 if (plist->flags & INFO_FLAG_SYSPEER)
494 c = '*';
495 else if (plist->hmode == MODE_ACTIVE)
496 c = '+';
497 else if (plist->hmode == MODE_PASSIVE)
498 c = '-';
499 else if (plist->hmode == MODE_CLIENT)
500 c = '=';
501 else if (plist->hmode == MODE_BROADCAST)
502 c = '^';
503 else if (plist->hmode == MODE_BCLIENT)
504 c = '~';
505 else
506 c = ' ';
507 } else {
508 if (plist->flags & INFO_FLAG_SYSPEER)
509 c = '*';
510 else if (plist->flags & INFO_FLAG_SHORTLIST)
511 c = '+';
512 else if (plist->flags & INFO_FLAG_SEL_CANDIDATE)
513 c = '.';
514 else
515 c = ' ';
517 NTOHL_FP(&(plist->offset), &tempts);
518 ntp_poll = 1<<max(min3(plist->ppoll, plist->hpoll, NTP_MAXPOLL),
519 NTP_MINPOLL);
520 SET_ADDRS(dstadr, srcadr, plist, dstadr, srcadr);
521 if ((pcmd->nargs == 0) ||
522 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
523 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
524 (void) fprintf(fp,
525 "%c%-15.15s %-15.15s %2d %4d %3o %7.7s %9.9s %7.7s\n",
526 c, nntohost(&srcadr), stoa(&dstadr),
527 plist->stratum, ntp_poll, plist->reach,
528 fptoa(NTOHS_FP(plist->delay), 5),
529 lfptoa(&tempts, 6),
530 ufptoa(NTOHS_FP(plist->dispersion), 5));
531 plist++;
532 items--;
536 /* Convert a refid & stratum (in host order) to a string */
537 static char*
538 refid_string(
539 u_int32 refid,
540 int stratum
543 if (stratum <= 1) {
544 static char junk[5];
545 junk[4] = 0;
546 memmove(junk, (char *)&refid, 4);
547 return junk;
550 return numtoa(refid);
553 static void
554 print_pflag(
555 FILE *fp,
556 u_int32 flags
559 const char *str;
561 if (flags == 0) {
562 (void) fprintf(fp, " none\n");
563 } else {
564 str = "";
565 if (flags & INFO_FLAG_SYSPEER) {
566 (void) fprintf(fp, " system_peer");
567 str = ",";
569 if (flags & INFO_FLAG_CONFIG) {
570 (void) fprintf(fp, "%s config", str);
571 str = ",";
573 if (flags & INFO_FLAG_REFCLOCK) {
574 (void) fprintf(fp, "%s refclock", str);
575 str = ",";
577 if (flags & INFO_FLAG_AUTHENABLE) {
578 (void) fprintf(fp, "%s auth", str);
579 str = ",";
581 if (flags & INFO_FLAG_BCLIENT) {
582 (void) fprintf(fp, "%s bclient", str);
583 str = ",";
585 if (flags & INFO_FLAG_PREFER) {
586 (void) fprintf(fp, "%s prefer", str);
587 str = ",";
589 if (flags & INFO_FLAG_IBURST) {
590 (void) fprintf(fp, "%s iburst", str);
591 str = ",";
593 if (flags & INFO_FLAG_BURST) {
594 (void) fprintf(fp, "%s burst", str);
596 (void) fprintf(fp, "\n");
600 * printpeer - print detail information for a peer
602 static void
603 printpeer(
604 register struct info_peer *pp,
605 FILE *fp
608 register int i;
609 l_fp tempts;
610 sockaddr_u srcadr, dstadr;
612 SET_ADDRS(dstadr, srcadr, pp, dstadr, srcadr);
614 (void) fprintf(fp, "remote %s, local %s\n",
615 stoa(&srcadr), stoa(&dstadr));
616 (void) fprintf(fp, "hmode %s, pmode %s, stratum %d, precision %d\n",
617 modetoa(pp->hmode), modetoa(pp->pmode),
618 pp->stratum, pp->precision);
620 (void) fprintf(fp,
621 "leap %c%c, refid [%s], rootdistance %s, rootdispersion %s\n",
622 pp->leap & 0x2 ? '1' : '0',
623 pp->leap & 0x1 ? '1' : '0',
624 refid_string(pp->refid, pp->stratum), fptoa(NTOHS_FP(pp->rootdelay), 5),
625 ufptoa(NTOHS_FP(pp->rootdispersion), 5));
627 (void) fprintf(fp,
628 "ppoll %d, hpoll %d, keyid %lu, version %d, association %u\n",
629 pp->ppoll, pp->hpoll, (u_long)pp->keyid, pp->version, ntohs(pp->associd));
631 (void) fprintf(fp,
632 "reach %03o, unreach %d, flash 0x%04x, ",
633 pp->reach, pp->unreach, pp->flash2);
635 (void) fprintf(fp, "boffset %s, ttl/mode %d\n",
636 fptoa(NTOHS_FP(pp->estbdelay), 5), pp->ttl);
638 (void) fprintf(fp, "timer %lds, flags", (long)ntohl(pp->timer));
639 print_pflag(fp, pp->flags);
641 NTOHL_FP(&pp->reftime, &tempts);
642 (void) fprintf(fp, "reference time: %s\n",
643 prettydate(&tempts));
644 NTOHL_FP(&pp->org, &tempts);
645 (void) fprintf(fp, "originate timestamp: %s\n",
646 prettydate(&tempts));
647 NTOHL_FP(&pp->rec, &tempts);
648 (void) fprintf(fp, "receive timestamp: %s\n",
649 prettydate(&tempts));
650 NTOHL_FP(&pp->xmt, &tempts);
651 (void) fprintf(fp, "transmit timestamp: %s\n",
652 prettydate(&tempts));
654 (void) fprintf(fp, "filter delay: ");
655 for (i = 0; i < NTP_SHIFT; i++) {
656 (void) fprintf(fp, " %-8.8s",
657 fptoa(NTOHS_FP(pp->filtdelay[i]), 5));
658 if (i == (NTP_SHIFT>>1)-1)
659 (void) fprintf(fp, "\n ");
661 (void) fprintf(fp, "\n");
663 (void) fprintf(fp, "filter offset:");
664 for (i = 0; i < NTP_SHIFT; i++) {
665 NTOHL_FP(&pp->filtoffset[i], &tempts);
666 (void) fprintf(fp, " %-8.8s", lfptoa(&tempts, 6));
667 if (i == (NTP_SHIFT>>1)-1)
668 (void) fprintf(fp, "\n ");
670 (void) fprintf(fp, "\n");
672 (void) fprintf(fp, "filter order: ");
673 for (i = 0; i < NTP_SHIFT; i++) {
674 (void) fprintf(fp, " %-8d", pp->order[i]);
675 if (i == (NTP_SHIFT>>1)-1)
676 (void) fprintf(fp, "\n ");
678 (void) fprintf(fp, "\n");
681 NTOHL_FP(&pp->offset, &tempts);
682 (void) fprintf(fp,
683 "offset %s, delay %s, error bound %s, filter error %s\n",
684 lfptoa(&tempts, 6), fptoa(NTOHS_FP(pp->delay), 5),
685 ufptoa(NTOHS_FP(pp->dispersion), 5),
686 ufptoa(NTOHS_FP(pp->selectdisp), 5));
691 * showpeer - show detailed information for a peer
693 static void
694 showpeer(
695 struct parse *pcmd,
696 FILE *fp
699 struct info_peer *pp;
700 /* 4 is the maximum number of peers which will fit in a packet */
701 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
702 int qitemlim;
703 int qitems;
704 int items;
705 int itemsize;
706 int res;
707 int sendsize;
709 again:
710 if (impl_ver == IMPL_XNTPD)
711 sendsize = sizeof(struct info_peer_list);
712 else
713 sendsize = v4sizeof(struct info_peer_list);
715 qitemlim = min(pcmd->nargs, COUNTOF(plist));
716 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
717 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
718 pl->addr = NSRCADR(&pcmd->argval[qitems].netnum);
719 if (impl_ver == IMPL_XNTPD)
720 pl->v6_flag = 0;
721 } else {
722 if (impl_ver == IMPL_XNTPD_OLD) {
723 fprintf(stderr,
724 "***Server doesn't understand IPv6 addresses\n");
725 return;
727 pl->addr6 = SOCK_ADDR6(&pcmd->argval[qitems].netnum);
728 pl->v6_flag = 1;
730 pl->port = (u_short)s_port;
731 pl->hmode = pl->flags = 0;
732 pl = (struct info_peer_list *)((char *)pl + sendsize);
735 res = doquery(impl_ver, REQ_PEER_INFO, 0, qitems,
736 sendsize, (char *)plist, &items,
737 &itemsize, (void *)&pp, 0, sizeof(struct info_peer));
739 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
740 impl_ver = IMPL_XNTPD_OLD;
741 goto again;
744 if (res != 0)
745 return;
747 if (!checkitems(items, fp))
748 return;
750 if (!checkitemsize(itemsize, sizeof(struct info_peer)) &&
751 !checkitemsize(itemsize, v4sizeof(struct info_peer)))
752 return;
754 while (items-- > 0) {
755 printpeer(pp, fp);
756 if (items > 0)
757 (void) fprintf(fp, "\n");
758 pp++;
764 * peerstats - return statistics for a peer
766 static void
767 peerstats(
768 struct parse *pcmd,
769 FILE *fp
772 struct info_peer_stats *pp;
773 /* 4 is the maximum number of peers which will fit in a packet */
774 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
775 sockaddr_u src, dst;
776 int qitemlim;
777 int qitems;
778 int items;
779 int itemsize;
780 int res;
781 int sendsize;
783 again:
784 if (impl_ver == IMPL_XNTPD)
785 sendsize = sizeof(struct info_peer_list);
786 else
787 sendsize = v4sizeof(struct info_peer_list);
789 memset(plist, 0, sizeof(plist));
791 qitemlim = min(pcmd->nargs, COUNTOF(plist));
792 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
793 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
794 pl->addr = NSRCADR(&pcmd->argval[qitems].netnum);
795 if (impl_ver == IMPL_XNTPD)
796 pl->v6_flag = 0;
797 } else {
798 if (impl_ver == IMPL_XNTPD_OLD) {
799 fprintf(stderr,
800 "***Server doesn't understand IPv6 addresses\n");
801 return;
803 pl->addr6 = SOCK_ADDR6(&pcmd->argval[qitems].netnum);
804 pl->v6_flag = 1;
806 pl->port = (u_short)s_port;
807 pl->hmode = plist[qitems].flags = 0;
808 pl = (struct info_peer_list *)((char *)pl + sendsize);
811 res = doquery(impl_ver, REQ_PEER_STATS, 0, qitems,
812 sendsize, (char *)plist, &items,
813 &itemsize, (void *)&pp, 0,
814 sizeof(struct info_peer_stats));
816 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
817 impl_ver = IMPL_XNTPD_OLD;
818 goto again;
821 if (res != 0)
822 return;
824 if (!checkitems(items, fp))
825 return;
827 if (!checkitemsize(itemsize, sizeof(struct info_peer_stats)) &&
828 !checkitemsize(itemsize, v4sizeof(struct info_peer_stats)))
829 return;
831 while (items-- > 0) {
832 ZERO_SOCK(&dst);
833 ZERO_SOCK(&src);
834 if (pp->v6_flag != 0) {
835 AF(&dst) = AF_INET6;
836 AF(&src) = AF_INET6;
837 SOCK_ADDR6(&dst) = pp->dstadr6;
838 SOCK_ADDR6(&src) = pp->srcadr6;
839 } else {
840 AF(&dst) = AF_INET;
841 AF(&src) = AF_INET;
842 NSRCADR(&dst) = pp->dstadr;
843 NSRCADR(&src) = pp->srcadr;
845 #ifdef ISC_PLATFORM_HAVESALEN
846 src.sas.ss_len = SOCKLEN(&src);
847 dst.sas.ss_len = SOCKLEN(&dst);
848 #endif
849 (void) fprintf(fp, "remote host: %s\n",
850 nntohost(&src));
851 (void) fprintf(fp, "local interface: %s\n",
852 stoa(&dst));
853 (void) fprintf(fp, "time last received: %lds\n",
854 (long)ntohl(pp->timereceived));
855 (void) fprintf(fp, "time until next send: %lds\n",
856 (long)ntohl(pp->timetosend));
857 (void) fprintf(fp, "reachability change: %lds\n",
858 (long)ntohl(pp->timereachable));
859 (void) fprintf(fp, "packets sent: %ld\n",
860 (long)ntohl(pp->sent));
861 (void) fprintf(fp, "packets received: %ld\n",
862 (long)ntohl(pp->processed));
863 (void) fprintf(fp, "bad authentication: %ld\n",
864 (long)ntohl(pp->badauth));
865 (void) fprintf(fp, "bogus origin: %ld\n",
866 (long)ntohl(pp->bogusorg));
867 (void) fprintf(fp, "duplicate: %ld\n",
868 (long)ntohl(pp->oldpkt));
869 (void) fprintf(fp, "bad dispersion: %ld\n",
870 (long)ntohl(pp->seldisp));
871 (void) fprintf(fp, "bad reference time: %ld\n",
872 (long)ntohl(pp->selbroken));
873 (void) fprintf(fp, "candidate order: %d\n",
874 (int)pp->candidate);
875 if (items > 0)
876 (void) fprintf(fp, "\n");
877 (void) fprintf(fp, "flags: ");
878 print_pflag(fp, ntohs(pp->flags));
879 pp++;
885 * loopinfo - show loop filter information
887 static void
888 loopinfo(
889 struct parse *pcmd,
890 FILE *fp
893 struct info_loop *il;
894 int items;
895 int itemsize;
896 int oneline = 0;
897 int res;
898 l_fp tempts;
900 if (pcmd->nargs > 0) {
901 if (STREQ(pcmd->argval[0].string, "oneline"))
902 oneline = 1;
903 else if (STREQ(pcmd->argval[0].string, "multiline"))
904 oneline = 0;
905 else {
906 (void) fprintf(stderr, "How many lines?\n");
907 return;
911 again:
912 res = doquery(impl_ver, REQ_LOOP_INFO, 0, 0, 0, (char *)NULL,
913 &items, &itemsize, (void *)&il, 0,
914 sizeof(struct info_loop));
916 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
917 impl_ver = IMPL_XNTPD_OLD;
918 goto again;
921 if (res != 0)
922 return;
924 if (!check1item(items, fp))
925 return;
927 if (!checkitemsize(itemsize, sizeof(struct info_loop)))
928 return;
930 if (oneline) {
931 l_fp temp2ts;
933 NTOHL_FP(&il->last_offset, &tempts);
934 NTOHL_FP(&il->drift_comp, &temp2ts);
936 (void) fprintf(fp,
937 "offset %s, frequency %s, time_const %ld, watchdog %ld\n",
938 lfptoa(&tempts, 6),
939 lfptoa(&temp2ts, 3),
940 (long)(int32)ntohl((u_long)il->compliance),
941 (u_long)ntohl((u_long)il->watchdog_timer));
942 } else {
943 NTOHL_FP(&il->last_offset, &tempts);
944 (void) fprintf(fp, "offset: %s s\n",
945 lfptoa(&tempts, 6));
946 NTOHL_FP(&il->drift_comp, &tempts);
947 (void) fprintf(fp, "frequency: %s ppm\n",
948 lfptoa(&tempts, 3));
949 (void) fprintf(fp, "poll adjust: %ld\n",
950 (long)(int32)ntohl(il->compliance));
951 (void) fprintf(fp, "watchdog timer: %ld s\n",
952 (u_long)ntohl(il->watchdog_timer));
958 * sysinfo - show current system state
960 /*ARGSUSED*/
961 static void
962 sysinfo(
963 struct parse *pcmd,
964 FILE *fp
967 struct info_sys *is;
968 sockaddr_u peeraddr;
969 int items;
970 int itemsize;
971 int res;
972 l_fp tempts;
974 again:
975 res = doquery(impl_ver, REQ_SYS_INFO, 0, 0, 0, (char *)NULL,
976 &items, &itemsize, (void *)&is, 0,
977 sizeof(struct info_sys));
979 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
980 impl_ver = IMPL_XNTPD_OLD;
981 goto again;
984 if (res != 0)
985 return;
987 if (!check1item(items, fp))
988 return;
990 if (!checkitemsize(itemsize, sizeof(struct info_sys)) &&
991 !checkitemsize(itemsize, v4sizeof(struct info_sys)))
992 return;
994 SET_ADDR(peeraddr, is->v6_flag, is->peer, is->peer6);
996 (void) fprintf(fp, "system peer: %s\n", nntohost(&peeraddr));
997 (void) fprintf(fp, "system peer mode: %s\n", modetoa(is->peer_mode));
998 (void) fprintf(fp, "leap indicator: %c%c\n",
999 is->leap & 0x2 ? '1' : '0',
1000 is->leap & 0x1 ? '1' : '0');
1001 (void) fprintf(fp, "stratum: %d\n", (int)is->stratum);
1002 (void) fprintf(fp, "precision: %d\n", (int)is->precision);
1003 (void) fprintf(fp, "root distance: %s s\n",
1004 fptoa(NTOHS_FP(is->rootdelay), 5));
1005 (void) fprintf(fp, "root dispersion: %s s\n",
1006 ufptoa(NTOHS_FP(is->rootdispersion), 5));
1007 (void) fprintf(fp, "reference ID: [%s]\n",
1008 refid_string(is->refid, is->stratum));
1009 NTOHL_FP(&is->reftime, &tempts);
1010 (void) fprintf(fp, "reference time: %s\n", prettydate(&tempts));
1012 (void) fprintf(fp, "system flags: ");
1013 if ((is->flags & (INFO_FLAG_BCLIENT | INFO_FLAG_AUTHENABLE |
1014 INFO_FLAG_NTP | INFO_FLAG_KERNEL| INFO_FLAG_CAL |
1015 INFO_FLAG_PPS_SYNC | INFO_FLAG_MONITOR | INFO_FLAG_FILEGEN)) == 0) {
1016 (void) fprintf(fp, "none\n");
1017 } else {
1018 if (is->flags & INFO_FLAG_BCLIENT)
1019 (void) fprintf(fp, "bclient ");
1020 if (is->flags & INFO_FLAG_AUTHENTICATE)
1021 (void) fprintf(fp, "auth ");
1022 if (is->flags & INFO_FLAG_MONITOR)
1023 (void) fprintf(fp, "monitor ");
1024 if (is->flags & INFO_FLAG_NTP)
1025 (void) fprintf(fp, "ntp ");
1026 if (is->flags & INFO_FLAG_KERNEL)
1027 (void) fprintf(fp, "kernel ");
1028 if (is->flags & INFO_FLAG_FILEGEN)
1029 (void) fprintf(fp, "stats ");
1030 if (is->flags & INFO_FLAG_CAL)
1031 (void) fprintf(fp, "calibrate ");
1032 if (is->flags & INFO_FLAG_PPS_SYNC)
1033 (void) fprintf(fp, "pps ");
1034 (void) fprintf(fp, "\n");
1036 (void) fprintf(fp, "jitter: %s s\n",
1037 fptoa(ntohl(is->frequency), 6));
1038 (void) fprintf(fp, "stability: %s ppm\n",
1039 ufptoa(ntohl(is->stability), 3));
1040 (void) fprintf(fp, "broadcastdelay: %s s\n",
1041 fptoa(NTOHS_FP(is->bdelay), 6));
1042 NTOHL_FP(&is->authdelay, &tempts);
1043 (void) fprintf(fp, "authdelay: %s s\n", lfptoa(&tempts, 6));
1048 * sysstats - print system statistics
1050 /*ARGSUSED*/
1051 static void
1052 sysstats(
1053 struct parse *pcmd,
1054 FILE *fp
1057 struct info_sys_stats *ss;
1058 int items;
1059 int itemsize;
1060 int res;
1062 again:
1063 res = doquery(impl_ver, REQ_SYS_STATS, 0, 0, 0, (char *)NULL,
1064 &items, &itemsize, (void *)&ss, 0,
1065 sizeof(struct info_sys_stats));
1067 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1068 impl_ver = IMPL_XNTPD_OLD;
1069 goto again;
1072 if (res != 0)
1073 return;
1075 if (!check1item(items, fp))
1076 return;
1078 if (itemsize != sizeof(struct info_sys_stats) &&
1079 itemsize != sizeof(struct old_info_sys_stats)) {
1080 /* issue warning according to new structure size */
1081 checkitemsize(itemsize, sizeof(struct info_sys_stats));
1082 return;
1084 fprintf(fp, "time since restart: %ld\n",
1085 (u_long)ntohl(ss->timeup));
1086 fprintf(fp, "time since reset: %ld\n",
1087 (u_long)ntohl(ss->timereset));
1088 fprintf(fp, "packets received: %ld\n",
1089 (u_long)ntohl(ss->received));
1090 fprintf(fp, "packets processed: %ld\n",
1091 (u_long)ntohl(ss->processed));
1092 fprintf(fp, "current version: %ld\n",
1093 (u_long)ntohl(ss->newversionpkt));
1094 fprintf(fp, "previous version: %ld\n",
1095 (u_long)ntohl(ss->oldversionpkt));
1096 fprintf(fp, "declined: %ld\n",
1097 (u_long)ntohl(ss->unknownversion));
1098 fprintf(fp, "access denied: %ld\n",
1099 (u_long)ntohl(ss->denied));
1100 fprintf(fp, "bad length or format: %ld\n",
1101 (u_long)ntohl(ss->badlength));
1102 fprintf(fp, "bad authentication: %ld\n",
1103 (u_long)ntohl(ss->badauth));
1104 if (itemsize != sizeof(struct info_sys_stats))
1105 return;
1107 fprintf(fp, "rate exceeded: %ld\n",
1108 (u_long)ntohl(ss->limitrejected));
1114 * iostats - print I/O statistics
1116 /*ARGSUSED*/
1117 static void
1118 iostats(
1119 struct parse *pcmd,
1120 FILE *fp
1123 struct info_io_stats *io;
1124 int items;
1125 int itemsize;
1126 int res;
1128 again:
1129 res = doquery(impl_ver, REQ_IO_STATS, 0, 0, 0, (char *)NULL,
1130 &items, &itemsize, (void *)&io, 0,
1131 sizeof(struct info_io_stats));
1133 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1134 impl_ver = IMPL_XNTPD_OLD;
1135 goto again;
1138 if (res != 0)
1139 return;
1141 if (!check1item(items, fp))
1142 return;
1144 if (!checkitemsize(itemsize, sizeof(struct info_io_stats)))
1145 return;
1147 (void) fprintf(fp, "time since reset: %ld\n",
1148 (u_long)ntohl(io->timereset));
1149 (void) fprintf(fp, "receive buffers: %d\n",
1150 ntohs(io->totalrecvbufs));
1151 (void) fprintf(fp, "free receive buffers: %d\n",
1152 ntohs(io->freerecvbufs));
1153 (void) fprintf(fp, "used receive buffers: %d\n",
1154 ntohs(io->fullrecvbufs));
1155 (void) fprintf(fp, "low water refills: %d\n",
1156 ntohs(io->lowwater));
1157 (void) fprintf(fp, "dropped packets: %ld\n",
1158 (u_long)ntohl(io->dropped));
1159 (void) fprintf(fp, "ignored packets: %ld\n",
1160 (u_long)ntohl(io->ignored));
1161 (void) fprintf(fp, "received packets: %ld\n",
1162 (u_long)ntohl(io->received));
1163 (void) fprintf(fp, "packets sent: %ld\n",
1164 (u_long)ntohl(io->sent));
1165 (void) fprintf(fp, "packets not sent: %ld\n",
1166 (u_long)ntohl(io->notsent));
1167 (void) fprintf(fp, "interrupts handled: %ld\n",
1168 (u_long)ntohl(io->interrupts));
1169 (void) fprintf(fp, "received by int: %ld\n",
1170 (u_long)ntohl(io->int_received));
1175 * memstats - print peer memory statistics
1177 /*ARGSUSED*/
1178 static void
1179 memstats(
1180 struct parse *pcmd,
1181 FILE *fp
1184 struct info_mem_stats *mem;
1185 int i;
1186 int items;
1187 int itemsize;
1188 int res;
1190 again:
1191 res = doquery(impl_ver, REQ_MEM_STATS, 0, 0, 0, (char *)NULL,
1192 &items, &itemsize, (void *)&mem, 0,
1193 sizeof(struct info_mem_stats));
1195 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1196 impl_ver = IMPL_XNTPD_OLD;
1197 goto again;
1200 if (res != 0)
1201 return;
1203 if (!check1item(items, fp))
1204 return;
1206 if (!checkitemsize(itemsize, sizeof(struct info_mem_stats)))
1207 return;
1209 (void) fprintf(fp, "time since reset: %ld\n",
1210 (u_long)ntohl(mem->timereset));
1211 (void) fprintf(fp, "total peer memory: %d\n",
1212 ntohs(mem->totalpeermem));
1213 (void) fprintf(fp, "free peer memory: %d\n",
1214 ntohs(mem->freepeermem));
1215 (void) fprintf(fp, "calls to findpeer: %ld\n",
1216 (u_long)ntohl(mem->findpeer_calls));
1217 (void) fprintf(fp, "new peer allocations: %ld\n",
1218 (u_long)ntohl(mem->allocations));
1219 (void) fprintf(fp, "peer demobilizations: %ld\n",
1220 (u_long)ntohl(mem->demobilizations));
1222 (void) fprintf(fp, "hash table counts: ");
1223 for (i = 0; i < NTP_HASH_SIZE; i++) {
1224 (void) fprintf(fp, "%4d", (int)mem->hashcount[i]);
1225 if ((i % 8) == 7 && i != (NTP_HASH_SIZE-1)) {
1226 (void) fprintf(fp, "\n ");
1229 (void) fprintf(fp, "\n");
1235 * timerstats - print timer statistics
1237 /*ARGSUSED*/
1238 static void
1239 timerstats(
1240 struct parse *pcmd,
1241 FILE *fp
1244 struct info_timer_stats *tim;
1245 int items;
1246 int itemsize;
1247 int res;
1249 again:
1250 res = doquery(impl_ver, REQ_TIMER_STATS, 0, 0, 0, (char *)NULL,
1251 &items, &itemsize, (void *)&tim, 0,
1252 sizeof(struct info_timer_stats));
1254 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1255 impl_ver = IMPL_XNTPD_OLD;
1256 goto again;
1259 if (res != 0)
1260 return;
1262 if (!check1item(items, fp))
1263 return;
1265 if (!checkitemsize(itemsize, sizeof(struct info_timer_stats)))
1266 return;
1268 (void) fprintf(fp, "time since reset: %ld\n",
1269 (u_long)ntohl(tim->timereset));
1270 (void) fprintf(fp, "alarms handled: %ld\n",
1271 (u_long)ntohl(tim->alarms));
1272 (void) fprintf(fp, "alarm overruns: %ld\n",
1273 (u_long)ntohl(tim->overflows));
1274 (void) fprintf(fp, "calls to transmit: %ld\n",
1275 (u_long)ntohl(tim->xmtcalls));
1280 * addpeer - configure an active mode association
1282 static void
1283 addpeer(
1284 struct parse *pcmd,
1285 FILE *fp
1288 doconfig(pcmd, fp, MODE_ACTIVE, 0);
1293 * addserver - configure a client mode association
1295 static void
1296 addserver(
1297 struct parse *pcmd,
1298 FILE *fp
1301 doconfig(pcmd, fp, MODE_CLIENT, 0);
1305 * addrefclock - configure a reference clock association
1307 static void
1308 addrefclock(
1309 struct parse *pcmd,
1310 FILE *fp
1313 doconfig(pcmd, fp, MODE_CLIENT, 1);
1317 * broadcast - configure a broadcast mode association
1319 static void
1320 broadcast(
1321 struct parse *pcmd,
1322 FILE *fp
1325 doconfig(pcmd, fp, MODE_BROADCAST, 0);
1330 * config - configure a new peer association
1332 static void
1333 doconfig(
1334 struct parse *pcmd,
1335 FILE *fp,
1336 int mode,
1337 int refc
1340 struct conf_peer cpeer;
1341 int items;
1342 int itemsize;
1343 char *dummy;
1344 u_long keyid;
1345 u_int version;
1346 u_char minpoll;
1347 u_char maxpoll;
1348 u_int flags;
1349 u_char cmode;
1350 int res;
1351 int sendsize;
1352 int numtyp;
1354 again:
1355 keyid = 0;
1356 version = 3;
1357 flags = 0;
1358 res = 0;
1359 cmode = 0;
1360 minpoll = NTP_MINDPOLL;
1361 maxpoll = NTP_MAXDPOLL;
1362 numtyp = 1;
1363 if (refc)
1364 numtyp = 5;
1366 if (impl_ver == IMPL_XNTPD)
1367 sendsize = sizeof(struct conf_peer);
1368 else
1369 sendsize = v4sizeof(struct conf_peer);
1371 items = 1;
1372 while (pcmd->nargs > items) {
1373 if (STREQ(pcmd->argval[items].string, "prefer"))
1374 flags |= CONF_FLAG_PREFER;
1375 else if (STREQ(pcmd->argval[items].string, "burst"))
1376 flags |= CONF_FLAG_BURST;
1377 else if (STREQ(pcmd->argval[items].string, "iburst"))
1378 flags |= CONF_FLAG_IBURST;
1379 else if (!refc && STREQ(pcmd->argval[items].string, "keyid"))
1380 numtyp = 1;
1381 else if (!refc && STREQ(pcmd->argval[items].string, "version"))
1382 numtyp = 2;
1383 else if (STREQ(pcmd->argval[items].string, "minpoll"))
1384 numtyp = 3;
1385 else if (STREQ(pcmd->argval[items].string, "maxpoll"))
1386 numtyp = 4;
1387 else {
1388 long val;
1389 if (!atoint(pcmd->argval[items].string, &val))
1390 numtyp = 0;
1391 switch (numtyp) {
1392 case 1:
1393 keyid = val;
1394 numtyp = 2;
1395 break;
1397 case 2:
1398 version = (u_int) val;
1399 numtyp = 0;
1400 break;
1402 case 3:
1403 minpoll = (u_char)val;
1404 numtyp = 0;
1405 break;
1407 case 4:
1408 maxpoll = (u_char)val;
1409 numtyp = 0;
1410 break;
1412 case 5:
1413 cmode = (u_char)val;
1414 numtyp = 0;
1415 break;
1417 default:
1418 (void) fprintf(fp, "*** '%s' not understood\n",
1419 pcmd->argval[items].string);
1420 res++;
1421 numtyp = 0;
1423 if (val < 0) {
1424 (void) fprintf(stderr,
1425 "***Value '%s' should be unsigned\n",
1426 pcmd->argval[items].string);
1427 res++;
1430 items++;
1432 if (keyid > 0)
1433 flags |= CONF_FLAG_AUTHENABLE;
1434 if (version > NTP_VERSION ||
1435 version < NTP_OLDVERSION) {
1436 (void)fprintf(fp, "***invalid version number: %u\n",
1437 version);
1438 res++;
1440 if (minpoll < NTP_MINPOLL || minpoll > NTP_MAXPOLL ||
1441 maxpoll < NTP_MINPOLL || maxpoll > NTP_MAXPOLL ||
1442 minpoll > maxpoll) {
1443 (void) fprintf(fp, "***min/max-poll must be within %d..%d\n",
1444 NTP_MINPOLL, NTP_MAXPOLL);
1445 res++;
1448 if (res)
1449 return;
1451 memset(&cpeer, 0, sizeof(cpeer));
1453 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1454 cpeer.peeraddr = NSRCADR(&pcmd->argval[0].netnum);
1455 if (impl_ver == IMPL_XNTPD)
1456 cpeer.v6_flag = 0;
1457 } else {
1458 if (impl_ver == IMPL_XNTPD_OLD) {
1459 fprintf(stderr,
1460 "***Server doesn't understand IPv6 addresses\n");
1461 return;
1463 cpeer.peeraddr6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
1464 cpeer.v6_flag = 1;
1466 cpeer.hmode = (u_char) mode;
1467 cpeer.keyid = keyid;
1468 cpeer.version = (u_char) version;
1469 cpeer.minpoll = minpoll;
1470 cpeer.maxpoll = maxpoll;
1471 cpeer.flags = (u_char)flags;
1472 cpeer.ttl = cmode;
1474 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1475 sendsize, (char *)&cpeer, &items,
1476 &itemsize, &dummy, 0, sizeof(struct conf_peer));
1478 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1479 impl_ver = IMPL_XNTPD_OLD;
1480 goto again;
1483 if (res == INFO_ERR_FMT) {
1484 (void) fprintf(fp,
1485 "***Retrying command with old conf_peer size\n");
1486 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1487 sizeof(struct old_conf_peer), (char *)&cpeer,
1488 &items, &itemsize, &dummy, 0,
1489 sizeof(struct conf_peer));
1491 if (res == 0)
1492 (void) fprintf(fp, "done!\n");
1493 return;
1498 * unconfig - unconfigure some associations
1500 static void
1501 unconfig(
1502 struct parse *pcmd,
1503 FILE *fp
1506 /* 8 is the maximum number of peers which will fit in a packet */
1507 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
1508 int qitemlim;
1509 int qitems;
1510 int items;
1511 int itemsize;
1512 char *dummy;
1513 int res;
1514 int sendsize;
1516 again:
1517 if (impl_ver == IMPL_XNTPD)
1518 sendsize = sizeof(struct conf_unpeer);
1519 else
1520 sendsize = v4sizeof(struct conf_unpeer);
1522 qitemlim = min(pcmd->nargs, COUNTOF(plist));
1523 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
1524 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1525 pl->peeraddr = NSRCADR(&pcmd->argval[qitems].netnum);
1526 if (impl_ver == IMPL_XNTPD)
1527 pl->v6_flag = 0;
1528 } else {
1529 if (impl_ver == IMPL_XNTPD_OLD) {
1530 fprintf(stderr,
1531 "***Server doesn't understand IPv6 addresses\n");
1532 return;
1534 pl->peeraddr6 =
1535 SOCK_ADDR6(&pcmd->argval[qitems].netnum);
1536 pl->v6_flag = 1;
1538 pl = (struct conf_unpeer *)((char *)pl + sendsize);
1541 res = doquery(impl_ver, REQ_UNCONFIG, 1, qitems,
1542 sendsize, (char *)plist, &items,
1543 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
1545 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1546 impl_ver = IMPL_XNTPD_OLD;
1547 goto again;
1550 if (res == 0)
1551 (void) fprintf(fp, "done!\n");
1556 * set - set some system flags
1558 static void
1559 set(
1560 struct parse *pcmd,
1561 FILE *fp
1564 doset(pcmd, fp, REQ_SET_SYS_FLAG);
1569 * clear - clear some system flags
1571 static void
1572 sys_clear(
1573 struct parse *pcmd,
1574 FILE *fp
1577 doset(pcmd, fp, REQ_CLR_SYS_FLAG);
1582 * doset - set/clear system flags
1584 static void
1585 doset(
1586 struct parse *pcmd,
1587 FILE *fp,
1588 int req
1591 struct conf_sys_flags sys;
1592 int items;
1593 int itemsize;
1594 char *dummy;
1595 int res;
1597 sys.flags = 0;
1598 res = 0;
1599 for (items = 0; items < pcmd->nargs; items++) {
1600 if (STREQ(pcmd->argval[items].string, "auth"))
1601 sys.flags |= SYS_FLAG_AUTH;
1602 else if (STREQ(pcmd->argval[items].string, "bclient"))
1603 sys.flags |= SYS_FLAG_BCLIENT;
1604 else if (STREQ(pcmd->argval[items].string, "calibrate"))
1605 sys.flags |= SYS_FLAG_CAL;
1606 else if (STREQ(pcmd->argval[items].string, "kernel"))
1607 sys.flags |= SYS_FLAG_KERNEL;
1608 else if (STREQ(pcmd->argval[items].string, "monitor"))
1609 sys.flags |= SYS_FLAG_MONITOR;
1610 else if (STREQ(pcmd->argval[items].string, "ntp"))
1611 sys.flags |= SYS_FLAG_NTP;
1612 else if (STREQ(pcmd->argval[items].string, "pps"))
1613 sys.flags |= SYS_FLAG_PPS;
1614 else if (STREQ(pcmd->argval[items].string, "stats"))
1615 sys.flags |= SYS_FLAG_FILEGEN;
1616 else {
1617 (void) fprintf(fp, "Unknown flag %s\n",
1618 pcmd->argval[items].string);
1619 res = 1;
1623 sys.flags = htonl(sys.flags);
1624 if (res || sys.flags == 0)
1625 return;
1627 again:
1628 res = doquery(impl_ver, req, 1, 1,
1629 sizeof(struct conf_sys_flags), (char *)&sys, &items,
1630 &itemsize, &dummy, 0, sizeof(struct conf_sys_flags));
1632 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1633 impl_ver = IMPL_XNTPD_OLD;
1634 goto again;
1637 if (res == 0)
1638 (void) fprintf(fp, "done!\n");
1643 * data for printing/interrpreting the restrict flags
1645 struct resflags {
1646 const char *str;
1647 int bit;
1650 /* XXX: HMS: we apparently don't report set bits we do not recognize. */
1652 static struct resflags resflagsV2[] = {
1653 { "ignore", 0x001 },
1654 { "noserve", 0x002 },
1655 { "notrust", 0x004 },
1656 { "noquery", 0x008 },
1657 { "nomodify", 0x010 },
1658 { "nopeer", 0x020 },
1659 { "notrap", 0x040 },
1660 { "lptrap", 0x080 },
1661 { "limited", 0x100 },
1662 { "", 0 }
1665 static struct resflags resflagsV3[] = {
1666 { "ignore", RES_IGNORE },
1667 { "noserve", RES_DONTSERVE },
1668 { "notrust", RES_DONTTRUST },
1669 { "noquery", RES_NOQUERY },
1670 { "nomodify", RES_NOMODIFY },
1671 { "nopeer", RES_NOPEER },
1672 { "notrap", RES_NOTRAP },
1673 { "lptrap", RES_LPTRAP },
1674 { "limited", RES_LIMITED },
1675 { "version", RES_VERSION },
1676 { "kod", RES_KOD },
1677 { "timeout", RES_TIMEOUT },
1679 { "", 0 }
1682 static struct resflags resmflags[] = {
1683 { "ntpport", RESM_NTPONLY },
1684 { "interface", RESM_INTERFACE },
1685 { "", 0 }
1690 * reslist - obtain and print the server's restrict list
1692 /*ARGSUSED*/
1693 static void
1694 reslist(
1695 struct parse *pcmd,
1696 FILE *fp
1699 struct info_restrict *rl;
1700 sockaddr_u resaddr;
1701 sockaddr_u maskaddr;
1702 int items;
1703 int itemsize;
1704 int res;
1705 int skip;
1706 char *addr;
1707 char *mask;
1708 struct resflags *rf;
1709 u_int32 count;
1710 u_short flags;
1711 u_short mflags;
1712 char flagstr[300];
1713 static const char *comma = ", ";
1715 again:
1716 res = doquery(impl_ver, REQ_GET_RESTRICT, 0, 0, 0, (char *)NULL,
1717 &items, &itemsize, (void *)&rl, 0,
1718 sizeof(struct info_restrict));
1720 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1721 impl_ver = IMPL_XNTPD_OLD;
1722 goto again;
1725 if (res != 0)
1726 return;
1728 if (!checkitems(items, fp))
1729 return;
1731 if (!checkitemsize(itemsize, sizeof(struct info_restrict)) &&
1732 !checkitemsize(itemsize, v4sizeof(struct info_restrict)))
1733 return;
1735 (void) fprintf(fp,
1736 " address mask count flags\n");
1737 (void) fprintf(fp,
1738 "=====================================================================\n");
1740 while (items > 0) {
1741 SET_ADDRS(resaddr, maskaddr, rl, addr, mask);
1742 if (rl->v6_flag != 0) {
1743 addr = nntohost(&resaddr);
1744 } else {
1745 if ((rl->mask == (u_int32)0xffffffff))
1746 addr = nntohost(&resaddr);
1747 else
1748 addr = stoa(&resaddr);
1750 mask = stoa(&maskaddr);
1751 skip = 1;
1752 if ((pcmd->nargs == 0) ||
1753 ((pcmd->argval->ival == 6) && (rl->v6_flag != 0)) ||
1754 ((pcmd->argval->ival == 4) && (rl->v6_flag == 0)))
1755 skip = 0;
1756 count = ntohl(rl->count);
1757 flags = ntohs(rl->flags);
1758 mflags = ntohs(rl->mflags);
1759 flagstr[0] = '\0';
1761 res = 1;
1762 rf = &resmflags[0];
1763 while (rf->bit != 0) {
1764 if (mflags & rf->bit) {
1765 if (!res)
1766 (void) strcat(flagstr, comma);
1767 res = 0;
1768 (void) strcat(flagstr, rf->str);
1770 rf++;
1773 rf = (impl_ver == IMPL_XNTPD_OLD)
1774 ? &resflagsV2[0]
1775 : &resflagsV3[0]
1777 while (rf->bit != 0) {
1778 if (flags & rf->bit) {
1779 if (!res)
1780 (void) strcat(flagstr, comma);
1781 res = 0;
1782 (void) strcat(flagstr, rf->str);
1784 rf++;
1787 if (flagstr[0] == '\0')
1788 (void) strcpy(flagstr, "none");
1790 if (!skip)
1791 (void) fprintf(fp, "%-15.15s %-15.15s %9ld %s\n",
1792 addr, mask, (u_long)count, flagstr);
1793 rl++;
1794 items--;
1801 * new_restrict - create/add a set of restrictions
1803 static void
1804 new_restrict(
1805 struct parse *pcmd,
1806 FILE *fp
1809 do_restrict(pcmd, fp, REQ_RESADDFLAGS);
1814 * unrestrict - remove restriction flags from existing entry
1816 static void
1817 unrestrict(
1818 struct parse *pcmd,
1819 FILE *fp
1822 do_restrict(pcmd, fp, REQ_RESSUBFLAGS);
1827 * delrestrict - delete an existing restriction
1829 static void
1830 delrestrict(
1831 struct parse *pcmd,
1832 FILE *fp
1835 do_restrict(pcmd, fp, REQ_UNRESTRICT);
1840 * do_restrict - decode commandline restrictions and make the request
1842 static void
1843 do_restrict(
1844 struct parse *pcmd,
1845 FILE *fp,
1846 int req_code
1849 struct conf_restrict cres;
1850 int items;
1851 int itemsize;
1852 char *dummy;
1853 u_int32 num;
1854 u_long bit;
1855 int i;
1856 int res;
1857 int err;
1858 int sendsize;
1860 /* Initialize cres */
1861 cres.addr = 0;
1862 cres.mask = 0;
1863 cres.flags = 0;
1864 cres.mflags = 0;
1865 cres.v6_flag = 0;
1867 again:
1868 if (impl_ver == IMPL_XNTPD)
1869 sendsize = sizeof(struct conf_restrict);
1870 else
1871 sendsize = v4sizeof(struct conf_restrict);
1873 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1874 cres.addr = NSRCADR(&pcmd->argval[0].netnum);
1875 cres.mask = NSRCADR(&pcmd->argval[1].netnum);
1876 if (impl_ver == IMPL_XNTPD)
1877 cres.v6_flag = 0;
1878 } else {
1879 if (impl_ver == IMPL_XNTPD_OLD) {
1880 fprintf(stderr,
1881 "***Server doesn't understand IPv6 addresses\n");
1882 return;
1884 cres.addr6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
1885 cres.v6_flag = 1;
1887 cres.flags = 0;
1888 cres.mflags = 0;
1889 err = 0;
1890 for (res = 2; res < pcmd->nargs; res++) {
1891 if (STREQ(pcmd->argval[res].string, "ntpport")) {
1892 cres.mflags |= RESM_NTPONLY;
1893 } else {
1894 for (i = 0; resflagsV3[i].bit != 0; i++) {
1895 if (STREQ(pcmd->argval[res].string,
1896 resflagsV3[i].str))
1897 break;
1899 if (resflagsV3[i].bit != 0) {
1900 cres.flags |= resflagsV3[i].bit;
1901 if (req_code == REQ_UNRESTRICT) {
1902 (void) fprintf(fp,
1903 "Flag %s inappropriate\n",
1904 resflagsV3[i].str);
1905 err++;
1907 } else {
1908 (void) fprintf(fp, "Unknown flag %s\n",
1909 pcmd->argval[res].string);
1910 err++;
1914 cres.flags = htons(cres.flags);
1915 cres.mflags = htons(cres.mflags);
1918 * Make sure mask for default address is zero. Otherwise,
1919 * make sure mask bits are contiguous.
1921 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1922 if (cres.addr == 0) {
1923 cres.mask = 0;
1924 } else {
1925 num = ntohl(cres.mask);
1926 for (bit = 0x80000000; bit != 0; bit >>= 1)
1927 if ((num & bit) == 0)
1928 break;
1929 for ( ; bit != 0; bit >>= 1)
1930 if ((num & bit) != 0)
1931 break;
1932 if (bit != 0) {
1933 (void) fprintf(fp, "Invalid mask %s\n",
1934 numtoa(cres.mask));
1935 err++;
1938 } else {
1939 /* XXX IPv6 sanity checking stuff */
1942 if (err)
1943 return;
1945 res = doquery(impl_ver, req_code, 1, 1,
1946 sendsize, (char *)&cres, &items,
1947 &itemsize, &dummy, 0, sizeof(struct conf_restrict));
1949 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1950 impl_ver = IMPL_XNTPD_OLD;
1951 goto again;
1954 if (res == 0)
1955 (void) fprintf(fp, "done!\n");
1956 return;
1961 * monlist - obtain and print the server's monitor data
1963 /*ARGSUSED*/
1964 static void
1965 monlist(
1966 struct parse *pcmd,
1967 FILE *fp
1970 char *struct_star;
1971 sockaddr_u addr;
1972 sockaddr_u dstadr;
1973 int items;
1974 int itemsize;
1975 int res;
1976 int version = -1;
1978 if (pcmd->nargs > 0) {
1979 version = pcmd->argval[0].ival;
1982 again:
1983 res = doquery(impl_ver,
1984 (version == 1 || version == -1) ? REQ_MON_GETLIST_1 :
1985 REQ_MON_GETLIST, 0, 0, 0, (char *)NULL,
1986 &items, &itemsize, &struct_star,
1987 (version < 0) ? (1 << INFO_ERR_REQ) : 0,
1988 sizeof(struct info_monitor_1));
1990 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1991 impl_ver = IMPL_XNTPD_OLD;
1992 goto again;
1995 if (res == INFO_ERR_REQ && version < 0)
1996 res = doquery(impl_ver, REQ_MON_GETLIST, 0, 0, 0, (char *)NULL,
1997 &items, &itemsize, &struct_star, 0,
1998 sizeof(struct info_monitor));
2000 if (res != 0)
2001 return;
2003 if (!checkitems(items, fp))
2004 return;
2006 if (itemsize == sizeof(struct info_monitor_1) ||
2007 itemsize == v4sizeof(struct info_monitor_1)) {
2008 struct info_monitor_1 *ml = (struct info_monitor_1 *) struct_star;
2010 (void) fprintf(fp,
2011 "remote address port local address count m ver rstr avgint lstint\n");
2012 (void) fprintf(fp,
2013 "===============================================================================\n");
2014 while (items > 0) {
2015 SET_ADDRS(dstadr, addr, ml, daddr, addr);
2016 if ((pcmd->nargs == 0) ||
2017 ((pcmd->argval->ival == 6) && (ml->v6_flag != 0)) ||
2018 ((pcmd->argval->ival == 4) && (ml->v6_flag == 0)))
2019 (void) fprintf(fp,
2020 "%-22.22s %5d %-15s %8ld %1d %1d %6lx %6lu %7lu\n",
2021 nntohost(&addr),
2022 ntohs(ml->port),
2023 stoa(&dstadr),
2024 (u_long)ntohl(ml->count),
2025 ml->mode,
2026 ml->version,
2027 (u_long)ntohl(ml->restr),
2028 (u_long)ntohl(ml->lasttime),
2029 (u_long)ntohl(ml->firsttime));
2030 ml++;
2031 items--;
2033 } else if (itemsize == sizeof(struct info_monitor) ||
2034 itemsize == v4sizeof(struct info_monitor)) {
2035 struct info_monitor *ml = (struct info_monitor *) struct_star;
2037 (void) fprintf(fp,
2038 " address port count mode ver rstr avgint lstint\n");
2039 (void) fprintf(fp,
2040 "===============================================================================\n");
2041 while (items > 0) {
2042 SET_ADDR(dstadr, ml->v6_flag, ml->addr, ml->addr6);
2043 if ((pcmd->nargs == 0) ||
2044 ((pcmd->argval->ival == 6) && (ml->v6_flag != 0)) ||
2045 ((pcmd->argval->ival == 4) && (ml->v6_flag == 0)))
2046 (void) fprintf(fp,
2047 "%-25.25s %5d %9ld %4d %2d %9lx %9lu %9lu\n",
2048 nntohost(&dstadr),
2049 ntohs(ml->port),
2050 (u_long)ntohl(ml->count),
2051 ml->mode,
2052 ml->version,
2053 (u_long)ntohl(ml->restr),
2054 (u_long)ntohl(ml->lasttime),
2055 (u_long)ntohl(ml->firsttime));
2056 ml++;
2057 items--;
2059 } else if (itemsize == sizeof(struct old_info_monitor)) {
2060 struct old_info_monitor *oml = (struct old_info_monitor *)struct_star;
2061 (void) fprintf(fp,
2062 " address port count mode version lasttime firsttime\n");
2063 (void) fprintf(fp,
2064 "======================================================================\n");
2065 while (items > 0) {
2066 SET_ADDR(dstadr, oml->v6_flag, oml->addr, oml->addr6);
2067 (void) fprintf(fp, "%-20.20s %5d %9ld %4d %3d %9lu %9lu\n",
2068 nntohost(&dstadr),
2069 ntohs(oml->port),
2070 (u_long)ntohl(oml->count),
2071 oml->mode,
2072 oml->version,
2073 (u_long)ntohl(oml->lasttime),
2074 (u_long)ntohl(oml->firsttime));
2075 oml++;
2076 items--;
2078 } else {
2079 /* issue warning according to new info_monitor size */
2080 checkitemsize(itemsize, sizeof(struct info_monitor));
2086 * Mapping between command line strings and stat reset flags
2088 struct statreset {
2089 const char *str;
2090 int flag;
2091 } sreset[] = {
2092 { "io", RESET_FLAG_IO },
2093 { "sys", RESET_FLAG_SYS },
2094 { "mem", RESET_FLAG_MEM },
2095 { "timer", RESET_FLAG_TIMER },
2096 { "auth", RESET_FLAG_AUTH },
2097 { "allpeers", RESET_FLAG_ALLPEERS },
2098 { "", 0 }
2102 * reset - reset statistic counters
2104 static void
2105 reset(
2106 struct parse *pcmd,
2107 FILE *fp
2110 struct reset_flags rflags;
2111 int items;
2112 int itemsize;
2113 char *dummy;
2114 int i;
2115 int res;
2116 int err;
2118 err = 0;
2119 rflags.flags = 0;
2120 for (res = 0; res < pcmd->nargs; res++) {
2121 for (i = 0; sreset[i].flag != 0; i++) {
2122 if (STREQ(pcmd->argval[res].string, sreset[i].str))
2123 break;
2125 if (sreset[i].flag == 0) {
2126 (void) fprintf(fp, "Flag %s unknown\n",
2127 pcmd->argval[res].string);
2128 err++;
2129 } else {
2130 rflags.flags |= sreset[i].flag;
2133 rflags.flags = htonl(rflags.flags);
2135 if (err) {
2136 (void) fprintf(fp, "Not done due to errors\n");
2137 return;
2140 again:
2141 res = doquery(impl_ver, REQ_RESET_STATS, 1, 1,
2142 sizeof(struct reset_flags), (char *)&rflags, &items,
2143 &itemsize, &dummy, 0, sizeof(struct reset_flags));
2145 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2146 impl_ver = IMPL_XNTPD_OLD;
2147 goto again;
2150 if (res == 0)
2151 (void) fprintf(fp, "done!\n");
2152 return;
2158 * preset - reset stat counters for particular peers
2160 static void
2161 preset(
2162 struct parse *pcmd,
2163 FILE *fp
2166 /* 8 is the maximum number of peers which will fit in a packet */
2167 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
2168 int qitemlim;
2169 int qitems;
2170 int items;
2171 int itemsize;
2172 char *dummy;
2173 int res;
2174 int sendsize;
2176 again:
2177 if (impl_ver == IMPL_XNTPD)
2178 sendsize = sizeof(struct conf_unpeer);
2179 else
2180 sendsize = v4sizeof(struct conf_unpeer);
2182 qitemlim = min(pcmd->nargs, COUNTOF(plist));
2183 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
2184 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
2185 pl->peeraddr = NSRCADR(&pcmd->argval[qitems].netnum);
2186 if (impl_ver == IMPL_XNTPD)
2187 pl->v6_flag = 0;
2188 } else {
2189 if (impl_ver == IMPL_XNTPD_OLD) {
2190 fprintf(stderr,
2191 "***Server doesn't understand IPv6 addresses\n");
2192 return;
2194 pl->peeraddr6 =
2195 SOCK_ADDR6(&pcmd->argval[qitems].netnum);
2196 pl->v6_flag = 1;
2198 pl = (struct conf_unpeer *)((char *)pl + sendsize);
2201 res = doquery(impl_ver, REQ_RESET_PEER, 1, qitems,
2202 sendsize, (char *)plist, &items,
2203 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
2205 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2206 impl_ver = IMPL_XNTPD_OLD;
2207 goto again;
2210 if (res == 0)
2211 (void) fprintf(fp, "done!\n");
2216 * readkeys - request the server to reread the keys file
2218 /*ARGSUSED*/
2219 static void
2220 readkeys(
2221 struct parse *pcmd,
2222 FILE *fp
2225 int items;
2226 int itemsize;
2227 char *dummy;
2228 int res;
2230 again:
2231 res = doquery(impl_ver, REQ_REREAD_KEYS, 1, 0, 0, (char *)0,
2232 &items, &itemsize, &dummy, 0, sizeof(dummy));
2234 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2235 impl_ver = IMPL_XNTPD_OLD;
2236 goto again;
2239 if (res == 0)
2240 (void) fprintf(fp, "done!\n");
2241 return;
2246 * trustkey - add some keys to the trusted key list
2248 static void
2249 trustkey(
2250 struct parse *pcmd,
2251 FILE *fp
2254 do_trustkey(pcmd, fp, REQ_TRUSTKEY);
2259 * untrustkey - remove some keys from the trusted key list
2261 static void
2262 untrustkey(
2263 struct parse *pcmd,
2264 FILE *fp
2267 do_trustkey(pcmd, fp, REQ_UNTRUSTKEY);
2272 * do_trustkey - do grunge work of adding/deleting keys
2274 static void
2275 do_trustkey(
2276 struct parse *pcmd,
2277 FILE *fp,
2278 int req
2281 u_long keyids[MAXARGS];
2282 int i;
2283 int items;
2284 int itemsize;
2285 char *dummy;
2286 int ritems;
2287 int res;
2289 ritems = 0;
2290 for (i = 0; i < pcmd->nargs; i++) {
2291 keyids[ritems++] = pcmd->argval[i].uval;
2294 again:
2295 res = doquery(impl_ver, req, 1, ritems, sizeof(u_long),
2296 (char *)keyids, &items, &itemsize, &dummy, 0,
2297 sizeof(dummy));
2299 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2300 impl_ver = IMPL_XNTPD_OLD;
2301 goto again;
2304 if (res == 0)
2305 (void) fprintf(fp, "done!\n");
2306 return;
2312 * authinfo - obtain and print info about authentication
2314 /*ARGSUSED*/
2315 static void
2316 authinfo(
2317 struct parse *pcmd,
2318 FILE *fp
2321 struct info_auth *ia;
2322 int items;
2323 int itemsize;
2324 int res;
2326 again:
2327 res = doquery(impl_ver, REQ_AUTHINFO, 0, 0, 0, (char *)NULL,
2328 &items, &itemsize, (void *)&ia, 0,
2329 sizeof(struct info_auth));
2331 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2332 impl_ver = IMPL_XNTPD_OLD;
2333 goto again;
2336 if (res != 0)
2337 return;
2339 if (!check1item(items, fp))
2340 return;
2342 if (!checkitemsize(itemsize, sizeof(struct info_auth)))
2343 return;
2345 (void) fprintf(fp, "time since reset: %ld\n",
2346 (u_long)ntohl(ia->timereset));
2347 (void) fprintf(fp, "stored keys: %ld\n",
2348 (u_long)ntohl(ia->numkeys));
2349 (void) fprintf(fp, "free keys: %ld\n",
2350 (u_long)ntohl(ia->numfreekeys));
2351 (void) fprintf(fp, "key lookups: %ld\n",
2352 (u_long)ntohl(ia->keylookups));
2353 (void) fprintf(fp, "keys not found: %ld\n",
2354 (u_long)ntohl(ia->keynotfound));
2355 (void) fprintf(fp, "uncached keys: %ld\n",
2356 (u_long)ntohl(ia->keyuncached));
2357 (void) fprintf(fp, "encryptions: %ld\n",
2358 (u_long)ntohl(ia->encryptions));
2359 (void) fprintf(fp, "decryptions: %ld\n",
2360 (u_long)ntohl(ia->decryptions));
2361 (void) fprintf(fp, "expired keys: %ld\n",
2362 (u_long)ntohl(ia->expired));
2368 * traps - obtain and print a list of traps
2370 /*ARGSUSED*/
2371 static void
2372 traps(
2373 struct parse *pcmd,
2374 FILE *fp
2377 int i;
2378 struct info_trap *it;
2379 sockaddr_u trap_addr, local_addr;
2380 int items;
2381 int itemsize;
2382 int res;
2384 again:
2385 res = doquery(impl_ver, REQ_TRAPS, 0, 0, 0, (char *)NULL,
2386 &items, &itemsize, (void *)&it, 0,
2387 sizeof(struct info_trap));
2389 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2390 impl_ver = IMPL_XNTPD_OLD;
2391 goto again;
2394 if (res != 0)
2395 return;
2397 if (!checkitems(items, fp))
2398 return;
2400 if (!checkitemsize(itemsize, sizeof(struct info_trap)) &&
2401 !checkitemsize(itemsize, v4sizeof(struct info_trap)))
2402 return;
2404 for (i = 0; i < items; i++ ) {
2405 if (i != 0)
2406 (void) fprintf(fp, "\n");
2407 SET_ADDRS(trap_addr, local_addr, it, trap_address, local_address);
2408 (void) fprintf(fp, "address %s, port %d\n",
2409 stoa(&trap_addr),
2410 ntohs(it->trap_port));
2411 (void) fprintf(fp, "interface: %s, ",
2412 (it->local_address == 0)
2413 ? "wildcard"
2414 : stoa(&local_addr));
2415 if (ntohl(it->flags) & TRAP_CONFIGURED)
2416 (void) fprintf(fp, "configured\n");
2417 else if (ntohl(it->flags) & TRAP_NONPRIO)
2418 (void) fprintf(fp, "low priority\n");
2419 else
2420 (void) fprintf(fp, "normal priority\n");
2422 (void) fprintf(fp, "set for %ld secs, last set %ld secs ago\n",
2423 (long)ntohl(it->origtime),
2424 (long)ntohl(it->settime));
2425 (void) fprintf(fp, "sequence %d, number of resets %ld\n",
2426 ntohs(it->sequence),
2427 (long)ntohl(it->resets));
2433 * addtrap - configure a trap
2435 static void
2436 addtrap(
2437 struct parse *pcmd,
2438 FILE *fp
2441 do_addclr_trap(pcmd, fp, REQ_ADD_TRAP);
2446 * clrtrap - clear a trap from the server
2448 static void
2449 clrtrap(
2450 struct parse *pcmd,
2451 FILE *fp
2454 do_addclr_trap(pcmd, fp, REQ_CLR_TRAP);
2459 * do_addclr_trap - do grunge work of adding/deleting traps
2461 static void
2462 do_addclr_trap(
2463 struct parse *pcmd,
2464 FILE *fp,
2465 int req
2468 struct conf_trap ctrap;
2469 int items;
2470 int itemsize;
2471 char *dummy;
2472 int res;
2473 int sendsize;
2475 again:
2476 if (impl_ver == IMPL_XNTPD)
2477 sendsize = sizeof(struct conf_trap);
2478 else
2479 sendsize = v4sizeof(struct conf_trap);
2481 if (IS_IPV4(&pcmd->argval[0].netnum)) {
2482 ctrap.trap_address = NSRCADR(&pcmd->argval[0].netnum);
2483 if (impl_ver == IMPL_XNTPD)
2484 ctrap.v6_flag = 0;
2485 } else {
2486 if (impl_ver == IMPL_XNTPD_OLD) {
2487 fprintf(stderr,
2488 "***Server doesn't understand IPv6 addresses\n");
2489 return;
2491 ctrap.trap_address6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
2492 ctrap.v6_flag = 1;
2494 ctrap.local_address = 0;
2495 ctrap.trap_port = htons(TRAPPORT);
2496 ctrap.unused = 0;
2498 if (pcmd->nargs > 1) {
2499 ctrap.trap_port = htons((u_short)pcmd->argval[1].uval);
2500 if (pcmd->nargs > 2) {
2501 if (AF(&pcmd->argval[2].netnum) !=
2502 AF(&pcmd->argval[0].netnum)) {
2503 fprintf(stderr,
2504 "***Cannot mix IPv4 and IPv6 addresses\n");
2505 return;
2507 if (IS_IPV4(&pcmd->argval[2].netnum))
2508 ctrap.local_address = NSRCADR(&pcmd->argval[2].netnum);
2509 else
2510 ctrap.local_address6 = SOCK_ADDR6(&pcmd->argval[2].netnum);
2514 res = doquery(impl_ver, req, 1, 1, sendsize,
2515 (char *)&ctrap, &items, &itemsize, &dummy, 0,
2516 sizeof(struct conf_trap));
2518 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2519 impl_ver = IMPL_XNTPD_OLD;
2520 goto again;
2523 if (res == 0)
2524 (void) fprintf(fp, "done!\n");
2525 return;
2531 * requestkey - change the server's request key (a dangerous request)
2533 static void
2534 requestkey(
2535 struct parse *pcmd,
2536 FILE *fp
2539 do_changekey(pcmd, fp, REQ_REQUEST_KEY);
2544 * controlkey - change the server's control key
2546 static void
2547 controlkey(
2548 struct parse *pcmd,
2549 FILE *fp
2552 do_changekey(pcmd, fp, REQ_CONTROL_KEY);
2558 * do_changekey - do grunge work of changing keys
2560 static void
2561 do_changekey(
2562 struct parse *pcmd,
2563 FILE *fp,
2564 int req
2567 u_long key;
2568 int items;
2569 int itemsize;
2570 char *dummy;
2571 int res;
2574 key = htonl((u_int32)pcmd->argval[0].uval);
2576 again:
2577 res = doquery(impl_ver, req, 1, 1, sizeof(u_int32),
2578 (char *)&key, &items, &itemsize, &dummy, 0,
2579 sizeof(dummy));
2581 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2582 impl_ver = IMPL_XNTPD_OLD;
2583 goto again;
2586 if (res == 0)
2587 (void) fprintf(fp, "done!\n");
2588 return;
2594 * ctlstats - obtain and print info about authentication
2596 /*ARGSUSED*/
2597 static void
2598 ctlstats(
2599 struct parse *pcmd,
2600 FILE *fp
2603 struct info_control *ic;
2604 int items;
2605 int itemsize;
2606 int res;
2608 again:
2609 res = doquery(impl_ver, REQ_GET_CTLSTATS, 0, 0, 0, (char *)NULL,
2610 &items, &itemsize, (void *)&ic, 0,
2611 sizeof(struct info_control));
2613 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2614 impl_ver = IMPL_XNTPD_OLD;
2615 goto again;
2618 if (res != 0)
2619 return;
2621 if (!check1item(items, fp))
2622 return;
2624 if (!checkitemsize(itemsize, sizeof(struct info_control)))
2625 return;
2627 (void) fprintf(fp, "time since reset: %ld\n",
2628 (u_long)ntohl(ic->ctltimereset));
2629 (void) fprintf(fp, "requests received: %ld\n",
2630 (u_long)ntohl(ic->numctlreq));
2631 (void) fprintf(fp, "responses sent: %ld\n",
2632 (u_long)ntohl(ic->numctlresponses));
2633 (void) fprintf(fp, "fragments sent: %ld\n",
2634 (u_long)ntohl(ic->numctlfrags));
2635 (void) fprintf(fp, "async messages sent: %ld\n",
2636 (u_long)ntohl(ic->numasyncmsgs));
2637 (void) fprintf(fp, "error msgs sent: %ld\n",
2638 (u_long)ntohl(ic->numctlerrors));
2639 (void) fprintf(fp, "total bad pkts: %ld\n",
2640 (u_long)ntohl(ic->numctlbadpkts));
2641 (void) fprintf(fp, "packet too short: %ld\n",
2642 (u_long)ntohl(ic->numctltooshort));
2643 (void) fprintf(fp, "response on input: %ld\n",
2644 (u_long)ntohl(ic->numctlinputresp));
2645 (void) fprintf(fp, "fragment on input: %ld\n",
2646 (u_long)ntohl(ic->numctlinputfrag));
2647 (void) fprintf(fp, "error set on input: %ld\n",
2648 (u_long)ntohl(ic->numctlinputerr));
2649 (void) fprintf(fp, "bad offset on input: %ld\n",
2650 (u_long)ntohl(ic->numctlbadoffset));
2651 (void) fprintf(fp, "bad version packets: %ld\n",
2652 (u_long)ntohl(ic->numctlbadversion));
2653 (void) fprintf(fp, "data in pkt too short: %ld\n",
2654 (u_long)ntohl(ic->numctldatatooshort));
2655 (void) fprintf(fp, "unknown op codes: %ld\n",
2656 (u_long)ntohl(ic->numctlbadop));
2661 * clockstat - get and print clock status information
2663 static void
2664 clockstat(
2665 struct parse *pcmd,
2666 FILE *fp
2669 struct info_clock *cl;
2670 /* 8 is the maximum number of clocks which will fit in a packet */
2671 u_long clist[min(MAXARGS, 8)];
2672 int qitemlim;
2673 int qitems;
2674 int items;
2675 int itemsize;
2676 int res;
2677 l_fp ts;
2678 struct clktype *clk;
2680 qitemlim = min(pcmd->nargs, COUNTOF(clist));
2681 for (qitems = 0; qitems < qitemlim; qitems++)
2682 clist[qitems] = NSRCADR(&pcmd->argval[qitems].netnum);
2684 again:
2685 res = doquery(impl_ver, REQ_GET_CLOCKINFO, 0, qitems,
2686 sizeof(u_int32), (char *)clist, &items,
2687 &itemsize, (void *)&cl, 0, sizeof(struct info_clock));
2689 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2690 impl_ver = IMPL_XNTPD_OLD;
2691 goto again;
2694 if (res != 0)
2695 return;
2697 if (!checkitems(items, fp))
2698 return;
2700 if (!checkitemsize(itemsize, sizeof(struct info_clock)))
2701 return;
2703 while (items-- > 0) {
2704 (void) fprintf(fp, "clock address: %s\n",
2705 numtoa(cl->clockadr));
2706 for (clk = clktypes; clk->code >= 0; clk++)
2707 if (clk->code == cl->type)
2708 break;
2709 if (clk->code >= 0)
2710 (void) fprintf(fp, "clock type: %s\n",
2711 clk->clocktype);
2712 else
2713 (void) fprintf(fp, "clock type: unknown type (%d)\n",
2714 cl->type);
2715 (void) fprintf(fp, "last event: %d\n",
2716 cl->lastevent);
2717 (void) fprintf(fp, "current status: %d\n",
2718 cl->currentstatus);
2719 (void) fprintf(fp, "number of polls: %lu\n",
2720 (u_long)ntohl(cl->polls));
2721 (void) fprintf(fp, "no response to poll: %lu\n",
2722 (u_long)ntohl(cl->noresponse));
2723 (void) fprintf(fp, "bad format responses: %lu\n",
2724 (u_long)ntohl(cl->badformat));
2725 (void) fprintf(fp, "bad data responses: %lu\n",
2726 (u_long)ntohl(cl->baddata));
2727 (void) fprintf(fp, "running time: %lu\n",
2728 (u_long)ntohl(cl->timestarted));
2729 NTOHL_FP(&cl->fudgetime1, &ts);
2730 (void) fprintf(fp, "fudge time 1: %s\n",
2731 lfptoa(&ts, 6));
2732 NTOHL_FP(&cl->fudgetime2, &ts);
2733 (void) fprintf(fp, "fudge time 2: %s\n",
2734 lfptoa(&ts, 6));
2735 (void) fprintf(fp, "stratum: %ld\n",
2736 (u_long)ntohl(cl->fudgeval1));
2737 (void) fprintf(fp, "reference ID: %s\n",
2738 refid_string(ntohl(cl->fudgeval2), 0));
2739 (void) fprintf(fp, "fudge flags: 0x%x\n",
2740 cl->flags);
2742 if (items > 0)
2743 (void) fprintf(fp, "\n");
2744 cl++;
2750 * fudge - set clock fudge factors
2752 static void
2753 fudge(
2754 struct parse *pcmd,
2755 FILE *fp
2758 struct conf_fudge fudgedata;
2759 int items;
2760 int itemsize;
2761 char *dummy;
2762 l_fp ts;
2763 int res;
2764 long val;
2765 u_long u_val;
2766 int err;
2769 err = 0;
2770 memset((char *)&fudgedata, 0, sizeof fudgedata);
2771 fudgedata.clockadr = NSRCADR(&pcmd->argval[0].netnum);
2773 if (STREQ(pcmd->argval[1].string, "time1")) {
2774 fudgedata.which = htonl(FUDGE_TIME1);
2775 if (!atolfp(pcmd->argval[2].string, &ts))
2776 err = 1;
2777 else
2778 NTOHL_FP(&ts, &fudgedata.fudgetime);
2779 } else if (STREQ(pcmd->argval[1].string, "time2")) {
2780 fudgedata.which = htonl(FUDGE_TIME2);
2781 if (!atolfp(pcmd->argval[2].string, &ts))
2782 err = 1;
2783 else
2784 NTOHL_FP(&ts, &fudgedata.fudgetime);
2785 } else if (STREQ(pcmd->argval[1].string, "val1")) {
2786 fudgedata.which = htonl(FUDGE_VAL1);
2787 if (!atoint(pcmd->argval[2].string, &val))
2788 err = 1;
2789 else
2790 fudgedata.fudgeval_flags = htonl(val);
2791 } else if (STREQ(pcmd->argval[1].string, "val2")) {
2792 fudgedata.which = htonl(FUDGE_VAL2);
2793 if (!atoint(pcmd->argval[2].string, &val))
2794 err = 1;
2795 else
2796 fudgedata.fudgeval_flags = htonl((u_int32)val);
2797 } else if (STREQ(pcmd->argval[1].string, "flags")) {
2798 fudgedata.which = htonl(FUDGE_FLAGS);
2799 if (!hextoint(pcmd->argval[2].string, &u_val))
2800 err = 1;
2801 else
2802 fudgedata.fudgeval_flags = htonl((u_int32)(u_val & 0xf));
2803 } else {
2804 (void) fprintf(stderr, "What fudge is %s?\n",
2805 pcmd->argval[1].string);
2806 return;
2809 if (err) {
2810 (void) fprintf(stderr, "Unknown fudge parameter %s\n",
2811 pcmd->argval[2].string);
2812 return;
2815 again:
2816 res = doquery(impl_ver, REQ_SET_CLKFUDGE, 1, 1,
2817 sizeof(struct conf_fudge), (char *)&fudgedata, &items,
2818 &itemsize, &dummy, 0, sizeof(dummy));
2820 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2821 impl_ver = IMPL_XNTPD_OLD;
2822 goto again;
2825 if (res == 0)
2826 (void) fprintf(fp, "done!\n");
2827 return;
2831 * clkbug - get and print clock debugging information
2833 static void
2834 clkbug(
2835 struct parse *pcmd,
2836 FILE *fp
2839 register int i;
2840 register int n;
2841 register u_int32 s;
2842 struct info_clkbug *cl;
2843 /* 8 is the maximum number of clocks which will fit in a packet */
2844 u_long clist[min(MAXARGS, 8)];
2845 u_int32 ltemp;
2846 int qitemlim;
2847 int qitems;
2848 int items;
2849 int itemsize;
2850 int res;
2851 int needsp;
2852 l_fp ts;
2854 qitemlim = min(pcmd->nargs, COUNTOF(clist));
2855 for (qitems = 0; qitems < qitemlim; qitems++)
2856 clist[qitems] = NSRCADR(&pcmd->argval[qitems].netnum);
2858 again:
2859 res = doquery(impl_ver, REQ_GET_CLKBUGINFO, 0, qitems,
2860 sizeof(u_int32), (char *)clist, &items,
2861 &itemsize, (void *)&cl, 0, sizeof(struct info_clkbug));
2863 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2864 impl_ver = IMPL_XNTPD_OLD;
2865 goto again;
2868 if (res != 0)
2869 return;
2871 if (!checkitems(items, fp))
2872 return;
2874 if (!checkitemsize(itemsize, sizeof(struct info_clkbug)))
2875 return;
2877 while (items-- > 0) {
2878 (void) fprintf(fp, "clock address: %s\n",
2879 numtoa(cl->clockadr));
2880 n = (int)cl->nvalues;
2881 (void) fprintf(fp, "values: %d", n);
2882 s = ntohs(cl->svalues);
2883 if (n > NUMCBUGVALUES)
2884 n = NUMCBUGVALUES;
2885 for (i = 0; i < n; i++) {
2886 ltemp = ntohl(cl->values[i]);
2887 ltemp &= 0xffffffff; /* HMS: This does nothing now */
2888 if ((i & 0x3) == 0)
2889 (void) fprintf(fp, "\n");
2890 if (s & (1 << i))
2891 (void) fprintf(fp, "%12ld", (u_long)ltemp);
2892 else
2893 (void) fprintf(fp, "%12lu", (u_long)ltemp);
2895 (void) fprintf(fp, "\n");
2897 n = (int)cl->ntimes;
2898 (void) fprintf(fp, "times: %d", n);
2899 s = ntohl(cl->stimes);
2900 if (n > NUMCBUGTIMES)
2901 n = NUMCBUGTIMES;
2902 needsp = 0;
2903 for (i = 0; i < n; i++) {
2904 if ((i & 0x1) == 0) {
2905 (void) fprintf(fp, "\n");
2906 } else {
2907 for (;needsp > 0; needsp--)
2908 putc(' ', fp);
2910 NTOHL_FP(&cl->times[i], &ts);
2911 if (s & (1 << i)) {
2912 (void) fprintf(fp, "%17s",
2913 lfptoa(&ts, 6));
2914 needsp = 22;
2915 } else {
2916 (void) fprintf(fp, "%37s",
2917 uglydate(&ts));
2918 needsp = 2;
2921 (void) fprintf(fp, "\n");
2922 if (items > 0) {
2923 cl++;
2924 (void) fprintf(fp, "\n");
2931 * kerninfo - display the kernel pll/pps variables
2933 static void
2934 kerninfo(
2935 struct parse *pcmd,
2936 FILE *fp
2939 struct info_kernel *ik;
2940 int items;
2941 int itemsize;
2942 int res;
2943 unsigned status;
2944 double tscale = 1e-6;
2946 again:
2947 res = doquery(impl_ver, REQ_GET_KERNEL, 0, 0, 0, (char *)NULL,
2948 &items, &itemsize, (void *)&ik, 0,
2949 sizeof(struct info_kernel));
2951 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2952 impl_ver = IMPL_XNTPD_OLD;
2953 goto again;
2956 if (res != 0)
2957 return;
2958 if (!check1item(items, fp))
2959 return;
2960 if (!checkitemsize(itemsize, sizeof(struct info_kernel)))
2961 return;
2963 status = ntohs(ik->status) & 0xffff;
2965 * pll variables. We know more than we should about the NANO bit.
2967 #ifdef STA_NANO
2968 if (status & STA_NANO)
2969 tscale = 1e-9;
2970 #endif
2971 (void)fprintf(fp, "pll offset: %g s\n",
2972 (int32)ntohl(ik->offset) * tscale);
2973 (void)fprintf(fp, "pll frequency: %s ppm\n",
2974 fptoa((s_fp)ntohl(ik->freq), 3));
2975 (void)fprintf(fp, "maximum error: %g s\n",
2976 (u_long)ntohl(ik->maxerror) * 1e-6);
2977 (void)fprintf(fp, "estimated error: %g s\n",
2978 (u_long)ntohl(ik->esterror) * 1e-6);
2979 (void)fprintf(fp, "status: %04x ", status);
2980 #ifdef STA_PLL
2981 if (status & STA_PLL) (void)fprintf(fp, " pll");
2982 #endif
2983 #ifdef STA_PPSFREQ
2984 if (status & STA_PPSFREQ) (void)fprintf(fp, " ppsfreq");
2985 #endif
2986 #ifdef STA_PPSTIME
2987 if (status & STA_PPSTIME) (void)fprintf(fp, " ppstime");
2988 #endif
2989 #ifdef STA_FLL
2990 if (status & STA_FLL) (void)fprintf(fp, " fll");
2991 #endif
2992 #ifdef STA_INS
2993 if (status & STA_INS) (void)fprintf(fp, " ins");
2994 #endif
2995 #ifdef STA_DEL
2996 if (status & STA_DEL) (void)fprintf(fp, " del");
2997 #endif
2998 #ifdef STA_UNSYNC
2999 if (status & STA_UNSYNC) (void)fprintf(fp, " unsync");
3000 #endif
3001 #ifdef STA_FREQHOLD
3002 if (status & STA_FREQHOLD) (void)fprintf(fp, " freqhold");
3003 #endif
3004 #ifdef STA_PPSSIGNAL
3005 if (status & STA_PPSSIGNAL) (void)fprintf(fp, " ppssignal");
3006 #endif
3007 #ifdef STA_PPSJITTER
3008 if (status & STA_PPSJITTER) (void)fprintf(fp, " ppsjitter");
3009 #endif
3010 #ifdef STA_PPSWANDER
3011 if (status & STA_PPSWANDER) (void)fprintf(fp, " ppswander");
3012 #endif
3013 #ifdef STA_PPSERROR
3014 if (status & STA_PPSERROR) (void)fprintf(fp, " ppserror");
3015 #endif
3016 #ifdef STA_CLOCKERR
3017 if (status & STA_CLOCKERR) (void)fprintf(fp, " clockerr");
3018 #endif
3019 #ifdef STA_NANO
3020 if (status & STA_NANO) (void)fprintf(fp, " nano");
3021 #endif
3022 #ifdef STA_MODE
3023 if (status & STA_MODE) (void)fprintf(fp, " mode=fll");
3024 #endif
3025 #ifdef STA_CLK
3026 if (status & STA_CLK) (void)fprintf(fp, " src=B");
3027 #endif
3028 (void)fprintf(fp, "\n");
3029 (void)fprintf(fp, "pll time constant: %ld\n",
3030 (u_long)ntohl(ik->constant));
3031 (void)fprintf(fp, "precision: %g s\n",
3032 (u_long)ntohl(ik->precision) * tscale);
3033 (void)fprintf(fp, "frequency tolerance: %s ppm\n",
3034 fptoa((s_fp)ntohl(ik->tolerance), 0));
3037 * For backwards compatibility (ugh), we find the pps variables
3038 * only if the shift member is nonzero.
3040 if (!ik->shift)
3041 return;
3044 * pps variables
3046 (void)fprintf(fp, "pps frequency: %s ppm\n",
3047 fptoa((s_fp)ntohl(ik->ppsfreq), 3));
3048 (void)fprintf(fp, "pps stability: %s ppm\n",
3049 fptoa((s_fp)ntohl(ik->stabil), 3));
3050 (void)fprintf(fp, "pps jitter: %g s\n",
3051 (u_long)ntohl(ik->jitter) * tscale);
3052 (void)fprintf(fp, "calibration interval: %d s\n",
3053 1 << ntohs(ik->shift));
3054 (void)fprintf(fp, "calibration cycles: %ld\n",
3055 (u_long)ntohl(ik->calcnt));
3056 (void)fprintf(fp, "jitter exceeded: %ld\n",
3057 (u_long)ntohl(ik->jitcnt));
3058 (void)fprintf(fp, "stability exceeded: %ld\n",
3059 (u_long)ntohl(ik->stbcnt));
3060 (void)fprintf(fp, "calibration errors: %ld\n",
3061 (u_long)ntohl(ik->errcnt));
3064 #define IF_LIST_FMT "%2d %c %48s %c %c %12.12s %03x %3d %2d %5d %5d %5d %2d %3d %7d\n"
3065 #define IF_LIST_FMT_STR "%2s %c %48s %c %c %12.12s %3s %3s %2s %5s %5s %5s %2s %3s %7s\n"
3066 #define IF_LIST_AFMT_STR " %48s %c\n"
3067 #define IF_LIST_LABELS "#", 'A', "Address/Mask/Broadcast", 'T', 'E', "IF name", "Flg", "TL", "#M", "recv", "sent", "drop", "S", "PC", "uptime"
3068 #define IF_LIST_LINE "==================================================================================================================\n"
3070 static void
3071 iflist(
3072 FILE *fp,
3073 struct info_if_stats *ifs,
3074 int items,
3075 int itemsize,
3076 int res
3079 static char *actions = "?.+-";
3080 sockaddr_u saddr;
3082 if (res != 0)
3083 return;
3085 if (!checkitems(items, fp))
3086 return;
3088 if (!checkitemsize(itemsize, sizeof(struct info_if_stats)))
3089 return;
3091 fprintf(fp, IF_LIST_FMT_STR, IF_LIST_LABELS);
3092 fprintf(fp, IF_LIST_LINE);
3094 while (items > 0) {
3095 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3096 ifs->unaddr.addr.s_addr, ifs->unaddr.addr6);
3097 fprintf(fp, IF_LIST_FMT,
3098 ntohl(ifs->ifnum),
3099 actions[(ifs->action >= 1 && ifs->action < 4) ? ifs->action : 0],
3100 stoa((&saddr)), 'A',
3101 ifs->ignore_packets ? 'D' : 'E',
3102 ifs->name,
3103 ntohl(ifs->flags),
3104 ntohl(ifs->last_ttl),
3105 ntohl(ifs->num_mcast),
3106 ntohl(ifs->received),
3107 ntohl(ifs->sent),
3108 ntohl(ifs->notsent),
3109 ntohl(ifs->scopeid),
3110 ntohl(ifs->peercnt),
3111 ntohl(ifs->uptime));
3113 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3114 ifs->unmask.addr.s_addr, ifs->unmask.addr6);
3115 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'M');
3117 if (!ntohl(ifs->v6_flag) && ntohl(ifs->flags) & (INT_BCASTOPEN)) {
3118 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3119 ifs->unbcast.addr.s_addr, ifs->unbcast.addr6);
3120 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'B');
3124 ifs++;
3125 items--;
3129 /*ARGSUSED*/
3130 static void
3131 get_if_stats(
3132 struct parse *pcmd,
3133 FILE *fp
3136 struct info_if_stats *ifs;
3137 int items;
3138 int itemsize;
3139 int res;
3141 res = doquery(impl_ver, REQ_IF_STATS, 1, 0, 0, (char *)NULL, &items,
3142 &itemsize, (void *)&ifs, 0,
3143 sizeof(struct info_if_stats));
3144 iflist(fp, ifs, items, itemsize, res);
3147 /*ARGSUSED*/
3148 static void
3149 do_if_reload(
3150 struct parse *pcmd,
3151 FILE *fp
3154 struct info_if_stats *ifs;
3155 int items;
3156 int itemsize;
3157 int res;
3159 res = doquery(impl_ver, REQ_IF_RELOAD, 1, 0, 0, (char *)NULL, &items,
3160 &itemsize, (void *)&ifs, 0,
3161 sizeof(struct info_if_stats));
3162 iflist(fp, ifs, items, itemsize, res);