<weasel> tortls.c: In function `tor_tls_client_is_using_v2_ciphers':
[tor.git] / src / or / main.c
blobfaeca20f0a8d42d4e7cb9a8a0e637686e9ad116a
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-2008, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 /* $Id$ */
7 const char main_c_id[] =
8 "$Id$";
10 /**
11 * \file main.c
12 * \brief Toplevel module. Handles signals, multiplexes between
13 * connections, implements main loop, and drives scheduled events.
14 **/
16 #define MAIN_PRIVATE
17 #include "or.h"
18 #ifdef USE_DMALLOC
19 #include <dmalloc.h>
20 #endif
22 void evdns_shutdown(int);
24 /********* PROTOTYPES **********/
26 static void dumpmemusage(int severity);
27 static void dumpstats(int severity); /* log stats */
28 static void conn_read_callback(int fd, short event, void *_conn);
29 static void conn_write_callback(int fd, short event, void *_conn);
30 static void signal_callback(int fd, short events, void *arg);
31 static void second_elapsed_callback(int fd, short event, void *args);
32 static int conn_close_if_marked(int i);
33 static void connection_start_reading_from_linked_conn(connection_t *conn);
34 static int connection_should_read_from_linked_conn(connection_t *conn);
36 /********* START VARIABLES **********/
38 int global_read_bucket; /**< Max number of bytes I can read this second. */
39 int global_write_bucket; /**< Max number of bytes I can write this second. */
41 /** Max number of relayed (bandwidth class 1) bytes I can read this second. */
42 int global_relayed_read_bucket;
43 /** Max number of relayed (bandwidth class 1) bytes I can write this second. */
44 int global_relayed_write_bucket;
46 /** What was the read bucket before the last call to prepare_for_pool?
47 * (used to determine how many bytes we've read). */
48 static int stats_prev_global_read_bucket;
49 /** What was the write bucket before the last call to prepare_for_pool?
50 * (used to determine how many bytes we've written). */
51 static int stats_prev_global_write_bucket;
52 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
53 /** How many bytes have we read/written since we started the process? */
54 static uint64_t stats_n_bytes_read = 0;
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 old do we let a connection to an OR get before deciding it's
100 * obsolete? */
101 #define TIME_BEFORE_OR_CONN_IS_OBSOLETE (60*60*24*7)
102 /** How long do we let OR connections handshake before we decide that
103 * they are obsolete? */
104 #define TLS_HANDSHAKE_TIMEOUT (60)
106 /********* END VARIABLES ************/
108 /****************************************************************************
110 * This section contains accessors and other methods on the connection_array
111 * variables (which are global within this file and unavailable outside it).
113 ****************************************************************************/
115 /** Add <b>conn</b> to the array of connections that we can poll on. The
116 * connection's socket must be set; the connection starts out
117 * non-reading and non-writing.
120 connection_add(connection_t *conn)
122 tor_assert(conn);
123 tor_assert(conn->s >= 0 ||
124 conn->linked ||
125 (conn->type == CONN_TYPE_AP &&
126 TO_EDGE_CONN(conn)->is_dns_request));
128 tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
129 conn->conn_array_index = smartlist_len(connection_array);
130 smartlist_add(connection_array, conn);
132 if (conn->s >= 0 || conn->linked) {
133 conn->read_event = tor_malloc_zero(sizeof(struct event));
134 conn->write_event = tor_malloc_zero(sizeof(struct event));
135 event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
136 conn_read_callback, conn);
137 event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
138 conn_write_callback, conn);
141 log_debug(LD_NET,"new conn type %s, socket %d, n_conns %d.",
142 conn_type_to_string(conn->type), conn->s,
143 smartlist_len(connection_array));
145 return 0;
148 /** Remove the connection from the global list, and remove the
149 * corresponding poll entry. Calling this function will shift the last
150 * connection (if any) into the position occupied by conn.
153 connection_remove(connection_t *conn)
155 int current_index;
156 connection_t *tmp;
158 tor_assert(conn);
160 log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
161 conn->s, conn_type_to_string(conn->type),
162 smartlist_len(connection_array));
164 tor_assert(conn->conn_array_index >= 0);
165 current_index = conn->conn_array_index;
166 connection_unregister_events(conn); /* This is redundant, but cheap. */
167 if (current_index == smartlist_len(connection_array)-1) { /* at the end */
168 smartlist_del(connection_array, current_index);
169 return 0;
172 /* replace this one with the one at the end */
173 smartlist_del(connection_array, current_index);
174 tmp = smartlist_get(connection_array, current_index);
175 tmp->conn_array_index = current_index;
177 return 0;
180 /** If <b>conn</b> is an edge conn, remove it from the list
181 * of conn's on this circuit. If it's not on an edge,
182 * flush and send destroys for all circuits on this conn.
184 * Remove it from connection_array (if applicable) and
185 * from closeable_connection_list.
187 * Then free it.
189 static void
190 connection_unlink(connection_t *conn)
192 connection_about_to_close_connection(conn);
193 if (conn->conn_array_index >= 0) {
194 connection_remove(conn);
196 if (conn->linked_conn) {
197 conn->linked_conn->linked_conn = NULL;
198 if (! conn->linked_conn->marked_for_close &&
199 conn->linked_conn->reading_from_linked_conn)
200 connection_start_reading(conn->linked_conn);
201 conn->linked_conn = NULL;
203 smartlist_remove(closeable_connection_lst, conn);
204 smartlist_remove(active_linked_connection_lst, conn);
205 if (conn->type == CONN_TYPE_EXIT) {
206 assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
208 if (conn->type == CONN_TYPE_OR) {
209 if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
210 connection_or_remove_from_identity_map(TO_OR_CONN(conn));
212 connection_free(conn);
215 /** Schedule <b>conn</b> to be closed. **/
216 void
217 add_connection_to_closeable_list(connection_t *conn)
219 tor_assert(!smartlist_isin(closeable_connection_lst, conn));
220 tor_assert(conn->marked_for_close);
221 assert_connection_ok(conn, time(NULL));
222 smartlist_add(closeable_connection_lst, conn);
225 /** Return 1 if conn is on the closeable list, else return 0. */
227 connection_is_on_closeable_list(connection_t *conn)
229 return smartlist_isin(closeable_connection_lst, conn);
232 /** Return true iff conn is in the current poll array. */
234 connection_in_array(connection_t *conn)
236 return smartlist_isin(connection_array, conn);
239 /** Set <b>*array</b> to an array of all connections, and <b>*n</b>
240 * to the length of the array. <b>*array</b> and <b>*n</b> must not
241 * be modified.
243 smartlist_t *
244 get_connection_array(void)
246 return connection_array;
249 /** Set the event mask on <b>conn</b> to <b>events</b>. (The event
250 * mask is a bitmask whose bits are EV_READ and EV_WRITE.)
252 void
253 connection_watch_events(connection_t *conn, short events)
255 if (events & EV_READ)
256 connection_start_reading(conn);
257 else
258 connection_stop_reading(conn);
260 if (events & EV_WRITE)
261 connection_start_writing(conn);
262 else
263 connection_stop_writing(conn);
266 /** Return true iff <b>conn</b> is listening for read events. */
268 connection_is_reading(connection_t *conn)
270 tor_assert(conn);
272 return conn->reading_from_linked_conn ||
273 (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
276 /** Tell the main loop to stop notifying <b>conn</b> of any read events. */
277 void
278 connection_stop_reading(connection_t *conn)
280 tor_assert(conn);
281 tor_assert(conn->read_event);
283 if (conn->linked) {
284 conn->reading_from_linked_conn = 0;
285 connection_stop_reading_from_linked_conn(conn);
286 } else {
287 if (event_del(conn->read_event))
288 log_warn(LD_NET, "Error from libevent setting read event state for %d "
289 "to unwatched: %s",
290 conn->s,
291 tor_socket_strerror(tor_socket_errno(conn->s)));
295 /** Tell the main loop to start notifying <b>conn</b> of any read events. */
296 void
297 connection_start_reading(connection_t *conn)
299 tor_assert(conn);
300 tor_assert(conn->read_event);
302 if (conn->linked) {
303 conn->reading_from_linked_conn = 1;
304 if (connection_should_read_from_linked_conn(conn))
305 connection_start_reading_from_linked_conn(conn);
306 } else {
307 if (event_add(conn->read_event, NULL))
308 log_warn(LD_NET, "Error from libevent setting read event state for %d "
309 "to watched: %s",
310 conn->s,
311 tor_socket_strerror(tor_socket_errno(conn->s)));
315 /** Return true iff <b>conn</b> is listening for write events. */
317 connection_is_writing(connection_t *conn)
319 tor_assert(conn);
321 return conn->writing_to_linked_conn ||
322 (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
325 /** Tell the main loop to stop notifying <b>conn</b> of any write events. */
326 void
327 connection_stop_writing(connection_t *conn)
329 tor_assert(conn);
330 tor_assert(conn->write_event);
332 if (conn->linked) {
333 conn->writing_to_linked_conn = 0;
334 if (conn->linked_conn)
335 connection_stop_reading_from_linked_conn(conn->linked_conn);
336 } else {
337 if (event_del(conn->write_event))
338 log_warn(LD_NET, "Error from libevent setting write event state for %d "
339 "to unwatched: %s",
340 conn->s,
341 tor_socket_strerror(tor_socket_errno(conn->s)));
345 /** Tell the main loop to start notifying <b>conn</b> of any write events. */
346 void
347 connection_start_writing(connection_t *conn)
349 tor_assert(conn);
350 tor_assert(conn->write_event);
352 if (conn->linked) {
353 conn->writing_to_linked_conn = 1;
354 if (conn->linked_conn &&
355 connection_should_read_from_linked_conn(conn->linked_conn))
356 connection_start_reading_from_linked_conn(conn->linked_conn);
357 } else {
358 if (event_add(conn->write_event, NULL))
359 log_warn(LD_NET, "Error from libevent setting write event state for %d "
360 "to watched: %s",
361 conn->s,
362 tor_socket_strerror(tor_socket_errno(conn->s)));
366 /** Return true iff <b>conn</b> is linked conn, and reading from the conn
367 * linked to it would be good and feasible. (Reading is "feasible" if the
368 * other conn exists and has data in its outbuf, and is "good" if we have our
369 * reading_from_linked_conn flag set and the other conn has its
370 * writing_to_linked_conn flag set.)*/
371 static int
372 connection_should_read_from_linked_conn(connection_t *conn)
374 if (conn->linked && conn->reading_from_linked_conn) {
375 if (! conn->linked_conn ||
376 (conn->linked_conn->writing_to_linked_conn &&
377 buf_datalen(conn->linked_conn->outbuf)))
378 return 1;
380 return 0;
383 /** Helper: Tell the main loop to begin reading bytes into <b>conn</b> from
384 * its linked connection, if it is not doing so already. Called by
385 * connection_start_reading and connection_start_writing as appropriate. */
386 static void
387 connection_start_reading_from_linked_conn(connection_t *conn)
389 tor_assert(conn);
390 tor_assert(conn->linked == 1);
392 if (!conn->active_on_link) {
393 conn->active_on_link = 1;
394 smartlist_add(active_linked_connection_lst, conn);
395 if (!called_loop_once) {
396 /* This is the first event on the list; we won't be in LOOP_ONCE mode,
397 * so we need to make sure that the event_loop() actually exits at the
398 * end of its run through the current connections and
399 * lets us activate read events for linked connections. */
400 struct timeval tv = { 0, 0 };
401 event_loopexit(&tv);
403 } else {
404 tor_assert(smartlist_isin(active_linked_connection_lst, conn));
408 /** Tell the main loop to stop reading bytes into <b>conn</b> from its linked
409 * connection, if is currently doing so. Called by connection_stop_reading,
410 * connection_stop_writing, and connection_read. */
411 void
412 connection_stop_reading_from_linked_conn(connection_t *conn)
414 tor_assert(conn);
415 tor_assert(conn->linked == 1);
417 if (conn->active_on_link) {
418 conn->active_on_link = 0;
419 /* FFFF We could keep an index here so we can smartlist_del
420 * cleanly. On the other hand, this doesn't show up on profiles,
421 * so let's leave it alone for now. */
422 smartlist_remove(active_linked_connection_lst, conn);
423 } else {
424 tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
428 /** Close all connections that have been scheduled to get closed. */
429 static void
430 close_closeable_connections(void)
432 int i;
433 for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
434 connection_t *conn = smartlist_get(closeable_connection_lst, i);
435 if (conn->conn_array_index < 0) {
436 connection_unlink(conn); /* blow it away right now */
437 } else {
438 if (!conn_close_if_marked(conn->conn_array_index))
439 ++i;
444 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
445 * some data to read. */
446 static void
447 conn_read_callback(int fd, short event, void *_conn)
449 connection_t *conn = _conn;
450 (void)fd;
451 (void)event;
453 log_debug(LD_NET,"socket %d wants to read.",conn->s);
455 assert_connection_ok(conn, time(NULL));
457 if (connection_handle_read(conn) < 0) {
458 if (!conn->marked_for_close) {
459 #ifndef MS_WINDOWS
460 log_warn(LD_BUG,"Unhandled error on read for %s connection "
461 "(fd %d); removing",
462 conn_type_to_string(conn->type), conn->s);
463 tor_fragile_assert();
464 #endif
465 if (CONN_IS_EDGE(conn))
466 connection_edge_end_errno(TO_EDGE_CONN(conn));
467 connection_mark_for_close(conn);
470 assert_connection_ok(conn, time(NULL));
472 if (smartlist_len(closeable_connection_lst))
473 close_closeable_connections();
476 /** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
477 * some data to write. */
478 static void
479 conn_write_callback(int fd, short events, void *_conn)
481 connection_t *conn = _conn;
482 (void)fd;
483 (void)events;
485 LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
487 assert_connection_ok(conn, time(NULL));
489 if (connection_handle_write(conn, 0) < 0) {
490 if (!conn->marked_for_close) {
491 /* this connection is broken. remove it. */
492 log_fn(LOG_WARN,LD_BUG,
493 "unhandled error on write for %s connection (fd %d); removing",
494 conn_type_to_string(conn->type), conn->s);
495 tor_fragile_assert();
496 if (CONN_IS_EDGE(conn)) {
497 /* otherwise we cry wolf about duplicate close */
498 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
499 if (!edge_conn->end_reason)
500 edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
501 conn->edge_has_sent_end = 1;
503 connection_close_immediate(conn); /* So we don't try to flush. */
504 connection_mark_for_close(conn);
507 assert_connection_ok(conn, time(NULL));
509 if (smartlist_len(closeable_connection_lst))
510 close_closeable_connections();
513 /** If the connection at connection_array[i] is marked for close, then:
514 * - If it has data that it wants to flush, try to flush it.
515 * - If it _still_ has data to flush, and conn->hold_open_until_flushed is
516 * true, then leave the connection open and return.
517 * - Otherwise, remove the connection from connection_array and from
518 * all other lists, close it, and free it.
519 * Returns 1 if the connection was closed, 0 otherwise.
521 static int
522 conn_close_if_marked(int i)
524 connection_t *conn;
525 int retval;
526 time_t now;
528 conn = smartlist_get(connection_array, i);
529 if (!conn->marked_for_close)
530 return 0; /* nothing to see here, move along */
531 now = time(NULL);
532 assert_connection_ok(conn, now);
533 assert_all_pending_dns_resolves_ok();
535 log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
536 if ((conn->s >= 0 || conn->linked_conn) && connection_wants_to_flush(conn)) {
537 /* s == -1 means it's an incomplete edge connection, or that the socket
538 * has already been closed as unflushable. */
539 int sz = connection_bucket_write_limit(conn, now);
540 if (!conn->hold_open_until_flushed)
541 log_info(LD_NET,
542 "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
543 "to flush %d bytes. (Marked at %s:%d)",
544 escaped_safe_str(conn->address),
545 conn->s, conn_type_to_string(conn->type), conn->state,
546 (int)conn->outbuf_flushlen,
547 conn->marked_for_close_file, conn->marked_for_close);
548 if (conn->linked_conn) {
549 retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
550 &conn->outbuf_flushlen);
551 if (retval >= 0) {
552 /* The linked conn will notice that it has data when it notices that
553 * we're gone. */
554 connection_start_reading_from_linked_conn(conn->linked_conn);
556 log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
557 "%d left; flushlen %d; wants-to-flush==%d", retval,
558 (int)buf_datalen(conn->outbuf),
559 (int)conn->outbuf_flushlen,
560 connection_wants_to_flush(conn));
561 } else if (connection_speaks_cells(conn)) {
562 if (conn->state == OR_CONN_STATE_OPEN) {
563 retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
564 &conn->outbuf_flushlen);
565 } else
566 retval = -1; /* never flush non-open broken tls connections */
567 } else {
568 retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
570 if (retval >= 0 && /* Technically, we could survive things like
571 TLS_WANT_WRITE here. But don't bother for now. */
572 conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
573 if (retval > 0) {
574 LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
575 "Holding conn (fd %d) open for more flushing.",
576 conn->s));
577 conn->timestamp_lastwritten = now; /* reset so we can flush more */
579 return 0;
581 if (connection_wants_to_flush(conn)) {
582 int severity;
583 if (conn->type == CONN_TYPE_EXIT ||
584 (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
585 (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
586 severity = LOG_INFO;
587 else
588 severity = LOG_NOTICE;
589 /* XXXX Maybe allow this to happen a certain amount per hour; it usually
590 * is meaningless. */
591 log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
592 "bytes to address %s. If this happens a lot, either "
593 "something is wrong with your network connection, or "
594 "something is wrong with theirs. "
595 "(fd %d, type %s, state %d, marked at %s:%d).",
596 (int)buf_datalen(conn->outbuf),
597 escaped_safe_str(conn->address), conn->s,
598 conn_type_to_string(conn->type), conn->state,
599 conn->marked_for_close_file,
600 conn->marked_for_close);
603 connection_unlink(conn); /* unlink, remove, free */
604 return 1;
607 /** We've just tried every dirserver we know about, and none of
608 * them were reachable. Assume the network is down. Change state
609 * so next time an application connection arrives we'll delay it
610 * and try another directory fetch. Kill off all the circuit_wait
611 * streams that are waiting now, since they will all timeout anyway.
613 void
614 directory_all_unreachable(time_t now)
616 connection_t *conn;
617 (void)now;
619 stats_n_seconds_working=0; /* reset it */
621 while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
622 AP_CONN_STATE_CIRCUIT_WAIT))) {
623 edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
624 log_notice(LD_NET,
625 "Is your network connection down? "
626 "Failing connection to '%s:%d'.",
627 safe_str(edge_conn->socks_request->address),
628 edge_conn->socks_request->port);
629 connection_mark_unattached_ap(edge_conn,
630 END_STREAM_REASON_NET_UNREACHABLE);
632 control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
635 /** This function is called whenever we successfully pull down some new
636 * network statuses or server descriptors. */
637 void
638 directory_info_has_arrived(time_t now, int from_cache)
640 or_options_t *options = get_options();
642 if (!router_have_minimum_dir_info()) {
643 log(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 we have enough dir info, then update our guard status with
650 * whatever we just learned. */
651 entry_guards_compute_status();
652 /* Don't even bother trying to get extrainfo until the rest of our
653 * directory info is up-to-date */
654 if (options->DownloadExtraInfo)
655 update_extrainfo_downloads(now);
658 if (server_mode(options) && !we_are_hibernating() && !from_cache &&
659 (has_completed_circuit || !any_predicted_circuits(now)))
660 consider_testing_reachability(1, 1);
663 /** Perform regular maintenance tasks for a single connection. This
664 * function gets run once per second per connection by run_scheduled_events.
666 static void
667 run_connection_housekeeping(int i, time_t now)
669 cell_t cell;
670 connection_t *conn = smartlist_get(connection_array, i);
671 or_options_t *options = get_options();
672 or_connection_t *or_conn;
674 if (conn->outbuf && !buf_datalen(conn->outbuf) && conn->type == CONN_TYPE_OR)
675 TO_OR_CONN(conn)->timestamp_lastempty = now;
677 if (conn->marked_for_close) {
678 /* nothing to do here */
679 return;
682 /* Expire any directory connections that haven't been active (sent
683 * if a server or received if a client) for 5 min */
684 if (conn->type == CONN_TYPE_DIR &&
685 ((DIR_CONN_IS_SERVER(conn) &&
686 conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
687 (!DIR_CONN_IS_SERVER(conn) &&
688 conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
689 log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
690 conn->s, conn->purpose);
691 /* This check is temporary; it's to let us know whether we should consider
692 * parsing partial serverdesc responses. */
693 if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
694 buf_datalen(conn->inbuf)>=1024) {
695 log_info(LD_DIR,"Trying to extract information from wedged server desc "
696 "download.");
697 connection_dir_reached_eof(TO_DIR_CONN(conn));
698 } else {
699 connection_mark_for_close(conn);
701 return;
704 if (!connection_speaks_cells(conn))
705 return; /* we're all done here, the rest is just for OR conns */
707 or_conn = TO_OR_CONN(conn);
709 if (!conn->or_is_obsolete) {
710 if (conn->timestamp_created + TIME_BEFORE_OR_CONN_IS_OBSOLETE < now) {
711 log_info(LD_OR,
712 "Marking OR conn to %s:%d obsolete (fd %d, %d secs old).",
713 conn->address, conn->port, conn->s,
714 (int)(now - conn->timestamp_created));
715 conn->or_is_obsolete = 1;
716 } else {
717 or_connection_t *best =
718 connection_or_get_by_identity_digest(or_conn->identity_digest);
719 if (best && best != or_conn &&
720 (conn->state == OR_CONN_STATE_OPEN ||
721 now > conn->timestamp_created + TLS_HANDSHAKE_TIMEOUT)) {
722 /* We only mark as obsolete connections that already are in
723 * OR_CONN_STATE_OPEN, i.e. that have finished their TLS handshaking.
724 * This is necessary because authorities judge whether a router is
725 * reachable based on whether they were able to TLS handshake with it
726 * recently. Without this check we would expire connections too
727 * early for router->last_reachable to be updated.
729 log_info(LD_OR,
730 "Marking duplicate conn to %s:%d obsolete "
731 "(fd %d, %d secs old).",
732 conn->address, conn->port, conn->s,
733 (int)(now - conn->timestamp_created));
734 conn->or_is_obsolete = 1;
739 if (conn->or_is_obsolete && !or_conn->n_circuits) {
740 /* no unmarked circs -- mark it now */
741 log_info(LD_OR,
742 "Expiring non-used OR connection to fd %d (%s:%d) [Obsolete].",
743 conn->s, conn->address, conn->port);
744 connection_mark_for_close(conn);
745 conn->hold_open_until_flushed = 1;
746 return;
749 /* If we haven't written to an OR connection for a while, then either nuke
750 the connection or send a keepalive, depending. */
751 if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
752 routerinfo_t *router = router_get_by_digest(or_conn->identity_digest);
753 int maxCircuitlessPeriod = options->MaxCircuitDirtiness*3/2;
754 if (!connection_state_is_open(conn)) {
755 /* We never managed to actually get this connection open and happy. */
756 log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
757 conn->s,conn->address, conn->port);
758 connection_mark_for_close(conn);
759 conn->hold_open_until_flushed = 1;
760 } else if (we_are_hibernating() && !or_conn->n_circuits &&
761 !buf_datalen(conn->outbuf)) {
762 /* We're hibernating, there's no circuits, and nothing to flush.*/
763 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
764 "[Hibernating or exiting].",
765 conn->s,conn->address, conn->port);
766 connection_mark_for_close(conn);
767 conn->hold_open_until_flushed = 1;
768 } else if (!clique_mode(options) && !or_conn->n_circuits &&
769 now >= or_conn->timestamp_last_added_nonpadding +
770 maxCircuitlessPeriod &&
771 (!router || !server_mode(options) ||
772 !router_is_clique_mode(router))) {
773 log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
774 "[Not in clique mode].",
775 conn->s,conn->address, conn->port);
776 connection_mark_for_close(conn);
777 conn->hold_open_until_flushed = 1;
778 } else if (
779 now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
780 now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
781 log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
782 "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
783 "flush; %d seconds since last write)",
784 conn->s, conn->address, conn->port,
785 (int)buf_datalen(conn->outbuf),
786 (int)(now-conn->timestamp_lastwritten));
787 connection_mark_for_close(conn);
788 } else if (!buf_datalen(conn->outbuf)) {
789 /* either in clique mode, or we've got a circuit. send a padding cell. */
790 log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
791 conn->address, conn->port);
792 memset(&cell,0,sizeof(cell_t));
793 cell.command = CELL_PADDING;
794 connection_or_write_cell_to_buf(&cell, or_conn);
799 /** Honor a NEWNYM request: make future requests unlinkability to past
800 * requests. */
801 static void
802 signewnym_impl(time_t now)
804 circuit_expire_all_dirty_circs();
805 addressmap_clear_transient();
806 time_of_last_signewnym = now;
807 signewnym_is_pending = 0;
810 /** Perform regular maintenance tasks. This function gets run once per
811 * second by prepare_for_poll.
813 static void
814 run_scheduled_events(time_t now)
816 static time_t time_to_fetch_directory = 0;
817 static time_t time_to_fetch_running_routers = 0;
818 static time_t last_rotated_x509_certificate = 0;
819 static time_t time_to_check_v3_certificate = 0;
820 static time_t time_to_check_listeners = 0;
821 static time_t time_to_check_descriptor = 0;
822 static time_t time_to_check_ipaddress = 0;
823 static time_t time_to_shrink_memory = 0;
824 static time_t time_to_try_getting_descriptors = 0;
825 static time_t time_to_reset_descriptor_failures = 0;
826 static time_t time_to_add_entropy = 0;
827 static time_t time_to_write_hs_statistics = 0;
828 static time_t time_to_write_bridge_status_file = 0;
829 static time_t time_to_downrate_stability = 0;
830 static time_t time_to_save_stability = 0;
831 static time_t time_to_clean_caches = 0;
832 static time_t time_to_recheck_bandwidth = 0;
833 static time_t time_to_check_for_expired_networkstatus = 0;
834 or_options_t *options = get_options();
835 int i;
836 int have_dir_info;
838 /** 0. See if we've been asked to shut down and our timeout has
839 * expired; or if our bandwidth limits are exhausted and we
840 * should hibernate; or if it's time to wake up from hibernation.
842 consider_hibernation(now);
844 /* 0b. If we've deferred a signewnym, make sure it gets handled
845 * eventually. */
846 if (signewnym_is_pending &&
847 time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
848 log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
849 signewnym_impl(now);
852 /** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
853 * shut down and restart all cpuworkers, and update the directory if
854 * necessary.
856 if (server_mode(options) &&
857 get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
858 log_info(LD_GENERAL,"Rotating onion key.");
859 rotate_onion_key();
860 cpuworkers_rotate();
861 if (router_rebuild_descriptor(1)<0) {
862 log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
864 if (advertised_server_mode())
865 router_upload_dir_desc_to_dirservers(0);
868 if (time_to_try_getting_descriptors < now) {
869 update_router_descriptor_downloads(now);
870 update_extrainfo_downloads(now);
871 if (options->UseBridges)
872 fetch_bridge_descriptors(now);
873 if (router_have_minimum_dir_info())
874 time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
875 else
876 time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
879 if (time_to_reset_descriptor_failures < now) {
880 router_reset_descriptor_download_failures();
881 time_to_reset_descriptor_failures =
882 now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
885 /** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
886 if (!last_rotated_x509_certificate)
887 last_rotated_x509_certificate = now;
888 if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME < now) {
889 log_info(LD_GENERAL,"Rotating tls context.");
890 if (tor_tls_context_new(get_identity_key(), MAX_SSL_KEY_LIFETIME) < 0) {
891 log_warn(LD_BUG, "Error reinitializing TLS context");
892 /* XXX is it a bug here, that we just keep going? -RD */
894 last_rotated_x509_certificate = now;
895 /* We also make sure to rotate the TLS connections themselves if they've
896 * been up for too long -- but that's done via or_is_obsolete in
897 * connection_run_housekeeping() above. */
900 if (time_to_add_entropy < now) {
901 if (time_to_add_entropy) {
902 /* We already seeded once, so don't die on failure. */
903 crypto_seed_rng();
905 /** How often do we add more entropy to OpenSSL's RNG pool? */
906 #define ENTROPY_INTERVAL (60*60)
907 time_to_add_entropy = now + ENTROPY_INTERVAL;
910 /** 1c. If we have to change the accounting interval or record
911 * bandwidth used in this accounting interval, do so. */
912 if (accounting_is_enabled(options))
913 accounting_run_housekeeping(now);
915 if (now % 10 == 0 && (authdir_mode_tests_reachability(options)) &&
916 !we_are_hibernating()) {
917 /* try to determine reachability of the other Tor relays */
918 dirserv_test_reachability(now, 0);
921 /** 1d. Periodically, we discount older stability information so that new
922 * stability info counts more, and save the stability information to disk as
923 * appropriate. */
924 if (time_to_downrate_stability < now)
925 time_to_downrate_stability = rep_hist_downrate_old_runs(now);
926 if (authdir_mode_tests_reachability(options)) {
927 if (time_to_save_stability < now) {
928 if (time_to_save_stability && rep_hist_record_mtbf_data()<0) {
929 log_warn(LD_GENERAL, "Couldn't store mtbf data.");
931 #define SAVE_STABILITY_INTERVAL (30*60)
932 time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
936 /* 1e. Periodicaly, if we're a v3 authority, we check whether our cert is
937 * close to expiring and warn the admin if it is. */
938 if (time_to_check_v3_certificate < now) {
939 v3_authority_check_key_expiry();
940 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
941 time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
944 /* 1f. Check whether our networkstatus has expired.
946 if (time_to_check_for_expired_networkstatus < now) {
947 networkstatus_t *ns = networkstatus_get_latest_consensus();
948 /*XXXX020 this value needs to be the same as REASONABLY_LIVE_TIME in
949 * networkstatus_get_reasonably_live_consensus(), but that value is way
950 * way too high. Arma: is the bridge issue there resolved yet? -NM */
951 #define NS_EXPIRY_SLOP (24*60*60)
952 if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
953 router_have_minimum_dir_info()) {
954 router_dir_info_changed();
956 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
957 time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
960 /** 2. Periodically, we consider getting a new directory, getting a
961 * new running-routers list, and/or force-uploading our descriptor
962 * (if we've passed our internal checks). */
963 if (time_to_fetch_directory < now) {
964 /* Only caches actually need to fetch v1 directories now. */
965 if (directory_fetches_dir_info_early(options) &&
966 !authdir_mode_v1(options) && any_trusted_dir_is_v1_authority() &&
967 !should_delay_dir_fetches(options))
968 directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR,
969 ROUTER_PURPOSE_GENERAL, NULL, 1);
970 /** How often do we (as a cache) fetch a new V1 directory? */
971 #define V1_DIR_FETCH_PERIOD (12*60*60)
972 time_to_fetch_directory = now + V1_DIR_FETCH_PERIOD;
975 /* Caches need to fetch running_routers; directory clients don't. */
976 if (time_to_fetch_running_routers < now) {
977 if (directory_fetches_dir_info_early(options) &&
978 !authdir_mode_v1(options) && any_trusted_dir_is_v1_authority() &&
979 !should_delay_dir_fetches(options))
980 directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST,
981 ROUTER_PURPOSE_GENERAL, NULL, 1);
982 /** How often do we (as a cache) fetch a new V1 runningrouters document? */
983 #define V1_RUNNINGROUTERS_FETCH_PERIOD (12*60*60)
984 time_to_fetch_running_routers = now + V1_RUNNINGROUTERS_FETCH_PERIOD;
987 /* Remove old information from rephist and the rend cache. */
988 if (time_to_clean_caches < now) {
989 rep_history_clean(now - options->RephistTrackTime);
990 rend_cache_clean();
991 rend_cache_clean_v2_descs_as_dir();
992 #define CLEAN_CACHES_INTERVAL (30*60)
993 time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
996 /** How often do we check whether part of our router info has changed in a way
997 * that would require an upload? */
998 #define CHECK_DESCRIPTOR_INTERVAL (60)
999 /** How often do we (as a router) check whether our IP address has changed? */
1000 #define CHECK_IPADDRESS_INTERVAL (15*60)
1002 /* 2b. Once per minute, regenerate and upload the descriptor if the old
1003 * one is inaccurate. */
1004 if (time_to_check_descriptor < now) {
1005 static int dirport_reachability_count = 0;
1006 time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
1007 check_descriptor_bandwidth_changed(now);
1008 if (time_to_check_ipaddress < now) {
1009 time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
1010 check_descriptor_ipaddress_changed(now);
1012 /** If our router descriptor ever goes this long without being regenerated
1013 * because something changed, we force an immediate regenerate-and-upload. */
1014 #define FORCE_REGENERATE_DESCRIPTOR_INTERVAL (18*60*60)
1015 mark_my_descriptor_dirty_if_older_than(
1016 now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
1017 consider_publishable_server(0);
1018 /* also, check religiously for reachability, if it's within the first
1019 * 20 minutes of our uptime. */
1020 if (server_mode(options) &&
1021 (has_completed_circuit || !any_predicted_circuits(now)) &&
1022 !we_are_hibernating()) {
1023 if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1024 consider_testing_reachability(1, dirport_reachability_count==0);
1025 if (++dirport_reachability_count > 5)
1026 dirport_reachability_count = 0;
1027 } else if (time_to_recheck_bandwidth < now) {
1028 /* If we haven't checked for 12 hours and our bandwidth estimate is
1029 * low, do another bandwidth test. This is especially important for
1030 * bridges, since they might go long periods without much use. */
1031 routerinfo_t *me = router_get_my_routerinfo();
1032 if (time_to_recheck_bandwidth && me &&
1033 me->bandwidthcapacity < me->bandwidthrate &&
1034 me->bandwidthcapacity < 51200) {
1035 reset_bandwidth_test();
1037 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
1038 time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
1042 /* If any networkstatus documents are no longer recent, we need to
1043 * update all the descriptors' running status. */
1044 /* purge obsolete entries */
1045 networkstatus_v2_list_clean(now);
1046 /* Remove dead routers. */
1047 routerlist_remove_old_routers();
1049 /* Also, once per minute, check whether we want to download any
1050 * networkstatus documents.
1052 update_networkstatus_downloads(now);
1055 /** 2c. Let directory voting happen. */
1056 if (authdir_mode_v3(options))
1057 dirvote_act(options, now);
1059 /** 3a. Every second, we examine pending circuits and prune the
1060 * ones which have been pending for more than a few seconds.
1061 * We do this before step 4, so it can try building more if
1062 * it's not comfortable with the number of available circuits.
1064 circuit_expire_building(now);
1066 /** 3b. Also look at pending streams and prune the ones that 'began'
1067 * a long time ago but haven't gotten a 'connected' yet.
1068 * Do this before step 4, so we can put them back into pending
1069 * state to be picked up by the new circuit.
1071 connection_ap_expire_beginning();
1073 /** 3c. And expire connections that we've held open for too long.
1075 connection_expire_held_open();
1077 /** 3d. And every 60 seconds, we relaunch listeners if any died. */
1078 if (!we_are_hibernating() && time_to_check_listeners < now) {
1079 retry_all_listeners(NULL, NULL);
1080 time_to_check_listeners = now+60;
1083 /** 4. Every second, we try a new circuit if there are no valid
1084 * circuits. Every NewCircuitPeriod seconds, we expire circuits
1085 * that became dirty more than MaxCircuitDirtiness seconds ago,
1086 * and we make a new circ if there are no clean circuits.
1088 have_dir_info = router_have_minimum_dir_info();
1089 if (have_dir_info && !we_are_hibernating())
1090 circuit_build_needed_circs(now);
1092 /** 5. We do housekeeping for each connection... */
1093 for (i=0;i<smartlist_len(connection_array);i++) {
1094 run_connection_housekeeping(i, now);
1096 if (time_to_shrink_memory < now) {
1097 SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
1098 if (conn->outbuf)
1099 buf_shrink(conn->outbuf);
1100 if (conn->inbuf)
1101 buf_shrink(conn->inbuf);
1103 clean_cell_pool();
1104 buf_shrink_freelists(0);
1105 /** How often do we check buffers and pools for empty space that can be
1106 * deallocated? */
1107 #define MEM_SHRINK_INTERVAL (60)
1108 time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
1111 /** 6. And remove any marked circuits... */
1112 circuit_close_all_marked();
1114 /** 7. And upload service descriptors if necessary. */
1115 if (has_completed_circuit && !we_are_hibernating())
1116 rend_consider_services_upload(now);
1118 /** 8. and blow away any connections that need to die. have to do this now,
1119 * because if we marked a conn for close and left its socket -1, then
1120 * we'll pass it to poll/select and bad things will happen.
1122 close_closeable_connections();
1124 /** 8b. And if anything in our state is ready to get flushed to disk, we
1125 * flush it. */
1126 or_state_save(now);
1128 /** 9. and if we're a server, check whether our DNS is telling stories to
1129 * us. */
1130 if (server_mode(options) && time_to_check_for_correct_dns < now) {
1131 if (!time_to_check_for_correct_dns) {
1132 time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
1133 } else {
1134 dns_launch_correctness_checks();
1135 time_to_check_for_correct_dns = now + 12*3600 +
1136 crypto_rand_int(12*3600);
1140 /** 10. write hidden service usage statistic to disk */
1141 if (options->HSAuthorityRecordStats && time_to_write_hs_statistics < now) {
1142 hs_usage_write_statistics_to_file(now);
1143 #define WRITE_HSUSAGE_INTERVAL (30*60)
1144 time_to_write_hs_statistics = now+WRITE_HSUSAGE_INTERVAL;
1146 /** 10b. write bridge networkstatus file to disk */
1147 if (options->BridgeAuthoritativeDir &&
1148 time_to_write_bridge_status_file < now) {
1149 networkstatus_dump_bridge_status_to_file(now);
1150 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
1151 time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
1155 /** Libevent timer: used to invoke second_elapsed_callback() once per
1156 * second. */
1157 static struct event *timeout_event = NULL;
1158 /** Number of libevent errors in the last second: we die if we get too many. */
1159 static int n_libevent_errors = 0;
1161 /** Libevent callback: invoked once every second. */
1162 static void
1163 second_elapsed_callback(int fd, short event, void *args)
1165 /* XXXX This could be sensibly refactored into multiple callbacks, and we
1166 * could use libevent's timers for this rather than checking the current
1167 * time against a bunch of timeouts every second. */
1168 static struct timeval one_second;
1169 static long current_second = 0;
1170 struct timeval now;
1171 size_t bytes_written;
1172 size_t bytes_read;
1173 int seconds_elapsed;
1174 or_options_t *options = get_options();
1175 (void)fd;
1176 (void)event;
1177 (void)args;
1178 if (!timeout_event) {
1179 timeout_event = tor_malloc_zero(sizeof(struct event));
1180 evtimer_set(timeout_event, second_elapsed_callback, NULL);
1181 one_second.tv_sec = 1;
1182 one_second.tv_usec = 0;
1185 n_libevent_errors = 0;
1187 /* log_fn(LOG_NOTICE, "Tick."); */
1188 tor_gettimeofday(&now);
1190 /* the second has rolled over. check more stuff. */
1191 bytes_written = stats_prev_global_write_bucket - global_write_bucket;
1192 bytes_read = stats_prev_global_read_bucket - global_read_bucket;
1193 seconds_elapsed = current_second ? (int)(now.tv_sec - current_second) : 0;
1194 stats_n_bytes_read += bytes_read;
1195 stats_n_bytes_written += bytes_written;
1196 if (accounting_is_enabled(options) && seconds_elapsed >= 0)
1197 accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
1198 control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
1199 control_event_stream_bandwidth_used();
1201 if (seconds_elapsed > 0)
1202 connection_bucket_refill(seconds_elapsed, now.tv_sec);
1203 stats_prev_global_read_bucket = global_read_bucket;
1204 stats_prev_global_write_bucket = global_write_bucket;
1206 if (server_mode(options) &&
1207 !we_are_hibernating() &&
1208 seconds_elapsed > 0 &&
1209 has_completed_circuit &&
1210 stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
1211 (stats_n_seconds_working+seconds_elapsed) /
1212 TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
1213 /* every 20 minutes, check and complain if necessary */
1214 routerinfo_t *me = router_get_my_routerinfo();
1215 if (me && !check_whether_orport_reachable())
1216 log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
1217 "its ORPort is reachable. Please check your firewalls, ports, "
1218 "address, /etc/hosts file, etc.",
1219 me->address, me->or_port);
1220 if (me && !check_whether_dirport_reachable())
1221 log_warn(LD_CONFIG,
1222 "Your server (%s:%d) has not managed to confirm that its "
1223 "DirPort is reachable. Please check your firewalls, ports, "
1224 "address, /etc/hosts file, etc.",
1225 me->address, me->dir_port);
1228 /** If more than this many seconds have elapsed, probably the clock
1229 * jumped: doesn't count. */
1230 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
1231 if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
1232 seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
1233 circuit_note_clock_jumped(seconds_elapsed);
1234 /* XXX if the time jumps *back* many months, do our events in
1235 * run_scheduled_events() recover? I don't think they do. -RD */
1236 } else if (seconds_elapsed > 0)
1237 stats_n_seconds_working += seconds_elapsed;
1239 run_scheduled_events(now.tv_sec);
1241 current_second = now.tv_sec; /* remember which second it is, for next time */
1243 #if 0
1244 if (current_second % 300 == 0) {
1245 rep_history_clean(current_second - options->RephistTrackTime);
1246 dumpmemusage(get_min_log_level()<LOG_INFO ?
1247 get_min_log_level() : LOG_INFO);
1249 #endif
1251 if (evtimer_add(timeout_event, &one_second))
1252 log_err(LD_NET,
1253 "Error from libevent when setting one-second timeout event");
1256 #ifndef MS_WINDOWS
1257 /** Called when a possibly ignorable libevent error occurs; ensures that we
1258 * don't get into an infinite loop by ignoring too many errors from
1259 * libevent. */
1260 static int
1261 got_libevent_error(void)
1263 if (++n_libevent_errors > 8) {
1264 log_err(LD_NET, "Too many libevent errors in one second; dying");
1265 return -1;
1267 return 0;
1269 #endif
1271 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
1273 /** Called when our IP address seems to have changed. <b>at_interface</b>
1274 * should be true if we detected a change in our interface, and false if we
1275 * detected a change in our published address. */
1276 void
1277 ip_address_changed(int at_interface)
1279 int server = server_mode(get_options());
1281 if (at_interface) {
1282 if (! server) {
1283 /* Okay, change our keys. */
1284 init_keys();
1286 } else {
1287 if (server) {
1288 if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
1289 reset_bandwidth_test();
1290 stats_n_seconds_working = 0;
1291 router_reset_reachability();
1292 mark_my_descriptor_dirty();
1296 dns_servers_relaunch_checks();
1299 /** Forget what we've learned about the correctness of our DNS servers, and
1300 * start learning again. */
1301 void
1302 dns_servers_relaunch_checks(void)
1304 if (server_mode(get_options())) {
1305 dns_reset_correctness_checks();
1306 time_to_check_for_correct_dns = 0;
1310 /** Called when we get a SIGHUP: reload configuration files and keys,
1311 * retry all connections, and so on. */
1312 static int
1313 do_hup(void)
1315 or_options_t *options = get_options();
1317 #ifdef USE_DMALLOC
1318 dmalloc_log_stats();
1319 dmalloc_log_changed(0, 1, 0, 0);
1320 #endif
1322 log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config.");
1323 if (accounting_is_enabled(options))
1324 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1326 router_reset_warnings();
1327 routerlist_reset_warnings();
1328 addressmap_clear_transient();
1329 /* first, reload config variables, in case they've changed */
1330 /* no need to provide argc/v, they've been cached inside init_from_config */
1331 if (options_init_from_torrc(0, NULL) < 0) {
1332 log_err(LD_CONFIG,"Reading config failed--see warnings above. "
1333 "For usage, try -h.");
1334 return -1;
1336 options = get_options(); /* they have changed now */
1337 if (authdir_mode_handles_descs(options, -1)) {
1338 /* reload the approved-routers file */
1339 if (dirserv_load_fingerprint_file() < 0) {
1340 /* warnings are logged from dirserv_load_fingerprint_file() directly */
1341 log_info(LD_GENERAL, "Error reloading fingerprints. "
1342 "Continuing with old list.");
1346 /* Rotate away from the old dirty circuits. This has to be done
1347 * after we've read the new options, but before we start using
1348 * circuits for directory fetches. */
1349 circuit_expire_all_dirty_circs();
1351 /* retry appropriate downloads */
1352 router_reset_status_download_failures();
1353 router_reset_descriptor_download_failures();
1354 update_networkstatus_downloads(time(NULL));
1356 /* We'll retry routerstatus downloads in about 10 seconds; no need to
1357 * force a retry there. */
1359 if (server_mode(options)) {
1360 /* Restart cpuworker and dnsworker processes, so they get up-to-date
1361 * configuration options. */
1362 cpuworkers_rotate();
1363 dns_reset();
1365 return 0;
1368 /** Tor main loop. */
1369 /* static */ int
1370 do_main_loop(void)
1372 int loop_result;
1373 time_t now;
1375 /* initialize dns resolve map, spawn workers if needed */
1376 if (dns_init() < 0) {
1377 log_err(LD_GENERAL,"Error initializing dns subsystem; exiting");
1378 return -1;
1381 handle_signals(1);
1383 /* load the private keys, if we're supposed to have them, and set up the
1384 * TLS context. */
1385 if (! identity_key_is_set()) {
1386 if (init_keys() < 0) {
1387 log_err(LD_BUG,"Error initializing keys; exiting");
1388 return -1;
1392 /* Set up the packed_cell_t memory pool. */
1393 init_cell_pool();
1395 /* Set up our buckets */
1396 connection_bucket_init();
1397 stats_prev_global_read_bucket = global_read_bucket;
1398 stats_prev_global_write_bucket = global_write_bucket;
1400 if (trusted_dirs_reload_certs())
1401 return -1;
1402 if (router_reload_v2_networkstatus()) {
1403 return -1;
1405 if (router_reload_consensus_networkstatus()) {
1406 return -1;
1408 /* load the routers file, or assign the defaults. */
1409 if (router_reload_router_list()) {
1410 return -1;
1412 /* load the networkstatuses. (This launches a download for new routers as
1413 * appropriate.)
1415 now = time(NULL);
1416 directory_info_has_arrived(now, 1);
1418 if (authdir_mode_tests_reachability(get_options())) {
1419 /* the directory is already here, run startup things */
1420 dirserv_test_reachability(now, 1);
1423 if (server_mode(get_options())) {
1424 /* launch cpuworkers. Need to do this *after* we've read the onion key. */
1425 cpu_init();
1428 /* set up once-a-second callback. */
1429 second_elapsed_callback(0,0,NULL);
1431 for (;;) {
1432 if (nt_service_is_stopping())
1433 return 0;
1435 #ifndef MS_WINDOWS
1436 /* Make it easier to tell whether libevent failure is our fault or not. */
1437 errno = 0;
1438 #endif
1439 /* All active linked conns should get their read events activated. */
1440 SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
1441 event_active(conn->read_event, EV_READ, 1));
1442 called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
1444 /* poll until we have an event, or the second ends, or until we have
1445 * some active linked connections to trigger events for. */
1446 loop_result = event_loop(called_loop_once ? EVLOOP_ONCE : 0);
1448 /* let catch() handle things like ^c, and otherwise don't worry about it */
1449 if (loop_result < 0) {
1450 int e = tor_socket_errno(-1);
1451 /* let the program survive things like ^z */
1452 if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
1453 #ifdef HAVE_EVENT_GET_METHOD
1454 log_err(LD_NET,"libevent call with %s failed: %s [%d]",
1455 event_get_method(), tor_socket_strerror(e), e);
1456 #else
1457 log_err(LD_NET,"libevent call failed: %s [%d]",
1458 tor_socket_strerror(e), e);
1459 #endif
1460 return -1;
1461 #ifndef MS_WINDOWS
1462 } else if (e == EINVAL) {
1463 log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
1464 if (got_libevent_error())
1465 return -1;
1466 #endif
1467 } else {
1468 if (ERRNO_IS_EINPROGRESS(e))
1469 log_warn(LD_BUG,
1470 "libevent call returned EINPROGRESS? Please report.");
1471 log_debug(LD_NET,"libevent call interrupted.");
1472 /* You can't trust the results of this poll(). Go back to the
1473 * top of the big for loop. */
1474 continue;
1480 /** Used to implement the SIGNAL control command: if we accept
1481 * <b>the_signal</b> as a remote pseudo-signal, act on it. */
1482 /* We don't re-use catch() here because:
1483 * 1. We handle a different set of signals than those allowed in catch.
1484 * 2. Platforms without signal() are unlikely to define SIGfoo.
1485 * 3. The control spec is defined to use fixed numeric signal values
1486 * which just happen to match the unix values.
1488 void
1489 control_signal_act(int the_signal)
1491 switch (the_signal)
1493 case 1:
1494 signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
1495 break;
1496 case 2:
1497 signal_callback(0,0,(void*)(uintptr_t)SIGINT);
1498 break;
1499 case 10:
1500 signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
1501 break;
1502 case 12:
1503 signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
1504 break;
1505 case 15:
1506 signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
1507 break;
1508 case SIGNEWNYM:
1509 signal_callback(0,0,(void*)(uintptr_t)SIGNEWNYM);
1510 break;
1511 case SIGCLEARDNSCACHE:
1512 signal_callback(0,0,(void*)(uintptr_t)SIGCLEARDNSCACHE);
1513 break;
1514 default:
1515 log_warn(LD_BUG, "Unrecognized signal number %d.", the_signal);
1516 break;
1520 /** Libevent callback: invoked when we get a signal.
1522 static void
1523 signal_callback(int fd, short events, void *arg)
1525 uintptr_t sig = (uintptr_t)arg;
1526 (void)fd;
1527 (void)events;
1528 switch (sig)
1530 case SIGTERM:
1531 log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
1532 tor_cleanup();
1533 exit(0);
1534 break;
1535 case SIGINT:
1536 if (!server_mode(get_options())) { /* do it now */
1537 log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
1538 tor_cleanup();
1539 exit(0);
1541 hibernate_begin_shutdown();
1542 break;
1543 #ifdef SIGPIPE
1544 case SIGPIPE:
1545 log_debug(LD_GENERAL,"Caught sigpipe. Ignoring.");
1546 break;
1547 #endif
1548 case SIGUSR1:
1549 /* prefer to log it at INFO, but make sure we always see it */
1550 dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
1551 break;
1552 case SIGUSR2:
1553 switch_logs_debug();
1554 log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
1555 "Send HUP to change back.");
1556 break;
1557 case SIGHUP:
1558 if (do_hup() < 0) {
1559 log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
1560 tor_cleanup();
1561 exit(1);
1563 break;
1564 #ifdef SIGCHLD
1565 case SIGCHLD:
1566 while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
1567 zombies */
1568 break;
1569 #endif
1570 case SIGNEWNYM: {
1571 time_t now = time(NULL);
1572 if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
1573 signewnym_is_pending = 1;
1574 log(LOG_NOTICE, LD_CONTROL,
1575 "Rate limiting NEWNYM request: delaying by %d second(s)",
1576 (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
1577 } else {
1578 signewnym_impl(now);
1580 break;
1582 case SIGCLEARDNSCACHE:
1583 addressmap_clear_transient();
1584 break;
1588 extern uint64_t rephist_total_alloc;
1589 extern uint32_t rephist_total_num;
1592 * Write current memory usage information to the log.
1594 static void
1595 dumpmemusage(int severity)
1597 connection_dump_buffer_mem_stats(severity);
1598 log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
1599 U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
1600 dump_routerlist_mem_usage(severity);
1601 dump_cell_pool_usage(severity);
1602 buf_dump_freelist_sizes(severity);
1603 tor_log_mallinfo(severity);
1606 /** Write all statistics to the log, with log level 'severity'. Called
1607 * in response to a SIGUSR1. */
1608 static void
1609 dumpstats(int severity)
1611 time_t now = time(NULL);
1612 time_t elapsed;
1614 log(severity, LD_GENERAL, "Dumping stats:");
1616 SMARTLIST_FOREACH(connection_array, connection_t *, conn,
1618 int i = conn_sl_idx;
1619 log(severity, LD_GENERAL,
1620 "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
1621 i, conn->s, conn->type, conn_type_to_string(conn->type),
1622 conn->state, conn_state_to_string(conn->type, conn->state),
1623 (int)(now - conn->timestamp_created));
1624 if (!connection_is_listener(conn)) {
1625 log(severity,LD_GENERAL,
1626 "Conn %d is to %s:%d.", i,
1627 safe_str(conn->address), conn->port);
1628 log(severity,LD_GENERAL,
1629 "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
1631 (int)buf_datalen(conn->inbuf),
1632 (int)buf_allocation(conn->inbuf),
1633 (int)(now - conn->timestamp_lastread));
1634 log(severity,LD_GENERAL,
1635 "Conn %d: %d bytes waiting on outbuf "
1636 "(len %d, last written %d secs ago)",i,
1637 (int)buf_datalen(conn->outbuf),
1638 (int)buf_allocation(conn->outbuf),
1639 (int)(now - conn->timestamp_lastwritten));
1641 circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
1642 * using this conn */
1644 log(severity, LD_NET,
1645 "Cells processed: "U64_FORMAT" padding\n"
1646 " "U64_FORMAT" create\n"
1647 " "U64_FORMAT" created\n"
1648 " "U64_FORMAT" relay\n"
1649 " ("U64_FORMAT" relayed)\n"
1650 " ("U64_FORMAT" delivered)\n"
1651 " "U64_FORMAT" destroy",
1652 U64_PRINTF_ARG(stats_n_padding_cells_processed),
1653 U64_PRINTF_ARG(stats_n_create_cells_processed),
1654 U64_PRINTF_ARG(stats_n_created_cells_processed),
1655 U64_PRINTF_ARG(stats_n_relay_cells_processed),
1656 U64_PRINTF_ARG(stats_n_relay_cells_relayed),
1657 U64_PRINTF_ARG(stats_n_relay_cells_delivered),
1658 U64_PRINTF_ARG(stats_n_destroy_cells_processed));
1659 if (stats_n_data_cells_packaged)
1660 log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
1661 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
1662 U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
1663 if (stats_n_data_cells_received)
1664 log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
1665 100*(U64_TO_DBL(stats_n_data_bytes_received) /
1666 U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
1668 if (now - time_of_process_start >= 0)
1669 elapsed = now - time_of_process_start;
1670 else
1671 elapsed = 0;
1673 if (elapsed) {
1674 log(severity, LD_NET,
1675 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
1676 U64_PRINTF_ARG(stats_n_bytes_read),
1677 (int)elapsed,
1678 (int) (stats_n_bytes_read/elapsed));
1679 log(severity, LD_NET,
1680 "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
1681 U64_PRINTF_ARG(stats_n_bytes_written),
1682 (int)elapsed,
1683 (int) (stats_n_bytes_written/elapsed));
1686 log(severity, LD_NET, "--------------- Dumping memory information:");
1687 dumpmemusage(severity);
1689 rep_hist_dump_stats(now,severity);
1690 rend_service_dump_stats(severity);
1691 dump_pk_ops(severity);
1692 dump_distinct_digest_count(severity);
1695 /** Called by exit() as we shut down the process.
1697 static void
1698 exit_function(void)
1700 /* NOTE: If we ever daemonize, this gets called immediately. That's
1701 * okay for now, because we only use this on Windows. */
1702 #ifdef MS_WINDOWS
1703 WSACleanup();
1704 #endif
1707 /** Set up the signal handlers for either parent or child. */
1708 void
1709 handle_signals(int is_parent)
1711 #ifndef MS_WINDOWS /* do signal stuff only on unix */
1712 int i;
1713 static int signals[] = {
1714 SIGINT, /* do a controlled slow shutdown */
1715 SIGTERM, /* to terminate now */
1716 SIGPIPE, /* otherwise sigpipe kills us */
1717 SIGUSR1, /* dump stats */
1718 SIGUSR2, /* go to loglevel debug */
1719 SIGHUP, /* to reload config, retry conns, etc */
1720 #ifdef SIGXFSZ
1721 SIGXFSZ, /* handle file-too-big resource exhaustion */
1722 #endif
1723 SIGCHLD, /* handle dns/cpu workers that exit */
1724 -1 };
1725 static struct event signal_events[16]; /* bigger than it has to be. */
1726 if (is_parent) {
1727 for (i = 0; signals[i] >= 0; ++i) {
1728 signal_set(&signal_events[i], signals[i], signal_callback,
1729 (void*)(uintptr_t)signals[i]);
1730 if (signal_add(&signal_events[i], NULL))
1731 log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
1732 signals[i]);
1734 } else {
1735 struct sigaction action;
1736 action.sa_flags = 0;
1737 sigemptyset(&action.sa_mask);
1738 action.sa_handler = SIG_IGN;
1739 sigaction(SIGINT, &action, NULL);
1740 sigaction(SIGTERM, &action, NULL);
1741 sigaction(SIGPIPE, &action, NULL);
1742 sigaction(SIGUSR1, &action, NULL);
1743 sigaction(SIGUSR2, &action, NULL);
1744 sigaction(SIGHUP, &action, NULL);
1745 #ifdef SIGXFSZ
1746 sigaction(SIGXFSZ, &action, NULL);
1747 #endif
1749 #else /* MS windows */
1750 (void)is_parent;
1751 #endif /* signal stuff */
1754 /** Main entry point for the Tor command-line client.
1756 /* static */ int
1757 tor_init(int argc, char *argv[])
1759 char buf[256];
1760 int i, quiet = 0;
1761 time_of_process_start = time(NULL);
1762 if (!connection_array)
1763 connection_array = smartlist_create();
1764 if (!closeable_connection_lst)
1765 closeable_connection_lst = smartlist_create();
1766 if (!active_linked_connection_lst)
1767 active_linked_connection_lst = smartlist_create();
1768 /* Have the log set up with our application name. */
1769 tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
1770 log_set_application_name(buf);
1771 /* Initialize threading. */
1772 tor_threads_init();
1773 /* Initialize the history structures. */
1774 rep_hist_init();
1775 /* Initialize the service cache. */
1776 rend_cache_init();
1777 addressmap_init(); /* Init the client dns cache. Do it always, since it's
1778 * cheap. */
1780 /* We search for the "quiet" option first, since it decides whether we
1781 * will log anything at all to the command line. */
1782 for (i=1;i<argc;++i) {
1783 if (!strcmp(argv[i], "--quiet"))
1784 quiet = 1;
1786 if (!quiet) {
1787 /* give it somewhere to log to initially */
1788 add_temp_log();
1791 log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
1792 "Do not rely on it for strong anonymity. (Running on %s)",get_version(),
1793 get_uname());
1795 if (network_init()<0) {
1796 log_err(LD_BUG,"Error initializing network; exiting.");
1797 return -1;
1799 atexit(exit_function);
1801 if (options_init_from_torrc(argc,argv) < 0) {
1802 log_err(LD_CONFIG,"Reading config failed--see warnings above.");
1803 return -1;
1806 #ifndef MS_WINDOWS
1807 if (geteuid()==0)
1808 log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
1809 "and you probably shouldn't.");
1810 #endif
1812 crypto_global_init(get_options()->HardwareAccel);
1813 if (crypto_seed_rng()) {
1814 log_err(LD_BUG, "Unable to seed random number generator. Exiting.");
1815 return -1;
1818 return 0;
1821 /** Free all memory that we might have allocated somewhere.
1822 * If <b>postfork</b>, we are a worker process and we want to free
1823 * only the parts of memory that we won't touch. If !<b>postfork</b>,
1824 * Tor is shutting down and we should free everything.
1826 * Helps us find the real leaks with dmalloc and the like. Also valgrind
1827 * should then report 0 reachable in its leak report (in an ideal world --
1828 * in practice libevent, ssl, libc etc never quite free everything). */
1829 void
1830 tor_free_all(int postfork)
1832 if (!postfork) {
1833 evdns_shutdown(1);
1835 geoip_free_all();
1836 dirvote_free_all();
1837 routerlist_free_all();
1838 networkstatus_free_all();
1839 addressmap_free_all();
1840 set_exit_redirects(NULL); /* free the registered exit redirects */
1841 dirserv_free_all();
1842 rend_service_free_all();
1843 rend_cache_free_all();
1844 rep_hist_free_all();
1845 hs_usage_free_all();
1846 dns_free_all();
1847 clear_pending_onions();
1848 circuit_free_all();
1849 entry_guards_free_all();
1850 connection_free_all();
1851 buf_shrink_freelists(1);
1852 policies_free_all();
1853 if (!postfork) {
1854 config_free_all();
1855 router_free_all();
1857 free_cell_pool();
1858 tor_tls_free_all();
1859 /* stuff in main.c */
1860 smartlist_free(connection_array);
1861 smartlist_free(closeable_connection_lst);
1862 smartlist_free(active_linked_connection_lst);
1863 tor_free(timeout_event);
1864 /* Stuff in util.c */
1865 if (!postfork) {
1866 escaped(NULL);
1867 esc_router_info(NULL);
1868 logs_free_all(); /* free log strings. do this last so logs keep working. */
1872 /** Do whatever cleanup is necessary before shutting Tor down. */
1873 void
1874 tor_cleanup(void)
1876 or_options_t *options = get_options();
1877 /* Remove our pid file. We don't care if there was an error when we
1878 * unlink, nothing we could do about it anyways. */
1879 if (options->command == CMD_RUN_TOR) {
1880 if (options->PidFile)
1881 unlink(options->PidFile);
1882 if (accounting_is_enabled(options))
1883 accounting_record_bandwidth_usage(time(NULL), get_or_state());
1884 or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
1885 or_state_save(time(NULL));
1886 if (authdir_mode_tests_reachability(options))
1887 rep_hist_record_mtbf_data();
1889 #ifdef USE_DMALLOC
1890 dmalloc_log_stats();
1891 #endif
1892 tor_free_all(0); /* We could move tor_free_all back into the ifdef below
1893 later, if it makes shutdown unacceptably slow. But for
1894 now, leave it here: it's helped us catch bugs in the
1895 past. */
1896 crypto_global_cleanup();
1897 #ifdef USE_DMALLOC
1898 dmalloc_log_unfreed();
1899 dmalloc_shutdown();
1900 #endif
1903 /** Read/create keys as needed, and echo our fingerprint to stdout. */
1904 /* static */ int
1905 do_list_fingerprint(void)
1907 char buf[FINGERPRINT_LEN+1];
1908 crypto_pk_env_t *k;
1909 const char *nickname = get_options()->Nickname;
1910 if (!server_mode(get_options())) {
1911 log_err(LD_GENERAL,
1912 "Clients don't have long-term identity keys. Exiting.\n");
1913 return -1;
1915 tor_assert(nickname);
1916 if (init_keys() < 0) {
1917 log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
1918 return -1;
1920 if (!(k = get_identity_key())) {
1921 log_err(LD_GENERAL,"Error: missing identity key.");
1922 return -1;
1924 if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
1925 log_err(LD_BUG, "Error computing fingerprint");
1926 return -1;
1928 printf("%s %s\n", nickname, buf);
1929 return 0;
1932 /** Entry point for password hashing: take the desired password from
1933 * the command line, and print its salted hash to stdout. **/
1934 /* static */ void
1935 do_hash_password(void)
1938 char output[256];
1939 char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
1941 crypto_rand(key, S2K_SPECIFIER_LEN-1);
1942 key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
1943 secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
1944 get_options()->command_arg, strlen(get_options()->command_arg),
1945 key);
1946 base16_encode(output, sizeof(output), key, sizeof(key));
1947 printf("16:%s\n",output);
1950 #ifdef USE_DMALLOC
1951 #include <openssl/crypto.h>
1952 static void
1953 _tor_dmalloc_free(void *p)
1955 tor_free(p);
1957 #endif
1959 /** Main entry point for the Tor process. Called from main(). */
1960 /* This function is distinct from main() only so we can link main.c into
1961 * the unittest binary without conflicting with the unittests' main. */
1963 tor_main(int argc, char *argv[])
1965 int result = 0;
1966 #ifdef USE_DMALLOC
1967 int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc,
1968 _tor_dmalloc_free);
1969 log_notice(LD_CONFIG, "Set up dmalloc; returned %d", r);
1970 #endif
1971 init_logging();
1972 #ifdef NT_SERVICE
1974 int done = 0;
1975 result = nt_service_parse_options(argc, argv, &done);
1976 if (done) return result;
1978 #endif
1979 if (tor_init(argc, argv)<0)
1980 return -1;
1981 switch (get_options()->command) {
1982 case CMD_RUN_TOR:
1983 #ifdef NT_SERVICE
1984 nt_service_set_state(SERVICE_RUNNING);
1985 #endif
1986 result = do_main_loop();
1987 break;
1988 case CMD_LIST_FINGERPRINT:
1989 result = do_list_fingerprint();
1990 break;
1991 case CMD_HASH_PASSWORD:
1992 do_hash_password();
1993 result = 0;
1994 break;
1995 case CMD_VERIFY_CONFIG:
1996 printf("Configuration was valid\n");
1997 result = 0;
1998 break;
1999 case CMD_RUN_UNITTESTS: /* only set by test.c */
2000 default:
2001 log_warn(LD_BUG,"Illegal command number %d: internal error.",
2002 get_options()->command);
2003 result = -1;
2005 tor_cleanup();
2006 return result;