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