Bump copyright date to 2019
[tor.git] / src / core / mainloop / netstatus.c
blobfc5a465ff7f198696502a8ead14fbbedda760d3b
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 */
7 #include "core/or/or.h"
8 #include "core/mainloop/netstatus.h"
9 #include "core/mainloop/mainloop.h"
10 #include "app/config/config.h"
11 #include "feature/hibernate/hibernate.h"
13 #include "app/config/or_state_st.h"
15 /** Return true iff our network is in some sense disabled or shutting down:
16 * either we're hibernating, entering hibernation, or the network is turned
17 * off with DisableNetwork. */
18 int
19 net_is_disabled(void)
21 return get_options()->DisableNetwork || we_are_hibernating();
24 /** Return true iff our network is in some sense "completely disabled" either
25 * we're fully hibernating or the network is turned off with
26 * DisableNetwork. */
27 int
28 net_is_completely_disabled(void)
30 return get_options()->DisableNetwork || we_are_fully_hibernating();
33 /**
34 * The time at which we've last seen "user activity" -- that is, any activity
35 * that should keep us as a participant on the network.
37 * This is not actually the true time. We will adjust this forward if
38 * our clock jumps, or if Tor is shut down for a while, so that the time
39 * since our last activity remains as it was before the jump or shutdown.
41 static time_t last_user_activity_seen = 0;
43 /**
44 * True iff we are currently a "network participant" -- that is, we
45 * are building circuits, fetching directory information, and so on.
46 **/
47 static bool participating_on_network = false;
49 /**
50 * Record the fact that we have seen "user activity" at the time now. Move
51 * "last activity seen" time forwards, but never backwards.
53 * If we were previously not participating on the network, set our
54 * participation status to true, and launch periodic events as appropriate.
55 **/
56 void
57 note_user_activity(time_t now)
59 last_user_activity_seen = MAX(now, last_user_activity_seen);
61 if (! participating_on_network) {
62 log_notice(LD_GENERAL, "Tor is no longer dormant.");
63 set_network_participation(true);
64 schedule_rescan_periodic_events();
68 /**
69 * Change the time at which "user activitiy" was last seen to <b>now</b>.
71 * Unlike note_user_actity, this function sets the time without checking
72 * whether it is in the past, and without causing any rescan of periodic events
73 * or change in participation status.
75 void
76 reset_user_activity(time_t now)
78 last_user_activity_seen = now;
81 /**
82 * Return the most recent time at which we recorded "user activity".
83 **/
84 time_t
85 get_last_user_activity_time(void)
87 return last_user_activity_seen;
90 /**
91 * Set the field that remembers whether we are currently participating on the
92 * network. Does not schedule or un-schedule periodic events.
93 **/
94 void
95 set_network_participation(bool participation)
97 participating_on_network = participation;
101 * Return true iff we are currently participating on the network.
103 bool
104 is_participating_on_network(void)
106 return participating_on_network;
110 * Update 'state' with the last time at which we were active on the network.
112 void
113 netstatus_flush_to_state(or_state_t *state, time_t now)
115 state->Dormant = ! participating_on_network;
116 if (participating_on_network) {
117 time_t sec_since_activity = MAX(0, now - last_user_activity_seen);
118 state->MinutesSinceUserActivity = (int)(sec_since_activity / 60);
119 } else {
120 state->MinutesSinceUserActivity = 0;
125 * Update our current view of network participation from an or_state_t object.
127 void
128 netstatus_load_from_state(const or_state_t *state, time_t now)
130 time_t last_activity;
131 if (state->Dormant == -1) { // Initial setup.
132 if (get_options()->DormantOnFirstStartup) {
133 last_activity = 0;
134 participating_on_network = false;
135 } else {
136 // Start up as active, treat activity as happening now.
137 last_activity = now;
138 participating_on_network = true;
140 } else if (state->Dormant) {
141 last_activity = 0;
142 participating_on_network = false;
143 } else {
144 last_activity = now - 60 * state->MinutesSinceUserActivity;
145 participating_on_network = true;
147 reset_user_activity(last_activity);
151 * Adjust the time at which the user was last active by <b>seconds_diff</b>
152 * in response to a clock jump.
154 void
155 netstatus_note_clock_jumped(time_t seconds_diff)
157 time_t last_active = get_last_user_activity_time();
158 if (last_active)
159 reset_user_activity(last_active + seconds_diff);