2 * ntpdc_ops.c - subroutines which are called to perform operations by
15 #include "ntp_control.h"
16 #include "ntp_refclock.h"
17 #include "ntp_stdlib.h"
20 #ifdef HAVE_SYS_TIMEX_H
21 # include <sys/timex.h>
23 #if !defined(__bsdi__) && !defined(apollo)
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
29 #include <arpa/inet.h>
34 static int checkitems (size_t, FILE *);
35 static int checkitemsize (size_t, size_t);
36 static int check1item (size_t, FILE *);
39 * Declarations for command handlers in here
41 static void peerlist (struct parse *, FILE *);
42 static void peers (struct parse *, FILE *);
43 static void doconfig (struct parse *pcmd, FILE *fp, int mode, int refc);
44 static void dmpeers (struct parse *, FILE *);
45 static void dopeers (struct parse *, FILE *, int);
46 static void printpeer (struct info_peer *, FILE *);
47 static void showpeer (struct parse *, FILE *);
48 static void peerstats (struct parse *, FILE *);
49 static void loopinfo (struct parse *, FILE *);
50 static void sysinfo (struct parse *, FILE *);
51 static void sysstats (struct parse *, FILE *);
52 static void iostats (struct parse *, FILE *);
53 static void memstats (struct parse *, FILE *);
54 static void timerstats (struct parse *, FILE *);
55 static void addpeer (struct parse *, FILE *);
56 static void addserver (struct parse *, FILE *);
57 static void addrefclock (struct parse *, FILE *);
58 static void broadcast (struct parse *, FILE *);
59 static void doconfig (struct parse *, FILE *, int, int);
60 static void unconfig (struct parse *, FILE *);
61 static void set (struct parse *, FILE *);
62 static void sys_clear (struct parse *, FILE *);
63 static void doset (struct parse *, FILE *, int);
64 static void reslist (struct parse *, FILE *);
65 static void new_restrict (struct parse *, FILE *);
66 static void unrestrict (struct parse *, FILE *);
67 static void delrestrict (struct parse *, FILE *);
68 static void do_restrict (struct parse *, FILE *, int);
69 static void monlist (struct parse *, FILE *);
70 static void reset (struct parse *, FILE *);
71 static void preset (struct parse *, FILE *);
72 static void readkeys (struct parse *, FILE *);
73 static void trustkey (struct parse *, FILE *);
74 static void untrustkey (struct parse *, FILE *);
75 static void do_trustkey (struct parse *, FILE *, int);
76 static void authinfo (struct parse *, FILE *);
77 static void traps (struct parse *, FILE *);
78 static void addtrap (struct parse *, FILE *);
79 static void clrtrap (struct parse *, FILE *);
80 static void do_addclr_trap (struct parse *, FILE *, int);
81 static void requestkey (struct parse *, FILE *);
82 static void controlkey (struct parse *, FILE *);
83 static void do_changekey (struct parse *, FILE *, int);
84 static void ctlstats (struct parse *, FILE *);
85 static void clockstat (struct parse *, FILE *);
86 static void fudge (struct parse *, FILE *);
87 static void clkbug (struct parse *, FILE *);
88 static void kerninfo (struct parse *, FILE *);
89 static void get_if_stats (struct parse *, FILE *);
90 static void do_if_reload (struct parse *, FILE *);
93 * Commands we understand. Ntpdc imports this.
95 struct xcmd opcmds[] = {
96 { "listpeers", peerlist, { OPT|IP_VERSION, NO, NO, NO },
97 { "-4|-6", "", "", "" },
98 "display list of peers the server knows about [IP Version]" },
99 { "peers", peers, { OPT|IP_VERSION, NO, NO, NO },
100 { "-4|-6", "", "", "" },
101 "display peer summary information [IP Version]" },
102 { "dmpeers", dmpeers, { OPT|IP_VERSION, NO, NO, NO },
103 { "-4|-6", "", "", "" },
104 "display peer summary info the way Dave Mills likes it (IP Version)" },
105 { "showpeer", showpeer, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD},
106 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
107 "display detailed information for one or more peers" },
108 { "pstats", peerstats, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
109 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
110 "display statistical information for one or more peers" },
111 { "loopinfo", loopinfo, { OPT|NTP_STR, NO, NO, NO },
112 { "oneline|multiline", "", "", "" },
113 "display loop filter information" },
114 { "sysinfo", sysinfo, { NO, NO, NO, NO },
116 "display local server information" },
117 { "sysstats", sysstats, { NO, NO, NO, NO },
119 "display local server statistics" },
120 { "memstats", memstats, { NO, NO, NO, NO },
122 "display peer memory usage statistics" },
123 { "iostats", iostats, { NO, NO, NO, NO },
125 "display I/O subsystem statistics" },
126 { "timerstats", timerstats, { NO, NO, NO, NO },
128 "display event timer subsystem statistics" },
129 { "addpeer", addpeer, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
130 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
131 "configure a new peer association" },
132 { "addserver", addserver, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
133 { "addr", "keyid", "version", "minpoll#|prefer|burst|iburst|'minpoll N'|'maxpoll N'|'keyid N'|'version N' ..." },
134 "configure a new server" },
135 { "addrefclock",addrefclock, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_STR, OPT|NTP_STR },
136 { "addr", "mode", "minpoll|prefer", "minpoll|prefer" },
137 "configure a new server" },
138 { "broadcast", broadcast, { NTP_ADD, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
139 { "addr", "keyid", "version", "minpoll" },
140 "configure broadcasting time service" },
141 { "unconfig", unconfig, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
142 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
143 "unconfigure existing peer assocations" },
144 { "enable", set, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
145 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
146 "set a system flag (auth, bclient, monitor, pll, kernel, stats)" },
147 { "disable", sys_clear, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
148 { "auth|bclient|monitor|pll|kernel|stats", "...", "...", "..." },
149 "clear a system flag (auth, bclient, monitor, pll, kernel, stats)" },
150 { "reslist", reslist, {OPT|IP_VERSION, NO, NO, NO },
151 { "-4|-6", "", "", "" },
152 "display the server's restrict list" },
153 { "restrict", new_restrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
155 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
157 "create restrict entry/add flags to entry" },
158 { "unrestrict", unrestrict, { NTP_ADD, NTP_ADD, NTP_STR, OPT|NTP_STR },
160 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
162 "remove flags from a restrict entry" },
163 { "delrestrict", delrestrict, { NTP_ADD, NTP_ADD, OPT|NTP_STR, NO },
164 { "address", "mask", "ntpport", "" },
165 "delete a restrict entry" },
166 { "monlist", monlist, { OPT|NTP_INT, NO, NO, NO },
167 { "version", "", "", "" },
168 "display data the server's monitor routines have collected" },
169 { "reset", reset, { NTP_STR, OPT|NTP_STR, OPT|NTP_STR, OPT|NTP_STR },
170 { "io|sys|mem|timer|auth|ctl|allpeers", "...", "...", "..." },
171 "reset various subsystem statistics counters" },
172 { "preset", preset, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
173 { "peer_address", "peer2_addr", "peer3_addr", "peer4_addr" },
174 "reset stat counters associated with particular peer(s)" },
175 { "readkeys", readkeys, { NO, NO, NO, NO },
177 "request a reread of the keys file and re-init of system keys" },
178 { "trustedkey", trustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
179 { "keyid", "keyid", "keyid", "keyid" },
180 "add one or more key ID's to the trusted list" },
181 { "untrustedkey", untrustkey, { NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT, OPT|NTP_UINT },
182 { "keyid", "keyid", "keyid", "keyid" },
183 "remove one or more key ID's from the trusted list" },
184 { "authinfo", authinfo, { NO, NO, NO, NO },
186 "display the state of the authentication code" },
187 { "traps", traps, { NO, NO, NO, NO },
189 "display the traps set in the server" },
190 { "addtrap", addtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
191 { "address", "port", "interface", "" },
192 "configure a trap in the server" },
193 { "clrtrap", clrtrap, { NTP_ADD, OPT|NTP_UINT, OPT|NTP_ADD, NO },
194 { "address", "port", "interface", "" },
195 "remove a trap (configured or otherwise) from the server" },
196 { "requestkey", requestkey, { NTP_UINT, NO, NO, NO },
197 { "keyid", "", "", "" },
198 "change the keyid the server uses to authenticate requests" },
199 { "controlkey", controlkey, { NTP_UINT, NO, NO, NO },
200 { "keyid", "", "", "" },
201 "change the keyid the server uses to authenticate control messages" },
202 { "ctlstats", ctlstats, { NO, NO, NO, NO },
204 "display packet count statistics from the control module" },
205 { "clockstat", clockstat, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
206 { "address", "address", "address", "address" },
207 "display clock status information" },
208 { "fudge", fudge, { NTP_ADD, NTP_STR, NTP_STR, NO },
209 { "address", "time1|time2|val1|val2|flags", "value", "" },
210 "set/change one of a clock's fudge factors" },
211 { "clkbug", clkbug, { NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD, OPT|NTP_ADD },
212 { "address", "address", "address", "address" },
213 "display clock debugging information" },
214 { "kerninfo", kerninfo, { NO, NO, NO, NO },
216 "display the kernel pll/pps variables" },
217 { "ifstats", get_if_stats, { NO, NO, NO, NO },
219 "list interface statistics" },
220 { "ifreload", do_if_reload, { NO, NO, NO, NO },
222 "reload interface configuration" },
223 { 0, 0, { NO, NO, NO, NO },
224 { "", "", "", "" }, "" }
228 * For quick string comparisons
230 #define STREQ(a, b) (*(a) == *(b) && strcmp((a), (b)) == 0)
233 * SET_SS_LEN_IF_PRESENT - used by SET_ADDR, SET_ADDRS macros
236 #ifdef ISC_PLATFORM_HAVESALEN
237 #define SET_SS_LEN_IF_PRESENT(psau) \
239 (psau)->sa.sa_len = SOCKLEN(psau); \
242 #define SET_SS_LEN_IF_PRESENT(psau) do { } while (0)
246 * SET_ADDR - setup address for v4/v6 as needed
248 #define SET_ADDR(address, v6flag, v4addr, v6addr) \
252 AF(&(address)) = AF_INET6; \
253 SOCK_ADDR6(&(address)) = (v6addr); \
255 AF(&(address)) = AF_INET; \
256 NSRCADR(&(address)) = (v4addr); \
258 SET_SS_LEN_IF_PRESENT(&(address)); \
263 * SET_ADDRS - setup source and destination addresses for
266 #define SET_ADDRS(a1, a2, info, a1prefix, a2prefix) \
270 if ((info)->v6_flag) { \
271 AF(&(a1)) = AF_INET6; \
272 AF(&(a2)) = AF_INET6; \
273 SOCK_ADDR6(&(a1)) = (info)->a1prefix##6; \
274 SOCK_ADDR6(&(a2)) = (info)->a2prefix##6; \
276 AF(&(a1)) = AF_INET; \
277 AF(&(a2)) = AF_INET; \
278 NSRCADR(&(a1)) = (info)->a1prefix; \
279 NSRCADR(&(a2)) = (info)->a2prefix; \
281 SET_SS_LEN_IF_PRESENT(&(a1)); \
282 SET_SS_LEN_IF_PRESENT(&(a2)); \
287 * checkitems - utility to print a message if no items were returned
296 (void) fprintf(fp, "No data returned in response to query\n");
304 * checkitemsize - utility to print a message if the item size is wrong
312 if (itemsize != expected) {
313 (void) fprintf(stderr,
314 "***Incorrect item size returned by remote host (%lu should be %lu)\n",
315 (u_long)itemsize, (u_long)expected);
323 * check1item - check to make sure we have exactly one item
332 (void) fprintf(fp, "No data returned in response to query\n");
336 (void) fprintf(fp, "Expected one item in response, got %lu\n",
345 * peerlist - get a short list of peers
354 struct info_peer_list *plist;
361 res = doquery(impl_ver, REQ_PEER_LIST, 0, 0, 0, (char *)NULL, &items,
362 &itemsize, (void *)&plist, 0,
363 sizeof(struct info_peer_list));
365 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
366 impl_ver = IMPL_XNTPD_OLD;
373 if (!checkitems(items, fp))
376 if (!checkitemsize(itemsize, sizeof(struct info_peer_list)) &&
377 !checkitemsize(itemsize, v4sizeof(struct info_peer_list)))
381 SET_ADDR(paddr, plist->v6_flag, plist->addr, plist->addr6);
382 if ((pcmd->nargs == 0) ||
383 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
384 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
386 const char *strhost = nntohost(&paddr);
387 const char *straddr = stoa(&paddr);
388 (void) fprintf(fp, "%-12s %s",
389 modetoa(plist->hmode), strhost);
390 if (strcmp(strhost,straddr))
391 (void) fprintf(fp, " (%s)\n", straddr);
393 (void) fprintf(fp, "\n");
402 * peers - show peer summary
410 dopeers(pcmd, fp, 0);
414 * dmpeers - show peer summary, Dave Mills style
422 dopeers(pcmd, fp, 1);
427 * peers - show peer summary
437 struct info_peer_summary *plist;
448 res = doquery(impl_ver, REQ_PEER_LIST_SUM, 0, 0, 0, (char *)NULL,
449 &items, &itemsize, (void *)&plist, 0,
450 sizeof(struct info_peer_summary));
452 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
453 impl_ver = IMPL_XNTPD_OLD;
460 if (!checkitems(items, fp))
463 if (!checkitemsize(itemsize, sizeof(struct info_peer_summary)) &&
464 !checkitemsize(itemsize, v4sizeof(struct info_peer_summary)))
468 " remote local st poll reach delay offset disp\n");
470 "=======================================================================\n");
473 if (plist->flags & INFO_FLAG_SYSPEER)
475 else if (plist->hmode == MODE_ACTIVE)
477 else if (plist->hmode == MODE_PASSIVE)
479 else if (plist->hmode == MODE_CLIENT)
481 else if (plist->hmode == MODE_BROADCAST)
483 else if (plist->hmode == MODE_BCLIENT)
488 if (plist->flags & INFO_FLAG_SYSPEER)
490 else if (plist->flags & INFO_FLAG_SHORTLIST)
492 else if (plist->flags & INFO_FLAG_SEL_CANDIDATE)
497 NTOHL_FP(&(plist->offset), &tempts);
498 ntp_poll = 1<<max(min3(plist->ppoll, plist->hpoll, NTP_MAXPOLL),
500 SET_ADDRS(dstadr, srcadr, plist, dstadr, srcadr);
501 if ((pcmd->nargs == 0) ||
502 ((pcmd->argval->ival == 6) && (plist->v6_flag != 0)) ||
503 ((pcmd->argval->ival == 4) && (plist->v6_flag == 0)))
505 "%c%-15.15s %-15.15s %2u %4d %3o %7.7s %9.9s %7.7s\n",
506 c, nntohost(&srcadr), stoa(&dstadr),
507 plist->stratum, ntp_poll, plist->reach,
508 fptoa(NTOHS_FP(plist->delay), 5),
510 ufptoa(NTOHS_FP(plist->dispersion), 5));
516 /* Convert a refid & stratum (in host order) to a string */
526 memcpy(junk, &refid, 4);
530 return numtoa(refid);
539 static const char none[] = "";
540 static const char comma[] = ",";
544 fprintf(fp, " none\n");
548 if (flags & INFO_FLAG_SYSPEER) {
549 fprintf(fp, " system_peer");
552 if (flags & INFO_FLAG_CONFIG) {
553 fprintf(fp, "%s config", dlim);
556 if (flags & INFO_FLAG_REFCLOCK) {
557 fprintf(fp, "%s refclock", dlim);
560 if (flags & INFO_FLAG_AUTHENABLE) {
561 fprintf(fp, "%s auth", dlim);
564 if (flags & INFO_FLAG_PREFER) {
565 fprintf(fp, "%s prefer", dlim);
568 if (flags & INFO_FLAG_IBURST) {
569 fprintf(fp, "%s iburst", dlim);
572 if (flags & INFO_FLAG_BURST) {
573 fprintf(fp, "%s burst", dlim);
576 if (flags & INFO_FLAG_SEL_CANDIDATE) {
577 fprintf(fp, "%s candidate", dlim);
580 if (flags & INFO_FLAG_SHORTLIST) {
581 fprintf(fp, "%s shortlist", dlim);
587 * printpeer - print detail information for a peer
591 register struct info_peer *pp,
597 sockaddr_u srcadr, dstadr;
599 SET_ADDRS(dstadr, srcadr, pp, dstadr, srcadr);
601 (void) fprintf(fp, "remote %s, local %s\n",
602 stoa(&srcadr), stoa(&dstadr));
603 (void) fprintf(fp, "hmode %s, pmode %s, stratum %d, precision %d\n",
604 modetoa(pp->hmode), modetoa(pp->pmode),
605 pp->stratum, pp->precision);
608 "leap %c%c, refid [%s], rootdistance %s, rootdispersion %s\n",
609 pp->leap & 0x2 ? '1' : '0',
610 pp->leap & 0x1 ? '1' : '0',
611 refid_string(pp->refid,
612 (pp->flags & INFO_FLAG_REFCLOCK ? 0 : pp->stratum)),
613 fptoa(NTOHS_FP(pp->rootdelay), 5),
614 ufptoa(NTOHS_FP(pp->rootdispersion), 5));
617 "ppoll %d, hpoll %d, keyid %lu, version %d, association %u\n",
618 pp->ppoll, pp->hpoll, (u_long)pp->keyid, pp->version, ntohs(pp->associd));
621 "reach %03o, unreach %d, flash 0x%04x, ",
622 pp->reach, pp->unreach, pp->flash2);
624 (void) fprintf(fp, "boffset %s, ttl/mode %d\n",
625 fptoa(NTOHS_FP(pp->estbdelay), 5), pp->ttl);
627 (void) fprintf(fp, "timer %lds, flags", (long)ntohl(pp->timer));
628 print_pflag(fp, pp->flags);
630 NTOHL_FP(&pp->reftime, &tempts);
631 (void) fprintf(fp, "reference time: %s\n",
632 prettydate(&tempts));
633 NTOHL_FP(&pp->org, &tempts);
634 (void) fprintf(fp, "originate timestamp: %s\n",
635 prettydate(&tempts));
636 NTOHL_FP(&pp->rec, &tempts);
637 (void) fprintf(fp, "receive timestamp: %s\n",
638 prettydate(&tempts));
639 NTOHL_FP(&pp->xmt, &tempts);
640 (void) fprintf(fp, "transmit timestamp: %s\n",
641 prettydate(&tempts));
643 (void) fprintf(fp, "filter delay: ");
644 for (i = 0; i < NTP_SHIFT; i++) {
645 (void) fprintf(fp, " %-8.8s",
646 fptoa(NTOHS_FP(pp->filtdelay[i]), 5));
647 if (i == (NTP_SHIFT>>1)-1)
648 (void) fprintf(fp, "\n ");
650 (void) fprintf(fp, "\n");
652 (void) fprintf(fp, "filter offset:");
653 for (i = 0; i < NTP_SHIFT; i++) {
654 NTOHL_FP(&pp->filtoffset[i], &tempts);
655 (void) fprintf(fp, " %-8.8s", lfptoa(&tempts, 6));
656 if (i == (NTP_SHIFT>>1)-1)
657 (void) fprintf(fp, "\n ");
659 (void) fprintf(fp, "\n");
661 (void) fprintf(fp, "filter order: ");
662 for (i = 0; i < NTP_SHIFT; i++) {
663 (void) fprintf(fp, " %-8d", pp->order[i]);
664 if (i == (NTP_SHIFT>>1)-1)
665 (void) fprintf(fp, "\n ");
667 (void) fprintf(fp, "\n");
670 NTOHL_FP(&pp->offset, &tempts);
672 "offset %s, delay %s, error bound %s, filter error %s\n",
673 lfptoa(&tempts, 6), fptoa(NTOHS_FP(pp->delay), 5),
674 ufptoa(NTOHS_FP(pp->dispersion), 5),
675 ufptoa(NTOHS_FP(pp->selectdisp), 5));
680 * showpeer - show detailed information for a peer
688 struct info_peer *pp;
689 /* 4 is the maximum number of peers which will fit in a packet */
690 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
699 if (impl_ver == IMPL_XNTPD)
700 sendsize = sizeof(struct info_peer_list);
702 sendsize = v4sizeof(struct info_peer_list);
704 qitemlim = min(pcmd->nargs, COUNTOF(plist));
705 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
706 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
707 pl->addr = NSRCADR(&pcmd->argval[qitems].netnum);
708 if (impl_ver == IMPL_XNTPD)
711 if (impl_ver == IMPL_XNTPD_OLD) {
713 "***Server doesn't understand IPv6 addresses\n");
716 pl->addr6 = SOCK_ADDR6(&pcmd->argval[qitems].netnum);
719 pl->port = (u_short)s_port;
720 pl->hmode = pl->flags = 0;
721 pl = (void *)((char *)pl + sendsize);
724 res = doquery(impl_ver, REQ_PEER_INFO, 0, qitems,
725 sendsize, (char *)plist, &items,
726 &itemsize, (void *)&pp, 0, sizeof(struct info_peer));
728 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
729 impl_ver = IMPL_XNTPD_OLD;
736 if (!checkitems(items, fp))
739 if (!checkitemsize(itemsize, sizeof(struct info_peer)) &&
740 !checkitemsize(itemsize, v4sizeof(struct info_peer)))
743 while (items-- > 0) {
753 * peerstats - return statistics for a peer
761 struct info_peer_stats *pp;
762 /* 4 is the maximum number of peers which will fit in a packet */
763 struct info_peer_list *pl, plist[min(MAXARGS, 4)];
773 if (impl_ver == IMPL_XNTPD)
774 sendsize = sizeof(struct info_peer_list);
776 sendsize = v4sizeof(struct info_peer_list);
780 qitemlim = min(pcmd->nargs, COUNTOF(plist));
781 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
782 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
783 pl->addr = NSRCADR(&pcmd->argval[qitems].netnum);
784 if (impl_ver == IMPL_XNTPD)
787 if (impl_ver == IMPL_XNTPD_OLD) {
789 "***Server doesn't understand IPv6 addresses\n");
792 pl->addr6 = SOCK_ADDR6(&pcmd->argval[qitems].netnum);
795 pl->port = (u_short)s_port;
796 pl->hmode = plist[qitems].flags = 0;
797 pl = (void *)((char *)pl + sendsize);
800 res = doquery(impl_ver, REQ_PEER_STATS, 0, qitems,
801 sendsize, (char *)plist, &items,
802 &itemsize, (void *)&pp, 0,
803 sizeof(struct info_peer_stats));
805 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
806 impl_ver = IMPL_XNTPD_OLD;
813 if (!checkitems(items, fp))
816 if (!checkitemsize(itemsize, sizeof(struct info_peer_stats)) &&
817 !checkitemsize(itemsize, v4sizeof(struct info_peer_stats)))
820 while (items-- > 0) {
823 if (pp->v6_flag != 0) {
826 SOCK_ADDR6(&dst) = pp->dstadr6;
827 SOCK_ADDR6(&src) = pp->srcadr6;
831 NSRCADR(&dst) = pp->dstadr;
832 NSRCADR(&src) = pp->srcadr;
834 #ifdef ISC_PLATFORM_HAVESALEN
835 src.sa.sa_len = SOCKLEN(&src);
836 dst.sa.sa_len = SOCKLEN(&dst);
838 fprintf(fp, "remote host: %s\n",
840 fprintf(fp, "local interface: %s\n",
842 fprintf(fp, "time last received: %lus\n",
843 (u_long)ntohl(pp->timereceived));
844 fprintf(fp, "time until next send: %lus\n",
845 (u_long)ntohl(pp->timetosend));
846 fprintf(fp, "reachability change: %lus\n",
847 (u_long)ntohl(pp->timereachable));
848 fprintf(fp, "packets sent: %lu\n",
849 (u_long)ntohl(pp->sent));
850 fprintf(fp, "packets received: %lu\n",
851 (u_long)ntohl(pp->processed));
852 fprintf(fp, "bad authentication: %lu\n",
853 (u_long)ntohl(pp->badauth));
854 fprintf(fp, "bogus origin: %lu\n",
855 (u_long)ntohl(pp->bogusorg));
856 fprintf(fp, "duplicate: %lu\n",
857 (u_long)ntohl(pp->oldpkt));
858 fprintf(fp, "bad dispersion: %lu\n",
859 (u_long)ntohl(pp->seldisp));
860 fprintf(fp, "bad reference time: %lu\n",
861 (u_long)ntohl(pp->selbroken));
862 fprintf(fp, "candidate order: %u\n",
866 fprintf(fp, "flags: ");
867 print_pflag(fp, ntohs(pp->flags));
874 * loopinfo - show loop filter information
882 struct info_loop *il;
889 if (pcmd->nargs > 0) {
890 if (STREQ(pcmd->argval[0].string, "oneline"))
892 else if (STREQ(pcmd->argval[0].string, "multiline"))
895 (void) fprintf(stderr, "How many lines?\n");
901 res = doquery(impl_ver, REQ_LOOP_INFO, 0, 0, 0, (char *)NULL,
902 &items, &itemsize, (void *)&il, 0,
903 sizeof(struct info_loop));
905 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
906 impl_ver = IMPL_XNTPD_OLD;
913 if (!check1item(items, fp))
916 if (!checkitemsize(itemsize, sizeof(struct info_loop)))
922 NTOHL_FP(&il->last_offset, &tempts);
923 NTOHL_FP(&il->drift_comp, &temp2ts);
926 "offset %s, frequency %s, time_const %ld, watchdog %ld\n",
929 (long)(int32)ntohl((u_long)il->compliance),
930 (u_long)ntohl((u_long)il->watchdog_timer));
932 NTOHL_FP(&il->last_offset, &tempts);
933 (void) fprintf(fp, "offset: %s s\n",
935 NTOHL_FP(&il->drift_comp, &tempts);
936 (void) fprintf(fp, "frequency: %s ppm\n",
938 (void) fprintf(fp, "poll adjust: %ld\n",
939 (long)(int32)ntohl(il->compliance));
940 (void) fprintf(fp, "watchdog timer: %ld s\n",
941 (u_long)ntohl(il->watchdog_timer));
947 * sysinfo - show current system state
964 res = doquery(impl_ver, REQ_SYS_INFO, 0, 0, 0, (char *)NULL,
965 &items, &itemsize, (void *)&is, 0,
966 sizeof(struct info_sys));
968 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
969 impl_ver = IMPL_XNTPD_OLD;
976 if (!check1item(items, fp))
979 if (!checkitemsize(itemsize, sizeof(struct info_sys)) &&
980 !checkitemsize(itemsize, v4sizeof(struct info_sys)))
983 SET_ADDR(peeraddr, is->v6_flag, is->peer, is->peer6);
985 (void) fprintf(fp, "system peer: %s\n", nntohost(&peeraddr));
986 (void) fprintf(fp, "system peer mode: %s\n", modetoa(is->peer_mode));
987 (void) fprintf(fp, "leap indicator: %c%c\n",
988 is->leap & 0x2 ? '1' : '0',
989 is->leap & 0x1 ? '1' : '0');
990 (void) fprintf(fp, "stratum: %d\n", (int)is->stratum);
991 (void) fprintf(fp, "precision: %d\n", (int)is->precision);
992 (void) fprintf(fp, "root distance: %s s\n",
993 fptoa(NTOHS_FP(is->rootdelay), 5));
994 (void) fprintf(fp, "root dispersion: %s s\n",
995 ufptoa(NTOHS_FP(is->rootdispersion), 5));
996 (void) fprintf(fp, "reference ID: [%s]\n",
997 refid_string(is->refid, is->stratum));
998 NTOHL_FP(&is->reftime, &tempts);
999 (void) fprintf(fp, "reference time: %s\n", prettydate(&tempts));
1001 (void) fprintf(fp, "system flags: ");
1002 if ((is->flags & (INFO_FLAG_BCLIENT | INFO_FLAG_AUTHENABLE |
1003 INFO_FLAG_NTP | INFO_FLAG_KERNEL| INFO_FLAG_CAL |
1004 INFO_FLAG_PPS_SYNC | INFO_FLAG_MONITOR | INFO_FLAG_FILEGEN)) == 0) {
1005 (void) fprintf(fp, "none\n");
1007 if (is->flags & INFO_FLAG_BCLIENT)
1008 (void) fprintf(fp, "bclient ");
1009 if (is->flags & INFO_FLAG_AUTHENTICATE)
1010 (void) fprintf(fp, "auth ");
1011 if (is->flags & INFO_FLAG_MONITOR)
1012 (void) fprintf(fp, "monitor ");
1013 if (is->flags & INFO_FLAG_NTP)
1014 (void) fprintf(fp, "ntp ");
1015 if (is->flags & INFO_FLAG_KERNEL)
1016 (void) fprintf(fp, "kernel ");
1017 if (is->flags & INFO_FLAG_FILEGEN)
1018 (void) fprintf(fp, "stats ");
1019 if (is->flags & INFO_FLAG_CAL)
1020 (void) fprintf(fp, "calibrate ");
1021 if (is->flags & INFO_FLAG_PPS_SYNC)
1022 (void) fprintf(fp, "pps ");
1023 (void) fprintf(fp, "\n");
1025 (void) fprintf(fp, "jitter: %s s\n",
1026 fptoa(ntohl(is->frequency), 6));
1027 (void) fprintf(fp, "stability: %s ppm\n",
1028 ufptoa(ntohl(is->stability), 3));
1029 (void) fprintf(fp, "broadcastdelay: %s s\n",
1030 fptoa(NTOHS_FP(is->bdelay), 6));
1031 NTOHL_FP(&is->authdelay, &tempts);
1032 (void) fprintf(fp, "authdelay: %s s\n", lfptoa(&tempts, 6));
1037 * sysstats - print system statistics
1046 struct info_sys_stats *ss;
1052 res = doquery(impl_ver, REQ_SYS_STATS, 0, 0, 0, (char *)NULL,
1053 &items, &itemsize, (void *)&ss, 0,
1054 sizeof(struct info_sys_stats));
1056 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1057 impl_ver = IMPL_XNTPD_OLD;
1064 if (!check1item(items, fp))
1067 if (itemsize != sizeof(struct info_sys_stats) &&
1068 itemsize != sizeof(struct old_info_sys_stats)) {
1069 /* issue warning according to new structure size */
1070 checkitemsize(itemsize, sizeof(struct info_sys_stats));
1073 fprintf(fp, "time since restart: %lu\n",
1074 (u_long)ntohl(ss->timeup));
1075 fprintf(fp, "time since reset: %lu\n",
1076 (u_long)ntohl(ss->timereset));
1077 fprintf(fp, "packets received: %lu\n",
1078 (u_long)ntohl(ss->received));
1079 fprintf(fp, "packets processed: %lu\n",
1080 (u_long)ntohl(ss->processed));
1081 fprintf(fp, "current version: %lu\n",
1082 (u_long)ntohl(ss->newversionpkt));
1083 fprintf(fp, "previous version: %lu\n",
1084 (u_long)ntohl(ss->oldversionpkt));
1085 fprintf(fp, "declined: %lu\n",
1086 (u_long)ntohl(ss->unknownversion));
1087 fprintf(fp, "access denied: %lu\n",
1088 (u_long)ntohl(ss->denied));
1089 fprintf(fp, "bad length or format: %lu\n",
1090 (u_long)ntohl(ss->badlength));
1091 fprintf(fp, "bad authentication: %lu\n",
1092 (u_long)ntohl(ss->badauth));
1093 if (itemsize != sizeof(struct info_sys_stats))
1096 fprintf(fp, "rate exceeded: %lu\n",
1097 (u_long)ntohl(ss->limitrejected));
1103 * iostats - print I/O statistics
1112 struct info_io_stats *io;
1118 res = doquery(impl_ver, REQ_IO_STATS, 0, 0, 0, NULL, &items,
1119 &itemsize, (void *)&io, 0, sizeof(*io));
1121 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1122 impl_ver = IMPL_XNTPD_OLD;
1129 if (!check1item(items, fp))
1132 if (!checkitemsize(itemsize, sizeof(*io)))
1135 fprintf(fp, "time since reset: %lu\n",
1136 (u_long)ntohl(io->timereset));
1137 fprintf(fp, "receive buffers: %u\n",
1138 (u_int)ntohs(io->totalrecvbufs));
1139 fprintf(fp, "free receive buffers: %u\n",
1140 (u_int)ntohs(io->freerecvbufs));
1141 fprintf(fp, "used receive buffers: %u\n",
1142 (u_int)ntohs(io->fullrecvbufs));
1143 fprintf(fp, "low water refills: %u\n",
1144 (u_int)ntohs(io->lowwater));
1145 fprintf(fp, "dropped packets: %lu\n",
1146 (u_long)ntohl(io->dropped));
1147 fprintf(fp, "ignored packets: %lu\n",
1148 (u_long)ntohl(io->ignored));
1149 fprintf(fp, "received packets: %lu\n",
1150 (u_long)ntohl(io->received));
1151 fprintf(fp, "packets sent: %lu\n",
1152 (u_long)ntohl(io->sent));
1153 fprintf(fp, "packets not sent: %lu\n",
1154 (u_long)ntohl(io->notsent));
1155 fprintf(fp, "interrupts handled: %lu\n",
1156 (u_long)ntohl(io->interrupts));
1157 fprintf(fp, "received by int: %lu\n",
1158 (u_long)ntohl(io->int_received));
1163 * memstats - print peer memory statistics
1172 struct info_mem_stats *mem;
1179 res = doquery(impl_ver, REQ_MEM_STATS, 0, 0, 0, NULL, &items,
1180 &itemsize, (void *)&mem, 0, sizeof(*mem));
1182 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1183 impl_ver = IMPL_XNTPD_OLD;
1190 if (!check1item(items, fp))
1193 if (!checkitemsize(itemsize, sizeof(*mem)))
1196 fprintf(fp, "time since reset: %lu\n",
1197 (u_long)ntohl(mem->timereset));
1198 fprintf(fp, "total peer memory: %u\n",
1199 (u_int)ntohs(mem->totalpeermem));
1200 fprintf(fp, "free peer memory: %u\n",
1201 (u_int)ntohs(mem->freepeermem));
1202 fprintf(fp, "calls to findpeer: %lu\n",
1203 (u_long)ntohl(mem->findpeer_calls));
1204 fprintf(fp, "new peer allocations: %lu\n",
1205 (u_long)ntohl(mem->allocations));
1206 fprintf(fp, "peer demobilizations: %lu\n",
1207 (u_long)ntohl(mem->demobilizations));
1209 fprintf(fp, "hash table counts: ");
1210 for (i = 0; i < NTP_HASH_SIZE; i++) {
1211 fprintf(fp, "%4d", (int)mem->hashcount[i]);
1212 if ((i % 8) == 7 && i != (NTP_HASH_SIZE-1))
1221 * timerstats - print timer statistics
1230 struct info_timer_stats *tim;
1236 res = doquery(impl_ver, REQ_TIMER_STATS, 0, 0, 0, NULL, &items,
1237 &itemsize, (void *)&tim, 0, sizeof(*tim));
1239 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1240 impl_ver = IMPL_XNTPD_OLD;
1247 if (!check1item(items, fp))
1250 if (!checkitemsize(itemsize, sizeof(*tim)))
1253 fprintf(fp, "time since reset: %lu\n",
1254 (u_long)ntohl(tim->timereset));
1255 fprintf(fp, "alarms handled: %lu\n",
1256 (u_long)ntohl(tim->alarms));
1257 fprintf(fp, "alarm overruns: %lu\n",
1258 (u_long)ntohl(tim->overflows));
1259 fprintf(fp, "calls to transmit: %lu\n",
1260 (u_long)ntohl(tim->xmtcalls));
1265 * addpeer - configure an active mode association
1273 doconfig(pcmd, fp, MODE_ACTIVE, 0);
1278 * addserver - configure a client mode association
1286 doconfig(pcmd, fp, MODE_CLIENT, 0);
1290 * addrefclock - configure a reference clock association
1298 doconfig(pcmd, fp, MODE_CLIENT, 1);
1302 * broadcast - configure a broadcast mode association
1310 doconfig(pcmd, fp, MODE_BROADCAST, 0);
1315 * config - configure a new peer association
1325 struct conf_peer cpeer;
1346 minpoll = NTP_MINDPOLL;
1347 maxpoll = NTP_MAXDPOLL;
1352 if (impl_ver == IMPL_XNTPD)
1353 sendsize = sizeof(struct conf_peer);
1355 sendsize = v4sizeof(struct conf_peer);
1358 while (pcmd->nargs > (size_t)items) {
1359 if (STREQ(pcmd->argval[items].string, "prefer"))
1360 flags |= CONF_FLAG_PREFER;
1361 else if (STREQ(pcmd->argval[items].string, "burst"))
1362 flags |= CONF_FLAG_BURST;
1363 else if (STREQ(pcmd->argval[items].string, "iburst"))
1364 flags |= CONF_FLAG_IBURST;
1365 else if (!refc && STREQ(pcmd->argval[items].string, "keyid"))
1367 else if (!refc && STREQ(pcmd->argval[items].string, "version"))
1369 else if (STREQ(pcmd->argval[items].string, "minpoll"))
1371 else if (STREQ(pcmd->argval[items].string, "maxpoll"))
1374 if (!atoint(pcmd->argval[items].string, &val))
1383 version = (u_int)val;
1388 minpoll = (u_char)val;
1393 maxpoll = (u_char)val;
1398 cmode = (u_char)val;
1403 fprintf(fp, "*** '%s' not understood\n",
1404 pcmd->argval[items].string);
1410 "*** Value '%s' should be unsigned\n",
1411 pcmd->argval[items].string);
1418 flags |= CONF_FLAG_AUTHENABLE;
1419 if (version > NTP_VERSION || version < NTP_OLDVERSION) {
1420 fprintf(fp, "***invalid version number: %u\n",
1424 if (minpoll < NTP_MINPOLL || minpoll > NTP_MAXPOLL ||
1425 maxpoll < NTP_MINPOLL || maxpoll > NTP_MAXPOLL ||
1426 minpoll > maxpoll) {
1427 fprintf(fp, "***min/max-poll must be within %d..%d\n",
1428 NTP_MINPOLL, NTP_MAXPOLL);
1437 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1438 cpeer.peeraddr = NSRCADR(&pcmd->argval[0].netnum);
1439 if (impl_ver == IMPL_XNTPD)
1442 if (impl_ver == IMPL_XNTPD_OLD) {
1444 "***Server doesn't understand IPv6 addresses\n");
1447 cpeer.peeraddr6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
1450 cpeer.hmode = (u_char) mode;
1451 cpeer.keyid = keyid;
1452 cpeer.version = (u_char) version;
1453 cpeer.minpoll = minpoll;
1454 cpeer.maxpoll = maxpoll;
1455 cpeer.flags = (u_char)flags;
1458 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1459 sendsize, (char *)&cpeer, &items,
1460 &itemsize, &dummy, 0, sizeof(struct conf_peer));
1462 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1463 impl_ver = IMPL_XNTPD_OLD;
1467 if (res == INFO_ERR_FMT) {
1469 "***Retrying command with old conf_peer size\n");
1470 res = doquery(impl_ver, REQ_CONFIG, 1, 1,
1471 sizeof(struct old_conf_peer), (char *)&cpeer,
1472 &items, &itemsize, &dummy, 0,
1473 sizeof(struct conf_peer));
1476 (void) fprintf(fp, "done!\n");
1482 * unconfig - unconfigure some associations
1490 /* 8 is the maximum number of peers which will fit in a packet */
1491 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
1501 if (impl_ver == IMPL_XNTPD)
1502 sendsize = sizeof(struct conf_unpeer);
1504 sendsize = v4sizeof(struct conf_unpeer);
1506 qitemlim = min(pcmd->nargs, COUNTOF(plist));
1507 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
1508 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1509 pl->peeraddr = NSRCADR(&pcmd->argval[qitems].netnum);
1510 if (impl_ver == IMPL_XNTPD)
1513 if (impl_ver == IMPL_XNTPD_OLD) {
1515 "***Server doesn't understand IPv6 addresses\n");
1519 SOCK_ADDR6(&pcmd->argval[qitems].netnum);
1522 pl = (void *)((char *)pl + sendsize);
1525 res = doquery(impl_ver, REQ_UNCONFIG, 1, qitems,
1526 sendsize, (char *)plist, &items,
1527 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
1529 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1530 impl_ver = IMPL_XNTPD_OLD;
1535 (void) fprintf(fp, "done!\n");
1540 * set - set some system flags
1548 doset(pcmd, fp, REQ_SET_SYS_FLAG);
1553 * clear - clear some system flags
1561 doset(pcmd, fp, REQ_CLR_SYS_FLAG);
1566 * doset - set/clear system flags
1575 struct conf_sys_flags sys;
1583 for (items = 0; (size_t)items < pcmd->nargs; items++) {
1584 if (STREQ(pcmd->argval[items].string, "auth"))
1585 sys.flags |= SYS_FLAG_AUTH;
1586 else if (STREQ(pcmd->argval[items].string, "bclient"))
1587 sys.flags |= SYS_FLAG_BCLIENT;
1588 else if (STREQ(pcmd->argval[items].string, "calibrate"))
1589 sys.flags |= SYS_FLAG_CAL;
1590 else if (STREQ(pcmd->argval[items].string, "kernel"))
1591 sys.flags |= SYS_FLAG_KERNEL;
1592 else if (STREQ(pcmd->argval[items].string, "monitor"))
1593 sys.flags |= SYS_FLAG_MONITOR;
1594 else if (STREQ(pcmd->argval[items].string, "ntp"))
1595 sys.flags |= SYS_FLAG_NTP;
1596 else if (STREQ(pcmd->argval[items].string, "pps"))
1597 sys.flags |= SYS_FLAG_PPS;
1598 else if (STREQ(pcmd->argval[items].string, "stats"))
1599 sys.flags |= SYS_FLAG_FILEGEN;
1601 (void) fprintf(fp, "Unknown flag %s\n",
1602 pcmd->argval[items].string);
1607 sys.flags = htonl(sys.flags);
1608 if (res || sys.flags == 0)
1612 res = doquery(impl_ver, req, 1, 1,
1613 sizeof(struct conf_sys_flags), (char *)&sys, &items,
1614 &itemsize, &dummy, 0, sizeof(struct conf_sys_flags));
1616 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1617 impl_ver = IMPL_XNTPD_OLD;
1622 (void) fprintf(fp, "done!\n");
1627 * data for printing/interrpreting the restrict flags
1634 /* XXX: HMS: we apparently don't report set bits we do not recognize. */
1636 static struct resflags resflagsV2[] = {
1637 { "ignore", 0x001 },
1638 { "noserve", 0x002 },
1639 { "notrust", 0x004 },
1640 { "noquery", 0x008 },
1641 { "nomodify", 0x010 },
1642 { "nopeer", 0x020 },
1643 { "notrap", 0x040 },
1644 { "lptrap", 0x080 },
1645 { "limited", 0x100 },
1649 static struct resflags resflagsV3[] = {
1650 { "ignore", RES_IGNORE },
1651 { "noserve", RES_DONTSERVE },
1652 { "notrust", RES_DONTTRUST },
1653 { "noquery", RES_NOQUERY },
1654 { "nomodify", RES_NOMODIFY },
1655 { "nopeer", RES_NOPEER },
1656 { "notrap", RES_NOTRAP },
1657 { "lptrap", RES_LPTRAP },
1658 { "limited", RES_LIMITED },
1659 { "version", RES_VERSION },
1661 { "flake", RES_FLAKE },
1666 static struct resflags resmflags[] = {
1667 { "ntpport", RESM_NTPONLY },
1668 { "interface", RESM_INTERFACE },
1669 { "source", RESM_SOURCE },
1675 * reslist - obtain and print the server's restrict list
1684 struct info_restrict *rl;
1686 sockaddr_u maskaddr;
1693 struct resflags *rf;
1698 static const char *comma = ", ";
1701 res = doquery(impl_ver, REQ_GET_RESTRICT, 0, 0, 0, (char *)NULL,
1702 &items, &itemsize, (void *)&rl, 0,
1703 sizeof(struct info_restrict));
1705 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1706 impl_ver = IMPL_XNTPD_OLD;
1713 if (!checkitems(items, fp))
1716 if (!checkitemsize(itemsize, sizeof(struct info_restrict)) &&
1717 !checkitemsize(itemsize, v4sizeof(struct info_restrict)))
1721 " address mask count flags\n");
1723 "=====================================================================\n");
1726 SET_ADDRS(resaddr, maskaddr, rl, addr, mask);
1727 if (rl->v6_flag != 0) {
1728 addr = nntohost(&resaddr);
1730 if (rl->mask == (u_int32)0xffffffff)
1731 addr = nntohost(&resaddr);
1733 addr = stoa(&resaddr);
1735 mask = stoa(&maskaddr);
1737 if ((pcmd->nargs == 0) ||
1738 ((pcmd->argval->ival == 6) && (rl->v6_flag != 0)) ||
1739 ((pcmd->argval->ival == 4) && (rl->v6_flag == 0)))
1741 count = ntohl(rl->count);
1742 rflags = ntohs(rl->rflags);
1743 mflags = ntohs(rl->mflags);
1748 while (rf->bit != 0) {
1749 if (mflags & rf->bit) {
1751 strlcat(flagstr, comma,
1754 strlcat(flagstr, rf->str,
1760 rf = (impl_ver == IMPL_XNTPD_OLD)
1764 while (rf->bit != 0) {
1765 if (rflags & rf->bit) {
1767 strlcat(flagstr, comma,
1770 strlcat(flagstr, rf->str,
1776 if (flagstr[0] == '\0')
1777 strlcpy(flagstr, "none", sizeof(flagstr));
1780 fprintf(fp, "%-15.15s %-15.15s %9lu %s\n",
1781 addr, mask, (u_long)count, flagstr);
1790 * new_restrict - create/add a set of restrictions
1798 do_restrict(pcmd, fp, REQ_RESADDFLAGS);
1803 * unrestrict - remove restriction flags from existing entry
1811 do_restrict(pcmd, fp, REQ_RESSUBFLAGS);
1816 * delrestrict - delete an existing restriction
1824 do_restrict(pcmd, fp, REQ_UNRESTRICT);
1829 * do_restrict - decode commandline restrictions and make the request
1838 struct conf_restrict cres;
1849 /* Initialize cres */
1857 if (impl_ver == IMPL_XNTPD)
1858 sendsize = sizeof(struct conf_restrict);
1860 sendsize = v4sizeof(struct conf_restrict);
1862 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1863 cres.addr = NSRCADR(&pcmd->argval[0].netnum);
1864 cres.mask = NSRCADR(&pcmd->argval[1].netnum);
1865 if (impl_ver == IMPL_XNTPD)
1868 if (impl_ver == IMPL_XNTPD_OLD) {
1870 "***Server doesn't understand IPv6 addresses\n");
1873 cres.addr6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
1879 for (res = 2; res < pcmd->nargs; res++) {
1880 if (STREQ(pcmd->argval[res].string, "ntpport")) {
1881 cres.mflags |= RESM_NTPONLY;
1883 for (i = 0; resflagsV3[i].bit != 0; i++) {
1884 if (STREQ(pcmd->argval[res].string,
1888 if (resflagsV3[i].bit != 0) {
1889 cres.flags |= resflagsV3[i].bit;
1890 if (req_code == REQ_UNRESTRICT) {
1892 "Flag %s inappropriate\n",
1897 fprintf(fp, "Unknown flag %s\n",
1898 pcmd->argval[res].string);
1903 cres.flags = htons(cres.flags);
1904 cres.mflags = htons(cres.mflags);
1907 * Make sure mask for default address is zero. Otherwise,
1908 * make sure mask bits are contiguous.
1910 if (IS_IPV4(&pcmd->argval[0].netnum)) {
1911 if (cres.addr == 0) {
1914 num = ntohl(cres.mask);
1915 for (bit = 0x80000000; bit != 0; bit >>= 1)
1916 if ((num & bit) == 0)
1918 for ( ; bit != 0; bit >>= 1)
1919 if ((num & bit) != 0)
1922 fprintf(fp, "Invalid mask %s\n",
1928 /* XXX IPv6 sanity checking stuff */
1934 res = doquery(impl_ver, req_code, 1, 1, sendsize, (char *)&cres,
1935 &items, &itemsize, &dummy, 0, sizeof(cres));
1937 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1938 impl_ver = IMPL_XNTPD_OLD;
1943 (void) fprintf(fp, "done!\n");
1949 * monlist - obtain and print the server's monitor data
1958 const char *struct_star;
1959 const struct info_monitor *ml;
1960 const struct info_monitor_1 *m1;
1961 const struct old_info_monitor *oml;
1969 if (pcmd->nargs > 0)
1970 version = pcmd->argval[0].ival;
1973 res = doquery(impl_ver,
1974 (version == 1 || version == -1) ? REQ_MON_GETLIST_1 :
1975 REQ_MON_GETLIST, 0, 0, 0, NULL,
1976 &items, &itemsize, &struct_star,
1977 (version < 0) ? (1 << INFO_ERR_REQ) : 0,
1978 sizeof(struct info_monitor_1));
1980 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
1981 impl_ver = IMPL_XNTPD_OLD;
1985 if (res == INFO_ERR_REQ && version < 0)
1986 res = doquery(impl_ver, REQ_MON_GETLIST, 0, 0, 0, NULL,
1987 &items, &itemsize, &struct_star, 0,
1988 sizeof(struct info_monitor));
1993 if (!checkitems(items, fp))
1996 if (itemsize == sizeof(struct info_monitor_1) ||
1997 itemsize == v4sizeof(struct info_monitor_1)) {
1999 m1 = (const void*)struct_star;
2001 "remote address port local address count m ver rstr avgint lstint\n");
2003 "===============================================================================\n");
2005 SET_ADDRS(dstadr, addr, m1, daddr, addr);
2006 if ((pcmd->nargs == 0) ||
2007 ((pcmd->argval->ival == 6) && (m1->v6_flag != 0)) ||
2008 ((pcmd->argval->ival == 4) && (m1->v6_flag == 0)))
2010 "%-22.22s %5d %-15s %8lu %1u %1u %6lx %6lu %7lu\n",
2014 (u_long)ntohl(m1->count),
2017 (u_long)ntohl(m1->restr),
2018 (u_long)ntohl(m1->avg_int),
2019 (u_long)ntohl(m1->last_int));
2023 } else if (itemsize == sizeof(struct info_monitor) ||
2024 itemsize == v4sizeof(struct info_monitor)) {
2026 ml = (const void *)struct_star;
2028 " address port count mode ver rstr avgint lstint\n");
2030 "===============================================================================\n");
2032 SET_ADDR(dstadr, ml->v6_flag, ml->addr, ml->addr6);
2033 if ((pcmd->nargs == 0) ||
2034 ((pcmd->argval->ival == 6) && (ml->v6_flag != 0)) ||
2035 ((pcmd->argval->ival == 4) && (ml->v6_flag == 0)))
2037 "%-25.25s %5u %9lu %4u %2u %9lx %9lu %9lu\n",
2040 (u_long)ntohl(ml->count),
2043 (u_long)ntohl(ml->restr),
2044 (u_long)ntohl(ml->avg_int),
2045 (u_long)ntohl(ml->last_int));
2049 } else if (itemsize == sizeof(struct old_info_monitor)) {
2051 oml = (const void *)struct_star;
2053 " address port count mode version lasttime firsttime\n");
2055 "======================================================================\n");
2057 SET_ADDR(dstadr, oml->v6_flag, oml->addr, oml->addr6);
2058 fprintf(fp, "%-20.20s %5u %9lu %4u %3u %9lu %9lu\n",
2061 (u_long)ntohl(oml->count),
2064 (u_long)ntohl(oml->lasttime),
2065 (u_long)ntohl(oml->firsttime));
2070 /* issue warning according to new info_monitor size */
2071 checkitemsize(itemsize, sizeof(struct info_monitor));
2077 * Mapping between command line strings and stat reset flags
2080 const char * const str;
2083 { "allpeers", RESET_FLAG_ALLPEERS },
2084 { "io", RESET_FLAG_IO },
2085 { "sys", RESET_FLAG_SYS },
2086 { "mem", RESET_FLAG_MEM },
2087 { "timer", RESET_FLAG_TIMER },
2088 { "auth", RESET_FLAG_AUTH },
2089 { "ctl", RESET_FLAG_CTL },
2094 * reset - reset statistic counters
2102 struct reset_flags rflags;
2112 for (res = 0; res < pcmd->nargs; res++) {
2113 for (i = 0; sreset[i].flag != 0; i++) {
2114 if (STREQ(pcmd->argval[res].string, sreset[i].str))
2117 if (sreset[i].flag == 0) {
2118 fprintf(fp, "Flag %s unknown\n",
2119 pcmd->argval[res].string);
2122 rflags.flags |= sreset[i].flag;
2125 rflags.flags = htonl(rflags.flags);
2128 (void) fprintf(fp, "Not done due to errors\n");
2133 res = doquery(impl_ver, REQ_RESET_STATS, 1, 1,
2134 sizeof(struct reset_flags), (char *)&rflags, &items,
2135 &itemsize, &dummy, 0, sizeof(struct reset_flags));
2137 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2138 impl_ver = IMPL_XNTPD_OLD;
2143 (void) fprintf(fp, "done!\n");
2150 * preset - reset stat counters for particular peers
2158 /* 8 is the maximum number of peers which will fit in a packet */
2159 struct conf_unpeer *pl, plist[min(MAXARGS, 8)];
2169 if (impl_ver == IMPL_XNTPD)
2170 sendsize = sizeof(struct conf_unpeer);
2172 sendsize = v4sizeof(struct conf_unpeer);
2174 qitemlim = min(pcmd->nargs, COUNTOF(plist));
2175 for (qitems = 0, pl = plist; qitems < qitemlim; qitems++) {
2176 if (IS_IPV4(&pcmd->argval[qitems].netnum)) {
2177 pl->peeraddr = NSRCADR(&pcmd->argval[qitems].netnum);
2178 if (impl_ver == IMPL_XNTPD)
2181 if (impl_ver == IMPL_XNTPD_OLD) {
2183 "***Server doesn't understand IPv6 addresses\n");
2187 SOCK_ADDR6(&pcmd->argval[qitems].netnum);
2190 pl = (void *)((char *)pl + sendsize);
2193 res = doquery(impl_ver, REQ_RESET_PEER, 1, qitems,
2194 sendsize, (char *)plist, &items,
2195 &itemsize, &dummy, 0, sizeof(struct conf_unpeer));
2197 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2198 impl_ver = IMPL_XNTPD_OLD;
2203 (void) fprintf(fp, "done!\n");
2208 * readkeys - request the server to reread the keys file
2223 res = doquery(impl_ver, REQ_REREAD_KEYS, 1, 0, 0, (char *)0,
2224 &items, &itemsize, &dummy, 0, sizeof(dummy));
2226 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2227 impl_ver = IMPL_XNTPD_OLD;
2232 (void) fprintf(fp, "done!\n");
2238 * trustkey - add some keys to the trusted key list
2246 do_trustkey(pcmd, fp, REQ_TRUSTKEY);
2251 * untrustkey - remove some keys from the trusted key list
2259 do_trustkey(pcmd, fp, REQ_UNTRUSTKEY);
2264 * do_trustkey - do grunge work of adding/deleting keys
2273 u_long keyids[MAXARGS];
2282 for (i = 0; i < pcmd->nargs; i++) {
2283 keyids[ritems++] = pcmd->argval[i].uval;
2287 res = doquery(impl_ver, req, 1, ritems, sizeof(u_long),
2288 (char *)keyids, &items, &itemsize, &dummy, 0,
2291 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2292 impl_ver = IMPL_XNTPD_OLD;
2297 (void) fprintf(fp, "done!\n");
2304 * authinfo - obtain and print info about authentication
2313 struct info_auth *ia;
2319 res = doquery(impl_ver, REQ_AUTHINFO, 0, 0, 0, NULL, &items,
2320 &itemsize, (void *)&ia, 0, sizeof(*ia));
2322 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2323 impl_ver = IMPL_XNTPD_OLD;
2330 if (!check1item(items, fp))
2333 if (!checkitemsize(itemsize, sizeof(*ia)))
2336 fprintf(fp, "time since reset: %lu\n",
2337 (u_long)ntohl(ia->timereset));
2338 fprintf(fp, "stored keys: %lu\n",
2339 (u_long)ntohl(ia->numkeys));
2340 fprintf(fp, "free keys: %lu\n",
2341 (u_long)ntohl(ia->numfreekeys));
2342 fprintf(fp, "key lookups: %lu\n",
2343 (u_long)ntohl(ia->keylookups));
2344 fprintf(fp, "keys not found: %lu\n",
2345 (u_long)ntohl(ia->keynotfound));
2346 fprintf(fp, "uncached keys: %lu\n",
2347 (u_long)ntohl(ia->keyuncached));
2348 fprintf(fp, "encryptions: %lu\n",
2349 (u_long)ntohl(ia->encryptions));
2350 fprintf(fp, "decryptions: %lu\n",
2351 (u_long)ntohl(ia->decryptions));
2352 fprintf(fp, "expired keys: %lu\n",
2353 (u_long)ntohl(ia->expired));
2359 * traps - obtain and print a list of traps
2369 struct info_trap *it;
2370 sockaddr_u trap_addr, local_addr;
2376 res = doquery(impl_ver, REQ_TRAPS, 0, 0, 0, NULL, &items,
2377 &itemsize, (void *)&it, 0, sizeof(*it));
2379 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2380 impl_ver = IMPL_XNTPD_OLD;
2387 if (!checkitems(items, fp))
2390 if (!checkitemsize(itemsize, sizeof(struct info_trap)) &&
2391 !checkitemsize(itemsize, v4sizeof(struct info_trap)))
2394 for (i = 0; i < items; i++ ) {
2395 SET_ADDRS(trap_addr, local_addr, it, trap_address, local_address);
2396 fprintf(fp, "%saddress %s, port %d\n",
2400 stoa(&trap_addr), ntohs(it->trap_port));
2401 fprintf(fp, "interface: %s, ",
2402 (0 == it->local_address)
2404 : stoa(&local_addr));
2405 if (ntohl(it->flags) & TRAP_CONFIGURED)
2406 fprintf(fp, "configured\n");
2407 else if (ntohl(it->flags) & TRAP_NONPRIO)
2408 fprintf(fp, "low priority\n");
2410 fprintf(fp, "normal priority\n");
2412 fprintf(fp, "set for %ld secs, last set %ld secs ago\n",
2413 (long)ntohl(it->origtime),
2414 (long)ntohl(it->settime));
2415 fprintf(fp, "sequence %d, number of resets %ld\n",
2416 ntohs(it->sequence), (long)ntohl(it->resets));
2422 * addtrap - configure a trap
2430 do_addclr_trap(pcmd, fp, REQ_ADD_TRAP);
2435 * clrtrap - clear a trap from the server
2443 do_addclr_trap(pcmd, fp, REQ_CLR_TRAP);
2448 * do_addclr_trap - do grunge work of adding/deleting traps
2457 struct conf_trap ctrap;
2465 if (impl_ver == IMPL_XNTPD)
2466 sendsize = sizeof(struct conf_trap);
2468 sendsize = v4sizeof(struct conf_trap);
2470 if (IS_IPV4(&pcmd->argval[0].netnum)) {
2471 ctrap.trap_address = NSRCADR(&pcmd->argval[0].netnum);
2472 if (impl_ver == IMPL_XNTPD)
2475 if (impl_ver == IMPL_XNTPD_OLD) {
2477 "***Server doesn't understand IPv6 addresses\n");
2480 ctrap.trap_address6 = SOCK_ADDR6(&pcmd->argval[0].netnum);
2483 ctrap.local_address = 0;
2484 ctrap.trap_port = htons(TRAPPORT);
2487 if (pcmd->nargs > 1) {
2488 ctrap.trap_port = htons((u_short)pcmd->argval[1].uval);
2489 if (pcmd->nargs > 2) {
2490 if (AF(&pcmd->argval[2].netnum) !=
2491 AF(&pcmd->argval[0].netnum)) {
2493 "***Cannot mix IPv4 and IPv6 addresses\n");
2496 if (IS_IPV4(&pcmd->argval[2].netnum))
2497 ctrap.local_address = NSRCADR(&pcmd->argval[2].netnum);
2499 ctrap.local_address6 = SOCK_ADDR6(&pcmd->argval[2].netnum);
2503 res = doquery(impl_ver, req, 1, 1, sendsize,
2504 (char *)&ctrap, &items, &itemsize, &dummy, 0,
2505 sizeof(struct conf_trap));
2507 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2508 impl_ver = IMPL_XNTPD_OLD;
2513 (void) fprintf(fp, "done!\n");
2520 * requestkey - change the server's request key (a dangerous request)
2528 do_changekey(pcmd, fp, REQ_REQUEST_KEY);
2533 * controlkey - change the server's control key
2541 do_changekey(pcmd, fp, REQ_CONTROL_KEY);
2547 * do_changekey - do grunge work of changing keys
2563 key = htonl((u_int32)pcmd->argval[0].uval);
2566 res = doquery(impl_ver, req, 1, 1, sizeof(u_int32),
2567 (char *)&key, &items, &itemsize, &dummy, 0,
2570 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2571 impl_ver = IMPL_XNTPD_OLD;
2576 (void) fprintf(fp, "done!\n");
2583 * ctlstats - obtain and print info about authentication
2592 struct info_control *ic;
2598 res = doquery(impl_ver, REQ_GET_CTLSTATS, 0, 0, 0, NULL, &items,
2599 &itemsize, (void *)&ic, 0, sizeof(*ic));
2601 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2602 impl_ver = IMPL_XNTPD_OLD;
2609 if (!check1item(items, fp))
2612 if (!checkitemsize(itemsize, sizeof(*ic)))
2615 fprintf(fp, "time since reset: %lu\n",
2616 (u_long)ntohl(ic->ctltimereset));
2617 fprintf(fp, "requests received: %lu\n",
2618 (u_long)ntohl(ic->numctlreq));
2619 fprintf(fp, "responses sent: %lu\n",
2620 (u_long)ntohl(ic->numctlresponses));
2621 fprintf(fp, "fragments sent: %lu\n",
2622 (u_long)ntohl(ic->numctlfrags));
2623 fprintf(fp, "async messages sent: %lu\n",
2624 (u_long)ntohl(ic->numasyncmsgs));
2625 fprintf(fp, "error msgs sent: %lu\n",
2626 (u_long)ntohl(ic->numctlerrors));
2627 fprintf(fp, "total bad pkts: %lu\n",
2628 (u_long)ntohl(ic->numctlbadpkts));
2629 fprintf(fp, "packet too short: %lu\n",
2630 (u_long)ntohl(ic->numctltooshort));
2631 fprintf(fp, "response on input: %lu\n",
2632 (u_long)ntohl(ic->numctlinputresp));
2633 fprintf(fp, "fragment on input: %lu\n",
2634 (u_long)ntohl(ic->numctlinputfrag));
2635 fprintf(fp, "error set on input: %lu\n",
2636 (u_long)ntohl(ic->numctlinputerr));
2637 fprintf(fp, "bad offset on input: %lu\n",
2638 (u_long)ntohl(ic->numctlbadoffset));
2639 fprintf(fp, "bad version packets: %lu\n",
2640 (u_long)ntohl(ic->numctlbadversion));
2641 fprintf(fp, "data in pkt too short: %lu\n",
2642 (u_long)ntohl(ic->numctldatatooshort));
2643 fprintf(fp, "unknown op codes: %lu\n",
2644 (u_long)ntohl(ic->numctlbadop));
2649 * clockstat - get and print clock status information
2657 struct info_clock *cl;
2658 /* 8 is the maximum number of clocks which will fit in a packet */
2659 u_long clist[min(MAXARGS, 8)];
2666 struct clktype *clk;
2668 qitemlim = min(pcmd->nargs, COUNTOF(clist));
2669 for (qitems = 0; qitems < qitemlim; qitems++)
2670 clist[qitems] = NSRCADR(&pcmd->argval[qitems].netnum);
2673 res = doquery(impl_ver, REQ_GET_CLOCKINFO, 0, qitems,
2674 sizeof(u_int32), (char *)clist, &items,
2675 &itemsize, (void *)&cl, 0, sizeof(struct info_clock));
2677 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2678 impl_ver = IMPL_XNTPD_OLD;
2685 if (!checkitems(items, fp))
2688 if (!checkitemsize(itemsize, sizeof(struct info_clock)))
2691 while (items-- > 0) {
2692 (void) fprintf(fp, "clock address: %s\n",
2693 numtoa(cl->clockadr));
2694 for (clk = clktypes; clk->code >= 0; clk++)
2695 if (clk->code == cl->type)
2698 (void) fprintf(fp, "clock type: %s\n",
2701 (void) fprintf(fp, "clock type: unknown type (%d)\n",
2703 (void) fprintf(fp, "last event: %d\n",
2705 (void) fprintf(fp, "current status: %d\n",
2707 (void) fprintf(fp, "number of polls: %lu\n",
2708 (u_long)ntohl(cl->polls));
2709 (void) fprintf(fp, "no response to poll: %lu\n",
2710 (u_long)ntohl(cl->noresponse));
2711 (void) fprintf(fp, "bad format responses: %lu\n",
2712 (u_long)ntohl(cl->badformat));
2713 (void) fprintf(fp, "bad data responses: %lu\n",
2714 (u_long)ntohl(cl->baddata));
2715 (void) fprintf(fp, "running time: %lu\n",
2716 (u_long)ntohl(cl->timestarted));
2717 NTOHL_FP(&cl->fudgetime1, &ts);
2718 (void) fprintf(fp, "fudge time 1: %s\n",
2720 NTOHL_FP(&cl->fudgetime2, &ts);
2721 (void) fprintf(fp, "fudge time 2: %s\n",
2723 (void) fprintf(fp, "stratum: %ld\n",
2724 (u_long)ntohl(cl->fudgeval1));
2725 /* [Bug3527] Backward Incompatible: cl->fudgeval2 is
2726 * a string, instantiated via memcpy() so there is no
2727 * endian issue to correct.
2729 #ifdef DISABLE_BUG3527_FIX
2730 (void) fprintf(fp, "reference ID: %s\n",
2731 refid_string(ntohl(cl->fudgeval2), 0));
2733 (void) fprintf(fp, "reference ID: %s\n",
2734 refid_string(cl->fudgeval2, 0));
2736 (void) fprintf(fp, "fudge flags: 0x%x\n",
2740 (void) fprintf(fp, "\n");
2747 * fudge - set clock fudge factors
2755 struct conf_fudge fudgedata;
2768 fudgedata.clockadr = NSRCADR(&pcmd->argval[0].netnum);
2770 if (STREQ(pcmd->argval[1].string, "time1")) {
2771 fudgedata.which = htonl(FUDGE_TIME1);
2772 if (!atolfp(pcmd->argval[2].string, &ts))
2775 NTOHL_FP(&ts, &fudgedata.fudgetime);
2776 } else if (STREQ(pcmd->argval[1].string, "time2")) {
2777 fudgedata.which = htonl(FUDGE_TIME2);
2778 if (!atolfp(pcmd->argval[2].string, &ts))
2781 NTOHL_FP(&ts, &fudgedata.fudgetime);
2782 } else if (STREQ(pcmd->argval[1].string, "val1")) {
2783 fudgedata.which = htonl(FUDGE_VAL1);
2784 if (!atoint(pcmd->argval[2].string, &val))
2787 fudgedata.fudgeval_flags = htonl(val);
2788 } else if (STREQ(pcmd->argval[1].string, "val2")) {
2789 fudgedata.which = htonl(FUDGE_VAL2);
2790 if (!atoint(pcmd->argval[2].string, &val))
2793 fudgedata.fudgeval_flags = htonl((u_int32)val);
2794 } else if (STREQ(pcmd->argval[1].string, "flags")) {
2795 fudgedata.which = htonl(FUDGE_FLAGS);
2796 if (!hextoint(pcmd->argval[2].string, &u_val))
2799 fudgedata.fudgeval_flags = htonl((u_int32)(u_val & 0xf));
2801 (void) fprintf(stderr, "What fudge is %s?\n",
2802 pcmd->argval[1].string);
2807 (void) fprintf(stderr, "Unknown fudge parameter %s\n",
2808 pcmd->argval[2].string);
2813 res = doquery(impl_ver, REQ_SET_CLKFUDGE, 1, 1,
2814 sizeof(struct conf_fudge), (char *)&fudgedata, &items,
2815 &itemsize, &dummy, 0, sizeof(dummy));
2817 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2818 impl_ver = IMPL_XNTPD_OLD;
2823 (void) fprintf(fp, "done!\n");
2828 * clkbug - get and print clock debugging information
2839 struct info_clkbug *cl;
2840 /* 8 is the maximum number of clocks which will fit in a packet */
2841 u_long clist[min(MAXARGS, 8)];
2851 qitemlim = min(pcmd->nargs, COUNTOF(clist));
2852 for (qitems = 0; qitems < qitemlim; qitems++)
2853 clist[qitems] = NSRCADR(&pcmd->argval[qitems].netnum);
2856 res = doquery(impl_ver, REQ_GET_CLKBUGINFO, 0, qitems,
2857 sizeof(u_int32), (char *)clist, &items,
2858 &itemsize, (void *)&cl, 0, sizeof(struct info_clkbug));
2860 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2861 impl_ver = IMPL_XNTPD_OLD;
2868 if (!checkitems(items, fp))
2871 if (!checkitemsize(itemsize, sizeof(struct info_clkbug)))
2874 while (items-- > 0) {
2875 (void) fprintf(fp, "clock address: %s\n",
2876 numtoa(cl->clockadr));
2877 n = (int)cl->nvalues;
2878 (void) fprintf(fp, "values: %d", n);
2879 s = ntohs(cl->svalues);
2880 if (n > NUMCBUGVALUES)
2882 for (i = 0; i < n; i++) {
2883 ltemp = ntohl(cl->values[i]);
2884 ltemp &= 0xffffffff; /* HMS: This does nothing now */
2886 (void) fprintf(fp, "\n");
2888 (void) fprintf(fp, "%12ld", (u_long)ltemp);
2890 (void) fprintf(fp, "%12lu", (u_long)ltemp);
2892 (void) fprintf(fp, "\n");
2894 n = (int)cl->ntimes;
2895 (void) fprintf(fp, "times: %d", n);
2896 s = ntohl(cl->stimes);
2897 if (n > NUMCBUGTIMES)
2900 for (i = 0; i < n; i++) {
2901 if ((i & 0x1) == 0) {
2902 (void) fprintf(fp, "\n");
2904 for (;needsp > 0; needsp--)
2907 NTOHL_FP(&cl->times[i], &ts);
2909 (void) fprintf(fp, "%17s",
2913 (void) fprintf(fp, "%37s",
2918 (void) fprintf(fp, "\n");
2921 (void) fprintf(fp, "\n");
2928 * kerninfo - display the kernel pll/pps variables
2936 struct info_kernel *ik;
2941 double tscale_usec = 1e-6, tscale_unano = 1e-6;
2944 res = doquery(impl_ver, REQ_GET_KERNEL, 0, 0, 0, (char *)NULL,
2945 &items, &itemsize, (void *)&ik, 0,
2946 sizeof(struct info_kernel));
2948 if (res == INFO_ERR_IMPL && impl_ver == IMPL_XNTPD) {
2949 impl_ver = IMPL_XNTPD_OLD;
2955 if (!check1item(items, fp))
2957 if (!checkitemsize(itemsize, sizeof(struct info_kernel)))
2960 status = ntohs(ik->status) & 0xffff;
2962 * pll variables. We know more than we should about the NANO bit.
2965 if (status & STA_NANO)
2966 tscale_unano = 1e-9;
2968 (void)fprintf(fp, "pll offset: %g s\n",
2969 (int32)ntohl(ik->offset) * tscale_unano);
2970 (void)fprintf(fp, "pll frequency: %s ppm\n",
2971 fptoa((s_fp)ntohl(ik->freq), 3));
2972 (void)fprintf(fp, "maximum error: %g s\n",
2973 (u_long)ntohl(ik->maxerror) * tscale_usec);
2974 (void)fprintf(fp, "estimated error: %g s\n",
2975 (u_long)ntohl(ik->esterror) * tscale_usec);
2976 (void)fprintf(fp, "status: %04x ", status);
2978 if (status & STA_PLL) (void)fprintf(fp, " pll");
2981 if (status & STA_PPSFREQ) (void)fprintf(fp, " ppsfreq");
2984 if (status & STA_PPSTIME) (void)fprintf(fp, " ppstime");
2987 if (status & STA_FLL) (void)fprintf(fp, " fll");
2990 if (status & STA_INS) (void)fprintf(fp, " ins");
2993 if (status & STA_DEL) (void)fprintf(fp, " del");
2996 if (status & STA_UNSYNC) (void)fprintf(fp, " unsync");
2999 if (status & STA_FREQHOLD) (void)fprintf(fp, " freqhold");
3001 #ifdef STA_PPSSIGNAL
3002 if (status & STA_PPSSIGNAL) (void)fprintf(fp, " ppssignal");
3004 #ifdef STA_PPSJITTER
3005 if (status & STA_PPSJITTER) (void)fprintf(fp, " ppsjitter");
3007 #ifdef STA_PPSWANDER
3008 if (status & STA_PPSWANDER) (void)fprintf(fp, " ppswander");
3011 if (status & STA_PPSERROR) (void)fprintf(fp, " ppserror");
3014 if (status & STA_CLOCKERR) (void)fprintf(fp, " clockerr");
3017 if (status & STA_NANO) (void)fprintf(fp, " nano");
3020 if (status & STA_MODE) (void)fprintf(fp, " mode=fll");
3023 if (status & STA_CLK) (void)fprintf(fp, " src=B");
3025 (void)fprintf(fp, "\n");
3026 (void)fprintf(fp, "pll time constant: %ld\n",
3027 (u_long)ntohl(ik->constant));
3028 (void)fprintf(fp, "precision: %g s\n",
3029 (u_long)ntohl(ik->precision) * tscale_usec);
3030 (void)fprintf(fp, "frequency tolerance: %s ppm\n",
3031 fptoa((s_fp)ntohl(ik->tolerance), 0));
3034 * For backwards compatibility (ugh), we find the pps variables
3035 * only if the shift member is nonzero.
3043 (void)fprintf(fp, "pps frequency: %s ppm\n",
3044 fptoa((s_fp)ntohl(ik->ppsfreq), 3));
3045 (void)fprintf(fp, "pps stability: %s ppm\n",
3046 fptoa((s_fp)ntohl(ik->stabil), 3));
3047 (void)fprintf(fp, "pps jitter: %g s\n",
3048 (u_long)ntohl(ik->jitter) * tscale_unano);
3049 (void)fprintf(fp, "calibration interval: %d s\n",
3050 1 << ntohs(ik->shift));
3051 (void)fprintf(fp, "calibration cycles: %ld\n",
3052 (u_long)ntohl(ik->calcnt));
3053 (void)fprintf(fp, "jitter exceeded: %ld\n",
3054 (u_long)ntohl(ik->jitcnt));
3055 (void)fprintf(fp, "stability exceeded: %ld\n",
3056 (u_long)ntohl(ik->stbcnt));
3057 (void)fprintf(fp, "calibration errors: %ld\n",
3058 (u_long)ntohl(ik->errcnt));
3061 #define IF_LIST_FMT "%2d %c %48s %c %c %12.12s %03lx %3lu %2lu %5lu %5lu %5lu %2lu %3lu %7lu\n"
3062 #define IF_LIST_FMT_STR "%2s %c %48s %c %c %12.12s %3s %3s %2s %5s %5s %5s %2s %3s %7s\n"
3063 #define IF_LIST_AFMT_STR " %48s %c\n"
3064 #define IF_LIST_LABELS "#", 'A', "Address/Mask/Broadcast", 'T', 'E', "IF name", "Flg", "TL", "#M", "recv", "sent", "drop", "S", "PC", "uptime"
3065 #define IF_LIST_LINE "==================================================================================================================\n"
3070 struct info_if_stats *ifs,
3076 static const char *actions = "?.+-";
3082 if (!checkitems(items, fp))
3085 if (!checkitemsize(itemsize, sizeof(struct info_if_stats)))
3088 fprintf(fp, IF_LIST_FMT_STR, IF_LIST_LABELS);
3089 fprintf(fp, IF_LIST_LINE);
3092 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3093 ifs->unaddr.addr.s_addr, ifs->unaddr.addr6);
3094 fprintf(fp, IF_LIST_FMT,
3096 actions[(ifs->action >= 1 && ifs->action < 4) ? ifs->action : 0],
3097 stoa((&saddr)), 'A',
3098 ifs->ignore_packets ? 'D' : 'E',
3100 (u_long)ntohl(ifs->flags),
3101 (u_long)ntohl(ifs->last_ttl),
3102 (u_long)ntohl(ifs->num_mcast),
3103 (u_long)ntohl(ifs->received),
3104 (u_long)ntohl(ifs->sent),
3105 (u_long)ntohl(ifs->notsent),
3106 (u_long)ntohl(ifs->scopeid),
3107 (u_long)ntohl(ifs->peercnt),
3108 (u_long)ntohl(ifs->uptime));
3110 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3111 ifs->unmask.addr.s_addr, ifs->unmask.addr6);
3112 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'M');
3114 if (!ntohl(ifs->v6_flag) && ntohl(ifs->flags) & (INT_BCASTOPEN)) {
3115 SET_ADDR(saddr, ntohl(ifs->v6_flag),
3116 ifs->unbcast.addr.s_addr, ifs->unbcast.addr6);
3117 fprintf(fp, IF_LIST_AFMT_STR, stoa(&saddr), 'B');
3133 struct info_if_stats *ifs;
3138 res = doquery(impl_ver, REQ_IF_STATS, 1, 0, 0, (char *)NULL, &items,
3139 &itemsize, (void *)&ifs, 0,
3140 sizeof(struct info_if_stats));
3141 iflist(fp, ifs, items, itemsize, res);
3151 struct info_if_stats *ifs;
3156 res = doquery(impl_ver, REQ_IF_RELOAD, 1, 0, 0, (char *)NULL, &items,
3157 &itemsize, (void *)&ifs, 0,
3158 sizeof(struct info_if_stats));
3159 iflist(fp, ifs, items, itemsize, res);