dirauth_periodic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #include "core/or/or.h"
  7. #include "app/config/or_options_st.h"
  8. #include "core/mainloop/netstatus.h"
  9. #include "feature/dirauth/reachability.h"
  10. #include "feature/stats/rephist.h"
  11. #include "feature/dirauth/dirvote.h"
  12. #include "feature/dirauth/dirauth_periodic.h"
  13. #include "feature/dirauth/authmode.h"
  14. #include "core/mainloop/periodic.h"
  15. #define DECLARE_EVENT(name, roles, flags) \
  16. static periodic_event_item_t name ## _event = \
  17. PERIODIC_EVENT(name, \
  18. PERIODIC_EVENT_ROLE_##roles, \
  19. flags)
  20. #define FL(name) (PERIODIC_EVENT_FLAG_##name)
  21. /**
  22. * Periodic callback: if we're an authority, check on our authority
  23. * certificate (the one that authenticates our authority signing key).
  24. */
  25. static int
  26. check_authority_cert_callback(time_t now, const or_options_t *options)
  27. {
  28. (void)now;
  29. (void)options;
  30. /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
  31. * close to expiring and warn the admin if it is. */
  32. v3_authority_check_key_expiry();
  33. #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
  34. return CHECK_V3_CERTIFICATE_INTERVAL;
  35. }
  36. DECLARE_EVENT(check_authority_cert, DIRAUTH, 0);
  37. /**
  38. * Scheduled callback: Run directory-authority voting functionality.
  39. *
  40. * The schedule is a bit complicated here, so dirvote_act() manages the
  41. * schedule itself.
  42. **/
  43. static int
  44. dirvote_callback(time_t now, const or_options_t *options)
  45. {
  46. if (!authdir_mode_v3(options)) {
  47. tor_assert_nonfatal_unreached();
  48. return 3600;
  49. }
  50. time_t next = dirvote_act(options, now);
  51. if (BUG(next == TIME_MAX)) {
  52. /* This shouldn't be returned unless we called dirvote_act() without
  53. * being an authority. If it happens, maybe our configuration will
  54. * fix itself in an hour or so? */
  55. return 3600;
  56. }
  57. return safe_timer_diff(now, next);
  58. }
  59. DECLARE_EVENT(dirvote, DIRAUTH, FL(NEED_NET));
  60. /** Reschedule the directory-authority voting event. Run this whenever the
  61. * schedule has changed. */
  62. void
  63. reschedule_dirvote(const or_options_t *options)
  64. {
  65. if (authdir_mode_v3(options)) {
  66. periodic_event_reschedule(&dirvote_event);
  67. }
  68. }
  69. /**
  70. * Periodic callback: if we're an authority, record our measured stability
  71. * information from rephist in an mtbf file.
  72. */
  73. static int
  74. save_stability_callback(time_t now, const or_options_t *options)
  75. {
  76. if (authdir_mode_tests_reachability(options)) {
  77. if (rep_hist_record_mtbf_data(now, 1)<0) {
  78. log_warn(LD_GENERAL, "Couldn't store mtbf data.");
  79. }
  80. }
  81. #define SAVE_STABILITY_INTERVAL (30*60)
  82. return SAVE_STABILITY_INTERVAL;
  83. }
  84. DECLARE_EVENT(save_stability, AUTHORITIES, 0);
  85. /**
  86. * Periodic callback: if we're an authority, make sure we test
  87. * the routers on the network for reachability.
  88. */
  89. static int
  90. launch_reachability_tests_callback(time_t now, const or_options_t *options)
  91. {
  92. if (authdir_mode_tests_reachability(options) &&
  93. !net_is_disabled()) {
  94. /* try to determine reachability of the other Tor relays */
  95. dirserv_test_reachability(now);
  96. }
  97. return REACHABILITY_TEST_INTERVAL;
  98. }
  99. DECLARE_EVENT(launch_reachability_tests, AUTHORITIES, FL(NEED_NET));
  100. /**
  101. * Periodic callback: if we're an authority, discount the stability
  102. * information (and other rephist information) that's older.
  103. */
  104. static int
  105. downrate_stability_callback(time_t now, const or_options_t *options)
  106. {
  107. (void)options;
  108. /* 1d. Periodically, we discount older stability information so that new
  109. * stability info counts more, and save the stability information to disk as
  110. * appropriate. */
  111. time_t next = rep_hist_downrate_old_runs(now);
  112. return safe_timer_diff(now, next);
  113. }
  114. DECLARE_EVENT(downrate_stability, AUTHORITIES, 0);
  115. void
  116. dirauth_register_periodic_events(void)
  117. {
  118. periodic_events_register(&downrate_stability_event);
  119. periodic_events_register(&launch_reachability_tests_event);
  120. periodic_events_register(&save_stability_event);
  121. periodic_events_register(&check_authority_cert_event);
  122. periodic_events_register(&dirvote_event);
  123. }