1 /* $NetBSD: statestr.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
4 * pretty printing of status information
10 #include "ntp_stdlib.h"
13 #include "lib_strbuf.h"
14 #include "ntp_refclock.h"
15 #include "ntp_control.h"
16 #include "ntp_string.h"
19 * Structure for turning various constants into a readable string.
30 struct codestring leap_codes
[] = {
31 { LEAP_NOWARNING
, "leap_none" },
32 { LEAP_ADDSECOND
, "leap_add_sec" },
33 { LEAP_DELSECOND
, "leap_del_sec" },
34 { LEAP_NOTINSYNC
, "sync_alarm" },
42 struct codestring sync_codes
[] = {
43 { CTL_SST_TS_UNSPEC
, "sync_unspec" },
44 { CTL_SST_TS_ATOM
, "sync_atomic" },
45 { CTL_SST_TS_LF
, "sync_lf_clock" },
46 { CTL_SST_TS_HF
, "sync_hf_clock" },
47 { CTL_SST_TS_UHF
, "sync_uhf_clock" },
48 { CTL_SST_TS_LOCAL
, "sync_local_proto" },
49 { CTL_SST_TS_NTP
, "sync_ntp" },
50 { CTL_SST_TS_UDPTIME
, "sync_udp/time" },
51 { CTL_SST_TS_WRSTWTCH
, "sync_wristwatch" },
52 { CTL_SST_TS_TELEPHONE
, "sync_telephone" },
61 struct codestring select_codes
[] = {
62 { CTL_PST_SEL_REJECT
, "selreject" },
63 { CTL_PST_SEL_SANE
, "sel_falsetick" },
64 { CTL_PST_SEL_CORRECT
, "sel_excess" },
65 { CTL_PST_SEL_SELCAND
, "sel_outlyer" },
66 { CTL_PST_SEL_SYNCCAND
, "sel_candidat" },
67 { CTL_PST_SEL_DISTSYSPEER
, "sel_selected" },
68 { CTL_PST_SEL_SYSPEER
, "sel_sys.peer" },
69 { CTL_PST_SEL_PPS
, "sel_pps.peer" },
78 struct codestring clock_codes
[] = {
79 { CTL_CLK_OKAY
, "clk_okay" },
80 { CTL_CLK_NOREPLY
, "clk_noreply" },
81 { CTL_CLK_BADFORMAT
, "clk_badformat" },
82 { CTL_CLK_FAULT
, "clk_fault" },
83 { CTL_CLK_PROPAGATION
, "clk_badsignal" },
84 { CTL_CLK_BADDATE
, "clk_baddate" },
85 { CTL_CLK_BADTIME
, "clk_badtime" },
94 struct codestring sys_codes
[] = {
95 { EVNT_UNSPEC
, "event_unspec" },
96 { EVNT_SYSRESTART
, "event_restart" },
97 { EVNT_SYSFAULT
, "event_fault" },
98 { EVNT_SYNCCHG
, "event_sync_chg" },
99 { EVNT_PEERSTCHG
, "event_peer/strat_chg" },
100 { EVNT_CLOCKRESET
, "event_clock_reset" },
101 { EVNT_BADDATETIM
, "event_bad_date" },
102 { EVNT_CLOCKEXCPT
, "event_clock_excptn" },
110 struct codestring peer_codes
[] = {
111 { EVNT_UNSPEC
, "event_unspec" },
112 { EVNT_PEERIPERR
& ~PEER_EVENT
, "event_ip_err" },
113 { EVNT_PEERAUTH
& ~PEER_EVENT
, "event_authen" },
114 { EVNT_UNREACH
& ~PEER_EVENT
, "event_unreach" },
115 { EVNT_REACH
& ~PEER_EVENT
, "event_reach" },
116 { EVNT_PEERCLOCK
& ~PEER_EVENT
, "event_peer_clock" },
118 { EVNT_PEERSTRAT
& ~PEER_EVENT
, "event_stratum_chg" },
128 struct codestring crypto_codes
[] = {
129 { XEVNT_OK
& ~CRPT_EVENT
, "success" },
130 { XEVNT_LEN
& ~CRPT_EVENT
, "bad_field_format_or_length" },
131 { XEVNT_TSP
& ~CRPT_EVENT
, "bad_timestamp" },
132 { XEVNT_FSP
& ~CRPT_EVENT
, "bad_filestamp" },
133 { XEVNT_PUB
& ~CRPT_EVENT
, "bad_or_missing_public_key" },
134 { XEVNT_MD
& ~CRPT_EVENT
, "unsupported_digest_type" },
135 { XEVNT_KEY
& ~CRPT_EVENT
, "unsupported_identity_type" },
136 { XEVNT_SGL
& ~CRPT_EVENT
, "bad_signature_length" },
137 { XEVNT_SIG
& ~CRPT_EVENT
, "signature_not_verified" },
138 { XEVNT_VFY
& ~CRPT_EVENT
, "certificate_not_verified" },
139 { XEVNT_PER
& ~CRPT_EVENT
, "host certificate_expired" },
140 { XEVNT_CKY
& ~CRPT_EVENT
, "bad_or_missing_cookie" },
141 { XEVNT_DAT
& ~CRPT_EVENT
, "bad_or_missing_leapsecond_table" },
142 { XEVNT_CRT
& ~CRPT_EVENT
, "bad_or_missing_certificate" },
143 { XEVNT_ID
& ~CRPT_EVENT
, "bad_or_missing_group_key" },
144 { XEVNT_ERR
& ~CRPT_EVENT
, "protocol_error" },
145 { XEVNT_SRV
& ~CRPT_EVENT
, "server certificate expired" },
151 static const char *getcode
P((int, struct codestring
*));
152 static const char *getevents
P((int));
155 * getcode - return string corresponding to code
160 struct codestring
*codetab
165 while (codetab
->code
!= -1) {
166 if (codetab
->code
== code
)
167 return codetab
->string
;
170 (void) sprintf(buf
, "%s_%d", codetab
->string
, code
);
175 * getevents - return a descriptive string for the event count
186 (void) sprintf(buf
, "%d event%s", cnt
, (cnt
==1) ? "" : "s");
191 * statustoa - return a descriptive string for a peer status
206 (void)strcpy(cb
, getcode(CTL_SYS_LI(st
), leap_codes
));
207 (void)strcat(cb
, ", ");
208 (void)strcat(cb
, getcode(CTL_SYS_SOURCE(st
) & ~CTL_SST_TS_PPS
, sync_codes
));
209 if (CTL_SYS_SOURCE(st
) & CTL_SST_TS_PPS
)
210 (void)strcat(cb
, "/PPS");
211 (void)strcat(cb
, ", ");
212 (void)strcat(cb
, getevents(CTL_SYS_NEVNT(st
)));
213 (void)strcat(cb
, ", ");
214 (void)strcat(cb
, getcode(CTL_SYS_EVENT(st
), sys_codes
));
221 pst
= (u_char
) CTL_PEER_STATVAL(st
);
222 if (!(pst
& CTL_PST_REACH
)) {
223 (void)strcpy(cb
, "unreach");
225 (void)strcpy(cb
, "reach");
228 if (pst
& CTL_PST_CONFIG
)
229 (void)strcat(cb
, ", conf");
230 if (pst
& CTL_PST_AUTHENABLE
) {
231 if (!(pst
& CTL_PST_REACH
) || (pst
& CTL_PST_AUTHENTIC
))
232 (void)strcat(cb
, ", auth");
234 (void)strcat(cb
, ", unauth");
240 if ((pst
& 0x7) != CTL_PST_SEL_REJECT
) {
241 (void)strcat(cb
, ", ");
242 (void)strcat(cb
, getcode(pst
& 0x7, select_codes
));
244 (void)strcat(cb
, ", ");
245 (void)strcat(cb
, getevents(CTL_PEER_NEVNT(st
)));
246 if (CTL_PEER_EVENT(st
) != EVNT_UNSPEC
) {
247 (void)strcat(cb
, ", ");
248 (void)strcat(cb
, getcode(CTL_PEER_EVENT(st
),
254 (void)strcpy(cb
, getcode(((st
)>>8) & 0xff, clock_codes
));
255 (void)strcat(cb
, ", last_");
256 (void)strcat(cb
, getcode((st
) & 0xff, clock_codes
));
267 if (num
& PEER_EVENT
)
268 return (getcode(num
& ~PEER_EVENT
, peer_codes
));
270 else if (num
& CRPT_EVENT
)
271 return (getcode(num
& ~CRPT_EVENT
, crypto_codes
));
274 return (getcode(num
, sys_codes
));
282 return getcode(num
, clock_codes
);
290 return statustoa(TYPE_SYS
, status
);
298 return statustoa(TYPE_PEER
, status
);
306 return statustoa(TYPE_CLOCK
, status
);