Sync usage with man page.
[netbsd-mini2440.git] / dist / ntp / libntp / statestr.c
blobb6135b99b9194c9ff8f0d3f610d045e3524d26d8
1 /* $NetBSD: statestr.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
3 /*
4 * pretty printing of status information
5 */
6 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9 #include <stdio.h>
10 #include "ntp_stdlib.h"
11 #include "ntp_fp.h"
12 #include "ntp.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.
21 struct codestring {
22 int code;
23 const char *string;
27 * Leap values
29 static
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" },
35 { -1, "leap" }
39 * Clock source
41 static
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" },
53 { -1, "sync" }
58 * Peer selection
60 static
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" },
70 { -1, "sel" }
75 * Clock status
77 static
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" },
86 { -1, "clk" }
91 * System Events
93 static
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" },
103 { -1, "event" }
107 * Peer events
109 static
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" },
117 #if 0
118 { EVNT_PEERSTRAT & ~PEER_EVENT, "event_stratum_chg" },
119 #endif
120 { -1, "event" }
123 #ifdef OPENSSL
125 * Crypto events
127 static
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" },
146 { -1, "crypto" }
148 #endif /* OPENSSL */
150 /* Forwards */
151 static const char *getcode P((int, struct codestring *));
152 static const char *getevents P((int));
155 * getcode - return string corresponding to code
157 static const char *
158 getcode(
159 int code,
160 struct codestring *codetab
163 static char buf[30];
165 while (codetab->code != -1) {
166 if (codetab->code == code)
167 return codetab->string;
168 codetab++;
170 (void) sprintf(buf, "%s_%d", codetab->string, code);
171 return buf;
175 * getevents - return a descriptive string for the event count
177 static const char *
178 getevents(
179 int cnt
182 static char buf[20];
184 if (cnt == 0)
185 return "no events";
186 (void) sprintf(buf, "%d event%s", cnt, (cnt==1) ? "" : "s");
187 return buf;
191 * statustoa - return a descriptive string for a peer status
193 char *
194 statustoa(
195 int type,
196 int st
199 char *cb;
200 u_char pst;
202 LIB_GETBUF(cb);
204 switch (type) {
205 case TYPE_SYS:
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));
215 break;
217 case TYPE_PEER:
219 * Handcraft the bits
221 pst = (u_char) CTL_PEER_STATVAL(st);
222 if (!(pst & CTL_PST_REACH)) {
223 (void)strcpy(cb, "unreach");
224 } else {
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");
233 else
234 (void)strcat(cb, ", unauth");
238 * Now the codes
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),
249 peer_codes));
251 break;
253 case TYPE_CLOCK:
254 (void)strcpy(cb, getcode(((st)>>8) & 0xff, clock_codes));
255 (void)strcat(cb, ", last_");
256 (void)strcat(cb, getcode((st) & 0xff, clock_codes));
257 break;
259 return cb;
262 const char *
263 eventstr(
264 int num
267 if (num & PEER_EVENT)
268 return (getcode(num & ~PEER_EVENT, peer_codes));
269 #ifdef OPENSSL
270 else if (num & CRPT_EVENT)
271 return (getcode(num & ~CRPT_EVENT, crypto_codes));
272 #endif /* OPENSSL */
273 else
274 return (getcode(num, sys_codes));
277 const char *
278 ceventstr(
279 int num
282 return getcode(num, clock_codes);
285 const char *
286 sysstatstr(
287 int status
290 return statustoa(TYPE_SYS, status);
293 const char *
294 peerstatstr(
295 int status
298 return statustoa(TYPE_PEER, status);
301 const char *
302 clockstatstr(
303 int status
306 return statustoa(TYPE_CLOCK, status);