Tor  0.4.5.0-alpha-dev
dirauth_periodic.c
Go to the documentation of this file.
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-2020, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file dirauth_periodic.c
9  * @brief Peridoic events for directory authorities.
10  **/
11 
12 #include "core/or/or.h"
13 
17 #include "feature/stats/rephist.h"
18 
23 
24 #include "core/mainloop/periodic.h"
25 
26 #ifndef COCCI
27 #define DECLARE_EVENT(name, roles, flags) \
28  static periodic_event_item_t name ## _event = \
29  PERIODIC_EVENT(name, \
30  PERIODIC_EVENT_ROLE_##roles, \
31  flags)
32 #endif /* !defined(COCCI) */
33 
34 #define FL(name) (PERIODIC_EVENT_FLAG_##name)
35 
36 /**
37  * Periodic callback: if we're an authority, check on our authority
38  * certificate (the one that authenticates our authority signing key).
39  */
40 static int
41 check_authority_cert_callback(time_t now, const or_options_t *options)
42 {
43  (void)now;
44  (void)options;
45  /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
46  * close to expiring and warn the admin if it is. */
48 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
49  return CHECK_V3_CERTIFICATE_INTERVAL;
50 }
51 
52 DECLARE_EVENT(check_authority_cert, DIRAUTH, 0);
53 
54 /**
55  * Scheduled callback: Run directory-authority voting functionality.
56  *
57  * The schedule is a bit complicated here, so dirvote_act() manages the
58  * schedule itself.
59  **/
60 static int
61 dirvote_callback(time_t now, const or_options_t *options)
62 {
63  if (!authdir_mode_v3(options)) {
65  return 3600;
66  }
67 
68  time_t next = dirvote_act(options, now);
69  if (BUG(next == TIME_MAX)) {
70  /* This shouldn't be returned unless we called dirvote_act() without
71  * being an authority. If it happens, maybe our configuration will
72  * fix itself in an hour or so? */
73  return 3600;
74  }
75  return safe_timer_diff(now, next);
76 }
77 
78 DECLARE_EVENT(dirvote, DIRAUTH, FL(NEED_NET));
79 
80 /** Reschedule the directory-authority voting event. Run this whenever the
81  * schedule has changed. */
82 void
84 {
85  if (authdir_mode_v3(options)) {
86  periodic_event_reschedule(&dirvote_event);
87  }
88 }
89 
90 /**
91  * Periodic callback: if we're an authority, record our measured stability
92  * information from rephist in an mtbf file.
93  */
94 static int
95 save_stability_callback(time_t now, const or_options_t *options)
96 {
97  if (authdir_mode_tests_reachability(options)) {
98  if (rep_hist_record_mtbf_data(now, 1)<0) {
99  log_warn(LD_GENERAL, "Couldn't store mtbf data.");
100  }
101  }
102 #define SAVE_STABILITY_INTERVAL (30*60)
103  return SAVE_STABILITY_INTERVAL;
104 }
105 
106 DECLARE_EVENT(save_stability, AUTHORITIES, 0);
107 
108 /**
109  * Periodic callback: if we're an authority, make sure we test
110  * the routers on the network for reachability.
111  */
112 static int
114 {
115  if (authdir_mode_tests_reachability(options) &&
116  !net_is_disabled()) {
117  /* try to determine reachability of the other Tor relays */
119  }
121 }
122 
123 DECLARE_EVENT(launch_reachability_tests, AUTHORITIES, FL(NEED_NET));
124 
125 /**
126  * Periodic callback: if we're an authority, discount the stability
127  * information (and other rephist information) that's older.
128  */
129 static int
130 downrate_stability_callback(time_t now, const or_options_t *options)
131 {
132  (void)options;
133  /* 1d. Periodically, we discount older stability information so that new
134  * stability info counts more, and save the stability information to disk as
135  * appropriate. */
136  time_t next = rep_hist_downrate_old_runs(now);
137  return safe_timer_diff(now, next);
138 }
139 
140 DECLARE_EVENT(downrate_stability, AUTHORITIES, 0);
141 
142 /**
143  * Periodic callback: if we're the bridge authority, write a networkstatus
144  * file to disk.
145  */
146 static int
147 write_bridge_ns_callback(time_t now, const or_options_t *options)
148 {
149  if (options->BridgeAuthoritativeDir) {
151 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
152  return BRIDGE_STATUSFILE_INTERVAL;
153  }
154  return PERIODIC_EVENT_NO_UPDATE;
155 }
156 
157 DECLARE_EVENT(write_bridge_ns, BRIDGEAUTH, 0);
158 
159 void
160 dirauth_register_periodic_events(void)
161 {
162  periodic_events_register(&downrate_stability_event);
163  periodic_events_register(&launch_reachability_tests_event);
164  periodic_events_register(&save_stability_event);
165  periodic_events_register(&check_authority_cert_event);
166  periodic_events_register(&dirvote_event);
167  periodic_events_register(&write_bridge_ns_event);
168 }
rep_hist_downrate_old_runs
time_t rep_hist_downrate_old_runs(time_t now)
Definition: rephist.c:382
net_is_disabled
int net_is_disabled(void)
Definition: netstatus.c:25
bridgeauth.h
Header for bridgeauth.c.
periodic_event_reschedule
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:106
reschedule_dirvote
void reschedule_dirvote(const or_options_t *options)
Definition: dirauth_periodic.c:83
or_options_st.h
The or_options_t structure, which represents Tor's configuration.
netstatus.h
Header for netstatus.c.
LD_GENERAL
#define LD_GENERAL
Definition: log.h:62
periodic.h
Header for periodic.c.
dirvote_callback
static int dirvote_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:61
reachability.h
Header file for reachability.c.
downrate_stability_callback
static int downrate_stability_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:130
bridgeauth_dump_bridge_status_to_file
void bridgeauth_dump_bridge_status_to_file(time_t now)
Definition: bridgeauth.c:24
dirvote_act
time_t dirvote_act(const or_options_t *options, time_t now)
Definition: dirvote.c:2847
dirauth_periodic.h
Header for dirauth_periodic.c.
safe_timer_diff
int safe_timer_diff(time_t now, time_t next)
Definition: periodic.c:351
or_options_t::BridgeAuthoritativeDir
int BridgeAuthoritativeDir
Definition: or_options_st.h:209
dirvote.h
Header file for dirvote.c.
tor_assert_nonfatal_unreached
#define tor_assert_nonfatal_unreached()
Definition: util_bug.h:176
dirserv_test_reachability
void dirserv_test_reachability(time_t now)
Definition: reachability.c:183
authmode.h
Header file for directory authority mode.
check_authority_cert_callback
static int check_authority_cert_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:41
REACHABILITY_TEST_INTERVAL
#define REACHABILITY_TEST_INTERVAL
Definition: reachability.h:21
save_stability_callback
static int save_stability_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:95
launch_reachability_tests_callback
static int launch_reachability_tests_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:113
v3_authority_check_key_expiry
void v3_authority_check_key_expiry(void)
Definition: router.c:709
periodic_events_register
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:219
authdir_mode_tests_reachability
int authdir_mode_tests_reachability(const or_options_t *options)
Definition: authmode.c:68
or_options_t
Definition: or_options_st.h:39
write_bridge_ns_callback
static int write_bridge_ns_callback(time_t now, const or_options_t *options)
Definition: dirauth_periodic.c:147
rephist.h
Header file for rephist.c.
or.h
Master header file for Tor-specific functionality.
rep_hist_record_mtbf_data
int rep_hist_record_mtbf_data(time_t now, int missing_means_down)
Definition: rephist.c:626