/contrib/ntp/libntp/statestr.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 305 lines · 234 code · 28 blank · 43 comment · 21 complexity · e79c50d38b24510ee1d15b8dbe3e4649 MD5 · raw file

  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. * Structure for turning various constants into a readable string.
  17. */
  18. struct codestring {
  19. int code;
  20. const char *string;
  21. };
  22. /*
  23. * Leap values
  24. */
  25. static
  26. struct codestring leap_codes[] = {
  27. { LEAP_NOWARNING, "leap_none" },
  28. { LEAP_ADDSECOND, "leap_add_sec" },
  29. { LEAP_DELSECOND, "leap_del_sec" },
  30. { LEAP_NOTINSYNC, "sync_alarm" },
  31. { -1, "leap" }
  32. };
  33. /*
  34. * Clock source
  35. */
  36. static
  37. struct codestring sync_codes[] = {
  38. { CTL_SST_TS_UNSPEC, "sync_unspec" },
  39. { CTL_SST_TS_ATOM, "sync_atomic" },
  40. { CTL_SST_TS_LF, "sync_lf_clock" },
  41. { CTL_SST_TS_HF, "sync_hf_clock" },
  42. { CTL_SST_TS_UHF, "sync_uhf_clock" },
  43. { CTL_SST_TS_LOCAL, "sync_local_proto" },
  44. { CTL_SST_TS_NTP, "sync_ntp" },
  45. { CTL_SST_TS_UDPTIME, "sync_udp/time" },
  46. { CTL_SST_TS_WRSTWTCH, "sync_wristwatch" },
  47. { CTL_SST_TS_TELEPHONE, "sync_telephone" },
  48. { -1, "sync" }
  49. };
  50. /*
  51. * Peer selection
  52. */
  53. static
  54. struct codestring select_codes[] = {
  55. { CTL_PST_SEL_REJECT, "selreject" },
  56. { CTL_PST_SEL_SANE, "sel_falsetick" },
  57. { CTL_PST_SEL_CORRECT, "sel_excess" },
  58. { CTL_PST_SEL_SELCAND, "sel_outlyer" },
  59. { CTL_PST_SEL_SYNCCAND, "sel_candidat" },
  60. { CTL_PST_SEL_DISTSYSPEER, "sel_selected" },
  61. { CTL_PST_SEL_SYSPEER, "sel_sys.peer" },
  62. { CTL_PST_SEL_PPS, "sel_pps.peer" },
  63. { -1, "sel" }
  64. };
  65. /*
  66. * Clock status
  67. */
  68. static
  69. struct codestring clock_codes[] = {
  70. { CTL_CLK_OKAY, "clk_okay" },
  71. { CTL_CLK_NOREPLY, "clk_noreply" },
  72. { CTL_CLK_BADFORMAT, "clk_badformat" },
  73. { CTL_CLK_FAULT, "clk_fault" },
  74. { CTL_CLK_PROPAGATION, "clk_badsignal" },
  75. { CTL_CLK_BADDATE, "clk_baddate" },
  76. { CTL_CLK_BADTIME, "clk_badtime" },
  77. { -1, "clk" }
  78. };
  79. /*
  80. * System Events
  81. */
  82. static
  83. struct codestring sys_codes[] = {
  84. { EVNT_UNSPEC, "event_unspec" },
  85. { EVNT_SYSRESTART, "event_restart" },
  86. { EVNT_SYSFAULT, "event_fault" },
  87. { EVNT_SYNCCHG, "event_sync_chg" },
  88. { EVNT_PEERSTCHG, "event_peer/strat_chg" },
  89. { EVNT_CLOCKRESET, "event_clock_reset" },
  90. { EVNT_BADDATETIM, "event_bad_date" },
  91. { EVNT_CLOCKEXCPT, "event_clock_excptn" },
  92. { -1, "event" }
  93. };
  94. /*
  95. * Peer events
  96. */
  97. static
  98. struct codestring peer_codes[] = {
  99. { EVNT_UNSPEC, "event_unspec" },
  100. { EVNT_PEERIPERR & ~PEER_EVENT, "event_ip_err" },
  101. { EVNT_PEERAUTH & ~PEER_EVENT, "event_authen" },
  102. { EVNT_UNREACH & ~PEER_EVENT, "event_unreach" },
  103. { EVNT_REACH & ~PEER_EVENT, "event_reach" },
  104. { EVNT_PEERCLOCK & ~PEER_EVENT, "event_peer_clock" },
  105. #if 0
  106. { EVNT_PEERSTRAT & ~PEER_EVENT, "event_stratum_chg" },
  107. #endif
  108. { -1, "event" }
  109. };
  110. #ifdef OPENSSL
  111. /*
  112. * Crypto events
  113. */
  114. static
  115. struct codestring crypto_codes[] = {
  116. { XEVNT_OK & ~CRPT_EVENT, "success" },
  117. { XEVNT_LEN & ~CRPT_EVENT, "bad_field_format_or_length" },
  118. { XEVNT_TSP & ~CRPT_EVENT, "bad_timestamp" },
  119. { XEVNT_FSP & ~CRPT_EVENT, "bad_filestamp" },
  120. { XEVNT_PUB & ~CRPT_EVENT, "bad_or_missing_public_key" },
  121. { XEVNT_MD & ~CRPT_EVENT, "unsupported_digest_type" },
  122. { XEVNT_KEY & ~CRPT_EVENT, "unsupported_identity_type" },
  123. { XEVNT_SGL & ~CRPT_EVENT, "bad_signature_length" },
  124. { XEVNT_SIG & ~CRPT_EVENT, "signature_not_verified" },
  125. { XEVNT_VFY & ~CRPT_EVENT, "certificate_not_verified" },
  126. { XEVNT_PER & ~CRPT_EVENT, "host certificate_expired" },
  127. { XEVNT_CKY & ~CRPT_EVENT, "bad_or_missing_cookie" },
  128. { XEVNT_DAT & ~CRPT_EVENT, "bad_or_missing_leapsecond_table" },
  129. { XEVNT_CRT & ~CRPT_EVENT, "bad_or_missing_certificate" },
  130. { XEVNT_ID & ~CRPT_EVENT, "bad_or_missing_group_key" },
  131. { XEVNT_ERR & ~CRPT_EVENT, "protocol_error" },
  132. { XEVNT_SRV & ~CRPT_EVENT, "server certificate expired" },
  133. { -1, "crypto" }
  134. };
  135. #endif /* OPENSSL */
  136. /* Forwards */
  137. static const char *getcode P((int, struct codestring *));
  138. static const char *getevents P((int));
  139. /*
  140. * getcode - return string corresponding to code
  141. */
  142. static const char *
  143. getcode(
  144. int code,
  145. struct codestring *codetab
  146. )
  147. {
  148. static char buf[30];
  149. while (codetab->code != -1) {
  150. if (codetab->code == code)
  151. return codetab->string;
  152. codetab++;
  153. }
  154. (void) sprintf(buf, "%s_%d", codetab->string, code);
  155. return buf;
  156. }
  157. /*
  158. * getevents - return a descriptive string for the event count
  159. */
  160. static const char *
  161. getevents(
  162. int cnt
  163. )
  164. {
  165. static char buf[20];
  166. if (cnt == 0)
  167. return "no events";
  168. (void) sprintf(buf, "%d event%s", cnt, (cnt==1) ? "" : "s");
  169. return buf;
  170. }
  171. /*
  172. * statustoa - return a descriptive string for a peer status
  173. */
  174. char *
  175. statustoa(
  176. int type,
  177. int st
  178. )
  179. {
  180. char *cb;
  181. u_char pst;
  182. LIB_GETBUF(cb);
  183. switch (type) {
  184. case TYPE_SYS:
  185. (void)strcpy(cb, getcode(CTL_SYS_LI(st), leap_codes));
  186. (void)strcat(cb, ", ");
  187. (void)strcat(cb, getcode(CTL_SYS_SOURCE(st) & ~CTL_SST_TS_PPS, sync_codes));
  188. if (CTL_SYS_SOURCE(st) & CTL_SST_TS_PPS)
  189. (void)strcat(cb, "/PPS");
  190. (void)strcat(cb, ", ");
  191. (void)strcat(cb, getevents(CTL_SYS_NEVNT(st)));
  192. (void)strcat(cb, ", ");
  193. (void)strcat(cb, getcode(CTL_SYS_EVENT(st), sys_codes));
  194. break;
  195. case TYPE_PEER:
  196. /*
  197. * Handcraft the bits
  198. */
  199. pst = (u_char) CTL_PEER_STATVAL(st);
  200. if (!(pst & CTL_PST_REACH)) {
  201. (void)strcpy(cb, "unreach");
  202. } else {
  203. (void)strcpy(cb, "reach");
  204. }
  205. if (pst & CTL_PST_CONFIG)
  206. (void)strcat(cb, ", conf");
  207. if (pst & CTL_PST_AUTHENABLE) {
  208. if (!(pst & CTL_PST_REACH) || (pst & CTL_PST_AUTHENTIC))
  209. (void)strcat(cb, ", auth");
  210. else
  211. (void)strcat(cb, ", unauth");
  212. }
  213. /*
  214. * Now the codes
  215. */
  216. if ((pst & 0x7) != CTL_PST_SEL_REJECT) {
  217. (void)strcat(cb, ", ");
  218. (void)strcat(cb, getcode(pst & 0x7, select_codes));
  219. }
  220. (void)strcat(cb, ", ");
  221. (void)strcat(cb, getevents(CTL_PEER_NEVNT(st)));
  222. if (CTL_PEER_EVENT(st) != EVNT_UNSPEC) {
  223. (void)strcat(cb, ", ");
  224. (void)strcat(cb, getcode(CTL_PEER_EVENT(st),
  225. peer_codes));
  226. }
  227. break;
  228. case TYPE_CLOCK:
  229. (void)strcpy(cb, getcode(((st)>>8) & 0xff, clock_codes));
  230. (void)strcat(cb, ", last_");
  231. (void)strcat(cb, getcode((st) & 0xff, clock_codes));
  232. break;
  233. }
  234. return cb;
  235. }
  236. const char *
  237. eventstr(
  238. int num
  239. )
  240. {
  241. if (num & PEER_EVENT)
  242. return (getcode(num & ~PEER_EVENT, peer_codes));
  243. #ifdef OPENSSL
  244. else if (num & CRPT_EVENT)
  245. return (getcode(num & ~CRPT_EVENT, crypto_codes));
  246. #endif /* OPENSSL */
  247. else
  248. return (getcode(num, sys_codes));
  249. }
  250. const char *
  251. ceventstr(
  252. int num
  253. )
  254. {
  255. return getcode(num, clock_codes);
  256. }
  257. const char *
  258. sysstatstr(
  259. int status
  260. )
  261. {
  262. return statustoa(TYPE_SYS, status);
  263. }
  264. const char *
  265. peerstatstr(
  266. int status
  267. )
  268. {
  269. return statustoa(TYPE_PEER, status);
  270. }
  271. const char *
  272. clockstatstr(
  273. int status
  274. )
  275. {
  276. return statustoa(TYPE_CLOCK, status);
  277. }