dirauth_periodic.c 4.7 KB

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