Annotation of embedaddon/ntp/libntp/statestr.c, revision 1.1
1.1 ! misho 1: /*
! 2: * pretty printing of status information
! 3: */
! 4: #ifdef HAVE_CONFIG_H
! 5: #include <config.h>
! 6: #endif
! 7: #include <stdio.h>
! 8: #include "ntp_stdlib.h"
! 9: #include "ntp_fp.h"
! 10: #include "ntp.h"
! 11: #include "lib_strbuf.h"
! 12: #include "ntp_refclock.h"
! 13: #include "ntp_control.h"
! 14: #include "ntp_string.h"
! 15:
! 16: /*
! 17: * Structure for turning various constants into a readable string.
! 18: */
! 19: struct codestring {
! 20: int code;
! 21: const char *string;
! 22: };
! 23:
! 24: /*
! 25: * Leap status (leap)
! 26: */
! 27: static
! 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, "leap_alarm" },
! 33: { -1, "leap" }
! 34: };
! 35:
! 36: /*
! 37: * Clock source status (sync)
! 38: */
! 39: static
! 40: struct codestring sync_codes[] = {
! 41: { CTL_SST_TS_UNSPEC, "sync_unspec" },
! 42: { CTL_SST_TS_ATOM, "sync_pps" },
! 43: { CTL_SST_TS_LF, "sync_lf_radio" },
! 44: { CTL_SST_TS_HF, "sync_hf_radio" },
! 45: { CTL_SST_TS_UHF, "sync_uhf_radio" },
! 46: { CTL_SST_TS_LOCAL, "sync_local" },
! 47: { CTL_SST_TS_NTP, "sync_ntp" },
! 48: { CTL_SST_TS_UDPTIME, "sync_other" },
! 49: { CTL_SST_TS_WRSTWTCH, "sync_wristwatch" },
! 50: { CTL_SST_TS_TELEPHONE, "sync_telephone" },
! 51: { -1, "sync" }
! 52: };
! 53:
! 54: /*
! 55: * Peer selection status (sel)
! 56: */
! 57: static
! 58: struct codestring select_codes[] = {
! 59: { CTL_PST_SEL_REJECT, "sel_reject" },
! 60: { CTL_PST_SEL_SANE, "sel_falsetick" },
! 61: { CTL_PST_SEL_CORRECT, "sel_excess" },
! 62: { CTL_PST_SEL_SELCAND, "sel_outlyer" },
! 63: { CTL_PST_SEL_SYNCCAND, "sel_candidate" },
! 64: { CTL_PST_SEL_EXCESS, "sel_backup" },
! 65: { CTL_PST_SEL_SYSPEER, "sel_sys.peer" },
! 66: { CTL_PST_SEL_PPS, "sel_pps.peer" },
! 67: { -1, "sel" }
! 68: };
! 69:
! 70: /*
! 71: * Clock status (clk)
! 72: */
! 73: static
! 74: struct codestring clock_codes[] = {
! 75: { CTL_CLK_OKAY, "clk_unspec" },
! 76: { CTL_CLK_NOREPLY, "clk_no_reply" },
! 77: { CTL_CLK_BADFORMAT, "clk_bad_format" },
! 78: { CTL_CLK_FAULT, "clk_fault" },
! 79: { CTL_CLK_PROPAGATION, "clk_bad_signal" },
! 80: { CTL_CLK_BADDATE, "clk_bad_date" },
! 81: { CTL_CLK_BADTIME, "clk_bad_time" },
! 82: { -1, "clk" }
! 83: };
! 84:
! 85:
! 86: #ifdef FLASH_CODES_UNUSED
! 87: /*
! 88: * Flash bits -- see ntpq.c tstflags & tstflagnames
! 89: */
! 90: static
! 91: struct codestring flash_codes[] = {
! 92: { TEST1, "pkt_dup" },
! 93: { TEST2, "pkt_bogus" },
! 94: { TEST3, "pkt_unsync" },
! 95: { TEST4, "pkt_denied" },
! 96: { TEST5, "pkt_auth" },
! 97: { TEST6, "pkt_stratum" },
! 98: { TEST7, "pkt_header" },
! 99: { TEST8, "pkt_autokey" },
! 100: { TEST9, "pkt_crypto" },
! 101: { TEST10, "peer_stratum" },
! 102: { TEST11, "peer_dist" },
! 103: { TEST12, "peer_loop" },
! 104: { TEST13, "peer_unreach" },
! 105: { -1, "flash" }
! 106: };
! 107: #endif
! 108:
! 109:
! 110: /*
! 111: * System events (sys)
! 112: */
! 113: static
! 114: struct codestring sys_codes[] = {
! 115: { EVNT_UNSPEC, "unspecified" },
! 116: { EVNT_NSET, "freq_not_set" },
! 117: { EVNT_FSET, "freq_set" },
! 118: { EVNT_SPIK, "spike_detect" },
! 119: { EVNT_FREQ, "freq_mode" },
! 120: { EVNT_SYNC, "clock_sync" },
! 121: { EVNT_SYSRESTART, "restart" },
! 122: { EVNT_SYSFAULT, "panic_stop" },
! 123: { EVNT_NOPEER, "no_sys_peer" },
! 124: { EVNT_ARMED, "leap_armed" },
! 125: { EVNT_DISARMED, "leap_disarmed" },
! 126: { EVNT_LEAP, "leap_event" },
! 127: { EVNT_CLOCKRESET, "clock_step" },
! 128: { EVNT_KERN, "kern" },
! 129: { EVNT_TAI, "TAI" },
! 130: { EVNT_LEAPVAL, "stale_leapsecond_values" },
! 131: { EVNT_CLKHOP, "clockhop" },
! 132: { -1, "" }
! 133: };
! 134:
! 135: /*
! 136: * Peer events (peer)
! 137: */
! 138: static
! 139: struct codestring peer_codes[] = {
! 140: { PEVNT_MOBIL & ~PEER_EVENT, "mobilize" },
! 141: { PEVNT_DEMOBIL & ~PEER_EVENT, "demobilize" },
! 142: { PEVNT_UNREACH & ~PEER_EVENT, "unreachable" },
! 143: { PEVNT_REACH & ~PEER_EVENT, "reachable" },
! 144: { PEVNT_RESTART & ~PEER_EVENT, "restart" },
! 145: { PEVNT_REPLY & ~PEER_EVENT, "no_reply" },
! 146: { PEVNT_RATE & ~PEER_EVENT, "rate_exceeded" },
! 147: { PEVNT_DENY & ~PEER_EVENT, "access_denied" },
! 148: { PEVNT_ARMED & ~PEER_EVENT, "leap_armed" },
! 149: { PEVNT_NEWPEER & ~PEER_EVENT, "sys_peer" },
! 150: { PEVNT_CLOCK & ~PEER_EVENT, "clock_event" },
! 151: { PEVNT_AUTH & ~PEER_EVENT, "bad_auth" },
! 152: { PEVNT_POPCORN & ~PEER_EVENT, "popcorn" },
! 153: { PEVNT_XLEAVE & ~PEER_EVENT, "interleave_mode" },
! 154: { PEVNT_XERR & ~PEER_EVENT, "interleave_error" },
! 155: { PEVNT_TAI & ~PEER_EVENT, "TAI" },
! 156: { -1, "" }
! 157: };
! 158:
! 159: #ifdef OPENSSL
! 160: /*
! 161: * Crypto events (cryp)
! 162: */
! 163: static
! 164: struct codestring crypto_codes[] = {
! 165: { XEVNT_OK & ~CRPT_EVENT, "success" },
! 166: { XEVNT_LEN & ~CRPT_EVENT, "bad_field_format_or_length" },
! 167: { XEVNT_TSP & ~CRPT_EVENT, "bad_timestamp" },
! 168: { XEVNT_FSP & ~CRPT_EVENT, "bad_filestamp" },
! 169: { XEVNT_PUB & ~CRPT_EVENT, "bad_or_missing_public_key" },
! 170: { XEVNT_MD & ~CRPT_EVENT, "unsupported_digest_type" },
! 171: { XEVNT_KEY & ~CRPT_EVENT, "unsupported_identity_type" },
! 172: { XEVNT_SGL & ~CRPT_EVENT, "bad_signature_length" },
! 173: { XEVNT_SIG & ~CRPT_EVENT, "signature_not_verified" },
! 174: { XEVNT_VFY & ~CRPT_EVENT, "certificate_not_verified" },
! 175: { XEVNT_PER & ~CRPT_EVENT, "host_certificate_expired" },
! 176: { XEVNT_CKY & ~CRPT_EVENT, "bad_or_missing_cookie" },
! 177: { XEVNT_DAT & ~CRPT_EVENT, "bad_or_missing_leapseconds" },
! 178: { XEVNT_CRT & ~CRPT_EVENT, "bad_or_missing_certificate" },
! 179: { XEVNT_ID & ~CRPT_EVENT, "bad_or_missing_group key" },
! 180: { XEVNT_ERR & ~CRPT_EVENT, "protocol_error" },
! 181: { -1, "" }
! 182: };
! 183: #endif /* OPENSSL */
! 184:
! 185: /* Forwards */
! 186: static const char *getcode (int, struct codestring *);
! 187: static const char *getevents (int);
! 188:
! 189: /*
! 190: * getcode - return string corresponding to code
! 191: */
! 192: static const char *
! 193: getcode(
! 194: int code,
! 195: struct codestring *codetab
! 196: )
! 197: {
! 198: static char buf[30];
! 199:
! 200: while (codetab->code != -1) {
! 201: if (codetab->code == code)
! 202: return codetab->string;
! 203: codetab++;
! 204: }
! 205: snprintf(buf, sizeof(buf), "%s_%d", codetab->string, code);
! 206: return buf;
! 207: }
! 208:
! 209: /*
! 210: * getevents - return a descriptive string for the event count
! 211: */
! 212: static const char *
! 213: getevents(
! 214: int cnt
! 215: )
! 216: {
! 217: static char buf[20];
! 218:
! 219: if (cnt == 0)
! 220: return "no events";
! 221: snprintf(buf, sizeof(buf), "%d event%s", cnt, (cnt==1) ? "" :
! 222: "s");
! 223: return buf;
! 224: }
! 225:
! 226: /*
! 227: * statustoa - return a descriptive string for a peer status
! 228: */
! 229: char *
! 230: statustoa(
! 231: int type,
! 232: int st
! 233: )
! 234: {
! 235: char *cb;
! 236: u_char pst;
! 237:
! 238: LIB_GETBUF(cb);
! 239:
! 240: switch (type) {
! 241: case TYPE_SYS:
! 242: strcpy(cb, getcode(CTL_SYS_LI(st), leap_codes));
! 243: strcat(cb, ", ");
! 244: strcat(cb, getcode(CTL_SYS_SOURCE(st), sync_codes));
! 245: strcat(cb, ", ");
! 246: strcat(cb, getevents(CTL_SYS_NEVNT(st)));
! 247: strcat(cb, ", ");
! 248: strcat(cb, getcode(CTL_SYS_EVENT(st), sys_codes));
! 249: break;
! 250:
! 251: case TYPE_PEER:
! 252:
! 253: /*
! 254: * Handcraft the bits
! 255: */
! 256: pst = (u_char) CTL_PEER_STATVAL(st);
! 257: if (pst & CTL_PST_CONFIG)
! 258: strcpy(cb, "conf");
! 259: if (pst & CTL_PST_AUTHENABLE) {
! 260: if (pst & CTL_PST_CONFIG)
! 261: strcat(cb, ", authenb");
! 262: else
! 263: strcat(cb, "authenb");
! 264: }
! 265: if (pst & CTL_PST_AUTHENTIC) {
! 266: if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE))
! 267: strcat(cb, ", auth");
! 268: else
! 269: strcat(cb, "auth");
! 270: }
! 271: if (pst & CTL_PST_REACH) {
! 272: if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE |
! 273: CTL_PST_AUTHENTIC))
! 274: strcat(cb, ", reach");
! 275: else
! 276: strcat(cb, "reach");
! 277: }
! 278: if (pst & CTL_PST_BCAST) {
! 279: if (pst & (CTL_PST_CONFIG | CTL_PST_AUTHENABLE |
! 280: CTL_PST_AUTHENTIC | CTL_PST_REACH))
! 281: strcat(cb, ", bcst");
! 282: else
! 283: strcat(cb, "bcst");
! 284: }
! 285:
! 286: /*
! 287: * Now the codes
! 288: */
! 289: strcat(cb, ", ");
! 290: strcat(cb, getcode(pst & 0x7, select_codes));
! 291: strcat(cb, ", ");
! 292: strcat(cb, getevents(CTL_PEER_NEVNT(st)));
! 293: if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
! 294: strcat(cb, ", ");
! 295: strcat(cb, getcode(CTL_PEER_EVENT(st),
! 296: peer_codes));
! 297: }
! 298: break;
! 299:
! 300: case TYPE_CLOCK:
! 301: strcat(cb, ", ");
! 302: strcat(cb, getevents(CTL_SYS_NEVNT(st)));
! 303: strcat(cb, ", ");
! 304: strcat(cb, getcode((st) & 0xf, clock_codes));
! 305: break;
! 306: }
! 307: return cb;
! 308: }
! 309:
! 310: const char *
! 311: eventstr(
! 312: int num
! 313: )
! 314: {
! 315: if (num & PEER_EVENT)
! 316: return (getcode(num & ~PEER_EVENT, peer_codes));
! 317: #ifdef OPENSSL
! 318: else if (num & CRPT_EVENT)
! 319: return (getcode(num & ~CRPT_EVENT, crypto_codes));
! 320: #endif /* OPENSSL */
! 321: else
! 322: return (getcode(num, sys_codes));
! 323: }
! 324:
! 325: const char *
! 326: ceventstr(
! 327: int num
! 328: )
! 329: {
! 330: return getcode(num, clock_codes);
! 331: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>