Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / ntpdc / ntpdc_ops.c
blobc7da513f68176348928358c10691b653cac10379
1 /* $NetBSD: ntpdc_ops.c,v 1.7 2007/01/06 19:45:23 kardel Exp $ */
3 /*
4 * ntpdc_ops.c - subroutines which are called to perform operations by xntpdc
5 */
7 #ifdef HAVE_CONFIG_H
8 # include <config.h>
9 #endif
11 #include <stdio.h>
12 #include <stddef.h>
14 #include "ntpdc.h"
15 #include "ntp_control.h"
16 #include "ntp_refclock.h"
17 #include "ntp_stdlib.h"
19 #include <ctype.h>
20 #ifdef HAVE_SYS_TIMEX_H
21 # include <sys/timex.h>
22 #endif
23 #if !defined(__bsdi__) && !defined(apollo)
24 #include <netinet/in.h>
25 #endif
27 #include <arpa/inet.h>
30 * Declarations for command handlers in here
32 static int checkitems P((int, FILE *));
33 static int checkitemsize P((int, int));
34 static int check1item P((int, FILE *));
35 static void peerlist P((struct parse *, FILE *));
36 static void peers P((struct parse *, FILE *));
37 static void doconfig P((struct parse *pcmd, FILE *fp, int mode, int refc));
38 static void dmpeers P((struct parse *, FILE *));
39 static void dopeers P((struct parse *, FILE *, int));
40 static void printpeer P((struct info_peer *, FILE *));
41 static void showpeer P((struct parse *, FILE *));
42 static void peerstats P((struct parse *, FILE *));
43 static void loopinfo P((struct parse *, FILE *));
44 static void sysinfo P((struct parse *, FILE *));
45 static void sysstats P((struct parse *, FILE *));
46 static void iostats P((struct parse *, FILE *));
47 static void memstats P((struct parse *, FILE *));
48 static void timerstats P((struct parse *, FILE *));
49 static void addpeer P((struct parse *, FILE *));
50 static void addserver P((struct parse *, FILE *));
51 static void addrefclock P((struct parse *, FILE *));
52 static void broadcast P((struct parse *, FILE *));
53 static void doconfig P((struct parse *, FILE *, int, int));
54 static void unconfig P((struct parse *, FILE *));
55 static void set P((struct parse *, FILE *));
56 static void sys_clear P((struct parse *, FILE *));
57 static void doset P((struct parse *, FILE *, int));
58 static void reslist P((struct parse *, FILE *));
59 static void new_restrict P((struct parse *, FILE *));
60 static void unrestrict P((struct parse *, FILE *));
61 static void delrestrict P((struct parse *, FILE *));
62 static void do_restrict P((struct parse *, FILE *, int));
63 static void monlist P((struct parse *, FILE *));
64 static void reset P((struct parse *, FILE *));
65 static void preset P((struct parse *, FILE *));
66 static void readkeys P((struct parse *, FILE *));
67 static void trustkey P((struct parse *, FILE *));
68 static void untrustkey P((struct parse *, FILE *));
69 static void do_trustkey P((struct parse *, FILE *, int));
70 static void authinfo P((struct parse *, FILE *));
71 static void traps P((struct parse *, FILE *));
72 static void addtrap P((struct parse *, FILE *));
73 static void clrtrap P((struct parse *, FILE *));
74 static void do_addclr_trap P((struct parse *, FILE *, int));
75 static void requestkey P((struct parse *, FILE *));
76 static void controlkey P((struct parse *, FILE *));
77 static void do_changekey P((struct parse *, FILE *, int));
78 static void ctlstats P((struct parse *, FILE *));
79 static void clockstat P((struct parse *, FILE *));
80 static void fudge P((struct parse *, FILE *));
81 static void clkbug P((struct parse *, FILE *));
82 static void kerninfo P((struct parse *, FILE *));
83 static void get_if_stats P((struct parse *, FILE *));
84 static void do_if_reload P((struct parse *, FILE *));
87 * Commands we understand. Ntpdc imports this.
89 struct xcmd opcmds[] = {
90 { "listpeers", peerlist, { OPT|IP_VERSION, NO, NO, NO },
91 { "-4|-6", "", "", "" },
92 "display list of peers the server knows about [IP Version]" },
93 { "peers", peers, { OPT|IP_VERSION, NO, NO, NO },
94 { "-4|-6", "", "", "" },
95 "display peer summary information [IP Version]" },
96 { "dmpeers", dmpeers, { OPT|IP_VERSION, NO, NO, NO },
97 { "-4|-6", "", "", "" },
98 "display peer summary info the way Dave Mills likes it (IP Version)" },
99 { "showpeer", showpeer, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD},
100 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
101 "display detailed information for one or more peers" },
102 { "pstats", peerstats, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
103 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
104 "display statistical information for one or more peers" },
105 { "loopinfo", loopinfo, { OPT|NTP_STR, NO, NO, NO },
106 { "oneline|multiline", "", "", "" },
107 "display loop filter information" },
108 { "sysinfo", sysinfo, { NO, NO, NO, NO },
109 { "", "", "", "" },
110 "display local server information" },
111 { "sysstats", sysstats, { NO, NO, NO, NO },
112 { "", "", "", "" },
113 "display local server statistics" },
114 { "memstats", memstats, { NO, NO, NO, NO },
115 { "", "", "", "" },
116 "display peer memory usage statistics" },
117 { "iostats", iostats, { NO, NO, NO, NO },
118 { "", "", "", "" },
119 "display I/O subsystem statistics" },
120 { "timerstats", timerstats, { NO, NO, NO, NO },
121 { "", "", "", "" },
122 "display event timer subsystem statistics" },
123 { "addpeer", addpeer, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
124 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
125 "configure a new peer association" },
126 { "addserver", addserver, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
127 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
128 "configure a new server" },
129 { "addrefclock",addrefclock, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_STR, OPT|NTP_STR },
130 { "addr", "mode", "minpoll|prefer", "minpoll|prefer" },
131 "configure a new server" },
132 { "broadcast", broadcast, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
133 { "addr", "keyid", "version", "minpoll" },
134 "configure broadcasting time service" },
135 { "unconfig", unconfig, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
136 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
137 "unconfigure existing peer assocations" },
138 { "enable", set, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
139 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
140 "set a system flag (auth, bclient, monitor, pll, kernel, stats)" },
141 { "disable", sys_clear, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
142 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
143 "clear a system flag (auth, bclient, monitor, pll, kernel, stats)" },
144 { "reslist", reslist, {OPT|IP_VERSION, NO, NO, NO },
145 { "-4|-6", "", "", "" },
146 "display the server's restrict list" },
147 { "restrict", new_restrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
148 { "address", "mask",
149 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
150 "..." },
151 "create restrict entry/add flags to entry" },
152 { "unrestrict", unrestrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
153 { "address", "mask",
154 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
155 "..." },
156 "remove flags from a restrict entry" },
157 { "delrestrict", delrestrict, { NTP_ADD, NTP_ADD, OPT|NTP_STR, NO },
158 { "address", "mask", "ntpport", "" },
159 "delete a restrict entry" },
160 { "monlist", monlist, { OPT|NTP_INT, NO, NO, NO },
161 { "version", "", "", "" },
162 "display data the server's monitor routines have collected" },
163 { "reset", reset, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
164 { "io|sys|mem|timer|auth|allpeers", "...", "...", "..." },
165 "reset various subsystem statistics counters" },
166 { "preset", preset, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
167 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
168 "reset stat counters associated with particular peer(s)" },
169 { "readkeys", readkeys, { NO, NO, NO, NO },
170 { "", "", "", "" },
171 "request a reread of the keys file and re-init of system keys" },
172 { "trustedkey", trustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
173 { "keyid", "keyid", "keyid", "keyid" },
174 "add one or more key ID's to the trusted list" },
175 { "untrustedkey", untrustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
176 { "keyid", "keyid", "keyid", "keyid" },
177 "remove one or more key ID's from the trusted list" },
178 { "authinfo", authinfo, { NO, NO, NO, NO },
179 { "", "", "", "" },
180 "display the state of the authentication code" },
181 { "traps", traps, { NO, NO, NO, NO },
182 { "", "", "", "" },
183 "display the traps set in the server" },
184 { "addtrap", addtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
185 { "address", "port", "interface", "" },
186 "configure a trap in the server" },
187 { "clrtrap", clrtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
188 { "address", "port", "interface", "" },
189 "remove a trap (configured or otherwise) from the server" },
190 { "requestkey", requestkey, { NTP_UINT, NO, NO, NO },
191 { "keyid", "", "", "" },
192 "change the keyid the server uses to authenticate requests" },
193 { "controlkey", controlkey, { NTP_UINT, NO, NO, NO },
194 { "keyid", "", "", "" },
195 "change the keyid the server uses to authenticate control messages" },
196 { "ctlstats", ctlstats, { NO, NO, NO, NO },
197 { "", "", "", "" },
198 "display packet count statistics from the control module" },
199 { "clockstat", clockstat, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
200 { "address", "address", "address", "address" },
201 "display clock status information" },
202 { "fudge", fudge, { NTP_ADD, NTP_STR, NTP_STR, NO },
203 { "address", "time1|time2|val1|val2|flags", "value", "" },
204 "set/change one of a clock's fudge factors" },
205 { "clkbug", clkbug, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
206 { "address", "address", "address", "address" },
207 "display clock debugging information" },
208 { "kerninfo", kerninfo, { NO, NO, NO, NO },
209 { "", "", "", "" },
210 "display the kernel pll/pps variables" },
211 { "ifstats", get_if_stats, { NO, NO, NO, NO },
212 { "", "", "", "" },
213 "list interface statistics" },
214 { "ifreload", do_if_reload, { NO, NO, NO, NO },
215 { "", "", "", "" },
216 "reload interface configuration" },
217 { 0, 0, { NO, NO, NO, NO },
218 { "", "", "", "" }, "" }
222 * For quick string comparisons
224 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
228 * checkitems - utility to print a message if no items were returned
230 static int
231 checkitems(
232 int items,
233 FILE *fp
236 if (items == 0) {
237 (void) fprintf(fp, "No data returned in response to query\n");
238 return 0;
240 return 1;
245 * checkitemsize - utility to print a message if the item size is wrong
247 static int
248 checkitemsize(
249 int itemsize,
250 int expected
253 if (itemsize != expected) {
254 (void) fprintf(stderr,
255 "***Incorrect item size returned by remote host (%d should be %d)\n",
256 itemsize, expected);
257 return 0;
259 return 1;
264 * check1item - check to make sure we have exactly one item
266 static int
267 check1item(
268 int items,
269 FILE *fp
272 if (items == 0) {
273 (void) fprintf(fp, "No data returned in response to query\n");
274 return 0;
276 if (items > 1) {
277 (void) fprintf(fp, "Expected one item in response, got %d\n",
278 items);
279 return 0;
281 return 1;
287 * peerlist - get a short list of peers
289 /*ARGSUSED*/
290 static void
291 peerlist(
292 struct parse *pcmd,
293 FILE *fp
296 struct info_peer_list *plist;
297 struct sockaddr_storage paddr;
298 int items;
299 int itemsize;
300 int res;
302 again:
303 res = doquery(impl_ver, REQ_PEER_LIST, 0, 0, 0, (char *)NULL, &items,
304 &itemsize, (void *)&plist, 0,
305 sizeof(struct info_peer_list));
307 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
308 impl_ver = IMPL_XNTPD_OLD;
309 goto again;
312 if (res != 0)
313 return;
315 if (!checkitems(items, fp))
316 return;
318 if (!checkitemsize(itemsize, sizeof(struct info_peer_list)) &&
319 !checkitemsize(itemsize, v4sizeof(struct info_peer_list)))
320 return;
322 while (items > 0) {
323 memset((char *)&paddr, 0, sizeof(paddr));
324 if (plist->v6_flag != 0) {
325 GET_INADDR6(paddr) = plist->addr6;
326 paddr.ss_family = AF_INET6;
327 } else {
328 GET_INADDR(paddr) = plist->addr;
329 paddr.ss_family = AF_INET;
331 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
332 paddr.ss_len = SOCKLEN(&paddr);
333 #endif
334 if ((pcmd->nargs == 0) ||
335 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
336 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
337 (void) fprintf(fp, "%-9s %s\n",
338 modetoa(plist->hmode),
339 nntohost(&paddr));
340 plist++;
341 items--;
347 * peers - show peer summary
349 static void
350 peers(
351 struct parse *pcmd,
352 FILE *fp
355 dopeers(pcmd, fp, 0);
359 * dmpeers - show peer summary, Dave Mills style
361 static void
362 dmpeers(
363 struct parse *pcmd,
364 FILE *fp
367 dopeers(pcmd, fp, 1);
372 * peers - show peer summary
374 /*ARGSUSED*/
375 static void
376 dopeers(
377 struct parse *pcmd,
378 FILE *fp,
379 int dmstyle
382 struct info_peer_summary *plist;
383 struct sockaddr_storage dstadr;
384 struct sockaddr_storage srcadr;
385 int items;
386 int itemsize;
387 int ntp_poll;
388 int res;
389 int c;
390 l_fp tempts;
392 again:
393 res = doquery(impl_ver, REQ_PEER_LIST_SUM, 0, 0, 0, (char *)NULL,
394 &items, &itemsize, (void *)&plist, 0,
395 sizeof(struct info_peer_summary));
397 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
398 impl_ver = IMPL_XNTPD_OLD;
399 goto again;
402 if (res != 0)
403 return;
405 if (!checkitems(items, fp))
406 return;
408 if (!checkitemsize(itemsize, sizeof(struct info_peer_summary)) &&
409 !checkitemsize(itemsize, v4sizeof(struct info_peer_summary)))
410 return;
412 (void) fprintf(fp,
413 " remote local st poll reach delay offset disp\n");
414 (void) fprintf(fp,
415 "=======================================================================\n");
416 while (items > 0) {
417 if (!dmstyle) {
418 if (plist->flags & INFO_FLAG_SYSPEER)
419 c = '*';
420 else if (plist->hmode == MODE_ACTIVE)
421 c = '+';
422 else if (plist->hmode == MODE_PASSIVE)
423 c = '-';
424 else if (plist->hmode == MODE_CLIENT)
425 c = '=';
426 else if (plist->hmode == MODE_BROADCAST)
427 c = '^';
428 else if (plist->hmode == MODE_BCLIENT)
429 c = '~';
430 else
431 c = ' ';
432 } else {
433 if (plist->flags & INFO_FLAG_SYSPEER)
434 c = '*';
435 else if (plist->flags & INFO_FLAG_SHORTLIST)
436 c = '+';
437 else if (plist->flags & INFO_FLAG_SEL_CANDIDATE)
438 c = '.';
439 else
440 c = ' ';
442 NTOHL_FP(&(plist->offset), &tempts);
443 ntp_poll = 1<<max(min3(plist->ppoll, plist->hpoll, NTP_MAXPOLL),
444 NTP_MINPOLL);
445 memset((char *)&dstadr, 0, sizeof(dstadr));
446 memset((char *)&srcadr, 0, sizeof(srcadr));
447 if (plist->v6_flag != 0) {
448 GET_INADDR6(dstadr) = plist->dstadr6;
449 GET_INADDR6(srcadr) = plist->srcadr6;
450 srcadr.ss_family = AF_INET6;
451 dstadr.ss_family = AF_INET6;
452 } else {
453 GET_INADDR(dstadr) = plist->dstadr;
454 GET_INADDR(srcadr) = plist->srcadr;
455 srcadr.ss_family = AF_INET;
456 dstadr.ss_family = AF_INET;
458 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
459 srcadr.ss_len = SOCKLEN(&srcadr);
460 dstadr.ss_len = SOCKLEN(&dstadr);
461 #endif
462 if ((pcmd->nargs == 0) ||
463 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
464 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
465 (void) fprintf(fp,
466 "%c%-15.15s %-15.15s %2d %4d %3o %7.7s %9.9s %7.7s\n",
467 c, nntohost(&srcadr), stoa(&dstadr),
468 plist->stratum, ntp_poll, plist->reach,
469 fptoa(NTOHS_FP(plist->delay), 5),
470 lfptoa(&tempts, 6),
471 ufptoa(NTOHS_FP(plist->dispersion), 5));
472 plist++;
473 items--;
477 /* Convert a refid & stratum (in host order) to a string */
478 static char*
479 refid_string(
480 u_int32 refid,
481 int stratum
484 if (stratum <= 1) {
485 static char junk[5];
486 junk[4] = 0;
487 memmove(junk, (char *)&refid, 4);
488 return junk;
491 return numtoa(refid);
494 static void
495 print_pflag(
496 FILE *fp,
497 u_int32 flags
500 const char *str;
502 if (flags == 0) {
503 (void) fprintf(fp, " none\n");
504 } else {
505 str = "";
506 if (flags & INFO_FLAG_SYSPEER) {
507 (void) fprintf(fp, " system_peer");
508 str = ",";
510 if (flags & INFO_FLAG_CONFIG) {
511 (void) fprintf(fp, "%s config", str);
512 str = ",";
514 if (flags & INFO_FLAG_REFCLOCK) {
515 (void) fprintf(fp, "%s refclock", str);
516 str = ",";
518 if (flags & INFO_FLAG_AUTHENABLE) {
519 (void) fprintf(fp, "%s auth", str);
520 str = ",";
522 if (flags & INFO_FLAG_BCLIENT) {
523 (void) fprintf(fp, "%s bclient", str);
524 str = ",";
526 if (flags & INFO_FLAG_PREFER) {
527 (void) fprintf(fp, "%s prefer", str);
528 str = ",";
530 if (flags & INFO_FLAG_IBURST) {
531 (void) fprintf(fp, "%s iburst", str);
532 str = ",";
534 if (flags & INFO_FLAG_BURST) {
535 (void) fprintf(fp, "%s burst", str);
537 (void) fprintf(fp, "\n");
541 * printpeer - print detail information for a peer
543 static void
544 printpeer(
545 register struct info_peer *pp,
546 FILE *fp
549 register int i;
550 l_fp tempts;
551 struct sockaddr_storage srcadr, dstadr;
553 memset((char *)&srcadr, 0, sizeof(srcadr));
554 memset((char *)&dstadr, 0, sizeof(dstadr));
555 if (pp->v6_flag != 0) {
556 srcadr.ss_family = AF_INET6;
557 dstadr.ss_family = AF_INET6;
558 GET_INADDR6(srcadr) = pp->srcadr6;
559 GET_INADDR6(dstadr) = pp->dstadr6;
560 } else {
561 srcadr.ss_family = AF_INET;
562 dstadr.ss_family = AF_INET;
563 GET_INADDR(srcadr) = pp->srcadr;
564 GET_INADDR(dstadr) = pp->dstadr;
566 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
567 srcadr.ss_len = SOCKLEN(&srcadr);
568 dstadr.ss_len = SOCKLEN(&dstadr);
569 #endif
570 (void) fprintf(fp, "remote %s, local %s\n",
571 stoa(&srcadr), stoa(&dstadr));
572 (void) fprintf(fp, "hmode %s, pmode %s, stratum %d, precision %d\n",
573 modetoa(pp->hmode), modetoa(pp->pmode),
574 pp->stratum, pp->precision);
576 (void) fprintf(fp,
577 "leap %c%c, refid [%s], rootdistance %s, rootdispersion %s\n",
578 pp->leap & 0x2 ? '1' : '0',
579 pp->leap & 0x1 ? '1' : '0',
580 refid_string(pp->refid, pp->stratum), fptoa(NTOHS_FP(pp->rootdelay), 5),
581 ufptoa(NTOHS_FP(pp->rootdispersion), 5));
583 (void) fprintf(fp,
584 "ppoll %d, hpoll %d, keyid %lu, version %d, association %u\n",
585 pp->ppoll, pp->hpoll, (u_long)pp->keyid, pp->version, ntohs(pp->associd));
587 (void) fprintf(fp,
588 "reach %03o, unreach %d, flash 0x%04x, ",
589 pp->reach, pp->unreach, pp->flash2);
591 (void) fprintf(fp, "boffset %s, ttl/mode %d\n",
592 fptoa(NTOHS_FP(pp->estbdelay), 5), pp->ttl);
594 (void) fprintf(fp, "timer %lds, flags", (long)ntohl(pp->timer));
595 print_pflag(fp, pp->flags);
597 NTOHL_FP(&pp->reftime, &tempts);
598 (void) fprintf(fp, "reference time: %s\n",
599 prettydate(&tempts));
600 NTOHL_FP(&pp->org, &tempts);
601 (void) fprintf(fp, "originate timestamp: %s\n",
602 prettydate(&tempts));
603 NTOHL_FP(&pp->rec, &tempts);
604 (void) fprintf(fp, "receive timestamp: %s\n",
605 prettydate(&tempts));
606 NTOHL_FP(&pp->xmt, &tempts);
607 (void) fprintf(fp, "transmit timestamp: %s\n",
608 prettydate(&tempts));
610 (void) fprintf(fp, "filter delay: ");
611 for (i = 0; i < NTP_SHIFT; i++) {
612 (void) fprintf(fp, " %-8.8s",
613 fptoa(NTOHS_FP(pp->filtdelay[i]), 5));
614 if (i == (NTP_SHIFT>>1)-1)
615 (void) fprintf(fp, "\n ");
617 (void) fprintf(fp, "\n");
619 (void) fprintf(fp, "filter offset:");
620 for (i = 0; i < NTP_SHIFT; i++) {
621 NTOHL_FP(&pp->filtoffset[i], &tempts);
622 (void) fprintf(fp, " %-8.8s", lfptoa(&tempts, 6));
623 if (i == (NTP_SHIFT>>1)-1)
624 (void) fprintf(fp, "\n ");
626 (void) fprintf(fp, "\n");
628 (void) fprintf(fp, "filter order: ");
629 for (i = 0; i < NTP_SHIFT; i++) {
630 (void) fprintf(fp, " %-8d", pp->order[i]);
631 if (i == (NTP_SHIFT>>1)-1)
632 (void) fprintf(fp, "\n ");
634 (void) fprintf(fp, "\n");
637 NTOHL_FP(&pp->offset, &tempts);
638 (void) fprintf(fp,
639 "offset %s, delay %s, error bound %s, filter error %s\n",
640 lfptoa(&tempts, 6), fptoa(NTOHS_FP(pp->delay), 5),
641 ufptoa(NTOHS_FP(pp->dispersion), 5),
642 ufptoa(NTOHS_FP(pp->selectdisp), 5));
647 * showpeer - show detailed information for a peer
649 static void
650 showpeer(
651 struct parse *pcmd,
652 FILE *fp
655 struct info_peer *pp;
656 /* 4 is the maximum number of peers which will fit in a packet */
657 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
658 int qitems;
659 int items;
660 int itemsize;
661 int res;
662 int sendsize;
664 again:
665 if (impl_ver == IMPL_XNTPD)
666 sendsize = sizeof(struct info_peer_list);
667 else
668 sendsize = v4sizeof(struct info_peer_list);
670 for (qitems = 0, pl = plist; qitems < min(pcmd->nargs, 4); qitems++) {
671 if (pcmd->argval[qitems].netnum.ss_family == AF_INET) {
672 pl->addr = GET_INADDR(pcmd->argval[qitems].netnum);
673 if (impl_ver == IMPL_XNTPD)
674 pl->v6_flag = 0;
675 } else {
676 if (impl_ver == IMPL_XNTPD_OLD) {
677 fprintf(stderr,
678 "***Server doesn't understand IPv6 addresses\n");
679 return;
681 pl->addr6 = GET_INADDR6(pcmd->argval[qitems].netnum);
682 pl->v6_flag = 1;
684 pl->port = (u_short)s_port;
685 pl->hmode = pl->flags = 0;
686 pl = (struct info_peer_list *)((char *)pl + sendsize);
689 res = doquery(impl_ver, REQ_PEER_INFO, 0, qitems,
690 sendsize, (char *)plist, &items,
691 &itemsize, (void *)&pp, 0, sizeof(struct info_peer));
693 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
694 impl_ver = IMPL_XNTPD_OLD;
695 goto again;
698 if (res != 0)
699 return;
701 if (!checkitems(items, fp))
702 return;
704 if (!checkitemsize(itemsize, sizeof(struct info_peer)) &&
705 !checkitemsize(itemsize, v4sizeof(struct info_peer)))
706 return;
708 while (items-- > 0) {
709 printpeer(pp, fp);
710 if (items > 0)
711 (void) fprintf(fp, "\n");
712 pp++;
718 * peerstats - return statistics for a peer
720 static void
721 peerstats(
722 struct parse *pcmd,
723 FILE *fp
726 struct info_peer_stats *pp;
727 /* 4 is the maximum number of peers which will fit in a packet */
728 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
729 struct sockaddr_storage src, dst;
730 int qitems;
731 int items;
732 int itemsize;
733 int res;
734 int sendsize;
736 again:
737 if (impl_ver == IMPL_XNTPD)
738 sendsize = sizeof(struct info_peer_list);
739 else
740 sendsize = v4sizeof(struct info_peer_list);
742 memset((char *)plist, 0, sizeof(struct info_peer_list) * min(MAXARGS, 4));
743 for (qitems = 0, pl = plist; qitems < min(pcmd->nargs, 4); qitems++) {
744 if (pcmd->argval[qitems].netnum.ss_family == AF_INET) {
745 pl->addr = GET_INADDR(pcmd->argval[qitems].netnum);
746 if (impl_ver == IMPL_XNTPD)
747 pl->v6_flag = 0;
748 } else {
749 if (impl_ver == IMPL_XNTPD_OLD) {
750 fprintf(stderr,
751 "***Server doesn't understand IPv6 addresses\n");
752 return;
754 pl->addr6 = GET_INADDR6(pcmd->argval[qitems].netnum);
755 pl->v6_flag = 1;
757 pl->port = (u_short)s_port;
758 pl->hmode = plist[qitems].flags = 0;
759 pl = (struct info_peer_list *)((char *)pl + sendsize);
762 res = doquery(impl_ver, REQ_PEER_STATS, 0, qitems,
763 sendsize, (char *)plist, &items,
764 &itemsize, (void *)&pp, 0,
765 sizeof(struct info_peer_stats));
767 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
768 impl_ver = IMPL_XNTPD_OLD;
769 goto again;
772 if (res != 0)
773 return;
775 if (!checkitems(items, fp))
776 return;
778 if (!checkitemsize(itemsize, sizeof(struct info_peer_stats)) &&
779 !checkitemsize(itemsize, v4sizeof(struct info_peer_stats)))
780 return;
782 while (items-- > 0) {
783 memset((char *)&src, 0, sizeof(src));
784 memset((char *)&dst, 0, sizeof(dst));
785 if (pp->v6_flag != 0) {
786 GET_INADDR6(src) = pp->srcadr6;
787 GET_INADDR6(dst) = pp->dstadr6;
788 src.ss_family = AF_INET6;
789 dst.ss_family = AF_INET6;
790 } else {
791 GET_INADDR(src) = pp->srcadr;
792 GET_INADDR(dst) = pp->dstadr;
793 src.ss_family = AF_INET;
794 dst.ss_family = AF_INET;
796 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
797 src.ss_len = SOCKLEN(&src);
798 dst.ss_len = SOCKLEN(&dst);
799 #endif
800 (void) fprintf(fp, "remote host: %s\n",
801 nntohost(&src));
802 (void) fprintf(fp, "local interface: %s\n",
803 stoa(&dst));
804 (void) fprintf(fp, "time last received: %lds\n",
805 (long)ntohl(pp->timereceived));
806 (void) fprintf(fp, "time until next send: %lds\n",
807 (long)ntohl(pp->timetosend));
808 (void) fprintf(fp, "reachability change: %lds\n",
809 (long)ntohl(pp->timereachable));
810 (void) fprintf(fp, "packets sent: %ld\n",
811 (long)ntohl(pp->sent));
812 (void) fprintf(fp, "packets received: %ld\n",
813 (long)ntohl(pp->processed));
814 (void) fprintf(fp, "bad authentication: %ld\n",
815 (long)ntohl(pp->badauth));
816 (void) fprintf(fp, "bogus origin: %ld\n",
817 (long)ntohl(pp->bogusorg));
818 (void) fprintf(fp, "duplicate: %ld\n",
819 (long)ntohl(pp->oldpkt));
820 (void) fprintf(fp, "bad dispersion: %ld\n",
821 (long)ntohl(pp->seldisp));
822 (void) fprintf(fp, "bad reference time: %ld\n",
823 (long)ntohl(pp->selbroken));
824 (void) fprintf(fp, "candidate order: %d\n",
825 (int)pp->candidate);
826 if (items > 0)
827 (void) fprintf(fp, "\n");
828 (void) fprintf(fp, "flags: ");
829 print_pflag(fp, ntohs(pp->flags));
830 pp++;
836 * loopinfo - show loop filter information
838 static void
839 loopinfo(
840 struct parse *pcmd,
841 FILE *fp
844 struct info_loop *il;
845 int items;
846 int itemsize;
847 int oneline = 0;
848 int res;
849 l_fp tempts;
851 if (pcmd->nargs > 0) {
852 if (STREQ(pcmd->argval[0].string, "oneline"))
853 oneline = 1;
854 else if (STREQ(pcmd->argval[0].string, "multiline"))
855 oneline = 0;
856 else {
857 (void) fprintf(stderr, "How many lines?\n");
858 return;
862 again:
863 res = doquery(impl_ver, REQ_LOOP_INFO, 0, 0, 0, (char *)NULL,
864 &items, &itemsize, (void *)&il, 0,
865 sizeof(struct info_loop));
867 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
868 impl_ver = IMPL_XNTPD_OLD;
869 goto again;
872 if (res != 0)
873 return;
875 if (!check1item(items, fp))
876 return;
878 if (!checkitemsize(itemsize, sizeof(struct info_loop)))
879 return;
881 if (oneline) {
882 l_fp temp2ts;
884 NTOHL_FP(&il->last_offset, &tempts);
885 NTOHL_FP(&il->drift_comp, &temp2ts);
887 (void) fprintf(fp,
888 "offset %s, frequency %s, time_const %ld, watchdog %ld\n",
889 lfptoa(&tempts, 6),
890 lfptoa(&temp2ts, 3),
891 (long)(int32_t)ntohl((u_long)il->compliance),
892 (u_long)ntohl((u_long)il->watchdog_timer));
893 } else {
894 NTOHL_FP(&il->last_offset, &tempts);
895 (void) fprintf(fp, "offset: %s s\n",
896 lfptoa(&tempts, 6));
897 NTOHL_FP(&il->drift_comp, &tempts);
898 (void) fprintf(fp, "frequency: %s ppm\n",
899 lfptoa(&tempts, 3));
900 (void) fprintf(fp, "poll adjust: %ld\n",
901 (long)(int32_t)ntohl(il->compliance));
902 (void) fprintf(fp, "watchdog timer: %ld s\n",
903 (u_long)ntohl(il->watchdog_timer));
909 * sysinfo - show current system state
911 /*ARGSUSED*/
912 static void
913 sysinfo(
914 struct parse *pcmd,
915 FILE *fp
918 struct info_sys *is;
919 struct sockaddr_storage peeraddr;
920 int items;
921 int itemsize;
922 int res;
923 l_fp tempts;
925 again:
926 res = doquery(impl_ver, REQ_SYS_INFO, 0, 0, 0, (char *)NULL,
927 &items, &itemsize, (void *)&is, 0,
928 sizeof(struct info_sys));
930 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
931 impl_ver = IMPL_XNTPD_OLD;
932 goto again;
935 if (res != 0)
936 return;
938 if (!check1item(items, fp))
939 return;
941 if (!checkitemsize(itemsize, sizeof(struct info_sys)) &&
942 !checkitemsize(itemsize, v4sizeof(struct info_sys)))
943 return;
945 memset((char *)&peeraddr, 0, sizeof(peeraddr));
946 if (is->v6_flag != 0) {
947 GET_INADDR6(peeraddr) = is->peer6;
948 peeraddr.ss_family = AF_INET6;
949 } else {
950 GET_INADDR(peeraddr) = is->peer;
951 peeraddr.ss_family = AF_INET;
953 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
954 peeraddr.ss_len = SOCKLEN(&peeraddr);
955 #endif
956 (void) fprintf(fp, "system peer: %s\n", nntohost(&peeraddr));
957 (void) fprintf(fp, "system peer mode: %s\n", modetoa(is->peer_mode));
958 (void) fprintf(fp, "leap indicator: %c%c\n",
959 is->leap & 0x2 ? '1' : '0',
960 is->leap & 0x1 ? '1' : '0');
961 (void) fprintf(fp, "stratum: %d\n", (int)is->stratum);
962 (void) fprintf(fp, "precision: %d\n", (int)is->precision);
963 (void) fprintf(fp, "root distance: %s s\n",
964 fptoa(NTOHS_FP(is->rootdelay), 5));
965 (void) fprintf(fp, "root dispersion: %s s\n",
966 ufptoa(NTOHS_FP(is->rootdispersion), 5));
967 (void) fprintf(fp, "reference ID: [%s]\n",
968 refid_string(is->refid, is->stratum));
969 NTOHL_FP(&is->reftime, &tempts);
970 (void) fprintf(fp, "reference time: %s\n", prettydate(&tempts));
972 (void) fprintf(fp, "system flags: ");
973 if ((is->flags & (INFO_FLAG_BCLIENT | INFO_FLAG_AUTHENABLE |
974 INFO_FLAG_NTP | INFO_FLAG_KERNEL| INFO_FLAG_CAL |
975 INFO_FLAG_PPS_SYNC | INFO_FLAG_MONITOR | INFO_FLAG_FILEGEN)) == 0) {
976 (void) fprintf(fp, "none\n");
977 } else {
978 if (is->flags & INFO_FLAG_BCLIENT)
979 (void) fprintf(fp, "bclient ");
980 if (is->flags & INFO_FLAG_AUTHENTICATE)
981 (void) fprintf(fp, "auth ");
982 if (is->flags & INFO_FLAG_MONITOR)
983 (void) fprintf(fp, "monitor ");
984 if (is->flags & INFO_FLAG_NTP)
985 (void) fprintf(fp, "ntp ");
986 if (is->flags & INFO_FLAG_KERNEL)
987 (void) fprintf(fp, "kernel ");
988 if (is->flags & INFO_FLAG_FILEGEN)
989 (void) fprintf(fp, "stats ");
990 if (is->flags & INFO_FLAG_CAL)
991 (void) fprintf(fp, "calibrate ");
992 if (is->flags & INFO_FLAG_PPS_SYNC)
993 (void) fprintf(fp, "pps ");
994 (void) fprintf(fp, "\n");
996 (void) fprintf(fp, "jitter: %s s\n",
997 fptoa(ntohl(is->frequency), 6));
998 (void) fprintf(fp, "stability: %s ppm\n",
999 ufptoa(ntohl(is->stability), 3));
1000 (void) fprintf(fp, "broadcastdelay: %s s\n",
1001 fptoa(NTOHS_FP(is->bdelay), 6));
1002 NTOHL_FP(&is->authdelay, &tempts);
1003 (void) fprintf(fp, "authdelay: %s s\n", lfptoa(&tempts, 6));
1008 * sysstats - print system statistics
1010 /*ARGSUSED*/
1011 static void
1012 sysstats(
1013 struct parse *pcmd,
1014 FILE *fp
1017 struct info_sys_stats *ss;
1018 int items;
1019 int itemsize;
1020 int res;
1022 again:
1023 res = doquery(impl_ver, REQ_SYS_STATS, 0, 0, 0, (char *)NULL,
1024 &items, &itemsize, (void *)&ss, 0,
1025 sizeof(struct info_sys_stats));
1027 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1028 impl_ver = IMPL_XNTPD_OLD;
1029 goto again;
1032 if (res != 0)
1033 return;
1035 if (!check1item(items, fp))
1036 return;
1038 if (itemsize != sizeof(struct info_sys_stats) &&
1039 itemsize != sizeof(struct old_info_sys_stats)) {
1040 /* issue warning according to new structure size */
1041 checkitemsize(itemsize, sizeof(struct info_sys_stats));
1042 return;
1044 fprintf(fp, "time since restart: %ld\n",
1045 (u_long)ntohl(ss->timeup));
1046 fprintf(fp, "time since reset: %ld\n",
1047 (u_long)ntohl(ss->timereset));
1048 fprintf(fp, "packets received: %ld\n",
1049 (u_long)ntohl(ss->received));
1050 fprintf(fp, "packets processed: %ld\n",
1051 (u_long)ntohl(ss->processed));
1052 fprintf(fp, "current version: %ld\n",
1053 (u_long)ntohl(ss->newversionpkt));
1054 fprintf(fp, "previous version: %ld\n",
1055 (u_long)ntohl(ss->oldversionpkt));
1056 fprintf(fp, "bad version: %ld\n",
1057 (u_long)ntohl(ss->unknownversion));
1058 fprintf(fp, "access denied: %ld\n",
1059 (u_long)ntohl(ss->denied));
1060 fprintf(fp, "bad length or format: %ld\n",
1061 (u_long)ntohl(ss->badlength));
1062 fprintf(fp, "bad authentication: %ld\n",
1063 (u_long)ntohl(ss->badauth));
1064 if (itemsize != sizeof(struct info_sys_stats))
1065 return;
1067 fprintf(fp, "rate exceeded: %ld\n",
1068 (u_long)ntohl(ss->limitrejected));
1074 * iostats - print I/O statistics
1076 /*ARGSUSED*/
1077 static void
1078 iostats(
1079 struct parse *pcmd,
1080 FILE *fp
1083 struct info_io_stats *io;
1084 int items;
1085 int itemsize;
1086 int res;
1088 again:
1089 res = doquery(impl_ver, REQ_IO_STATS, 0, 0, 0, (char *)NULL,
1090 &items, &itemsize, (void *)&io, 0,
1091 sizeof(struct info_io_stats));
1093 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1094 impl_ver = IMPL_XNTPD_OLD;
1095 goto again;
1098 if (res != 0)
1099 return;
1101 if (!check1item(items, fp))
1102 return;
1104 if (!checkitemsize(itemsize, sizeof(struct info_io_stats)))
1105 return;
1107 (void) fprintf(fp, "time since reset: %ld\n",
1108 (u_long)ntohl(io->timereset));
1109 (void) fprintf(fp, "receive buffers: %d\n",
1110 ntohs(io->totalrecvbufs));
1111 (void) fprintf(fp, "free receive buffers: %d\n",
1112 ntohs(io->freerecvbufs));
1113 (void) fprintf(fp, "used receive buffers: %d\n",
1114 ntohs(io->fullrecvbufs));
1115 (void) fprintf(fp, "low water refills: %d\n",
1116 ntohs(io->lowwater));
1117 (void) fprintf(fp, "dropped packets: %ld\n",
1118 (u_long)ntohl(io->dropped));
1119 (void) fprintf(fp, "ignored packets: %ld\n",
1120 (u_long)ntohl(io->ignored));
1121 (void) fprintf(fp, "received packets: %ld\n",
1122 (u_long)ntohl(io->received));
1123 (void) fprintf(fp, "packets sent: %ld\n",
1124 (u_long)ntohl(io->sent));
1125 (void) fprintf(fp, "packets not sent: %ld\n",
1126 (u_long)ntohl(io->notsent));
1127 (void) fprintf(fp, "interrupts handled: %ld\n",
1128 (u_long)ntohl(io->interrupts));
1129 (void) fprintf(fp, "received by int: %ld\n",
1130 (u_long)ntohl(io->int_received));
1135 * memstats - print peer memory statistics
1137 /*ARGSUSED*/
1138 static void
1139 memstats(
1140 struct parse *pcmd,
1141 FILE *fp
1144 struct info_mem_stats *mem;
1145 int i;
1146 int items;
1147 int itemsize;
1148 int res;
1150 again:
1151 res = doquery(impl_ver, REQ_MEM_STATS, 0, 0, 0, (char *)NULL,
1152 &items, &itemsize, (void *)&mem, 0,
1153 sizeof(struct info_mem_stats));
1155 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1156 impl_ver = IMPL_XNTPD_OLD;
1157 goto again;
1160 if (res != 0)
1161 return;
1163 if (!check1item(items, fp))
1164 return;
1166 if (!checkitemsize(itemsize, sizeof(struct info_mem_stats)))
1167 return;
1169 (void) fprintf(fp, "time since reset: %ld\n",
1170 (u_long)ntohl(mem->timereset));
1171 (void) fprintf(fp, "total peer memory: %d\n",
1172 ntohs(mem->totalpeermem));
1173 (void) fprintf(fp, "free peer memory: %d\n",
1174 ntohs(mem->freepeermem));
1175 (void) fprintf(fp, "calls to findpeer: %ld\n",
1176 (u_long)ntohl(mem->findpeer_calls));
1177 (void) fprintf(fp, "new peer allocations: %ld\n",
1178 (u_long)ntohl(mem->allocations));
1179 (void) fprintf(fp, "peer demobilizations: %ld\n",
1180 (u_long)ntohl(mem->demobilizations));
1182 (void) fprintf(fp, "hash table counts: ");
1183 for (i = 0; i < NTP_HASH_SIZE; i++) {
1184 (void) fprintf(fp, "%4d", (int)mem->hashcount[i]);
1185 if ((i % 8) == 7 && i != (NTP_HASH_SIZE-1)) {
1186 (void) fprintf(fp, "\n ");
1189 (void) fprintf(fp, "\n");
1195 * timerstats - print timer statistics
1197 /*ARGSUSED*/
1198 static void
1199 timerstats(
1200 struct parse *pcmd,
1201 FILE *fp
1204 struct info_timer_stats *tim;
1205 int items;
1206 int itemsize;
1207 int res;
1209 again:
1210 res = doquery(impl_ver, REQ_TIMER_STATS, 0, 0, 0, (char *)NULL,
1211 &items, &itemsize, (void *)&tim, 0,
1212 sizeof(struct info_timer_stats));
1214 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1215 impl_ver = IMPL_XNTPD_OLD;
1216 goto again;
1219 if (res != 0)
1220 return;
1222 if (!check1item(items, fp))
1223 return;
1225 if (!checkitemsize(itemsize, sizeof(struct info_timer_stats)))
1226 return;
1228 (void) fprintf(fp, "time since reset: %ld\n",
1229 (u_long)ntohl(tim->timereset));
1230 (void) fprintf(fp, "alarms handled: %ld\n",
1231 (u_long)ntohl(tim->alarms));
1232 (void) fprintf(fp, "alarm overruns: %ld\n",
1233 (u_long)ntohl(tim->overflows));
1234 (void) fprintf(fp, "calls to transmit: %ld\n",
1235 (u_long)ntohl(tim->xmtcalls));
1240 * addpeer - configure an active mode association
1242 static void
1243 addpeer(
1244 struct parse *pcmd,
1245 FILE *fp
1248 doconfig(pcmd, fp, MODE_ACTIVE, 0);
1253 * addserver - configure a client mode association
1255 static void
1256 addserver(
1257 struct parse *pcmd,
1258 FILE *fp
1261 doconfig(pcmd, fp, MODE_CLIENT, 0);
1265 * addrefclock - configure a reference clock association
1267 static void
1268 addrefclock(
1269 struct parse *pcmd,
1270 FILE *fp
1273 doconfig(pcmd, fp, MODE_CLIENT, 1);
1277 * broadcast - configure a broadcast mode association
1279 static void
1280 broadcast(
1281 struct parse *pcmd,
1282 FILE *fp
1285 doconfig(pcmd, fp, MODE_BROADCAST, 0);
1290 * config - configure a new peer association
1292 static void
1293 doconfig(
1294 struct parse *pcmd,
1295 FILE *fp,
1296 int mode,
1297 int refc
1300 struct conf_peer cpeer;
1301 int items;
1302 int itemsize;
1303 char *dummy;
1304 u_long keyid;
1305 u_int version;
1306 u_char minpoll;
1307 u_char maxpoll;
1308 u_int flags;
1309 u_char cmode;
1310 int res;
1311 int sendsize;
1312 int numtyp;
1314 again:
1315 keyid = 0;
1316 version = 3;
1317 flags = 0;
1318 res = 0;
1319 cmode = 0;
1320 minpoll = NTP_MINDPOLL;
1321 maxpoll = NTP_MAXDPOLL;
1322 numtyp = 1;
1323 if (refc)
1324 numtyp = 5;
1326 if (impl_ver == IMPL_XNTPD)
1327 sendsize = sizeof(struct conf_peer);
1328 else
1329 sendsize = v4sizeof(struct conf_peer);
1331 items = 1;
1332 while (pcmd->nargs > items) {
1333 if (STREQ(pcmd->argval[items].string, "prefer"))
1334 flags |= CONF_FLAG_PREFER;
1335 else if (STREQ(pcmd->argval[items].string, "burst"))
1336 flags |= CONF_FLAG_BURST;
1337 else if (STREQ(pcmd->argval[items].string, "dynamic"))
1338 (void) fprintf(fp, "Warning: the \"dynamic\" keyword has been obsoleted and will be removed in the next release\n");
1339 else if (STREQ(pcmd->argval[items].string, "iburst"))
1340 flags |= CONF_FLAG_IBURST;
1341 else if (!refc && STREQ(pcmd->argval[items].string, "keyid"))
1342 numtyp = 1;
1343 else if (!refc && STREQ(pcmd->argval[items].string, "version"))
1344 numtyp = 2;
1345 else if (STREQ(pcmd->argval[items].string, "minpoll"))
1346 numtyp = 3;
1347 else if (STREQ(pcmd->argval[items].string, "maxpoll"))
1348 numtyp = 4;
1349 else {
1350 long val;
1351 if (!atoint(pcmd->argval[items].string, &val))
1352 numtyp = 0;
1353 switch (numtyp) {
1354 case 1:
1355 keyid = val;
1356 numtyp = 2;
1357 break;
1359 case 2:
1360 version = (u_int) val;
1361 numtyp = 0;
1362 break;
1364 case 3:
1365 minpoll = (u_char)val;
1366 numtyp = 0;
1367 break;
1369 case 4:
1370 maxpoll = (u_char)val;
1371 numtyp = 0;
1372 break;
1374 case 5:
1375 cmode = (u_char)val;
1376 numtyp = 0;
1377 break;
1379 default:
1380 (void) fprintf(fp, "*** '%s' not understood\n",
1381 pcmd->argval[items].string);
1382 res++;
1383 numtyp = 0;
1385 if (val < 0) {
1386 (void) fprintf(stderr,
1387 "***Value '%s' should be unsigned\n",
1388 pcmd->argval[items].string);
1389 res++;
1392 items++;
1394 if (keyid > 0)
1395 flags |= CONF_FLAG_AUTHENABLE;
1396 if (version > NTP_VERSION ||
1397 version < NTP_OLDVERSION) {
1398 (void)fprintf(fp, "***invalid version number: %u\n",
1399 version);
1400 res++;
1402 if (minpoll < NTP_MINPOLL || minpoll > NTP_MAXPOLL ||
1403 maxpoll < NTP_MINPOLL || maxpoll > NTP_MAXPOLL ||
1404 minpoll > maxpoll) {
1405 (void) fprintf(fp, "***min/max-poll must be within %d..%d\n",
1406 NTP_MINPOLL, NTP_MAXPOLL);
1407 res++;
1410 if (res)
1411 return;
1413 memset((void *)&cpeer, 0, sizeof(cpeer));
1415 if (pcmd->argval[0].netnum.ss_family == AF_INET) {
1416 cpeer.peeraddr = GET_INADDR(pcmd->argval[0].netnum);
1417 if (impl_ver == IMPL_XNTPD)
1418 cpeer.v6_flag = 0;
1419 } else {
1420 if (impl_ver == IMPL_XNTPD_OLD) {
1421 fprintf(stderr,
1422 "***Server doesn't understand IPv6 addresses\n");
1423 return;
1425 cpeer.peeraddr6 = GET_INADDR6(pcmd->argval[0].netnum);
1426 cpeer.v6_flag = 1;
1428 cpeer.hmode = (u_char) mode;
1429 cpeer.keyid = keyid;
1430 cpeer.version = (u_char) version;
1431 cpeer.minpoll = minpoll;
1432 cpeer.maxpoll = maxpoll;
1433 cpeer.flags = (u_char)flags;
1434 cpeer.ttl = cmode;
1436 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1437 sendsize, (char *)&cpeer, &items,
1438 &itemsize, &dummy, 0, sizeof(struct conf_peer));
1440 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1441 impl_ver = IMPL_XNTPD_OLD;
1442 goto again;
1445 if (res == INFO_ERR_FMT) {
1446 (void) fprintf(fp,
1447 "***Retrying command with old conf_peer size\n");
1448 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1449 sizeof(struct old_conf_peer), (char *)&cpeer,
1450 &items, &itemsize, &dummy, 0,
1451 sizeof(struct conf_peer));
1453 if (res == 0)
1454 (void) fprintf(fp, "done!\n");
1455 return;
1460 * unconfig - unconfigure some associations
1462 static void
1463 unconfig(
1464 struct parse *pcmd,
1465 FILE *fp
1468 /* 8 is the maximum number of peers which will fit in a packet */
1469 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
1470 int qitems;
1471 int items;
1472 int itemsize;
1473 char *dummy;
1474 int res;
1475 int sendsize;
1477 again:
1478 if (impl_ver == IMPL_XNTPD)
1479 sendsize = sizeof(struct conf_unpeer);
1480 else
1481 sendsize = v4sizeof(struct conf_unpeer);
1483 for (qitems = 0, pl = plist; qitems < min(pcmd->nargs, 8); qitems++) {
1484 if (pcmd->argval[0].netnum.ss_family == AF_INET) {
1485 pl->peeraddr = GET_INADDR(pcmd->argval[qitems].netnum);
1486 if (impl_ver == IMPL_XNTPD)
1487 pl->v6_flag = 0;
1488 } else {
1489 if (impl_ver == IMPL_XNTPD_OLD) {
1490 fprintf(stderr,
1491 "***Server doesn't understand IPv6 addresses\n");
1492 return;
1494 pl->peeraddr6 =
1495 GET_INADDR6(pcmd->argval[qitems].netnum);
1496 pl->v6_flag = 1;
1498 pl = (struct conf_unpeer *)((char *)pl + sendsize);
1501 res = doquery(impl_ver, REQ_UNCONFIG, 1, qitems,
1502 sendsize, (char *)plist, &items,
1503 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
1505 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1506 impl_ver = IMPL_XNTPD_OLD;
1507 goto again;
1510 if (res == 0)
1511 (void) fprintf(fp, "done!\n");
1516 * set - set some system flags
1518 static void
1519 set(
1520 struct parse *pcmd,
1521 FILE *fp
1524 doset(pcmd, fp, REQ_SET_SYS_FLAG);
1529 * clear - clear some system flags
1531 static void
1532 sys_clear(
1533 struct parse *pcmd,
1534 FILE *fp
1537 doset(pcmd, fp, REQ_CLR_SYS_FLAG);
1542 * doset - set/clear system flags
1544 static void
1545 doset(
1546 struct parse *pcmd,
1547 FILE *fp,
1548 int req
1551 /* 8 is the maximum number of peers which will fit in a packet */
1552 struct conf_sys_flags sys;
1553 int items;
1554 int itemsize;
1555 char *dummy;
1556 int res;
1558 sys.flags = 0;
1559 res = 0;
1560 for (items = 0; items < pcmd->nargs; items++) {
1561 if (STREQ(pcmd->argval[items].string, "auth"))
1562 sys.flags |= SYS_FLAG_AUTH;
1563 else if (STREQ(pcmd->argval[items].string, "bclient"))
1564 sys.flags |= SYS_FLAG_BCLIENT;
1565 else if (STREQ(pcmd->argval[items].string, "calibrate"))
1566 sys.flags |= SYS_FLAG_CAL;
1567 else if (STREQ(pcmd->argval[items].string, "kernel"))
1568 sys.flags |= SYS_FLAG_KERNEL;
1569 else if (STREQ(pcmd->argval[items].string, "monitor"))
1570 sys.flags |= SYS_FLAG_MONITOR;
1571 else if (STREQ(pcmd->argval[items].string, "ntp"))
1572 sys.flags |= SYS_FLAG_NTP;
1573 else if (STREQ(pcmd->argval[items].string, "pps"))
1574 sys.flags |= SYS_FLAG_PPS;
1575 else if (STREQ(pcmd->argval[items].string, "stats"))
1576 sys.flags |= SYS_FLAG_FILEGEN;
1577 else {
1578 (void) fprintf(fp, "Unknown flag %s\n",
1579 pcmd->argval[items].string);
1580 res = 1;
1584 sys.flags = htonl(sys.flags);
1585 if (res || sys.flags == 0)
1586 return;
1588 again:
1589 res = doquery(impl_ver, req, 1, 1,
1590 sizeof(struct conf_sys_flags), (char *)&sys, &items,
1591 &itemsize, &dummy, 0, sizeof(struct conf_sys_flags));
1593 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1594 impl_ver = IMPL_XNTPD_OLD;
1595 goto again;
1598 if (res == 0)
1599 (void) fprintf(fp, "done!\n");
1604 * data for printing/interrpreting the restrict flags
1606 struct resflags {
1607 const char *str;
1608 int bit;
1611 /* XXX: HMS: we apparently don't report set bits we do not recognize. */
1613 static struct resflags resflagsV2[] = {
1614 { "ignore", 0x001 },
1615 { "noserve", 0x002 },
1616 { "notrust", 0x004 },
1617 { "noquery", 0x008 },
1618 { "nomodify", 0x010 },
1619 { "nopeer", 0x020 },
1620 { "notrap", 0x040 },
1621 { "lptrap", 0x080 },
1622 { "limited", 0x100 },
1623 { "", 0 }
1626 static struct resflags resflagsV3[] = {
1627 { "ignore", RES_IGNORE },
1628 { "noserve", RES_DONTSERVE },
1629 { "notrust", RES_DONTTRUST },
1630 { "noquery", RES_NOQUERY },
1631 { "nomodify", RES_NOMODIFY },
1632 { "nopeer", RES_NOPEER },
1633 { "notrap", RES_NOTRAP },
1634 { "lptrap", RES_LPTRAP },
1635 { "limited", RES_LIMITED },
1636 { "version", RES_VERSION },
1637 { "kod", RES_DEMOBILIZE },
1638 { "timeout", RES_TIMEOUT },
1640 { "", 0 }
1643 static struct resflags resmflags[] = {
1644 { "ntpport", RESM_NTPONLY },
1645 { "interface", RESM_INTERFACE },
1646 { "", 0 }
1651 * reslist - obtain and print the server's restrict list
1653 /*ARGSUSED*/
1654 static void
1655 reslist(
1656 struct parse *pcmd,
1657 FILE *fp
1660 struct info_restrict *rl;
1661 struct sockaddr_storage resaddr;
1662 struct sockaddr_storage maskaddr;
1663 int items;
1664 int itemsize;
1665 int res;
1666 int skip;
1667 char *addr;
1668 char *mask;
1669 struct resflags *rf;
1670 u_int32 count;
1671 u_short flags;
1672 u_short mflags;
1673 char flagstr[300];
1674 static const char *comma = ", ";
1676 again:
1677 res = doquery(impl_ver, REQ_GET_RESTRICT, 0, 0, 0, (char *)NULL,
1678 &items, &itemsize, (void *)&rl, 0,
1679 sizeof(struct info_restrict));
1681 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1682 impl_ver = IMPL_XNTPD_OLD;
1683 goto again;
1686 if (res != 0)
1687 return;
1689 if (!checkitems(items, fp))
1690 return;
1692 if (!checkitemsize(itemsize, sizeof(struct info_restrict)) &&
1693 !checkitemsize(itemsize, v4sizeof(struct info_restrict)))
1694 return;
1696 (void) fprintf(fp,
1697 " address mask count flags\n");
1698 (void) fprintf(fp,
1699 "=====================================================================\n");
1701 while (items > 0) {
1702 memset((char *)&resaddr, 0, sizeof(resaddr));
1703 memset((char *)&maskaddr, 0, sizeof(maskaddr));
1704 if (rl->v6_flag != 0) {
1705 GET_INADDR6(resaddr) = rl->addr6;
1706 GET_INADDR6(maskaddr) = rl->mask6;
1707 resaddr.ss_family = AF_INET6;
1708 maskaddr.ss_family = AF_INET6;
1709 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
1710 resaddr.ss_len = SOCKLEN(&resaddr);
1711 #endif
1712 addr = nntohost(&resaddr);
1713 } else {
1714 GET_INADDR(resaddr) = rl->addr;
1715 GET_INADDR(maskaddr) = rl->mask;
1716 resaddr.ss_family = AF_INET;
1717 maskaddr.ss_family = AF_INET;
1718 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
1719 resaddr.ss_len = SOCKLEN(&resaddr);
1720 #endif
1721 if ((rl->mask == (u_int32)0xffffffff))
1722 addr = nntohost(&resaddr);
1723 else
1724 addr = stoa(&resaddr);
1726 mask = stoa(&maskaddr);
1727 skip = 1;
1728 if ((pcmd->nargs == 0) ||
1729 ((pcmd->argval->ival == 6) && (rl->v6_flag != 0)) ||
1730 ((pcmd->argval->ival == 4) && (rl->v6_flag == 0)))
1731 skip = 0;
1732 count = ntohl(rl->count);
1733 flags = ntohs(rl->flags);
1734 mflags = ntohs(rl->mflags);
1735 flagstr[0] = '\0';
1737 res = 1;
1738 rf = &resmflags[0];
1739 while (rf->bit != 0) {
1740 if (mflags & rf->bit) {
1741 if (!res)
1742 (void) strcat(flagstr, comma);
1743 res = 0;
1744 (void) strcat(flagstr, rf->str);
1746 rf++;
1749 rf = (impl_ver == IMPL_XNTPD_OLD)
1750 ? &resflagsV2[0]
1751 : &resflagsV3[0]
1753 while (rf->bit != 0) {
1754 if (flags & rf->bit) {
1755 if (!res)
1756 (void) strcat(flagstr, comma);
1757 res = 0;
1758 (void) strcat(flagstr, rf->str);
1760 rf++;
1763 if (flagstr[0] == '\0')
1764 (void) strcpy(flagstr, "none");
1766 if (!skip)
1767 (void) fprintf(fp, "%-15.15s %-15.15s %9ld %s\n",
1768 addr, mask, (u_long)count, flagstr);
1769 rl++;
1770 items--;
1777 * new_restrict - create/add a set of restrictions
1779 static void
1780 new_restrict(
1781 struct parse *pcmd,
1782 FILE *fp
1785 do_restrict(pcmd, fp, REQ_RESADDFLAGS);
1790 * unrestrict - remove restriction flags from existing entry
1792 static void
1793 unrestrict(
1794 struct parse *pcmd,
1795 FILE *fp
1798 do_restrict(pcmd, fp, REQ_RESSUBFLAGS);
1803 * delrestrict - delete an existing restriction
1805 static void
1806 delrestrict(
1807 struct parse *pcmd,
1808 FILE *fp
1811 do_restrict(pcmd, fp, REQ_UNRESTRICT);
1816 * do_restrict - decode commandline restrictions and make the request
1818 static void
1819 do_restrict(
1820 struct parse *pcmd,
1821 FILE *fp,
1822 int req_code
1825 struct conf_restrict cres;
1826 int items;
1827 int itemsize;
1828 char *dummy;
1829 u_int32 num;
1830 u_long bit;
1831 int i;
1832 int res;
1833 int err;
1834 int sendsize;
1836 /* Initialize cres */
1837 cres.addr = 0;
1838 cres.mask = 0;
1839 cres.flags = 0;
1840 cres.mflags = 0;
1841 cres.v6_flag = 0;
1843 again:
1844 if (impl_ver == IMPL_XNTPD)
1845 sendsize = sizeof(struct conf_restrict);
1846 else
1847 sendsize = v4sizeof(struct conf_restrict);
1849 if (pcmd->argval[0].netnum.ss_family == AF_INET) {
1850 cres.addr = GET_INADDR(pcmd->argval[0].netnum);
1851 cres.mask = GET_INADDR(pcmd->argval[1].netnum);
1852 if (impl_ver == IMPL_XNTPD)
1853 cres.v6_flag = 0;
1854 } else {
1855 if (impl_ver == IMPL_XNTPD_OLD) {
1856 fprintf(stderr,
1857 "***Server doesn't understand IPv6 addresses\n");
1858 return;
1860 cres.addr6 = GET_INADDR6(pcmd->argval[0].netnum);
1861 cres.v6_flag = 1;
1863 cres.flags = 0;
1864 cres.mflags = 0;
1865 err = 0;
1866 for (res = 2; res < pcmd->nargs; res++) {
1867 if (STREQ(pcmd->argval[res].string, "ntpport")) {
1868 cres.mflags |= RESM_NTPONLY;
1869 } else {
1870 for (i = 0; resflagsV3[i].bit != 0; i++) {
1871 if (STREQ(pcmd->argval[res].string,
1872 resflagsV3[i].str))
1873 break;
1875 if (resflagsV3[i].bit != 0) {
1876 cres.flags |= resflagsV3[i].bit;
1877 if (req_code == REQ_UNRESTRICT) {
1878 (void) fprintf(fp,
1879 "Flag %s inappropriate\n",
1880 resflagsV3[i].str);
1881 err++;
1883 } else {
1884 (void) fprintf(fp, "Unknown flag %s\n",
1885 pcmd->argval[res].string);
1886 err++;
1890 cres.flags = htons(cres.flags);
1891 cres.mflags = htons(cres.mflags);
1894 * Make sure mask for default address is zero. Otherwise,
1895 * make sure mask bits are contiguous.
1897 if (pcmd->argval[0].netnum.ss_family == AF_INET) {
1898 if (cres.addr == 0) {
1899 cres.mask = 0;
1900 } else {
1901 num = ntohl(cres.mask);
1902 for (bit = 0x80000000; bit != 0; bit >>= 1)
1903 if ((num & bit) == 0)
1904 break;
1905 for ( ; bit != 0; bit >>= 1)
1906 if ((num & bit) != 0)
1907 break;
1908 if (bit != 0) {
1909 (void) fprintf(fp, "Invalid mask %s\n",
1910 numtoa(cres.mask));
1911 err++;
1914 } else {
1915 /* XXX IPv6 sanity checking stuff */
1918 if (err)
1919 return;
1921 res = doquery(impl_ver, req_code, 1, 1,
1922 sendsize, (char *)&cres, &items,
1923 &itemsize, &dummy, 0, sizeof(struct conf_restrict));
1925 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1926 impl_ver = IMPL_XNTPD_OLD;
1927 goto again;
1930 if (res == 0)
1931 (void) fprintf(fp, "done!\n");
1932 return;
1937 * monlist - obtain and print the server's monitor data
1939 /*ARGSUSED*/
1940 static void
1941 monlist(
1942 struct parse *pcmd,
1943 FILE *fp
1946 char *struct_star;
1947 struct sockaddr_storage addr;
1948 struct sockaddr_storage dstadr;
1949 int items;
1950 int itemsize;
1951 int res;
1952 int version = -1;
1954 if (pcmd->nargs > 0) {
1955 version = pcmd->argval[0].ival;
1958 again:
1959 res = doquery(impl_ver,
1960 (version == 1 || version == -1) ? REQ_MON_GETLIST_1 :
1961 REQ_MON_GETLIST, 0, 0, 0, (char *)NULL,
1962 &items, &itemsize, &struct_star,
1963 (version < 0) ? (1 << INFO_ERR_REQ) : 0,
1964 sizeof(struct info_monitor_1));
1966 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1967 impl_ver = IMPL_XNTPD_OLD;
1968 goto again;
1971 if (res == INFO_ERR_REQ && version < 0)
1972 res = doquery(impl_ver, REQ_MON_GETLIST, 0, 0, 0, (char *)NULL,
1973 &items, &itemsize, &struct_star, 0,
1974 sizeof(struct info_monitor));
1976 if (res != 0)
1977 return;
1979 if (!checkitems(items, fp))
1980 return;
1982 if (itemsize == sizeof(struct info_monitor_1) ||
1983 itemsize == v4sizeof(struct info_monitor_1)) {
1984 struct info_monitor_1 *ml = (struct info_monitor_1 *) struct_star;
1986 (void) fprintf(fp,
1987 "remote address port local address count m ver code avgint lstint\n");
1988 (void) fprintf(fp,
1989 "===============================================================================\n");
1990 while (items > 0) {
1991 memset((char *)&addr, 0, sizeof(addr));
1992 memset((char *)&dstadr, 0, sizeof(dstadr));
1993 if (ml->v6_flag != 0) {
1994 GET_INADDR6(addr) = ml->addr6;
1995 addr.ss_family = AF_INET6;
1996 GET_INADDR6(dstadr) = ml->daddr6;
1997 dstadr.ss_family = AF_INET6;
1998 } else {
1999 GET_INADDR(addr) = ml->addr;
2000 addr.ss_family = AF_INET;
2001 GET_INADDR(dstadr) = ml->daddr;
2002 dstadr.ss_family = AF_INET;
2004 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
2005 addr.ss_len = SOCKLEN(&addr);
2006 dstadr.ss_len = SOCKLEN(&dstadr);
2007 #endif
2008 if ((pcmd->nargs == 0) ||
2009 ((pcmd->argval->ival == 6) && (ml->v6_flag != 0)) ||
2010 ((pcmd->argval->ival == 4) && (ml->v6_flag == 0)))
2011 (void) fprintf(fp,
2012 "%-22.22s %5d %-15s %8ld %1d %1d %6lx %6lu %7lu\n",
2013 nntohost(&addr),
2014 ntohs(ml->port),
2015 stoa(&dstadr),
2016 (u_long)ntohl(ml->count),
2017 ml->mode,
2018 ml->version,
2019 (u_long)ntohl(ml->lastdrop),
2020 (u_long)ntohl(ml->lasttime),
2021 (u_long)ntohl(ml->firsttime));
2022 ml++;
2023 items--;
2025 } else if (itemsize == sizeof(struct info_monitor) ||
2026 itemsize == v4sizeof(struct info_monitor)) {
2027 struct info_monitor *ml = (struct info_monitor *) struct_star;
2029 (void) fprintf(fp,
2030 " address port count mode ver code avgint lstint\n");
2031 (void) fprintf(fp,
2032 "===============================================================================\n");
2033 while (items > 0) {
2034 memset((char *)&dstadr, 0, sizeof(dstadr));
2035 if (ml->v6_flag != 0) {
2036 GET_INADDR6(dstadr) = ml->addr6;
2037 dstadr.ss_family = AF_INET6;
2038 } else {
2039 GET_INADDR(dstadr) = ml->addr;
2040 dstadr.ss_family = AF_INET;
2042 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
2043 dstadr.ss_len = SOCKLEN(&dstadr);
2044 #endif
2045 if ((pcmd->nargs == 0) ||
2046 ((pcmd->argval->ival == 6) && (ml->v6_flag != 0)) ||
2047 ((pcmd->argval->ival == 4) && (ml->v6_flag == 0)))
2048 (void) fprintf(fp,
2049 "%-25.25s %5d %9ld %4d %2d %9lx %9lu %9lu\n",
2050 nntohost(&dstadr),
2051 ntohs(ml->port),
2052 (u_long)ntohl(ml->count),
2053 ml->mode,
2054 ml->version,
2055 (u_long)ntohl(ml->lastdrop),
2056 (u_long)ntohl(ml->lasttime),
2057 (u_long)ntohl(ml->firsttime));
2058 ml++;
2059 items--;
2061 } else if (itemsize == sizeof(struct old_info_monitor)) {
2062 struct old_info_monitor *oml = (struct old_info_monitor *)struct_star;
2063 (void) fprintf(fp,
2064 " address port count mode version lasttime firsttime\n");
2065 (void) fprintf(fp,
2066 "======================================================================\n");
2067 while (items > 0) {
2068 memset((char *)&dstadr, 0, sizeof(dstadr));
2069 if (oml->v6_flag != 0) {
2070 GET_INADDR6(dstadr) = oml->addr6;
2071 dstadr.ss_family = AF_INET6;
2072 } else {
2073 GET_INADDR(dstadr) = oml->addr;
2074 dstadr.ss_family = AF_INET;
2076 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
2077 dstadr.ss_len = SOCKLEN(&dstadr);
2078 #endif
2079 (void) fprintf(fp, "%-20.20s %5d %9ld %4d %3d %9lu %9lu\n",
2080 nntohost(&dstadr),
2081 ntohs(oml->port),
2082 (u_long)ntohl(oml->count),
2083 oml->mode,
2084 oml->version,
2085 (u_long)ntohl(oml->lasttime),
2086 (u_long)ntohl(oml->firsttime));
2087 oml++;
2088 items--;
2090 } else {
2091 /* issue warning according to new info_monitor size */
2092 checkitemsize(itemsize, sizeof(struct info_monitor));
2098 * Mapping between command line strings and stat reset flags
2100 struct statreset {
2101 const char *str;
2102 int flag;
2103 } sreset[] = {
2104 { "io", RESET_FLAG_IO },
2105 { "sys", RESET_FLAG_SYS },
2106 { "mem", RESET_FLAG_MEM },
2107 { "timer", RESET_FLAG_TIMER },
2108 { "auth", RESET_FLAG_AUTH },
2109 { "allpeers", RESET_FLAG_ALLPEERS },
2110 { "", 0 }
2114 * reset - reset statistic counters
2116 static void
2117 reset(
2118 struct parse *pcmd,
2119 FILE *fp
2122 struct reset_flags rflags;
2123 int items;
2124 int itemsize;
2125 char *dummy;
2126 int i;
2127 int res;
2128 int err;
2130 err = 0;
2131 rflags.flags = 0;
2132 for (res = 0; res < pcmd->nargs; res++) {
2133 for (i = 0; sreset[i].flag != 0; i++) {
2134 if (STREQ(pcmd->argval[res].string, sreset[i].str))
2135 break;
2137 if (sreset[i].flag == 0) {
2138 (void) fprintf(fp, "Flag %s unknown\n",
2139 pcmd->argval[res].string);
2140 err++;
2141 } else {
2142 rflags.flags |= sreset[i].flag;
2145 rflags.flags = htonl(rflags.flags);
2147 if (err) {
2148 (void) fprintf(fp, "Not done due to errors\n");
2149 return;
2152 again:
2153 res = doquery(impl_ver, REQ_RESET_STATS, 1, 1,
2154 sizeof(struct reset_flags), (char *)&rflags, &items,
2155 &itemsize, &dummy, 0, sizeof(struct reset_flags));
2157 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2158 impl_ver = IMPL_XNTPD_OLD;
2159 goto again;
2162 if (res == 0)
2163 (void) fprintf(fp, "done!\n");
2164 return;
2170 * preset - reset stat counters for particular peers
2172 static void
2173 preset(
2174 struct parse *pcmd,
2175 FILE *fp
2178 /* 8 is the maximum number of peers which will fit in a packet */
2179 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
2180 int qitems;
2181 int items;
2182 int itemsize;
2183 char *dummy;
2184 int res;
2185 int sendsize;
2187 again:
2188 if (impl_ver == IMPL_XNTPD)
2189 sendsize = sizeof(struct conf_unpeer);
2190 else
2191 sendsize = v4sizeof(struct conf_unpeer);
2193 for (qitems = 0, pl = plist; qitems < min(pcmd->nargs, 8); qitems++) {
2194 if (pcmd->argval[qitems].netnum.ss_family == AF_INET) {
2195 pl->peeraddr = GET_INADDR(pcmd->argval[qitems].netnum);
2196 if (impl_ver == IMPL_XNTPD)
2197 pl->v6_flag = 0;
2198 } else {
2199 if (impl_ver == IMPL_XNTPD_OLD) {
2200 fprintf(stderr,
2201 "***Server doesn't understand IPv6 addresses\n");
2202 return;
2204 pl->peeraddr6 =
2205 GET_INADDR6(pcmd->argval[qitems].netnum);
2206 pl->v6_flag = 1;
2208 pl = (struct conf_unpeer *)((char *)pl + sendsize);
2211 res = doquery(impl_ver, REQ_RESET_PEER, 1, qitems,
2212 sendsize, (char *)plist, &items,
2213 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
2215 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2216 impl_ver = IMPL_XNTPD_OLD;
2217 goto again;
2220 if (res == 0)
2221 (void) fprintf(fp, "done!\n");
2226 * readkeys - request the server to reread the keys file
2228 /*ARGSUSED*/
2229 static void
2230 readkeys(
2231 struct parse *pcmd,
2232 FILE *fp
2235 int items;
2236 int itemsize;
2237 char *dummy;
2238 int res;
2240 again:
2241 res = doquery(impl_ver, REQ_REREAD_KEYS, 1, 0, 0, (char *)0,
2242 &items, &itemsize, &dummy, 0, sizeof(dummy));
2244 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2245 impl_ver = IMPL_XNTPD_OLD;
2246 goto again;
2249 if (res == 0)
2250 (void) fprintf(fp, "done!\n");
2251 return;
2256 * trustkey - add some keys to the trusted key list
2258 static void
2259 trustkey(
2260 struct parse *pcmd,
2261 FILE *fp
2264 do_trustkey(pcmd, fp, REQ_TRUSTKEY);
2269 * untrustkey - remove some keys from the trusted key list
2271 static void
2272 untrustkey(
2273 struct parse *pcmd,
2274 FILE *fp
2277 do_trustkey(pcmd, fp, REQ_UNTRUSTKEY);
2282 * do_trustkey - do grunge work of adding/deleting keys
2284 static void
2285 do_trustkey(
2286 struct parse *pcmd,
2287 FILE *fp,
2288 int req
2291 u_long keyids[MAXARGS];
2292 int i;
2293 int items;
2294 int itemsize;
2295 char *dummy;
2296 int ritems;
2297 int res;
2299 ritems = 0;
2300 for (i = 0; i < pcmd->nargs; i++) {
2301 keyids[ritems++] = pcmd->argval[i].uval;
2304 again:
2305 res = doquery(impl_ver, req, 1, ritems, sizeof(u_long),
2306 (char *)keyids, &items, &itemsize, &dummy, 0,
2307 sizeof(dummy));
2309 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2310 impl_ver = IMPL_XNTPD_OLD;
2311 goto again;
2314 if (res == 0)
2315 (void) fprintf(fp, "done!\n");
2316 return;
2322 * authinfo - obtain and print info about authentication
2324 /*ARGSUSED*/
2325 static void
2326 authinfo(
2327 struct parse *pcmd,
2328 FILE *fp
2331 struct info_auth *ia;
2332 int items;
2333 int itemsize;
2334 int res;
2336 again:
2337 res = doquery(impl_ver, REQ_AUTHINFO, 0, 0, 0, (char *)NULL,
2338 &items, &itemsize, (void *)&ia, 0,
2339 sizeof(struct info_auth));
2341 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2342 impl_ver = IMPL_XNTPD_OLD;
2343 goto again;
2346 if (res != 0)
2347 return;
2349 if (!check1item(items, fp))
2350 return;
2352 if (!checkitemsize(itemsize, sizeof(struct info_auth)))
2353 return;
2355 (void) fprintf(fp, "time since reset: %ld\n",
2356 (u_long)ntohl(ia->timereset));
2357 (void) fprintf(fp, "stored keys: %ld\n",
2358 (u_long)ntohl(ia->numkeys));
2359 (void) fprintf(fp, "free keys: %ld\n",
2360 (u_long)ntohl(ia->numfreekeys));
2361 (void) fprintf(fp, "key lookups: %ld\n",
2362 (u_long)ntohl(ia->keylookups));
2363 (void) fprintf(fp, "keys not found: %ld\n",
2364 (u_long)ntohl(ia->keynotfound));
2365 (void) fprintf(fp, "uncached keys: %ld\n",
2366 (u_long)ntohl(ia->keyuncached));
2367 (void) fprintf(fp, "encryptions: %ld\n",
2368 (u_long)ntohl(ia->encryptions));
2369 (void) fprintf(fp, "decryptions: %ld\n",
2370 (u_long)ntohl(ia->decryptions));
2371 (void) fprintf(fp, "expired keys: %ld\n",
2372 (u_long)ntohl(ia->expired));
2378 * traps - obtain and print a list of traps
2380 /*ARGSUSED*/
2381 static void
2382 traps(
2383 struct parse *pcmd,
2384 FILE *fp
2387 int i;
2388 struct info_trap *it;
2389 struct sockaddr_storage trap_addr, local_addr;
2390 int items;
2391 int itemsize;
2392 int res;
2394 again:
2395 res = doquery(impl_ver, REQ_TRAPS, 0, 0, 0, (char *)NULL,
2396 &items, &itemsize, (void *)&it, 0,
2397 sizeof(struct info_trap));
2399 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2400 impl_ver = IMPL_XNTPD_OLD;
2401 goto again;
2404 if (res != 0)
2405 return;
2407 if (!checkitems(items, fp))
2408 return;
2410 if (!checkitemsize(itemsize, sizeof(struct info_trap)) &&
2411 !checkitemsize(itemsize, v4sizeof(struct info_trap)))
2412 return;
2414 for (i = 0; i < items; i++ ) {
2415 if (i != 0)
2416 (void) fprintf(fp, "\n");
2417 memset((char *)&trap_addr, 0, sizeof(trap_addr));
2418 memset((char *)&local_addr, 0, sizeof(local_addr));
2419 if (it->v6_flag != 0) {
2420 GET_INADDR6(trap_addr) = it->trap_address6;
2421 GET_INADDR6(local_addr) = it->local_address6;
2422 trap_addr.ss_family = AF_INET6;
2423 local_addr.ss_family = AF_INET6;
2424 } else {
2425 GET_INADDR(trap_addr) = it->trap_address;
2426 GET_INADDR(local_addr) = it->local_address;
2427 trap_addr.ss_family = AF_INET;
2428 local_addr.ss_family = AF_INET;
2430 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
2431 trap_addr.ss_len = SOCKLEN(&trap_addr);
2432 local_addr.ss_len = SOCKLEN(&local_addr);
2433 #endif
2434 (void) fprintf(fp, "address %s, port %d\n",
2435 stoa(&trap_addr),
2436 ntohs(it->trap_port));
2437 (void) fprintf(fp, "interface: %s, ",
2438 (it->local_address == 0)
2439 ? "wildcard"
2440 : stoa(&local_addr));
2441 if (ntohl(it->flags) & TRAP_CONFIGURED)
2442 (void) fprintf(fp, "configured\n");
2443 else if (ntohl(it->flags) & TRAP_NONPRIO)
2444 (void) fprintf(fp, "low priority\n");
2445 else
2446 (void) fprintf(fp, "normal priority\n");
2448 (void) fprintf(fp, "set for %ld secs, last set %ld secs ago\n",
2449 (long)ntohl(it->origtime),
2450 (long)ntohl(it->settime));
2451 (void) fprintf(fp, "sequence %d, number of resets %ld\n",
2452 ntohs(it->sequence),
2453 (long)ntohl(it->resets));
2459 * addtrap - configure a trap
2461 static void
2462 addtrap(
2463 struct parse *pcmd,
2464 FILE *fp
2467 do_addclr_trap(pcmd, fp, REQ_ADD_TRAP);
2472 * clrtrap - clear a trap from the server
2474 static void
2475 clrtrap(
2476 struct parse *pcmd,
2477 FILE *fp
2480 do_addclr_trap(pcmd, fp, REQ_CLR_TRAP);
2485 * do_addclr_trap - do grunge work of adding/deleting traps
2487 static void
2488 do_addclr_trap(
2489 struct parse *pcmd,
2490 FILE *fp,
2491 int req
2494 struct conf_trap ctrap;
2495 int items;
2496 int itemsize;
2497 char *dummy;
2498 int res;
2499 int sendsize;
2501 again:
2502 if (impl_ver == IMPL_XNTPD)
2503 sendsize = sizeof(struct conf_trap);
2504 else
2505 sendsize = v4sizeof(struct conf_trap);
2507 if (pcmd->argval[0].netnum.ss_family == AF_INET) {
2508 ctrap.trap_address = GET_INADDR(pcmd->argval[0].netnum);
2509 if (impl_ver == IMPL_XNTPD)
2510 ctrap.v6_flag = 0;
2511 } else {
2512 if (impl_ver == IMPL_XNTPD_OLD) {
2513 fprintf(stderr,
2514 "***Server doesn't understand IPv6 addresses\n");
2515 return;
2517 ctrap.trap_address6 = GET_INADDR6(pcmd->argval[0].netnum);
2518 ctrap.v6_flag = 1;
2520 ctrap.local_address = 0;
2521 ctrap.trap_port = htons(TRAPPORT);
2522 ctrap.unused = 0;
2524 if (pcmd->nargs > 1) {
2525 ctrap.trap_port
2526 = htons((u_short)(pcmd->argval[1].uval & 0xffff));
2527 if (pcmd->nargs > 2) {
2528 if (pcmd->argval[2].netnum.ss_family !=
2529 pcmd->argval[0].netnum.ss_family) {
2530 fprintf(stderr,
2531 "***Cannot mix IPv4 and IPv6 addresses\n");
2532 return;
2534 if (pcmd->argval[2].netnum.ss_family == AF_INET)
2535 ctrap.local_address = GET_INADDR(pcmd->argval[2].netnum);
2536 else
2537 ctrap.local_address6 = GET_INADDR6(pcmd->argval[2].netnum);
2541 res = doquery(impl_ver, req, 1, 1, sendsize,
2542 (char *)&ctrap, &items, &itemsize, &dummy, 0,
2543 sizeof(struct conf_trap));
2545 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2546 impl_ver = IMPL_XNTPD_OLD;
2547 goto again;
2550 if (res == 0)
2551 (void) fprintf(fp, "done!\n");
2552 return;
2558 * requestkey - change the server's request key (a dangerous request)
2560 static void
2561 requestkey(
2562 struct parse *pcmd,
2563 FILE *fp
2566 do_changekey(pcmd, fp, REQ_REQUEST_KEY);
2571 * controlkey - change the server's control key
2573 static void
2574 controlkey(
2575 struct parse *pcmd,
2576 FILE *fp
2579 do_changekey(pcmd, fp, REQ_CONTROL_KEY);
2585 * do_changekey - do grunge work of changing keys
2587 static void
2588 do_changekey(
2589 struct parse *pcmd,
2590 FILE *fp,
2591 int req
2594 u_long key;
2595 int items;
2596 int itemsize;
2597 char *dummy;
2598 int res;
2601 key = htonl((u_int32)pcmd->argval[0].uval);
2603 again:
2604 res = doquery(impl_ver, req, 1, 1, sizeof(u_int32),
2605 (char *)&key, &items, &itemsize, &dummy, 0,
2606 sizeof(dummy));
2608 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2609 impl_ver = IMPL_XNTPD_OLD;
2610 goto again;
2613 if (res == 0)
2614 (void) fprintf(fp, "done!\n");
2615 return;
2621 * ctlstats - obtain and print info about authentication
2623 /*ARGSUSED*/
2624 static void
2625 ctlstats(
2626 struct parse *pcmd,
2627 FILE *fp
2630 struct info_control *ic;
2631 int items;
2632 int itemsize;
2633 int res;
2635 again:
2636 res = doquery(impl_ver, REQ_GET_CTLSTATS, 0, 0, 0, (char *)NULL,
2637 &items, &itemsize, (void *)&ic, 0,
2638 sizeof(struct info_control));
2640 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2641 impl_ver = IMPL_XNTPD_OLD;
2642 goto again;
2645 if (res != 0)
2646 return;
2648 if (!check1item(items, fp))
2649 return;
2651 if (!checkitemsize(itemsize, sizeof(struct info_control)))
2652 return;
2654 (void) fprintf(fp, "time since reset: %ld\n",
2655 (u_long)ntohl(ic->ctltimereset));
2656 (void) fprintf(fp, "requests received: %ld\n",
2657 (u_long)ntohl(ic->numctlreq));
2658 (void) fprintf(fp, "responses sent: %ld\n",
2659 (u_long)ntohl(ic->numctlresponses));
2660 (void) fprintf(fp, "fragments sent: %ld\n",
2661 (u_long)ntohl(ic->numctlfrags));
2662 (void) fprintf(fp, "async messages sent: %ld\n",
2663 (u_long)ntohl(ic->numasyncmsgs));
2664 (void) fprintf(fp, "error msgs sent: %ld\n",
2665 (u_long)ntohl(ic->numctlerrors));
2666 (void) fprintf(fp, "total bad pkts: %ld\n",
2667 (u_long)ntohl(ic->numctlbadpkts));
2668 (void) fprintf(fp, "packet too short: %ld\n",
2669 (u_long)ntohl(ic->numctltooshort));
2670 (void) fprintf(fp, "response on input: %ld\n",
2671 (u_long)ntohl(ic->numctlinputresp));
2672 (void) fprintf(fp, "fragment on input: %ld\n",
2673 (u_long)ntohl(ic->numctlinputfrag));
2674 (void) fprintf(fp, "error set on input: %ld\n",
2675 (u_long)ntohl(ic->numctlinputerr));
2676 (void) fprintf(fp, "bad offset on input: %ld\n",
2677 (u_long)ntohl(ic->numctlbadoffset));
2678 (void) fprintf(fp, "bad version packets: %ld\n",
2679 (u_long)ntohl(ic->numctlbadversion));
2680 (void) fprintf(fp, "data in pkt too short: %ld\n",
2681 (u_long)ntohl(ic->numctldatatooshort));
2682 (void) fprintf(fp, "unknown op codes: %ld\n",
2683 (u_long)ntohl(ic->numctlbadop));
2688 * clockstat - get and print clock status information
2690 static void
2691 clockstat(
2692 struct parse *pcmd,
2693 FILE *fp
2696 struct info_clock *cl;
2697 /* 8 is the maximum number of clocks which will fit in a packet */
2698 u_long clist[min(MAXARGS, 8)];
2699 int qitems;
2700 int items;
2701 int itemsize;
2702 int res;
2703 l_fp ts;
2704 struct clktype *clk;
2706 for (qitems = 0; qitems < min(pcmd->nargs, 8); qitems++)
2707 clist[qitems] = GET_INADDR(pcmd->argval[qitems].netnum);
2709 again:
2710 res = doquery(impl_ver, REQ_GET_CLOCKINFO, 0, qitems,
2711 sizeof(u_int32), (char *)clist, &items,
2712 &itemsize, (void *)&cl, 0, sizeof(struct info_clock));
2714 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2715 impl_ver = IMPL_XNTPD_OLD;
2716 goto again;
2719 if (res != 0)
2720 return;
2722 if (!checkitems(items, fp))
2723 return;
2725 if (!checkitemsize(itemsize, sizeof(struct info_clock)))
2726 return;
2728 while (items-- > 0) {
2729 (void) fprintf(fp, "clock address: %s\n",
2730 numtoa(cl->clockadr));
2731 for (clk = clktypes; clk->code >= 0; clk++)
2732 if (clk->code == cl->type)
2733 break;
2734 if (clk->code >= 0)
2735 (void) fprintf(fp, "clock type: %s\n",
2736 clk->clocktype);
2737 else
2738 (void) fprintf(fp, "clock type: unknown type (%d)\n",
2739 cl->type);
2740 (void) fprintf(fp, "last event: %d\n",
2741 cl->lastevent);
2742 (void) fprintf(fp, "current status: %d\n",
2743 cl->currentstatus);
2744 (void) fprintf(fp, "number of polls: %lu\n",
2745 (u_long)ntohl(cl->polls));
2746 (void) fprintf(fp, "no response to poll: %lu\n",
2747 (u_long)ntohl(cl->noresponse));
2748 (void) fprintf(fp, "bad format responses: %lu\n",
2749 (u_long)ntohl(cl->badformat));
2750 (void) fprintf(fp, "bad data responses: %lu\n",
2751 (u_long)ntohl(cl->baddata));
2752 (void) fprintf(fp, "running time: %lu\n",
2753 (u_long)ntohl(cl->timestarted));
2754 NTOHL_FP(&cl->fudgetime1, &ts);
2755 (void) fprintf(fp, "fudge time 1: %s\n",
2756 lfptoa(&ts, 6));
2757 NTOHL_FP(&cl->fudgetime2, &ts);
2758 (void) fprintf(fp, "fudge time 2: %s\n",
2759 lfptoa(&ts, 6));
2760 (void) fprintf(fp, "stratum: %ld\n",
2761 (u_long)ntohl(cl->fudgeval1));
2762 (void) fprintf(fp, "reference ID: %s\n",
2763 refid_string(ntohl(cl->fudgeval2), 0));
2764 (void) fprintf(fp, "fudge flags: 0x%x\n",
2765 cl->flags);
2767 if (items > 0)
2768 (void) fprintf(fp, "\n");
2769 cl++;
2775 * fudge - set clock fudge factors
2777 static void
2778 fudge(
2779 struct parse *pcmd,
2780 FILE *fp
2783 struct conf_fudge fudgedata;
2784 int items;
2785 int itemsize;
2786 char *dummy;
2787 l_fp ts;
2788 int res;
2789 long val;
2790 u_long u_val;
2791 int err;
2794 err = 0;
2795 memset((char *)&fudgedata, 0, sizeof fudgedata);
2796 fudgedata.clockadr = GET_INADDR(pcmd->argval[0].netnum);
2798 if (STREQ(pcmd->argval[1].string, "time1")) {
2799 fudgedata.which = htonl(FUDGE_TIME1);
2800 if (!atolfp(pcmd->argval[2].string, &ts))
2801 err = 1;
2802 else
2803 NTOHL_FP(&ts, &fudgedata.fudgetime);
2804 } else if (STREQ(pcmd->argval[1].string, "time2")) {
2805 fudgedata.which = htonl(FUDGE_TIME2);
2806 if (!atolfp(pcmd->argval[2].string, &ts))
2807 err = 1;
2808 else
2809 NTOHL_FP(&ts, &fudgedata.fudgetime);
2810 } else if (STREQ(pcmd->argval[1].string, "val1")) {
2811 fudgedata.which = htonl(FUDGE_VAL1);
2812 if (!atoint(pcmd->argval[2].string, &val))
2813 err = 1;
2814 else
2815 fudgedata.fudgeval_flags = htonl(val);
2816 } else if (STREQ(pcmd->argval[1].string, "val2")) {
2817 fudgedata.which = htonl(FUDGE_VAL2);
2818 if (!atoint(pcmd->argval[2].string, &val))
2819 err = 1;
2820 else
2821 fudgedata.fudgeval_flags = htonl((u_int32)val);
2822 } else if (STREQ(pcmd->argval[1].string, "flags")) {
2823 fudgedata.which = htonl(FUDGE_FLAGS);
2824 if (!hextoint(pcmd->argval[2].string, &u_val))
2825 err = 1;
2826 else
2827 fudgedata.fudgeval_flags = htonl((u_int32)(u_val & 0xf));
2828 } else {
2829 (void) fprintf(stderr, "What fudge is %s?\n",
2830 pcmd->argval[1].string);
2831 return;
2834 if (err) {
2835 (void) fprintf(stderr, "Unknown fudge parameter %s\n",
2836 pcmd->argval[2].string);
2837 return;
2840 again:
2841 res = doquery(impl_ver, REQ_SET_CLKFUDGE, 1, 1,
2842 sizeof(struct conf_fudge), (char *)&fudgedata, &items,
2843 &itemsize, &dummy, 0, sizeof(dummy));
2845 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2846 impl_ver = IMPL_XNTPD_OLD;
2847 goto again;
2850 if (res == 0)
2851 (void) fprintf(fp, "done!\n");
2852 return;
2856 * clkbug - get and print clock debugging information
2858 static void
2859 clkbug(
2860 struct parse *pcmd,
2861 FILE *fp
2864 register int i;
2865 register int n;
2866 register u_int32 s;
2867 struct info_clkbug *cl;
2868 /* 8 is the maximum number of clocks which will fit in a packet */
2869 u_long clist[min(MAXARGS, 8)];
2870 u_int32 ltemp;
2871 int qitems;
2872 int items;
2873 int itemsize;
2874 int res;
2875 int needsp;
2876 l_fp ts;
2878 for (qitems = 0; qitems < min(pcmd->nargs, 8); qitems++)
2879 clist[qitems] = GET_INADDR(pcmd->argval[qitems].netnum);
2881 again:
2882 res = doquery(impl_ver, REQ_GET_CLKBUGINFO, 0, qitems,
2883 sizeof(u_int32), (char *)clist, &items,
2884 &itemsize, (void *)&cl, 0, sizeof(struct info_clkbug));
2886 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2887 impl_ver = IMPL_XNTPD_OLD;
2888 goto again;
2891 if (res != 0)
2892 return;
2894 if (!checkitems(items, fp))
2895 return;
2897 if (!checkitemsize(itemsize, sizeof(struct info_clkbug)))
2898 return;
2900 while (items-- > 0) {
2901 (void) fprintf(fp, "clock address: %s\n",
2902 numtoa(cl->clockadr));
2903 n = (int)cl->nvalues;
2904 (void) fprintf(fp, "values: %d", n);
2905 s = ntohs(cl->svalues);
2906 if (n > NUMCBUGVALUES)
2907 n = NUMCBUGVALUES;
2908 for (i = 0; i < n; i++) {
2909 ltemp = ntohl(cl->values[i]);
2910 ltemp &= 0xffffffff; /* HMS: This does nothing now */
2911 if ((i & 0x3) == 0)
2912 (void) fprintf(fp, "\n");
2913 if (s & (1 << i))
2914 (void) fprintf(fp, "%12ld", (u_long)ltemp);
2915 else
2916 (void) fprintf(fp, "%12lu", (u_long)ltemp);
2918 (void) fprintf(fp, "\n");
2920 n = (int)cl->ntimes;
2921 (void) fprintf(fp, "times: %d", n);
2922 s = ntohl(cl->stimes);
2923 if (n > NUMCBUGTIMES)
2924 n = NUMCBUGTIMES;
2925 needsp = 0;
2926 for (i = 0; i < n; i++) {
2927 if ((i & 0x1) == 0) {
2928 (void) fprintf(fp, "\n");
2929 } else {
2930 for (;needsp > 0; needsp--)
2931 putc(' ', fp);
2933 NTOHL_FP(&cl->times[i], &ts);
2934 if (s & (1 << i)) {
2935 (void) fprintf(fp, "%17s",
2936 lfptoa(&ts, 6));
2937 needsp = 22;
2938 } else {
2939 (void) fprintf(fp, "%37s",
2940 uglydate(&ts));
2941 needsp = 2;
2944 (void) fprintf(fp, "\n");
2945 if (items > 0) {
2946 cl++;
2947 (void) fprintf(fp, "\n");
2954 * kerninfo - display the kernel pll/pps variables
2956 static void
2957 kerninfo(
2958 struct parse *pcmd,
2959 FILE *fp
2962 struct info_kernel *ik;
2963 int items;
2964 int itemsize;
2965 int res;
2966 unsigned status;
2967 double tscale = 1e-6;
2969 again:
2970 res = doquery(impl_ver, REQ_GET_KERNEL, 0, 0, 0, (char *)NULL,
2971 &items, &itemsize, (void *)&ik, 0,
2972 sizeof(struct info_kernel));
2974 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2975 impl_ver = IMPL_XNTPD_OLD;
2976 goto again;
2979 if (res != 0)
2980 return;
2981 if (!check1item(items, fp))
2982 return;
2983 if (!checkitemsize(itemsize, sizeof(struct info_kernel)))
2984 return;
2986 status = ntohs(ik->status) & 0xffff;
2988 * pll variables. We know more than we should about the NANO bit.
2990 #ifdef STA_NANO
2991 if (status & STA_NANO)
2992 tscale = 1e-9;
2993 #endif
2994 (void)fprintf(fp, "pll offset: %g s\n",
2995 (int32_t)ntohl(ik->offset) * tscale);
2996 (void)fprintf(fp, "pll frequency: %s ppm\n",
2997 fptoa((s_fp)ntohl(ik->freq), 3));
2998 (void)fprintf(fp, "maximum error: %g s\n",
2999 (u_long)ntohl(ik->maxerror) * 1e-6);
3000 (void)fprintf(fp, "estimated error: %g s\n",
3001 (u_long)ntohl(ik->esterror) * 1e-6);
3002 (void)fprintf(fp, "status: %04x ", status);
3003 #ifdef STA_PLL
3004 if (status & STA_PLL) (void)fprintf(fp, " pll");
3005 #endif
3006 #ifdef STA_PPSFREQ
3007 if (status & STA_PPSFREQ) (void)fprintf(fp, " ppsfreq");
3008 #endif
3009 #ifdef STA_PPSTIME
3010 if (status & STA_PPSTIME) (void)fprintf(fp, " ppstime");
3011 #endif
3012 #ifdef STA_FLL
3013 if (status & STA_FLL) (void)fprintf(fp, " fll");
3014 #endif
3015 #ifdef STA_INS
3016 if (status & STA_INS) (void)fprintf(fp, " ins");
3017 #endif
3018 #ifdef STA_DEL
3019 if (status & STA_DEL) (void)fprintf(fp, " del");
3020 #endif
3021 #ifdef STA_UNSYNC
3022 if (status & STA_UNSYNC) (void)fprintf(fp, " unsync");
3023 #endif
3024 #ifdef STA_FREQHOLD
3025 if (status & STA_FREQHOLD) (void)fprintf(fp, " freqhold");
3026 #endif
3027 #ifdef STA_PPSSIGNAL
3028 if (status & STA_PPSSIGNAL) (void)fprintf(fp, " ppssignal");
3029 #endif
3030 #ifdef STA_PPSJITTER
3031 if (status & STA_PPSJITTER) (void)fprintf(fp, " ppsjitter");
3032 #endif
3033 #ifdef STA_PPSWANDER
3034 if (status & STA_PPSWANDER) (void)fprintf(fp, " ppswander");
3035 #endif
3036 #ifdef STA_PPSERROR
3037 if (status & STA_PPSERROR) (void)fprintf(fp, " ppserror");
3038 #endif
3039 #ifdef STA_CLOCKERR
3040 if (status & STA_CLOCKERR) (void)fprintf(fp, " clockerr");
3041 #endif
3042 #ifdef STA_NANO
3043 if (status & STA_NANO) (void)fprintf(fp, " nano");
3044 #endif
3045 #ifdef STA_MODE
3046 if (status & STA_MODE) (void)fprintf(fp, " mode=fll");
3047 #endif
3048 #ifdef STA_CLK
3049 if (status & STA_CLK) (void)fprintf(fp, " src=B");
3050 #endif
3051 (void)fprintf(fp, "\n");
3052 (void)fprintf(fp, "pll time constant: %ld\n",
3053 (u_long)ntohl(ik->constant));
3054 (void)fprintf(fp, "precision: %g s\n",
3055 (u_long)ntohl(ik->precision) * tscale);
3056 (void)fprintf(fp, "frequency tolerance: %s ppm\n",
3057 fptoa((s_fp)ntohl(ik->tolerance), 0));
3060 * For backwards compatibility (ugh), we find the pps variables
3061 * only if the shift member is nonzero.
3063 if (!ik->shift)
3064 return;
3067 * pps variables
3069 (void)fprintf(fp, "pps frequency: %s ppm\n",
3070 fptoa((s_fp)ntohl(ik->ppsfreq), 3));
3071 (void)fprintf(fp, "pps stability: %s ppm\n",
3072 fptoa((s_fp)ntohl(ik->stabil), 3));
3073 (void)fprintf(fp, "pps jitter: %g s\n",
3074 (u_long)ntohl(ik->jitter) * tscale);
3075 (void)fprintf(fp, "calibration interval: %d s\n",
3076 1 << ntohs(ik->shift));
3077 (void)fprintf(fp, "calibration cycles: %ld\n",
3078 (u_long)ntohl(ik->calcnt));
3079 (void)fprintf(fp, "jitter exceeded: %ld\n",
3080 (u_long)ntohl(ik->jitcnt));
3081 (void)fprintf(fp, "stability exceeded: %ld\n",
3082 (u_long)ntohl(ik->stbcnt));
3083 (void)fprintf(fp, "calibration errors: %ld\n",
3084 (u_long)ntohl(ik->errcnt));
3087 #define IF_LIST_FMT "%2d %c %48s %c %c %12.12s %03x %3d %2d %5d %5d %5d %2d %2d %3d %7d\n"
3088 #define IF_LIST_FMT_STR "%2s %c %48s %c %c %12.12s %3s %3s %2s %5s %5s %5s %2s %2s %3s %7s\n"
3089 #define IF_LIST_AFMT_STR " %48s %c\n"
3090 #define IF_LIST_LABELS "#", 'A', "Address/Mask/Broadcast", 'T', 'E', "IF name", "Flg", "TL", "#M", "recv", "sent", "drop", "S", "IX", "PC", "uptime"
3091 #define IF_LIST_LINE "=====================================================================================================================\n"
3093 static void
3094 iflist(
3095 FILE *fp,
3096 struct info_if_stats *ifs,
3097 int items,
3098 int itemsize,
3099 int res
3102 static char *actions = "?.+-";
3103 struct sockaddr_storage saddr;
3105 if (res != 0)
3106 return;
3108 if (!checkitems(items, fp))
3109 return;
3111 if (!checkitemsize(itemsize, sizeof(struct info_if_stats)))
3112 return;
3114 fprintf(fp, IF_LIST_FMT_STR, IF_LIST_LABELS);
3115 fprintf(fp, IF_LIST_LINE);
3117 while (items > 0) {
3118 if (ntohl(ifs->v6_flag)) {
3119 memcpy((char *)&GET_INADDR6(saddr), (char *)&ifs->unaddr.addr6, sizeof(ifs->unaddr.addr6));
3120 saddr.ss_family = AF_INET6;
3121 } else {
3122 memcpy((char *)&GET_INADDR(saddr), (char *)&ifs->unaddr.addr, sizeof(ifs->unaddr.addr));
3123 saddr.ss_family = AF_INET;
3125 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
3126 saddr.ss_len = SOCKLEN(&saddr);
3127 #endif
3128 fprintf(fp, IF_LIST_FMT,
3129 ntohl(ifs->ifnum),
3130 actions[(ifs->action >= 1 && ifs->action < 4) ? ifs->action : 0],
3131 stoa((&saddr)), 'A',
3132 ifs->ignore_packets ? 'D' : 'E',
3133 ifs->name,
3134 ntohl(ifs->flags),
3135 ntohl(ifs->last_ttl),
3136 ntohl(ifs->num_mcast),
3137 ntohl(ifs->received),
3138 ntohl(ifs->sent),
3139 ntohl(ifs->notsent),
3140 ntohl(ifs->scopeid),
3141 ntohl(ifs->ifindex),
3142 ntohl(ifs->peercnt),
3143 ntohl(ifs->uptime));
3145 if (ntohl(ifs->v6_flag)) {
3146 memcpy((char *)&GET_INADDR6(saddr), (char *)&ifs->unmask.addr6, sizeof(ifs->unmask.addr6));
3147 saddr.ss_family = AF_INET6;
3148 } else {
3149 memcpy((char *)&GET_INADDR(saddr), (char *)&ifs->unmask.addr, sizeof(ifs->unmask.addr));
3150 saddr.ss_family = AF_INET;
3152 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
3153 saddr.ss_len = SOCKLEN(&saddr);
3154 #endif
3155 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'M');
3157 if (!ntohl(ifs->v6_flag) && ntohl(ifs->flags) & (INT_BCASTOPEN)) {
3158 memcpy((char *)&GET_INADDR(saddr), (char *)&ifs->unbcast.addr, sizeof(ifs->unbcast.addr));
3159 saddr.ss_family = AF_INET;
3160 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
3161 saddr.ss_len = SOCKLEN(&saddr);
3162 #endif
3163 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'B');
3167 ifs++;
3168 items--;
3172 /*ARGSUSED*/
3173 static void
3174 get_if_stats(
3175 struct parse *pcmd,
3176 FILE *fp
3179 struct info_if_stats *ifs;
3180 int items;
3181 int itemsize;
3182 int res;
3184 res = doquery(impl_ver, REQ_IF_STATS, 1, 0, 0, (char *)NULL, &items,
3185 &itemsize, (void *)&ifs, 0,
3186 sizeof(struct info_if_stats));
3187 iflist(fp, ifs, items, itemsize, res);
3190 /*ARGSUSED*/
3191 static void
3192 do_if_reload(
3193 struct parse *pcmd,
3194 FILE *fp
3197 struct info_if_stats *ifs;
3198 int items;
3199 int itemsize;
3200 int res;
3202 res = doquery(impl_ver, REQ_IF_RELOAD, 1, 0, 0, (char *)NULL, &items,
3203 &itemsize, (void *)&ifs, 0,
3204 sizeof(struct info_if_stats));
3205 iflist(fp, ifs, items, itemsize, res);