Fix compile warnings on Snow Leopard
[tor/rransom.git] / src / or / main.c
blobca09af05611c4b8c8a432ced3827ef5d39b170bc
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-2009, 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 /** Perform regular maintenance tasks for a single connection. This
667 * function gets run once per second per connection by run_scheduled_events.
669 static void
670 run_connection_housekeeping(int i, time_t now)
672 cell_t cell;
673 connection_t *conn = smartlist_get(connection_array, i);
674 or_options_t *options = get_options();
675 or_connection_t *or_conn;
677 if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
678 TO_OR_CONN(conn)->timestamp_lastempty = now;
680 if (conn->marked_for_close) {
681 /* nothing to do here */
682 return;
685 /* Expire any directory connections that haven't been active (sent
686 * if a server or received if a client) for 5 min */
687 if (conn->type == CONN_TYPE_DIR &&
688 ((DIR_CONN_IS_SERVER(conn) &&
689 conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
690 (!DIR_CONN_IS_SERVER(conn) &&
691 conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
692 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
693 conn->s, conn->purpose);
694 /* This check is temporary; it's to let us know whether we should consider
695 * parsing partial serverdesc responses. */
696 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
697 buf_datalen(conn->inbuf)>=1024) {
698 log_info(LD_DIR,"Trying to extract information from wedged server desc "
699 "download.");
700 connection_dir_reached_eof(TO_DIR_CONN(conn));
701 } else {
702 connection_mark_for_close(conn);
704 return;
707 if (!connection_speaks_cells(conn))
708 return; /* we're all done here, the rest is just for OR conns */
710 or_conn = TO_OR_CONN(conn);
712 if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
713 /* It's bad for new circuits, and has no unmarked circuits on it:
714 * mark it now. */
715 log_info(LD_OR,
716 "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
717 conn->s, conn->address, conn->port);
718 if (conn->state == OR_CONN_STATE_CONNECTING)
719 connection_or_connect_failed(TO_OR_CONN(conn),
720 END_OR_CONN_REASON_TIMEOUT,
721 "Tor gave up on the connection");
722 connection_mark_for_close(conn);
723 conn->hold_open_until_flushed = 1;
724 return;
727 /* If we haven't written to an OR connection for a while, then either nuke
728 the connection or send a keepalive, depending. */
729 if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
730 routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
731 int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
732 if (!connection_state_is_open(conn)) {
733 /* We never managed to actually get this connection open and happy. */
734 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
735 conn->s,conn->address, conn->port);
736 connection_mark_for_close(conn);
737 conn->hold_open_until_flushed = 1;
738 } else if (we_are_hibernating() && !or_conn->n_circuits &&
739 !buf_datalen(conn->outbuf)) {
740 /* We're hibernating, there's no circuits, and nothing to flush.*/
741 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
742 "[Hibernating or exiting].",
743 conn->s,conn->address, conn->port);
744 connection_mark_for_close(conn);
745 conn->hold_open_until_flushed = 1;
746 } else if (!clique_mode(options) && !or_conn->n_circuits &&
747 now >= or_conn->timestamp_last_added_nonpadding +
748 maxCircuitlessPeriod &&
749 (!router || !server_mode(options) ||
750 !router_is_clique_mode(router))) {
751 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
752 "[Not in clique mode].",
753 conn->s,conn->address, conn->port);
754 connection_mark_for_close(conn);
755 conn->hold_open_until_flushed = 1;
756 } else if (
757 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
758 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
759 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
760 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
761 "flush; %d seconds since last write)",
762 conn->s, conn->address, conn->port,
763 (int)buf_datalen(conn->outbuf),
764 (int)(now-conn->timestamp_lastwritten));
765 connection_mark_for_close(conn);
766 } else if (!buf_datalen(conn->outbuf)) {
767 /* either in clique mode, or we've got a circuit. send a padding cell. */
768 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
769 conn->address, conn->port);
770 memset(&cell,0,sizeof(cell_t));
771 cell.command = CELL_PADDING;
772 connection_or_write_cell_to_buf(&cell, or_conn);
777 /** Honor a NEWNYM request: make future requests unlinkability to past
778 * requests. */
779 static void
780 signewnym_impl(time_t now)
782 circuit_expire_all_dirty_circs();
783 addressmap_clear_transient();
784 time_of_last_signewnym = now;
785 signewnym_is_pending = 0;
788 /** Perform regular maintenance tasks. This function gets run once per
789 * second by second_elapsed_callback().
791 static void
792 run_scheduled_events(time_t now)
794 static time_t last_rotated_x509_certificate = 0;
795 static time_t time_to_check_v3_certificate = 0;
796 static time_t time_to_check_listeners = 0;
797 static time_t time_to_check_descriptor = 0;
798 static time_t time_to_check_ipaddress = 0;
799 static time_t time_to_shrink_memory = 0;
800 static time_t time_to_try_getting_descriptors = 0;
801 static time_t time_to_reset_descriptor_failures = 0;
802 static time_t time_to_add_entropy = 0;
803 static time_t time_to_write_hs_statistics = 0;
804 static time_t time_to_write_bridge_status_file = 0;
805 static time_t time_to_downrate_stability = 0;
806 static time_t time_to_save_stability = 0;
807 static time_t time_to_clean_caches = 0;
808 static time_t time_to_recheck_bandwidth = 0;
809 static time_t time_to_check_for_expired_networkstatus = 0;
810 static time_t time_to_dump_geoip_stats = 0;
811 static time_t time_to_retry_dns_init = 0;
812 or_options_t *options = get_options();
813 int i;
814 int have_dir_info;
816 /** 0. See if we've been asked to shut down and our timeout has
817 * expired; or if our bandwidth limits are exhausted and we
818 * should hibernate; or if it's time to wake up from hibernation.
820 consider_hibernation(now);
822 /* 0b. If we've deferred a signewnym, make sure it gets handled
823 * eventually. */
824 if (signewnym_is_pending &&
825 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
826 log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
827 signewnym_impl(now);
830 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
831 * shut down and restart all cpuworkers, and update the directory if
832 * necessary.
834 if (server_mode(options) &&
835 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
836 log_info(LD_GENERAL,"Rotating onion key.");
837 rotate_onion_key();
838 cpuworkers_rotate();
839 if (router_rebuild_descriptor(1)<0) {
840 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
842 if (advertised_server_mode())
843 router_upload_dir_desc_to_dirservers(0);
846 if (time_to_try_getting_descriptors < now) {
847 update_router_descriptor_downloads(now);
848 update_extrainfo_downloads(now);
849 if (options->UseBridges)
850 fetch_bridge_descriptors(now);
851 if (router_have_minimum_dir_info())
852 time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
853 else
854 time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
857 if (time_to_reset_descriptor_failures < now) {
858 router_reset_descriptor_download_failures();
859 time_to_reset_descriptor_failures =
860 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
863 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
864 if (!last_rotated_x509_certificate)
865 last_rotated_x509_certificate = now;
866 if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
867 log_info(LD_GENERAL,"Rotating tls context.");
868 if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
869 log_warn(LD_BUG, "Error reinitializing TLS context");
870 /* XXX is it a bug here, that we just keep going? -RD */
872 last_rotated_x509_certificate = now;
873 /* We also make sure to rotate the TLS connections themselves if they've
874 * been up for too long -- but that's done via is_bad_for_new_circs in
875 * connection_run_housekeeping() above. */
878 if (time_to_add_entropy < now) {
879 if (time_to_add_entropy) {
880 /* We already seeded once, so don't die on failure. */
881 crypto_seed_rng(0);
883 /** How often do we add more entropy to OpenSSL's RNG pool? */
884 #define ENTROPY_INTERVAL (60*60)
885 time_to_add_entropy = now + ENTROPY_INTERVAL;
888 /** 1c. If we have to change the accounting interval or record
889 * bandwidth used in this accounting interval, do so. */
890 if (accounting_is_enabled(options))
891 accounting_run_housekeeping(now);
893 if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
894 !we_are_hibernating()) {
895 /* try to determine reachability of the other Tor relays */
896 dirserv_test_reachability(now, 0);
899 /** 1d. Periodically, we discount older stability information so that new
900 * stability info counts more, and save the stability information to disk as
901 * appropriate. */
902 if (time_to_downrate_stability < now)
903 time_to_downrate_stability = rep_hist_downrate_old_runs(now);
904 if (authdir_mode_tests_reachability(options)) {
905 if (time_to_save_stability < now) {
906 if (time_to_save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
907 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
909 #define SAVE_STABILITY_INTERVAL (30*60)
910 time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
914 /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
915 * close to expiring and warn the admin if it is. */
916 if (time_to_check_v3_certificate < now) {
917 v3_authority_check_key_expiry();
918 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
919 time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
922 /* 1f. Check whether our networkstatus has expired.
924 if (time_to_check_for_expired_networkstatus < now) {
925 networkstatus_t *ns = networkstatus_get_latest_consensus();
926 /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
927 * networkstatus_get_reasonably_live_consensus(), but that value is way
928 * way too high. Arma: is the bridge issue there resolved yet? -NM */
929 #define NS_EXPIRY_SLOP (24*60*60)
930 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
931 router_have_minimum_dir_info()) {
932 router_dir_info_changed();
934 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
935 time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
938 if (time_to_dump_geoip_stats < now) {
939 #define DUMP_GEOIP_STATS_INTERVAL (60*60);
940 if (time_to_dump_geoip_stats)
941 dump_geoip_stats();
942 time_to_dump_geoip_stats = now + DUMP_GEOIP_STATS_INTERVAL;
945 /* Remove old information from rephist and the rend cache. */
946 if (time_to_clean_caches < now) {
947 rep_history_clean(now - options->RephistTrackTime);
948 rend_cache_clean();
949 rend_cache_clean_v2_descs_as_dir();
950 #define CLEAN_CACHES_INTERVAL (30*60)
951 time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
954 #define RETRY_DNS_INTERVAL (10*60)
955 /* If we're a server and initializing dns failed, retry periodically. */
956 if (time_to_retry_dns_init < now) {
957 time_to_retry_dns_init = now + RETRY_DNS_INTERVAL;
958 if (server_mode(options) && has_dns_init_failed())
959 dns_init();
962 /** 2. Periodically, we consider force-uploading our descriptor
963 * (if we've passed our internal checks). */
965 /** How often do we check whether part of our router info has changed in a way
966 * that would require an upload? */
967 #define CHECK_DESCRIPTOR_INTERVAL (60)
968 /** How often do we (as a router) check whether our IP address has changed? */
969 #define CHECK_IPADDRESS_INTERVAL (15*60)
971 /* 2b. Once per minute, regenerate and upload the descriptor if the old
972 * one is inaccurate. */
973 if (time_to_check_descriptor < now) {
974 static int dirport_reachability_count = 0;
975 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
976 check_descriptor_bandwidth_changed(now);
977 if (time_to_check_ipaddress < now) {
978 time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
979 check_descriptor_ipaddress_changed(now);
981 /** If our router descriptor ever goes this long without being regenerated
982 * because something changed, we force an immediate regenerate-and-upload. */
983 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
984 mark_my_descriptor_dirty_if_older_than(
985 now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
986 consider_publishable_server(0);
987 /* also, check religiously for reachability, if it's within the first
988 * 20 minutes of our uptime. */
989 if (server_mode(options) &&
990 (has_completed_circuit || !any_predicted_circuits(now)) &&
991 !we_are_hibernating()) {
992 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
993 consider_testing_reachability(1, dirport_reachability_count==0);
994 if (++dirport_reachability_count > 5)
995 dirport_reachability_count = 0;
996 } else if (time_to_recheck_bandwidth < now) {
997 /* If we haven't checked for 12 hours and our bandwidth estimate is
998 * low, do another bandwidth test. This is especially important for
999 * bridges, since they might go long periods without much use. */
1000 routerinfo_t *me = router_get_my_routerinfo();
1001 if (time_to_recheck_bandwidth && me &&
1002 me->bandwidthcapacity < me->bandwidthrate &&
1003 me->bandwidthcapacity < 51200) {
1004 reset_bandwidth_test();
1006 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1007 time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1011 /* If any networkstatus documents are no longer recent, we need to
1012 * update all the descriptors' running status. */
1013 /* purge obsolete entries */
1014 networkstatus_v2_list_clean(now);
1015 /* Remove dead routers. */
1016 routerlist_remove_old_routers();
1018 /* Also, once per minute, check whether we want to download any
1019 * networkstatus documents.
1021 update_networkstatus_downloads(now);
1024 /** 2c. Let directory voting happen. */
1025 if (authdir_mode_v3(options))
1026 dirvote_act(options, now);
1028 /** 3a. Every second, we examine pending circuits and prune the
1029 * ones which have been pending for more than a few seconds.
1030 * We do this before step 4, so it can try building more if
1031 * it's not comfortable with the number of available circuits.
1033 circuit_expire_building(now);
1035 /** 3b. Also look at pending streams and prune the ones that 'began'
1036 * a long time ago but haven't gotten a 'connected' yet.
1037 * Do this before step 4, so we can put them back into pending
1038 * state to be picked up by the new circuit.
1040 connection_ap_expire_beginning();
1042 /** 3c. And expire connections that we've held open for too long.
1044 connection_expire_held_open();
1046 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
1047 if (!we_are_hibernating() && time_to_check_listeners < now) {
1048 retry_all_listeners(NULL, NULL);
1049 time_to_check_listeners = now+60;
1052 /** 4. Every second, we try a new circuit if there are no valid
1053 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1054 * that became dirty more than MaxCircuitDirtiness seconds ago,
1055 * and we make a new circ if there are no clean circuits.
1057 have_dir_info = router_have_minimum_dir_info();
1058 if (have_dir_info && !we_are_hibernating())
1059 circuit_build_needed_circs(now);
1061 /** 5. We do housekeeping for each connection... */
1062 connection_or_set_bad_connections();
1063 for (i=0;i<smartlist_len(connection_array);i++) {
1064 run_connection_housekeeping(i, now);
1066 if (time_to_shrink_memory < now) {
1067 SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
1068 if (conn->outbuf)
1069 buf_shrink(conn->outbuf);
1070 if (conn->inbuf)
1071 buf_shrink(conn->inbuf);
1073 clean_cell_pool();
1074 buf_shrink_freelists(0);
1075 /** How often do we check buffers and pools for empty space that can be
1076 * deallocated? */
1077 #define MEM_SHRINK_INTERVAL (60)
1078 time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
1081 /** 6. And remove any marked circuits... */
1082 circuit_close_all_marked();
1084 /** 7. And upload service descriptors if necessary. */
1085 if (has_completed_circuit && !we_are_hibernating()) {
1086 rend_consider_services_upload(now);
1087 rend_consider_descriptor_republication();
1090 /** 8. and blow away any connections that need to die. have to do this now,
1091 * because if we marked a conn for close and left its socket -1, then
1092 * we'll pass it to poll/select and bad things will happen.
1094 close_closeable_connections();
1096 /** 8b. And if anything in our state is ready to get flushed to disk, we
1097 * flush it. */
1098 or_state_save(now);
1100 /** 9. and if we're a server, check whether our DNS is telling stories to
1101 * us. */
1102 if (server_mode(options) && time_to_check_for_correct_dns < now) {
1103 if (!time_to_check_for_correct_dns) {
1104 time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
1105 } else {
1106 dns_launch_correctness_checks();
1107 time_to_check_for_correct_dns = now + 12*3600 +
1108 crypto_rand_int(12*3600);
1112 /** 10. write hidden service usage statistic to disk */
1113 if (options->HSAuthorityRecordStats && time_to_write_hs_statistics < now) {
1114 hs_usage_write_statistics_to_file(now);
1115 #define WRITE_HSUSAGE_INTERVAL (30*60)
1116 time_to_write_hs_statistics = now+WRITE_HSUSAGE_INTERVAL;
1118 /** 10b. write bridge networkstatus file to disk */
1119 if (options->BridgeAuthoritativeDir &&
1120 time_to_write_bridge_status_file < now) {
1121 networkstatus_dump_bridge_status_to_file(now);
1122 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1123 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1127 /** Libevent timer: used to invoke second_elapsed_callback() once per
1128 * second. */
1129 static struct event *timeout_event = NULL;
1130 /** Number of libevent errors in the last second: we die if we get too many. */
1131 static int n_libevent_errors = 0;
1133 /** Libevent callback: invoked once every second. */
1134 static void
1135 second_elapsed_callback(int fd, short event, void *args)
1137 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1138 * could use Libevent's timers for this rather than checking the current
1139 * time against a bunch of timeouts every second. */
1140 static struct timeval one_second;
1141 static time_t current_second = 0;
1142 time_t now;
1143 size_t bytes_written;
1144 size_t bytes_read;
1145 int seconds_elapsed;
1146 or_options_t *options = get_options();
1147 (void)fd;
1148 (void)event;
1149 (void)args;
1150 if (!timeout_event) {
1151 timeout_event = tor_malloc_zero(sizeof(struct event));
1152 evtimer_set(timeout_event, second_elapsed_callback, NULL);
1153 one_second.tv_sec = 1;
1154 one_second.tv_usec = 0;
1157 n_libevent_errors = 0;
1159 /* log_fn(LOG_NOTICE, "Tick."); */
1160 now = time(NULL);
1161 update_approx_time(now);
1163 /* the second has rolled over. check more stuff. */
1164 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1165 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1166 seconds_elapsed = current_second ? (int)(now - current_second) : 0;
1167 stats_n_bytes_read += bytes_read;
1168 stats_n_bytes_written += bytes_written;
1169 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1170 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1171 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1172 control_event_stream_bandwidth_used();
1174 if (seconds_elapsed > 0)
1175 connection_bucket_refill(seconds_elapsed, now);
1176 stats_prev_global_read_bucket = global_read_bucket;
1177 stats_prev_global_write_bucket = global_write_bucket;
1179 if (server_mode(options) &&
1180 !we_are_hibernating() &&
1181 seconds_elapsed > 0 &&
1182 has_completed_circuit &&
1183 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1184 (stats_n_seconds_working+seconds_elapsed) /
1185 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1186 /* every 20 minutes, check and complain if necessary */
1187 routerinfo_t *me = router_get_my_routerinfo();
1188 if (me && !check_whether_orport_reachable()) {
1189 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1190 "its ORPort is reachable. Please check your firewalls, ports, "
1191 "address, /etc/hosts file, etc.",
1192 me->address, me->or_port);
1193 control_event_server_status(LOG_WARN,
1194 "REACHABILITY_FAILED ORADDRESS=%s:%d",
1195 me->address, me->or_port);
1198 if (me && !check_whether_dirport_reachable()) {
1199 log_warn(LD_CONFIG,
1200 "Your server (%s:%d) has not managed to confirm that its "
1201 "DirPort is reachable. Please check your firewalls, ports, "
1202 "address, /etc/hosts file, etc.",
1203 me->address, me->dir_port);
1204 control_event_server_status(LOG_WARN,
1205 "REACHABILITY_FAILED DIRADDRESS=%s:%d",
1206 me->address, me->dir_port);
1210 /** If more than this many seconds have elapsed, probably the clock
1211 * jumped: doesn't count. */
1212 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1213 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1214 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1215 circuit_note_clock_jumped(seconds_elapsed);
1216 /* XXX if the time jumps *back* many months, do our events in
1217 * run_scheduled_events() recover? I don't think they do. -RD */
1218 } else if (seconds_elapsed > 0)
1219 stats_n_seconds_working += seconds_elapsed;
1221 run_scheduled_events(now);
1223 current_second = now; /* remember which second it is, for next time */
1225 #if 0
1226 if (current_second % 300 == 0) {
1227 rep_history_clean(current_second - options->RephistTrackTime);
1228 dumpmemusage(get_min_log_level()<LOG_INFO ?
1229 get_min_log_level() : LOG_INFO);
1231 #endif
1233 if (evtimer_add(timeout_event, &one_second))
1234 log_err(LD_NET,
1235 "Error from libevent when setting one-second timeout event");
1238 #ifndef MS_WINDOWS
1239 /** Called when a possibly ignorable libevent error occurs; ensures that we
1240 * don't get into an infinite loop by ignoring too many errors from
1241 * libevent. */
1242 static int
1243 got_libevent_error(void)
1245 if (++n_libevent_errors > 8) {
1246 log_err(LD_NET, "Too many libevent errors in one second; dying");
1247 return -1;
1249 return 0;
1251 #endif
1253 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1255 /** Called when our IP address seems to have changed. <b>at_interface</b>
1256 * should be true if we detected a change in our interface, and false if we
1257 * detected a change in our published address. */
1258 void
1259 ip_address_changed(int at_interface)
1261 int server = server_mode(get_options());
1263 if (at_interface) {
1264 if (! server) {
1265 /* Okay, change our keys. */
1266 init_keys();
1268 } else {
1269 if (server) {
1270 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1271 reset_bandwidth_test();
1272 stats_n_seconds_working = 0;
1273 router_reset_reachability();
1274 mark_my_descriptor_dirty();
1278 dns_servers_relaunch_checks();
1281 /** Forget what we've learned about the correctness of our DNS servers, and
1282 * start learning again. */
1283 void
1284 dns_servers_relaunch_checks(void)
1286 if (server_mode(get_options())) {
1287 dns_reset_correctness_checks();
1288 time_to_check_for_correct_dns = 0;
1292 /** Called when we get a SIGHUP: reload configuration files and keys,
1293 * retry all connections, and so on. */
1294 static int
1295 do_hup(void)
1297 or_options_t *options = get_options();
1299 #ifdef USE_DMALLOC
1300 dmalloc_log_stats();
1301 dmalloc_log_changed(0, 1, 0, 0);
1302 #endif
1304 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
1305 "resetting internal state.");
1306 if (accounting_is_enabled(options))
1307 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1309 router_reset_warnings();
1310 routerlist_reset_warnings();
1311 addressmap_clear_transient();
1312 /* first, reload config variables, in case they've changed */
1313 if (options->ReloadTorrcOnSIGHUP) {
1314 /* no need to provide argc/v, they've been cached in init_from_config */
1315 if (options_init_from_torrc(0, NULL) < 0) {
1316 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1317 "For usage, try -h.");
1318 return -1;
1320 options = get_options(); /* they have changed now */
1321 } else {
1322 log_notice(LD_GENERAL, "Not reloading config file: the controller told "
1323 "us not to.");
1325 if (authdir_mode_handles_descs(options, -1)) {
1326 /* reload the approved-routers file */
1327 if (dirserv_load_fingerprint_file() < 0) {
1328 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1329 log_info(LD_GENERAL, "Error reloading fingerprints. "
1330 "Continuing with old list.");
1334 /* Rotate away from the old dirty circuits. This has to be done
1335 * after we've read the new options, but before we start using
1336 * circuits for directory fetches. */
1337 circuit_expire_all_dirty_circs();
1339 /* retry appropriate downloads */
1340 router_reset_status_download_failures();
1341 router_reset_descriptor_download_failures();
1342 update_networkstatus_downloads(time(NULL));
1344 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1345 * force a retry there. */
1347 if (server_mode(options)) {
1348 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1349 * configuration options. */
1350 cpuworkers_rotate();
1351 dns_reset();
1353 return 0;
1356 /** Tor main loop. */
1357 /* static */ int
1358 do_main_loop(void)
1360 int loop_result;
1361 time_t now;
1363 /* initialize dns resolve map, spawn workers if needed */
1364 if (dns_init() < 0) {
1365 if (get_options()->ServerDNSAllowBrokenConfig)
1366 log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
1367 "Network not up yet? Will try again soon.");
1368 else {
1369 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting. To "
1370 "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
1374 handle_signals(1);
1376 /* load the private keys, if we're supposed to have them, and set up the
1377 * TLS context. */
1378 if (! identity_key_is_set()) {
1379 if (init_keys() < 0) {
1380 log_err(LD_BUG,"Error initializing keys; exiting");
1381 return -1;
1385 /* Set up the packed_cell_t memory pool. */
1386 init_cell_pool();
1388 /* Set up our buckets */
1389 connection_bucket_init();
1390 stats_prev_global_read_bucket = global_read_bucket;
1391 stats_prev_global_write_bucket = global_write_bucket;
1393 /* initialize the bootstrap status events to know we're starting up */
1394 control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
1396 if (trusted_dirs_reload_certs())
1397 return -1;
1398 if (router_reload_v2_networkstatus()) {
1399 return -1;
1401 if (router_reload_consensus_networkstatus()) {
1402 return -1;
1404 /* load the routers file, or assign the defaults. */
1405 if (router_reload_router_list()) {
1406 return -1;
1408 /* load the networkstatuses. (This launches a download for new routers as
1409 * appropriate.)
1411 now = time(NULL);
1412 directory_info_has_arrived(now, 1);
1414 if (authdir_mode_tests_reachability(get_options())) {
1415 /* the directory is already here, run startup things */
1416 dirserv_test_reachability(now, 1);
1419 if (server_mode(get_options())) {
1420 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1421 cpu_init();
1424 /* set up once-a-second callback. */
1425 second_elapsed_callback(0,0,NULL);
1427 for (;;) {
1428 if (nt_service_is_stopping())
1429 return 0;
1431 #ifndef MS_WINDOWS
1432 /* Make it easier to tell whether libevent failure is our fault or not. */
1433 errno = 0;
1434 #endif
1435 /* All active linked conns should get their read events activated. */
1436 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1437 event_active(conn->read_event, EV_READ, 1));
1438 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1440 update_approx_time(time(NULL));
1442 /* poll until we have an event, or the second ends, or until we have
1443 * some active linked connections to trigger events for. */
1444 loop_result = event_loop(called_loop_once ? EVLOOP_ONCE : 0);
1446 /* let catch() handle things like ^c, and otherwise don't worry about it */
1447 if (loop_result < 0) {
1448 int e = tor_socket_errno(-1);
1449 /* let the program survive things like ^z */
1450 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1451 #ifdef HAVE_EVENT_GET_METHOD
1452 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1453 event_get_method(), tor_socket_strerror(e), e);
1454 #else
1455 log_err(LD_NET,"libevent call failed: %s [%d]",
1456 tor_socket_strerror(e), e);
1457 #endif
1458 return -1;
1459 #ifndef MS_WINDOWS
1460 } else if (e == EINVAL) {
1461 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1462 if (got_libevent_error())
1463 return -1;
1464 #endif
1465 } else {
1466 if (ERRNO_IS_EINPROGRESS(e))
1467 log_warn(LD_BUG,
1468 "libevent call returned EINPROGRESS? Please report.");
1469 log_debug(LD_NET,"libevent call interrupted.");
1470 /* You can't trust the results of this poll(). Go back to the
1471 * top of the big for loop. */
1472 continue;
1478 /** Used to implement the SIGNAL control command: if we accept
1479 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1480 /* We don't re-use catch() here because:
1481 * 1. We handle a different set of signals than those allowed in catch.
1482 * 2. Platforms without signal() are unlikely to define SIGfoo.
1483 * 3. The control spec is defined to use fixed numeric signal values
1484 * which just happen to match the Unix values.
1486 void
1487 control_signal_act(int the_signal)
1489 switch (the_signal)
1491 case 1:
1492 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1493 break;
1494 case 2:
1495 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1496 break;
1497 case 10:
1498 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1499 break;
1500 case 12:
1501 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1502 break;
1503 case 15:
1504 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1505 break;
1506 case SIGNEWNYM:
1507 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1508 break;
1509 case SIGCLEARDNSCACHE:
1510 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1511 break;
1512 default:
1513 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1514 break;
1518 /** Libevent callback: invoked when we get a signal.
1520 static void
1521 signal_callback(int fd, short events, void *arg)
1523 uintptr_t sig = (uintptr_t)arg;
1524 (void)fd;
1525 (void)events;
1526 switch (sig)
1528 case SIGTERM:
1529 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1530 tor_cleanup();
1531 exit(0);
1532 break;
1533 case SIGINT:
1534 if (!server_mode(get_options())) { /* do it now */
1535 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1536 tor_cleanup();
1537 exit(0);
1539 hibernate_begin_shutdown();
1540 break;
1541 #ifdef SIGPIPE
1542 case SIGPIPE:
1543 log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
1544 break;
1545 #endif
1546 case SIGUSR1:
1547 /* prefer to log it at INFO, but make sure we always see it */
1548 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1549 break;
1550 case SIGUSR2:
1551 switch_logs_debug();
1552 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1553 "Send HUP to change back.");
1554 break;
1555 case SIGHUP:
1556 if (do_hup() < 0) {
1557 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1558 tor_cleanup();
1559 exit(1);
1561 break;
1562 #ifdef SIGCHLD
1563 case SIGCHLD:
1564 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1565 zombies */
1566 break;
1567 #endif
1568 case SIGNEWNYM: {
1569 time_t now = time(NULL);
1570 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1571 signewnym_is_pending = 1;
1572 log(LOG_NOTICE, LD_CONTROL,
1573 "Rate limiting NEWNYM request: delaying by %d second(s)",
1574 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1575 } else {
1576 signewnym_impl(now);
1578 break;
1580 case SIGCLEARDNSCACHE:
1581 addressmap_clear_transient();
1582 break;
1586 extern uint64_t rephist_total_alloc;
1587 extern uint32_t rephist_total_num;
1590 * Write current memory usage information to the log.
1592 static void
1593 dumpmemusage(int severity)
1595 connection_dump_buffer_mem_stats(severity);
1596 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1597 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1598 dump_routerlist_mem_usage(severity);
1599 dump_cell_pool_usage(severity);
1600 buf_dump_freelist_sizes(severity);
1601 tor_log_mallinfo(severity);
1604 /** Write all statistics to the log, with log level 'severity'. Called
1605 * in response to a SIGUSR1. */
1606 static void
1607 dumpstats(int severity)
1609 time_t now = time(NULL);
1610 time_t elapsed;
1611 size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
1613 log(severity, LD_GENERAL, "Dumping stats:");
1615 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1617 int i = conn_sl_idx;
1618 log(severity, LD_GENERAL,
1619 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1620 i, conn->s, conn->type, conn_type_to_string(conn->type),
1621 conn->state, conn_state_to_string(conn->type, conn->state),
1622 (int)(now - conn->timestamp_created));
1623 if (!connection_is_listener(conn)) {
1624 log(severity,LD_GENERAL,
1625 "Conn %d is to %s:%d.", i,
1626 safe_str(conn->address), conn->port);
1627 log(severity,LD_GENERAL,
1628 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1630 (int)buf_datalen(conn->inbuf),
1631 (int)buf_allocation(conn->inbuf),
1632 (int)(now - conn->timestamp_lastread));
1633 log(severity,LD_GENERAL,
1634 "Conn %d: %d bytes waiting on outbuf "
1635 "(len %d, last written %d secs ago)",i,
1636 (int)buf_datalen(conn->outbuf),
1637 (int)buf_allocation(conn->outbuf),
1638 (int)(now - conn->timestamp_lastwritten));
1639 if (conn->type == CONN_TYPE_OR) {
1640 or_connection_t *or_conn = TO_OR_CONN(conn);
1641 if (or_conn->tls) {
1642 tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
1643 &wbuf_cap, &wbuf_len);
1644 log(severity, LD_GENERAL,
1645 "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
1646 "%d/%d bytes used on write buffer.",
1647 i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
1651 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1652 * using this conn */
1654 log(severity, LD_NET,
1655 "Cells processed: "U64_FORMAT" padding\n"
1656 " "U64_FORMAT" create\n"
1657 " "U64_FORMAT" created\n"
1658 " "U64_FORMAT" relay\n"
1659 " ("U64_FORMAT" relayed)\n"
1660 " ("U64_FORMAT" delivered)\n"
1661 " "U64_FORMAT" destroy",
1662 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1663 U64_PRINTF_ARG(stats_n_create_cells_processed),
1664 U64_PRINTF_ARG(stats_n_created_cells_processed),
1665 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1666 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1667 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1668 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1669 if (stats_n_data_cells_packaged)
1670 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1671 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1672 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1673 if (stats_n_data_cells_received)
1674 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1675 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1676 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1678 if (now - time_of_process_start >= 0)
1679 elapsed = now - time_of_process_start;
1680 else
1681 elapsed = 0;
1683 if (elapsed) {
1684 log(severity, LD_NET,
1685 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1686 U64_PRINTF_ARG(stats_n_bytes_read),
1687 (int)elapsed,
1688 (int) (stats_n_bytes_read/elapsed));
1689 log(severity, LD_NET,
1690 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1691 U64_PRINTF_ARG(stats_n_bytes_written),
1692 (int)elapsed,
1693 (int) (stats_n_bytes_written/elapsed));
1696 log(severity, LD_NET, "--------------- Dumping memory information:");
1697 dumpmemusage(severity);
1699 rep_hist_dump_stats(now,severity);
1700 rend_service_dump_stats(severity);
1701 dump_pk_ops(severity);
1702 dump_distinct_digest_count(severity);
1705 /** Called by exit() as we shut down the process.
1707 static void
1708 exit_function(void)
1710 /* NOTE: If we ever daemonize, this gets called immediately. That's
1711 * okay for now, because we only use this on Windows. */
1712 #ifdef MS_WINDOWS
1713 WSACleanup();
1714 #endif
1717 /** Set up the signal handlers for either parent or child. */
1718 void
1719 handle_signals(int is_parent)
1721 #ifndef MS_WINDOWS /* do signal stuff only on Unix */
1722 int i;
1723 static int signals[] = {
1724 SIGINT, /* do a controlled slow shutdown */
1725 SIGTERM, /* to terminate now */
1726 SIGPIPE, /* otherwise SIGPIPE kills us */
1727 SIGUSR1, /* dump stats */
1728 SIGUSR2, /* go to loglevel debug */
1729 SIGHUP, /* to reload config, retry conns, etc */
1730 #ifdef SIGXFSZ
1731 SIGXFSZ, /* handle file-too-big resource exhaustion */
1732 #endif
1733 SIGCHLD, /* handle dns/cpu workers that exit */
1734 -1 };
1735 static struct event signal_events[16]; /* bigger than it has to be. */
1736 if (is_parent) {
1737 for (i = 0; signals[i] >= 0; ++i) {
1738 signal_set(&signal_events[i], signals[i], signal_callback,
1739 (void*)(uintptr_t)signals[i]);
1740 if (signal_add(&signal_events[i], NULL))
1741 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1742 signals[i]);
1744 } else {
1745 struct sigaction action;
1746 action.sa_flags = 0;
1747 sigemptyset(&action.sa_mask);
1748 action.sa_handler = SIG_IGN;
1749 sigaction(SIGINT, &action, NULL);
1750 sigaction(SIGTERM, &action, NULL);
1751 sigaction(SIGPIPE, &action, NULL);
1752 sigaction(SIGUSR1, &action, NULL);
1753 sigaction(SIGUSR2, &action, NULL);
1754 sigaction(SIGHUP, &action, NULL);
1755 #ifdef SIGXFSZ
1756 sigaction(SIGXFSZ, &action, NULL);
1757 #endif
1759 #else /* MS windows */
1760 (void)is_parent;
1761 #endif /* signal stuff */
1764 /** Main entry point for the Tor command-line client.
1766 /* static */ int
1767 tor_init(int argc, char *argv[])
1769 char buf[256];
1770 int i, quiet = 0;
1771 time_of_process_start = time(NULL);
1772 if (!connection_array)
1773 connection_array = smartlist_create();
1774 if (!closeable_connection_lst)
1775 closeable_connection_lst = smartlist_create();
1776 if (!active_linked_connection_lst)
1777 active_linked_connection_lst = smartlist_create();
1778 /* Have the log set up with our application name. */
1779 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1780 log_set_application_name(buf);
1781 /* Initialize the history structures. */
1782 rep_hist_init();
1783 /* Initialize the service cache. */
1784 rend_cache_init();
1785 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1786 * cheap. */
1788 /* We search for the "quiet" option first, since it decides whether we
1789 * will log anything at all to the command line. */
1790 for (i=1;i<argc;++i) {
1791 if (!strcmp(argv[i], "--hush"))
1792 quiet = 1;
1793 if (!strcmp(argv[i], "--quiet"))
1794 quiet = 2;
1796 /* give it somewhere to log to initially */
1797 switch (quiet) {
1798 case 2:
1799 /* no initial logging */
1800 break;
1801 case 1:
1802 add_temp_log(LOG_WARN);
1803 break;
1804 default:
1805 add_temp_log(LOG_NOTICE);
1808 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1809 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1810 get_uname());
1812 if (network_init()<0) {
1813 log_err(LD_BUG,"Error initializing network; exiting.");
1814 return -1;
1816 atexit(exit_function);
1818 if (options_init_from_torrc(argc,argv) < 0) {
1819 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1820 return -1;
1823 #ifndef MS_WINDOWS
1824 if (geteuid()==0)
1825 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1826 "and you probably shouldn't.");
1827 #endif
1829 if (crypto_global_init(get_options()->HardwareAccel)) {
1830 log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
1831 return -1;
1834 return 0;
1837 /** A lockfile structure, used to prevent two Tors from messing with the
1838 * data directory at once. If this variable is non-NULL, we're holding
1839 * the lockfile. */
1840 static tor_lockfile_t *lockfile = NULL;
1842 /** Try to grab the lock file described in <b>options</b>, if we do not
1843 * already have it. If <b>err_if_locked</b> is true, warn if somebody else is
1844 * holding the lock, and exit if we can't get it after waiting. Otherwise,
1845 * return -1 if we can't get the lockfile. Return 0 on success.
1848 try_locking(or_options_t *options, int err_if_locked)
1850 if (lockfile)
1851 return 0;
1852 else {
1853 char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
1854 int already_locked = 0;
1855 tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
1856 tor_free(fname);
1857 if (!lf) {
1858 if (err_if_locked && already_locked) {
1859 int r;
1860 log_warn(LD_GENERAL, "It looks like another Tor process is running "
1861 "with the same data directory. Waiting 5 seconds to see "
1862 "if it goes away.");
1863 #ifndef WIN32
1864 sleep(5);
1865 #else
1866 Sleep(5000);
1867 #endif
1868 r = try_locking(options, 0);
1869 if (r<0) {
1870 log_err(LD_GENERAL, "No, it's still there. Exiting.");
1871 exit(0);
1873 return r;
1875 return -1;
1877 lockfile = lf;
1878 return 0;
1882 /** Return true iff we've successfully acquired the lock file. */
1884 have_lockfile(void)
1886 return lockfile != NULL;
1889 /** If we have successfully acquired the lock file, release it. */
1890 void
1891 release_lockfile(void)
1893 if (lockfile) {
1894 tor_lockfile_unlock(lockfile);
1895 lockfile = NULL;
1899 /** Free all memory that we might have allocated somewhere.
1900 * If <b>postfork</b>, we are a worker process and we want to free
1901 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1902 * Tor is shutting down and we should free everything.
1904 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1905 * should then report 0 reachable in its leak report (in an ideal world --
1906 * in practice libevent, SSL, libc etc never quite free everything). */
1907 void
1908 tor_free_all(int postfork)
1910 if (!postfork) {
1911 evdns_shutdown(1);
1913 geoip_free_all();
1914 dirvote_free_all();
1915 routerlist_free_all();
1916 networkstatus_free_all();
1917 addressmap_free_all();
1918 dirserv_free_all();
1919 rend_service_free_all();
1920 rend_cache_free_all();
1921 rend_service_authorization_free_all();
1922 rep_hist_free_all();
1923 hs_usage_free_all();
1924 dns_free_all();
1925 clear_pending_onions();
1926 circuit_free_all();
1927 entry_guards_free_all();
1928 connection_free_all();
1929 buf_shrink_freelists(1);
1930 memarea_clear_freelist();
1931 if (!postfork) {
1932 config_free_all();
1933 router_free_all();
1934 policies_free_all();
1936 free_cell_pool();
1937 if (!postfork) {
1938 tor_tls_free_all();
1940 /* stuff in main.c */
1941 if (connection_array)
1942 smartlist_free(connection_array);
1943 if (closeable_connection_lst)
1944 smartlist_free(closeable_connection_lst);
1945 if (active_linked_connection_lst)
1946 smartlist_free(active_linked_connection_lst);
1947 tor_free(timeout_event);
1948 if (!postfork) {
1949 release_lockfile();
1951 /* Stuff in util.c and address.c*/
1952 if (!postfork) {
1953 escaped(NULL);
1954 esc_router_info(NULL);
1955 logs_free_all(); /* free log strings. do this last so logs keep working. */
1959 /** Do whatever cleanup is necessary before shutting Tor down. */
1960 void
1961 tor_cleanup(void)
1963 or_options_t *options = get_options();
1964 /* Remove our pid file. We don't care if there was an error when we
1965 * unlink, nothing we could do about it anyways. */
1966 if (options->command == CMD_RUN_TOR) {
1967 time_t now = time(NULL);
1968 if (options->PidFile)
1969 unlink(options->PidFile);
1970 if (accounting_is_enabled(options))
1971 accounting_record_bandwidth_usage(now, get_or_state());
1972 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
1973 or_state_save(now);
1974 if (authdir_mode_tests_reachability(options))
1975 rep_hist_record_mtbf_data(now, 0);
1977 #ifdef USE_DMALLOC
1978 dmalloc_log_stats();
1979 #endif
1980 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
1981 later, if it makes shutdown unacceptably slow. But for
1982 now, leave it here: it's helped us catch bugs in the
1983 past. */
1984 crypto_global_cleanup();
1985 #ifdef USE_DMALLOC
1986 dmalloc_log_unfreed();
1987 dmalloc_shutdown();
1988 #endif
1991 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1992 /* static */ int
1993 do_list_fingerprint(void)
1995 char buf[FINGERPRINT_LEN+1];
1996 crypto_pk_env_t *k;
1997 const char *nickname = get_options()->Nickname;
1998 if (!server_mode(get_options())) {
1999 log_err(LD_GENERAL,
2000 "Clients don't have long-term identity keys. Exiting.\n");
2001 return -1;
2003 tor_assert(nickname);
2004 if (init_keys() < 0) {
2005 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
2006 return -1;
2008 if (!(k = get_identity_key())) {
2009 log_err(LD_GENERAL,"Error: missing identity key.");
2010 return -1;
2012 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
2013 log_err(LD_BUG, "Error computing fingerprint");
2014 return -1;
2016 printf("%s %s\n", nickname, buf);
2017 return 0;
2020 /** Entry point for password hashing: take the desired password from
2021 * the command line, and print its salted hash to stdout. **/
2022 /* static */ void
2023 do_hash_password(void)
2026 char output[256];
2027 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
2029 crypto_rand(key, S2K_SPECIFIER_LEN-1);
2030 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
2031 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
2032 get_options()->command_arg, strlen(get_options()->command_arg),
2033 key);
2034 base16_encode(output, sizeof(output), key, sizeof(key));
2035 printf("16:%s\n",output);
2038 /** Main entry point for the Tor process. Called from main(). */
2039 /* This function is distinct from main() only so we can link main.c into
2040 * the unittest binary without conflicting with the unittests' main. */
2042 tor_main(int argc, char *argv[])
2044 int result = 0;
2045 update_approx_time(time(NULL));
2046 tor_threads_init();
2047 init_logging();
2048 #ifdef USE_DMALLOC
2050 /* Instruct OpenSSL to use our internal wrappers for malloc,
2051 realloc and free. */
2052 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
2053 tor_assert(r);
2055 #endif
2056 #ifdef NT_SERVICE
2058 int done = 0;
2059 result = nt_service_parse_options(argc, argv, &done);
2060 if (done) return result;
2062 #endif
2063 if (tor_init(argc, argv)<0)
2064 return -1;
2065 switch (get_options()->command) {
2066 case CMD_RUN_TOR:
2067 #ifdef NT_SERVICE
2068 nt_service_set_state(SERVICE_RUNNING);
2069 #endif
2070 result = do_main_loop();
2071 break;
2072 case CMD_LIST_FINGERPRINT:
2073 result = do_list_fingerprint();
2074 break;
2075 case CMD_HASH_PASSWORD:
2076 do_hash_password();
2077 result = 0;
2078 break;
2079 case CMD_VERIFY_CONFIG:
2080 printf("Configuration was valid\n");
2081 result = 0;
2082 break;
2083 case CMD_RUN_UNITTESTS: /* only set by test.c */
2084 default:
2085 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2086 get_options()->command);
2087 result = -1;
2089 tor_cleanup();
2090 return result;