tor  0.4.2.0-alpha-dev
mainloop.c
Go to the documentation of this file.
1 /* Copyright (c) 2001 Matej Pfajfar.
2  * Copyright (c) 2001-2004, Roger Dingledine.
3  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
4  * Copyright (c) 2007-2019, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
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"
58 #include "core/mainloop/netstatus.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"
70 #include "feature/client/addressmap.h"
71 #include "feature/client/bridges.h"
72 #include "feature/client/dnsserv.h"
78 #include "feature/dircache/consdiffmgr.h"
82 #include "feature/hs/hs_cache.h"
83 #include "feature/hs/hs_client.h"
84 #include "feature/hs/hs_service.h"
89 #include "feature/relay/dns.h"
90 #include "feature/relay/routerkeys.h"
92 #include "feature/relay/selftest.h"
93 #include "feature/rend/rendcache.h"
96 #include "feature/stats/predict_ports.h"
97 #include "feature/stats/rephist.h"
98 #include "lib/buf/buffers.h"
100 #include "lib/err/backtrace.h"
101 #include "lib/tls/buffers_tls.h"
102 
103 #include "lib/net/buffers_net.h"
105 
106 #include <event2/event.h>
107 
108 #include "core/or/cell_st.h"
109 #include "core/or/entry_connection_st.h"
110 #include "feature/nodelist/networkstatus_st.h"
111 #include "core/or/or_connection_st.h"
112 #include "app/config/or_state_st.h"
113 #include "feature/nodelist/routerinfo_st.h"
114 #include "core/or/socks_request_st.h"
115 
116 #ifdef HAVE_UNISTD_H
117 #include <unistd.h>
118 #endif
119 
120 #ifdef HAVE_SYSTEMD
121 # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__)
122 /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse
123  * Coverity. Here's a kludge to unconfuse it.
124  */
125 # define __INCLUDE_LEVEL__ 2
126 #endif /* defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) */
127 #include <systemd/sd-daemon.h>
128 #endif /* defined(HAVE_SYSTEMD) */
129 
130 /* Token bucket for all traffic. */
131 token_bucket_rw_t global_bucket;
132 
133 /* Token bucket for relayed traffic. */
134 token_bucket_rw_t global_relayed_bucket;
135 
136 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
138 static uint64_t stats_n_bytes_read = 0;
140 static uint64_t stats_n_bytes_written = 0;
144 static long stats_n_seconds_working = 0;
146 static uint64_t stats_n_main_loop_successes = 0;
148 static uint64_t stats_n_main_loop_errors = 0;
150 static uint64_t stats_n_main_loop_idle = 0;
151 
153 #define MAX_SIGNEWNYM_RATE 10
154 
155 static time_t time_of_last_signewnym = 0;
157 static int signewnym_is_pending = 0;
161 static unsigned newnym_epoch = 0;
162 
174 static int called_loop_once = 0;
178 static int main_loop_should_exit = 0;
182 static int main_loop_exit_value = 0;
183 
190 static int can_complete_circuits = 0;
191 
194 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
195 
197 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
198 
199 static int conn_close_if_marked(int i);
202 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
203 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
204 static void shutdown_did_not_work_callback(evutil_socket_t fd, short event,
205  void *arg) ATTR_NORETURN;
206 
207 /****************************************************************************
208  *
209  * This section contains accessors and other methods on the connection_array
210  * variables (which are global within this file and unavailable outside it).
211  *
212  ****************************************************************************/
213 
217 int
219 {
220  return can_complete_circuits;
221 }
222 
225 void
227 {
229 }
230 
233 void
235 {
237 }
238 
243 int
244 connection_add_impl(connection_t *conn, int is_connecting)
245 {
246  tor_assert(conn);
247  tor_assert(SOCKET_OK(conn->s) ||
248  conn->linked ||
249  (conn->type == CONN_TYPE_AP &&
250  TO_EDGE_CONN(conn)->is_dns_request));
251 
252  tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
253  conn->conn_array_index = smartlist_len(connection_array);
255 
256  (void) is_connecting;
257 
258  if (SOCKET_OK(conn->s) || conn->linked) {
259  conn->read_event = tor_event_new(tor_libevent_get_base(),
260  conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
261  conn->write_event = tor_event_new(tor_libevent_get_base(),
262  conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
263  /* XXXX CHECK FOR NULL RETURN! */
264  }
265 
266  log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
267  conn_type_to_string(conn->type), (int)conn->s, conn->address,
268  smartlist_len(connection_array));
269 
270  return 0;
271 }
272 
274 void
276 {
277  if (conn->read_event) {
278  if (event_del(conn->read_event))
279  log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
280  tor_free(conn->read_event);
281  }
282  if (conn->write_event) {
283  if (event_del(conn->write_event))
284  log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
285  tor_free(conn->write_event);
286  }
287  if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
289  }
290 }
291 
296 int
298 {
299  int current_index;
300  connection_t *tmp;
301 
302  tor_assert(conn);
303 
304  log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
305  (int)conn->s, conn_type_to_string(conn->type),
306  smartlist_len(connection_array));
307 
308  if (conn->type == CONN_TYPE_AP && conn->socket_family == AF_UNIX) {
309  log_info(LD_NET, "Closing SOCKS Unix socket connection");
310  }
311 
313 
314  tor_assert(conn->conn_array_index >= 0);
315  current_index = conn->conn_array_index;
316  connection_unregister_events(conn); /* This is redundant, but cheap. */
317  if (current_index == smartlist_len(connection_array)-1) { /* at the end */
318  smartlist_del(connection_array, current_index);
319  return 0;
320  }
321 
322  /* replace this one with the one at the end */
323  smartlist_del(connection_array, current_index);
324  tmp = smartlist_get(connection_array, current_index);
325  tmp->conn_array_index = current_index;
326 
327  return 0;
328 }
329 
339 static void
341 {
343  if (conn->conn_array_index >= 0) {
344  connection_remove(conn);
345  }
346  if (conn->linked_conn) {
347  conn->linked_conn->linked_conn = NULL;
348  if (! conn->linked_conn->marked_for_close &&
350  connection_start_reading(conn->linked_conn);
351  conn->linked_conn = NULL;
352  }
355  if (conn->type == CONN_TYPE_EXIT) {
357  }
358  if (conn->type == CONN_TYPE_OR) {
359  if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
361  /* connection_unlink() can only get called if the connection
362  * was already on the closeable list, and it got there by
363  * connection_mark_for_close(), which was called from
364  * connection_or_close_normally() or
365  * connection_or_close_for_error(), so the channel should
366  * already be in CHANNEL_STATE_CLOSING, and then the
367  * connection_about_to_close_connection() goes to
368  * connection_or_about_to_close(), which calls channel_closed()
369  * to notify the channel_t layer, and closed the channel, so
370  * nothing more to do here to deal with the channel associated
371  * with an orconn.
372  */
373  }
374  connection_free(conn);
375 }
376 
379 
386 static void
388 {
389  (void)event;
390  (void)arg;
391 
392  /* All active linked conns should get their read events activated,
393  * so that libevent knows to run their callbacks. */
395  event_active(conn->read_event, EV_READ, 1));
396 
397  /* Reactivate the event if we still have connections in the active list.
398  *
399  * A linked connection doesn't get woken up by I/O but rather artificially
400  * by this event callback. It has directory data spooled in it and it is
401  * sent incrementally by small chunks unless spool_eagerly is true. For that
402  * to happen, we need to induce the activation of the read event so it can
403  * be flushed. */
404  if (smartlist_len(active_linked_connection_lst)) {
406  }
407 }
408 
411 void
413 {
414  if (!connection_array)
415  connection_array = smartlist_new();
417  closeable_connection_lst = smartlist_new();
419  active_linked_connection_lst = smartlist_new();
420 }
421 
423 void
425 {
428  assert_connection_ok(conn, time(NULL));
431 }
432 
434 int
436 {
438 }
439 
441 int
443 {
444  return smartlist_contains(connection_array, conn);
445 }
446 
451 get_connection_array, (void))
452 {
453  if (!connection_array)
454  connection_array = smartlist_new();
455  return connection_array;
456 }
457 
462 MOCK_IMPL(uint64_t,
463 get_bytes_read,(void))
464 {
465  return stats_n_bytes_read;
466 }
467 
472 MOCK_IMPL(uint64_t,
473 get_bytes_written,(void))
474 {
475  return stats_n_bytes_written;
476 }
477 
482 void
484 {
485  stats_n_bytes_read += r;
487 }
488 
492 void
494 {
495  if (events & READ_EVENT)
496  connection_start_reading(conn);
497  else
498  connection_stop_reading(conn);
499 
500  if (events & WRITE_EVENT)
501  connection_start_writing(conn);
502  else
503  connection_stop_writing(conn);
504 }
505 
507 int
509 {
510  tor_assert(conn);
511 
512  return conn->reading_from_linked_conn ||
513  (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
514 }
515 
517 void
519 {
523 }
524 
526 static void
528 {
530 }
531 
533 uint64_t
535 {
537 }
538 
540 static void
542 {
544 }
545 
547 uint64_t
549 {
551 }
552 
554 static void
556 {
558 }
559 
561 uint64_t
563 {
564  return stats_n_main_loop_idle;
565 }
566 
570 static int
571 connection_check_event(connection_t *conn, struct event *ev)
572 {
573  int bad;
574 
575  if (conn->type == CONN_TYPE_AP && TO_EDGE_CONN(conn)->is_dns_request) {
576  /* DNS requests which we launch through the dnsserv.c module do not have
577  * any underlying socket or any underlying linked connection, so they
578  * shouldn't have any attached events either.
579  */
580  bad = ev != NULL;
581  } else {
582  /* Everything else should have an underlying socket, or a linked
583  * connection (which is also tracked with a read_event/write_event pair).
584  */
585  bad = ev == NULL;
586  }
587 
588  if (bad) {
589  log_warn(LD_BUG, "Event missing on connection %p [%s;%s]. "
590  "socket=%d. linked=%d. "
591  "is_dns_request=%d. Marked_for_close=%s:%d",
592  conn,
593  conn_type_to_string(conn->type),
594  conn_state_to_string(conn->type, conn->state),
595  (int)conn->s, (int)conn->linked,
596  (conn->type == CONN_TYPE_AP &&
597  TO_EDGE_CONN(conn)->is_dns_request),
598  conn->marked_for_close_file ? conn->marked_for_close_file : "-",
599  conn->marked_for_close
600  );
601  log_backtrace(LOG_WARN, LD_BUG, "Backtrace attached.");
602  return -1;
603  }
604  return 0;
605 }
606 
609 connection_stop_reading,(connection_t *conn))
610 {
611  tor_assert(conn);
612 
613  if (connection_check_event(conn, conn->read_event) < 0) {
614  return;
615  }
616 
617  if (conn->linked) {
618  conn->reading_from_linked_conn = 0;
620  } else {
621  if (event_del(conn->read_event))
622  log_warn(LD_NET, "Error from libevent setting read event state for %d "
623  "to unwatched: %s",
624  (int)conn->s,
625  tor_socket_strerror(tor_socket_errno(conn->s)));
626  }
627 }
628 
631 connection_start_reading,(connection_t *conn))
632 {
633  tor_assert(conn);
634 
635  if (connection_check_event(conn, conn->read_event) < 0) {
636  return;
637  }
638 
639  if (conn->linked) {
640  conn->reading_from_linked_conn = 1;
643  } else {
644  if (event_add(conn->read_event, NULL))
645  log_warn(LD_NET, "Error from libevent setting read event state for %d "
646  "to watched: %s",
647  (int)conn->s,
648  tor_socket_strerror(tor_socket_errno(conn->s)));
649  }
650 }
651 
653 int
655 {
656  tor_assert(conn);
657 
658  return conn->writing_to_linked_conn ||
659  (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
660 }
661 
664 connection_stop_writing,(connection_t *conn))
665 {
666  tor_assert(conn);
667 
668  if (connection_check_event(conn, conn->write_event) < 0) {
669  return;
670  }
671 
672  if (conn->linked) {
673  conn->writing_to_linked_conn = 0;
674  if (conn->linked_conn)
676  } else {
677  if (event_del(conn->write_event))
678  log_warn(LD_NET, "Error from libevent setting write event state for %d "
679  "to unwatched: %s",
680  (int)conn->s,
681  tor_socket_strerror(tor_socket_errno(conn->s)));
682  }
683 }
684 
687 connection_start_writing,(connection_t *conn))
688 {
689  tor_assert(conn);
690 
691  if (connection_check_event(conn, conn->write_event) < 0) {
692  return;
693  }
694 
695  if (conn->linked) {
696  conn->writing_to_linked_conn = 1;
697  if (conn->linked_conn &&
700  } else {
701  if (event_add(conn->write_event, NULL))
702  log_warn(LD_NET, "Error from libevent setting write event state for %d "
703  "to watched: %s",
704  (int)conn->s,
705  tor_socket_strerror(tor_socket_errno(conn->s)));
706  }
707 }
708 
714 static int
716 {
717  if (conn->linked && conn->reading_from_linked_conn) {
718  if (! conn->linked_conn ||
720  buf_datalen(conn->linked_conn->outbuf)))
721  return 1;
722  }
723  return 0;
724 }
725 
727 static struct event *shutdown_did_not_work_event = NULL;
728 
734 static void
735 shutdown_did_not_work_callback(evutil_socket_t fd, short event, void *arg)
736 {
737  // LCOV_EXCL_START
738  (void) fd;
739  (void) event;
740  (void) arg;
741  tor_assert_unreached();
742  // LCOV_EXCL_STOP
743 }
744 
745 #ifdef ENABLE_RESTART_DEBUGGING
746 static struct event *tor_shutdown_event_loop_for_restart_event = NULL;
747 static void
748 tor_shutdown_event_loop_for_restart_cb(
749  evutil_socket_t fd, short event, void *arg)
750 {
751  (void)fd;
752  (void)event;
753  (void)arg;
754  tor_event_free(tor_shutdown_event_loop_for_restart_event);
756 }
757 #endif /* defined(ENABLE_RESTART_DEBUGGING) */
758 
763 void
765 {
767  return; /* Ignore multiple calls to this function. */
768 
770  main_loop_exit_value = exitcode;
771 
772  /* Die with an assertion failure in ten seconds, if for some reason we don't
773  * exit normally. */
774  /* XXXX We should consider this code if it's never used. */
775  struct timeval ten_seconds = { 10, 0 };
776  shutdown_did_not_work_event = tor_evtimer_new(
777  tor_libevent_get_base(),
779  event_add(shutdown_did_not_work_event, &ten_seconds);
780 
781  /* Unlike exit_loop_after_delay(), exit_loop_after_callback
782  * prevents other callbacks from running. */
783  tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
784 }
785 
787 int
789 {
790  return main_loop_should_exit;
791 }
792 
796 static void
798 {
799  tor_assert(conn);
800  tor_assert(conn->linked == 1);
801 
802  if (!conn->active_on_link) {
803  conn->active_on_link = 1;
806  } else {
808  }
809 }
810 
814 void
816 {
817  tor_assert(conn);
818  tor_assert(conn->linked == 1);
819 
820  if (conn->active_on_link) {
821  conn->active_on_link = 0;
822  /* FFFF We could keep an index here so we can smartlist_del
823  * cleanly. On the other hand, this doesn't show up on profiles,
824  * so let's leave it alone for now. */
826  } else {
828  }
829 }
830 
832 STATIC void
834 {
835  int i;
836  for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
837  connection_t *conn = smartlist_get(closeable_connection_lst, i);
838  if (conn->conn_array_index < 0) {
839  connection_unlink(conn); /* blow it away right now */
840  } else {
842  ++i;
843  }
844  }
845 }
846 
849 connection_count_moribund, (void))
850 {
851  int moribund = 0;
852 
853  /*
854  * Count things we'll try to kill when close_closeable_connections()
855  * runs next.
856  */
858  if (SOCKET_OK(conn->s) && connection_is_moribund(conn)) ++moribund;
859  } SMARTLIST_FOREACH_END(conn);
860 
861  return moribund;
862 }
863 
866 static void
867 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
868 {
869  connection_t *conn = _conn;
870  (void)fd;
871  (void)event;
872 
873  log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
874 
875  /* assert_connection_ok(conn, time(NULL)); */
876 
877  if (connection_handle_read(conn) < 0) {
878  if (!conn->marked_for_close) {
879 #ifndef _WIN32
880  log_warn(LD_BUG,"Unhandled error on read for %s connection "
881  "(fd %d); removing",
882  conn_type_to_string(conn->type), (int)conn->s);
884 #endif /* !defined(_WIN32) */
885  if (CONN_IS_EDGE(conn))
887  connection_mark_for_close(conn);
888  }
889  }
890  assert_connection_ok(conn, time(NULL));
891 
892  if (smartlist_len(closeable_connection_lst))
894 }
895 
898 static void
899 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
900 {
901  connection_t *conn = _conn;
902  (void)fd;
903  (void)events;
904 
905  LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
906  (int)conn->s));
907 
908  /* assert_connection_ok(conn, time(NULL)); */
909 
910  if (connection_handle_write(conn, 0) < 0) {
911  if (!conn->marked_for_close) {
912  /* this connection is broken. remove it. */
914  "unhandled error on write for %s connection (fd %d); removing",
915  conn_type_to_string(conn->type), (int)conn->s);
917  if (CONN_IS_EDGE(conn)) {
918  /* otherwise we cry wolf about duplicate close */
919  edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
920  if (!edge_conn->end_reason)
921  edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
922  edge_conn->edge_has_sent_end = 1;
923  }
924  connection_close_immediate(conn); /* So we don't try to flush. */
925  connection_mark_for_close(conn);
926  }
927  }
928  assert_connection_ok(conn, time(NULL));
929 
930  if (smartlist_len(closeable_connection_lst))
932 }
933 
942 static int
944 {
945  connection_t *conn;
946  int retval;
947  time_t now;
948 
949  conn = smartlist_get(connection_array, i);
950  if (!conn->marked_for_close)
951  return 0; /* nothing to see here, move along */
952  now = time(NULL);
953  assert_connection_ok(conn, now);
954  /* assert_all_pending_dns_resolves_ok(); */
955 
956  log_debug(LD_NET,"Cleaning up connection (fd "TOR_SOCKET_T_FORMAT").",
957  conn->s);
958 
959  /* If the connection we are about to close was trying to connect to
960  a proxy server and failed, the client won't be able to use that
961  proxy. We should warn the user about this. */
962  if (conn->proxy_state == PROXY_INFANT)
964 
965  if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
967  /* s == -1 means it's an incomplete edge connection, or that the socket
968  * has already been closed as unflushable. */
969  ssize_t sz = connection_bucket_write_limit(conn, now);
970  if (!conn->hold_open_until_flushed)
971  log_info(LD_NET,
972  "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
973  "to flush %d bytes. (Marked at %s:%d)",
975  (int)conn->s, conn_type_to_string(conn->type), conn->state,
976  (int)conn->outbuf_flushlen,
978  if (conn->linked_conn) {
979  retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf,
980  &conn->outbuf_flushlen);
981  if (retval >= 0) {
982  /* The linked conn will notice that it has data when it notices that
983  * we're gone. */
985  }
986  log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
987  "%d left; flushlen %d; wants-to-flush==%d", retval,
988  (int)connection_get_outbuf_len(conn),
989  (int)conn->outbuf_flushlen,
991  } else if (connection_speaks_cells(conn)) {
992  if (conn->state == OR_CONN_STATE_OPEN) {
993  retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz,
994  &conn->outbuf_flushlen);
995  } else
996  retval = -1; /* never flush non-open broken tls connections */
997  } else {
998  retval = buf_flush_to_socket(conn->outbuf, conn->s, sz,
999  &conn->outbuf_flushlen);
1000  }
1001  if (retval >= 0 && /* Technically, we could survive things like
1002  TLS_WANT_WRITE here. But don't bother for now. */
1004  if (retval > 0) {
1005  LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
1006  "Holding conn (fd %d) open for more flushing.",
1007  (int)conn->s));
1008  conn->timestamp_last_write_allowed = now; /* reset so we can flush
1009  * more */
1010  } else if (sz == 0) {
1011  /* Also, retval==0. If we get here, we didn't want to write anything
1012  * (because of rate-limiting) and we didn't. */
1013 
1014  /* Connection must flush before closing, but it's being rate-limited.
1015  * Let's remove from Libevent, and mark it as blocked on bandwidth
1016  * so it will be re-added on next token bucket refill. Prevents
1017  * busy Libevent loops where we keep ending up here and returning
1018  * 0 until we are no longer blocked on bandwidth.
1019  */
1021  /* Make sure that consider_empty_buckets really disabled the
1022  * connection: */
1023  if (BUG(connection_is_writing(conn))) {
1024  connection_write_bw_exhausted(conn, true);
1025  }
1026 
1027  /* The connection is being held due to write rate limit and thus will
1028  * flush its data later. We need to stop reading because this
1029  * connection is about to be closed once flushed. It should not
1030  * process anything more coming in at this stage. */
1031  connection_stop_reading(conn);
1032  }
1033  return 0;
1034  }
1035  if (connection_wants_to_flush(conn)) {
1036  log_fn(LOG_INFO, LD_NET, "We stalled too much while trying to write %d "
1037  "bytes to address %s. If this happens a lot, either "
1038  "something is wrong with your network connection, or "
1039  "something is wrong with theirs. "
1040  "(fd %d, type %s, state %d, marked at %s:%d).",
1041  (int)connection_get_outbuf_len(conn),
1043  (int)conn->s, conn_type_to_string(conn->type), conn->state,
1044  conn->marked_for_close_file,
1045  conn->marked_for_close);
1046  }
1047  }
1048 
1049  connection_unlink(conn); /* unlink, remove, free */
1050  return 1;
1051 }
1052 
1057 static void
1059 {
1060  (void)event;
1061  (void)arg;
1062 
1063  connection_t *conn;
1064 
1067  entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
1068  log_notice(LD_NET,
1069  "Is your network connection down? "
1070  "Failing connection to '%s:%d'.",
1071  safe_str_client(entry_conn->socks_request->address),
1072  entry_conn->socks_request->port);
1073  connection_mark_unattached_ap(entry_conn,
1075  }
1076  control_event_general_error("DIR_ALL_UNREACHABLE");
1077 }
1078 
1079 static mainloop_event_t *directory_all_unreachable_cb_event = NULL;
1080 
1087 void
1089 {
1090  (void)now;
1091 
1092  reset_uptime(); /* reset it */
1093 
1094  if (!directory_all_unreachable_cb_event) {
1095  directory_all_unreachable_cb_event =
1097  tor_assert(directory_all_unreachable_cb_event);
1098  }
1099 
1100  mainloop_event_activate(directory_all_unreachable_cb_event);
1101 }
1102 
1105 void
1106 directory_info_has_arrived(time_t now, int from_cache, int suppress_logs)
1107 {
1108  const or_options_t *options = get_options();
1109 
1110  /* if we have enough dir info, then update our guard status with
1111  * whatever we just learned. */
1112  int invalidate_circs = guards_update_all();
1113 
1114  if (invalidate_circs) {
1117  }
1118 
1119  if (!router_have_minimum_dir_info()) {
1120  int quiet = suppress_logs || from_cache ||
1122  tor_log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
1123  "I learned some more directory information, but not enough to "
1124  "build a circuit: %s", get_dir_info_status_string());
1126  return;
1127  } else {
1128  if (directory_fetches_from_authorities(options)) {
1130  }
1131 
1132  /* Don't even bother trying to get extrainfo until the rest of our
1133  * directory info is up-to-date */
1134  if (options->DownloadExtraInfo)
1136  }
1137 
1138  if (server_mode(options) && !net_is_disabled() && !from_cache &&
1141 }
1142 
1146 static void
1148 {
1149  cell_t cell;
1150  connection_t *conn = smartlist_get(connection_array, i);
1151  const or_options_t *options = get_options();
1152  or_connection_t *or_conn;
1153  channel_t *chan = NULL;
1154  int have_any_circuits;
1155  int past_keepalive =
1156  now >= conn->timestamp_last_write_allowed + options->KeepalivePeriod;
1157 
1158  if (conn->outbuf && !connection_get_outbuf_len(conn) &&
1159  conn->type == CONN_TYPE_OR)
1160  TO_OR_CONN(conn)->timestamp_lastempty = now;
1161 
1162  if (conn->marked_for_close) {
1163  /* nothing to do here */
1164  return;
1165  }
1166 
1167  /* Expire any directory connections that haven't been active (sent
1168  * if a server or received if a client) for 5 min */
1169  if (conn->type == CONN_TYPE_DIR &&
1170  ((DIR_CONN_IS_SERVER(conn) &&
1172  + options->TestingDirConnectionMaxStall < now) ||
1173  (!DIR_CONN_IS_SERVER(conn) &&
1175  + options->TestingDirConnectionMaxStall < now))) {
1176  log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
1177  (int)conn->s, conn->purpose);
1178  /* This check is temporary; it's to let us know whether we should consider
1179  * parsing partial serverdesc responses. */
1180  if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
1181  connection_get_inbuf_len(conn) >= 1024) {
1182  log_info(LD_DIR,"Trying to extract information from wedged server desc "
1183  "download.");
1185  } else {
1186  connection_mark_for_close(conn);
1187  }
1188  return;
1189  }
1190 
1191  if (!connection_speaks_cells(conn))
1192  return; /* we're all done here, the rest is just for OR conns */
1193 
1194  /* If we haven't flushed to an OR connection for a while, then either nuke
1195  the connection or send a keepalive, depending. */
1196 
1197  or_conn = TO_OR_CONN(conn);
1198  tor_assert(conn->outbuf);
1199 
1200  chan = TLS_CHAN_TO_BASE(or_conn->chan);
1201  tor_assert(chan);
1202 
1203  if (channel_num_circuits(chan) != 0) {
1204  have_any_circuits = 1;
1205  chan->timestamp_last_had_circuits = now;
1206  } else {
1207  have_any_circuits = 0;
1208  }
1209 
1210  if (channel_is_bad_for_new_circs(TLS_CHAN_TO_BASE(or_conn->chan)) &&
1211  ! have_any_circuits) {
1212  /* It's bad for new circuits, and has no unmarked circuits on it:
1213  * mark it now. */
1214  log_info(LD_OR,
1215  "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
1216  (int)conn->s, conn->address, conn->port);
1217  if (conn->state == OR_CONN_STATE_CONNECTING)
1219  END_OR_CONN_REASON_TIMEOUT,
1220  "Tor gave up on the connection");
1222  } else if (!connection_state_is_open(conn)) {
1223  if (past_keepalive) {
1224  /* We never managed to actually get this connection open and happy. */
1225  log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
1226  (int)conn->s,conn->address, conn->port);
1228  }
1229  } else if (we_are_hibernating() &&
1230  ! have_any_circuits &&
1231  !connection_get_outbuf_len(conn)) {
1232  /* We're hibernating or shutting down, there's no circuits, and nothing to
1233  * flush.*/
1234  log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
1235  "[Hibernating or exiting].",
1236  (int)conn->s,conn->address, conn->port);
1238  } else if (!have_any_circuits &&
1239  now - or_conn->idle_timeout >=
1241  log_info(LD_OR,"Expiring non-used OR connection %"PRIu64" to fd %d "
1242  "(%s:%d) [no circuits for %d; timeout %d; %scanonical].",
1243  (chan->global_identifier),
1244  (int)conn->s, conn->address, conn->port,
1245  (int)(now - chan->timestamp_last_had_circuits),
1246  or_conn->idle_timeout,
1247  or_conn->is_canonical ? "" : "non");
1249  } else if (
1250  now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
1251  now >=
1252  conn->timestamp_last_write_allowed + options->KeepalivePeriod*10) {
1253  log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
1254  "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
1255  "flush; %d seconds since last write)",
1256  (int)conn->s, conn->address, conn->port,
1257  (int)connection_get_outbuf_len(conn),
1258  (int)(now-conn->timestamp_last_write_allowed));
1260  } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
1261  /* send a padding cell */
1262  log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
1263  conn->address, conn->port);
1264  memset(&cell,0,sizeof(cell_t));
1265  cell.command = CELL_PADDING;
1266  connection_or_write_cell_to_buf(&cell, or_conn);
1267  } else {
1268  channelpadding_decide_to_pad_channel(chan);
1269  }
1270 }
1271 
1274 static void
1275 signewnym_impl(time_t now)
1276 {
1277  const or_options_t *options = get_options();
1278  if (!proxy_mode(options)) {
1279  log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
1280  "is disabled.");
1281  return;
1282  }
1283 
1286  hs_client_purge_state();
1287  time_of_last_signewnym = now;
1289 
1290  ++newnym_epoch;
1291 
1292  control_event_signal(SIGNEWNYM);
1293 }
1294 
1296 static void
1298 {
1299  (void)event;
1300  (void)arg;
1301  log_info(LD_CONTROL, "Honoring delayed NEWNYM request");
1302  do_signewnym(time(NULL));
1303 }
1304 
1306 void
1307 do_signewnym(time_t now)
1308 {
1310  const time_t delay_sec =
1312  if (! signewnym_is_pending) {
1317  }
1318  const struct timeval delay_tv = { delay_sec, 0 };
1320  }
1321  log_notice(LD_CONTROL,
1322  "Rate limiting NEWNYM request: delaying by %d second(s)",
1323  (int)(delay_sec));
1324  } else {
1325  signewnym_impl(now);
1326  }
1327 }
1328 
1330 unsigned
1332 {
1333  return newnym_epoch;
1334 }
1335 
1339 
1340 /* Declare all the timer callback functions... */
1341 #undef CALLBACK
1342 #define CALLBACK(name) \
1343  static int name ## _callback(time_t, const or_options_t *)
1344 CALLBACK(add_entropy);
1345 CALLBACK(check_expired_networkstatus);
1346 CALLBACK(clean_caches);
1347 CALLBACK(clean_consdiffmgr);
1348 CALLBACK(fetch_networkstatus);
1349 CALLBACK(heartbeat);
1350 CALLBACK(hs_service);
1351 CALLBACK(launch_descriptor_fetches);
1352 CALLBACK(prune_old_routers);
1353 CALLBACK(record_bridge_stats);
1354 CALLBACK(rend_cache_failure_clean);
1355 CALLBACK(reset_padding_counts);
1356 CALLBACK(retry_listeners);
1357 CALLBACK(rotate_x509_certificate);
1358 CALLBACK(save_state);
1359 CALLBACK(write_stats_file);
1360 CALLBACK(control_per_second_events);
1361 CALLBACK(second_elapsed);
1362 
1363 #undef CALLBACK
1364 
1365 /* Now we declare an array of periodic_event_item_t for each periodic event */
1366 #define CALLBACK(name, r, f) \
1367  PERIODIC_EVENT(name, PERIODIC_EVENT_ROLE_ ## r, f)
1368 #define FL(name) (PERIODIC_EVENT_FLAG_ ## name)
1369 
1370 STATIC periodic_event_item_t mainloop_periodic_events[] = {
1371 
1372  /* Everyone needs to run these. They need to have very long timeouts for
1373  * that to be safe. */
1374  CALLBACK(add_entropy, ALL, 0),
1375  CALLBACK(heartbeat, ALL, 0),
1376  CALLBACK(reset_padding_counts, ALL, 0),
1377 
1378  /* This is a legacy catch-all callback that runs once per second if
1379  * we are online and active. */
1380  CALLBACK(second_elapsed, NET_PARTICIPANT,
1381  FL(NEED_NET)|FL(RUN_ON_DISABLE)),
1382 
1383  /* XXXX Do we have a reason to do this on a callback? Does it do any good at
1384  * all? For now, if we're dormant, we can let our listeners decay. */
1385  CALLBACK(retry_listeners, NET_PARTICIPANT, FL(NEED_NET)),
1386 
1387  /* We need to do these if we're participating in the Tor network. */
1388  CALLBACK(check_expired_networkstatus, NET_PARTICIPANT, 0),
1389  CALLBACK(fetch_networkstatus, NET_PARTICIPANT, 0),
1390  CALLBACK(launch_descriptor_fetches, NET_PARTICIPANT, FL(NEED_NET)),
1391  CALLBACK(rotate_x509_certificate, NET_PARTICIPANT, 0),
1392  CALLBACK(check_network_participation, NET_PARTICIPANT, 0),
1393 
1394  /* We need to do these if we're participating in the Tor network, and
1395  * immediately before we stop. */
1396  CALLBACK(clean_caches, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1397  CALLBACK(save_state, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1398  CALLBACK(write_stats_file, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1399  CALLBACK(prune_old_routers, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1400 
1401  /* Hidden Service service only. */
1402  CALLBACK(hs_service, HS_SERVICE, FL(NEED_NET)), // XXXX break this down more
1403 
1404  /* Bridge only. */
1405  CALLBACK(record_bridge_stats, BRIDGE, 0),
1406 
1407  /* Client only. */
1408  /* XXXX this could be restricted to CLIENT+NET_PARTICIPANT */
1409  CALLBACK(rend_cache_failure_clean, NET_PARTICIPANT, FL(RUN_ON_DISABLE)),
1410 
1411  /* Directory server only. */
1412  CALLBACK(clean_consdiffmgr, DIRSERVER, 0),
1413 
1414  /* Controller with per-second events only. */
1415  CALLBACK(control_per_second_events, CONTROLEV, 0),
1416 
1417  END_OF_PERIODIC_EVENTS
1418 };
1419 #undef CALLBACK
1420 #undef FL
1421 
1422 /* These are pointers to members of periodic_events[] that are used to
1423  * implement particular callbacks. We keep them separate here so that we
1424  * can access them by name. We also keep them inside periodic_events[]
1425  * so that we can implement "reset all timers" in a reasonable way. */
1426 static periodic_event_item_t *fetch_networkstatus_event=NULL;
1427 static periodic_event_item_t *launch_descriptor_fetches_event=NULL;
1428 static periodic_event_item_t *check_dns_honesty_event=NULL;
1429 static periodic_event_item_t *save_state_event=NULL;
1430 static periodic_event_item_t *prune_old_routers_event=NULL;
1431 
1437 void
1439 {
1441 }
1442 
1445 STATIC int
1447 {
1448  tor_assert(options);
1449 
1450  int roles = PERIODIC_EVENT_ROLE_ALL;
1451  int is_bridge = options->BridgeRelay;
1452  int is_relay = server_mode(options);
1453  int is_dirauth = authdir_mode_v3(options);
1454  int is_bridgeauth = authdir_mode_bridge(options);
1455  int is_hidden_service = !!hs_service_get_num_services() ||
1456  !!rend_num_services();
1457  int is_dirserver = dir_server_mode(options);
1458  int sending_control_events = control_any_per_second_event_enabled();
1459 
1460  /* We also consider tor to have the role of a client if the ControlPort is
1461  * set because a lot of things can be done over the control port which
1462  * requires tor to have basic functionnalities. */
1463  int is_client = options_any_client_port_set(options) ||
1464  options->ControlPort_set ||
1465  options->OwningControllerFD != UINT64_MAX;
1466 
1467  int is_net_participant = is_participating_on_network() ||
1468  is_relay || is_hidden_service;
1469 
1470  if (is_bridge) roles |= PERIODIC_EVENT_ROLE_BRIDGE;
1471  if (is_client) roles |= PERIODIC_EVENT_ROLE_CLIENT;
1472  if (is_relay) roles |= PERIODIC_EVENT_ROLE_RELAY;
1473  if (is_dirauth) roles |= PERIODIC_EVENT_ROLE_DIRAUTH;
1474  if (is_bridgeauth) roles |= PERIODIC_EVENT_ROLE_BRIDGEAUTH;
1475  if (is_hidden_service) roles |= PERIODIC_EVENT_ROLE_HS_SERVICE;
1476  if (is_dirserver) roles |= PERIODIC_EVENT_ROLE_DIRSERVER;
1477  if (is_net_participant) roles |= PERIODIC_EVENT_ROLE_NET_PARTICIPANT;
1478  if (sending_control_events) roles |= PERIODIC_EVENT_ROLE_CONTROLEV;
1479 
1480  return roles;
1481 }
1482 
1484 static struct event *initialize_periodic_events_event = NULL;
1485 
1491 static void
1492 initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
1493 {
1494  (void) fd;
1495  (void) events;
1496  (void) data;
1497 
1498  tor_event_free(initialize_periodic_events_event);
1499 
1500  rescan_periodic_events(get_options());
1501 }
1502 
1505 void
1507 {
1509  return;
1510 
1512 
1513  for (int i = 0; mainloop_periodic_events[i].name; ++i) {
1514  periodic_events_register(&mainloop_periodic_events[i]);
1515  }
1516 
1517  /* Set up all periodic events. We'll launch them by roles. */
1518 
1519 #define NAMED_CALLBACK(name) \
1520  STMT_BEGIN name ## _event = periodic_events_find( #name ); STMT_END
1521 
1522  NAMED_CALLBACK(prune_old_routers);
1523  NAMED_CALLBACK(fetch_networkstatus);
1524  NAMED_CALLBACK(launch_descriptor_fetches);
1525  NAMED_CALLBACK(check_dns_honesty);
1526  NAMED_CALLBACK(save_state);
1527 }
1528 
1529 STATIC void
1530 teardown_periodic_events(void)
1531 {
1533  fetch_networkstatus_event = NULL;
1534  launch_descriptor_fetches_event = NULL;
1535  check_dns_honesty_event = NULL;
1536  save_state_event = NULL;
1537  prune_old_routers_event = NULL;
1539 }
1540 
1541 static mainloop_event_t *rescan_periodic_events_ev = NULL;
1542 
1544 static void
1546 {
1547  (void)event;
1548  (void)arg;
1549  rescan_periodic_events(get_options());
1550 }
1551 
1556 schedule_rescan_periodic_events,(void))
1557 {
1558  if (!rescan_periodic_events_ev) {
1559  rescan_periodic_events_ev =
1561  }
1562  mainloop_event_activate(rescan_periodic_events_ev);
1563 }
1564 
1567 void
1569 {
1570  tor_assert(options);
1571 
1572  periodic_events_rescan_by_roles(get_my_roles(options), net_is_disabled());
1573 }
1574 
1575 /* We just got new options globally set, see if we need to enabled or disable
1576  * periodic events. */
1577 void
1578 periodic_events_on_new_options(const or_options_t *options)
1579 {
1580  rescan_periodic_events(options);
1581 }
1582 
1587 void
1589 {
1590  tor_assert(fetch_networkstatus_event);
1591  tor_assert(launch_descriptor_fetches_event);
1592 
1593  periodic_event_reschedule(fetch_networkstatus_event);
1594  periodic_event_reschedule(launch_descriptor_fetches_event);
1595 }
1596 
1599 static void
1601 {
1602  (void)ev;
1603  (void)arg;
1608 }
1609 
1612 
1615 void
1617 {
1618  if (PREDICT_UNLIKELY(postloop_cleanup_ev == NULL)) {
1619  // (It's possible that we can get here if we decide to close a connection
1620  // in the earliest stages of our configuration, before we create events.)
1621  return;
1622  }
1624 }
1625 
1628 
1630 static void
1632 {
1633  (void)ev;
1634  (void)arg;
1635  log_notice(LD_GENERAL, "Clean shutdown finished. Exiting.");
1637 }
1638 
1640 void
1642 {
1643  const struct timeval delay_tv = { delay_sec, 0 };
1644  if (! scheduled_shutdown_ev) {
1646  }
1648 }
1649 
1653 static int
1654 second_elapsed_callback(time_t now, const or_options_t *options)
1655 {
1656  /* 0. See if our bandwidth limits are exhausted and we should hibernate
1657  *
1658  * Note: we have redundant mechanisms to handle the case where it's
1659  * time to wake up from hibernation; or where we have a scheduled
1660  * shutdown and it's time to run it, but this will also handle those.
1661  */
1662  consider_hibernation(now);
1663 
1664  /* Maybe enough time elapsed for us to reconsider a circuit. */
1666 
1667  if (options->UseBridges && !net_is_disabled()) {
1668  /* Note: this check uses net_is_disabled(), not should_delay_dir_fetches()
1669  * -- the latter is only for fetching consensus-derived directory info. */
1670  // TODO: client
1671  // Also, schedule this rather than probing 1x / sec
1672  fetch_bridge_descriptors(options, now);
1673  }
1674 
1675  if (accounting_is_enabled(options)) {
1676  // TODO: refactor or rewrite?
1678  }
1679 
1680  /* 3a. Every second, we examine pending circuits and prune the
1681  * ones which have been pending for more than a few seconds.
1682  * We do this before step 4, so it can try building more if
1683  * it's not comfortable with the number of available circuits.
1684  */
1685  /* (If our circuit build timeout can ever become lower than a second (which
1686  * it can't, currently), we should do this more often.) */
1687  // TODO: All expire stuff can become NET_PARTICIPANT, RUN_ON_DISABLE
1690 
1691  /* 3b. Also look at pending streams and prune the ones that 'began'
1692  * a long time ago but haven't gotten a 'connected' yet.
1693  * Do this before step 4, so we can put them back into pending
1694  * state to be picked up by the new circuit.
1695  */
1697 
1698  /* 3c. And expire connections that we've held open for too long.
1699  */
1701 
1702  /* 4. Every second, we try a new circuit if there are no valid
1703  * circuits. Every NewCircuitPeriod seconds, we expire circuits
1704  * that became dirty more than MaxCircuitDirtiness seconds ago,
1705  * and we make a new circ if there are no clean circuits.
1706  */
1707  const int have_dir_info = router_have_minimum_dir_info();
1708  if (have_dir_info && !net_is_disabled()) {
1710  } else {
1712  }
1713 
1714  /* 5. We do housekeeping for each connection... */
1716  int i;
1717  for (i=0;i<smartlist_len(connection_array);i++) {
1719  }
1720 
1721  /* Run again in a second. */
1722  return 1;
1723 }
1724 
1730 static int
1732 {
1733  if (should_delay_dir_fetches(options, NULL))
1734  return PERIODIC_EVENT_NO_UPDATE;
1735 
1738  if (router_have_minimum_dir_info())
1740  else
1742 }
1743 
1748 static int
1750 {
1751  static int first = 1;
1752  (void)now;
1753  (void)options;
1754  if (first) {
1755  first = 0;
1757  }
1758 
1759  /* 1b. Every MAX_SSL_KEY_LIFETIME_INTERNAL seconds, we change our
1760  * TLS context. */
1761  log_info(LD_GENERAL,"Rotating tls context.");
1762  if (router_initialize_tls_context() < 0) {
1763  log_err(LD_BUG, "Error reinitializing TLS context");
1764  tor_assert_unreached();
1765  }
1766  if (generate_ed_link_cert(options, now, 1)) {
1767  log_err(LD_OR, "Unable to update Ed25519->TLS link certificate for "
1768  "new TLS context.");
1769  tor_assert_unreached();
1770  }
1771 
1772  /* We also make sure to rotate the TLS connections themselves if they've
1773  * been up for too long -- but that's done via is_bad_for_new_circs in
1774  * run_connection_housekeeping() above. */
1776 }
1777 
1782 static int
1783 add_entropy_callback(time_t now, const or_options_t *options)
1784 {
1785  (void)now;
1786  (void)options;
1787  /* We already seeded once, so don't die on failure. */
1788  if (crypto_seed_rng() < 0) {
1789  log_warn(LD_GENERAL, "Tried to re-seed RNG, but failed. We already "
1790  "seeded once, though, so we won't exit here.");
1791  }
1792 
1794 #define ENTROPY_INTERVAL (60*60)
1795  return ENTROPY_INTERVAL;
1796 }
1797 
1800 STATIC int
1802 {
1803  /* If we're a server, we can't become dormant. */
1804  if (server_mode(options)) {
1805  goto found_activity;
1806  }
1807 
1808  /* If we're running an onion service, we can't become dormant. */
1809  /* XXXX this would be nice to change, so that we can be dormant with a
1810  * service. */
1811  if (hs_service_get_num_services() || rend_num_services()) {
1812  goto found_activity;
1813  }
1814 
1815  /* If we have any currently open entry streams other than "linked"
1816  * connections used for directory requests, those count as user activity.
1817  */
1818  if (options->DormantTimeoutDisabledByIdleStreams) {
1819  if (connection_get_by_type_nonlinked(CONN_TYPE_AP) != NULL) {
1820  goto found_activity;
1821  }
1822  }
1823 
1824  /* XXXX Make this configurable? */
1826 #define CHECK_PARTICIPATION_INTERVAL (5*60)
1827 
1828  /* Become dormant if there has been no user activity in a long time.
1829  * (The funny checks below are in order to prevent overflow.) */
1830  time_t time_since_last_activity = 0;
1831  if (get_last_user_activity_time() < now)
1832  time_since_last_activity = now - get_last_user_activity_time();
1833  if (time_since_last_activity >= options->DormantClientTimeout) {
1834  log_notice(LD_GENERAL, "No user activity in a long time: becoming"
1835  " dormant.");
1836  set_network_participation(false);
1837  rescan_periodic_events(options);
1838  }
1839 
1840  return CHECK_PARTICIPATION_INTERVAL;
1841 
1842  found_activity:
1843  note_user_activity(now);
1844  return CHECK_PARTICIPATION_INTERVAL;
1845 }
1846 
1851 static int
1853 {
1854  (void)options;
1855  /* Check whether our networkstatus has expired. */
1856  networkstatus_t *ns = networkstatus_get_latest_consensus();
1857  /* Use reasonably live consensuses until they are no longer reasonably live.
1858  */
1859  if (ns && !networkstatus_consensus_reasonably_live(ns, now) &&
1860  router_have_minimum_dir_info()) {
1862  }
1863 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
1864  return CHECK_EXPIRED_NS_INTERVAL;
1865 }
1866 
1870 static int
1871 save_state_callback(time_t now, const or_options_t *options)
1872 {
1873  (void) options;
1874  (void) or_state_save(now); // only saves if appropriate
1875  const time_t next_write = get_or_state()->next_write;
1876  if (next_write == TIME_MAX) {
1877  return 86400;
1878  }
1879  return safe_timer_diff(now, next_write);
1880 }
1881 
1885 void
1887 {
1888  if (save_state_event == NULL) {
1889  /* This can happen early on during startup. */
1890  return;
1891  }
1892  periodic_event_reschedule(save_state_event);
1893 }
1894 
1898 static int
1899 write_stats_file_callback(time_t now, const or_options_t *options)
1900 {
1901  /* 1g. Check whether we should write statistics to disk.
1902  */
1903 #define CHECK_WRITE_STATS_INTERVAL (60*60)
1904  time_t next_time_to_write_stats_files = now + CHECK_WRITE_STATS_INTERVAL;
1905  if (options->CellStatistics) {
1906  time_t next_write =
1908  if (next_write && next_write < next_time_to_write_stats_files)
1909  next_time_to_write_stats_files = next_write;
1910  }
1911  if (options->DirReqStatistics) {
1912  time_t next_write = geoip_dirreq_stats_write(now);
1913  if (next_write && next_write < next_time_to_write_stats_files)
1914  next_time_to_write_stats_files = next_write;
1915  }
1916  if (options->EntryStatistics) {
1917  time_t next_write = geoip_entry_stats_write(now);
1918  if (next_write && next_write < next_time_to_write_stats_files)
1919  next_time_to_write_stats_files = next_write;
1920  }
1921  if (options->HiddenServiceStatistics) {
1922  time_t next_write = rep_hist_hs_stats_write(now);
1923  if (next_write && next_write < next_time_to_write_stats_files)
1924  next_time_to_write_stats_files = next_write;
1925  }
1926  if (options->ExitPortStatistics) {
1927  time_t next_write = rep_hist_exit_stats_write(now);
1928  if (next_write && next_write < next_time_to_write_stats_files)
1929  next_time_to_write_stats_files = next_write;
1930  }
1931  if (options->ConnDirectionStatistics) {
1932  time_t next_write = rep_hist_conn_stats_write(now);
1933  if (next_write && next_write < next_time_to_write_stats_files)
1934  next_time_to_write_stats_files = next_write;
1935  }
1936  if (options->BridgeAuthoritativeDir) {
1937  time_t next_write = rep_hist_desc_stats_write(now);
1938  if (next_write && next_write < next_time_to_write_stats_files)
1939  next_time_to_write_stats_files = next_write;
1940  }
1941 
1942  return safe_timer_diff(now, next_time_to_write_stats_files);
1943 }
1944 
1945 static int
1946 reset_padding_counts_callback(time_t now, const or_options_t *options)
1947 {
1948  if (options->PaddingStatistics) {
1949  rep_hist_prep_published_padding_counts(now);
1950  }
1951 
1954 }
1955 
1956 static int should_init_bridge_stats = 1;
1957 
1961 static int
1962 record_bridge_stats_callback(time_t now, const or_options_t *options)
1963 {
1964  /* 1h. Check whether we should write bridge statistics to disk.
1965  */
1966  if (should_record_bridge_info(options)) {
1967  if (should_init_bridge_stats) {
1968  /* (Re-)initialize bridge statistics. */
1970  should_init_bridge_stats = 0;
1971  return WRITE_STATS_INTERVAL;
1972  } else {
1973  /* Possibly write bridge statistics to disk and ask when to write
1974  * them next time. */
1975  time_t next = geoip_bridge_stats_write(now);
1976  return safe_timer_diff(now, next);
1977  }
1978  } else if (!should_init_bridge_stats) {
1979  /* Bridge mode was turned off. Ensure that stats are re-initialized
1980  * next time bridge mode is turned on. */
1981  should_init_bridge_stats = 1;
1982  }
1983  return PERIODIC_EVENT_NO_UPDATE;
1984 }
1985 
1989 static int
1990 clean_caches_callback(time_t now, const or_options_t *options)
1991 {
1992  /* Remove old information from rephist and the rend cache. */
1993  rep_history_clean(now - options->RephistTrackTime);
1994  rend_cache_clean(now, REND_CACHE_TYPE_SERVICE);
1995  hs_cache_clean_as_client(now);
1996  hs_cache_clean_as_dir(now);
1997  microdesc_cache_rebuild(NULL, 0);
1998 #define CLEAN_CACHES_INTERVAL (30*60)
1999  return CLEAN_CACHES_INTERVAL;
2000 }
2001 
2006 static int
2008 {
2009  (void)options;
2010  /* We don't keep entries that are more than five minutes old so we try to
2011  * clean it as soon as we can since we want to make sure the client waits
2012  * as little as possible for reachability reasons. */
2014  hs_cache_client_intro_state_clean(now);
2015  return 30;
2016 }
2017 
2021 static int
2022 prune_old_routers_callback(time_t now, const or_options_t *options)
2023 {
2024 #define ROUTERLIST_PRUNING_INTERVAL (60*60) // 1 hour.
2025  (void)now;
2026  (void)options;
2027 
2028  if (!net_is_disabled()) {
2029  /* If any networkstatus documents are no longer recent, we need to
2030  * update all the descriptors' running status. */
2031  /* Remove dead routers. */
2032  log_debug(LD_GENERAL, "Pruning routerlist...");
2034  }
2035 
2036  return ROUTERLIST_PRUNING_INTERVAL;
2037 }
2038 
2043 static int
2044 fetch_networkstatus_callback(time_t now, const or_options_t *options)
2045 {
2046  /* How often do we check whether we should download network status
2047  * documents? */
2048  const int we_are_bootstrapping = networkstatus_consensus_is_bootstrapping(
2049  now);
2050  const int prefer_mirrors = !directory_fetches_from_authorities(
2051  get_options());
2052  int networkstatus_dl_check_interval = 60;
2053  /* check more often when testing, or when bootstrapping from mirrors
2054  * (connection limits prevent too many connections being made) */
2055  if (options->TestingTorNetwork
2056  || (we_are_bootstrapping && prefer_mirrors)) {
2057  networkstatus_dl_check_interval = 1;
2058  }
2059 
2060  if (should_delay_dir_fetches(options, NULL))
2061  return PERIODIC_EVENT_NO_UPDATE;
2062 
2064  return networkstatus_dl_check_interval;
2065 }
2066 
2069 static int
2070 retry_listeners_callback(time_t now, const or_options_t *options)
2071 {
2072  (void)now;
2073  (void)options;
2074  if (!net_is_disabled()) {
2075  retry_all_listeners(NULL, 0);
2076  return 60;
2077  }
2078  return PERIODIC_EVENT_NO_UPDATE;
2079 }
2080 
2081 static int heartbeat_callback_first_time = 1;
2082 
2089 static int
2090 heartbeat_callback(time_t now, const or_options_t *options)
2091 {
2092  /* Check if heartbeat is disabled */
2093  if (!options->HeartbeatPeriod) {
2094  return PERIODIC_EVENT_NO_UPDATE;
2095  }
2096 
2097  /* Skip the first one. */
2098  if (heartbeat_callback_first_time) {
2099  heartbeat_callback_first_time = 0;
2100  return options->HeartbeatPeriod;
2101  }
2102 
2103  /* Write the heartbeat message */
2104  if (log_heartbeat(now) == 0) {
2105  return options->HeartbeatPeriod;
2106  } else {
2107  /* If we couldn't write the heartbeat log message, try again in the minimum
2108  * interval of time. */
2109  return MIN_HEARTBEAT_PERIOD;
2110  }
2111 }
2112 
2113 #define CDM_CLEAN_CALLBACK_INTERVAL 600
2114 static int
2115 clean_consdiffmgr_callback(time_t now, const or_options_t *options)
2116 {
2117  (void)now;
2118  if (dir_server_mode(options)) {
2119  consdiffmgr_cleanup();
2120  }
2121  return CDM_CLEAN_CALLBACK_INTERVAL;
2122 }
2123 
2124 /*
2125  * Periodic callback: Run scheduled events for HS service. This is called
2126  * every second.
2127  */
2128 static int
2129 hs_service_callback(time_t now, const or_options_t *options)
2130 {
2131  (void) options;
2132 
2133  /* We need to at least be able to build circuits and that we actually have
2134  * a working network. */
2135  if (!have_completed_a_circuit() || net_is_disabled() ||
2136  networkstatus_get_live_consensus(now) == NULL) {
2137  goto end;
2138  }
2139 
2140  hs_service_run_scheduled_events(now);
2141 
2142  end:
2143  /* Every 1 second. */
2144  return 1;
2145 }
2146 
2147 /*
2148  * Periodic callback: Send once-per-second events to the controller(s).
2149  * This is called every second.
2150  */
2151 static int
2152 control_per_second_events_callback(time_t now, const or_options_t *options)
2153 {
2154  (void) options;
2155  (void) now;
2156 
2158 
2159  return 1;
2160 }
2161 
2163 static time_t current_second = 0;
2165 static monotime_coarse_t current_second_last_changed;
2166 
2172 void
2174 {
2175  if (PREDICT_LIKELY(now == current_second)) {
2176  /* We call this function a lot. Most frequently, the current second
2177  * will not have changed, so we just return. */
2178  return;
2179  }
2180 
2181  const time_t seconds_elapsed = current_second ? (now - current_second) : 0;
2182 
2183  /* Check the wall clock against the monotonic clock, so we can
2184  * better tell idleness from clock jumps and/or other shenanigans. */
2185  monotime_coarse_t last_updated;
2186  memcpy(&last_updated, &current_second_last_changed, sizeof(last_updated));
2187  monotime_coarse_get(&current_second_last_changed);
2188 
2191 #define NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE 20
2192 
2193  /* Don't go dormant early or late just because we jumped in time. */
2194  if (ABS(seconds_elapsed) >= NUM_JUMPED_SECONDS_BEFORE_NETSTATUS_UPDATE) {
2195  if (is_participating_on_network()) {
2196  netstatus_note_clock_jumped(seconds_elapsed);
2197  }
2198  }
2199 
2201 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
2202 
2204 #define NUM_IDLE_SECONDS_BEFORE_WARN 3600
2205 
2206  if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN) {
2207  // moving back in time is always a bad sign.
2208  circuit_note_clock_jumped(seconds_elapsed, false);
2209 
2210  } else if (seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
2211  /* Compare the monotonic clock to the result of time(). */
2212  const int32_t monotime_msec_passed =
2213  monotime_coarse_diff_msec32(&last_updated,
2215  const int monotime_sec_passed = monotime_msec_passed / 1000;
2216  const int discrepancy = monotime_sec_passed - (int)seconds_elapsed;
2217  /* If the monotonic clock deviates from time(NULL), we have a couple of
2218  * possibilities. On some systems, this means we have been suspended or
2219  * sleeping. Everywhere, it can mean that the wall-clock time has
2220  * been changed -- for example, with settimeofday().
2221  *
2222  * On the other hand, if the monotonic time matches with the wall-clock
2223  * time, we've probably just been idle for a while, with no events firing.
2224  * we tolerate much more of that.
2225  */
2226  const bool clock_jumped = abs(discrepancy) > 2;
2227 
2228  if (clock_jumped || seconds_elapsed >= NUM_IDLE_SECONDS_BEFORE_WARN) {
2229  circuit_note_clock_jumped(seconds_elapsed, ! clock_jumped);
2230  }
2231  } else if (seconds_elapsed > 0) {
2232  stats_n_seconds_working += seconds_elapsed;
2233  }
2234 
2235  update_approx_time(now);
2236  current_second = now;
2237 }
2238 
2239 #ifdef HAVE_SYSTEMD_209
2240 static periodic_timer_t *systemd_watchdog_timer = NULL;
2241 
2243 static void
2244 systemd_watchdog_callback(periodic_timer_t *timer, void *arg)
2245 {
2246  (void)timer;
2247  (void)arg;
2248  sd_notify(0, "WATCHDOG=1");
2249 }
2250 #endif /* defined(HAVE_SYSTEMD_209) */
2251 
2252 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
2253 
2257 void
2258 ip_address_changed(int at_interface)
2259 {
2260  const or_options_t *options = get_options();
2261  int server = server_mode(options);
2262  int exit_reject_interfaces = (server && options->ExitRelay
2263  && options->ExitPolicyRejectLocalInterfaces);
2264 
2265  if (at_interface) {
2266  if (! server) {
2267  /* Okay, change our keys. */
2268  if (init_keys_client() < 0)
2269  log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
2270  }
2271  } else {
2272  if (server) {
2273  if (get_uptime() > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
2275  reset_uptime();
2277  }
2278  }
2279 
2280  /* Exit relays incorporate interface addresses in their exit policies when
2281  * ExitPolicyRejectLocalInterfaces is set */
2282  if (exit_reject_interfaces || (server && !at_interface)) {
2283  mark_my_descriptor_dirty("IP address changed");
2284  }
2285 
2287 }
2288 
2291 void
2293 {
2294  if (server_mode(get_options())) {
2296  if (check_dns_honesty_event) {
2297  periodic_event_reschedule(check_dns_honesty_event);
2298  }
2299  }
2300 }
2301 
2303 void
2305 {
2309  }
2310  if (!postloop_cleanup_ev) {
2313  }
2314 }
2315 
2317 int
2319 {
2320  /* initialize the periodic events first, so that code that depends on the
2321  * events being present does not assert.
2322  */
2325 
2327 
2328  struct timeval one_second = { 1, 0 };
2329  initialize_periodic_events_event = tor_evtimer_new(
2330  tor_libevent_get_base(),
2332  event_add(initialize_periodic_events_event, &one_second);
2333 
2334 #ifdef HAVE_SYSTEMD_209
2335  uint64_t watchdog_delay;
2336  /* set up systemd watchdog notification. */
2337  if (sd_watchdog_enabled(1, &watchdog_delay) > 0) {
2338  if (! systemd_watchdog_timer) {
2339  struct timeval watchdog;
2340  /* The manager will "act on" us if we don't send them a notification
2341  * every 'watchdog_delay' microseconds. So, send notifications twice
2342  * that often. */
2343  watchdog_delay /= 2;
2344  watchdog.tv_sec = watchdog_delay / 1000000;
2345  watchdog.tv_usec = watchdog_delay % 1000000;
2346 
2347  systemd_watchdog_timer = periodic_timer_new(tor_libevent_get_base(),
2348  &watchdog,
2349  systemd_watchdog_callback,
2350  NULL);
2351  tor_assert(systemd_watchdog_timer);
2352  }
2353  }
2354 #endif /* defined(HAVE_SYSTEMD_209) */
2355 #ifdef ENABLE_RESTART_DEBUGGING
2356  {
2357  static int first_time = 1;
2358 
2359  if (first_time && getenv("TOR_DEBUG_RESTART")) {
2360  first_time = 0;
2361  const char *sec_str = getenv("TOR_DEBUG_RESTART_AFTER_SECONDS");
2362  long sec;
2363  int sec_ok=0;
2364  if (sec_str &&
2365  (sec = tor_parse_long(sec_str, 10, 0, INT_MAX, &sec_ok, NULL)) &&
2366  sec_ok) {
2367  /* Okay, we parsed the seconds. */
2368  } else {
2369  sec = 5;
2370  }
2371  struct timeval restart_after = { (time_t) sec, 0 };
2372  tor_shutdown_event_loop_for_restart_event =
2373  tor_evtimer_new(tor_libevent_get_base(),
2374  tor_shutdown_event_loop_for_restart_cb, NULL);
2375  event_add(tor_shutdown_event_loop_for_restart_event, &restart_after);
2376  }
2377  }
2378 #endif /* defined(ENABLE_RESTART_DEBUGGING) */
2379 
2380  return run_main_loop_until_done();
2381 }
2382 
2383 #ifndef _WIN32
2384 
2385 static ratelim_t libevent_error_ratelim = RATELIM_INIT(10);
2386 #endif
2387 
2392 static int
2394 {
2395  int loop_result;
2396 
2397  if (nt_service_is_stopping())
2398  return 0;
2399 
2401  return 0;
2402 
2403 #ifndef _WIN32
2404  /* Make it easier to tell whether libevent failure is our fault or not. */
2405  errno = 0;
2406 #endif
2407 
2408  if (get_options()->MainloopStats) {
2409  /* We always enforce that EVLOOP_ONCE is passed to event_base_loop() if we
2410  * are collecting main loop statistics. */
2411  called_loop_once = 1;
2412  } else {
2413  called_loop_once = 0;
2414  }
2415 
2416  /* Make sure we know (about) what time it is. */
2417  update_approx_time(time(NULL));
2418 
2419  /* Here it is: the main loop. Here we tell Libevent to poll until we have
2420  * an event, or the second ends, or until we have some active linked
2421  * connections to trigger events for. Libevent will wait till one
2422  * of these happens, then run all the appropriate callbacks. */
2423  loop_result = tor_libevent_run_event_loop(tor_libevent_get_base(),
2425 
2426  if (get_options()->MainloopStats) {
2427  /* Update our main loop counters. */
2428  if (loop_result == 0) {
2429  // The call was successful.
2431  } else if (loop_result == -1) {
2432  // The call was erroneous.
2434  } else if (loop_result == 1) {
2435  // The call didn't have any active or pending events
2436  // to handle.
2438  }
2439  }
2440 
2441  /* Oh, the loop failed. That might be an error that we need to
2442  * catch, but more likely, it's just an interrupted poll() call or something,
2443  * and we should try again. */
2444  if (loop_result < 0) {
2445  int e = tor_socket_errno(-1);
2446  /* let the program survive things like ^z */
2447  if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
2448  log_err(LD_NET,"libevent call with %s failed: %s [%d]",
2449  tor_libevent_get_method(), tor_socket_strerror(e), e);
2450  return -1;
2451 #ifndef _WIN32
2452  } else if (e == EINVAL) {
2454  "EINVAL from libevent: should you upgrade libevent?");
2455  if (libevent_error_ratelim.n_calls_since_last_time > 8) {
2456  log_err(LD_NET, "Too many libevent errors, too fast: dying");
2457  return -1;
2458  }
2459 #endif /* !defined(_WIN32) */
2460  } else {
2461  tor_assert_nonfatal_once(! ERRNO_IS_EINPROGRESS(e));
2462  log_debug(LD_NET,"libevent call interrupted.");
2463  /* You can't trust the results of this poll(). Go back to the
2464  * top of the big for loop. */
2465  return 1;
2466  }
2467  }
2468 
2470  return 0;
2471 
2472  return 1;
2473 }
2474 
2480 STATIC int
2482 {
2483  int loop_result = 1;
2484 
2487 
2488  do {
2489  loop_result = run_main_loop_once();
2490  } while (loop_result == 1);
2491 
2493  return main_loop_exit_value;
2494  else
2495  return loop_result;
2496 }
2497 
2500 get_uptime,(void))
2501 {
2502  return stats_n_seconds_working;
2503 }
2504 
2507 reset_uptime,(void))
2508 {
2510 }
2511 
2512 void
2513 tor_mainloop_free_all(void)
2514 {
2515  smartlist_free(connection_array);
2516  smartlist_free(closeable_connection_lst);
2517  smartlist_free(active_linked_connection_lst);
2518  teardown_periodic_events();
2519  tor_event_free(shutdown_did_not_work_event);
2520  tor_event_free(initialize_periodic_events_event);
2521  mainloop_event_free(directory_all_unreachable_cb_event);
2522  mainloop_event_free(schedule_active_linked_connections_event);
2523  mainloop_event_free(postloop_cleanup_ev);
2524  mainloop_event_free(handle_deferred_signewnym_ev);
2525  mainloop_event_free(scheduled_shutdown_ev);
2526  mainloop_event_free(rescan_periodic_events_ev);
2527 
2528 #ifdef HAVE_SYSTEMD_209
2529  periodic_timer_free(systemd_watchdog_timer);
2530 #endif
2531 
2533 
2534  memset(&global_bucket, 0, sizeof(global_bucket));
2535  memset(&global_relayed_bucket, 0, sizeof(global_relayed_bucket));
2539  newnym_epoch = 0;
2540  called_loop_once = 0;
2544  quiet_level = 0;
2545  should_init_bridge_stats = 1;
2546  heartbeat_callback_first_time = 1;
2547  current_second = 0;
2548  memset(&current_second_last_changed, 0,
2549  sizeof(current_second_last_changed));
2550 }
tor_socket_t s
Definition: connection_st.h:88
static int rend_cache_failure_clean_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2007
Header file for dirserv.c.
static int connection_should_read_from_linked_conn(connection_t *conn)
Definition: mainloop.c:715
Header for statefile.c.
STATIC int run_main_loop_until_done(void)
Definition: mainloop.c:2481
static int run_main_loop_once(void)
Definition: mainloop.c:2393
time_t time_of_process_start
Definition: mainloop.c:142
void update_all_descriptor_downloads(time_t now)
Definition: routerlist.c:2216
int ExitPolicyRejectLocalInterfaces
static void handle_deferred_signewnym_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1297
uint64_t get_main_loop_error_count(void)
Definition: mainloop.c:548
static void connection_unlink(connection_t *conn)
Definition: mainloop.c:340
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:1088
unsigned int channel_num_circuits(channel_t *chan)
Definition: channel.c:3346
void connection_or_close_normally(or_connection_t *orconn, int flush)
int rend_num_services(void)
Definition: rendservice.c:184
int connection_remove(connection_t *conn)
Definition: mainloop.c:297
int ExitPortStatistics
Header file for circuitbuild.c.
void periodic_events_connect_all(void)
Definition: periodic.c:230
Common functions for using (pseudo-)random number generators.
int connection_is_moribund(connection_t *conn)
Definition: connection.c:5101
static void increment_main_loop_error_count(void)
Definition: mainloop.c:541
Header file containing service data for the HS subsytem.
void assert_connection_edge_not_dns_pending(edge_connection_t *conn)
Definition: dns.c:966
#define SMARTLIST_FOREACH_BEGIN(sl, type, var)
Header for backtrace.c.
uint64_t get_main_loop_idle_count(void)
Definition: mainloop.c:562
int connection_dir_reached_eof(dir_connection_t *conn)
Definition: dirclient.c:2988
void update_current_time(time_t now)
Definition: mainloop.c:2173
Header file for geoip_stats.c.
static void rescan_periodic_events_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1545
int dir_server_mode(const or_options_t *options)
Definition: routermode.c:20
int quiet_level
Definition: main.c:116
Header file containing client data for the HS subsytem.
Header for buffers_tls.c.
static int clean_caches_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1990
const char * name
Definition: periodic.h:63
void router_dir_info_changed(void)
Definition: nodelist.c:2330
Header file for connection.c.
static int rotate_x509_certificate_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1749
void connection_or_connect_failed(or_connection_t *conn, int reason, const char *msg)
void connection_stop_reading_from_linked_conn(connection_t *conn)
Definition: mainloop.c:815
int guards_update_all(void)
Definition: entrynodes.c:3603
Definition: cell_st.h:12
#define LD_GENERAL
Definition: log.h:59
static void schedule_active_linked_connections_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:387
int buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz, size_t *buf_flushlen)
Definition: buffers_net.c:231
unsigned int proxy_state
Definition: connection_st.h:84
static uint64_t stats_n_bytes_written
Definition: mainloop.c:140
uint8_t state
Definition: connection_st.h:44
#define LOG_INFO
Definition: log.h:42
Header file for nodelist.c.
void reschedule_or_state_save(void)
Definition: mainloop.c:1886
void tor_log(int severity, log_domain_mask_t domain, const char *format,...)
Definition: log.c:632
void stats_increment_bytes_read_and_written(uint64_t r, uint64_t w)
Definition: mainloop.c:483
static time_t time_of_last_signewnym
Definition: mainloop.c:155
Header file for ntmain.c.
int DormantTimeoutDisabledByIdleStreams
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:1962
void note_that_we_maybe_cant_complete_circuits(void)
Definition: mainloop.c:234
int any_predicted_circuits(time_t now)
unsigned get_signewnym_epoch(void)
Definition: mainloop.c:1331
#define ABS(x)
Definition: cmp.h:40
#define GREEDY_DESCRIPTOR_RETRY_INTERVAL
Definition: mainloop.c:194
char address[MAX_SOCKS_ADDR_LEN]
static int heartbeat_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2090
void note_that_we_completed_a_circuit(void)
Definition: mainloop.c:226
int router_initialize_tls_context(void)
Definition: router.c:791
void circuit_expire_building(void)
Definition: circuituse.c:456
int authdir_mode_bridge(const or_options_t *options)
Definition: authmode.c:67
int HiddenServiceStatistics
int smartlist_contains(const smartlist_t *sl, const void *element)
static smartlist_t * active_linked_connection_lst
Definition: mainloop.c:170
Header file for config.c.
static struct event * shutdown_did_not_work_event
Definition: mainloop.c:727
#define CONN_TYPE_OR
Definition: connection.h:24
void router_do_reachability_checks(int test_or, int test_dir)
Definition: selftest.c:172
const char * conn_type_to_string(int type)
Definition: connection.c:243
static int check_expired_networkstatus_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1852
int tor_libevent_run_event_loop(struct event_base *base, int once)
struct connection_t * linked_conn
Header file for microdesc.c.
void initialize_periodic_events(void)
Definition: mainloop.c:1506
unsigned int writing_to_linked_conn
Definition: connection_st.h:75
Header file for cpuworker.c.
void rep_history_clean(time_t before)
Definition: rephist.c:593
int directory_fetches_from_authorities(const or_options_t *options)
Definition: dirserv.c:77
unsigned int reading_from_linked_conn
Definition: connection_st.h:73
uint16_t port
int control_event_signal(uintptr_t signal_num)
int should_record_bridge_info(const or_options_t *options)
Definition: geoip_stats.c:112
void mainloop_schedule_postloop_cleanup(void)
Definition: mainloop.c:1616
int directory_too_idle_to_fetch_descriptors(const or_options_t *options, time_t now)
Definition: dirserv.c:169
#define tor_free(p)
Definition: malloc.h:52
int do_main_loop(void)
Definition: mainloop.c:2318
void rend_cache_clean(time_t now, rend_cache_type_t cache_type)
Definition: rendcache.c:278
void tor_shutdown_event_loop_and_exit(int exitcode)
Definition: mainloop.c:764
#define tor_fragile_assert()
Definition: util_bug.h:241
void initialize_mainloop_events(void)
Definition: mainloop.c:2304
#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:47
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:508
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:1338
const char * conn_state_to_string(int type, int state)
Definition: connection.c:275
int networkstatus_consensus_reasonably_live(const networkstatus_t *consensus, time_t now)
Header file for mainloop.c.
void accounting_run_housekeeping(time_t now)
Definition: hibernate.c:585
void mainloop_event_activate(mainloop_event_t *event)
uint64_t get_main_loop_success_count(void)
Definition: mainloop.c:534
void dns_reset_correctness_checks(void)
Definition: dns.c:2080
void router_reset_reachability(void)
Definition: selftest.c:49
void channel_update_bad_for_new_circs(const char *digest, int force)
Definition: channel.c:3468
unsigned int purpose
Definition: connection_st.h:46
void connection_or_clear_identity(or_connection_t *conn)
edge_connection_t * TO_EDGE_CONN(connection_t *c)
static struct event * initialize_periodic_events_event
Definition: mainloop.c:1484
or_connection_t * TO_OR_CONN(connection_t *c)
static int conn_close_if_marked(int i)
Definition: mainloop.c:943
Header file for dnsserv.c.
size_t outbuf_flushlen
Definition: connection_st.h:96
time_t geoip_bridge_stats_write(time_t now)
Definition: geoip_stats.c:1237
void connection_unregister_events(connection_t *conn)
Definition: mainloop.c:275
unsigned int linked
Definition: connection_st.h:70
unsigned int active_on_link
Definition: connection_st.h:78
Header file for directory authority mode.
time_t timestamp_lastempty
void reset_all_main_loop_timers(void)
Definition: mainloop.c:1438
int retry_all_listeners(smartlist_t *new_conns, int close_all_noncontrol)
Definition: connection.c:2899
void periodic_events_register(periodic_event_item_t *item)
Definition: periodic.c:215
int connection_wants_to_flush(connection_t *conn)
Definition: connection.c:3913
int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
Definition: buffers.c:654
#define SOCKET_OK(s)
Definition: nettypes.h:39
int connection_is_on_closeable_list(connection_t *conn)
Definition: mainloop.c:435
static ratelim_t libevent_error_ratelim
Definition: mainloop.c:2385
socks_request_t * socks_request
int safe_timer_diff(time_t now, time_t next)
Definition: periodic.c:347
Header file for hibernate.c.
static int periodic_events_initialized
Definition: mainloop.c:1338
time_t rep_hist_conn_stats_write(time_t now)
Definition: rephist.c:2408
static smartlist_t * closeable_connection_lst
Definition: mainloop.c:167
void consider_hibernation(time_t now)
Definition: hibernate.c:1098
#define MIN_HEARTBEAT_PERIOD
Definition: config.h:24
struct buf_t * inbuf
Definition: connection_st.h:93
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:1106
static monotime_coarse_t current_second_last_changed
Definition: mainloop.c:2165
#define DIR_PURPOSE_FETCH_SERVERDESC
Definition: directory.h:38
tor_assert(buffer)
static void initialize_periodic_events_cb(evutil_socket_t fd, short events, void *data)
Definition: mainloop.c:1492
#define LD_CONTROL
Definition: log.h:77
static void run_connection_housekeeping(int i, time_t now)
Definition: mainloop.c:1147
static int called_loop_once
Definition: mainloop.c:174
static int prune_old_routers_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2022
void control_per_second_events(void)
Header file for routermode.c.
int TestingDirConnectionMaxStall
dir_connection_t * TO_DIR_CONN(connection_t *c)
Definition: directory.c:77
void channel_run_cleanup(void)
Definition: channel.c:2130
int conn_array_index
Definition: connection_st.h:89
Header file for rendcache.c.
int log_heartbeat(time_t now)
Definition: status.c:119
int control_event_general_error(const char *format,...)
Header file for circuitbuild.c.
Master header file for Tor-specific functionality.
enum watchable_events watchable_events_t
void periodic_events_reset_all(void)
Definition: periodic.c:250
time_t geoip_entry_stats_write(time_t now)
Definition: geoip_stats.c:1372
periodic_timer_t * periodic_timer_new(struct event_base *base, const struct timeval *tv, void(*cb)(periodic_timer_t *timer, void *data), void *data)
void connection_close_immediate(connection_t *conn)
Definition: connection.c:844
void rend_cache_failure_clean(time_t now)
Definition: rendcache.c:254
int options_any_client_port_set(const or_options_t *options)
Definition: config.c:8503
Header file for circuitbuild.c.
static int signewnym_is_pending
Definition: mainloop.c:157
time_t rep_hist_buffer_stats_write(time_t now)
Definition: rephist.c:2032
#define OR_CONN_STATE_OPEN
Definition: orconn_event.h:53
uint16_t marked_for_close
void circuit_mark_all_unused_circs(void)
Definition: circuitlist.c:2087
#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:735
void periodic_event_reschedule(periodic_event_item_t *event)
Definition: periodic.c:107
static long stats_n_seconds_working
Definition: mainloop.c:144
static void directory_all_unreachable_cb(mainloop_event_t *event, void *arg)
Definition: mainloop.c:1058
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:3348
void add_connection_to_closeable_list(connection_t *conn)
Definition: mainloop.c:424
#define LOG_WARN
Definition: log.h:50
unsigned int type
Definition: connection_st.h:45
void connection_or_write_cell_to_buf(const cell_t *cell, or_connection_t *conn)
void connection_consider_empty_write_buckets(connection_t *conn)
Definition: connection.c:3388
int ConnDirectionStatistics
const char * tor_libevent_get_method(void)
unsigned int edge_has_sent_end
void dns_servers_relaunch_checks(void)
Definition: mainloop.c:2292
#define log_fn_ratelim(ratelim, severity, domain, args,...)
Definition: log.h:277
Header file for circuituse.c.
int microdesc_cache_rebuild(microdesc_cache_t *cache, int force)
Definition: microdesc.c:703
Header file for buffers_net.c.
#define MAX_SIGNEWNYM_RATE
Definition: mainloop.c:153
connection_t * connection_get_by_type_state(int type, int state)
Definition: connection.c:4486
static mainloop_event_t * handle_deferred_signewnym_ev
Definition: mainloop.c:159
int connection_in_array(connection_t *conn)
Definition: mainloop.c:442
static int main_loop_exit_value
Definition: mainloop.c:182
time_t timestamp_last_had_circuits
Definition: channel.h:454
void smartlist_del(smartlist_t *sl, int idx)
void mark_my_descriptor_dirty(const char *reason)
Definition: router.c:2427
void do_signewnym(time_t now)
Definition: mainloop.c:1307
Header file for circuitlist.c.
void periodic_events_disconnect_all(void)
Definition: periodic.c:327
void circuit_expire_old_circs_as_needed(time_t now)
Definition: circuituse.c:1361
int BridgeAuthoritativeDir
Header file for rendservice.c.
STATIC void close_closeable_connections(void)
Definition: mainloop.c:833
void routerlist_remove_old_routers(void)
Definition: routerlist.c:1802
Headers for transports.c.
int connection_state_is_open(connection_t *conn)
Definition: connection.c:4642
static void increment_main_loop_idle_count(void)
Definition: mainloop.c:555
#define LD_OR
Definition: log.h:89
int tor_digest_is_zero(const char *digest)
Definition: util_string.c:96
void circuit_expire_waiting_for_better_guard(void)
Definition: circuituse.c:858
int channel_is_bad_for_new_circs(channel_t *chan)
Definition: channel.c:2896
void circuit_close_all_marked(void)
Definition: circuitlist.c:657
static void postloop_cleanup_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1600
void channel_listener_run_cleanup(void)
Definition: channel.c:2156
void mainloop_schedule_shutdown(int delay_sec)
Definition: mainloop.c:1641
ssize_t connection_bucket_write_limit(connection_t *conn, time_t now)
Definition: connection.c:3153
static uint64_t stats_n_main_loop_successes
Definition: mainloop.c:146
int tor_event_loop_shutdown_is_pending(void)
Definition: mainloop.c:788
struct buf_t * outbuf
Definition: connection_st.h:94
const char * get_dir_info_status_string(void)
Definition: nodelist.c:2341
STATIC int check_network_participation_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1801
#define CONN_TYPE_AP_DNS_LISTENER
Definition: connection.h:48
#define LD_DIR
Definition: log.h:85
int generate_ed_link_cert(const or_options_t *options, time_t now, int force)
Definition: routerkeys.c:365
#define LAZY_DESCRIPTOR_RETRY_INTERVAL
Definition: mainloop.c:197
static mainloop_event_t * schedule_active_linked_connections_event
Definition: mainloop.c:378
struct event * write_event
Definition: connection_st.h:92
static int add_entropy_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1783
Header file for connection_edge.c.
void connection_about_to_close_connection(connection_t *conn)
Definition: connection.c:813
void geoip_bridge_stats_init(time_t now)
Definition: geoip_stats.c:1064
void assert_connection_ok(connection_t *conn, time_t now)
Definition: connection.c:5251
static int launch_descriptor_fetches_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1731
static int main_loop_should_exit
Definition: mainloop.c:178
static int can_complete_circuits
Definition: mainloop.c:190
void circuit_mark_all_dirty_circs_as_unusable(void)
Definition: circuitlist.c:2106
STATIC int get_my_roles(const or_options_t *options)
Definition: mainloop.c:1446
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:138
STATIC smartlist_t * connection_array
Definition: mainloop.c:164
void reset_main_loop_counters(void)
Definition: mainloop.c:518
static int save_state_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1871
mainloop_event_t * mainloop_event_new(void(*cb)(mainloop_event_t *, void *), void *userdata)
time_t timestamp_last_read_allowed
Definition: connection_st.h:98
static void increment_main_loop_success_count(void)
Definition: mainloop.c:527
void update_networkstatus_downloads(time_t now)
#define log_fn(severity, domain, args,...)
Definition: log.h:272
channel_tls_t * chan
#define CONN_TYPE_EXIT
Definition: connection.h:26
void fetch_bridge_descriptors(const or_options_t *options, time_t now)
Definition: bridges.c:722
uint64_t OwningControllerFD
static int second_elapsed_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1654
struct event * read_event
Definition: connection_st.h:91
#define LOG_FN_CONN(conn, args)
Definition: control.h:32
void update_extrainfo_downloads(time_t now)
Definition: routerlist.c:2718
Header file for control.c.
void connection_expire_held_open(void)
Definition: connection.c:955
static mainloop_event_t * postloop_cleanup_ev
Definition: mainloop.c:1611
static int write_stats_file_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:1899
time_t geoip_dirreq_stats_write(time_t now)
Definition: geoip_stats.c:1029
unsigned int hold_open_until_flushed
Definition: connection_st.h:56
int connection_is_writing(connection_t *conn)
Definition: mainloop.c:654
static uint64_t stats_n_main_loop_errors
Definition: mainloop.c:148
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:1568
#define LOG_DEBUG
Definition: log.h:39
int or_state_save(time_t now)
Definition: statefile.c:493
const char * marked_for_close_file
Header file for hs_cache.c.
long tor_parse_long(const char *s, int base, long min, long max, int *ok, char **next)
Definition: parse_int.c:56
static void scheduled_shutdown_cb(mainloop_event_t *ev, void *arg)
Definition: mainloop.c:1631
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:1588
int control_any_per_second_event_enabled(void)
static unsigned newnym_epoch
Definition: mainloop.c:161
Header file for buffers.c.
Header file for selftest.c.
int mainloop_event_schedule(mainloop_event_t *event, const struct timeval *tv)
uint8_t command
Definition: cell_st.h:14
#define CONN_TYPE_DIR
Definition: connection.h:35
void log_failed_proxy_connection(connection_t *conn)
Definition: connection.c:5459
Header file for connection_or.c.
static mainloop_event_t * scheduled_shutdown_ev
Definition: mainloop.c:1627
void reset_bandwidth_test(void)
Definition: circuituse.c:1611
static int connection_check_event(connection_t *conn, struct event *ev)
Definition: mainloop.c:571
#define MAX_SSL_KEY_LIFETIME_INTERNAL
Definition: or.h:155
void ip_address_changed(int at_interface)
Definition: mainloop.c:2258
static uint64_t stats_n_main_loop_idle
Definition: mainloop.c:150
#define LD_NET
Definition: log.h:63
static int fetch_networkstatus_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2044
#define OR_CONN_STATE_CONNECTING
Definition: orconn_event.h:31
int crypto_seed_rng(void)
Definition: crypto_rand.c:452
void connection_watch_events(connection_t *conn, watchable_events_t events)
Definition: mainloop.c:493
static void signewnym_impl(time_t now)
Definition: mainloop.c:1275
void addressmap_clear_transient(void)
Definition: addressmap.c:312
void connection_ap_expire_beginning(void)
static int retry_listeners_callback(time_t now, const or_options_t *options)
Definition: mainloop.c:2070
int connection_add_impl(connection_t *conn, int is_connecting)
Definition: mainloop.c:244
Header for compat_libevent.c.
static void connection_start_reading_from_linked_conn(connection_t *conn)
Definition: mainloop.c:797
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:287
#define CONN_TYPE_AP
Definition: connection.h:31
static void conn_read_callback(evutil_socket_t fd, short event, void *_conn)
Definition: mainloop.c:867
Header file for control_events.c.
MOCK_IMPL(smartlist_t *, get_connection_array,(void))
Definition: mainloop.c:450
void tor_init_connection_lists(void)
Definition: mainloop.c:412
#define LD_PROTOCOL
Definition: log.h:69
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:899
int DormantClientTimeout
int proxy_mode(const or_options_t *options)
Definition: routermode.c:30
int should_delay_dir_fetches(const or_options_t *options, const char **msg_out)
unsigned int is_canonical
void update_approx_time(time_t now)
Definition: approx_time.c:41
static time_t current_second
Definition: mainloop.c:2163
time_t timestamp_last_write_allowed
Header file for networkstatus.c.
#define LD_BUG
Definition: log.h:83
Header file for dns.c.
void circuit_upgrade_circuits_from_guard_wait(void)
Header file for routerlist.c.
const char * escaped_safe_str_client(const char *address)
Definition: config.c:1103
void circuit_note_clock_jumped(int64_t seconds_elapsed, bool was_idle)
int have_completed_a_circuit(void)
Definition: mainloop.c:218
int connection_edge_end_errno(edge_connection_t *conn)
uint64_t global_identifier
Definition: channel.h:197
int control_event_conn_bandwidth(connection_t *conn)