2 * pretty printing of status information
8 #include "ntp_stdlib.h"
11 #include "lib_strbuf.h"
12 #include "ntp_refclock.h"
13 #include "ntp_control.h"
14 #include "ntp_string.h"
17 * Structure for turning various constants into a readable string.
28 struct codestring leap_codes[] = {
29 { LEAP_NOWARNING, "leap_none" },
30 { LEAP_ADDSECOND, "leap_add_sec" },
31 { LEAP_DELSECOND, "leap_del_sec" },
32 { LEAP_NOTINSYNC, "sync_alarm" },
40 struct codestring sync_codes[] = {
41 { CTL_SST_TS_UNSPEC, "sync_unspec" },
42 { CTL_SST_TS_ATOM, "sync_atomic" },
43 { CTL_SST_TS_LF, "sync_lf_clock" },
44 { CTL_SST_TS_HF, "sync_hf_clock" },
45 { CTL_SST_TS_UHF, "sync_uhf_clock" },
46 { CTL_SST_TS_LOCAL, "sync_local_proto" },
47 { CTL_SST_TS_NTP, "sync_ntp" },
48 { CTL_SST_TS_UDPTIME, "sync_udp/time" },
49 { CTL_SST_TS_WRSTWTCH, "sync_wristwatch" },
50 { CTL_SST_TS_TELEPHONE, "sync_telephone" },
59 struct codestring select_codes[] = {
60 { CTL_PST_SEL_REJECT, "selreject" },
61 { CTL_PST_SEL_SANE, "sel_falsetick" },
62 { CTL_PST_SEL_CORRECT, "sel_excess" },
63 { CTL_PST_SEL_SELCAND, "sel_outlyer" },
64 { CTL_PST_SEL_SYNCCAND, "sel_candidat" },
65 { CTL_PST_SEL_DISTSYSPEER, "sel_selected" },
66 { CTL_PST_SEL_SYSPEER, "sel_sys.peer" },
67 { CTL_PST_SEL_PPS, "sel_pps.peer" },
76 struct codestring clock_codes[] = {
77 { CTL_CLK_OKAY, "clk_okay" },
78 { CTL_CLK_NOREPLY, "clk_noreply" },
79 { CTL_CLK_BADFORMAT, "clk_badformat" },
80 { CTL_CLK_FAULT, "clk_fault" },
81 { CTL_CLK_PROPAGATION, "clk_badsignal" },
82 { CTL_CLK_BADDATE, "clk_baddate" },
83 { CTL_CLK_BADTIME, "clk_badtime" },
92 struct codestring sys_codes[] = {
93 { EVNT_UNSPEC, "event_unspec" },
94 { EVNT_SYSRESTART, "event_restart" },
95 { EVNT_SYSFAULT, "event_fault" },
96 { EVNT_SYNCCHG, "event_sync_chg" },
97 { EVNT_PEERSTCHG, "event_peer/strat_chg" },
98 { EVNT_CLOCKRESET, "event_clock_reset" },
99 { EVNT_BADDATETIM, "event_bad_date" },
100 { EVNT_CLOCKEXCPT, "event_clock_excptn" },
108 struct codestring peer_codes[] = {
109 { EVNT_UNSPEC, "event_unspec" },
110 { EVNT_PEERIPERR & ~PEER_EVENT, "event_ip_err" },
111 { EVNT_PEERAUTH & ~PEER_EVENT, "event_authen" },
112 { EVNT_UNREACH & ~PEER_EVENT, "event_unreach" },
113 { EVNT_REACH & ~PEER_EVENT, "event_reach" },
114 { EVNT_PEERCLOCK & ~PEER_EVENT, "event_peer_clock" },
116 { EVNT_PEERSTRAT & ~PEER_EVENT, "event_stratum_chg" },
126 struct codestring crypto_codes[] = {
127 { XEVNT_OK & ~CRPT_EVENT, "success" },
128 { XEVNT_LEN & ~CRPT_EVENT, "bad_field_format_or_length" },
129 { XEVNT_TSP & ~CRPT_EVENT, "bad_timestamp" },
130 { XEVNT_FSP & ~CRPT_EVENT, "bad_filestamp" },
131 { XEVNT_PUB & ~CRPT_EVENT, "bad_or_missing_public_key" },
132 { XEVNT_MD & ~CRPT_EVENT, "unsupported_digest_type" },
133 { XEVNT_KEY & ~CRPT_EVENT, "unsupported_identity_type" },
134 { XEVNT_SGL & ~CRPT_EVENT, "bad_signature_length" },
135 { XEVNT_SIG & ~CRPT_EVENT, "signature_not_verified" },
136 { XEVNT_VFY & ~CRPT_EVENT, "certificate_not_verified" },
137 { XEVNT_PER & ~CRPT_EVENT, "host certificate_expired" },
138 { XEVNT_CKY & ~CRPT_EVENT, "bad_or_missing_cookie" },
139 { XEVNT_DAT & ~CRPT_EVENT, "bad_or_missing_leapsecond_table" },
140 { XEVNT_CRT & ~CRPT_EVENT, "bad_or_missing_certificate" },
141 { XEVNT_ID & ~CRPT_EVENT, "bad_or_missing_group_key" },
142 { XEVNT_ERR & ~CRPT_EVENT, "protocol_error" },
143 { XEVNT_SRV & ~CRPT_EVENT, "server certificate expired" },
149 static const char *getcode P((int, struct codestring *));
150 static const char *getevents P((int));
153 * getcode - return string corresponding to code
158 struct codestring *codetab
163 while (codetab->code != -1) {
164 if (codetab->code == code)
165 return codetab->string;
168 (void) sprintf(buf, "%s_%d", codetab->string, code);
173 * getevents - return a descriptive string for the event count
184 (void) sprintf(buf, "%d event%s", cnt, (cnt==1) ? "" : "s");
189 * statustoa - return a descriptive string for a peer status
204 (void)strcpy(cb, getcode(CTL_SYS_LI(st), leap_codes));
205 (void)strcat(cb, ", ");
206 (void)strcat(cb, getcode(CTL_SYS_SOURCE(st) & ~CTL_SST_TS_PPS, sync_codes));
207 if (CTL_SYS_SOURCE(st) & CTL_SST_TS_PPS)
208 (void)strcat(cb, "/PPS");
209 (void)strcat(cb, ", ");
210 (void)strcat(cb, getevents(CTL_SYS_NEVNT(st)));
211 (void)strcat(cb, ", ");
212 (void)strcat(cb, getcode(CTL_SYS_EVENT(st), sys_codes));
219 pst = (u_char) CTL_PEER_STATVAL(st);
220 if (!(pst & CTL_PST_REACH)) {
221 (void)strcpy(cb, "unreach");
223 (void)strcpy(cb, "reach");
226 if (pst & CTL_PST_CONFIG)
227 (void)strcat(cb, ", conf");
228 if (pst & CTL_PST_AUTHENABLE) {
229 if (!(pst & CTL_PST_REACH) || (pst & CTL_PST_AUTHENTIC))
230 (void)strcat(cb, ", auth");
232 (void)strcat(cb, ", unauth");
238 if ((pst & 0x7) != CTL_PST_SEL_REJECT) {
239 (void)strcat(cb, ", ");
240 (void)strcat(cb, getcode(pst & 0x7, select_codes));
242 (void)strcat(cb, ", ");
243 (void)strcat(cb, getevents(CTL_PEER_NEVNT(st)));
244 if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
245 (void)strcat(cb, ", ");
246 (void)strcat(cb, getcode(CTL_PEER_EVENT(st),
252 (void)strcpy(cb, getcode(((st)>>8) & 0xff, clock_codes));
253 (void)strcat(cb, ", last_");
254 (void)strcat(cb, getcode((st) & 0xff, clock_codes));
265 if (num & PEER_EVENT)
266 return (getcode(num & ~PEER_EVENT, peer_codes));
268 else if (num & CRPT_EVENT)
269 return (getcode(num & ~CRPT_EVENT, crypto_codes));
272 return (getcode(num, sys_codes));
280 return getcode(num, clock_codes);
288 return statustoa(TYPE_SYS, status);
296 return statustoa(TYPE_PEER, status);
304 return statustoa(TYPE_CLOCK, status);