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. */
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
28 net_is_completely_disabled(void)
30 return get_options()->DisableNetwork
|| we_are_fully_hibernating();
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;
44 * True iff we are currently a "network participant" -- that is, we
45 * are building circuits, fetching directory information, and so on.
47 static bool participating_on_network
= false;
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.
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();
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.
76 reset_user_activity(time_t now
)
78 last_user_activity_seen
= now
;
82 * Return the most recent time at which we recorded "user activity".
85 get_last_user_activity_time(void)
87 return last_user_activity_seen
;
91 * Set the field that remembers whether we are currently participating on the
92 * network. Does not schedule or un-schedule periodic events.
95 set_network_participation(bool participation
)
97 participating_on_network
= participation
;
101 * Return true iff we are currently participating on the network.
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.
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);
120 state
->MinutesSinceUserActivity
= 0;
125 * Update our current view of network participation from an or_state_t object.
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
) {
134 participating_on_network
= false;
136 // Start up as active, treat activity as happening now.
138 participating_on_network
= true;
140 } else if (state
->Dormant
) {
142 participating_on_network
= false;
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.
155 netstatus_note_clock_jumped(time_t seconds_diff
)
157 time_t last_active
= get_last_user_activity_time();
159 reset_user_activity(last_active
+ seconds_diff
);