netstatus.c 4.4 KB

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