Tor  0.4.7.0-alpha-dev
netstatus.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-2021, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 /**
8  * @file netstatus.c
9  * @brief Track whether the network is disabled, dormant, etc.
10  **/
11 
12 #include "core/or/or.h"
14 #include "core/mainloop/mainloop.h"
16 #include "app/config/config.h"
18 
19 #include "app/config/or_state_st.h"
20 
21 /** Return true iff our network is in some sense disabled or shutting down:
22  * either we're hibernating, entering hibernation, or the network is turned
23  * off with DisableNetwork. */
24 int
26 {
28 }
29 
30 /** Return true iff our network is in some sense "completely disabled" either
31  * we're fully hibernating or the network is turned off with
32  * DisableNetwork. */
33 int
35 {
37 }
38 
39 /**
40  * The time at which we've last seen "user activity" -- that is, any activity
41  * that should keep us as a participant on the network.
42  *
43  * This is not actually the true time. We will adjust this forward if
44  * our clock jumps, or if Tor is shut down for a while, so that the time
45  * since our last activity remains as it was before the jump or shutdown.
46  */
47 static time_t last_user_activity_seen = 0;
48 
49 /**
50  * True iff we are currently a "network participant" -- that is, we
51  * are building circuits, fetching directory information, and so on.
52  **/
53 static bool participating_on_network = false;
54 
55 /**
56  * Record the fact that we have seen "user activity" at the time now. Move
57  * "last activity seen" time forwards, but never backwards.
58  *
59  * If we were previously not participating on the network, set our
60  * participation status to true, and launch periodic events as appropriate.
61  **/
62 void
63 note_user_activity(time_t now)
64 {
66 
68  log_notice(LD_GENERAL, "Tor is no longer dormant.");
71  }
72 }
73 
74 /**
75  * Change the time at which "user activity" was last seen to <b>now</b>.
76  *
77  * Unlike note_user_actity, this function sets the time without checking
78  * whether it is in the past, and without causing any rescan of periodic events
79  * or change in participation status.
80  */
81 void
83 {
85 }
86 
87 /**
88  * Return the most recent time at which we recorded "user activity".
89  **/
90 time_t
92 {
94 }
95 
96 /**
97  * Set the field that remembers whether we are currently participating on the
98  * network. Does not schedule or un-schedule periodic events.
99  **/
100 void
101 set_network_participation(bool participation)
102 {
103  participating_on_network = participation;
104 }
105 
106 /**
107  * Return true iff we are currently participating on the network.
108  **/
109 bool
111 {
113 }
114 
115 /**
116  * Update 'state' with the last time at which we were active on the network.
117  **/
118 void
120 {
123  time_t sec_since_activity = MAX(0, now - last_user_activity_seen);
124  state->MinutesSinceUserActivity = (int)(sec_since_activity / 60);
125  } else {
126  state->MinutesSinceUserActivity = 0;
127  }
128 }
129 
130 /**
131  * Update our current view of network participation from an or_state_t object.
132  **/
133 void
135 {
136  time_t last_activity;
137  if (state->Dormant == -1) { // Initial setup.
138  if (get_options()->DormantOnFirstStartup) {
139  last_activity = 0;
140  participating_on_network = false;
141  } else {
142  // Start up as active, treat activity as happening now.
143  last_activity = now;
145  }
146  } else if (state->Dormant) {
147  last_activity = 0;
148  participating_on_network = false;
149  } else {
150  last_activity = now - 60 * state->MinutesSinceUserActivity;
152  }
153  if (get_options()->DormantCanceledByStartup) {
154  last_activity = now;
156  }
157  if (! get_options()->DormantTimeoutEnabled) {
159  }
160  reset_user_activity(last_activity);
161 }
162 
163 /**
164  * Adjust the time at which the user was last active by <b>seconds_diff</b>
165  * in response to a clock jump.
166  */
167 void
168 netstatus_note_clock_jumped(time_t seconds_diff)
169 {
170  time_t last_active = get_last_user_activity_time();
171  if (last_active)
172  reset_user_activity(last_active + seconds_diff);
173 }
#define MAX(a, b)
Definition: cmp.h:22
const or_options_t * get_options(void)
Definition: config.c:919
Header file for config.c.
int we_are_fully_hibernating(void)
Definition: hibernate.c:947
int we_are_hibernating(void)
Definition: hibernate.c:937
Header file for hibernate.c.
#define LD_GENERAL
Definition: log.h:62
void schedule_rescan_periodic_events(void)
Definition: mainloop.c:1571
Header file for mainloop.c.
Declare a state structure for mainloop-relevant fields.
int net_is_disabled(void)
Definition: netstatus.c:25
void netstatus_note_clock_jumped(time_t seconds_diff)
Definition: netstatus.c:168
int net_is_completely_disabled(void)
Definition: netstatus.c:34
static time_t last_user_activity_seen
Definition: netstatus.c:47
void set_network_participation(bool participation)
Definition: netstatus.c:101
time_t get_last_user_activity_time(void)
Definition: netstatus.c:91
void reset_user_activity(time_t now)
Definition: netstatus.c:82
void note_user_activity(time_t now)
Definition: netstatus.c:63
void netstatus_load_from_state(const mainloop_state_t *state, time_t now)
Definition: netstatus.c:134
void netstatus_flush_to_state(mainloop_state_t *state, time_t now)
Definition: netstatus.c:119
static bool participating_on_network
Definition: netstatus.c:53
bool is_participating_on_network(void)
Definition: netstatus.c:110
Header for netstatus.c.
Master header file for Tor-specific functionality.
The or_state_t structure, which represents Tor's state file.
POSINT MinutesSinceUserActivity