tor  0.4.2.1-alpha-dev
netstatus.c
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 
7 #include "core/or/or.h"
8 #include "core/mainloop/netstatus.h"
10 #include "app/config/config.h"
12 
13 #include "app/config/or_state_st.h"
14 
18 int
19 net_is_disabled(void)
20 {
21  return get_options()->DisableNetwork || we_are_hibernating();
22 }
23 
27 int
28 net_is_completely_disabled(void)
29 {
30  return get_options()->DisableNetwork || we_are_fully_hibernating();
31 }
32 
41 static time_t last_user_activity_seen = 0;
42 
47 static bool participating_on_network = false;
48 
56 void
57 note_user_activity(time_t now)
58 {
59  last_user_activity_seen = MAX(now, last_user_activity_seen);
60 
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();
65  }
66 }
67 
75 void
76 reset_user_activity(time_t now)
77 {
78  last_user_activity_seen = now;
79 }
80 
84 time_t
85 get_last_user_activity_time(void)
86 {
87  return last_user_activity_seen;
88 }
89 
94 void
95 set_network_participation(bool participation)
96 {
97  participating_on_network = participation;
98 }
99 
103 bool
104 is_participating_on_network(void)
105 {
106  return participating_on_network;
107 }
108 
112 void
113 netstatus_flush_to_state(or_state_t *state, time_t now)
114 {
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;
121  }
122 }
123 
127 void
128 netstatus_load_from_state(const or_state_t *state, time_t now)
129 {
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;
139  }
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;
146  }
147  if (get_options()->DormantCanceledByStartup) {
148  last_activity = now;
149  participating_on_network = true;
150  }
151  reset_user_activity(last_activity);
152 }
153 
158 void
159 netstatus_note_clock_jumped(time_t seconds_diff)
160 {
161  time_t last_active = get_last_user_activity_time();
162  if (last_active)
163  reset_user_activity(last_active + seconds_diff);
164 }
#define LD_GENERAL
Definition: log.h:60
Header file for config.c.
Header file for mainloop.c.
Header file for hibernate.c.
Master header file for Tor-specific functionality.
int MinutesSinceUserActivity
Definition: or_state_st.h:94
#define MAX(a, b)
Definition: cmp.h:22