1 /* $NetBSD: ntpdc_ops.c,v 1.7 2007/01/06 19:45:23 kardel Exp $ */
4 * ntpdc_ops.c - subroutines which are called to perform operations by xntpdc
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 #include <netinet/in.h>
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
},
110 "display local server information" },
111 { "sysstats", sysstats
, { NO
, NO
, NO
, NO
},
113 "display local server statistics" },
114 { "memstats", memstats
, { NO
, NO
, NO
, NO
},
116 "display peer memory usage statistics" },
117 { "iostats", iostats
, { NO
, NO
, NO
, NO
},
119 "display I/O subsystem statistics" },
120 { "timerstats", timerstats
, { NO
, NO
, NO
, NO
},
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
},
149 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
151 "create restrict entry/add flags to entry" },
152 { "unrestrict", unrestrict
, { NTP_ADD
, NTP_ADD
, NTP_STR
, OPT
|NTP_STR
},
154 "ntpport|ignore|noserve|notrust|noquery|nomodify|nopeer|version|kod",
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
},
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
},
180 "display the state of the authentication code" },
181 { "traps", traps
, { NO
, NO
, NO
, NO
},
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
},
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
},
210 "display the kernel pll/pps variables" },
211 { "ifstats", get_if_stats
, { NO
, NO
, NO
, NO
},
213 "list interface statistics" },
214 { "ifreload", do_if_reload
, { NO
, NO
, NO
, NO
},
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
237 (void) fprintf(fp
, "No data returned in response to query\n");
245 * checkitemsize - utility to print a message if the item size is wrong
253 if (itemsize
!= expected
) {
254 (void) fprintf(stderr
,
255 "***Incorrect item size returned by remote host (%d should be %d)\n",
264 * check1item - check to make sure we have exactly one item
273 (void) fprintf(fp
, "No data returned in response to query\n");
277 (void) fprintf(fp
, "Expected one item in response, got %d\n",
287 * peerlist - get a short list of peers
296 struct info_peer_list
*plist
;
297 struct sockaddr_storage paddr
;
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
;
315 if (!checkitems(items
, fp
))
318 if (!checkitemsize(itemsize
, sizeof(struct info_peer_list
)) &&
319 !checkitemsize(itemsize
, v4sizeof(struct info_peer_list
)))
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
;
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
);
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
),
347 * peers - show peer summary
355 dopeers(pcmd
, fp
, 0);
359 * dmpeers - show peer summary, Dave Mills style
367 dopeers(pcmd
, fp
, 1);
372 * peers - show peer summary
382 struct info_peer_summary
*plist
;
383 struct sockaddr_storage dstadr
;
384 struct sockaddr_storage srcadr
;
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
;
405 if (!checkitems(items
, fp
))
408 if (!checkitemsize(itemsize
, sizeof(struct info_peer_summary
)) &&
409 !checkitemsize(itemsize
, v4sizeof(struct info_peer_summary
)))
413 " remote local st poll reach delay offset disp\n");
415 "=======================================================================\n");
418 if (plist
->flags
& INFO_FLAG_SYSPEER
)
420 else if (plist
->hmode
== MODE_ACTIVE
)
422 else if (plist
->hmode
== MODE_PASSIVE
)
424 else if (plist
->hmode
== MODE_CLIENT
)
426 else if (plist
->hmode
== MODE_BROADCAST
)
428 else if (plist
->hmode
== MODE_BCLIENT
)
433 if (plist
->flags
& INFO_FLAG_SYSPEER
)
435 else if (plist
->flags
& INFO_FLAG_SHORTLIST
)
437 else if (plist
->flags
& INFO_FLAG_SEL_CANDIDATE
)
442 NTOHL_FP(&(plist
->offset
), &tempts
);
443 ntp_poll
= 1<<max(min3(plist
->ppoll
, plist
->hpoll
, NTP_MAXPOLL
),
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
;
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
);
462 if ((pcmd
->nargs
== 0) ||
463 ((pcmd
->argval
->ival
== 6) && (plist
->v6_flag
!= 0)) ||
464 ((pcmd
->argval
->ival
== 4) && (plist
->v6_flag
== 0)))
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),
471 ufptoa(NTOHS_FP(plist
->dispersion
), 5));
477 /* Convert a refid & stratum (in host order) to a string */
487 memmove(junk
, (char *)&refid
, 4);
491 return numtoa(refid
);
503 (void) fprintf(fp
, " none\n");
506 if (flags
& INFO_FLAG_SYSPEER
) {
507 (void) fprintf(fp
, " system_peer");
510 if (flags
& INFO_FLAG_CONFIG
) {
511 (void) fprintf(fp
, "%s config", str
);
514 if (flags
& INFO_FLAG_REFCLOCK
) {
515 (void) fprintf(fp
, "%s refclock", str
);
518 if (flags
& INFO_FLAG_AUTHENABLE
) {
519 (void) fprintf(fp
, "%s auth", str
);
522 if (flags
& INFO_FLAG_BCLIENT
) {
523 (void) fprintf(fp
, "%s bclient", str
);
526 if (flags
& INFO_FLAG_PREFER
) {
527 (void) fprintf(fp
, "%s prefer", str
);
530 if (flags
& INFO_FLAG_IBURST
) {
531 (void) fprintf(fp
, "%s iburst", 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
545 register struct info_peer
*pp
,
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
;
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
);
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
);
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));
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
));
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
);
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
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)];
665 if (impl_ver
== IMPL_XNTPD
)
666 sendsize
= sizeof(struct info_peer_list
);
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
)
676 if (impl_ver
== IMPL_XNTPD_OLD
) {
678 "***Server doesn't understand IPv6 addresses\n");
681 pl
->addr6
= GET_INADDR6(pcmd
->argval
[qitems
].netnum
);
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
;
701 if (!checkitems(items
, fp
))
704 if (!checkitemsize(itemsize
, sizeof(struct info_peer
)) &&
705 !checkitemsize(itemsize
, v4sizeof(struct info_peer
)))
708 while (items
-- > 0) {
711 (void) fprintf(fp
, "\n");
718 * peerstats - return statistics for a peer
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
;
737 if (impl_ver
== IMPL_XNTPD
)
738 sendsize
= sizeof(struct info_peer_list
);
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
)
749 if (impl_ver
== IMPL_XNTPD_OLD
) {
751 "***Server doesn't understand IPv6 addresses\n");
754 pl
->addr6
= GET_INADDR6(pcmd
->argval
[qitems
].netnum
);
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
;
775 if (!checkitems(items
, fp
))
778 if (!checkitemsize(itemsize
, sizeof(struct info_peer_stats
)) &&
779 !checkitemsize(itemsize
, v4sizeof(struct info_peer_stats
)))
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
;
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
);
800 (void) fprintf(fp
, "remote host: %s\n",
802 (void) fprintf(fp
, "local interface: %s\n",
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",
827 (void) fprintf(fp
, "\n");
828 (void) fprintf(fp
, "flags: ");
829 print_pflag(fp
, ntohs(pp
->flags
));
836 * loopinfo - show loop filter information
844 struct info_loop
*il
;
851 if (pcmd
->nargs
> 0) {
852 if (STREQ(pcmd
->argval
[0].string
, "oneline"))
854 else if (STREQ(pcmd
->argval
[0].string
, "multiline"))
857 (void) fprintf(stderr
, "How many lines?\n");
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
;
875 if (!check1item(items
, fp
))
878 if (!checkitemsize(itemsize
, sizeof(struct info_loop
)))
884 NTOHL_FP(&il
->last_offset
, &tempts
);
885 NTOHL_FP(&il
->drift_comp
, &temp2ts
);
888 "offset %s, frequency %s, time_const %ld, watchdog %ld\n",
891 (long)(int32_t)ntohl((u_long
)il
->compliance
),
892 (u_long
)ntohl((u_long
)il
->watchdog_timer
));
894 NTOHL_FP(&il
->last_offset
, &tempts
);
895 (void) fprintf(fp
, "offset: %s s\n",
897 NTOHL_FP(&il
->drift_comp
, &tempts
);
898 (void) fprintf(fp
, "frequency: %s ppm\n",
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
919 struct sockaddr_storage peeraddr
;
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
;
938 if (!check1item(items
, fp
))
941 if (!checkitemsize(itemsize
, sizeof(struct info_sys
)) &&
942 !checkitemsize(itemsize
, v4sizeof(struct info_sys
)))
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
;
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
);
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");
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
1017 struct info_sys_stats
*ss
;
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
;
1035 if (!check1item(items
, fp
))
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
));
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
))
1067 fprintf(fp
, "rate exceeded: %ld\n",
1068 (u_long
)ntohl(ss
->limitrejected
));
1074 * iostats - print I/O statistics
1083 struct info_io_stats
*io
;
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
;
1101 if (!check1item(items
, fp
))
1104 if (!checkitemsize(itemsize
, sizeof(struct info_io_stats
)))
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
1144 struct info_mem_stats
*mem
;
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
;
1163 if (!check1item(items
, fp
))
1166 if (!checkitemsize(itemsize
, sizeof(struct info_mem_stats
)))
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
1204 struct info_timer_stats
*tim
;
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
;
1222 if (!check1item(items
, fp
))
1225 if (!checkitemsize(itemsize
, sizeof(struct info_timer_stats
)))
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
1248 doconfig(pcmd
, fp
, MODE_ACTIVE
, 0);
1253 * addserver - configure a client mode association
1261 doconfig(pcmd
, fp
, MODE_CLIENT
, 0);
1265 * addrefclock - configure a reference clock association
1273 doconfig(pcmd
, fp
, MODE_CLIENT
, 1);
1277 * broadcast - configure a broadcast mode association
1285 doconfig(pcmd
, fp
, MODE_BROADCAST
, 0);
1290 * config - configure a new peer association
1300 struct conf_peer cpeer
;
1320 minpoll
= NTP_MINDPOLL
;
1321 maxpoll
= NTP_MAXDPOLL
;
1326 if (impl_ver
== IMPL_XNTPD
)
1327 sendsize
= sizeof(struct conf_peer
);
1329 sendsize
= v4sizeof(struct conf_peer
);
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"))
1343 else if (!refc
&& STREQ(pcmd
->argval
[items
].string
, "version"))
1345 else if (STREQ(pcmd
->argval
[items
].string
, "minpoll"))
1347 else if (STREQ(pcmd
->argval
[items
].string
, "maxpoll"))
1351 if (!atoint(pcmd
->argval
[items
].string
, &val
))
1360 version
= (u_int
) val
;
1365 minpoll
= (u_char
)val
;
1370 maxpoll
= (u_char
)val
;
1375 cmode
= (u_char
)val
;
1380 (void) fprintf(fp
, "*** '%s' not understood\n",
1381 pcmd
->argval
[items
].string
);
1386 (void) fprintf(stderr
,
1387 "***Value '%s' should be unsigned\n",
1388 pcmd
->argval
[items
].string
);
1395 flags
|= CONF_FLAG_AUTHENABLE
;
1396 if (version
> NTP_VERSION
||
1397 version
< NTP_OLDVERSION
) {
1398 (void)fprintf(fp
, "***invalid version number: %u\n",
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
);
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
)
1420 if (impl_ver
== IMPL_XNTPD_OLD
) {
1422 "***Server doesn't understand IPv6 addresses\n");
1425 cpeer
.peeraddr6
= GET_INADDR6(pcmd
->argval
[0].netnum
);
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
;
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
;
1445 if (res
== INFO_ERR_FMT
) {
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
));
1454 (void) fprintf(fp
, "done!\n");
1460 * unconfig - unconfigure some associations
1468 /* 8 is the maximum number of peers which will fit in a packet */
1469 struct conf_unpeer
*pl
, plist
[min(MAXARGS
, 8)];
1478 if (impl_ver
== IMPL_XNTPD
)
1479 sendsize
= sizeof(struct conf_unpeer
);
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
)
1489 if (impl_ver
== IMPL_XNTPD_OLD
) {
1491 "***Server doesn't understand IPv6 addresses\n");
1495 GET_INADDR6(pcmd
->argval
[qitems
].netnum
);
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
;
1511 (void) fprintf(fp
, "done!\n");
1516 * set - set some system flags
1524 doset(pcmd
, fp
, REQ_SET_SYS_FLAG
);
1529 * clear - clear some system flags
1537 doset(pcmd
, fp
, REQ_CLR_SYS_FLAG
);
1542 * doset - set/clear system flags
1551 /* 8 is the maximum number of peers which will fit in a packet */
1552 struct conf_sys_flags sys
;
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
;
1578 (void) fprintf(fp
, "Unknown flag %s\n",
1579 pcmd
->argval
[items
].string
);
1584 sys
.flags
= htonl(sys
.flags
);
1585 if (res
|| sys
.flags
== 0)
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
;
1599 (void) fprintf(fp
, "done!\n");
1604 * data for printing/interrpreting the restrict flags
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 },
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
},
1643 static struct resflags resmflags
[] = {
1644 { "ntpport", RESM_NTPONLY
},
1645 { "interface", RESM_INTERFACE
},
1651 * reslist - obtain and print the server's restrict list
1660 struct info_restrict
*rl
;
1661 struct sockaddr_storage resaddr
;
1662 struct sockaddr_storage maskaddr
;
1669 struct resflags
*rf
;
1674 static const char *comma
= ", ";
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
;
1689 if (!checkitems(items
, fp
))
1692 if (!checkitemsize(itemsize
, sizeof(struct info_restrict
)) &&
1693 !checkitemsize(itemsize
, v4sizeof(struct info_restrict
)))
1697 " address mask count flags\n");
1699 "=====================================================================\n");
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
);
1712 addr
= nntohost(&resaddr
);
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
);
1721 if ((rl
->mask
== (u_int32
)0xffffffff))
1722 addr
= nntohost(&resaddr
);
1724 addr
= stoa(&resaddr
);
1726 mask
= stoa(&maskaddr
);
1728 if ((pcmd
->nargs
== 0) ||
1729 ((pcmd
->argval
->ival
== 6) && (rl
->v6_flag
!= 0)) ||
1730 ((pcmd
->argval
->ival
== 4) && (rl
->v6_flag
== 0)))
1732 count
= ntohl(rl
->count
);
1733 flags
= ntohs(rl
->flags
);
1734 mflags
= ntohs(rl
->mflags
);
1739 while (rf
->bit
!= 0) {
1740 if (mflags
& rf
->bit
) {
1742 (void) strcat(flagstr
, comma
);
1744 (void) strcat(flagstr
, rf
->str
);
1749 rf
= (impl_ver
== IMPL_XNTPD_OLD
)
1753 while (rf
->bit
!= 0) {
1754 if (flags
& rf
->bit
) {
1756 (void) strcat(flagstr
, comma
);
1758 (void) strcat(flagstr
, rf
->str
);
1763 if (flagstr
[0] == '\0')
1764 (void) strcpy(flagstr
, "none");
1767 (void) fprintf(fp
, "%-15.15s %-15.15s %9ld %s\n",
1768 addr
, mask
, (u_long
)count
, flagstr
);
1777 * new_restrict - create/add a set of restrictions
1785 do_restrict(pcmd
, fp
, REQ_RESADDFLAGS
);
1790 * unrestrict - remove restriction flags from existing entry
1798 do_restrict(pcmd
, fp
, REQ_RESSUBFLAGS
);
1803 * delrestrict - delete an existing restriction
1811 do_restrict(pcmd
, fp
, REQ_UNRESTRICT
);
1816 * do_restrict - decode commandline restrictions and make the request
1825 struct conf_restrict cres
;
1836 /* Initialize cres */
1844 if (impl_ver
== IMPL_XNTPD
)
1845 sendsize
= sizeof(struct conf_restrict
);
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
)
1855 if (impl_ver
== IMPL_XNTPD_OLD
) {
1857 "***Server doesn't understand IPv6 addresses\n");
1860 cres
.addr6
= GET_INADDR6(pcmd
->argval
[0].netnum
);
1866 for (res
= 2; res
< pcmd
->nargs
; res
++) {
1867 if (STREQ(pcmd
->argval
[res
].string
, "ntpport")) {
1868 cres
.mflags
|= RESM_NTPONLY
;
1870 for (i
= 0; resflagsV3
[i
].bit
!= 0; i
++) {
1871 if (STREQ(pcmd
->argval
[res
].string
,
1875 if (resflagsV3
[i
].bit
!= 0) {
1876 cres
.flags
|= resflagsV3
[i
].bit
;
1877 if (req_code
== REQ_UNRESTRICT
) {
1879 "Flag %s inappropriate\n",
1884 (void) fprintf(fp
, "Unknown flag %s\n",
1885 pcmd
->argval
[res
].string
);
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) {
1901 num
= ntohl(cres
.mask
);
1902 for (bit
= 0x80000000; bit
!= 0; bit
>>= 1)
1903 if ((num
& bit
) == 0)
1905 for ( ; bit
!= 0; bit
>>= 1)
1906 if ((num
& bit
) != 0)
1909 (void) fprintf(fp
, "Invalid mask %s\n",
1915 /* XXX IPv6 sanity checking stuff */
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
;
1931 (void) fprintf(fp
, "done!\n");
1937 * monlist - obtain and print the server's monitor data
1947 struct sockaddr_storage addr
;
1948 struct sockaddr_storage dstadr
;
1954 if (pcmd
->nargs
> 0) {
1955 version
= pcmd
->argval
[0].ival
;
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
;
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
));
1979 if (!checkitems(items
, fp
))
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
;
1987 "remote address port local address count m ver code avgint lstint\n");
1989 "===============================================================================\n");
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
;
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
);
2008 if ((pcmd
->nargs
== 0) ||
2009 ((pcmd
->argval
->ival
== 6) && (ml
->v6_flag
!= 0)) ||
2010 ((pcmd
->argval
->ival
== 4) && (ml
->v6_flag
== 0)))
2012 "%-22.22s %5d %-15s %8ld %1d %1d %6lx %6lu %7lu\n",
2016 (u_long
)ntohl(ml
->count
),
2019 (u_long
)ntohl(ml
->lastdrop
),
2020 (u_long
)ntohl(ml
->lasttime
),
2021 (u_long
)ntohl(ml
->firsttime
));
2025 } else if (itemsize
== sizeof(struct info_monitor
) ||
2026 itemsize
== v4sizeof(struct info_monitor
)) {
2027 struct info_monitor
*ml
= (struct info_monitor
*) struct_star
;
2030 " address port count mode ver code avgint lstint\n");
2032 "===============================================================================\n");
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
;
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
);
2045 if ((pcmd
->nargs
== 0) ||
2046 ((pcmd
->argval
->ival
== 6) && (ml
->v6_flag
!= 0)) ||
2047 ((pcmd
->argval
->ival
== 4) && (ml
->v6_flag
== 0)))
2049 "%-25.25s %5d %9ld %4d %2d %9lx %9lu %9lu\n",
2052 (u_long
)ntohl(ml
->count
),
2055 (u_long
)ntohl(ml
->lastdrop
),
2056 (u_long
)ntohl(ml
->lasttime
),
2057 (u_long
)ntohl(ml
->firsttime
));
2061 } else if (itemsize
== sizeof(struct old_info_monitor
)) {
2062 struct old_info_monitor
*oml
= (struct old_info_monitor
*)struct_star
;
2064 " address port count mode version lasttime firsttime\n");
2066 "======================================================================\n");
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
;
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
);
2079 (void) fprintf(fp
, "%-20.20s %5d %9ld %4d %3d %9lu %9lu\n",
2082 (u_long
)ntohl(oml
->count
),
2085 (u_long
)ntohl(oml
->lasttime
),
2086 (u_long
)ntohl(oml
->firsttime
));
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
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
},
2114 * reset - reset statistic counters
2122 struct reset_flags rflags
;
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
))
2137 if (sreset
[i
].flag
== 0) {
2138 (void) fprintf(fp
, "Flag %s unknown\n",
2139 pcmd
->argval
[res
].string
);
2142 rflags
.flags
|= sreset
[i
].flag
;
2145 rflags
.flags
= htonl(rflags
.flags
);
2148 (void) fprintf(fp
, "Not done due to errors\n");
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
;
2163 (void) fprintf(fp
, "done!\n");
2170 * preset - reset stat counters for particular peers
2178 /* 8 is the maximum number of peers which will fit in a packet */
2179 struct conf_unpeer
*pl
, plist
[min(MAXARGS
, 8)];
2188 if (impl_ver
== IMPL_XNTPD
)
2189 sendsize
= sizeof(struct conf_unpeer
);
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
)
2199 if (impl_ver
== IMPL_XNTPD_OLD
) {
2201 "***Server doesn't understand IPv6 addresses\n");
2205 GET_INADDR6(pcmd
->argval
[qitems
].netnum
);
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
;
2221 (void) fprintf(fp
, "done!\n");
2226 * readkeys - request the server to reread the keys file
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
;
2250 (void) fprintf(fp
, "done!\n");
2256 * trustkey - add some keys to the trusted key list
2264 do_trustkey(pcmd
, fp
, REQ_TRUSTKEY
);
2269 * untrustkey - remove some keys from the trusted key list
2277 do_trustkey(pcmd
, fp
, REQ_UNTRUSTKEY
);
2282 * do_trustkey - do grunge work of adding/deleting keys
2291 u_long keyids
[MAXARGS
];
2300 for (i
= 0; i
< pcmd
->nargs
; i
++) {
2301 keyids
[ritems
++] = pcmd
->argval
[i
].uval
;
2305 res
= doquery(impl_ver
, req
, 1, ritems
, sizeof(u_long
),
2306 (char *)keyids
, &items
, &itemsize
, &dummy
, 0,
2309 if (res
== INFO_ERR_IMPL
&& impl_ver
== IMPL_XNTPD
) {
2310 impl_ver
= IMPL_XNTPD_OLD
;
2315 (void) fprintf(fp
, "done!\n");
2322 * authinfo - obtain and print info about authentication
2331 struct info_auth
*ia
;
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
;
2349 if (!check1item(items
, fp
))
2352 if (!checkitemsize(itemsize
, sizeof(struct info_auth
)))
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
2388 struct info_trap
*it
;
2389 struct sockaddr_storage trap_addr
, local_addr
;
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
;
2407 if (!checkitems(items
, fp
))
2410 if (!checkitemsize(itemsize
, sizeof(struct info_trap
)) &&
2411 !checkitemsize(itemsize
, v4sizeof(struct info_trap
)))
2414 for (i
= 0; i
< items
; i
++ ) {
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
;
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
);
2434 (void) fprintf(fp
, "address %s, port %d\n",
2436 ntohs(it
->trap_port
));
2437 (void) fprintf(fp
, "interface: %s, ",
2438 (it
->local_address
== 0)
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");
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
2467 do_addclr_trap(pcmd
, fp
, REQ_ADD_TRAP
);
2472 * clrtrap - clear a trap from the server
2480 do_addclr_trap(pcmd
, fp
, REQ_CLR_TRAP
);
2485 * do_addclr_trap - do grunge work of adding/deleting traps
2494 struct conf_trap ctrap
;
2502 if (impl_ver
== IMPL_XNTPD
)
2503 sendsize
= sizeof(struct conf_trap
);
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
)
2512 if (impl_ver
== IMPL_XNTPD_OLD
) {
2514 "***Server doesn't understand IPv6 addresses\n");
2517 ctrap
.trap_address6
= GET_INADDR6(pcmd
->argval
[0].netnum
);
2520 ctrap
.local_address
= 0;
2521 ctrap
.trap_port
= htons(TRAPPORT
);
2524 if (pcmd
->nargs
> 1) {
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
) {
2531 "***Cannot mix IPv4 and IPv6 addresses\n");
2534 if (pcmd
->argval
[2].netnum
.ss_family
== AF_INET
)
2535 ctrap
.local_address
= GET_INADDR(pcmd
->argval
[2].netnum
);
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
;
2551 (void) fprintf(fp
, "done!\n");
2558 * requestkey - change the server's request key (a dangerous request)
2566 do_changekey(pcmd
, fp
, REQ_REQUEST_KEY
);
2571 * controlkey - change the server's control key
2579 do_changekey(pcmd
, fp
, REQ_CONTROL_KEY
);
2585 * do_changekey - do grunge work of changing keys
2601 key
= htonl((u_int32
)pcmd
->argval
[0].uval
);
2604 res
= doquery(impl_ver
, req
, 1, 1, sizeof(u_int32
),
2605 (char *)&key
, &items
, &itemsize
, &dummy
, 0,
2608 if (res
== INFO_ERR_IMPL
&& impl_ver
== IMPL_XNTPD
) {
2609 impl_ver
= IMPL_XNTPD_OLD
;
2614 (void) fprintf(fp
, "done!\n");
2621 * ctlstats - obtain and print info about authentication
2630 struct info_control
*ic
;
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
;
2648 if (!check1item(items
, fp
))
2651 if (!checkitemsize(itemsize
, sizeof(struct info_control
)))
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
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)];
2704 struct clktype
*clk
;
2706 for (qitems
= 0; qitems
< min(pcmd
->nargs
, 8); qitems
++)
2707 clist
[qitems
] = GET_INADDR(pcmd
->argval
[qitems
].netnum
);
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
;
2722 if (!checkitems(items
, fp
))
2725 if (!checkitemsize(itemsize
, sizeof(struct info_clock
)))
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
)
2735 (void) fprintf(fp
, "clock type: %s\n",
2738 (void) fprintf(fp
, "clock type: unknown type (%d)\n",
2740 (void) fprintf(fp
, "last event: %d\n",
2742 (void) fprintf(fp
, "current status: %d\n",
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",
2757 NTOHL_FP(&cl
->fudgetime2
, &ts
);
2758 (void) fprintf(fp
, "fudge time 2: %s\n",
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",
2768 (void) fprintf(fp
, "\n");
2775 * fudge - set clock fudge factors
2783 struct conf_fudge fudgedata
;
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
))
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
))
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
))
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
))
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
))
2827 fudgedata
.fudgeval_flags
= htonl((u_int32
)(u_val
& 0xf));
2829 (void) fprintf(stderr
, "What fudge is %s?\n",
2830 pcmd
->argval
[1].string
);
2835 (void) fprintf(stderr
, "Unknown fudge parameter %s\n",
2836 pcmd
->argval
[2].string
);
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
;
2851 (void) fprintf(fp
, "done!\n");
2856 * clkbug - get and print clock debugging information
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)];
2878 for (qitems
= 0; qitems
< min(pcmd
->nargs
, 8); qitems
++)
2879 clist
[qitems
] = GET_INADDR(pcmd
->argval
[qitems
].netnum
);
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
;
2894 if (!checkitems(items
, fp
))
2897 if (!checkitemsize(itemsize
, sizeof(struct info_clkbug
)))
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
)
2908 for (i
= 0; i
< n
; i
++) {
2909 ltemp
= ntohl(cl
->values
[i
]);
2910 ltemp
&= 0xffffffff; /* HMS: This does nothing now */
2912 (void) fprintf(fp
, "\n");
2914 (void) fprintf(fp
, "%12ld", (u_long
)ltemp
);
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
)
2926 for (i
= 0; i
< n
; i
++) {
2927 if ((i
& 0x1) == 0) {
2928 (void) fprintf(fp
, "\n");
2930 for (;needsp
> 0; needsp
--)
2933 NTOHL_FP(&cl
->times
[i
], &ts
);
2935 (void) fprintf(fp
, "%17s",
2939 (void) fprintf(fp
, "%37s",
2944 (void) fprintf(fp
, "\n");
2947 (void) fprintf(fp
, "\n");
2954 * kerninfo - display the kernel pll/pps variables
2962 struct info_kernel
*ik
;
2967 double tscale
= 1e-6;
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
;
2981 if (!check1item(items
, fp
))
2983 if (!checkitemsize(itemsize
, sizeof(struct info_kernel
)))
2986 status
= ntohs(ik
->status
) & 0xffff;
2988 * pll variables. We know more than we should about the NANO bit.
2991 if (status
& STA_NANO
)
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
);
3004 if (status
& STA_PLL
) (void)fprintf(fp
, " pll");
3007 if (status
& STA_PPSFREQ
) (void)fprintf(fp
, " ppsfreq");
3010 if (status
& STA_PPSTIME
) (void)fprintf(fp
, " ppstime");
3013 if (status
& STA_FLL
) (void)fprintf(fp
, " fll");
3016 if (status
& STA_INS
) (void)fprintf(fp
, " ins");
3019 if (status
& STA_DEL
) (void)fprintf(fp
, " del");
3022 if (status
& STA_UNSYNC
) (void)fprintf(fp
, " unsync");
3025 if (status
& STA_FREQHOLD
) (void)fprintf(fp
, " freqhold");
3027 #ifdef STA_PPSSIGNAL
3028 if (status
& STA_PPSSIGNAL
) (void)fprintf(fp
, " ppssignal");
3030 #ifdef STA_PPSJITTER
3031 if (status
& STA_PPSJITTER
) (void)fprintf(fp
, " ppsjitter");
3033 #ifdef STA_PPSWANDER
3034 if (status
& STA_PPSWANDER
) (void)fprintf(fp
, " ppswander");
3037 if (status
& STA_PPSERROR
) (void)fprintf(fp
, " ppserror");
3040 if (status
& STA_CLOCKERR
) (void)fprintf(fp
, " clockerr");
3043 if (status
& STA_NANO
) (void)fprintf(fp
, " nano");
3046 if (status
& STA_MODE
) (void)fprintf(fp
, " mode=fll");
3049 if (status
& STA_CLK
) (void)fprintf(fp
, " src=B");
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.
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"
3096 struct info_if_stats
*ifs
,
3102 static char *actions
= "?.+-";
3103 struct sockaddr_storage saddr
;
3108 if (!checkitems(items
, fp
))
3111 if (!checkitemsize(itemsize
, sizeof(struct info_if_stats
)))
3114 fprintf(fp
, IF_LIST_FMT_STR
, IF_LIST_LABELS
);
3115 fprintf(fp
, IF_LIST_LINE
);
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
;
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
);
3128 fprintf(fp
, IF_LIST_FMT
,
3130 actions
[(ifs
->action
>= 1 && ifs
->action
< 4) ? ifs
->action
: 0],
3131 stoa((&saddr
)), 'A',
3132 ifs
->ignore_packets
? 'D' : 'E',
3135 ntohl(ifs
->last_ttl
),
3136 ntohl(ifs
->num_mcast
),
3137 ntohl(ifs
->received
),
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
;
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
);
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
);
3163 fprintf(fp
, IF_LIST_AFMT_STR
, stoa(&saddr
), 'B');
3179 struct info_if_stats
*ifs
;
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
);
3197 struct info_if_stats
*ifs
;
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
);