Tor  0.4.4.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-2020, 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 
970  log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
971  conn->s);
972 
973  /* If the connection we are about to close was trying to connect to
974  a proxy server and failed, the client won't be able to use that
975  proxy. We should warn the user about this. */
976  if (conn->proxy_state == PROXY_INFANT)
978 
979  if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
981  /* s == -1 means it's an incomplete edge connection, or that the socket
982  * has already been closed as unflushable. */
983  ssize_t sz = connection_bucket_write_limit(conn, now);
984  if (!conn->hold_open_until_flushed)
985  log_info(LD_NET,
986  "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
987  "to flush %d bytes. (Marked at %s:%d)",
989  (int)conn->s, conn_type_to_string(conn->type), conn->state,
990  (int)conn->outbuf_flushlen,
992  if (conn->linked_conn) {
993  retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
994  &conn->outbuf_flushlen);
995  if (retval >= 0) {
996  /* The linked conn will notice that it has data when it notices that
997  * we're gone. */
999  }
1000  log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
1001  "%d left; flushlen %d; wants-to-flush==%d", retval,
1002  (int)connection_get_outbuf_len(conn),
1003  (int)conn->outbuf_flushlen,
1005  } else if (connection_speaks_cells(conn)) {
1006  if (conn->state == OR_CONN_STATE_OPEN) {
1007  retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
1008  &conn->outbuf_flushlen);
1009  } else
1010  retval = -1; /* never flush non-open broken tls connections */
1011  } else {
1012  retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
1013  &conn->outbuf_flushlen);
1014  }
1015  if (retval >= 0 && /* Technically, we could survive things like
1016  TLS_WANT_WRITE here. But don't bother for now. */
1018  if (retval > 0) {
1019  LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
1020  "Holding conn (fd %d) open for more flushing.",
1021  (int)conn->s));
1022  conn->timestamp_last_write_allowed = now; /* reset so we can flush
1023  * more */
1024  } else if (sz == 0) {
1025  /* Also, retval==0. If we get here, we didn't want to write anything
1026  * (because of rate-limiting) and we didn't. */
1027 
1028  /* Connection must flush before closing, but it's being rate-limited.
1029  * Let's remove from Libevent, and mark it as blocked on bandwidth
1030  * so it will be re-added on next token bucket refill. Prevents
1031  * busy Libevent loops where we keep ending up here and returning
1032  * 0 until we are no longer blocked on bandwidth.
1033  */
1035  /* Make sure that consider_empty_buckets really disabled the
1036  * connection: */
1037  if (BUG(connection_is_writing(conn))) {
1038  connection_write_bw_exhausted(conn, true);
1039  }
1040 
1041  /* The connection is being held due to write rate limit and thus will
1042  * flush its data later. We need to stop reading because this
1043  * connection is about to be closed once flushed. It should not
1044  * process anything more coming in at this stage. */
1046  }
1047  return 0;
1048  }
1049  if (connection_wants_to_flush(conn)) {
1050  log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1051  "bytes to address %s. If this happens a lot, either "
1052  "something is wrong with your network connection, or "
1053  "something is wrong with theirs. "
1054  "(fd %d, type %s, state %d, marked at %s:%d).",
1055  (int)connection_get_outbuf_len(conn),
1057  (int)conn->s, conn_type_to_string(conn->type), conn->state,
1058  conn->marked_for_close_file,
1059  conn->marked_for_close);
1060  }
1061  }
1062 
1063  connection_unlink(conn); /* unlink, remove, free */
1064  return 1;
1065 }
1066 
1067 /** Implementation for directory_all_unreachable. This is done in a callback,
1068  * since otherwise it would complicate Tor's control-flow graph beyond all
1069  * reason.
1070  */
1071 static void
1073 {
1074  (void)event;
1075  (void)arg;
1076 
1077  connection_t *conn;
1078 
1081  entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1082  log_notice(LD_NET,
1083  "Is your network connection down? "
1084  "Failing connection to '%s:%d'.",
1085  safe_str_client(entry_conn->socks_request->address),
1086  entry_conn->socks_request->port);
1087  connection_mark_unattached_ap(entry_conn,
1089  }
1090  control_event_general_error("DIR_ALL_UNREACHABLE");
1091 }
1092 
1093 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1094 
1095 /** We've just tried every dirserver we know about, and none of
1096  * them were reachable. Assume the network is down. Change state
1097  * so next time an application connection arrives we'll delay it
1098  * and try another directory fetch. Kill off all the circuit_wait
1099  * streams that are waiting now, since they will all timeout anyway.
1100  */
1101 void
1103 {
1104  (void)now;
1105 
1106  reset_uptime(); /* reset it */
1107 
1108  if (!directory_all_unreachable_cb_event) {
1109  directory_all_unreachable_cb_event =
1111  tor_assert(directory_all_unreachable_cb_event);
1112  }
1113 
1114  mainloop_event_activate(directory_all_unreachable_cb_event);
1115 }
1116 
1117 /** This function is called whenever we successfully pull down some new
1118  * network statuses or server descriptors. */
1119 void
1120 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1121 {
1122  const or_options_t *options = get_options();
1123 
1124  /* if we have enough dir info, then update our guard status with
1125  * whatever we just learned. */
1126  int invalidate_circs = guards_update_all();
1127 
1128  if (invalidate_circs) {
1131  }
1132 
1134  int quiet = suppress_logs || from_cache ||
1137  "I learned some more directory information, but not enough to "
1138  "build a circuit: %s", get_dir_info_status_string());
1140  return;
1141  } else {
1142  if (dirclient_fetches_from_authorities(options)) {
1144  }
1145 
1146  /* Don't even bother trying to get extrainfo until the rest of our
1147  * directory info is up-to-date */
1148  if (options->DownloadExtraInfo)
1150  }
1151 
1152  if (server_mode(options) && !net_is_disabled() && !from_cache &&
1155 }
1156 
1157 /** Perform regular maintenance tasks for a single connection. This
1158  * function gets run once per second per connection by run_scheduled_events.
1159  */
1160 static void
1162 {
1163  cell_t cell;
1164  connection_t *conn = smartlist_get(connection_array, i);
1165  const or_options_t *options = get_options();
1166  or_connection_t *or_conn;
1167  channel_t *chan = NULL;
1168  int have_any_circuits;
1169  int past_keepalive =
1170  now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1171 
1172  if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1173  conn->type == CONN_TYPE_OR)
1174  TO_OR_CONN(conn)->timestamp_lastempty = now;
1175 
1176  if (conn->marked_for_close) {
1177  /* nothing to do here */
1178  return;
1179  }
1180 
1181  /* Expire any directory connections that haven't been active (sent
1182  * if a server or received if a client) for 5 min */
1183  if (conn->type == CONN_TYPE_DIR &&
1184  ((DIR_CONN_IS_SERVER(conn) &&
1186  + options->TestingDirConnectionMaxStall < now) ||
1187  (!DIR_CONN_IS_SERVER(conn) &&
1189  + options->TestingDirConnectionMaxStall < now))) {
1190  log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1191  (int)conn->s, conn->purpose);
1192  /* This check is temporary; it's to let us know whether we should consider
1193  * parsing partial serverdesc responses. */
1194  if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1195  connection_get_inbuf_len(conn) >= 1024) {
1196  log_info(LD_DIR,"Trying to extract information from wedged server desc "
1197  "download.");
1199  } else {
1200  connection_mark_for_close(conn);
1201  }
1202  return;
1203  }
1204 
1205  if (!connection_speaks_cells(conn))
1206  return; /* we're all done here, the rest is just for OR conns */
1207 
1208  /* If we haven't flushed to an OR connection for a while, then either nuke
1209  the connection or send a keepalive, depending. */
1210 
1211  or_conn = TO_OR_CONN(conn);
1212  tor_assert(conn->outbuf);
1213 
1214  chan = TLS_CHAN_TO_BASE(or_conn->chan);
1215  tor_assert(chan);
1216 
1217  if (channel_num_circuits(chan) != 0) {
1218  have_any_circuits = 1;
1219  chan->timestamp_last_had_circuits = now;
1220  } else {
1221  have_any_circuits = 0;
1222  }
1223 
1224  if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1225  ! have_any_circuits) {
1226  /* It's bad for new circuits, and has no unmarked circuits on it:
1227  * mark it now. */
1228  log_info(LD_OR,
1229  "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1230  (int)conn->s, conn->address, conn->port);
1231  if (conn->state == OR_CONN_STATE_CONNECTING)
1233  END_OR_CONN_REASON_TIMEOUT,
1234  "Tor gave up on the connection");
1236  } else if (!connection_state_is_open(conn)) {
1237  if (past_keepalive) {
1238  /* We never managed to actually get this connection open and happy. */
1239  log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1240  (int)conn->s,conn->address, conn->port);
1242  }
1243  } else if (we_are_hibernating() &&
1244  ! have_any_circuits &&
1245  !connection_get_outbuf_len(conn)) {
1246  /* We're hibernating or shutting down, there's no circuits, and nothing to
1247  * flush.*/
1248  log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1249  "[Hibernating or exiting].",
1250  (int)conn->s,conn->address, conn->port);
1252  } else if (!have_any_circuits &&
1253  now - or_conn->idle_timeout >=
1255  log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
1256  "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1257  (chan->global_identifier),
1258  (int)conn->s, conn->address, conn->port,
1259  (int)(now - chan->timestamp_last_had_circuits),
1260  or_conn->idle_timeout,
1261  or_conn->is_canonical ? "" : "non");
1263  } else if (
1264  now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1265  now >=
1266  conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1267  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1268  "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1269  "flush; %d seconds since last write)",
1270  (int)conn->s, conn->address, conn->port,
1271  (int)connection_get_outbuf_len(conn),
1272  (int)(now-conn->timestamp_last_write_allowed));
1274  } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1275  /* send a padding cell */
1276  log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1277  conn->address, conn->port);
1278  memset(&cell,0,sizeof(cell_t));
1279  cell.command = CELL_PADDING;
1280  connection_or_write_cell_to_buf(&cell, or_conn);
1281  } else {
1283  }
1284 }
1285 
1286 /** Honor a NEWNYM request: make future requests unlinkable to past
1287  * requests. */
1288 static void
1289 signewnym_impl(time_t now)
1290 {
1291  const or_options_t *options = get_options();
1292  if (!proxy_mode(options)) {
1293  log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1294  "is disabled.");
1295  return;
1296  }
1297 
1301  time_of_last_signewnym = now;
1303 
1304  ++newnym_epoch;
1305 
1306  control_event_signal(SIGNEWNYM);
1307 }
1308 
1309 /** Callback: run a deferred signewnym. */
1310 static void
1312 {
1313  (void)event;
1314  (void)arg;
1315  log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1316  do_signewnym(time(NULL));
1317 }
1318 
1319 /** Either perform a signewnym or schedule one, depending on rate limiting. */
1320 void
1321 do_signewnym(time_t now)
1322 {
1324  const time_t delay_sec =
1326  if (! signewnym_is_pending) {
1331  }
1332  const struct timeval delay_tv = { delay_sec, 0 };
1334  }
1335  log_notice(LD_CONTROL,
1336  "Rate limiting NEWNYM request: delaying by %d second(s)",
1337  (int)(delay_sec));
1338  } else {
1339  signewnym_impl(now);
1340  }
1341 }
1342 
1343 /** Return the number of times that signewnym has been called. */
1344 unsigned
1346 {
1347  return newnym_epoch;
1348 }
1349 
1350 /** True iff we have initialized all the members of <b>periodic_events</b>.
1351  * Used to prevent double-initialization. */
1353 
1354 /* Declare all the timer callback functions... */
1355 #ifndef COCCI
1356 #undef CALLBACK
1357 #define CALLBACK(name) \
1358  static int name ## _callback(time_t, const or_options_t *)
1359 
1360 CALLBACK(add_entropy);
1361 CALLBACK(check_expired_networkstatus);
1362 CALLBACK(clean_caches);
1363 CALLBACK(clean_consdiffmgr);
1364 CALLBACK(fetch_networkstatus);
1365 CALLBACK(heartbeat);
1366 CALLBACK(hs_service);
1367 CALLBACK(launch_descriptor_fetches);
1368 CALLBACK(prune_old_routers);
1369 CALLBACK(record_bridge_stats);
1370 CALLBACK(rend_cache_failure_clean);
1371 CALLBACK(reset_padding_counts);
1372 CALLBACK(retry_listeners);
1373 CALLBACK(rotate_x509_certificate);
1374 CALLBACK(save_state);
1375 CALLBACK(write_stats_file);
1376 CALLBACK(control_per_second_events);
1377 CALLBACK(second_elapsed);
1378 
1379 #undef CALLBACK
1380 
1381 /* Now we declare an array of periodic_event_item_t for each periodic event */
1382 #define CALLBACK(name, r, f) \
1383  PERIODIC_EVENT(name, PERIODIC_EVENT_ROLE_ ## r, f)
1384 #define FL(name) (PERIODIC_EVENT_FLAG_ ## name)
1385 #endif /* !defined(COCCI) */
1386 
1387 STATIC periodic_event_item_t mainloop_periodic_events[] = {
1388 
1389  /* Everyone needs to run these. They need to have very long timeouts for
1390  * that to be safe. */
1391  CALLBACK(add_entropy, ALL, 0),
1392  CALLBACK(heartbeat, ALL, 0),
1393  CALLBACK(reset_padding_counts, ALL, 0),
1394 
1395  /* This is a legacy catch-all callback that runs once per second if
1396  * we are online and active. */
1397  CALLBACK(second_elapsed, NET_PARTICIPANT,
1398  FL(RUN_ON_DISABLE)),
1399 
1400  /* XXXX Do we have a reason to do this on a callback? Does it do any good at
1401  * all? For now, if we're dormant, we can let our listeners decay. */
1402  CALLBACK(retry_listeners, NET_PARTICIPANT, FL(NEED_NET)),
1403 
1404  /* We need to do these if we're participating in the Tor network. */
1405  CALLBACK(check_expired_networkstatus, NET_PARTICIPANT, 0),
1406  CALLBACK(fetch_networkstatus, NET_PARTICIPANT, 0),
1407  CALLBACK(launch_descriptor_fetches, NET_PARTICIPANT, FL(NEED_NET)),
1408  CALLBACK(rotate_x509_certificate, NET_PARTICIPANT, 0),
1409  CALLBACK(check_network_participation, NET_PARTICIPANT, 0),
1410 
1411  /* We need to do these if we're participating in the Tor network, and
1412  * immediately before we stop. */
1413  CALLBACK(clean_caches, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1414  CALLBACK(save_state, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1415  CALLBACK(write_stats_file, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1416  CALLBACK(prune_old_routers, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1417 
1418  /* Hidden Service service only. */
1419  CALLBACK(hs_service, HS_SERVICE, FL(NEED_NET)), // XXXX break this down more
1420 
1421  /* Bridge only. */
1422  CALLBACK(record_bridge_stats, BRIDGE, 0),
1423 
1424  /* Client only. */
1425  /* XXXX this could be restricted to CLIENT+NET_PARTICIPANT */
1426  CALLBACK(rend_cache_failure_clean, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1427 
1428  /* Directory server only. */
1429  CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
1430 
1431  /* Controller with per-second events only. */
1432  CALLBACK(control_per_second_events, CONTROLEV, 0),
1433 
1434  END_OF_PERIODIC_EVENTS
1435 };
1436 #ifndef COCCI
1437 #undef CALLBACK
1438 #undef FL
1439 #endif
1440 
1441 /* These are pointers to members of periodic_events[] that are used to
1442  * implement particular callbacks. We keep them separate here so that we
1443  * can access them by name. We also keep them inside periodic_events[]
1444  * so that we can implement "reset all timers" in a reasonable way. */
1445 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1446 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1447 static periodic_event_item_t *check_dns_honesty_event=NULL;
1448 static periodic_event_item_t *save_state_event=NULL;
1449 static periodic_event_item_t *prune_old_routers_event=NULL;
1450 
1451 /** Reset all the periodic events so we'll do all our actions again as if we
1452  * just started up.
1453  * Useful if our clock just moved back a long time from the future,
1454  * so we don't wait until that future arrives again before acting.
1455  */
1456 void
1458 {
1460 }
1461 
1462 /** Return a bitmask of the roles this tor instance is configured for using
1463  * the given options. */
1464 STATIC int
1466 {
1467  tor_assert(options);
1468 
1469  int roles = PERIODIC_EVENT_ROLE_ALL;
1470  int is_bridge = options->BridgeRelay;
1471  int is_relay = server_mode(options);
1472  int is_dirauth = authdir_mode_v3(options);
1473  int is_bridgeauth = authdir_mode_bridge(options);
1474  int is_hidden_service = !!hs_service_get_num_services() ||
1475  !!rend_num_services();
1476  int is_dirserver = dir_server_mode(options);
1477  int sending_control_events = control_any_per_second_event_enabled();
1478 
1479  /* We also consider tor to have the role of a client if the ControlPort is
1480  * set because a lot of things can be done over the control port which
1481  * requires tor to have basic functionnalities. */
1482  int is_client = options_any_client_port_set(options) ||
1483  options->ControlPort_set ||
1484  options->OwningControllerFD != UINT64_MAX;
1485 
1486  int is_net_participant = is_participating_on_network() ||
1487  is_relay || is_hidden_service;
1488 
1489  if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1490  if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1491  if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1492  if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1493  if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1494  if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1495  if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
1496  if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
1497  if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
1498 
1499  return roles;
1500 }
1501 
1502 /** Event to run initialize_periodic_events_cb */
1503 static struct event *initialize_periodic_events_event = NULL;
1504 
1505 /** Helper, run one second after setup:
1506  * Initializes all members of periodic_events and starts them running.
1507  *
1508  * (We do this one second after setup for backward-compatibility reasons;
1509  * it might not actually be necessary.) */
1510 static void
1511 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1512 {
1513  (void) fd;
1514  (void) events;
1515  (void) data;
1516 
1517  tor_event_free(initialize_periodic_events_event);
1518 
1520 }
1521 
1522 /** Set up all the members of mainloop_periodic_events[], and configure them
1523  * all to be launched from a callback. */
1524 void
1526 {
1528  return;
1529 
1531 
1532  for (int i = 0; mainloop_periodic_events[i].name; ++i) {
1533  periodic_events_register(&mainloop_periodic_events[i]);
1534  }
1535 
1536  /* Set up all periodic events. We'll launch them by roles. */
1537 
1538 #ifndef COCCI
1539 #define NAMED_CALLBACK(name) \
1540  STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
1541 #endif
1542 
1543  NAMED_CALLBACK(prune_old_routers);
1544  NAMED_CALLBACK(fetch_networkstatus);
1545  NAMED_CALLBACK(launch_descriptor_fetches);
1546  NAMED_CALLBACK(check_dns_honesty);
1547  NAMED_CALLBACK(save_state);
1548 }
1549 
1550 STATIC void
1551 teardown_periodic_events(void)
1552 {
1554  fetch_networkstatus_event = NULL;
1555  launch_descriptor_fetches_event = NULL;
1556  check_dns_honesty_event = NULL;
1557  save_state_event = NULL;
1558  prune_old_routers_event = NULL;
1560 }
1561 
1562 static mainloop_event_t *rescan_periodic_events_ev = NULL;
1563 
1564 /** Callback: rescan the periodic event list. */
1565 static void
1567 {
1568  (void)event;
1569  (void)arg;
1571 }
1572 
1573 /**
1574  * Schedule an event that will rescan which periodic events should run.
1575  **/
1576 MOCK_IMPL(void,
1578 {
1579  if (!rescan_periodic_events_ev) {
1580  rescan_periodic_events_ev =
1582  }
1583  mainloop_event_activate(rescan_periodic_events_ev);
1584 }
1585 
1586 /** Do a pass at all our periodic events, disable those we don't need anymore
1587  * and enable those we need now using the given options. */
1588 void
1590 {
1591  tor_assert(options);
1592 
1594 }
1595 
1596 /* We just got new options globally set, see if we need to enabled or disable
1597  * periodic events. */
1598 void
1599 periodic_events_on_new_options(const or_options_t *options)
1600 {
1601  rescan_periodic_events(options);
1602 }
1603 
1604 /**
1605  * Update our schedule so that we'll check whether we need to fetch directory
1606  * info immediately.
1607  */
1608 void
1610 {
1611  tor_assert(fetch_networkstatus_event);
1612  tor_assert(launch_descriptor_fetches_event);
1613 
1614  periodic_event_reschedule(fetch_networkstatus_event);
1615  periodic_event_reschedule(launch_descriptor_fetches_event);
1616 }
1617 
1618 /** Mainloop callback: clean up circuits, channels, and connections
1619  * that are pending close. */
1620 static void
1622 {
1623  (void)ev;
1624  (void)arg;
1629 }
1630 
1631 /** Event to run postloop_cleanup_cb */
1633 
1634 /** Schedule a post-loop event to clean up marked channels, connections, and
1635  * circuits. */
1636 void
1638 {
1639  if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
1640  // (It's possible that we can get here if we decide to close a connection
1641  // in the earliest stages of our configuration, before we create events.)
1642  return;
1643  }
1645 }
1646 
1647 /** Event to run 'scheduled_shutdown_cb' */
1649 
1650 /** Callback: run a scheduled shutdown */
1651 static void
1653 {
1654  (void)ev;
1655  (void)arg;
1656  log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
1658 }
1659 
1660 /** Schedule the mainloop to exit after <b>delay_sec</b> seconds. */
1661 void
1663 {
1664  const struct timeval delay_tv = { delay_sec, 0 };
1665  if (! scheduled_shutdown_ev) {
1667  }
1669 }
1670 
1671 /** Perform regular maintenance tasks. This function gets run once per
1672  * second.
1673  */
1674 static int
1675 second_elapsed_callback(time_t now, const or_options_t *options)
1676 {
1677  /* 0. See if our bandwidth limits are exhausted and we should hibernate
1678  *
1679  * Note: we have redundant mechanisms to handle the case where it's
1680  * time to wake up from hibernation; or where we have a scheduled
1681  * shutdown and it's time to run it, but this will also handle those.
1682  */
1683  consider_hibernation(now);
1684 
1685  /* Maybe enough time elapsed for us to reconsider a circuit. */
1687 
1688  if (options->UseBridges && !net_is_disabled()) {
1689  /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1690  * -- the latter is only for fetching consensus-derived directory info. */
1691  // TODO: client
1692  // Also, schedule this rather than probing 1x / sec
1693  fetch_bridge_descriptors(options, now);
1694  }
1695 
1696  if (accounting_is_enabled(options)) {
1697  // TODO: refactor or rewrite?
1699  }
1700 
1701  /* 3a. Every second, we examine pending circuits and prune the
1702  * ones which have been pending for more than a few seconds.
1703  * We do this before step 4, so it can try building more if
1704  * it's not comfortable with the number of available circuits.
1705  */
1706  /* (If our circuit build timeout can ever become lower than a second (which
1707  * it can't, currently), we should do this more often.) */
1708  // TODO: All expire stuff can become NET_PARTICIPANT, RUN_ON_DISABLE
1711 
1712  /* 3b. Also look at pending streams and prune the ones that 'began'
1713  * a long time ago but haven't gotten a 'connected' yet.
1714  * Do this before step 4, so we can put them back into pending
1715  * state to be picked up by the new circuit.
1716  */
1718 
1719  /* 3c. And expire connections that we've held open for too long.
1720  */
1722 
1723  /* 4. Every second, we try a new circuit if there are no valid
1724  * circuits. Every NewCircuitPeriod seconds, we expire circuits
1725  * that became dirty more than MaxCircuitDirtiness seconds ago,
1726  * and we make a new circ if there are no clean circuits.
1727  */
1728  const int have_dir_info = router_have_minimum_dir_info();
1729  if (have_dir_info && !net_is_disabled()) {
1731  } else {
1733  }
1734 
1735  /* 5. We do housekeeping for each connection... */
1737  int i;
1738  for (i=0;i<smartlist_len(connection_array);i++) {
1740  }
1741 
1742  /* Run again in a second. */
1743  return 1;
1744 }
1745 
1746 /**
1747  * Periodic callback: Every {LAZY,GREEDY}_DESCRIPTOR_RETRY_INTERVAL,
1748  * see about fetching descriptors, microdescriptors, and extrainfo
1749  * documents.
1750  */
1751 static int
1753 {
1754  if (should_delay_dir_fetches(options, NULL))
1755  return PERIODIC_EVENT_NO_UPDATE;
1756 
1761  else
1763 }
1764 
1765 /**
1766  * Periodic event: Rotate our X.509 certificates and TLS keys once every
1767  * MAX_SSL_KEY_LIFETIME_INTERNAL.
1768  */
1769 static int
1771 {
1772  static int first = 1;
1773  (void)now;
1774  (void)options;
1775  if (first) {
1776  first = 0;
1778  }
1779 
1780  /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1781  * TLS context. */
1782  log_info(LD_GENERAL,"Rotating tls context.");
1783  if (router_initialize_tls_context() < 0) {
1784  log_err(LD_BUG, "Error reinitializing TLS context");
1785  tor_assert_unreached();
1786  }
1787  if (generate_ed_link_cert(options, now, 1)) {
1788  log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1789  "new TLS context.");
1790  tor_assert_unreached();
1791  }
1792 
1793  /* We also make sure to rotate the TLS connections themselves if they've
1794  * been up for too long -- but that's done via is_bad_for_new_circs in
1795  * run_connection_housekeeping() above. */
1797 }
1798 
1799 /**
1800  * Periodic callback: once an hour, grab some more entropy from the
1801  * kernel and feed it to our CSPRNG.
1802  **/
1803 static int
1804 add_entropy_callback(time_t now, const or_options_t *options)
1805 {
1806  (void)now;
1807  (void)options;
1808  /* We already seeded once, so don't die on failure. */
1809  if (crypto_seed_rng() < 0) {
1810  log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1811  "seeded once, though, so we won't exit here.");
1812  }
1813 
1814  /** How often do we add more entropy to OpenSSL's RNG pool? */
1815 #define ENTROPY_INTERVAL (60*60)
1816  return ENTROPY_INTERVAL;
1817 }
1818 
1819 /** Periodic callback: if there has been no network usage in a while,
1820  * enter a dormant state. */
1821 STATIC int
1823 {
1824  /* If we're a server, we can't become dormant. */
1825  if (server_mode(options)) {
1826  goto found_activity;
1827  }
1828 
1829  /* If we're running an onion service, we can't become dormant. */
1830  /* XXXX this would be nice to change, so that we can be dormant with a
1831  * service. */
1833  goto found_activity;
1834  }
1835 
1836  /* If we have any currently open entry streams other than "linked"
1837  * connections used for directory requests, those count as user activity.
1838  */
1839  if (options->DormantTimeoutDisabledByIdleStreams) {
1841  goto found_activity;
1842  }
1843  }
1844 
1845  /* XXXX Make this configurable? */
1846 /** How often do we check whether we have had network activity? */
1847 #define CHECK_PARTICIPATION_INTERVAL (5*60)
1848 
1849  /* Become dormant if there has been no user activity in a long time.
1850  * (The funny checks below are in order to prevent overflow.) */
1851  time_t time_since_last_activity = 0;
1852  if (get_last_user_activity_time() < now)
1853  time_since_last_activity = now - get_last_user_activity_time();
1854  if (time_since_last_activity >= options->DormantClientTimeout) {
1855  log_notice(LD_GENERAL, "No user activity in a long time: becoming"
1856  " dormant.");
1858  rescan_periodic_events(options);
1859  }
1860 
1861  return CHECK_PARTICIPATION_INTERVAL;
1862 
1863  found_activity:
1864  note_user_activity(now);
1865  return CHECK_PARTICIPATION_INTERVAL;
1866 }
1867 
1868 /**
1869  * Periodic callback: If our consensus is too old, recalculate whether
1870  * we can actually use it.
1871  */
1872 static int
1874 {
1875  (void)options;
1876  /* Check whether our networkstatus has expired. */
1878  /* Use reasonably live consensuses until they are no longer reasonably live.
1879  */
1880  if (ns && !networkstatus_consensus_reasonably_live(ns, now) &&
1883  }
1884 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1885  return CHECK_EXPIRED_NS_INTERVAL;
1886 }
1887 
1888 /**
1889  * Scheduled callback: Save the state file to disk if appropriate.
1890  */
1891 static int
1892 save_state_callback(time_t now, const or_options_t *options)
1893 {
1894  (void) options;
1895  (void) or_state_save(now); // only saves if appropriate
1896  const time_t next_write = get_or_state()->next_write;
1897  if (next_write == TIME_MAX) {
1898  return 86400;
1899  }
1900  return safe_timer_diff(now, next_write);
1901 }
1902 
1903 /** Reschedule the event for saving the state file.
1904  *
1905  * Run this when the state becomes dirty. */
1906 void
1908 {
1909  if (save_state_event == NULL) {
1910  /* This can happen early on during startup. */
1911  return;
1912  }
1913  periodic_event_reschedule(save_state_event);
1914 }
1915 
1916 /**
1917  * Periodic callback: Write statistics to disk if appropriate.
1918  */
1919 static int
1920 write_stats_file_callback(time_t now, const or_options_t *options)
1921 {
1922  /* 1g. Check whether we should write statistics to disk.
1923  */
1924 #define CHECK_WRITE_STATS_INTERVAL (60*60)
1925  time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
1926  if (options->CellStatistics) {
1927  time_t next_write =
1929  if (next_write && next_write < next_time_to_write_stats_files)
1930  next_time_to_write_stats_files = next_write;
1931  }
1932  if (options->DirReqStatistics) {
1933  time_t next_write = geoip_dirreq_stats_write(now);
1934  if (next_write && next_write < next_time_to_write_stats_files)
1935  next_time_to_write_stats_files = next_write;
1936  }
1937  if (options->EntryStatistics) {
1938  time_t next_write = geoip_entry_stats_write(now);
1939  if (next_write && next_write < next_time_to_write_stats_files)
1940  next_time_to_write_stats_files = next_write;
1941  }
1942  if (options->HiddenServiceStatistics) {
1943  time_t next_write = rep_hist_hs_stats_write(now);
1944  if (next_write && next_write < next_time_to_write_stats_files)
1945  next_time_to_write_stats_files = next_write;
1946  }
1947  if (options->ExitPortStatistics) {
1948  time_t next_write = rep_hist_exit_stats_write(now);
1949  if (next_write && next_write < next_time_to_write_stats_files)
1950  next_time_to_write_stats_files = next_write;
1951  }
1952  if (options->ConnDirectionStatistics) {
1953  time_t next_write = rep_hist_conn_stats_write(now);
1954  if (next_write && next_write < next_time_to_write_stats_files)
1955  next_time_to_write_stats_files = next_write;
1956  }
1957  if (options->BridgeAuthoritativeDir) {
1958  time_t next_write = rep_hist_desc_stats_write(now);
1959  if (next_write && next_write < next_time_to_write_stats_files)
1960  next_time_to_write_stats_files = next_write;
1961  }
1962 
1963  return safe_timer_diff(now, next_time_to_write_stats_files);
1964 }
1965 
1966 static int
1967 reset_padding_counts_callback(time_t now, const or_options_t *options)
1968 {
1969  if (options->PaddingStatistics) {
1970  rep_hist_prep_published_padding_counts(now);
1971  }
1972 
1975 }
1976 
1977 static int should_init_bridge_stats = 1;
1978 
1979 /**
1980  * Periodic callback: Write bridge statistics to disk if appropriate.
1981  */
1982 static int
1983 record_bridge_stats_callback(time_t now, const or_options_t *options)
1984 {
1985  /* 1h. Check whether we should write bridge statistics to disk.
1986  */
1987  if (should_record_bridge_info(options)) {
1988  if (should_init_bridge_stats) {
1989  /* (Re-)initialize bridge statistics. */
1991  should_init_bridge_stats = 0;
1992  return WRITE_STATS_INTERVAL;
1993  } else {
1994  /* Possibly write bridge statistics to disk and ask when to write
1995  * them next time. */
1996  time_t next = geoip_bridge_stats_write(now);
1997  return safe_timer_diff(now, next);
1998  }
1999  } else if (!should_init_bridge_stats) {
2000  /* Bridge mode was turned off. Ensure that stats are re-initialized
2001  * next time bridge mode is turned on. */
2002  should_init_bridge_stats = 1;
2003  }
2004  return PERIODIC_EVENT_NO_UPDATE;
2005 }
2006 
2007 /**
2008  * Periodic callback: Clean in-memory caches every once in a while
2009  */
2010 static int
2011 clean_caches_callback(time_t now, const or_options_t *options)
2012 {
2013  /* Remove old information from rephist and the rend cache. */
2014  rep_history_clean(now - options->RephistTrackTime);
2015  rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
2017  hs_cache_clean_as_dir(now);
2018  microdesc_cache_rebuild(NULL, 0);
2019 #define CLEAN_CACHES_INTERVAL (30*60)
2020  return CLEAN_CACHES_INTERVAL;
2021 }
2022 
2023 /**
2024  * Periodic callback: Clean the cache of failed hidden service lookups
2025  * frequently.
2026  */
2027 static int
2029 {
2030  (void)options;
2031  /* We don't keep entries that are more than five minutes old so we try to
2032  * clean it as soon as we can since we want to make sure the client waits
2033  * as little as possible for reachability reasons. */
2036  return 30;
2037 }
2038 
2039 /**
2040  * Periodic callback: prune routerlist of old information about Tor network.
2041  */
2042 static int
2043 prune_old_routers_callback(time_t now, const or_options_t *options)
2044 {
2045 #define ROUTERLIST_PRUNING_INTERVAL (60*60) // 1 hour.
2046  (void)now;
2047  (void)options;
2048 
2049  if (!net_is_disabled()) {
2050  /* If any networkstatus documents are no longer recent, we need to
2051  * update all the descriptors' running status. */
2052  /* Remove dead routers. */
2053  log_debug(LD_GENERAL, "Pruning routerlist...");
2055  }
2056 
2057  return ROUTERLIST_PRUNING_INTERVAL;
2058 }
2059 
2060 /**
2061  * Periodic event: once a minute, (or every second if TestingTorNetwork, or
2062  * during client bootstrap), check whether we want to download any
2063  * networkstatus documents. */
2064 static int
2065 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2066 {
2067  /* How often do we check whether we should download network status
2068  * documents? */
2069  const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2070  now);
2071  const int prefer_mirrors = !dirclient_fetches_from_authorities(
2072  get_options());
2073  int networkstatus_dl_check_interval = 60;
2074  /* check more often when testing, or when bootstrapping from mirrors
2075  * (connection limits prevent too many connections being made) */
2076  if (options->TestingTorNetwork
2077  || (we_are_bootstrapping && prefer_mirrors)) {
2078  networkstatus_dl_check_interval = 1;
2079  }
2080 
2081  if (should_delay_dir_fetches(options, NULL))
2082  return PERIODIC_EVENT_NO_UPDATE;
2083 
2085  return networkstatus_dl_check_interval;
2086 }
2087 
2088 /**
2089  * Periodic callback: Every 60 seconds, we relaunch listeners if any died. */
2090 static int
2091 retry_listeners_callback(time_t now, const or_options_t *options)
2092 {
2093  (void)now;
2094  (void)options;
2095  if (!net_is_disabled()) {
2096  retry_all_listeners(NULL, 0);
2097  return 60;
2098  }
2099  return PERIODIC_EVENT_NO_UPDATE;
2100 }
2101 
2102 static int heartbeat_callback_first_time = 1;
2103 
2104 /**
2105  * Periodic callback: write the heartbeat message in the logs.
2106  *
2107  * If writing the heartbeat message to the logs fails for some reason, retry
2108  * again after <b>MIN_HEARTBEAT_PERIOD</b> seconds.
2109  */
2110 static int
2111 heartbeat_callback(time_t now, const or_options_t *options)
2112 {
2113  /* Check if heartbeat is disabled */
2114  if (!options->HeartbeatPeriod) {
2115  return PERIODIC_EVENT_NO_UPDATE;
2116  }
2117 
2118  /* Skip the first one. */
2119  if (heartbeat_callback_first_time) {
2120  heartbeat_callback_first_time = 0;
2121  return options->HeartbeatPeriod;
2122  }
2123 
2124  /* Write the heartbeat message */
2125  if (log_heartbeat(now) == 0) {
2126  return options->HeartbeatPeriod;
2127  } else {
2128  /* If we couldn't write the heartbeat log message, try again in the minimum
2129  * interval of time. */
2130  return MIN_HEARTBEAT_PERIOD;
2131  }
2132 }
2133 
2134 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2135 static int
2136 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2137 {
2138  (void)now;
2139  if (dir_server_mode(options)) {
2141  }
2142  return CDM_CLEAN_CALLBACK_INTERVAL;
2143 }
2144 
2145 /*
2146  * Periodic callback: Run scheduled events for HS service. This is called
2147  * every second.
2148  */
2149 static int
2150 hs_service_callback(time_t now, const or_options_t *options)
2151 {
2152  (void) options;
2153 
2154  /* We need to at least be able to build circuits and that we actually have
2155  * a working network. */
2157  networkstatus_get_live_consensus(now) == NULL) {
2158  goto end;
2159  }
2160 
2162 
2163  end:
2164  /* Every 1 second. */
2165  return 1;
2166 }
2167 
2168 /*
2169  * Periodic callback: Send once-per-second events to the controller(s).
2170  * This is called every second.
2171  */
2172 static int
2173 control_per_second_events_callback(time_t now, const or_options_t *options)
2174 {
2175  (void) options;
2176  (void) now;
2177 
2179 
2180  return 1;
2181 }
2182 
2183 /** Last time that update_current_time was called. */
2184 static time_t current_second = 0;
2185 /** Last time that update_current_time updated current_second. */
2186 static monotime_coarse_t current_second_last_changed;
2187 
2188 /**
2189  * Set the current time to "now", which should be the value returned by
2190  * time(). Check for clock jumps and track the total number of seconds we
2191  * have been running.
2192  */
2193 void
2195 {
2196  if (PREDICT_LIKELY(now == current_second)) {
2197  /* We call this function a lot. Most frequently, the current second
2198  * will not have changed, so we just return. */
2199  return;
2200  }
2201 
2202  const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
2203 
2204  /* Check the wall clock against the monotonic clock, so we can
2205  * better tell idleness from clock jumps and/or other shenanigans. */
2206  monotime_coarse_t last_updated;
2207  memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
2208  monotime_coarse_get(&current_second_last_changed);
2209 
2210  /** How much clock jumping means that we should adjust our idea of when
2211  * to go dormant? */
2212 #define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE 20
2213 
2214  /* Don't go dormant early or late just because we jumped in time. */
2215  if (ABS(seconds_elapsed) >= NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE) {
2217  netstatus_note_clock_jumped(seconds_elapsed);
2218  }
2219  }
2220 
2221  /** How much clock jumping do we tolerate? */
2222 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2223 
2224  /** How much idleness do we tolerate? */
2225 #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
2226 
2227  if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
2228  // moving back in time is always a bad sign.
2229  circuit_note_clock_jumped(seconds_elapsed, false);
2230 
2231  } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2232  /* Compare the monotonic clock to the result of time(). */
2233  const int32_t monotime_msec_passed =
2234  monotime_coarse_diff_msec32(&last_updated,
2236  const int monotime_sec_passed = monotime_msec_passed / 1000;
2237  const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
2238  /* If the monotonic clock deviates from time(NULL), we have a couple of
2239  * possibilities. On some systems, this means we have been suspended or
2240  * sleeping. Everywhere, it can mean that the wall-clock time has
2241  * been changed -- for example, with settimeofday().
2242  *
2243  * On the other hand, if the monotonic time matches with the wall-clock
2244  * time, we've probably just been idle for a while, with no events firing.
2245  * we tolerate much more of that.
2246  */
2247  const bool clock_jumped = abs(discrepancy) > 2;
2248 
2249  if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
2250  circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
2251  }
2252  } else if (seconds_elapsed > 0) {
2253  stats_n_seconds_working += seconds_elapsed;
2254  }
2255 
2256  update_approx_time(now);
2257  current_second = now;
2258 }
2259 
2260 #ifdef HAVE_SYSTEMD_209
2261 static periodic_timer_t *systemd_watchdog_timer = NULL;
2262 
2263 /** Libevent callback: invoked to reset systemd watchdog. */
2264 static void
2265 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2266 {
2267  (void)timer;
2268  (void)arg;
2269  sd_notify(0, "WATCHDOG=1");
2270 }
2271 #endif /* defined(HAVE_SYSTEMD_209) */
2272 
2273 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2274 
2275 /** Called when our IP address seems to have changed. <b>on_client_conn</b>
2276  * should be true if:
2277  * - we detected a change in our interface address, using an outbound
2278  * connection, and therefore
2279  * - our client TLS keys need to be rotated.
2280  * Otherwise, it should be false, and:
2281  * - we detected a change in our published address
2282  * (using some other method), and therefore
2283  * - the published addresses in our descriptor need to change.
2284  */
2285 void
2286 ip_address_changed(int on_client_conn)
2287 {
2288  const or_options_t *options = get_options();
2289  int server = server_mode(options);
2290 
2291  if (on_client_conn) {
2292  if (! server) {
2293  /* Okay, change our keys. */
2294  if (init_keys_client() < 0)
2295  log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2296  }
2297  } else {
2298  if (server) {
2299  if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2301  reset_uptime();
2303  /* All relays include their IP addresses as their ORPort addresses in
2304  * their descriptor.
2305  * Exit relays also incorporate interface addresses in their exit
2306  * policies, when ExitPolicyRejectLocalInterfaces is set. */
2307  mark_my_descriptor_dirty("IP address changed");
2308  }
2309  }
2310 
2312 }
2313 
2314 /** Forget what we've learned about the correctness of our DNS servers, and
2315  * start learning again. */
2316 void
2318 {
2319  if (server_mode(get_options())) {
2321  if (check_dns_honesty_event) {
2322  periodic_event_reschedule(check_dns_honesty_event);
2323  }
2324  }
2325 }
2326 
2327 /** Initialize some mainloop_event_t objects that we require. */
2328 void
2330 {
2334  }
2335  if (!postloop_cleanup_ev) {
2338  }
2339 }
2340 
2341 /** Tor main loop. */
2342 int
2344 {
2345  /* initialize the periodic events first, so that code that depends on the
2346  * events being present does not assert.
2347  */
2350 
2352 
2353  struct timeval one_second = { 1, 0 };
2354  initialize_periodic_events_event = tor_evtimer_new(
2357  event_add(initialize_periodic_events_event, &one_second);
2358 
2359 #ifdef HAVE_SYSTEMD_209
2360  uint64_t watchdog_delay;
2361  /* set up systemd watchdog notification. */
2362  if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2363  if (! systemd_watchdog_timer) {
2364  struct timeval watchdog;
2365  /* The manager will "act on" us if we don't send them a notification
2366  * every 'watchdog_delay' microseconds. So, send notifications twice
2367  * that often. */
2368  watchdog_delay /= 2;
2369  watchdog.tv_sec = watchdog_delay / 1000000;
2370  watchdog.tv_usec = watchdog_delay % 1000000;
2371 
2372  systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2373  &watchdog,
2374  systemd_watchdog_callback,
2375  NULL);
2376  tor_assert(systemd_watchdog_timer);
2377  }
2378  }
2379 #endif /* defined(HAVE_SYSTEMD_209) */
2380 #ifdef ENABLE_RESTART_DEBUGGING
2381  {
2382  static int first_time = 1;
2383 
2384  if (first_time && getenv("TOR_DEBUG_RESTART")) {
2385  first_time = 0;
2386  const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2387  long sec;
2388  int sec_ok=0;
2389  if (sec_str &&
2390  (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2391  sec_ok) {
2392  /* Okay, we parsed the seconds. */
2393  } else {
2394  sec = 5;
2395  }
2396  struct timeval restart_after = { (time_t) sec, 0 };
2397  tor_shutdown_event_loop_for_restart_event =
2398  tor_evtimer_new(tor_libevent_get_base(),
2399  tor_shutdown_event_loop_for_restart_cb, NULL);
2400  event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2401  }
2402  }
2403 #endif /* defined(ENABLE_RESTART_DEBUGGING) */
2404 
2405  return run_main_loop_until_done();
2406 }
2407 
2408 #ifndef _WIN32
2409 /** Rate-limiter for EINVAL-type libevent warnings. */
2410 static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
2411 #endif
2412 
2413 /**
2414  * Run the main loop a single time. Return 0 for "exit"; -1 for "exit with
2415  * error", and 1 for "run this again."
2416  */
2417 static int
2419 {
2420  int loop_result;
2421 
2422  if (nt_service_is_stopping())
2423  return 0;
2424 
2426  return 0;
2427 
2428 #ifndef _WIN32
2429  /* Make it easier to tell whether libevent failure is our fault or not. */
2430  errno = 0;
2431 #endif
2432 
2433  if (get_options()->MainloopStats) {
2434  /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2435  * are collecting main loop statistics. */
2436  called_loop_once = 1;
2437  } else {
2438  called_loop_once = 0;
2439  }
2440 
2441  /* Make sure we know (about) what time it is. */
2442  update_approx_time(time(NULL));
2443 
2444  /* Here it is: the main loop. Here we tell Libevent to poll until we have
2445  * an event, or the second ends, or until we have some active linked
2446  * connections to trigger events for. Libevent will wait till one
2447  * of these happens, then run all the appropriate callbacks. */
2450 
2451  if (get_options()->MainloopStats) {
2452  /* Update our main loop counters. */
2453  if (loop_result == 0) {
2454  // The call was successful.
2456  } else if (loop_result == -1) {
2457  // The call was erroneous.
2459  } else if (loop_result == 1) {
2460  // The call didn't have any active or pending events
2461  // to handle.
2463  }
2464  }
2465 
2466  /* Oh, the loop failed. That might be an error that we need to
2467  * catch, but more likely, it's just an interrupted poll() call or something,
2468  * and we should try again. */
2469  if (loop_result < 0) {
2470  int e = tor_socket_errno(-1);
2471  /* let the program survive things like ^z */
2472  if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2473  log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2474  tor_libevent_get_method(), tor_socket_strerror(e), e);
2475  return -1;
2476 #ifndef _WIN32
2477  } else if (e == EINVAL) {
2479  "EINVAL from libevent: should you upgrade libevent?");
2480  if (libevent_error_ratelim.n_calls_since_last_time > 8) {
2481  log_err(LD_NET, "Too many libevent errors, too fast: dying");
2482  return -1;
2483  }
2484 #endif /* !defined(_WIN32) */
2485  } else {
2486  tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2487  log_debug(LD_NET,"libevent call interrupted.");
2488  /* You can't trust the results of this poll(). Go back to the
2489  * top of the big for loop. */
2490  return 1;
2491  }
2492  }
2493 
2495  return 0;
2496 
2497  return 1;
2498 }
2499 
2500 /** Run the run_main_loop_once() function until it declares itself done,
2501  * and return its final return value.
2502  *
2503  * Shadow won't invoke this function, so don't fill it up with things.
2504  */
2505 STATIC int
2507 {
2508  int loop_result = 1;
2509 
2512 
2513  do {
2514  loop_result = run_main_loop_once();
2515  } while (loop_result == 1);
2516 
2518  return main_loop_exit_value;
2519  else
2520  return loop_result;
2521 }
2522 
2523 /** Returns Tor's uptime. */
2524 MOCK_IMPL(long,
2525 get_uptime,(void))
2526 {
2527  return stats_n_seconds_working;
2528 }
2529 
2530 /** Reset Tor's uptime. */
2531 MOCK_IMPL(void,
2533 {
2535 }
2536 
2537 void
2538 tor_mainloop_free_all(void)
2539 {
2540  smartlist_free(connection_array);
2541  smartlist_free(closeable_connection_lst);
2542  smartlist_free(active_linked_connection_lst);
2543  teardown_periodic_events();
2544  tor_event_free(shutdown_did_not_work_event);
2545  tor_event_free(initialize_periodic_events_event);
2546  mainloop_event_free(directory_all_unreachable_cb_event);
2547  mainloop_event_free(schedule_active_linked_connections_event);
2548  mainloop_event_free(postloop_cleanup_ev);
2549  mainloop_event_free(handle_deferred_signewnym_ev);
2550  mainloop_event_free(scheduled_shutdown_ev);
2551  mainloop_event_free(rescan_periodic_events_ev);
2552 
2553 #ifdef HAVE_SYSTEMD_209
2554  periodic_timer_free(systemd_watchdog_timer);
2555 #endif
2556 
2558 
2559  memset(&global_bucket, 0, sizeof(global_bucket));
2560  memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
2564  newnym_epoch = 0;
2565  called_loop_once = 0;
2569  quiet_level = 0;
2570  should_init_bridge_stats = 1;
2571  heartbeat_callback_first_time = 1;
2572  current_second = 0;
2573  memset(&current_second_last_changed, 0,
2574  sizeof(current_second_last_changed));
2575 }
static int rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2028
Header for periodic.c.
int DormantClientTimeout
uint64_t global_identifier
Definition: channel.h:197
void hs_client_purge_state(void)
Definition: hs_client.c:2560
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:2506
static int run_main_loop_once(void)
Definition: mainloop.c:2418
time_t time_of_process_start
Definition: mainloop.c:143
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2215
static void handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1311
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:1102
unsigned int channel_num_circuits(channel_t *chan)
Definition: channel.c:3322
void connection_or_close_normally(or_connection_t *orconn, int flush)
Router descriptor structure.
int rend_num_services(void)
Definition: rendservice.c:185
int connection_remove(connection_t *conn)
Definition: mainloop.c:298
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:234
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:5205
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:950
#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:2194
Header file for geoip_stats.c.
static void rescan_periodic_events_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1566
Header file containing client data for the HS subsytem.
connection_t * connection_get_by_type_nonlinked(int type)
Definition: connection.c:4600
Header for buffers_tls.c.
static int clean_caches_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2011
void router_dir_info_changed(void)
Definition: nodelist.c:2367
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:1770
long get_uptime(void)
Definition: mainloop.c:2525
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:3605
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:1907
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:1983
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:1345
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:2111
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:332
void circuit_expire_building(void)
Definition: circuituse.c:457
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:76
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:42
int ConnDirectionStatistics
void router_do_reachability_checks(int test_or, int test_dir)
Definition: selftest.c:170
const char * conn_type_to_string(int type)
Definition: connection.c:245
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:1873
const or_options_t * get_options(void)
Definition: config.c:925
#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:1525
Header file for cpuworker.c.
void rep_history_clean(time_t before)
Definition: rephist.c:593
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:1637
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:2343
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:2329
#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:1334
const char * conn_state_to_string(int type, int state)
Definition: connection.c:277
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:2045
void channel_update_bad_for_new_circs(const char *digest, int force)
Definition: channel.c:3444
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:1503
or_connection_t * TO_OR_CONN(connection_t *c)
int router_have_minimum_dir_info(void)
Definition: nodelist.c:2324
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:1457
int retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:2993
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:219
int connection_wants_to_flush(connection_t *conn)
Definition: connection.c:4017
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:2410
networkstatus_t * networkstatus_get_latest_consensus(void)
int safe_timer_diff(time_t now, time_t next)
Definition: periodic.c:351
Header file for hibernate.c.
static int periodic_events_initialized
Definition: mainloop.c:1352
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:1120
static monotime_coarse_t current_second_last_changed
Definition: mainloop.c:2186
#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:1511
#define LD_CONTROL
Definition: log.h:80
static void run_connection_housekeeping(int i, time_t now)
Definition: mainloop.c:1161
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:2043
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:2106
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:120
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:254
time_t geoip_entry_stats_write(time_t now)
Definition: geoip_stats.c:1372
int dirclient_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
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:842
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:7518
Header file for circuitbuild.c.
void hs_cache_client_intro_state_clean(time_t now)
Definition: hs_cache.c:989
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:2092
#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:106
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:1072
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:3452
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:3492
struct event_base * tor_libevent_get_base(void)
const char * tor_libevent_get_method(void)
Header for feature/dirclient/dirclient_modes.c.
void dns_servers_relaunch_checks(void)
Definition: mainloop.c:2317
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:704
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:4590
int quiet
Definition: config.c:2446
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:2443
void do_signewnym(time_t now)
Definition: mainloop.c:1321
Header file for circuitlist.c.
void periodic_events_disconnect_all(void)
Definition: periodic.c:331
void circuit_expire_old_circs_as_needed(time_t now)
Definition: circuituse.c:1357
Header file for rendservice.c.
uint16_t marked_for_close
void ip_address_changed(int on_client_conn)
Definition: mainloop.c:2286
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:1803
void schedule_rescan_periodic_events(void)
Definition: mainloop.c:1577
Headers for transports.c.
Header file for proxymode.c.
int connection_state_is_open(connection_t *conn)
Definition: connection.c:4746
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:854
Fixed-size cell structure.
int channel_is_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2872
unsigned int edge_has_sent_end
void circuit_close_all_marked(void)
Definition: circuitlist.c:659
uint8_t command
Definition: cell_st.h:19
static void postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1621
void channel_listener_run_cleanup(void)
Definition: channel.c:2132
void mainloop_schedule_shutdown(int delay_sec)
Definition: mainloop.c:1662
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:3247
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:2378
STATIC int check_network_participation_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1822
#define CONN_TYPE_AP_DNS_LISTENER
Definition: connection.h:66
#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:1804
Header file for connection_edge.c.
void connection_about_to_close_connection(connection_t *conn)
Definition: connection.c:811
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:5355
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:1752
Header for status.c.
void hs_cache_clean_as_client(time_t now)
Definition: hs_cache.c:928
int TestingDirConnectionMaxStall
time_t timestamp_last_read_allowed
int dirclient_fetches_from_authorities(const or_options_t *options)
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:2111
const char * marked_for_close_file
STATIC int get_my_roles(const or_options_t *options)
Definition: mainloop.c:1465
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:4179
size_t outbuf_flushlen
static int save_state_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1892
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:44
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:1675
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:2724
Header file for control.c.
void connection_expire_held_open(void)
Definition: connection.c:959
static mainloop_event_t * postloop_cleanup_ev
Definition: mainloop.c:1632
static int write_stats_file_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1920
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:1589
#define LOG_DEBUG
Definition: log.h:42
int or_state_save(time_t now)
Definition: statefile.c:511
unsigned int hs_service_get_num_services(void)
Definition: hs_service.c:3795
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:2977
static void scheduled_shutdown_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1652
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:1609
int control_any_per_second_event_enabled(void)
Client request structure.
void reset_uptime(void)
Definition: mainloop.c:2532
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:53
void log_failed_proxy_connection(connection_t *conn)
Definition: connection.c:5570
Header file for connection_or.c.
static mainloop_event_t * scheduled_shutdown_ev
Definition: mainloop.c:1648
void reset_bandwidth_test(void)
Definition: circuituse.c:1607
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:154
char address[MAX_SOCKS_ADDR_LEN]
int HiddenServiceStatistics
#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:2065
#define OR_CONN_STATE_CONNECTING
Definition: orconn_event.h:31
void router_reset_reachability(void)
Definition: selftest.c:47
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:200
static void signewnym_impl(time_t now)
Definition: mainloop.c:1289
time_t timestamp_last_had_circuits
Definition: channel.h:453
void addressmap_clear_transient(void)
Definition: addressmap.c:311
void connection_ap_expire_beginning(void)
static int retry_listeners_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2091
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:291
#define CONN_TYPE_AP
Definition: connection.h:49
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:2184
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:1116
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)