Tor  0.4.8.0-alpha-dev
mainloop.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 mainloop.c
9  * \brief Toplevel module. Handles signals, multiplexes between
10  * connections, implements main loop, and drives scheduled events.
11  *
12  * For the main loop itself; see run_main_loop_once(). It invokes the rest of
13  * Tor mostly through Libevent callbacks. Libevent callbacks can happen when
14  * a timer elapses, a signal is received, a socket is ready to read or write,
15  * or an event is manually activated.
16  *
17  * Most events in Tor are driven from these callbacks:
18  * <ul>
19  * <li>conn_read_callback() and conn_write_callback() here, which are
20  * invoked when a socket is ready to read or write respectively.
21  * <li>signal_callback(), which handles incoming signals.
22  * </ul>
23  * Other events are used for specific purposes, or for building more complex
24  * control structures. If you search for usage of tor_libevent_new(), you
25  * will find all the events that we construct in Tor.
26  *
27  * Tor has numerous housekeeping operations that need to happen
28  * regularly. They are handled in different ways:
29  * <ul>
30  * <li>The most frequent operations are handled after every read or write
31  * event, at the end of connection_handle_read() and
32  * connection_handle_write().
33  *
34  * <li>The next most frequent operations happen after each invocation of the
35  * main loop, in run_main_loop_once().
36  *
37  * <li>Once per second, we run all of the operations listed in
38  * second_elapsed_callback(), and in its child, run_scheduled_events().
39  *
40  * <li>Once-a-second operations are handled in second_elapsed_callback().
41  *
42  * <li>More infrequent operations take place based on the periodic event
43  * driver in periodic.c . These are stored in the periodic_events[]
44  * table.
45  * </ul>
46  *
47  **/
48 
49 #define MAINLOOP_PRIVATE
50 #include "core/or/or.h"
51 
52 #include "app/config/config.h"
53 #include "app/config/statefile.h"
54 #include "app/main/ntmain.h"
57 #include "core/mainloop/mainloop.h"
59 #include "core/mainloop/periodic.h"
60 #include "core/or/channel.h"
61 #include "core/or/channelpadding.h"
62 #include "core/or/channeltls.h"
63 #include "core/or/circuitbuild.h"
64 #include "core/or/circuitlist.h"
65 #include "core/or/circuituse.h"
67 #include "core/or/connection_or.h"
68 #include "core/or/dos.h"
69 #include "core/or/status.h"
71 #include "feature/client/bridges.h"
72 #include "feature/client/dnsserv.h"
83 #include "feature/hs/hs_cache.h"
84 #include "feature/hs/hs_client.h"
85 #include "feature/hs/hs_service.h"
90 #include "feature/relay/dns.h"
93 #include "feature/relay/selftest.h"
97 #include "feature/stats/rephist.h"
98 #include "lib/buf/buffers.h"
100 #include "lib/err/backtrace.h"
101 #include "lib/tls/buffers_tls.h"
102 
103 #include "lib/net/buffers_net.h"
105 
106 #include <event2/event.h>
107 
108 #include "core/or/cell_st.h"
112 #include "app/config/or_state_st.h"
115 
116 #ifdef HAVE_UNISTD_H
117 #include <unistd.h>
118 #endif
119 
120 #ifdef HAVE_SYSTEMD
121 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
122 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
123  * Coverity. Here's a kludge to unconfuse it.
124  */
125 # define __INCLUDE_LEVEL__ 2
126 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
127 #include <systemd/sd-daemon.h>
128 #endif /* defined(HAVE_SYSTEMD) */
129 
130 /* Token bucket for all traffic. */
131 token_bucket_rw_t global_bucket;
132 
133 /* Token bucket for relayed traffic. */
134 token_bucket_rw_t global_relayed_bucket;
135 
136 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
137 /** How many bytes have we read since we started the process? */
138 static uint64_t stats_n_bytes_read = 0;
139 /** How many bytes have we written since we started the process? */
140 static uint64_t stats_n_bytes_written = 0;
141 /** What time did this process start up? */
143 /** How many seconds have we been running? */
144 static long stats_n_seconds_working = 0;
145 /** How many times have we returned from the main loop successfully? */
146 static uint64_t stats_n_main_loop_successes = 0;
147 /** How many times have we received an error from the main loop? */
148 static uint64_t stats_n_main_loop_errors = 0;
149 /** How many times have we returned from the main loop with no events. */
150 static uint64_t stats_n_main_loop_idle = 0;
151 
152 /** How often will we honor SIGNEWNYM requests? */
153 #define MAX_SIGNEWNYM_RATE 10
154 /** When did we last process a SIGNEWNYM request? */
155 static time_t time_of_last_signewnym = 0;
156 /** Is there a signewnym request we're currently waiting to handle? */
157 static int signewnym_is_pending = 0;
158 /** Mainloop event for the deferred signewnym call. */
160 /** How many times have we called newnym? */
161 static unsigned newnym_epoch = 0;
162 
163 /** Smartlist of all open connections. */
165 /** List of connections that have been marked for close and need to be freed
166  * and removed from connection_array. */
168 /** List of linked connections that are currently reading data into their
169  * inbuf from their partner's outbuf. */
171 /** Flag: Set to true iff we entered the current libevent main loop via
172  * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
173  * to handle linked connections. */
174 static int called_loop_once = 0;
175 /** Flag: if true, it's time to shut down, so the main loop should exit as
176  * soon as possible.
177  */
178 static int main_loop_should_exit = 0;
179 /** The return value that the main loop should yield when it exits, if
180  * main_loop_should_exit is true.
181  */
182 static int main_loop_exit_value = 0;
183 
184 /** We set this to 1 when we've opened a circuit, so we can print a log
185  * entry to inform the user that Tor is working. We set it to 0 when
186  * we think the fact that we once opened a circuit doesn't mean we can do so
187  * any longer (a big time jump happened, when we notice our directory is
188  * heinously out-of-date, etc.
189  */
190 static int can_complete_circuits = 0;
191 
192 /** How often do we check for router descriptors that we should download
193  * when we have too little directory info? */
194 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
195 /** How often do we check for router descriptors that we should download
196  * when we have enough directory info? */
197 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
198 
199 static int conn_close_if_marked(int i);
202 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
203 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
204 static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
205  void *arg) ATTR_NORETURN;
206 
207 /****************************************************************************
208  *
209  * This section contains accessors and other methods on the connection_array
210  * variables (which are global within this file and unavailable outside it).
211  *
212  ****************************************************************************/
213 
214 /** Return 1 if we have successfully built a circuit, and nothing has changed
215  * to make us think that maybe we can't.
216  */
217 int
219 {
220  return can_complete_circuits;
221 }
222 
223 /** Note that we have successfully built a circuit, so that reachability
224  * testing and introduction points and so on may be attempted. */
225 void
227 {
229 }
230 
231 /** Note that something has happened (like a clock jump, or DisableNetwork) to
232  * make us think that maybe we can't complete circuits. */
233 void
235 {
237 }
238 
239 /** Add <b>conn</b> to the array of connections that we can poll on. The
240  * connection's socket must be set; the connection starts out
241  * non-reading and non-writing.
242  */
243 int
244 connection_add_impl(connection_t *conn, int is_connecting)
245 {
246  tor_assert(conn);
247  tor_assert(SOCKET_OK(conn->s) ||
248  conn->linked ||
249  (conn->type == CONN_TYPE_AP &&
250  TO_EDGE_CONN(conn)->is_dns_request));
251 
252  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
253  conn->conn_array_index = smartlist_len(connection_array);
255 
256  (void) is_connecting;
257 
258  if (SOCKET_OK(conn->s) || conn->linked) {
259  conn->read_event = tor_event_new(tor_libevent_get_base(),
260  conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
261  conn->write_event = tor_event_new(tor_libevent_get_base(),
262  conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
263  /* XXXX CHECK FOR NULL RETURN! */
264  }
265 
266  log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
267  conn_type_to_string(conn->type), (int)conn->s, conn->address,
268  smartlist_len(connection_array));
269 
270  return 0;
271 }
272 
273 /** Tell libevent that we don't care about <b>conn</b> any more. */
274 void
276 {
277  tor_event_free(conn->read_event);
278  tor_event_free(conn->write_event);
279  if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
281  }
282 }
283 
284 /** Remove the connection from the global list, and remove the
285  * corresponding poll entry. Calling this function will shift the last
286  * connection (if any) into the position occupied by conn.
287  */
288 int
290 {
291  int current_index;
292  connection_t *tmp;
293 
294  tor_assert(conn);
295 
296  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
297  (int)conn->s, conn_type_to_string(conn->type),
298  smartlist_len(connection_array));
299 
300  if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
301  log_info(LD_NET, "Closing SOCKS Unix socket connection");
302  }
303 
305 
306  tor_assert(conn->conn_array_index >= 0);
307  current_index = conn->conn_array_index;
308  connection_unregister_events(conn); /* This is redundant, but cheap. */
309  if (current_index == smartlist_len(connection_array)-1) { /* at the end */
310  smartlist_del(connection_array, current_index);
311  return 0;
312  }
313 
314  /* replace this one with the one at the end */
315  smartlist_del(connection_array, current_index);
316  tmp = smartlist_get(connection_array, current_index);
317  tmp->conn_array_index = current_index;
318 
319  return 0;
320 }
321 
322 /** If <b>conn</b> is an edge conn, remove it from the list
323  * of conn's on this circuit. If it's not on an edge,
324  * flush and send destroys for all circuits on this conn.
325  *
326  * Remove it from connection_array (if applicable) and
327  * from closeable_connection_list.
328  *
329  * Then free it.
330  */
331 static void
333 {
335  if (conn->conn_array_index >= 0) {
336  connection_remove(conn);
337  }
338  if (conn->linked_conn) {
339  conn->linked_conn->linked_conn = NULL;
340  if (! conn->linked_conn->marked_for_close &&
343  conn->linked_conn = NULL;
344  }
347  if (conn->type == CONN_TYPE_EXIT) {
349  }
350  if (conn->type == CONN_TYPE_OR) {
351  if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
353  /* connection_unlink() can only get called if the connection
354  * was already on the closeable list, and it got there by
355  * connection_mark_for_close(), which was called from
356  * connection_or_close_normally() or
357  * connection_or_close_for_error(), so the channel should
358  * already be in CHANNEL_STATE_CLOSING, and then the
359  * connection_about_to_close_connection() goes to
360  * connection_or_about_to_close(), which calls channel_closed()
361  * to notify the channel_t layer, and closed the channel, so
362  * nothing more to do here to deal with the channel associated
363  * with an orconn.
364  */
365  }
366  connection_free(conn);
367 }
368 
369 /** Event that invokes schedule_active_linked_connections_cb. */
371 
372 /**
373  * Callback: used to activate read events for all linked connections, so
374  * libevent knows to call their read callbacks. This callback run as a
375  * postloop event, so that the events _it_ activates don't happen until
376  * Libevent has a chance to check for other events.
377  */
378 static void
380 {
381  (void)event;
382  (void)arg;
383 
384  /* All active linked conns should get their read events activated,
385  * so that libevent knows to run their callbacks. */
387  event_active(conn->read_event, EV_READ, 1));
388 
389  /* Reactivate the event if we still have connections in the active list.
390  *
391  * A linked connection doesn't get woken up by I/O but rather artificially
392  * by this event callback. It has directory data spooled in it and it is
393  * sent incrementally by small chunks unless spool_eagerly is true. For that
394  * to happen, we need to induce the activation of the read event so it can
395  * be flushed. */
396  if (smartlist_len(active_linked_connection_lst)) {
398  }
399 }
400 
401 /** Initialize the global connection list, closeable connection list,
402  * and active connection list. */
403 void
405 {
406  if (!connection_array)
412 }
413 
414 /** Schedule <b>conn</b> to be closed. **/
415 void
417 {
420  assert_connection_ok(conn, time(NULL));
423 }
424 
425 /** Return 1 if conn is on the closeable list, else return 0. */
426 int
428 {
430 }
431 
432 /** Return true iff conn is in the current poll array. */
433 int
435 {
436  return smartlist_contains(connection_array, conn);
437 }
438 
439 /** Set <b>*array</b> to an array of all connections. <b>*array</b> must not
440  * be modified.
441  */
444 {
445  if (!connection_array)
447  return connection_array;
448 }
449 
450 /**
451  * Return the amount of network traffic read, in bytes, over the life of this
452  * process.
453  */
454 MOCK_IMPL(uint64_t,
456 {
457  return stats_n_bytes_read;
458 }
459 
460 /**
461  * Return the amount of network traffic read, in bytes, over the life of this
462  * process.
463  */
464 MOCK_IMPL(uint64_t,
466 {
467  return stats_n_bytes_written;
468 }
469 
470 /**
471  * Increment the amount of network traffic read and written, over the life of
472  * this process.
473  */
474 void
476 {
477  stats_n_bytes_read += r;
479 }
480 
481 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
482  * mask is a bitmask whose bits are READ_EVENT and WRITE_EVENT)
483  */
484 void
486 {
487  if (events & READ_EVENT)
489  else
491 
492  if (events & WRITE_EVENT)
494  else
496 }
497 
498 /** Return true iff <b>conn</b> is listening for read events. */
499 int
501 {
502  tor_assert(conn);
503 
504  return conn->reading_from_linked_conn ||
505  (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
506 }
507 
508 /** Reset our main loop counters. */
509 void
511 {
515 }
516 
517 /** Increment the main loop success counter. */
518 static void
520 {
522 }
523 
524 /** Get the main loop success counter. */
525 uint64_t
527 {
529 }
530 
531 /** Increment the main loop error counter. */
532 static void
534 {
536 }
537 
538 /** Get the main loop error counter. */
539 uint64_t
541 {
543 }
544 
545 /** Increment the main loop idle counter. */
546 static void
548 {
550 }
551 
552 /** Get the main loop idle counter. */
553 uint64_t
555 {
556  return stats_n_main_loop_idle;
557 }
558 
559 /** Check whether <b>conn</b> is correct in having (or not having) a
560  * read/write event (passed in <b>ev</b>). On success, return 0. On failure,
561  * log a warning and return -1. */
562 static int
563 connection_check_event(connection_t *conn, struct event *ev)
564 {
565  int bad;
566 
567  if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
568  /* DNS requests which we launch through the dnsserv.c module do not have
569  * any underlying socket or any underlying linked connection, so they
570  * shouldn't have any attached events either.
571  */
572  bad = ev != NULL;
573  } else {
574  /* Everything else should have an underlying socket, or a linked
575  * connection (which is also tracked with a read_event/write_event pair).
576  */
577  bad = ev == NULL;
578  }
579 
580  if (bad) {
581  log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
582  "socket=%d. linked=%d. "
583  "is_dns_request=%d. Marked_for_close=%s:%d",
584  conn,
585  conn_type_to_string(conn->type),
586  conn_state_to_string(conn->type, conn->state),
587  (int)conn->s, (int)conn->linked,
588  (conn->type == CONN_TYPE_AP &&
589  TO_EDGE_CONN(conn)->is_dns_request),
590  conn->marked_for_close_file ? conn->marked_for_close_file : "-",
591  conn->marked_for_close
592  );
593  log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
594  return -1;
595  }
596  return 0;
597 }
598 
599 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
600 MOCK_IMPL(void,
602 {
603  tor_assert(conn);
604 
605  if (connection_check_event(conn, conn->read_event) < 0) {
606  return;
607  }
608 
609  if (conn->linked) {
610  conn->reading_from_linked_conn = 0;
612  } else {
613  if (event_del(conn->read_event))
614  log_warn(LD_NET, "Error from libevent setting read event state for %d "
615  "to unwatched: %s",
616  (int)conn->s,
617  tor_socket_strerror(tor_socket_errno(conn->s)));
618  }
619 }
620 
621 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
622 MOCK_IMPL(void,
624 {
625  tor_assert(conn);
626 
627  if (connection_check_event(conn, conn->read_event) < 0) {
628  return;
629  }
630 
631  if (conn->linked) {
632  conn->reading_from_linked_conn = 1;
635  } else {
636  if (CONN_IS_EDGE(conn) && TO_EDGE_CONN(conn)->xoff_received) {
637  /* We should not get called here if we're waiting for an XON, but
638  * belt-and-suspenders */
639  log_info(LD_NET,
640  "Request to start reading on an edgeconn blocked with XOFF");
641  return;
642  }
643  if (event_add(conn->read_event, NULL))
644  log_warn(LD_NET, "Error from libevent setting read event state for %d "
645  "to watched: %s",
646  (int)conn->s,
647  tor_socket_strerror(tor_socket_errno(conn->s)));
648  }
649 }
650 
651 /** Return true iff <b>conn</b> is listening for write events. */
652 int
654 {
655  tor_assert(conn);
656 
657  return conn->writing_to_linked_conn ||
658  (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
659 }
660 
661 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
662 MOCK_IMPL(void,
664 {
665  tor_assert(conn);
666 
667  if (connection_check_event(conn, conn->write_event) < 0) {
668  return;
669  }
670 
671  if (conn->linked) {
672  conn->writing_to_linked_conn = 0;
673  if (conn->linked_conn)
675  } else {
676  if (event_del(conn->write_event))
677  log_warn(LD_NET, "Error from libevent setting write event state for %d "
678  "to unwatched: %s",
679  (int)conn->s,
680  tor_socket_strerror(tor_socket_errno(conn->s)));
681  }
682 }
683 
684 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
685 MOCK_IMPL(void,
687 {
688  tor_assert(conn);
689 
690  if (connection_check_event(conn, conn->write_event) < 0) {
691  return;
692  }
693 
694  if (conn->linked) {
695  conn->writing_to_linked_conn = 1;
696  if (conn->linked_conn &&
699  } else {
700  if (event_add(conn->write_event, NULL))
701  log_warn(LD_NET, "Error from libevent setting write event state for %d "
702  "to watched: %s",
703  (int)conn->s,
704  tor_socket_strerror(tor_socket_errno(conn->s)));
705  }
706 }
707 
708 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
709  * linked to it would be good and feasible. (Reading is "feasible" if the
710  * other conn exists and has data in its outbuf, and is "good" if we have our
711  * reading_from_linked_conn flag set and the other conn has its
712  * writing_to_linked_conn flag set.)*/
713 static int
715 {
716  if (conn->linked && conn->reading_from_linked_conn) {
717  if (! conn->linked_conn ||
719  buf_datalen(conn->linked_conn->outbuf)))
720  return 1;
721  }
722  return 0;
723 }
724 
725 /** Event to run 'shutdown did not work callback'. */
726 static struct event *shutdown_did_not_work_event = NULL;
727 
728 /** Failsafe measure that should never actually be necessary: If
729  * tor_shutdown_event_loop_and_exit() somehow doesn't successfully exit the
730  * event loop, then this callback will kill Tor with an assertion failure
731  * seconds later
732  */
733 static void
734 shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
735 {
736  // LCOV_EXCL_START
737  (void) fd;
738  (void) event;
739  (void) arg;
740  tor_assert_unreached();
741  // LCOV_EXCL_STOP
742 }
743 
744 #ifdef ENABLE_RESTART_DEBUGGING
745 static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
746 static void
747 tor_shutdown_event_loop_for_restart_cb(
748  evutil_socket_t fd, short event, void *arg)
749 {
750  (void)fd;
751  (void)event;
752  (void)arg;
753  tor_event_free(tor_shutdown_event_loop_for_restart_event);
755 }
756 #endif /* defined(ENABLE_RESTART_DEBUGGING) */
757 
758 /**
759  * After finishing the current callback (if any), shut down the main loop,
760  * clean up the process, and exit with <b>exitcode</b>.
761  */
762 void
764 {
766  return; /* Ignore multiple calls to this function. */
767 
769  main_loop_exit_value = exitcode;
770 
771  if (! tor_libevent_is_initialized()) {
772  return; /* No event loop to shut down. */
773  }
774 
775  /* Die with an assertion failure in ten seconds, if for some reason we don't
776  * exit normally. */
777  /* XXXX We should consider this code if it's never used. */
778  struct timeval ten_seconds = { 10, 0 };
779  shutdown_did_not_work_event = tor_evtimer_new(
782  event_add(shutdown_did_not_work_event, &ten_seconds);
783 
784  /* Unlike exit_loop_after_delay(), exit_loop_after_callback
785  * prevents other callbacks from running. */
787 }
788 
789 /** Return true iff tor_shutdown_event_loop_and_exit() has been called. */
790 int
792 {
793  return main_loop_should_exit;
794 }
795 
796 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
797  * its linked connection, if it is not doing so already. Called by
798  * connection_start_reading and connection_start_writing as appropriate. */
799 static void
801 {
802  tor_assert(conn);
803  tor_assert(conn->linked == 1);
804 
805  if (!conn->active_on_link) {
806  conn->active_on_link = 1;
809  } else {
811  }
812 }
813 
814 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
815  * connection, if is currently doing so. Called by connection_stop_reading,
816  * connection_stop_writing, and connection_read. */
817 void
819 {
820  tor_assert(conn);
821  tor_assert(conn->linked == 1);
822 
823  if (conn->active_on_link) {
824  conn->active_on_link = 0;
825  /* FFFF We could keep an index here so we can smartlist_del
826  * cleanly. On the other hand, this doesn't show up on profiles,
827  * so let's leave it alone for now. */
829  } else {
831  }
832 }
833 
834 /** Close all connections that have been scheduled to get closed. */
835 STATIC void
837 {
838  int i;
839  for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
840  connection_t *conn = smartlist_get(closeable_connection_lst, i);
841  if (conn->conn_array_index < 0) {
842  connection_unlink(conn); /* blow it away right now */
843  } else {
845  ++i;
846  }
847  }
848 }
849 
850 /** Count moribund connections for the OOS handler */
851 MOCK_IMPL(int,
853 {
854  int moribund = 0;
855 
856  /*
857  * Count things we'll try to kill when close_closeable_connections()
858  * runs next.
859  */
861  if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
862  } SMARTLIST_FOREACH_END(conn);
863 
864  return moribund;
865 }
866 
867 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
868  * some data to read. */
869 static void
870 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
871 {
872  connection_t *conn = _conn;
873  (void)fd;
874  (void)event;
875 
876  log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
877 
878  /* assert_connection_ok(conn, time(NULL)); */
879 
880  /* Handle marked for close connections early */
881  if (conn->marked_for_close && connection_is_reading(conn)) {
882  /* Libevent says we can read, but we are marked for close so we will never
883  * try to read again. We will try to close the connection below inside of
884  * close_closeable_connections(), but let's make sure not to cause Libevent
885  * to spin on conn_read_callback() while we wait for the socket to let us
886  * flush to it.*/
888  }
889 
890  if (connection_handle_read(conn) < 0) {
891  if (!conn->marked_for_close) {
892 #ifndef _WIN32
893  log_warn(LD_BUG,"Unhandled error on read for %s connection "
894  "(fd %d); removing",
895  conn_type_to_string(conn->type), (int)conn->s);
897 #endif /* !defined(_WIN32) */
898  if (CONN_IS_EDGE(conn))
900  connection_mark_for_close(conn);
901  }
902  }
903  assert_connection_ok(conn, time(NULL));
904 
905  if (smartlist_len(closeable_connection_lst))
907 }
908 
909 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
910  * some data to write. */
911 static void
912 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
913 {
914  connection_t *conn = _conn;
915  (void)fd;
916  (void)events;
917 
918  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
919  (int)conn->s));
920 
921  /* assert_connection_ok(conn, time(NULL)); */
922 
923  if (connection_handle_write(conn, 0) < 0) {
924  if (!conn->marked_for_close) {
925  /* this connection is broken. remove it. */
927  "unhandled error on write for %s connection (fd %d); removing",
928  conn_type_to_string(conn->type), (int)conn->s);
930  if (CONN_IS_EDGE(conn)) {
931  /* otherwise we cry wolf about duplicate close */
932  edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
933  if (!edge_conn->end_reason)
934  edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
935  edge_conn->edge_has_sent_end = 1;
936  }
937  connection_close_immediate(conn); /* So we don't try to flush. */
938  connection_mark_for_close(conn);
939  }
940  }
941  assert_connection_ok(conn, time(NULL));
942 
943  if (smartlist_len(closeable_connection_lst))
945 }
946 
947 /** If the connection at connection_array[i] is marked for close, then:
948  * - If it has data that it wants to flush, try to flush it.
949  * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
950  * true, then leave the connection open and return.
951  * - Otherwise, remove the connection from connection_array and from
952  * all other lists, close it, and free it.
953  * Returns 1 if the connection was closed, 0 otherwise.
954  */
955 static int
957 {
958  connection_t *conn;
959  int retval;
960  time_t now;
961 
962  conn = smartlist_get(connection_array, i);
963  if (!conn->marked_for_close)
964  return 0; /* nothing to see here, move along */
965  now = time(NULL);
966  assert_connection_ok(conn, now);
967 
968  log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
969  conn->s);
970 
971  /* If the connection we are about to close was trying to connect to
972  a proxy server and failed, the client won't be able to use that
973  proxy. We should warn the user about this. */
974  if (conn->proxy_state == PROXY_INFANT)
976 
977  if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
979  /* s == -1 means it's an incomplete edge connection, or that the socket
980  * has already been closed as unflushable. */
981  ssize_t sz = connection_bucket_write_limit(conn, now);
982  if (!conn->hold_open_until_flushed)
983  log_info(LD_NET,
984  "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
985  "to flush %"TOR_PRIuSZ" bytes. (Marked at %s:%d)",
987  (int)conn->s, conn_type_to_string(conn->type), conn->state,
988  connection_get_outbuf_len(conn),
990  if (conn->linked_conn) {
991  retval = (int) buf_move_all(conn->linked_conn->inbuf, conn->outbuf);
992  if (retval >= 0) {
993  /* The linked conn will notice that it has data when it notices that
994  * we're gone. */
996  }
997  log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
998  "%d left; wants-to-flush==%d", retval,
999  (int)connection_get_outbuf_len(conn),
1001  } else if (connection_speaks_cells(conn)) {
1002  if (conn->state == OR_CONN_STATE_OPEN) {
1003  retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz);
1004  } else
1005  retval = -1; /* never flush non-open broken tls connections */
1006  } else {
1007  retval = buf_flush_to_socket(conn->outbuf, conn->s, sz);
1008  }
1009  if (retval >= 0 && /* Technically, we could survive things like
1010  TLS_WANT_WRITE here. But don't bother for now. */
1012  if (retval > 0) {
1013  LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
1014  "Holding conn (fd %d) open for more flushing.",
1015  (int)conn->s));
1016  conn->timestamp_last_write_allowed = now; /* reset so we can flush
1017  * more */
1018  } else if (sz == 0) {
1019  /* Also, retval==0. If we get here, we didn't want to write anything
1020  * (because of rate-limiting) and we didn't. */
1021 
1022  /* Connection must flush before closing, but it's being rate-limited.
1023  * Let's remove from Libevent, and mark it as blocked on bandwidth
1024  * so it will be re-added on next token bucket refill. Prevents
1025  * busy Libevent loops where we keep ending up here and returning
1026  * 0 until we are no longer blocked on bandwidth.
1027  */
1029  /* Make sure that consider_empty_buckets really disabled the
1030  * connection: */
1031  if (BUG(connection_is_writing(conn))) {
1032  connection_write_bw_exhausted(conn, true);
1033  }
1034 
1035  /* The connection is being held due to write rate limit and thus will
1036  * flush its data later. We need to stop reading because this
1037  * connection is about to be closed once flushed. It should not
1038  * process anything more coming in at this stage. */
1040  }
1041  return 0;
1042  }
1043  if (connection_wants_to_flush(conn)) {
1044  log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1045  "bytes to address %s. If this happens a lot, either "
1046  "something is wrong with your network connection, or "
1047  "something is wrong with theirs. "
1048  "(fd %d, type %s, state %d, marked at %s:%d).",
1049  (int)connection_get_outbuf_len(conn),
1051  (int)conn->s, conn_type_to_string(conn->type), conn->state,
1052  conn->marked_for_close_file,
1053  conn->marked_for_close);
1054  }
1055  }
1056 
1057  connection_unlink(conn); /* unlink, remove, free */
1058  return 1;
1059 }
1060 
1061 /** Implementation for directory_all_unreachable. This is done in a callback,
1062  * since otherwise it would complicate Tor's control-flow graph beyond all
1063  * reason.
1064  */
1065 static void
1067 {
1068  (void)event;
1069  (void)arg;
1070 
1071  connection_t *conn;
1072 
1075  entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1076  log_notice(LD_NET,
1077  "Is your network connection down? "
1078  "Failing connection to '%s:%d'.",
1079  safe_str_client(entry_conn->socks_request->address),
1080  entry_conn->socks_request->port);
1081  connection_mark_unattached_ap(entry_conn,
1083  }
1084  control_event_general_error("DIR_ALL_UNREACHABLE");
1085 }
1086 
1087 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1088 
1089 /** We've just tried every dirserver we know about, and none of
1090  * them were reachable. Assume the network is down. Change state
1091  * so next time an application connection arrives we'll delay it
1092  * and try another directory fetch. Kill off all the circuit_wait
1093  * streams that are waiting now, since they will all timeout anyway.
1094  */
1095 void
1097 {
1098  (void)now;
1099 
1100  reset_uptime(); /* reset it */
1101 
1102  if (!directory_all_unreachable_cb_event) {
1103  directory_all_unreachable_cb_event =
1105  tor_assert(directory_all_unreachable_cb_event);
1106  }
1107 
1108  mainloop_event_activate(directory_all_unreachable_cb_event);
1109 }
1110 
1111 /** This function is called whenever we successfully pull down some new
1112  * network statuses or server descriptors. */
1113 void
1114 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1115 {
1116  const or_options_t *options = get_options();
1117 
1118  /* if we have enough dir info, then update our guard status with
1119  * whatever we just learned. */
1120  int invalidate_circs = guards_update_all();
1121 
1122  if (invalidate_circs) {
1125  }
1126 
1128  int quiet = suppress_logs || from_cache ||
1131  "I learned some more directory information, but not enough to "
1132  "build a circuit: %s", get_dir_info_status_string());
1134  return;
1135  } else {
1136  if (dirclient_fetches_from_authorities(options)) {
1138  }
1139 
1140  /* Don't even bother trying to get extrainfo until the rest of our
1141  * directory info is up-to-date */
1142  if (options->DownloadExtraInfo)
1144  }
1145 
1146  if (server_mode(options) && !net_is_disabled() && !from_cache &&
1149 }
1150 
1151 /** Perform regular maintenance tasks for a single connection. This
1152  * function gets run once per second per connection by run_scheduled_events.
1153  */
1154 static void
1156 {
1157  cell_t cell;
1158  connection_t *conn = smartlist_get(connection_array, i);
1159  const or_options_t *options = get_options();
1160  or_connection_t *or_conn;
1161  channel_t *chan = NULL;
1162  int have_any_circuits;
1163  int past_keepalive =
1164  now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1165 
1166  if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1167  conn->type == CONN_TYPE_OR)
1168  TO_OR_CONN(conn)->timestamp_lastempty = now;
1169 
1170  if (conn->marked_for_close) {
1171  /* nothing to do here */
1172  return;
1173  }
1174 
1175  /* Expire any directory connections that haven't been active (sent
1176  * if a server or received if a client) for 5 min */
1177  if (conn->type == CONN_TYPE_DIR &&
1178  ((DIR_CONN_IS_SERVER(conn) &&
1180  + options->TestingDirConnectionMaxStall < now) ||
1181  (!DIR_CONN_IS_SERVER(conn) &&
1183  + options->TestingDirConnectionMaxStall < now))) {
1184  log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1185  (int)conn->s, conn->purpose);
1186  /* This check is temporary; it's to let us know whether we should consider
1187  * parsing partial serverdesc responses. */
1188  if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1189  connection_get_inbuf_len(conn) >= 1024) {
1190  log_info(LD_DIR,"Trying to extract information from wedged server desc "
1191  "download.");
1193  } else {
1194  connection_mark_for_close(conn);
1195  }
1196  return;
1197  }
1198 
1199  if (!connection_speaks_cells(conn))
1200  return; /* we're all done here, the rest is just for OR conns */
1201 
1202  /* If we haven't flushed to an OR connection for a while, then either nuke
1203  the connection or send a keepalive, depending. */
1204 
1205  or_conn = TO_OR_CONN(conn);
1206  tor_assert(conn->outbuf);
1207 
1208  chan = TLS_CHAN_TO_BASE(or_conn->chan);
1209  tor_assert(chan);
1210 
1211  if (channel_num_circuits(chan) != 0) {
1212  have_any_circuits = 1;
1213  chan->timestamp_last_had_circuits = now;
1214  } else {
1215  have_any_circuits = 0;
1216  }
1217 
1218  if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1219  ! have_any_circuits) {
1220  /* It's bad for new circuits, and has no unmarked circuits on it:
1221  * mark it now. */
1222  log_info(LD_OR,
1223  "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1224  (int)conn->s, fmt_and_decorate_addr(&conn->addr), conn->port);
1225  if (conn->state == OR_CONN_STATE_CONNECTING)
1227  END_OR_CONN_REASON_TIMEOUT,
1228  "Tor gave up on the connection");
1230  } else if (!connection_state_is_open(conn)) {
1231  if (past_keepalive) {
1232  /* We never managed to actually get this connection open and happy. */
1233  log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1234  (int)conn->s, fmt_and_decorate_addr(&conn->addr), conn->port);
1236  }
1237  } else if (we_are_hibernating() &&
1238  ! have_any_circuits &&
1239  !connection_get_outbuf_len(conn)) {
1240  /* We're hibernating or shutting down, there's no circuits, and nothing to
1241  * flush.*/
1242  log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1243  "[Hibernating or exiting].",
1244  (int)conn->s, fmt_and_decorate_addr(&conn->addr), conn->port);
1246  } else if (!have_any_circuits &&
1247  now - or_conn->idle_timeout >=
1249  log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
1250  "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1251  (chan->global_identifier),
1252  (int)conn->s, fmt_and_decorate_addr(&conn->addr), conn->port,
1253  (int)(now - chan->timestamp_last_had_circuits),
1254  or_conn->idle_timeout,
1255  or_conn->is_canonical ? "" : "non");
1257  } else if (
1258  now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1259  now >=
1260  conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1261  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1262  "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1263  "flush; %d seconds since last write)",
1264  (int)conn->s, fmt_and_decorate_addr(&conn->addr), conn->port,
1265  (int)connection_get_outbuf_len(conn),
1266  (int)(now-conn->timestamp_last_write_allowed));
1268  } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1269  /* send a padding cell */
1270  log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1271  fmt_and_decorate_addr(&conn->addr), conn->port);
1272  memset(&cell,0,sizeof(cell_t));
1273  cell.command = CELL_PADDING;
1274  connection_or_write_cell_to_buf(&cell, or_conn);
1275  } else {
1277  }
1278 }
1279 
1280 /** Honor a NEWNYM request: make future requests unlinkable to past
1281  * requests. */
1282 static void
1283 signewnym_impl(time_t now)
1284 {
1285  const or_options_t *options = get_options();
1286  if (!proxy_mode(options)) {
1287  log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1288  "is disabled.");
1289  return;
1290  }
1291 
1296  time_of_last_signewnym = now;
1298 
1299  ++newnym_epoch;
1300 
1301  control_event_signal(SIGNEWNYM);
1302 }
1303 
1304 /** Callback: run a deferred signewnym. */
1305 static void
1307 {
1308  (void)event;
1309  (void)arg;
1310  log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1311  do_signewnym(time(NULL));
1312 }
1313 
1314 /** Either perform a signewnym or schedule one, depending on rate limiting. */
1315 void
1316 do_signewnym(time_t now)
1317 {
1319  const time_t delay_sec =
1321  if (! signewnym_is_pending) {
1326  }
1327  const struct timeval delay_tv = { delay_sec, 0 };
1329  }
1330  log_notice(LD_CONTROL,
1331  "Rate limiting NEWNYM request: delaying by %d second(s)",
1332  (int)(delay_sec));
1333  } else {
1334  signewnym_impl(now);
1335  }
1336 }
1337 
1338 /** Return the number of times that signewnym has been called. */
1339 unsigned
1341 {
1342  return newnym_epoch;
1343 }
1344 
1345 /** True iff we have initialized all the members of <b>periodic_events</b>.
1346  * Used to prevent double-initialization. */
1348 
1349 /* Declare all the timer callback functions... */
1350 #ifndef COCCI
1351 #undef CALLBACK
1352 #define CALLBACK(name) \
1353  static int name ## _callback(time_t, const or_options_t *)
1354 
1355 CALLBACK(add_entropy);
1356 CALLBACK(check_expired_networkstatus);
1357 CALLBACK(clean_caches);
1358 CALLBACK(clean_consdiffmgr);
1359 CALLBACK(fetch_networkstatus);
1360 CALLBACK(heartbeat);
1361 CALLBACK(hs_service);
1362 CALLBACK(launch_descriptor_fetches);
1363 CALLBACK(prune_old_routers);
1364 CALLBACK(record_bridge_stats);
1365 CALLBACK(rend_cache_failure_clean);
1366 CALLBACK(reset_padding_counts);
1367 CALLBACK(retry_listeners);
1368 CALLBACK(rotate_x509_certificate);
1369 CALLBACK(save_state);
1370 CALLBACK(write_stats_file);
1371 CALLBACK(control_per_second_events);
1372 CALLBACK(second_elapsed);
1373 CALLBACK(manage_vglite);
1374 
1375 #undef CALLBACK
1376 
1377 /* Now we declare an array of periodic_event_item_t for each periodic event */
1378 #define CALLBACK(name, r, f) \
1379  PERIODIC_EVENT(name, PERIODIC_EVENT_ROLE_ ## r, f)
1380 #define FL(name) (PERIODIC_EVENT_FLAG_ ## name)
1381 #endif /* !defined(COCCI) */
1382 
1383 STATIC periodic_event_item_t mainloop_periodic_events[] = {
1384 
1385  /* Everyone needs to run these. They need to have very long timeouts for
1386  * that to be safe. */
1387  CALLBACK(add_entropy, ALL, 0),
1388  CALLBACK(heartbeat, ALL, 0),
1389  CALLBACK(reset_padding_counts, ALL, 0),
1390 
1391  /* This is a legacy catch-all callback that runs once per second if
1392  * we are online and active. */
1393  CALLBACK(second_elapsed, NET_PARTICIPANT,
1394  FL(RUN_ON_DISABLE)),
1395 
1396  /* Update vanguards-lite once per hour, if we have networking */
1397  CALLBACK(manage_vglite, NET_PARTICIPANT, FL(NEED_NET)),
1398 
1399  /* XXXX Do we have a reason to do this on a callback? Does it do any good at
1400  * all? For now, if we're dormant, we can let our listeners decay. */
1401  CALLBACK(retry_listeners, NET_PARTICIPANT, FL(NEED_NET)),
1402 
1403  /* We need to do these if we're participating in the Tor network. */
1404  CALLBACK(check_expired_networkstatus, NET_PARTICIPANT, 0),
1405  CALLBACK(fetch_networkstatus, NET_PARTICIPANT, 0),
1406  CALLBACK(launch_descriptor_fetches, NET_PARTICIPANT, FL(NEED_NET)),
1407  CALLBACK(rotate_x509_certificate, NET_PARTICIPANT, 0),
1408  CALLBACK(check_network_participation, NET_PARTICIPANT, 0),
1409 
1410  /* We need to do these if we're participating in the Tor network, and
1411  * immediately before we stop. */
1412  CALLBACK(clean_caches, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1413  CALLBACK(save_state, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1414  CALLBACK(write_stats_file, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1415  CALLBACK(prune_old_routers, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1416 
1417  /* Hidden Service service only. */
1418  CALLBACK(hs_service, HS_SERVICE, FL(NEED_NET)), // XXXX break this down more
1419 
1420  /* Bridge only. */
1421  CALLBACK(record_bridge_stats, BRIDGE, 0),
1422 
1423  /* Client only. */
1424  /* XXXX this could be restricted to CLIENT+NET_PARTICIPANT */
1425  CALLBACK(rend_cache_failure_clean, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1426 
1427  /* Directory server only. */
1428  CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
1429 
1430  /* Controller with per-second events only. */
1431  CALLBACK(control_per_second_events, CONTROLEV, 0),
1432 
1433  END_OF_PERIODIC_EVENTS
1434 };
1435 #ifndef COCCI
1436 #undef CALLBACK
1437 #undef FL
1438 #endif
1439 
1440 /* These are pointers to members of periodic_events[] that are used to
1441  * implement particular callbacks. We keep them separate here so that we
1442  * can access them by name. We also keep them inside periodic_events[]
1443  * so that we can implement "reset all timers" in a reasonable way. */
1444 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1445 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1446 static periodic_event_item_t *check_dns_honesty_event=NULL;
1447 static periodic_event_item_t *save_state_event=NULL;
1448 static periodic_event_item_t *prune_old_routers_event=NULL;
1449 
1450 /** Reset all the periodic events so we'll do all our actions again as if we
1451  * just started up.
1452  * Useful if our clock just moved back a long time from the future,
1453  * so we don't wait until that future arrives again before acting.
1454  */
1455 void
1457 {
1459 }
1460 
1461 /** Return a bitmask of the roles this tor instance is configured for using
1462  * the given options. */
1463 STATIC int
1465 {
1466  tor_assert(options);
1467 
1468  int roles = PERIODIC_EVENT_ROLE_ALL;
1469  int is_bridge = options->BridgeRelay;
1470  int is_relay = server_mode(options);
1471  int is_dirauth = authdir_mode_v3(options);
1472  int is_bridgeauth = authdir_mode_bridge(options);
1473  int is_hidden_service = !!hs_service_get_num_services();
1474  int is_dirserver = dir_server_mode(options);
1475  int sending_control_events = control_any_per_second_event_enabled();
1476 
1477  /* We also consider tor to have the role of a client if the ControlPort is
1478  * set because a lot of things can be done over the control port which
1479  * requires tor to have basic functionalities. */
1480  int is_client = options_any_client_port_set(options) ||
1481  options->ControlPort_set ||
1482  options->OwningControllerFD != UINT64_MAX;
1483 
1484  int is_net_participant = is_participating_on_network() ||
1485  is_relay || is_hidden_service;
1486 
1487  if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1488  if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1489  if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1490  if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1491  if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1492  if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1493  if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
1494  if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
1495  if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
1496 
1497  return roles;
1498 }
1499 
1500 /** Event to run initialize_periodic_events_cb */
1501 static struct event *initialize_periodic_events_event = NULL;
1502 
1503 /** Helper, run one second after setup:
1504  * Initializes all members of periodic_events and starts them running.
1505  *
1506  * (We do this one second after setup for backward-compatibility reasons;
1507  * it might not actually be necessary.) */
1508 static void
1509 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1510 {
1511  (void) fd;
1512  (void) events;
1513  (void) data;
1514 
1515  tor_event_free(initialize_periodic_events_event);
1516 
1518 }
1519 
1520 /** Set up all the members of mainloop_periodic_events[], and configure them
1521  * all to be launched from a callback. */
1522 void
1524 {
1526  return;
1527 
1529 
1530  for (int i = 0; mainloop_periodic_events[i].name; ++i) {
1531  periodic_events_register(&mainloop_periodic_events[i]);
1532  }
1533 
1534  /* Set up all periodic events. We'll launch them by roles. */
1535 
1536 #ifndef COCCI
1537 #define NAMED_CALLBACK(name) \
1538  STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
1539 #endif
1540 
1541  NAMED_CALLBACK(prune_old_routers);
1542  NAMED_CALLBACK(fetch_networkstatus);
1543  NAMED_CALLBACK(launch_descriptor_fetches);
1544  NAMED_CALLBACK(check_dns_honesty);
1545  NAMED_CALLBACK(save_state);
1546 }
1547 
1548 STATIC void
1549 teardown_periodic_events(void)
1550 {
1552  fetch_networkstatus_event = NULL;
1553  launch_descriptor_fetches_event = NULL;
1554  check_dns_honesty_event = NULL;
1555  save_state_event = NULL;
1556  prune_old_routers_event = NULL;
1558 }
1559 
1560 static mainloop_event_t *rescan_periodic_events_ev = NULL;
1561 
1562 /** Callback: rescan the periodic event list. */
1563 static void
1565 {
1566  (void)event;
1567  (void)arg;
1569 }
1570 
1571 /**
1572  * Schedule an event that will rescan which periodic events should run.
1573  **/
1574 MOCK_IMPL(void,
1576 {
1577  if (!rescan_periodic_events_ev) {
1578  rescan_periodic_events_ev =
1580  }
1581  mainloop_event_activate(rescan_periodic_events_ev);
1582 }
1583 
1584 /** Do a pass at all our periodic events, disable those we don't need anymore
1585  * and enable those we need now using the given options. */
1586 void
1588 {
1589  tor_assert(options);
1590 
1592 }
1593 
1594 /* We just got new options globally set, see if we need to enabled or disable
1595  * periodic events. */
1596 void
1597 periodic_events_on_new_options(const or_options_t *options)
1598 {
1599  rescan_periodic_events(options);
1600 }
1601 
1602 /**
1603  * Update our schedule so that we'll check whether we need to fetch directory
1604  * info immediately.
1605  */
1606 void
1608 {
1609  tor_assert(fetch_networkstatus_event);
1610  tor_assert(launch_descriptor_fetches_event);
1611 
1612  periodic_event_reschedule(fetch_networkstatus_event);
1613  periodic_event_reschedule(launch_descriptor_fetches_event);
1614 }
1615 
1616 /** Mainloop callback: clean up circuits, channels, and connections
1617  * that are pending close. */
1618 static void
1620 {
1621  (void)ev;
1622  (void)arg;
1627 }
1628 
1629 /** Event to run postloop_cleanup_cb */
1631 
1632 /** Schedule a post-loop event to clean up marked channels, connections, and
1633  * circuits. */
1634 void
1636 {
1637  if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
1638  // (It's possible that we can get here if we decide to close a connection
1639  // in the earliest stages of our configuration, before we create events.)
1640  return;
1641  }
1643 }
1644 
1645 /** Event to run 'scheduled_shutdown_cb' */
1647 
1648 /** Callback: run a scheduled shutdown */
1649 static void
1651 {
1652  (void)ev;
1653  (void)arg;
1654  log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
1656 }
1657 
1658 /** Schedule the mainloop to exit after <b>delay_sec</b> seconds. */
1659 void
1661 {
1662  const struct timeval delay_tv = { delay_sec, 0 };
1663  if (! scheduled_shutdown_ev) {
1665  }
1667 }
1668 
1669 /**
1670  * Update vanguards-lite layer2 nodes, once every 15 minutes
1671  */
1672 static int
1673 manage_vglite_callback(time_t now, const or_options_t *options)
1674 {
1675  (void)now;
1676  (void)options;
1677 #define VANGUARDS_LITE_INTERVAL (15*60)
1678 
1680 
1681  return VANGUARDS_LITE_INTERVAL;
1682 }
1683 
1684 /** Perform regular maintenance tasks. This function gets run once per
1685  * second.
1686  */
1687 static int
1688 second_elapsed_callback(time_t now, const or_options_t *options)
1689 {
1690  /* 0. See if our bandwidth limits are exhausted and we should hibernate
1691  *
1692  * Note: we have redundant mechanisms to handle the case where it's
1693  * time to wake up from hibernation; or where we have a scheduled
1694  * shutdown and it's time to run it, but this will also handle those.
1695  */
1696  consider_hibernation(now);
1697 
1698  /* Maybe enough time elapsed for us to reconsider a circuit. */
1700 
1701  if (options->UseBridges && !net_is_disabled()) {
1702  /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1703  * -- the latter is only for fetching consensus-derived directory info. */
1704  // TODO: client
1705  // Also, schedule this rather than probing 1x / sec
1706  fetch_bridge_descriptors(options, now);
1707  }
1708 
1709  if (accounting_is_enabled(options)) {
1710  // TODO: refactor or rewrite?
1712  }
1713 
1714  /* 3a. Every second, we examine pending circuits and prune the
1715  * ones which have been pending for more than a few seconds.
1716  * We do this before step 4, so it can try building more if
1717  * it's not comfortable with the number of available circuits.
1718  */
1719  /* (If our circuit build timeout can ever become lower than a second (which
1720  * it can't, currently), we should do this more often.) */
1721  // TODO: All expire stuff can become NET_PARTICIPANT, RUN_ON_DISABLE
1724 
1725  /* 3b. Also look at pending streams and prune the ones that 'began'
1726  * a long time ago but haven't gotten a 'connected' yet.
1727  * Do this before step 4, so we can put them back into pending
1728  * state to be picked up by the new circuit.
1729  */
1731 
1732  /* 3c. And expire connections that we've held open for too long.
1733  */
1735 
1736  /* 4. Every second, we try a new circuit if there are no valid
1737  * circuits. Every NewCircuitPeriod seconds, we expire circuits
1738  * that became dirty more than MaxCircuitDirtiness seconds ago,
1739  * and we make a new circ if there are no clean circuits.
1740  */
1741  const int have_dir_info = router_have_minimum_dir_info();
1742  if (have_dir_info && !net_is_disabled()) {
1744  } else {
1746  }
1747 
1748  /* 5. We do housekeeping for each connection... */
1750  int i;
1751  for (i=0;i<smartlist_len(connection_array);i++) {
1753  }
1754 
1755  /* Run again in a second. */
1756  return 1;
1757 }
1758 
1759 /**
1760  * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1761  * see about fetching descriptors, microdescriptors, and extrainfo
1762  * documents.
1763  */
1764 static int
1766 {
1767  if (should_delay_dir_fetches(options, NULL))
1768  return PERIODIC_EVENT_NO_UPDATE;
1769 
1774  else
1776 }
1777 
1778 /**
1779  * Periodic event: Rotate our X.509 certificates and TLS keys once every
1780  * MAX_SSL_KEY_LIFETIME_INTERNAL.
1781  */
1782 static int
1784 {
1785  static int first = 1;
1786  (void)now;
1787  (void)options;
1788  if (first) {
1789  first = 0;
1791  }
1792 
1793  /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1794  * TLS context. */
1795  log_info(LD_GENERAL,"Rotating tls context.");
1796  if (router_initialize_tls_context() < 0) {
1797  log_err(LD_BUG, "Error reinitializing TLS context");
1798  tor_assert_unreached();
1799  }
1800  if (generate_ed_link_cert(options, now, 1)) {
1801  log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1802  "new TLS context.");
1803  tor_assert_unreached();
1804  }
1805 
1806  /* We also make sure to rotate the TLS connections themselves if they've
1807  * been up for too long -- but that's done via is_bad_for_new_circs in
1808  * run_connection_housekeeping() above. */
1810 }
1811 
1812 /**
1813  * Periodic callback: once an hour, grab some more entropy from the
1814  * kernel and feed it to our CSPRNG.
1815  **/
1816 static int
1817 add_entropy_callback(time_t now, const or_options_t *options)
1818 {
1819  (void)now;
1820  (void)options;
1821  /* We already seeded once, so don't die on failure. */
1822  if (crypto_seed_rng() < 0) {
1823  log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1824  "seeded once, though, so we won't exit here.");
1825  }
1826 
1827  /** How often do we add more entropy to OpenSSL's RNG pool? */
1828 #define ENTROPY_INTERVAL (60*60)
1829  return ENTROPY_INTERVAL;
1830 }
1831 
1832 /** Periodic callback: if there has been no network usage in a while,
1833  * enter a dormant state. */
1834 STATIC int
1836 {
1837  /* If we're a server, we can't become dormant. */
1838  if (server_mode(options)) {
1839  goto found_activity;
1840  }
1841 
1842  /* If we aren't allowed to become dormant, then participation doesn't
1843  matter */
1844  if (! options->DormantTimeoutEnabled) {
1845  goto found_activity;
1846  }
1847 
1848  /* If we're running an onion service, we can't become dormant. */
1849  /* XXXX this would be nice to change, so that we can be dormant with a
1850  * service. */
1852  goto found_activity;
1853  }
1854 
1855  /* If we have any currently open entry streams other than "linked"
1856  * connections used for directory requests, those count as user activity.
1857  */
1858  if (options->DormantTimeoutDisabledByIdleStreams) {
1860  goto found_activity;
1861  }
1862  }
1863 
1864  /* XXXX Make this configurable? */
1865 /** How often do we check whether we have had network activity? */
1866 #define CHECK_PARTICIPATION_INTERVAL (5*60)
1867 
1868  /* Become dormant if there has been no user activity in a long time.
1869  * (The funny checks below are in order to prevent overflow.) */
1870  time_t time_since_last_activity = 0;
1871  if (get_last_user_activity_time() < now)
1872  time_since_last_activity = now - get_last_user_activity_time();
1873  if (time_since_last_activity >= options->DormantClientTimeout) {
1874  log_notice(LD_GENERAL, "No user activity in a long time: becoming"
1875  " dormant.");
1877  rescan_periodic_events(options);
1878  }
1879 
1880  return CHECK_PARTICIPATION_INTERVAL;
1881 
1882  found_activity:
1883  note_user_activity(now);
1884  return CHECK_PARTICIPATION_INTERVAL;
1885 }
1886 
1887 /**
1888  * Periodic callback: If our consensus is too old, recalculate whether
1889  * we can actually use it.
1890  */
1891 static int
1893 {
1894  (void)options;
1895  /* Check whether our networkstatus has expired. */
1897  /* Use reasonably live consensuses until they are no longer reasonably live.
1898  */
1899  if (ns && !networkstatus_consensus_reasonably_live(ns, now) &&
1902  }
1903 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1904  return CHECK_EXPIRED_NS_INTERVAL;
1905 }
1906 
1907 /**
1908  * Scheduled callback: Save the state file to disk if appropriate.
1909  */
1910 static int
1911 save_state_callback(time_t now, const or_options_t *options)
1912 {
1913  (void) options;
1914  (void) or_state_save(now); // only saves if appropriate
1915  const time_t next_write = get_or_state()->next_write;
1916  if (next_write == TIME_MAX) {
1917  return 86400;
1918  }
1919  return safe_timer_diff(now, next_write);
1920 }
1921 
1922 /** Reschedule the event for saving the state file.
1923  *
1924  * Run this when the state becomes dirty. */
1925 void
1927 {
1928  if (save_state_event == NULL) {
1929  /* This can happen early on during startup. */
1930  return;
1931  }
1932  periodic_event_reschedule(save_state_event);
1933 }
1934 
1935 /**
1936  * Periodic callback: Write statistics to disk if appropriate.
1937  */
1938 static int
1939 write_stats_file_callback(time_t now, const or_options_t *options)
1940 {
1941  /* 1g. Check whether we should write statistics to disk.
1942  */
1943 #define CHECK_WRITE_STATS_INTERVAL (60*60)
1944  time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
1945  if (options->CellStatistics) {
1946  time_t next_write =
1948  if (next_write && next_write < next_time_to_write_stats_files)
1949  next_time_to_write_stats_files = next_write;
1950  }
1951  if (options->DirReqStatistics) {
1952  time_t next_write = geoip_dirreq_stats_write(now);
1953  if (next_write && next_write < next_time_to_write_stats_files)
1954  next_time_to_write_stats_files = next_write;
1955  }
1956  if (options->EntryStatistics) {
1957  time_t next_write = geoip_entry_stats_write(now);
1958  if (next_write && next_write < next_time_to_write_stats_files)
1959  next_time_to_write_stats_files = next_write;
1960  }
1961  if (options->HiddenServiceStatistics) {
1962  time_t next_write = rep_hist_hs_stats_write(now, false);
1963  if (next_write && next_write < next_time_to_write_stats_files)
1964  next_time_to_write_stats_files = next_write;
1965 
1966  next_write = rep_hist_hs_stats_write(now, true);
1967  if (next_write && next_write < next_time_to_write_stats_files)
1968  next_time_to_write_stats_files = next_write;
1969  }
1970  if (options->ExitPortStatistics) {
1971  time_t next_write = rep_hist_exit_stats_write(now);
1972  if (next_write && next_write < next_time_to_write_stats_files)
1973  next_time_to_write_stats_files = next_write;
1974  }
1975  if (options->ConnDirectionStatistics) {
1976  time_t next_write = conn_stats_save(now);
1977  if (next_write && next_write < next_time_to_write_stats_files)
1978  next_time_to_write_stats_files = next_write;
1979  }
1980  if (options->BridgeAuthoritativeDir) {
1981  time_t next_write = rep_hist_desc_stats_write(now);
1982  if (next_write && next_write < next_time_to_write_stats_files)
1983  next_time_to_write_stats_files = next_write;
1984  }
1985 
1986  return safe_timer_diff(now, next_time_to_write_stats_files);
1987 }
1988 
1989 static int
1990 reset_padding_counts_callback(time_t now, const or_options_t *options)
1991 {
1992  if (options->PaddingStatistics) {
1993  rep_hist_prep_published_padding_counts(now);
1994  }
1995 
1998 }
1999 
2000 static int should_init_bridge_stats = 1;
2001 
2002 /**
2003  * Periodic callback: Write bridge statistics to disk if appropriate.
2004  */
2005 static int
2006 record_bridge_stats_callback(time_t now, const or_options_t *options)
2007 {
2008  /* 1h. Check whether we should write bridge statistics to disk.
2009  */
2010  if (should_record_bridge_info(options)) {
2011  if (should_init_bridge_stats) {
2012  /* (Re-)initialize bridge statistics. */
2014  should_init_bridge_stats = 0;
2015  return WRITE_STATS_INTERVAL;
2016  } else {
2017  /* Possibly write bridge statistics to disk and ask when to write
2018  * them next time. */
2019  time_t next = geoip_bridge_stats_write(now);
2020  return safe_timer_diff(now, next);
2021  }
2022  } else if (!should_init_bridge_stats) {
2023  /* Bridge mode was turned off. Ensure that stats are re-initialized
2024  * next time bridge mode is turned on. */
2025  should_init_bridge_stats = 1;
2026  }
2027  return PERIODIC_EVENT_NO_UPDATE;
2028 }
2029 
2030 /**
2031  * Periodic callback: Clean in-memory caches every once in a while
2032  */
2033 static int
2034 clean_caches_callback(time_t now, const or_options_t *options)
2035 {
2036  /* Remove old information from rephist and the rend cache. */
2037  rep_history_clean(now - options->RephistTrackTime);
2039  hs_cache_clean_as_dir(now);
2040  microdesc_cache_rebuild(NULL, 0);
2041 #define CLEAN_CACHES_INTERVAL (30*60)
2042  return CLEAN_CACHES_INTERVAL;
2043 }
2044 
2045 /**
2046  * Periodic callback: Clean the cache of failed hidden service lookups
2047  * frequently.
2048  */
2049 static int
2051 {
2052  (void)options;
2053  /* We don't keep entries that are more than five minutes old so we try to
2054  * clean it as soon as we can since we want to make sure the client waits
2055  * as little as possible for reachability reasons. */
2057  return 30;
2058 }
2059 
2060 /**
2061  * Periodic callback: prune routerlist of old information about Tor network.
2062  */
2063 static int
2064 prune_old_routers_callback(time_t now, const or_options_t *options)
2065 {
2066 #define ROUTERLIST_PRUNING_INTERVAL (60*60) // 1 hour.
2067  (void)now;
2068  (void)options;
2069 
2070  if (!net_is_disabled()) {
2071  /* If any networkstatus documents are no longer recent, we need to
2072  * update all the descriptors' running status. */
2073  /* Remove dead routers. */
2074  log_debug(LD_GENERAL, "Pruning routerlist...");
2076  }
2077 
2078  return ROUTERLIST_PRUNING_INTERVAL;
2079 }
2080 
2081 /**
2082  * Periodic event: once a minute, (or every second if TestingTorNetwork, or
2083  * during client bootstrap), check whether we want to download any
2084  * networkstatus documents. */
2085 static int
2086 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2087 {
2088  /* How often do we check whether we should download network status
2089  * documents? */
2090  const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2091  now);
2092  const int prefer_mirrors = !dirclient_fetches_from_authorities(
2093  get_options());
2094  int networkstatus_dl_check_interval = 60;
2095  /* check more often when testing, or when bootstrapping from mirrors
2096  * (connection limits prevent too many connections being made) */
2097  if (options->TestingTorNetwork
2098  || (we_are_bootstrapping && prefer_mirrors)) {
2099  networkstatus_dl_check_interval = 1;
2100  }
2101 
2102  if (should_delay_dir_fetches(options, NULL))
2103  return PERIODIC_EVENT_NO_UPDATE;
2104 
2106  return networkstatus_dl_check_interval;
2107 }
2108 
2109 /**
2110  * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
2111 static int
2112 retry_listeners_callback(time_t now, const or_options_t *options)
2113 {
2114  (void)now;
2115  (void)options;
2116  if (!net_is_disabled()) {
2117  retry_all_listeners(NULL, 0);
2118  return 60;
2119  }
2120  return PERIODIC_EVENT_NO_UPDATE;
2121 }
2122 
2123 static int heartbeat_callback_first_time = 1;
2124 
2125 /**
2126  * Periodic callback: write the heartbeat message in the logs.
2127  *
2128  * If writing the heartbeat message to the logs fails for some reason, retry
2129  * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
2130  */
2131 static int
2132 heartbeat_callback(time_t now, const or_options_t *options)
2133 {
2134  /* Check if heartbeat is disabled */
2135  if (!options->HeartbeatPeriod) {
2136  return PERIODIC_EVENT_NO_UPDATE;
2137  }
2138 
2139  /* Skip the first one. */
2140  if (heartbeat_callback_first_time) {
2141  heartbeat_callback_first_time = 0;
2142  return options->HeartbeatPeriod;
2143  }
2144 
2145  /* Write the heartbeat message */
2146  if (log_heartbeat(now) == 0) {
2147  return options->HeartbeatPeriod;
2148  } else {
2149  /* If we couldn't write the heartbeat log message, try again in the minimum
2150  * interval of time. */
2151  return MIN_HEARTBEAT_PERIOD;
2152  }
2153 }
2154 
2155 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2156 static int
2157 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2158 {
2159  (void)now;
2160  if (dir_server_mode(options)) {
2162  }
2163  return CDM_CLEAN_CALLBACK_INTERVAL;
2164 }
2165 
2166 /*
2167  * Periodic callback: Run scheduled events for HS service. This is called
2168  * every second.
2169  */
2170 static int
2171 hs_service_callback(time_t now, const or_options_t *options)
2172 {
2173  (void) options;
2174 
2175  /* We need to at least be able to build circuits and that we actually have
2176  * a working network. */
2180  goto end;
2181  }
2182 
2184 
2185  end:
2186  /* Every 1 second. */
2187  return 1;
2188 }
2189 
2190 /*
2191  * Periodic callback: Send once-per-second events to the controller(s).
2192  * This is called every second.
2193  */
2194 static int
2195 control_per_second_events_callback(time_t now, const or_options_t *options)
2196 {
2197  (void) options;
2198  (void) now;
2199 
2201 
2202  return 1;
2203 }
2204 
2205 /** Last time that update_current_time was called. */
2206 static time_t current_second = 0;
2207 /** Last time that update_current_time updated current_second. */
2208 static monotime_coarse_t current_second_last_changed;
2209 
2210 /**
2211  * Set the current time to "now", which should be the value returned by
2212  * time(). Check for clock jumps and track the total number of seconds we
2213  * have been running.
2214  */
2215 void
2217 {
2218  if (PREDICT_LIKELY(now == current_second)) {
2219  /* We call this function a lot. Most frequently, the current second
2220  * will not have changed, so we just return. */
2221  return;
2222  }
2223 
2224  const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
2225 
2226  /* Check the wall clock against the monotonic clock, so we can
2227  * better tell idleness from clock jumps and/or other shenanigans. */
2228  monotime_coarse_t last_updated;
2229  memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
2230  monotime_coarse_get(&current_second_last_changed);
2231 
2232  /** How much clock jumping means that we should adjust our idea of when
2233  * to go dormant? */
2234 #define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE 20
2235 
2236  /* Don't go dormant early or late just because we jumped in time. */
2237  if (ABS(seconds_elapsed) >= NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE) {
2239  netstatus_note_clock_jumped(seconds_elapsed);
2240  }
2241  }
2242 
2243  /** How much clock jumping do we tolerate? */
2244 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2245 
2246  /** How much idleness do we tolerate? */
2247 #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
2248 
2249  if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
2250  // moving back in time is always a bad sign.
2251  circuit_note_clock_jumped(seconds_elapsed, false);
2252 
2253  } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2254  /* Compare the monotonic clock to the result of time(). */
2255  const int32_t monotime_msec_passed =
2256  monotime_coarse_diff_msec32(&last_updated,
2258  const int monotime_sec_passed = monotime_msec_passed / 1000;
2259  const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
2260  /* If the monotonic clock deviates from time(NULL), we have a couple of
2261  * possibilities. On some systems, this means we have been suspended or
2262  * sleeping. Everywhere, it can mean that the wall-clock time has
2263  * been changed -- for example, with settimeofday().
2264  *
2265  * On the other hand, if the monotonic time matches with the wall-clock
2266  * time, we've probably just been idle for a while, with no events firing.
2267  * we tolerate much more of that.
2268  */
2269  const bool clock_jumped = abs(discrepancy) > 2;
2270 
2271  if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
2272  circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
2273  }
2274  } else if (seconds_elapsed > 0) {
2275  stats_n_seconds_working += seconds_elapsed;
2276  }
2277 
2278  update_approx_time(now);
2279  current_second = now;
2280 }
2281 
2282 #ifdef HAVE_SYSTEMD_209
2283 static periodic_timer_t *systemd_watchdog_timer = NULL;
2284 
2285 /** Libevent callback: invoked to reset systemd watchdog. */
2286 static void
2287 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2288 {
2289  (void)timer;
2290  (void)arg;
2291  sd_notify(0, "WATCHDOG=1");
2292 }
2293 #endif /* defined(HAVE_SYSTEMD_209) */
2294 
2295 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2296 
2297 /** Called when our IP address seems to have changed. <b>on_client_conn</b>
2298  * should be true if:
2299  * - we detected a change in our interface address, using an outbound
2300  * connection, and therefore
2301  * - our client TLS keys need to be rotated.
2302  * Otherwise, it should be false, and:
2303  * - we detected a change in our published address
2304  * (using some other method), and therefore
2305  * - the published addresses in our descriptor need to change.
2306  */
2307 void
2308 ip_address_changed(int on_client_conn)
2309 {
2310  const or_options_t *options = get_options();
2311  int server = server_mode(options);
2312 
2313  if (on_client_conn) {
2314  if (! server) {
2315  /* Okay, change our keys. */
2316  if (init_keys_client() < 0)
2317  log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2318  }
2319  } else {
2320  if (server) {
2321  if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2323  reset_uptime();
2325  /* All relays include their IP addresses as their ORPort addresses in
2326  * their descriptor.
2327  * Exit relays also incorporate interface addresses in their exit
2328  * policies, when ExitPolicyRejectLocalInterfaces is set. */
2329  mark_my_descriptor_dirty("IP address changed");
2330  }
2331  }
2332 
2334 }
2335 
2336 /** Forget what we've learned about the correctness of our DNS servers, and
2337  * start learning again. */
2338 void
2340 {
2341  if (server_mode(get_options())) {
2343  if (check_dns_honesty_event) {
2344  periodic_event_reschedule(check_dns_honesty_event);
2345  }
2346  }
2347 }
2348 
2349 /** Initialize some mainloop_event_t objects that we require. */
2350 void
2352 {
2356  }
2357  if (!postloop_cleanup_ev) {
2360  }
2361 }
2362 
2363 /** Tor main loop. */
2364 int
2366 {
2367  /* initialize the periodic events first, so that code that depends on the
2368  * events being present does not assert.
2369  */
2372 
2374 
2375  struct timeval one_second = { 1, 0 };
2376  initialize_periodic_events_event = tor_evtimer_new(
2379  event_add(initialize_periodic_events_event, &one_second);
2380 
2381 #ifdef HAVE_SYSTEMD_209
2382  uint64_t watchdog_delay;
2383  /* set up systemd watchdog notification. */
2384  if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2385  if (! systemd_watchdog_timer) {
2386  struct timeval watchdog;
2387  /* The manager will "act on" us if we don't send them a notification
2388  * every 'watchdog_delay' microseconds. So, send notifications twice
2389  * that often. */
2390  watchdog_delay /= 2;
2391  watchdog.tv_sec = watchdog_delay / 1000000;
2392  watchdog.tv_usec = watchdog_delay % 1000000;
2393 
2394  systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2395  &watchdog,
2396  systemd_watchdog_callback,
2397  NULL);
2398  tor_assert(systemd_watchdog_timer);
2399  }
2400  }
2401 #endif /* defined(HAVE_SYSTEMD_209) */
2402 #ifdef ENABLE_RESTART_DEBUGGING
2403  {
2404  static int first_time = 1;
2405 
2406  if (first_time && getenv("TOR_DEBUG_RESTART")) {
2407  first_time = 0;
2408  const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2409  long sec;
2410  int sec_ok=0;
2411  if (sec_str &&
2412  (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2413  sec_ok) {
2414  /* Okay, we parsed the seconds. */
2415  } else {
2416  sec = 5;
2417  }
2418  struct timeval restart_after = { (time_t) sec, 0 };
2419  tor_shutdown_event_loop_for_restart_event =
2420  tor_evtimer_new(tor_libevent_get_base(),
2421  tor_shutdown_event_loop_for_restart_cb, NULL);
2422  event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2423  }
2424  }
2425 #endif /* defined(ENABLE_RESTART_DEBUGGING) */
2426 
2427  return run_main_loop_until_done();
2428 }
2429 
2430 #ifndef _WIN32
2431 /** Rate-limiter for EINVAL-type libevent warnings. */
2432 static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
2433 #endif
2434 
2435 /**
2436  * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2437  * error", and 1 for "run this again."
2438  */
2439 static int
2441 {
2442  int loop_result;
2443 
2444  if (nt_service_is_stopping())
2445  return 0;
2446 
2448  return 0;
2449 
2450 #ifndef _WIN32
2451  /* Make it easier to tell whether libevent failure is our fault or not. */
2452  errno = 0;
2453 #endif
2454 
2455  if (get_options()->MainloopStats) {
2456  /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2457  * are collecting main loop statistics. */
2458  called_loop_once = 1;
2459  } else {
2460  called_loop_once = 0;
2461  }
2462 
2463  /* Make sure we know (about) what time it is. */
2464  update_approx_time(time(NULL));
2465 
2466  /* Here it is: the main loop. Here we tell Libevent to poll until we have
2467  * an event, or the second ends, or until we have some active linked
2468  * connections to trigger events for. Libevent will wait till one
2469  * of these happens, then run all the appropriate callbacks. */
2472 
2473  if (get_options()->MainloopStats) {
2474  /* Update our main loop counters. */
2475  if (loop_result == 0) {
2476  // The call was successful.
2478  } else if (loop_result == -1) {
2479  // The call was erroneous.
2481  } else if (loop_result == 1) {
2482  // The call didn't have any active or pending events
2483  // to handle.
2485  }
2486  }
2487 
2488  /* Oh, the loop failed. That might be an error that we need to
2489  * catch, but more likely, it's just an interrupted poll() call or something,
2490  * and we should try again. */
2491  if (loop_result < 0) {
2492  int e = tor_socket_errno(-1);
2493  /* let the program survive things like ^z */
2494  if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2495  log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2496  tor_libevent_get_method(), tor_socket_strerror(e), e);
2497  return -1;
2498 #ifndef _WIN32
2499  } else if (e == EINVAL) {
2501  "EINVAL from libevent: should you upgrade libevent?");
2503  log_err(LD_NET, "Too many libevent errors, too fast: dying");
2504  return -1;
2505  }
2506 #endif /* !defined(_WIN32) */
2507  } else {
2508  tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2509  log_debug(LD_NET,"libevent call interrupted.");
2510  /* You can't trust the results of this poll(). Go back to the
2511  * top of the big for loop. */
2512  return 1;
2513  }
2514  }
2515 
2517  return 0;
2518 
2519  return 1;
2520 }
2521 
2522 /** Run the run_main_loop_once() function until it declares itself done,
2523  * and return its final return value.
2524  *
2525  * Shadow won't invoke this function, so don't fill it up with things.
2526  */
2527 STATIC int
2529 {
2530  int loop_result = 1;
2531 
2534 
2535  do {
2536  loop_result = run_main_loop_once();
2537  } while (loop_result == 1);
2538 
2540  return main_loop_exit_value;
2541  else
2542  return loop_result;
2543 }
2544 
2545 /** Returns Tor's uptime. */
2546 MOCK_IMPL(long,
2547 get_uptime,(void))
2548 {
2549  return stats_n_seconds_working;
2550 }
2551 
2552 /** Reset Tor's uptime. */
2553 MOCK_IMPL(void,
2555 {
2557 }
2558 
2559 void
2560 tor_mainloop_free_all(void)
2561 {
2562  smartlist_free(connection_array);
2563  smartlist_free(closeable_connection_lst);
2564  smartlist_free(active_linked_connection_lst);
2565  teardown_periodic_events();
2566  tor_event_free(shutdown_did_not_work_event);
2567  tor_event_free(initialize_periodic_events_event);
2568  mainloop_event_free(directory_all_unreachable_cb_event);
2569  mainloop_event_free(schedule_active_linked_connections_event);
2570  mainloop_event_free(postloop_cleanup_ev);
2571  mainloop_event_free(handle_deferred_signewnym_ev);
2572  mainloop_event_free(scheduled_shutdown_ev);
2573  mainloop_event_free(rescan_periodic_events_ev);
2574 
2575 #ifdef HAVE_SYSTEMD_209
2576  periodic_timer_free(systemd_watchdog_timer);
2577 #endif
2578 
2580 
2581  memset(&global_bucket, 0, sizeof(global_bucket));
2582  memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
2586  newnym_epoch = 0;
2587  called_loop_once = 0;
2591  quiet_level = 0;
2592  should_init_bridge_stats = 1;
2593  heartbeat_callback_first_time = 1;
2594  current_second = 0;
2595  memset(&current_second_last_changed, 0,
2596  sizeof(current_second_last_changed));
2597 }
#define fmt_and_decorate_addr(a)
Definition: address.h:243
void addressmap_clear_transient(void)
Definition: addressmap.c:311
Header for addressmap.c.
void update_approx_time(time_t now)
Definition: approx_time.c:41
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:76
Header file for directory authority mode.
Header for backtrace.c.
void fetch_bridge_descriptors(const or_options_t *options, time_t now)
Definition: bridges.c:737
Header file for circuitbuild.c.
size_t buf_move_all(buf_t *buf_out, buf_t *buf_in)
Definition: buffers.c:691
size_t buf_datalen(const buf_t *buf)
Definition: buffers.c:394
Header file for buffers.c.
int buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz)
Definition: buffers_net.c:224
Header file for buffers_net.c.
int buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen)
Definition: buffers_tls.c:138
Header for buffers_tls.c.
Fixed-size cell structure.
int channel_is_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2887
void channel_run_cleanup(void)
Definition: channel.c:2137
void channel_update_bad_for_new_circs(const char *digest, int force)
Definition: channel.c:3460
void channel_listener_run_cleanup(void)
Definition: channel.c:2163
unsigned int channel_num_circuits(channel_t *chan)
Definition: channel.c:3338
Header file for channel.c.
channelpadding_decision_t channelpadding_decide_to_pad_channel(channel_t *chan)
Header file for channeltls.c.
void circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
void circuit_upgrade_circuits_from_guard_wait(void)
Header file for circuitbuild.c.
void circuit_close_all_marked(void)
Definition: circuitlist.c:673
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2077
void circuit_mark_all_unused_circs(void)
Definition: circuitlist.c:2058
Header file for circuitlist.c.
void circuit_expire_waiting_for_better_guard(void)
Definition: circuituse.c:811
void circuit_expire_old_circs_as_needed(time_t now)
Definition: circuituse.c:1298
void reset_bandwidth_test(void)
Definition: circuituse.c:1553
void circuit_expire_building(void)
Definition: circuituse.c:427
void circuit_build_needed_circs(time_t now)
Definition: circuituse.c:1275
Header file for circuituse.c.
#define ABS(x)
Definition: cmp.h:40
bool tor_libevent_is_initialized(void)
struct event_base * tor_libevent_get_base(void)
mainloop_event_t * mainloop_event_postloop_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
const char * tor_libevent_get_method(void)
periodic_timer_t * periodic_timer_new(struct event_base *base, const struct timeval *tv, void(*cb)(periodic_timer_t *timer, void *data), void *data)
void tor_libevent_exit_loop_after_callback(struct event_base *base)
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
int tor_libevent_run_event_loop(struct event_base *base, int once)
int mainloop_event_schedule(mainloop_event_t *event, const struct timeval *tv)
void mainloop_event_activate(mainloop_event_t *event)
Header for compat_libevent.c.
static int32_t monotime_coarse_diff_msec32(const monotime_coarse_t *start, const monotime_coarse_t *end)
Definition: compat_time.h:338
int quiet
Definition: config.c:2451
const or_options_t * get_options(void)
Definition: config.c:926
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:7489
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1117
Header file for config.c.
#define MIN_HEARTBEAT_PERIOD
Definition: config.h:25
const char * conn_state_to_string(int type, int state)
Definition: connection.c:305
int connection_wants_to_flush(connection_t *conn)
Definition: connection.c:4338
int connection_is_moribund(connection_t *conn)
Definition: connection.c:5510
void connection_consider_empty_write_buckets(connection_t *conn)
Definition: connection.c:3802
void connection_close_immediate(connection_t *conn)
Definition: connection.c:1056
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5660
ssize_t connection_bucket_write_limit(connection_t *conn, time_t now)
Definition: connection.c:3541
int retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:3254
int connection_state_is_open(connection_t *conn)
Definition: connection.c:5045
const char * conn_type_to_string(int type)
Definition: connection.c:271
connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4916
void log_failed_proxy_connection(connection_t *conn)
Definition: connection.c:5866
void connection_write_bw_exhausted(connection_t *conn, bool is_global_bw)
Definition: connection.c:3759
void connection_about_to_close_connection(connection_t *conn)
Definition: connection.c:1025
connection_t * connection_get_by_type_nonlinked(int type)
Definition: connection.c:4926
void connection_expire_held_open(void)
Definition: connection.c:1176
Header file for connection.c.
#define CONN_TYPE_OR
Definition: connection.h:44
#define CONN_TYPE_AP
Definition: connection.h:51
#define CONN_TYPE_DIR
Definition: connection.h:55
#define CONN_TYPE_AP_DNS_LISTENER
Definition: connection.h:68
#define CONN_TYPE_EXIT
Definition: connection.h:46
void connection_ap_expire_beginning(void)
int connection_edge_end_errno(edge_connection_t *conn)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
entry_connection_t * TO_ENTRY_CONN(connection_t *c)
Header file for connection_edge.c.
#define AP_CONN_STATE_CIRCUIT_WAIT
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
void connection_or_clear_identity(or_connection_t *conn)
void connection_or_connect_failed(or_connection_t *conn, int reason, const char *msg)
or_connection_t * TO_OR_CONN(connection_t *c)
void connection_or_close_normally(or_connection_t *orconn, int flush)
Header file for connection_or.c.
#define CONN_IS_EDGE(x)
#define DIR_CONN_IS_SERVER(conn)
time_t conn_stats_save(time_t now)
Definition: connstats.c:260
Header for feature/stats/connstats.c.
int consdiffmgr_cleanup(void)
Definition: consdiffmgr.c:722
Header for consdiffmgr.c.
Header file for control.c.
#define LOG_FN_CONN(conn, args)
Definition: control.h:33
int control_event_conn_bandwidth(connection_t *conn)
int control_event_signal(uintptr_t signal_num)
int control_event_general_error(const char *format,...)
void control_per_second_events(void)
int control_any_per_second_event_enabled(void)
Header file for control_events.c.
Header file for cpuworker.c.
int crypto_seed_rng(void)
Definition: crypto_rand.c:454
Common functions for using (pseudo-)random number generators.
int connection_dir_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:2842
int dirclient_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
int dirclient_fetches_from_authorities(const or_options_t *options)
Header for feature/dirclient/dirclient_modes.c.
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:88
Header file for directory.c.
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:36
void dns_reset_correctness_checks(void)
Definition: dns.c:2143
void assert_connection_edge_not_dns_pending(edge_connection_t *conn)
Definition: dns.c:957
Header file for dns.c.
void dnsserv_close_listener(connection_t *conn)
Definition: dnsserv.c:410
Header file for dnsserv.c.
Entry connection structure.
void purge_vanguards_lite(void)
Definition: entrynodes.c:4196
void maintain_layer2_guards(void)
Definition: entrynodes.c:4093
int guards_update_all(void)
Definition: entrynodes.c:3766
Header file for circuitbuild.c.
Header file for geoip_stats.c.
time_t geoip_entry_stats_write(time_t now)
Definition: geoip_stats.c:1373
time_t geoip_dirreq_stats_write(time_t now)
Definition: geoip_stats.c:1031
void geoip_bridge_stats_init(time_t now)
Definition: geoip_stats.c:1066
int should_record_bridge_info(const or_options_t *options)
Definition: geoip_stats.c:112
time_t geoip_bridge_stats_write(time_t now)
Definition: geoip_stats.c:1238
void consider_hibernation(time_t now)
Definition: hibernate.c:1098
int accounting_is_enabled(const or_options_t *options)
Definition: hibernate.c:305
void accounting_run_housekeeping(time_t now)
Definition: hibernate.c:585
int we_are_hibernating(void)
Definition: hibernate.c:937
Header file for hibernate.c.
void hs_cache_client_intro_state_clean(time_t now)
Definition: hs_cache.c:1001
void hs_cache_clean_as_client(time_t now)
Definition: hs_cache.c:942
void hs_cache_clean_as_dir(time_t now)
Definition: hs_cache.c:339
Header file for hs_cache.c.
void hs_client_purge_state(void)
Definition: hs_client.c:2636
Header file containing client data for the HS subsystem.
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3949
void hs_service_run_scheduled_events(time_t now)
Definition: hs_service.c:4380
Header file containing service data for the HS subsystem.
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:590
#define log_fn(severity, domain, args,...)
Definition: log.h:283
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:288
#define LD_PROTOCOL
Definition: log.h:72
#define LOG_DEBUG
Definition: log.h:42
#define LD_OR
Definition: log.h:92
#define LD_BUG
Definition: log.h:86
#define LD_NET
Definition: log.h:66
#define LD_GENERAL
Definition: log.h:62
#define LD_DIR
Definition: log.h:88
#define LOG_NOTICE
Definition: log.h:50
#define LD_CONTROL
Definition: log.h:80
#define LOG_WARN
Definition: log.h:53
#define LOG_INFO
Definition: log.h:45
static monotime_coarse_t current_second_last_changed
Definition: mainloop.c:2208
static struct event * initialize_periodic_events_event
Definition: mainloop.c:1501
void stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
Definition: mainloop.c:475
#define MAX_SIGNEWNYM_RATE
Definition: mainloop.c:153
static int main_loop_should_exit
Definition: mainloop.c:178
#define LAZY_DESCRIPTOR_RETRY_INTERVAL
Definition: mainloop.c:197
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:485
void dns_servers_relaunch_checks(void)
Definition: mainloop.c:2339
STATIC int check_network_participation_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1835
static int add_entropy_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1817
static int periodic_events_initialized
Definition: mainloop.c:1347
static int rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2050
static time_t time_of_last_signewnym
Definition: mainloop.c:155
static void conn_read_callback(evutil_socket_t fd, short event, void *_conn)
Definition: mainloop.c:870
static int clean_caches_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2034
static int main_loop_exit_value
Definition: mainloop.c:182
static mainloop_event_t * schedule_active_linked_connections_event
Definition: mainloop.c:370
static mainloop_event_t * scheduled_shutdown_ev
Definition: mainloop.c:1646
static void increment_main_loop_success_count(void)
Definition: mainloop.c:519
static void rescan_periodic_events_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1564
int connection_add_impl(connection_t *conn, int is_connecting)
Definition: mainloop.c:244
static void scheduled_shutdown_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1650
void note_that_we_maybe_cant_complete_circuits(void)
Definition: mainloop.c:234
void connection_stop_reading(connection_t *conn)
Definition: mainloop.c:601
static smartlist_t * active_linked_connection_lst
Definition: mainloop.c:170
void connection_stop_reading_from_linked_conn(connection_t *conn)
Definition: mainloop.c:818
int connection_in_array(connection_t *conn)
Definition: mainloop.c:434
static struct event * shutdown_did_not_work_event
Definition: mainloop.c:726
int have_completed_a_circuit(void)
Definition: mainloop.c:218
void ip_address_changed(int on_client_conn)
Definition: mainloop.c:2308
static int retry_listeners_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2112
void reset_uptime(void)
Definition: mainloop.c:2554
void note_that_we_completed_a_circuit(void)
Definition: mainloop.c:226
static int write_stats_file_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1939
void connection_unregister_events(connection_t *conn)
Definition: mainloop.c:275
void directory_all_unreachable(time_t now)
Definition: mainloop.c:1096
int connection_remove(connection_t *conn)
Definition: mainloop.c:289
void add_connection_to_closeable_list(connection_t *conn)
Definition: mainloop.c:416
STATIC void close_closeable_connections(void)
Definition: mainloop.c:836
void reschedule_directory_downloads(void)
Definition: mainloop.c:1607
uint64_t get_bytes_read(void)
Definition: mainloop.c:455
void initialize_periodic_events(void)
Definition: mainloop.c:1523
void mainloop_schedule_shutdown(int delay_sec)
Definition: mainloop.c:1660
int connection_is_on_closeable_list(connection_t *conn)
Definition: mainloop.c:427
static void connection_unlink(connection_t *conn)
Definition: mainloop.c:332
void connection_start_reading(connection_t *conn)
Definition: mainloop.c:623
static void increment_main_loop_idle_count(void)
Definition: mainloop.c:547
static int manage_vglite_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1673
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL
Definition: mainloop.c:194
void update_current_time(time_t now)
Definition: mainloop.c:2216
void do_signewnym(time_t now)
Definition: mainloop.c:1316
static int check_expired_networkstatus_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1892
static int launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1765
void initialize_mainloop_events(void)
Definition: mainloop.c:2351
static int fetch_networkstatus_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2086
static uint64_t stats_n_bytes_written
Definition: mainloop.c:140
static uint64_t stats_n_bytes_read
Definition: mainloop.c:138
static int second_elapsed_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1688
int do_main_loop(void)
Definition: mainloop.c:2365
int connection_is_writing(connection_t *conn)
Definition: mainloop.c:653
static void signewnym_impl(time_t now)
Definition: mainloop.c:1283
void schedule_rescan_periodic_events(void)
Definition: mainloop.c:1575
void connection_start_writing(connection_t *conn)
Definition: mainloop.c:686
static void run_connection_housekeeping(int i, time_t now)
Definition: mainloop.c:1155
uint64_t get_main_loop_error_count(void)
Definition: mainloop.c:540
static int connection_check_event(connection_t *conn, struct event *ev)
Definition: mainloop.c:563
static void shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg) ATTR_NORETURN
Definition: mainloop.c:734
static void postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1619
static int record_bridge_stats_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2006
static int can_complete_circuits
Definition: mainloop.c:190
static long stats_n_seconds_working
Definition: mainloop.c:144
static int signewnym_is_pending
Definition: mainloop.c:157
uint64_t get_main_loop_idle_count(void)
Definition: mainloop.c:554
int connection_is_reading(connection_t *conn)
Definition: mainloop.c:500
void reschedule_or_state_save(void)
Definition: mainloop.c:1926
static void schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:379
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:763
static ratelim_t libevent_error_ratelim
Definition: mainloop.c:2432
static int conn_close_if_marked(int i)
Definition: mainloop.c:956
void tor_init_connection_lists(void)
Definition: mainloop.c:404
smartlist_t * get_connection_array(void)
Definition: mainloop.c:443
static time_t current_second
Definition: mainloop.c:2206
static void conn_write_callback(evutil_socket_t fd, short event, void *_conn)
Definition: mainloop.c:912
void reset_all_main_loop_timers(void)
Definition: mainloop.c:1456
static unsigned newnym_epoch
Definition: mainloop.c:161
STATIC smartlist_t * connection_array
Definition: mainloop.c:164
static smartlist_t * closeable_connection_lst
Definition: mainloop.c:167
static void handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1306
STATIC int get_my_roles(const or_options_t *options)
Definition: mainloop.c:1464
static mainloop_event_t * postloop_cleanup_ev
Definition: mainloop.c:1630
static int connection_should_read_from_linked_conn(connection_t *conn)
Definition: mainloop.c:714
int connection_count_moribund(void)
Definition: mainloop.c:852
uint64_t get_main_loop_success_count(void)
Definition: mainloop.c:526
static void connection_start_reading_from_linked_conn(connection_t *conn)
Definition: mainloop.c:800
void directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
Definition: mainloop.c:1114
void connection_stop_writing(connection_t *conn)
Definition: mainloop.c:663
uint64_t get_bytes_written(void)
Definition: mainloop.c:465
static int save_state_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1911
static int run_main_loop_once(void)
Definition: mainloop.c:2440
static void increment_main_loop_error_count(void)
Definition: mainloop.c:533
static int heartbeat_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2132
static uint64_t stats_n_main_loop_successes
Definition: mainloop.c:146
void reset_main_loop_counters(void)
Definition: mainloop.c:510
static uint64_t stats_n_main_loop_idle
Definition: mainloop.c:150
int tor_event_loop_shutdown_is_pending(void)
Definition: mainloop.c:791
static int rotate_x509_certificate_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1783
static int called_loop_once
Definition: mainloop.c:174
long get_uptime(void)
Definition: mainloop.c:2547
time_t time_of_process_start
Definition: mainloop.c:142
static void directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1066
void mainloop_schedule_postloop_cleanup(void)
Definition: mainloop.c:1635
static void initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
Definition: mainloop.c:1509
void rescan_periodic_events(const or_options_t *options)
Definition: mainloop.c:1587
unsigned get_signewnym_epoch(void)
Definition: mainloop.c:1340
static int prune_old_routers_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2064
static uint64_t stats_n_main_loop_errors
Definition: mainloop.c:148
STATIC int run_main_loop_until_done(void)
Definition: mainloop.c:2528
static mainloop_event_t * handle_deferred_signewnym_ev
Definition: mainloop.c:159
Header file for mainloop.c.
watchable_events_t
Definition: mainloop.h:35
@ WRITE_EVENT
Definition: mainloop.h:38
@ READ_EVENT
Definition: mainloop.h:37
int usable_consensus_flavor(void)
Definition: microdesc.c:1086
int microdesc_cache_rebuild(microdesc_cache_t *cache, int force)
Definition: microdesc.c:705
Header file for microdesc.c.
int net_is_disabled(void)
Definition: netstatus.c:25
void netstatus_note_clock_jumped(time_t seconds_diff)
Definition: netstatus.c:168
void set_network_participation(bool participation)
Definition: netstatus.c:101
time_t get_last_user_activity_time(void)
Definition: netstatus.c:91
void note_user_activity(time_t now)
Definition: netstatus.c:63
bool is_participating_on_network(void)
Definition: netstatus.c:110
Header for netstatus.c.
#define SOCKET_OK(s)
Definition: nettypes.h:39
void update_networkstatus_downloads(time_t now)
int networkstatus_consensus_reasonably_live(const networkstatus_t *consensus, time_t now)
networkstatus_t * networkstatus_get_reasonably_live_consensus(time_t now, int flavor)
networkstatus_t * networkstatus_get_latest_consensus(void)
int networkstatus_consensus_is_bootstrapping(time_t now)
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
Header file for networkstatus.c.
Networkstatus consensus/vote structure.
void router_dir_info_changed(void)
Definition: nodelist.c:2470
const char * get_dir_info_status_string(void)
Definition: nodelist.c:2480
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2427
Header file for nodelist.c.
Header file for ntmain.c.
Master header file for Tor-specific functionality.
#define MAX_SSL_KEY_LIFETIME_INTERNAL
Definition: or.h:154
#define END_STREAM_REASON_NET_UNREACHABLE
Definition: or.h:257
OR connection structure.
The or_state_t structure, which represents Tor's state file.
#define OR_CONN_STATE_CONNECTING
Definition: orconn_event.h:31
#define OR_CONN_STATE_OPEN
Definition: orconn_event.h:53
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: parse_int.c:59
void periodic_events_rescan_by_roles(int roles, bool net_disabled)
Definition: periodic.c:291
void periodic_events_connect_all(void)
Definition: periodic.c:234
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:219
int safe_timer_diff(time_t now, time_t next)
Definition: periodic.c:351
void periodic_events_disconnect_all(void)
Definition: periodic.c:331
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:106
void periodic_events_reset_all(void)
Definition: periodic.c:254
Header for periodic.c.
int any_predicted_circuits(time_t now)
Header file for predict_ports.c.
int proxy_mode(const or_options_t *options)
Definition: proxymode.c:21
Header file for proxymode.c.
quiet_level_t quiet_level
Definition: quiet_level.c:20
void rep_hist_reset_padding_counts(void)
Definition: rephist.c:2867
time_t rep_hist_desc_stats_write(time_t now)
Definition: rephist.c:2181
void rep_history_clean(time_t before)
Definition: rephist.c:982
time_t rep_hist_hs_stats_write(time_t now, bool is_v3)
Definition: rephist.c:2741
time_t rep_hist_buffer_stats_write(time_t now)
Definition: rephist.c:2044
time_t rep_hist_exit_stats_write(time_t now)
Definition: rephist.c:1590
Header file for rephist.c.
#define REPHIST_CELL_PADDING_COUNTS_INTERVAL
Definition: rephist.h:162
int router_initialize_tls_context(void)
Definition: router.c:813
void mark_my_descriptor_dirty(const char *reason)
Definition: router.c:2567
Router descriptor structure.
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
Header for routerkeys.c.
void update_extrainfo_downloads(time_t now)
Definition: routerlist.c:2816
void routerlist_remove_old_routers(void)
Definition: routerlist.c:1897
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2331
Header file for routerlist.c.
int dir_server_mode(const or_options_t *options)
Definition: routermode.c:23
int server_mode(const or_options_t *options)
Definition: routermode.c:34
Header file for routermode.c.
void router_do_reachability_checks(void)
Definition: selftest.c:292
void router_reset_reachability(void)
Definition: selftest.c:68
Header file for selftest.c.
smartlist_t * smartlist_new(void)
int smartlist_contains(const smartlist_t *sl, const void *element)
void smartlist_add(smartlist_t *sl, void *element)
void smartlist_remove(smartlist_t *sl, const void *element)
void smartlist_del(smartlist_t *sl, int idx)
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
#define SMARTLIST_FOREACH(sl, type, var, cmd)
Client request structure.
or_state_t * get_or_state(void)
Definition: statefile.c:220
int or_state_save(time_t now)
Definition: statefile.c:562
Header for statefile.c.
int log_heartbeat(time_t now)
Definition: status.c:183
Header for status.c.
Definition: cell_st.h:17
uint8_t command
Definition: cell_st.h:19
time_t timestamp_last_had_circuits
Definition: channel.h:448
uint64_t global_identifier
Definition: channel.h:197
time_t timestamp_last_read_allowed
unsigned int proxy_state
Definition: connection_st.h:96
uint8_t state
Definition: connection_st.h:49
unsigned int writing_to_linked_conn
Definition: connection_st.h:83
struct buf_t * inbuf
struct event * write_event
struct connection_t * linked_conn
unsigned int hold_open_until_flushed
Definition: connection_st.h:61
unsigned int reading_from_linked_conn
Definition: connection_st.h:81
unsigned int type
Definition: connection_st.h:50
struct buf_t * outbuf
unsigned int linked
Definition: connection_st.h:78
uint16_t marked_for_close
uint16_t port
const char * marked_for_close_file
unsigned int purpose
Definition: connection_st.h:51
tor_socket_t s
unsigned int active_on_link
Definition: connection_st.h:86
struct event * read_event
time_t timestamp_last_write_allowed
tor_addr_t addr
unsigned int edge_has_sent_end
socks_request_t * socks_request
channel_tls_t * chan
time_t timestamp_lastempty
unsigned int is_canonical
int ExitPortStatistics
int TestingDirConnectionMaxStall
uint64_t OwningControllerFD
int DormantTimeoutEnabled
int DormantTimeoutDisabledByIdleStreams
int HiddenServiceStatistics
int DormantClientTimeout
int ConnDirectionStatistics
int BridgeAuthoritativeDir
time_t next_write
Definition: or_state_st.h:26
const char * name
Definition: periodic.h:68
int n_calls_since_last_time
Definition: ratelim.h:51
char address[MAX_SOCKS_ADDR_LEN]
#define STATIC
Definition: testsupport.h:32
#define MOCK_IMPL(rv, funcname, arglist)
Definition: testsupport.h:133
Headers for transports.c.
#define tor_assert(expr)
Definition: util_bug.h:102
#define tor_fragile_assert()
Definition: util_bug.h:270
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96