Issues with router_get_by_nickname() (3)
[tor/rransom.git] / src / or / main.c
blobaf2bf526d499428613a18acde7926089b8037c9c
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-2010, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
7 /**
8 * \file main.c
9 * \brief Toplevel module. Handles signals, multiplexes between
10 * connections, implements main loop, and drives scheduled events.
11 **/
13 #define MAIN_PRIVATE
14 #include "or.h"
15 #ifdef USE_DMALLOC
16 #include <dmalloc.h>
17 #include <openssl/crypto.h>
18 #endif
19 #include "memarea.h"
21 void evdns_shutdown(int);
23 /********* PROTOTYPES **********/
25 static void dumpmemusage(int severity);
26 static void dumpstats(int severity); /* log stats */
27 static void conn_read_callback(int fd, short event, void *_conn);
28 static void conn_write_callback(int fd, short event, void *_conn);
29 static void signal_callback(int fd, short events, void *arg);
30 static void second_elapsed_callback(int fd, short event, void *args);
31 static int conn_close_if_marked(int i);
32 static void connection_start_reading_from_linked_conn(connection_t *conn);
33 static int connection_should_read_from_linked_conn(connection_t *conn);
35 /********* START VARIABLES **********/
37 int global_read_bucket; /**< Max number of bytes I can read this second. */
38 int global_write_bucket; /**< Max number of bytes I can write this second. */
40 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
41 int global_relayed_read_bucket;
42 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
43 int global_relayed_write_bucket;
45 /** What was the read bucket before the last second_elapsed_callback() call?
46 * (used to determine how many bytes we've read). */
47 static int stats_prev_global_read_bucket;
48 /** What was the write bucket before the last second_elapsed_callback() call?
49 * (used to determine how many bytes we've written). */
50 static int stats_prev_global_write_bucket;
51 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
52 /** How many bytes have we read since we started the process? */
53 static uint64_t stats_n_bytes_read = 0;
54 /** How many bytes have we written since we started the process? */
55 static uint64_t stats_n_bytes_written = 0;
56 /** What time did this process start up? */
57 time_t time_of_process_start = 0;
58 /** How many seconds have we been running? */
59 long stats_n_seconds_working = 0;
60 /** When do we next launch DNS wildcarding checks? */
61 static time_t time_to_check_for_correct_dns = 0;
63 /** How often will we honor SIGNEWNYM requests? */
64 #define MAX_SIGNEWNYM_RATE 10
65 /** When did we last process a SIGNEWNYM request? */
66 static time_t time_of_last_signewnym = 0;
67 /** Is there a signewnym request we're currently waiting to handle? */
68 static int signewnym_is_pending = 0;
70 /** Smartlist of all open connections. */
71 static smartlist_t *connection_array = NULL;
72 /** List of connections that have been marked for close and need to be freed
73 * and removed from connection_array. */
74 static smartlist_t *closeable_connection_lst = NULL;
75 /** List of linked connections that are currently reading data into their
76 * inbuf from their partner's outbuf. */
77 static smartlist_t *active_linked_connection_lst = NULL;
78 /** Flag: Set to true iff we entered the current libevent main loop via
79 * <b>loop_once</b>. If so, there's no need to trigger a loopexit in order
80 * to handle linked connections. */
81 static int called_loop_once = 0;
83 /** We set this to 1 when we've opened a circuit, so we can print a log
84 * entry to inform the user that Tor is working. */
85 int has_completed_circuit=0;
87 /** How often do we check for router descriptors that we should download
88 * when we have too little directory info? */
89 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
90 /** How often do we check for router descriptors that we should download
91 * when we have enough directory info? */
92 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
93 /** How often do we 'forgive' undownloadable router descriptors and attempt
94 * to download them again? */
95 #define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
96 /** How long do we let a directory connection stall before expiring it? */
97 #define DIR_CONN_MAX_STALL (5*60)
99 /** How long do we let OR connections handshake before we decide that
100 * they are obsolete? */
101 #define TLS_HANDSHAKE_TIMEOUT (60)
103 /********* END VARIABLES ************/
105 /****************************************************************************
107 * This section contains accessors and other methods on the connection_array
108 * variables (which are global within this file and unavailable outside it).
110 ****************************************************************************/
112 /** Add <b>conn</b> to the array of connections that we can poll on. The
113 * connection's socket must be set; the connection starts out
114 * non-reading and non-writing.
117 connection_add(connection_t *conn)
119 tor_assert(conn);
120 tor_assert(conn->s >= 0 ||
121 conn->linked ||
122 (conn->type == CONN_TYPE_AP &&
123 TO_EDGE_CONN(conn)->is_dns_request));
125 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
126 conn->conn_array_index = smartlist_len(connection_array);
127 smartlist_add(connection_array, conn);
129 if (conn->s >= 0 || conn->linked) {
130 conn->read_event = tor_malloc_zero(sizeof(struct event));
131 conn->write_event = tor_malloc_zero(sizeof(struct event));
132 event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
133 conn_read_callback, conn);
134 event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
135 conn_write_callback, conn);
138 log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
139 conn_type_to_string(conn->type), conn->s, conn->address,
140 smartlist_len(connection_array));
142 return 0;
145 /** Remove the connection from the global list, and remove the
146 * corresponding poll entry. Calling this function will shift the last
147 * connection (if any) into the position occupied by conn.
150 connection_remove(connection_t *conn)
152 int current_index;
153 connection_t *tmp;
155 tor_assert(conn);
157 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
158 conn->s, conn_type_to_string(conn->type),
159 smartlist_len(connection_array));
161 tor_assert(conn->conn_array_index >= 0);
162 current_index = conn->conn_array_index;
163 connection_unregister_events(conn); /* This is redundant, but cheap. */
164 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
165 smartlist_del(connection_array, current_index);
166 return 0;
169 /* replace this one with the one at the end */
170 smartlist_del(connection_array, current_index);
171 tmp = smartlist_get(connection_array, current_index);
172 tmp->conn_array_index = current_index;
174 return 0;
177 /** If <b>conn</b> is an edge conn, remove it from the list
178 * of conn's on this circuit. If it's not on an edge,
179 * flush and send destroys for all circuits on this conn.
181 * Remove it from connection_array (if applicable) and
182 * from closeable_connection_list.
184 * Then free it.
186 static void
187 connection_unlink(connection_t *conn)
189 connection_about_to_close_connection(conn);
190 if (conn->conn_array_index >= 0) {
191 connection_remove(conn);
193 if (conn->linked_conn) {
194 conn->linked_conn->linked_conn = NULL;
195 if (! conn->linked_conn->marked_for_close &&
196 conn->linked_conn->reading_from_linked_conn)
197 connection_start_reading(conn->linked_conn);
198 conn->linked_conn = NULL;
200 smartlist_remove(closeable_connection_lst, conn);
201 smartlist_remove(active_linked_connection_lst, conn);
202 if (conn->type == CONN_TYPE_EXIT) {
203 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
205 if (conn->type == CONN_TYPE_OR) {
206 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
207 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
209 connection_free(conn);
212 /** Schedule <b>conn</b> to be closed. **/
213 void
214 add_connection_to_closeable_list(connection_t *conn)
216 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
217 tor_assert(conn->marked_for_close);
218 assert_connection_ok(conn, time(NULL));
219 smartlist_add(closeable_connection_lst, conn);
222 /** Return 1 if conn is on the closeable list, else return 0. */
224 connection_is_on_closeable_list(connection_t *conn)
226 return smartlist_isin(closeable_connection_lst, conn);
229 /** Return true iff conn is in the current poll array. */
231 connection_in_array(connection_t *conn)
233 return smartlist_isin(connection_array, conn);
236 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
237 * to the length of the array. <b>*array</b> and <b>*n</b> must not
238 * be modified.
240 smartlist_t *
241 get_connection_array(void)
243 if (!connection_array)
244 connection_array = smartlist_create();
245 return connection_array;
248 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
249 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
251 void
252 connection_watch_events(connection_t *conn, short events)
254 if (events & EV_READ)
255 connection_start_reading(conn);
256 else
257 connection_stop_reading(conn);
259 if (events & EV_WRITE)
260 connection_start_writing(conn);
261 else
262 connection_stop_writing(conn);
265 /** Return true iff <b>conn</b> is listening for read events. */
267 connection_is_reading(connection_t *conn)
269 tor_assert(conn);
271 return conn->reading_from_linked_conn ||
272 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
275 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
276 void
277 connection_stop_reading(connection_t *conn)
279 tor_assert(conn);
280 tor_assert(conn->read_event);
282 if (conn->linked) {
283 conn->reading_from_linked_conn = 0;
284 connection_stop_reading_from_linked_conn(conn);
285 } else {
286 if (event_del(conn->read_event))
287 log_warn(LD_NET, "Error from libevent setting read event state for %d "
288 "to unwatched: %s",
289 conn->s,
290 tor_socket_strerror(tor_socket_errno(conn->s)));
294 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
295 void
296 connection_start_reading(connection_t *conn)
298 tor_assert(conn);
299 tor_assert(conn->read_event);
301 if (conn->linked) {
302 conn->reading_from_linked_conn = 1;
303 if (connection_should_read_from_linked_conn(conn))
304 connection_start_reading_from_linked_conn(conn);
305 } else {
306 if (event_add(conn->read_event, NULL))
307 log_warn(LD_NET, "Error from libevent setting read event state for %d "
308 "to watched: %s",
309 conn->s,
310 tor_socket_strerror(tor_socket_errno(conn->s)));
314 /** Return true iff <b>conn</b> is listening for write events. */
316 connection_is_writing(connection_t *conn)
318 tor_assert(conn);
320 return conn->writing_to_linked_conn ||
321 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
324 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
325 void
326 connection_stop_writing(connection_t *conn)
328 tor_assert(conn);
329 tor_assert(conn->write_event);
331 if (conn->linked) {
332 conn->writing_to_linked_conn = 0;
333 if (conn->linked_conn)
334 connection_stop_reading_from_linked_conn(conn->linked_conn);
335 } else {
336 if (event_del(conn->write_event))
337 log_warn(LD_NET, "Error from libevent setting write event state for %d "
338 "to unwatched: %s",
339 conn->s,
340 tor_socket_strerror(tor_socket_errno(conn->s)));
344 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
345 void
346 connection_start_writing(connection_t *conn)
348 tor_assert(conn);
349 tor_assert(conn->write_event);
351 if (conn->linked) {
352 conn->writing_to_linked_conn = 1;
353 if (conn->linked_conn &&
354 connection_should_read_from_linked_conn(conn->linked_conn))
355 connection_start_reading_from_linked_conn(conn->linked_conn);
356 } else {
357 if (event_add(conn->write_event, NULL))
358 log_warn(LD_NET, "Error from libevent setting write event state for %d "
359 "to watched: %s",
360 conn->s,
361 tor_socket_strerror(tor_socket_errno(conn->s)));
365 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
366 * linked to it would be good and feasible. (Reading is "feasible" if the
367 * other conn exists and has data in its outbuf, and is "good" if we have our
368 * reading_from_linked_conn flag set and the other conn has its
369 * writing_to_linked_conn flag set.)*/
370 static int
371 connection_should_read_from_linked_conn(connection_t *conn)
373 if (conn->linked && conn->reading_from_linked_conn) {
374 if (! conn->linked_conn ||
375 (conn->linked_conn->writing_to_linked_conn &&
376 buf_datalen(conn->linked_conn->outbuf)))
377 return 1;
379 return 0;
382 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
383 * its linked connection, if it is not doing so already. Called by
384 * connection_start_reading and connection_start_writing as appropriate. */
385 static void
386 connection_start_reading_from_linked_conn(connection_t *conn)
388 tor_assert(conn);
389 tor_assert(conn->linked == 1);
391 if (!conn->active_on_link) {
392 conn->active_on_link = 1;
393 smartlist_add(active_linked_connection_lst, conn);
394 if (!called_loop_once) {
395 /* This is the first event on the list; we won't be in LOOP_ONCE mode,
396 * so we need to make sure that the event_loop() actually exits at the
397 * end of its run through the current connections and
398 * lets us activate read events for linked connections. */
399 struct timeval tv = { 0, 0 };
400 event_loopexit(&tv);
402 } else {
403 tor_assert(smartlist_isin(active_linked_connection_lst, conn));
407 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
408 * connection, if is currently doing so. Called by connection_stop_reading,
409 * connection_stop_writing, and connection_read. */
410 void
411 connection_stop_reading_from_linked_conn(connection_t *conn)
413 tor_assert(conn);
414 tor_assert(conn->linked == 1);
416 if (conn->active_on_link) {
417 conn->active_on_link = 0;
418 /* FFFF We could keep an index here so we can smartlist_del
419 * cleanly. On the other hand, this doesn't show up on profiles,
420 * so let's leave it alone for now. */
421 smartlist_remove(active_linked_connection_lst, conn);
422 } else {
423 tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
427 /** Close all connections that have been scheduled to get closed. */
428 static void
429 close_closeable_connections(void)
431 int i;
432 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
433 connection_t *conn = smartlist_get(closeable_connection_lst, i);
434 if (conn->conn_array_index < 0) {
435 connection_unlink(conn); /* blow it away right now */
436 } else {
437 if (!conn_close_if_marked(conn->conn_array_index))
438 ++i;
443 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
444 * some data to read. */
445 static void
446 conn_read_callback(int fd, short event, void *_conn)
448 connection_t *conn = _conn;
449 (void)fd;
450 (void)event;
452 log_debug(LD_NET,"socket %d wants to read.",conn->s);
454 /* assert_connection_ok(conn, time(NULL)); */
456 if (connection_handle_read(conn) < 0) {
457 if (!conn->marked_for_close) {
458 #ifndef MS_WINDOWS
459 log_warn(LD_BUG,"Unhandled error on read for %s connection "
460 "(fd %d); removing",
461 conn_type_to_string(conn->type), conn->s);
462 tor_fragile_assert();
463 #endif
464 if (CONN_IS_EDGE(conn))
465 connection_edge_end_errno(TO_EDGE_CONN(conn));
466 connection_mark_for_close(conn);
469 assert_connection_ok(conn, time(NULL));
471 if (smartlist_len(closeable_connection_lst))
472 close_closeable_connections();
475 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
476 * some data to write. */
477 static void
478 conn_write_callback(int fd, short events, void *_conn)
480 connection_t *conn = _conn;
481 (void)fd;
482 (void)events;
484 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
486 /* assert_connection_ok(conn, time(NULL)); */
488 if (connection_handle_write(conn, 0) < 0) {
489 if (!conn->marked_for_close) {
490 /* this connection is broken. remove it. */
491 log_fn(LOG_WARN,LD_BUG,
492 "unhandled error on write for %s connection (fd %d); removing",
493 conn_type_to_string(conn->type), conn->s);
494 tor_fragile_assert();
495 if (CONN_IS_EDGE(conn)) {
496 /* otherwise we cry wolf about duplicate close */
497 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
498 if (!edge_conn->end_reason)
499 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
500 edge_conn->edge_has_sent_end = 1;
502 connection_close_immediate(conn); /* So we don't try to flush. */
503 connection_mark_for_close(conn);
506 assert_connection_ok(conn, time(NULL));
508 if (smartlist_len(closeable_connection_lst))
509 close_closeable_connections();
512 /** If the connection at connection_array[i] is marked for close, then:
513 * - If it has data that it wants to flush, try to flush it.
514 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
515 * true, then leave the connection open and return.
516 * - Otherwise, remove the connection from connection_array and from
517 * all other lists, close it, and free it.
518 * Returns 1 if the connection was closed, 0 otherwise.
520 static int
521 conn_close_if_marked(int i)
523 connection_t *conn;
524 int retval;
525 time_t now;
527 conn = smartlist_get(connection_array, i);
528 if (!conn->marked_for_close)
529 return 0; /* nothing to see here, move along */
530 now = time(NULL);
531 assert_connection_ok(conn, now);
532 /* assert_all_pending_dns_resolves_ok(); */
534 log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
535 if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
536 /* s == -1 means it's an incomplete edge connection, or that the socket
537 * has already been closed as unflushable. */
538 ssize_t sz = connection_bucket_write_limit(conn, now);
539 if (!conn->hold_open_until_flushed)
540 log_info(LD_NET,
541 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
542 "to flush %d bytes. (Marked at %s:%d)",
543 escaped_safe_str(conn->address),
544 conn->s, conn_type_to_string(conn->type), conn->state,
545 (int)conn->outbuf_flushlen,
546 conn->marked_for_close_file, conn->marked_for_close);
547 if (conn->linked_conn) {
548 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
549 &conn->outbuf_flushlen);
550 if (retval >= 0) {
551 /* The linked conn will notice that it has data when it notices that
552 * we're gone. */
553 connection_start_reading_from_linked_conn(conn->linked_conn);
555 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
556 "%d left; flushlen %d; wants-to-flush==%d", retval,
557 (int)buf_datalen(conn->outbuf),
558 (int)conn->outbuf_flushlen,
559 connection_wants_to_flush(conn));
560 } else if (connection_speaks_cells(conn)) {
561 if (conn->state == OR_CONN_STATE_OPEN) {
562 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
563 &conn->outbuf_flushlen);
564 } else
565 retval = -1; /* never flush non-open broken tls connections */
566 } else {
567 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
569 if (retval >= 0 && /* Technically, we could survive things like
570 TLS_WANT_WRITE here. But don't bother for now. */
571 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
572 if (retval > 0) {
573 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
574 "Holding conn (fd %d) open for more flushing.",
575 conn->s));
576 conn->timestamp_lastwritten = now; /* reset so we can flush more */
578 return 0;
580 if (connection_wants_to_flush(conn)) {
581 int severity;
582 if (conn->type == CONN_TYPE_EXIT ||
583 (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
584 (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
585 severity = LOG_INFO;
586 else
587 severity = LOG_NOTICE;
588 /* XXXX Maybe allow this to happen a certain amount per hour; it usually
589 * is meaningless. */
590 log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
591 "bytes to address %s. If this happens a lot, either "
592 "something is wrong with your network connection, or "
593 "something is wrong with theirs. "
594 "(fd %d, type %s, state %d, marked at %s:%d).",
595 (int)buf_datalen(conn->outbuf),
596 escaped_safe_str(conn->address), conn->s,
597 conn_type_to_string(conn->type), conn->state,
598 conn->marked_for_close_file,
599 conn->marked_for_close);
602 connection_unlink(conn); /* unlink, remove, free */
603 return 1;
606 /** We've just tried every dirserver we know about, and none of
607 * them were reachable. Assume the network is down. Change state
608 * so next time an application connection arrives we'll delay it
609 * and try another directory fetch. Kill off all the circuit_wait
610 * streams that are waiting now, since they will all timeout anyway.
612 void
613 directory_all_unreachable(time_t now)
615 connection_t *conn;
616 (void)now;
618 stats_n_seconds_working=0; /* reset it */
620 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
621 AP_CONN_STATE_CIRCUIT_WAIT))) {
622 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
623 log_notice(LD_NET,
624 "Is your network connection down? "
625 "Failing connection to '%s:%d'.",
626 safe_str(edge_conn->socks_request->address),
627 edge_conn->socks_request->port);
628 connection_mark_unattached_ap(edge_conn,
629 END_STREAM_REASON_NET_UNREACHABLE);
631 control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
634 /** This function is called whenever we successfully pull down some new
635 * network statuses or server descriptors. */
636 void
637 directory_info_has_arrived(time_t now, int from_cache)
639 or_options_t *options = get_options();
641 if (!router_have_minimum_dir_info()) {
642 int quiet = directory_too_idle_to_fetch_descriptors(options, now);
643 log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
644 "I learned some more directory information, but not enough to "
645 "build a circuit: %s", get_dir_info_status_string());
646 update_router_descriptor_downloads(now);
647 return;
648 } else {
649 if (directory_fetches_from_authorities(options))
650 update_router_descriptor_downloads(now);
652 /* if we have enough dir info, then update our guard status with
653 * whatever we just learned. */
654 entry_guards_compute_status();
655 /* Don't even bother trying to get extrainfo until the rest of our
656 * directory info is up-to-date */
657 if (options->DownloadExtraInfo)
658 update_extrainfo_downloads(now);
661 if (server_mode(options) && !we_are_hibernating() && !from_cache &&
662 (has_completed_circuit || !any_predicted_circuits(now)))
663 consider_testing_reachability(1, 1);
666 /** How long do we wait before killing OR connections with no circuits?
667 * In Tor versions up to 0.2.1.25 and 0.2.2.12-alpha, we waited 15 minutes
668 * before cancelling these connections, which caused fast relays to accrue
669 * many many idle connections. Hopefully 3 minutes is low enough that
670 * it kills most idle connections, without being so low that we cause
671 * clients to bounce on and off.
673 #define IDLE_OR_CONN_TIMEOUT 180
675 /** Perform regular maintenance tasks for a single connection. This
676 * function gets run once per second per connection by run_scheduled_events.
678 static void
679 run_connection_housekeeping(int i, time_t now)
681 cell_t cell;
682 connection_t *conn = smartlist_get(connection_array, i);
683 or_options_t *options = get_options();
684 or_connection_t *or_conn;
685 int past_keepalive =
686 now >= conn->timestamp_lastwritten + options->KeepalivePeriod;
688 if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
689 TO_OR_CONN(conn)->timestamp_lastempty = now;
691 if (conn->marked_for_close) {
692 /* nothing to do here */
693 return;
696 /* Expire any directory connections that haven't been active (sent
697 * if a server or received if a client) for 5 min */
698 if (conn->type == CONN_TYPE_DIR &&
699 ((DIR_CONN_IS_SERVER(conn) &&
700 conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
701 (!DIR_CONN_IS_SERVER(conn) &&
702 conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
703 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
704 conn->s, conn->purpose);
705 /* This check is temporary; it's to let us know whether we should consider
706 * parsing partial serverdesc responses. */
707 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
708 buf_datalen(conn->inbuf)>=1024) {
709 log_info(LD_DIR,"Trying to extract information from wedged server desc "
710 "download.");
711 connection_dir_reached_eof(TO_DIR_CONN(conn));
712 } else {
713 connection_mark_for_close(conn);
715 return;
718 if (!connection_speaks_cells(conn))
719 return; /* we're all done here, the rest is just for OR conns */
721 /* If we haven't written to an OR connection for a while, then either nuke
722 the connection or send a keepalive, depending. */
724 or_conn = TO_OR_CONN(conn);
726 if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
727 /* It's bad for new circuits, and has no unmarked circuits on it:
728 * mark it now. */
729 log_info(LD_OR,
730 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
731 conn->s, conn->address, conn->port);
732 if (conn->state == OR_CONN_STATE_CONNECTING)
733 connection_or_connect_failed(TO_OR_CONN(conn),
734 END_OR_CONN_REASON_TIMEOUT,
735 "Tor gave up on the connection");
736 connection_mark_for_close(conn);
737 conn->hold_open_until_flushed = 1;
738 } else if (!connection_state_is_open(conn)) {
739 if (past_keepalive) {
740 /* We never managed to actually get this connection open and happy. */
741 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
742 conn->s,conn->address, conn->port);
743 connection_mark_for_close(conn);
745 } else if (we_are_hibernating() && !or_conn->n_circuits &&
746 !buf_datalen(conn->outbuf)) {
747 /* We're hibernating, there's no circuits, and nothing to flush.*/
748 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
749 "[Hibernating or exiting].",
750 conn->s,conn->address, conn->port);
751 connection_mark_for_close(conn);
752 conn->hold_open_until_flushed = 1;
753 } else if (!or_conn->n_circuits &&
754 now >= or_conn->timestamp_last_added_nonpadding +
755 IDLE_OR_CONN_TIMEOUT) {
756 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
757 "[idle %d].", conn->s,conn->address, conn->port,
758 (int)(now - or_conn->timestamp_last_added_nonpadding));
759 connection_mark_for_close(conn);
760 conn->hold_open_until_flushed = 1;
761 } else if (
762 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
763 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
764 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
765 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
766 "flush; %d seconds since last write)",
767 conn->s, conn->address, conn->port,
768 (int)buf_datalen(conn->outbuf),
769 (int)(now-conn->timestamp_lastwritten));
770 connection_mark_for_close(conn);
771 } else if (past_keepalive && !buf_datalen(conn->outbuf)) {
772 /* send a padding cell */
773 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
774 conn->address, conn->port);
775 memset(&cell,0,sizeof(cell_t));
776 cell.command = CELL_PADDING;
777 connection_or_write_cell_to_buf(&cell, or_conn);
781 /** Honor a NEWNYM request: make future requests unlinkability to past
782 * requests. */
783 static void
784 signewnym_impl(time_t now)
786 circuit_expire_all_dirty_circs();
787 addressmap_clear_transient();
788 time_of_last_signewnym = now;
789 signewnym_is_pending = 0;
792 /** Perform regular maintenance tasks. This function gets run once per
793 * second by second_elapsed_callback().
795 static void
796 run_scheduled_events(time_t now)
798 static time_t last_rotated_x509_certificate = 0;
799 static time_t time_to_check_v3_certificate = 0;
800 static time_t time_to_check_listeners = 0;
801 static time_t time_to_check_descriptor = 0;
802 static time_t time_to_check_ipaddress = 0;
803 static time_t time_to_shrink_memory = 0;
804 static time_t time_to_try_getting_descriptors = 0;
805 static time_t time_to_reset_descriptor_failures = 0;
806 static time_t time_to_add_entropy = 0;
807 static time_t time_to_write_hs_statistics = 0;
808 static time_t time_to_write_bridge_status_file = 0;
809 static time_t time_to_downrate_stability = 0;
810 static time_t time_to_save_stability = 0;
811 static time_t time_to_clean_caches = 0;
812 static time_t time_to_recheck_bandwidth = 0;
813 static time_t time_to_check_for_expired_networkstatus = 0;
814 static time_t time_to_dump_geoip_stats = 0;
815 static time_t time_to_retry_dns_init = 0;
816 or_options_t *options = get_options();
817 int i;
818 int have_dir_info;
820 /** 0. See if we've been asked to shut down and our timeout has
821 * expired; or if our bandwidth limits are exhausted and we
822 * should hibernate; or if it's time to wake up from hibernation.
824 consider_hibernation(now);
826 /* 0b. If we've deferred a signewnym, make sure it gets handled
827 * eventually. */
828 if (signewnym_is_pending &&
829 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
830 log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
831 signewnym_impl(now);
834 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
835 * shut down and restart all cpuworkers, and update the directory if
836 * necessary.
838 if (server_mode(options) &&
839 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
840 log_info(LD_GENERAL,"Rotating onion key.");
841 rotate_onion_key();
842 cpuworkers_rotate();
843 if (router_rebuild_descriptor(1)<0) {
844 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
846 if (advertised_server_mode())
847 router_upload_dir_desc_to_dirservers(0);
850 if (time_to_try_getting_descriptors < now) {
851 update_router_descriptor_downloads(now);
852 update_extrainfo_downloads(now);
853 if (options->UseBridges)
854 fetch_bridge_descriptors(now);
855 if (router_have_minimum_dir_info())
856 time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
857 else
858 time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
861 if (time_to_reset_descriptor_failures < now) {
862 router_reset_descriptor_download_failures();
863 time_to_reset_descriptor_failures =
864 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
867 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
868 if (!last_rotated_x509_certificate)
869 last_rotated_x509_certificate = now;
870 if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
871 log_info(LD_GENERAL,"Rotating tls context.");
872 if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
873 log_warn(LD_BUG, "Error reinitializing TLS context");
874 /* XXX is it a bug here, that we just keep going? -RD */
876 last_rotated_x509_certificate = now;
877 /* We also make sure to rotate the TLS connections themselves if they've
878 * been up for too long -- but that's done via is_bad_for_new_circs in
879 * connection_run_housekeeping() above. */
882 if (time_to_add_entropy < now) {
883 if (time_to_add_entropy) {
884 /* We already seeded once, so don't die on failure. */
885 crypto_seed_rng(0);
887 /** How often do we add more entropy to OpenSSL's RNG pool? */
888 #define ENTROPY_INTERVAL (60*60)
889 time_to_add_entropy = now + ENTROPY_INTERVAL;
892 /** 1c. If we have to change the accounting interval or record
893 * bandwidth used in this accounting interval, do so. */
894 if (accounting_is_enabled(options))
895 accounting_run_housekeeping(now);
897 if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
898 !we_are_hibernating()) {
899 /* try to determine reachability of the other Tor relays */
900 dirserv_test_reachability(now, 0);
903 /** 1d. Periodically, we discount older stability information so that new
904 * stability info counts more, and save the stability information to disk as
905 * appropriate. */
906 if (time_to_downrate_stability < now)
907 time_to_downrate_stability = rep_hist_downrate_old_runs(now);
908 if (authdir_mode_tests_reachability(options)) {
909 if (time_to_save_stability < now) {
910 if (time_to_save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
911 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
913 #define SAVE_STABILITY_INTERVAL (30*60)
914 time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
918 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
919 * close to expiring and warn the admin if it is. */
920 if (time_to_check_v3_certificate < now) {
921 v3_authority_check_key_expiry();
922 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
923 time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
926 /* 1f. Check whether our networkstatus has expired.
928 if (time_to_check_for_expired_networkstatus < now) {
929 networkstatus_t *ns = networkstatus_get_latest_consensus();
930 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
931 * networkstatus_get_reasonably_live_consensus(), but that value is way
932 * way too high. Arma: is the bridge issue there resolved yet? -NM */
933 #define NS_EXPIRY_SLOP (24*60*60)
934 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
935 router_have_minimum_dir_info()) {
936 router_dir_info_changed();
938 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
939 time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
942 if (time_to_dump_geoip_stats < now) {
943 #define DUMP_GEOIP_STATS_INTERVAL (60*60);
944 if (time_to_dump_geoip_stats)
945 dump_geoip_stats();
946 time_to_dump_geoip_stats = now + DUMP_GEOIP_STATS_INTERVAL;
949 /* Remove old information from rephist and the rend cache. */
950 if (time_to_clean_caches < now) {
951 rep_history_clean(now - options->RephistTrackTime);
952 rend_cache_clean();
953 rend_cache_clean_v2_descs_as_dir();
954 #define CLEAN_CACHES_INTERVAL (30*60)
955 time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
958 #define RETRY_DNS_INTERVAL (10*60)
959 /* If we're a server and initializing dns failed, retry periodically. */
960 if (time_to_retry_dns_init < now) {
961 time_to_retry_dns_init = now + RETRY_DNS_INTERVAL;
962 if (server_mode(options) && has_dns_init_failed())
963 dns_init();
966 /** 2. Periodically, we consider force-uploading our descriptor
967 * (if we've passed our internal checks). */
969 /** How often do we check whether part of our router info has changed in a way
970 * that would require an upload? */
971 #define CHECK_DESCRIPTOR_INTERVAL (60)
972 /** How often do we (as a router) check whether our IP address has changed? */
973 #define CHECK_IPADDRESS_INTERVAL (15*60)
975 /* 2b. Once per minute, regenerate and upload the descriptor if the old
976 * one is inaccurate. */
977 if (time_to_check_descriptor < now) {
978 static int dirport_reachability_count = 0;
979 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
980 check_descriptor_bandwidth_changed(now);
981 if (time_to_check_ipaddress < now) {
982 time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
983 check_descriptor_ipaddress_changed(now);
985 /** If our router descriptor ever goes this long without being regenerated
986 * because something changed, we force an immediate regenerate-and-upload. */
987 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
988 mark_my_descriptor_dirty_if_older_than(
989 now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
990 consider_publishable_server(0);
991 /* also, check religiously for reachability, if it's within the first
992 * 20 minutes of our uptime. */
993 if (server_mode(options) &&
994 (has_completed_circuit || !any_predicted_circuits(now)) &&
995 !we_are_hibernating()) {
996 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
997 consider_testing_reachability(1, dirport_reachability_count==0);
998 if (++dirport_reachability_count > 5)
999 dirport_reachability_count = 0;
1000 } else if (time_to_recheck_bandwidth < now) {
1001 /* If we haven't checked for 12 hours and our bandwidth estimate is
1002 * low, do another bandwidth test. This is especially important for
1003 * bridges, since they might go long periods without much use. */
1004 routerinfo_t *me = router_get_my_routerinfo();
1005 if (time_to_recheck_bandwidth && me &&
1006 me->bandwidthcapacity < me->bandwidthrate &&
1007 me->bandwidthcapacity < 51200) {
1008 reset_bandwidth_test();
1010 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1011 time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1015 /* If any networkstatus documents are no longer recent, we need to
1016 * update all the descriptors' running status. */
1017 /* purge obsolete entries */
1018 networkstatus_v2_list_clean(now);
1019 /* Remove dead routers. */
1020 routerlist_remove_old_routers();
1022 /* Also, once per minute, check whether we want to download any
1023 * networkstatus documents.
1025 update_networkstatus_downloads(now);
1028 /** 2c. Let directory voting happen. */
1029 if (authdir_mode_v3(options))
1030 dirvote_act(options, now);
1032 /** 3a. Every second, we examine pending circuits and prune the
1033 * ones which have been pending for more than a few seconds.
1034 * We do this before step 4, so it can try building more if
1035 * it's not comfortable with the number of available circuits.
1037 circuit_expire_building(now);
1039 /** 3b. Also look at pending streams and prune the ones that 'began'
1040 * a long time ago but haven't gotten a 'connected' yet.
1041 * Do this before step 4, so we can put them back into pending
1042 * state to be picked up by the new circuit.
1044 connection_ap_expire_beginning();
1046 /** 3c. And expire connections that we've held open for too long.
1048 connection_expire_held_open();
1050 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
1051 if (!we_are_hibernating() && time_to_check_listeners < now) {
1052 retry_all_listeners(NULL, NULL);
1053 time_to_check_listeners = now+60;
1056 /** 4. Every second, we try a new circuit if there are no valid
1057 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1058 * that became dirty more than MaxCircuitDirtiness seconds ago,
1059 * and we make a new circ if there are no clean circuits.
1061 have_dir_info = router_have_minimum_dir_info();
1062 if (have_dir_info && !we_are_hibernating())
1063 circuit_build_needed_circs(now);
1065 /* every 10 seconds, but not at the same second as other such events */
1066 if (now % 10 == 5)
1067 circuit_expire_old_circuits_serverside(now);
1069 /** 5. We do housekeeping for each connection... */
1070 connection_or_set_bad_connections();
1071 for (i=0;i<smartlist_len(connection_array);i++) {
1072 run_connection_housekeeping(i, now);
1074 if (time_to_shrink_memory < now) {
1075 SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
1076 if (conn->outbuf)
1077 buf_shrink(conn->outbuf);
1078 if (conn->inbuf)
1079 buf_shrink(conn->inbuf);
1081 clean_cell_pool();
1082 buf_shrink_freelists(0);
1083 /** How often do we check buffers and pools for empty space that can be
1084 * deallocated? */
1085 #define MEM_SHRINK_INTERVAL (60)
1086 time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
1089 /** 6. And remove any marked circuits... */
1090 circuit_close_all_marked();
1092 /** 7. And upload service descriptors if necessary. */
1093 if (has_completed_circuit && !we_are_hibernating()) {
1094 rend_consider_services_upload(now);
1095 rend_consider_descriptor_republication();
1098 /** 8. and blow away any connections that need to die. have to do this now,
1099 * because if we marked a conn for close and left its socket -1, then
1100 * we'll pass it to poll/select and bad things will happen.
1102 close_closeable_connections();
1104 /** 8b. And if anything in our state is ready to get flushed to disk, we
1105 * flush it. */
1106 or_state_save(now);
1108 /** 9. and if we're a server, check whether our DNS is telling stories to
1109 * us. */
1110 if (server_mode(options) && time_to_check_for_correct_dns < now) {
1111 if (!time_to_check_for_correct_dns) {
1112 time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
1113 } else {
1114 dns_launch_correctness_checks();
1115 time_to_check_for_correct_dns = now + 12*3600 +
1116 crypto_rand_int(12*3600);
1120 /** 10. write hidden service usage statistic to disk */
1121 if (options->HSAuthorityRecordStats && time_to_write_hs_statistics < now) {
1122 hs_usage_write_statistics_to_file(now);
1123 #define WRITE_HSUSAGE_INTERVAL (30*60)
1124 time_to_write_hs_statistics = now+WRITE_HSUSAGE_INTERVAL;
1126 /** 10b. write bridge networkstatus file to disk */
1127 if (options->BridgeAuthoritativeDir &&
1128 time_to_write_bridge_status_file < now) {
1129 networkstatus_dump_bridge_status_to_file(now);
1130 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1131 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1135 /** Libevent timer: used to invoke second_elapsed_callback() once per
1136 * second. */
1137 static struct event *timeout_event = NULL;
1138 /** Number of libevent errors in the last second: we die if we get too many. */
1139 static int n_libevent_errors = 0;
1141 /** Libevent callback: invoked once every second. */
1142 static void
1143 second_elapsed_callback(int fd, short event, void *args)
1145 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1146 * could use Libevent's timers for this rather than checking the current
1147 * time against a bunch of timeouts every second. */
1148 static struct timeval one_second;
1149 static time_t current_second = 0;
1150 time_t now;
1151 size_t bytes_written;
1152 size_t bytes_read;
1153 int seconds_elapsed;
1154 or_options_t *options = get_options();
1155 (void)fd;
1156 (void)event;
1157 (void)args;
1158 if (!timeout_event) {
1159 timeout_event = tor_malloc_zero(sizeof(struct event));
1160 evtimer_set(timeout_event, second_elapsed_callback, NULL);
1161 one_second.tv_sec = 1;
1162 one_second.tv_usec = 0;
1165 n_libevent_errors = 0;
1167 /* log_fn(LOG_NOTICE, "Tick."); */
1168 now = time(NULL);
1169 update_approx_time(now);
1171 /* the second has rolled over. check more stuff. */
1172 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1173 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1174 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1175 stats_n_bytes_read += bytes_read;
1176 stats_n_bytes_written += bytes_written;
1177 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1178 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1179 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1180 control_event_stream_bandwidth_used();
1182 if (seconds_elapsed > 0)
1183 connection_bucket_refill(seconds_elapsed, now);
1184 stats_prev_global_read_bucket = global_read_bucket;
1185 stats_prev_global_write_bucket = global_write_bucket;
1187 if (server_mode(options) &&
1188 !we_are_hibernating() &&
1189 seconds_elapsed > 0 &&
1190 has_completed_circuit &&
1191 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1192 (stats_n_seconds_working+seconds_elapsed) /
1193 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1194 /* every 20 minutes, check and complain if necessary */
1195 routerinfo_t *me = router_get_my_routerinfo();
1196 if (me && !check_whether_orport_reachable()) {
1197 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1198 "its ORPort is reachable. Please check your firewalls, ports, "
1199 "address, /etc/hosts file, etc.",
1200 me->address, me->or_port);
1201 control_event_server_status(LOG_WARN,
1202 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1203 me->address, me->or_port);
1206 if (me && !check_whether_dirport_reachable()) {
1207 log_warn(LD_CONFIG,
1208 "Your server (%s:%d) has not managed to confirm that its "
1209 "DirPort is reachable. Please check your firewalls, ports, "
1210 "address, /etc/hosts file, etc.",
1211 me->address, me->dir_port);
1212 control_event_server_status(LOG_WARN,
1213 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1214 me->address, me->dir_port);
1218 /** If more than this many seconds have elapsed, probably the clock
1219 * jumped: doesn't count. */
1220 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1221 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1222 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1223 circuit_note_clock_jumped(seconds_elapsed);
1224 /* XXX if the time jumps *back* many months, do our events in
1225 * run_scheduled_events() recover? I don't think they do. -RD */
1226 } else if (seconds_elapsed > 0)
1227 stats_n_seconds_working += seconds_elapsed;
1229 run_scheduled_events(now);
1231 current_second = now; /* remember which second it is, for next time */
1233 #if 0
1234 if (current_second % 300 == 0) {
1235 rep_history_clean(current_second - options->RephistTrackTime);
1236 dumpmemusage(get_min_log_level()<LOG_INFO ?
1237 get_min_log_level() : LOG_INFO);
1239 #endif
1241 if (evtimer_add(timeout_event, &one_second))
1242 log_err(LD_NET,
1243 "Error from libevent when setting one-second timeout event");
1246 #ifndef MS_WINDOWS
1247 /** Called when a possibly ignorable libevent error occurs; ensures that we
1248 * don't get into an infinite loop by ignoring too many errors from
1249 * libevent. */
1250 static int
1251 got_libevent_error(void)
1253 if (++n_libevent_errors > 8) {
1254 log_err(LD_NET, "Too many libevent errors in one second; dying");
1255 return -1;
1257 return 0;
1259 #endif
1261 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1263 /** Called when our IP address seems to have changed. <b>at_interface</b>
1264 * should be true if we detected a change in our interface, and false if we
1265 * detected a change in our published address. */
1266 void
1267 ip_address_changed(int at_interface)
1269 int server = server_mode(get_options());
1271 if (at_interface) {
1272 if (! server) {
1273 /* Okay, change our keys. */
1274 init_keys();
1276 } else {
1277 if (server) {
1278 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1279 reset_bandwidth_test();
1280 stats_n_seconds_working = 0;
1281 router_reset_reachability();
1282 mark_my_descriptor_dirty();
1286 dns_servers_relaunch_checks();
1289 /** Forget what we've learned about the correctness of our DNS servers, and
1290 * start learning again. */
1291 void
1292 dns_servers_relaunch_checks(void)
1294 if (server_mode(get_options())) {
1295 dns_reset_correctness_checks();
1296 time_to_check_for_correct_dns = 0;
1300 /** Called when we get a SIGHUP: reload configuration files and keys,
1301 * retry all connections, and so on. */
1302 static int
1303 do_hup(void)
1305 or_options_t *options = get_options();
1307 #ifdef USE_DMALLOC
1308 dmalloc_log_stats();
1309 dmalloc_log_changed(0, 1, 0, 0);
1310 #endif
1312 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1313 "resetting internal state.");
1314 if (accounting_is_enabled(options))
1315 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1317 router_reset_warnings();
1318 routerlist_reset_warnings();
1319 addressmap_clear_transient();
1320 /* first, reload config variables, in case they've changed */
1321 if (options->ReloadTorrcOnSIGHUP) {
1322 /* no need to provide argc/v, they've been cached in init_from_config */
1323 if (options_init_from_torrc(0, NULL) < 0) {
1324 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1325 "For usage, try -h.");
1326 return -1;
1328 options = get_options(); /* they have changed now */
1329 } else {
1330 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1331 "us not to.");
1333 if (authdir_mode_handles_descs(options, -1)) {
1334 /* reload the approved-routers file */
1335 if (dirserv_load_fingerprint_file() < 0) {
1336 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1337 log_info(LD_GENERAL, "Error reloading fingerprints. "
1338 "Continuing with old list.");
1342 /* Rotate away from the old dirty circuits. This has to be done
1343 * after we've read the new options, but before we start using
1344 * circuits for directory fetches. */
1345 circuit_expire_all_dirty_circs();
1347 /* retry appropriate downloads */
1348 router_reset_status_download_failures();
1349 router_reset_descriptor_download_failures();
1350 update_networkstatus_downloads(time(NULL));
1352 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1353 * force a retry there. */
1355 if (server_mode(options)) {
1356 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1357 * configuration options. */
1358 cpuworkers_rotate();
1359 dns_reset();
1361 return 0;
1364 /** Tor main loop. */
1365 /* static */ int
1366 do_main_loop(void)
1368 int loop_result;
1369 time_t now;
1371 /* initialize dns resolve map, spawn workers if needed */
1372 if (dns_init() < 0) {
1373 if (get_options()->ServerDNSAllowBrokenConfig)
1374 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1375 "Network not up yet? Will try again soon.");
1376 else {
1377 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1378 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1382 handle_signals(1);
1384 /* load the private keys, if we're supposed to have them, and set up the
1385 * TLS context. */
1386 if (! identity_key_is_set()) {
1387 if (init_keys() < 0) {
1388 log_err(LD_BUG,"Error initializing keys; exiting");
1389 return -1;
1393 /* Set up the packed_cell_t memory pool. */
1394 init_cell_pool();
1396 /* Set up our buckets */
1397 connection_bucket_init();
1398 stats_prev_global_read_bucket = global_read_bucket;
1399 stats_prev_global_write_bucket = global_write_bucket;
1401 /* initialize the bootstrap status events to know we're starting up */
1402 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1404 if (trusted_dirs_reload_certs())
1405 return -1;
1406 if (router_reload_v2_networkstatus()) {
1407 return -1;
1409 if (router_reload_consensus_networkstatus()) {
1410 return -1;
1412 /* load the routers file, or assign the defaults. */
1413 if (router_reload_router_list()) {
1414 return -1;
1416 /* load the networkstatuses. (This launches a download for new routers as
1417 * appropriate.)
1419 now = time(NULL);
1420 directory_info_has_arrived(now, 1);
1422 if (authdir_mode_tests_reachability(get_options())) {
1423 /* the directory is already here, run startup things */
1424 dirserv_test_reachability(now, 1);
1427 if (server_mode(get_options())) {
1428 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1429 cpu_init();
1432 /* set up once-a-second callback. */
1433 second_elapsed_callback(0,0,NULL);
1435 for (;;) {
1436 if (nt_service_is_stopping())
1437 return 0;
1439 #ifndef MS_WINDOWS
1440 /* Make it easier to tell whether libevent failure is our fault or not. */
1441 errno = 0;
1442 #endif
1443 /* All active linked conns should get their read events activated. */
1444 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1445 event_active(conn->read_event, EV_READ, 1));
1446 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1448 update_approx_time(time(NULL));
1450 /* poll until we have an event, or the second ends, or until we have
1451 * some active linked connections to trigger events for. */
1452 loop_result = event_loop(called_loop_once ? EVLOOP_ONCE : 0);
1454 /* let catch() handle things like ^c, and otherwise don't worry about it */
1455 if (loop_result < 0) {
1456 int e = tor_socket_errno(-1);
1457 /* let the program survive things like ^z */
1458 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1459 #ifdef HAVE_EVENT_GET_METHOD
1460 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1461 event_get_method(), tor_socket_strerror(e), e);
1462 #else
1463 log_err(LD_NET,"libevent call failed: %s [%d]",
1464 tor_socket_strerror(e), e);
1465 #endif
1466 return -1;
1467 #ifndef MS_WINDOWS
1468 } else if (e == EINVAL) {
1469 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1470 if (got_libevent_error())
1471 return -1;
1472 #endif
1473 } else {
1474 if (ERRNO_IS_EINPROGRESS(e))
1475 log_warn(LD_BUG,
1476 "libevent call returned EINPROGRESS? Please report.");
1477 log_debug(LD_NET,"libevent call interrupted.");
1478 /* You can't trust the results of this poll(). Go back to the
1479 * top of the big for loop. */
1480 continue;
1486 /** Used to implement the SIGNAL control command: if we accept
1487 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1488 /* We don't re-use catch() here because:
1489 * 1. We handle a different set of signals than those allowed in catch.
1490 * 2. Platforms without signal() are unlikely to define SIGfoo.
1491 * 3. The control spec is defined to use fixed numeric signal values
1492 * which just happen to match the Unix values.
1494 void
1495 control_signal_act(int the_signal)
1497 switch (the_signal)
1499 case 1:
1500 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1501 break;
1502 case 2:
1503 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1504 break;
1505 case 10:
1506 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1507 break;
1508 case 12:
1509 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1510 break;
1511 case 15:
1512 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1513 break;
1514 case SIGNEWNYM:
1515 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1516 break;
1517 case SIGCLEARDNSCACHE:
1518 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1519 break;
1520 default:
1521 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1522 break;
1526 /** Libevent callback: invoked when we get a signal.
1528 static void
1529 signal_callback(int fd, short events, void *arg)
1531 uintptr_t sig = (uintptr_t)arg;
1532 (void)fd;
1533 (void)events;
1534 switch (sig)
1536 case SIGTERM:
1537 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1538 tor_cleanup();
1539 exit(0);
1540 break;
1541 case SIGINT:
1542 if (!server_mode(get_options())) { /* do it now */
1543 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1544 tor_cleanup();
1545 exit(0);
1547 hibernate_begin_shutdown();
1548 break;
1549 #ifdef SIGPIPE
1550 case SIGPIPE:
1551 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
1552 break;
1553 #endif
1554 case SIGUSR1:
1555 /* prefer to log it at INFO, but make sure we always see it */
1556 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1557 break;
1558 case SIGUSR2:
1559 switch_logs_debug();
1560 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1561 "Send HUP to change back.");
1562 break;
1563 case SIGHUP:
1564 if (do_hup() < 0) {
1565 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1566 tor_cleanup();
1567 exit(1);
1569 break;
1570 #ifdef SIGCHLD
1571 case SIGCHLD:
1572 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1573 zombies */
1574 break;
1575 #endif
1576 case SIGNEWNYM: {
1577 time_t now = time(NULL);
1578 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1579 signewnym_is_pending = 1;
1580 log(LOG_NOTICE, LD_CONTROL,
1581 "Rate limiting NEWNYM request: delaying by %d second(s)",
1582 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1583 } else {
1584 signewnym_impl(now);
1586 break;
1588 case SIGCLEARDNSCACHE:
1589 addressmap_clear_transient();
1590 break;
1594 extern uint64_t rephist_total_alloc;
1595 extern uint32_t rephist_total_num;
1598 * Write current memory usage information to the log.
1600 static void
1601 dumpmemusage(int severity)
1603 connection_dump_buffer_mem_stats(severity);
1604 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1605 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1606 dump_routerlist_mem_usage(severity);
1607 dump_cell_pool_usage(severity);
1608 buf_dump_freelist_sizes(severity);
1609 tor_log_mallinfo(severity);
1612 /** Write all statistics to the log, with log level <b>severity</b>. Called
1613 * in response to a SIGUSR1. */
1614 static void
1615 dumpstats(int severity)
1617 time_t now = time(NULL);
1618 time_t elapsed;
1619 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
1621 log(severity, LD_GENERAL, "Dumping stats:");
1623 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1625 int i = conn_sl_idx;
1626 log(severity, LD_GENERAL,
1627 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1628 i, conn->s, conn->type, conn_type_to_string(conn->type),
1629 conn->state, conn_state_to_string(conn->type, conn->state),
1630 (int)(now - conn->timestamp_created));
1631 if (!connection_is_listener(conn)) {
1632 log(severity,LD_GENERAL,
1633 "Conn %d is to %s:%d.", i,
1634 safe_str(conn->address), conn->port);
1635 log(severity,LD_GENERAL,
1636 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1638 (int)buf_datalen(conn->inbuf),
1639 (int)buf_allocation(conn->inbuf),
1640 (int)(now - conn->timestamp_lastread));
1641 log(severity,LD_GENERAL,
1642 "Conn %d: %d bytes waiting on outbuf "
1643 "(len %d, last written %d secs ago)",i,
1644 (int)buf_datalen(conn->outbuf),
1645 (int)buf_allocation(conn->outbuf),
1646 (int)(now - conn->timestamp_lastwritten));
1647 if (conn->type == CONN_TYPE_OR) {
1648 or_connection_t *or_conn = TO_OR_CONN(conn);
1649 if (or_conn->tls) {
1650 tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
1651 &wbuf_cap, &wbuf_len);
1652 log(severity, LD_GENERAL,
1653 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
1654 "%d/%d bytes used on write buffer.",
1655 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
1659 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1660 * using this conn */
1662 log(severity, LD_NET,
1663 "Cells processed: "U64_FORMAT" padding\n"
1664 " "U64_FORMAT" create\n"
1665 " "U64_FORMAT" created\n"
1666 " "U64_FORMAT" relay\n"
1667 " ("U64_FORMAT" relayed)\n"
1668 " ("U64_FORMAT" delivered)\n"
1669 " "U64_FORMAT" destroy",
1670 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1671 U64_PRINTF_ARG(stats_n_create_cells_processed),
1672 U64_PRINTF_ARG(stats_n_created_cells_processed),
1673 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1674 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1675 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1676 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1677 if (stats_n_data_cells_packaged)
1678 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1679 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1680 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1681 if (stats_n_data_cells_received)
1682 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1683 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1684 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1686 if (now - time_of_process_start >= 0)
1687 elapsed = now - time_of_process_start;
1688 else
1689 elapsed = 0;
1691 if (elapsed) {
1692 log(severity, LD_NET,
1693 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1694 U64_PRINTF_ARG(stats_n_bytes_read),
1695 (int)elapsed,
1696 (int) (stats_n_bytes_read/elapsed));
1697 log(severity, LD_NET,
1698 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1699 U64_PRINTF_ARG(stats_n_bytes_written),
1700 (int)elapsed,
1701 (int) (stats_n_bytes_written/elapsed));
1704 log(severity, LD_NET, "--------------- Dumping memory information:");
1705 dumpmemusage(severity);
1707 rep_hist_dump_stats(now,severity);
1708 rend_service_dump_stats(severity);
1709 dump_pk_ops(severity);
1710 dump_distinct_digest_count(severity);
1713 /** Called by exit() as we shut down the process.
1715 static void
1716 exit_function(void)
1718 /* NOTE: If we ever daemonize, this gets called immediately. That's
1719 * okay for now, because we only use this on Windows. */
1720 #ifdef MS_WINDOWS
1721 WSACleanup();
1722 #endif
1725 /** Set up the signal handlers for either parent or child. */
1726 void
1727 handle_signals(int is_parent)
1729 #ifndef MS_WINDOWS /* do signal stuff only on Unix */
1730 int i;
1731 static int signals[] = {
1732 SIGINT, /* do a controlled slow shutdown */
1733 SIGTERM, /* to terminate now */
1734 SIGPIPE, /* otherwise SIGPIPE kills us */
1735 SIGUSR1, /* dump stats */
1736 SIGUSR2, /* go to loglevel debug */
1737 SIGHUP, /* to reload config, retry conns, etc */
1738 #ifdef SIGXFSZ
1739 SIGXFSZ, /* handle file-too-big resource exhaustion */
1740 #endif
1741 SIGCHLD, /* handle dns/cpu workers that exit */
1742 -1 };
1743 static struct event signal_events[16]; /* bigger than it has to be. */
1744 if (is_parent) {
1745 for (i = 0; signals[i] >= 0; ++i) {
1746 signal_set(&signal_events[i], signals[i], signal_callback,
1747 (void*)(uintptr_t)signals[i]);
1748 if (signal_add(&signal_events[i], NULL))
1749 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1750 signals[i]);
1752 } else {
1753 struct sigaction action;
1754 action.sa_flags = 0;
1755 sigemptyset(&action.sa_mask);
1756 action.sa_handler = SIG_IGN;
1757 sigaction(SIGINT, &action, NULL);
1758 sigaction(SIGTERM, &action, NULL);
1759 sigaction(SIGPIPE, &action, NULL);
1760 sigaction(SIGUSR1, &action, NULL);
1761 sigaction(SIGUSR2, &action, NULL);
1762 sigaction(SIGHUP, &action, NULL);
1763 #ifdef SIGXFSZ
1764 sigaction(SIGXFSZ, &action, NULL);
1765 #endif
1767 #else /* MS windows */
1768 (void)is_parent;
1769 #endif /* signal stuff */
1772 /** Main entry point for the Tor command-line client.
1774 /* static */ int
1775 tor_init(int argc, char *argv[])
1777 char buf[256];
1778 int i, quiet = 0;
1779 time_of_process_start = time(NULL);
1780 if (!connection_array)
1781 connection_array = smartlist_create();
1782 if (!closeable_connection_lst)
1783 closeable_connection_lst = smartlist_create();
1784 if (!active_linked_connection_lst)
1785 active_linked_connection_lst = smartlist_create();
1786 /* Have the log set up with our application name. */
1787 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1788 log_set_application_name(buf);
1789 /* Initialize the history structures. */
1790 rep_hist_init();
1791 /* Initialize the service cache. */
1792 rend_cache_init();
1793 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1794 * cheap. */
1796 /* We search for the "quiet" option first, since it decides whether we
1797 * will log anything at all to the command line. */
1798 for (i=1;i<argc;++i) {
1799 if (!strcmp(argv[i], "--hush"))
1800 quiet = 1;
1801 if (!strcmp(argv[i], "--quiet"))
1802 quiet = 2;
1804 /* give it somewhere to log to initially */
1805 switch (quiet) {
1806 case 2:
1807 /* no initial logging */
1808 break;
1809 case 1:
1810 add_temp_log(LOG_WARN);
1811 break;
1812 default:
1813 add_temp_log(LOG_NOTICE);
1816 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1817 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1818 get_uname());
1820 if (network_init()<0) {
1821 log_err(LD_BUG,"Error initializing network; exiting.");
1822 return -1;
1824 atexit(exit_function);
1826 if (options_init_from_torrc(argc,argv) < 0) {
1827 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1828 return -1;
1831 #ifndef MS_WINDOWS
1832 if (geteuid()==0)
1833 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1834 "and you probably shouldn't.");
1835 #endif
1837 if (crypto_global_init(get_options()->HardwareAccel)) {
1838 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
1839 return -1;
1842 return 0;
1845 /** A lockfile structure, used to prevent two Tors from messing with the
1846 * data directory at once. If this variable is non-NULL, we're holding
1847 * the lockfile. */
1848 static tor_lockfile_t *lockfile = NULL;
1850 /** Try to grab the lock file described in <b>options</b>, if we do not
1851 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
1852 * holding the lock, and exit if we can't get it after waiting. Otherwise,
1853 * return -1 if we can't get the lockfile. Return 0 on success.
1856 try_locking(or_options_t *options, int err_if_locked)
1858 if (lockfile)
1859 return 0;
1860 else {
1861 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
1862 int already_locked = 0;
1863 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
1864 tor_free(fname);
1865 if (!lf) {
1866 if (err_if_locked && already_locked) {
1867 int r;
1868 log_warn(LD_GENERAL, "It looks like another Tor process is running "
1869 "with the same data directory. Waiting 5 seconds to see "
1870 "if it goes away.");
1871 #ifndef WIN32
1872 sleep(5);
1873 #else
1874 Sleep(5000);
1875 #endif
1876 r = try_locking(options, 0);
1877 if (r<0) {
1878 log_err(LD_GENERAL, "No, it's still there. Exiting.");
1879 exit(0);
1881 return r;
1883 return -1;
1885 lockfile = lf;
1886 return 0;
1890 /** Return true iff we've successfully acquired the lock file. */
1892 have_lockfile(void)
1894 return lockfile != NULL;
1897 /** If we have successfully acquired the lock file, release it. */
1898 void
1899 release_lockfile(void)
1901 if (lockfile) {
1902 tor_lockfile_unlock(lockfile);
1903 lockfile = NULL;
1907 /** Free all memory that we might have allocated somewhere.
1908 * If <b>postfork</b>, we are a worker process and we want to free
1909 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1910 * Tor is shutting down and we should free everything.
1912 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1913 * should then report 0 reachable in its leak report (in an ideal world --
1914 * in practice libevent, SSL, libc etc never quite free everything). */
1915 void
1916 tor_free_all(int postfork)
1918 if (!postfork) {
1919 evdns_shutdown(1);
1921 geoip_free_all();
1922 dirvote_free_all();
1923 routerlist_free_all();
1924 networkstatus_free_all();
1925 addressmap_free_all();
1926 dirserv_free_all();
1927 rend_service_free_all();
1928 rend_cache_free_all();
1929 rend_service_authorization_free_all();
1930 rep_hist_free_all();
1931 hs_usage_free_all();
1932 dns_free_all();
1933 clear_pending_onions();
1934 circuit_free_all();
1935 entry_guards_free_all();
1936 connection_free_all();
1937 buf_shrink_freelists(1);
1938 memarea_clear_freelist();
1939 if (!postfork) {
1940 config_free_all();
1941 router_free_all();
1942 policies_free_all();
1944 free_cell_pool();
1945 if (!postfork) {
1946 tor_tls_free_all();
1948 /* stuff in main.c */
1949 if (connection_array)
1950 smartlist_free(connection_array);
1951 if (closeable_connection_lst)
1952 smartlist_free(closeable_connection_lst);
1953 if (active_linked_connection_lst)
1954 smartlist_free(active_linked_connection_lst);
1955 tor_free(timeout_event);
1956 if (!postfork) {
1957 release_lockfile();
1959 /* Stuff in util.c and address.c*/
1960 if (!postfork) {
1961 escaped(NULL);
1962 esc_router_info(NULL);
1963 logs_free_all(); /* free log strings. do this last so logs keep working. */
1967 /** Do whatever cleanup is necessary before shutting Tor down. */
1968 void
1969 tor_cleanup(void)
1971 or_options_t *options = get_options();
1972 /* Remove our pid file. We don't care if there was an error when we
1973 * unlink, nothing we could do about it anyways. */
1974 if (options->command == CMD_RUN_TOR) {
1975 time_t now = time(NULL);
1976 if (options->PidFile)
1977 unlink(options->PidFile);
1978 if (accounting_is_enabled(options))
1979 accounting_record_bandwidth_usage(now, get_or_state());
1980 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
1981 or_state_save(now);
1982 if (authdir_mode_tests_reachability(options))
1983 rep_hist_record_mtbf_data(now, 0);
1985 #ifdef USE_DMALLOC
1986 dmalloc_log_stats();
1987 #endif
1988 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
1989 later, if it makes shutdown unacceptably slow. But for
1990 now, leave it here: it's helped us catch bugs in the
1991 past. */
1992 crypto_global_cleanup();
1993 #ifdef USE_DMALLOC
1994 dmalloc_log_unfreed();
1995 dmalloc_shutdown();
1996 #endif
1999 /** Read/create keys as needed, and echo our fingerprint to stdout. */
2000 /* static */ int
2001 do_list_fingerprint(void)
2003 char buf[FINGERPRINT_LEN+1];
2004 crypto_pk_env_t *k;
2005 const char *nickname = get_options()->Nickname;
2006 if (!server_mode(get_options())) {
2007 log_err(LD_GENERAL,
2008 "Clients don't have long-term identity keys. Exiting.\n");
2009 return -1;
2011 tor_assert(nickname);
2012 if (init_keys() < 0) {
2013 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2014 return -1;
2016 if (!(k = get_identity_key())) {
2017 log_err(LD_GENERAL,"Error: missing identity key.");
2018 return -1;
2020 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2021 log_err(LD_BUG, "Error computing fingerprint");
2022 return -1;
2024 printf("%s %s\n", nickname, buf);
2025 return 0;
2028 /** Entry point for password hashing: take the desired password from
2029 * the command line, and print its salted hash to stdout. **/
2030 /* static */ void
2031 do_hash_password(void)
2034 char output[256];
2035 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
2037 crypto_rand(key, S2K_SPECIFIER_LEN-1);
2038 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2039 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
2040 get_options()->command_arg, strlen(get_options()->command_arg),
2041 key);
2042 base16_encode(output, sizeof(output), key, sizeof(key));
2043 printf("16:%s\n",output);
2046 /** Main entry point for the Tor process. Called from main(). */
2047 /* This function is distinct from main() only so we can link main.c into
2048 * the unittest binary without conflicting with the unittests' main. */
2050 tor_main(int argc, char *argv[])
2052 int result = 0;
2053 update_approx_time(time(NULL));
2054 tor_threads_init();
2055 init_logging();
2056 #ifdef USE_DMALLOC
2058 /* Instruct OpenSSL to use our internal wrappers for malloc,
2059 realloc and free. */
2060 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
2061 tor_assert(r);
2063 #endif
2064 #ifdef NT_SERVICE
2066 int done = 0;
2067 result = nt_service_parse_options(argc, argv, &done);
2068 if (done) return result;
2070 #endif
2071 if (tor_init(argc, argv)<0)
2072 return -1;
2073 switch (get_options()->command) {
2074 case CMD_RUN_TOR:
2075 #ifdef NT_SERVICE
2076 nt_service_set_state(SERVICE_RUNNING);
2077 #endif
2078 result = do_main_loop();
2079 break;
2080 case CMD_LIST_FINGERPRINT:
2081 result = do_list_fingerprint();
2082 break;
2083 case CMD_HASH_PASSWORD:
2084 do_hash_password();
2085 result = 0;
2086 break;
2087 case CMD_VERIFY_CONFIG:
2088 printf("Configuration was valid\n");
2089 result = 0;
2090 break;
2091 case CMD_RUN_UNITTESTS: /* only set by test.c */
2092 default:
2093 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2094 get_options()->command);
2095 result = -1;
2097 tor_cleanup();
2098 return result;