Back to index

tor  0.2.3.19-rc
main.c
Go to the documentation of this file.
00001 /* Copyright (c) 2001 Matej Pfajfar.
00002  * Copyright (c) 2001-2004, Roger Dingledine.
00003  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
00004  * Copyright (c) 2007-2012, The Tor Project, Inc. */
00005 /* See LICENSE for licensing information */
00006 
00013 #define MAIN_PRIVATE
00014 #include "or.h"
00015 #include "buffers.h"
00016 #include "circuitbuild.h"
00017 #include "circuitlist.h"
00018 #include "circuituse.h"
00019 #include "command.h"
00020 #include "config.h"
00021 #include "connection.h"
00022 #include "connection_edge.h"
00023 #include "connection_or.h"
00024 #include "control.h"
00025 #include "cpuworker.h"
00026 #include "directory.h"
00027 #include "dirserv.h"
00028 #include "dirvote.h"
00029 #include "dns.h"
00030 #include "dnsserv.h"
00031 #include "geoip.h"
00032 #include "hibernate.h"
00033 #include "main.h"
00034 #include "microdesc.h"
00035 #include "networkstatus.h"
00036 #include "nodelist.h"
00037 #include "ntmain.h"
00038 #include "onion.h"
00039 #include "policies.h"
00040 #include "transports.h"
00041 #include "relay.h"
00042 #include "rendclient.h"
00043 #include "rendcommon.h"
00044 #include "rendservice.h"
00045 #include "rephist.h"
00046 #include "router.h"
00047 #include "routerlist.h"
00048 #include "routerparse.h"
00049 #include "status.h"
00050 #ifdef USE_DMALLOC
00051 #include <dmalloc.h>
00052 #include <openssl/crypto.h>
00053 #endif
00054 #include "memarea.h"
00055 
00056 #ifdef HAVE_EVENT2_EVENT_H
00057 #include <event2/event.h>
00058 #else
00059 #include <event.h>
00060 #endif
00061 
00062 #ifdef USE_BUFFEREVENTS
00063 #include <event2/bufferevent.h>
00064 #endif
00065 
00066 void evdns_shutdown(int);
00067 
00068 /********* PROTOTYPES **********/
00069 
00070 static void dumpmemusage(int severity);
00071 static void dumpstats(int severity); /* log stats */
00072 static void conn_read_callback(evutil_socket_t fd, short event, void *_conn);
00073 static void conn_write_callback(evutil_socket_t fd, short event, void *_conn);
00074 static void second_elapsed_callback(periodic_timer_t *timer, void *args);
00075 static int conn_close_if_marked(int i);
00076 static void connection_start_reading_from_linked_conn(connection_t *conn);
00077 static int connection_should_read_from_linked_conn(connection_t *conn);
00078 
00079 /********* START VARIABLES **********/
00080 
00081 #ifndef USE_BUFFEREVENTS
00082 int global_read_bucket; 
00083 int global_write_bucket; 
00086 int global_relayed_read_bucket;
00088 int global_relayed_write_bucket;
00091 static int stats_prev_global_read_bucket;
00094 static int stats_prev_global_write_bucket;
00095 #endif
00096 
00097 /* DOCDOC stats_prev_n_read */
00098 static uint64_t stats_prev_n_read = 0;
00099 /* DOCDOC stats_prev_n_written */
00100 static uint64_t stats_prev_n_written = 0;
00101 
00102 /* XXX we might want to keep stats about global_relayed_*_bucket too. Or not.*/
00104 static uint64_t stats_n_bytes_read = 0;
00106 static uint64_t stats_n_bytes_written = 0;
00108 time_t time_of_process_start = 0;
00110 long stats_n_seconds_working = 0;
00112 static time_t time_to_check_for_correct_dns = 0;
00113 
00115 #define MAX_SIGNEWNYM_RATE 10
00116 
00117 static time_t time_of_last_signewnym = 0;
00119 static int signewnym_is_pending = 0;
00121 static unsigned newnym_epoch = 0;
00122 
00124 static smartlist_t *connection_array = NULL;
00127 static smartlist_t *closeable_connection_lst = NULL;
00130 static smartlist_t *active_linked_connection_lst = NULL;
00134 static int called_loop_once = 0;
00135 
00142 int can_complete_circuit=0;
00143 
00146 #define GREEDY_DESCRIPTOR_RETRY_INTERVAL (10)
00147 
00149 #define LAZY_DESCRIPTOR_RETRY_INTERVAL (60)
00150 
00152 #define DESCRIPTOR_FAILURE_RESET_INTERVAL (60*60)
00153 
00154 #define DIR_CONN_MAX_STALL (5*60)
00155 
00158 #define TLS_HANDSHAKE_TIMEOUT (60)
00159 
00164 int quiet_level = 0;
00165 
00166 /********* END VARIABLES ************/
00167 
00168 /****************************************************************************
00169 *
00170 * This section contains accessors and other methods on the connection_array
00171 * variables (which are global within this file and unavailable outside it).
00172 *
00173 ****************************************************************************/
00174 
00175 #if 0 && defined(USE_BUFFEREVENTS)
00176 static void
00177 free_old_inbuf(connection_t *conn)
00178 {
00179   if (! conn->inbuf)
00180     return;
00181 
00182   tor_assert(conn->outbuf);
00183   tor_assert(buf_datalen(conn->inbuf) == 0);
00184   tor_assert(buf_datalen(conn->outbuf) == 0);
00185   buf_free(conn->inbuf);
00186   buf_free(conn->outbuf);
00187   conn->inbuf = conn->outbuf = NULL;
00188 
00189   if (conn->read_event) {
00190     event_del(conn->read_event);
00191     tor_event_free(conn->read_event);
00192   }
00193   if (conn->write_event) {
00194     event_del(conn->read_event);
00195     tor_event_free(conn->write_event);
00196   }
00197   conn->read_event = conn->write_event = NULL;
00198 }
00199 #endif
00200 
00201 #if defined(_WIN32) && defined(USE_BUFFEREVENTS)
00202 
00204 static int
00205 set_buffer_lengths_to_zero(tor_socket_t s)
00206 {
00207   int zero = 0;
00208   int r = 0;
00209   if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (void*)&zero, sizeof(zero))) {
00210     log_warn(LD_NET, "Unable to clear SO_SNDBUF");
00211     r = -1;
00212   }
00213   if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void*)&zero, sizeof(zero))) {
00214     log_warn(LD_NET, "Unable to clear SO_RCVBUF");
00215     r = -1;
00216   }
00217   return r;
00218 }
00219 #endif
00220 
00225 int
00226 connection_add_impl(connection_t *conn, int is_connecting)
00227 {
00228   tor_assert(conn);
00229   tor_assert(SOCKET_OK(conn->s) ||
00230              conn->linked ||
00231              (conn->type == CONN_TYPE_AP &&
00232               TO_EDGE_CONN(conn)->is_dns_request));
00233 
00234   tor_assert(conn->conn_array_index == -1); /* can only connection_add once */
00235   conn->conn_array_index = smartlist_len(connection_array);
00236   smartlist_add(connection_array, conn);
00237 
00238 #ifdef USE_BUFFEREVENTS
00239   if (connection_type_uses_bufferevent(conn)) {
00240     if (SOCKET_OK(conn->s) && !conn->linked) {
00241 
00242 #ifdef _WIN32
00243       if (tor_libevent_using_iocp_bufferevents() &&
00244           get_options()->UserspaceIOCPBuffers) {
00245         set_buffer_lengths_to_zero(conn->s);
00246       }
00247 #endif
00248 
00249       conn->bufev = bufferevent_socket_new(
00250                          tor_libevent_get_base(),
00251                          conn->s,
00252                          BEV_OPT_DEFER_CALLBACKS);
00253       if (!conn->bufev) {
00254         log_warn(LD_BUG, "Unable to create socket bufferevent");
00255         smartlist_del(connection_array, conn->conn_array_index);
00256         conn->conn_array_index = -1;
00257         return -1;
00258       }
00259       if (is_connecting) {
00260         /* Put the bufferevent into a "connecting" state so that we'll get
00261          * a "connected" event callback on successful write. */
00262         bufferevent_socket_connect(conn->bufev, NULL, 0);
00263       }
00264       connection_configure_bufferevent_callbacks(conn);
00265     } else if (conn->linked && conn->linked_conn &&
00266                connection_type_uses_bufferevent(conn->linked_conn)) {
00267       tor_assert(!(SOCKET_OK(conn->s)));
00268       if (!conn->bufev) {
00269         struct bufferevent *pair[2] = { NULL, NULL };
00270         if (bufferevent_pair_new(tor_libevent_get_base(),
00271                                  BEV_OPT_DEFER_CALLBACKS,
00272                                  pair) < 0) {
00273           log_warn(LD_BUG, "Unable to create bufferevent pair");
00274           smartlist_del(connection_array, conn->conn_array_index);
00275           conn->conn_array_index = -1;
00276           return -1;
00277         }
00278         tor_assert(pair[0]);
00279         conn->bufev = pair[0];
00280         conn->linked_conn->bufev = pair[1];
00281       } /* else the other side already was added, and got a bufferevent_pair */
00282       connection_configure_bufferevent_callbacks(conn);
00283     } else {
00284       tor_assert(!conn->linked);
00285     }
00286 
00287     if (conn->bufev)
00288       tor_assert(conn->inbuf == NULL);
00289 
00290     if (conn->linked_conn && conn->linked_conn->bufev)
00291       tor_assert(conn->linked_conn->inbuf == NULL);
00292   }
00293 #else
00294   (void) is_connecting;
00295 #endif
00296 
00297   if (!HAS_BUFFEREVENT(conn) && (SOCKET_OK(conn->s) || conn->linked)) {
00298     conn->read_event = tor_event_new(tor_libevent_get_base(),
00299          conn->s, EV_READ|EV_PERSIST, conn_read_callback, conn);
00300     conn->write_event = tor_event_new(tor_libevent_get_base(),
00301          conn->s, EV_WRITE|EV_PERSIST, conn_write_callback, conn);
00302     /* XXXX CHECK FOR NULL RETURN! */
00303   }
00304 
00305   log_debug(LD_NET,"new conn type %s, socket %d, address %s, n_conns %d.",
00306             conn_type_to_string(conn->type), (int)conn->s, conn->address,
00307             smartlist_len(connection_array));
00308 
00309   return 0;
00310 }
00311 
00313 void
00314 connection_unregister_events(connection_t *conn)
00315 {
00316   if (conn->read_event) {
00317     if (event_del(conn->read_event))
00318       log_warn(LD_BUG, "Error removing read event for %d", (int)conn->s);
00319     tor_free(conn->read_event);
00320   }
00321   if (conn->write_event) {
00322     if (event_del(conn->write_event))
00323       log_warn(LD_BUG, "Error removing write event for %d", (int)conn->s);
00324     tor_free(conn->write_event);
00325   }
00326 #ifdef USE_BUFFEREVENTS
00327   if (conn->bufev) {
00328     bufferevent_free(conn->bufev);
00329     conn->bufev = NULL;
00330   }
00331 #endif
00332   if (conn->type == CONN_TYPE_AP_DNS_LISTENER) {
00333     dnsserv_close_listener(conn);
00334   }
00335 }
00336 
00341 int
00342 connection_remove(connection_t *conn)
00343 {
00344   int current_index;
00345   connection_t *tmp;
00346 
00347   tor_assert(conn);
00348 
00349   log_debug(LD_NET,"removing socket %d (type %s), n_conns now %d",
00350             (int)conn->s, conn_type_to_string(conn->type),
00351             smartlist_len(connection_array));
00352 
00353   tor_assert(conn->conn_array_index >= 0);
00354   current_index = conn->conn_array_index;
00355   connection_unregister_events(conn); /* This is redundant, but cheap. */
00356   if (current_index == smartlist_len(connection_array)-1) { /* at the end */
00357     smartlist_del(connection_array, current_index);
00358     return 0;
00359   }
00360 
00361   /* replace this one with the one at the end */
00362   smartlist_del(connection_array, current_index);
00363   tmp = smartlist_get(connection_array, current_index);
00364   tmp->conn_array_index = current_index;
00365 
00366   return 0;
00367 }
00368 
00378 static void
00379 connection_unlink(connection_t *conn)
00380 {
00381   connection_about_to_close_connection(conn);
00382   if (conn->conn_array_index >= 0) {
00383     connection_remove(conn);
00384   }
00385   if (conn->linked_conn) {
00386     conn->linked_conn->linked_conn = NULL;
00387     if (! conn->linked_conn->marked_for_close &&
00388         conn->linked_conn->reading_from_linked_conn)
00389       connection_start_reading(conn->linked_conn);
00390     conn->linked_conn = NULL;
00391   }
00392   smartlist_remove(closeable_connection_lst, conn);
00393   smartlist_remove(active_linked_connection_lst, conn);
00394   if (conn->type == CONN_TYPE_EXIT) {
00395     assert_connection_edge_not_dns_pending(TO_EDGE_CONN(conn));
00396   }
00397   if (conn->type == CONN_TYPE_OR) {
00398     if (!tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest))
00399       connection_or_remove_from_identity_map(TO_OR_CONN(conn));
00400   }
00401   connection_free(conn);
00402 }
00403 
00405 void
00406 add_connection_to_closeable_list(connection_t *conn)
00407 {
00408   tor_assert(!smartlist_isin(closeable_connection_lst, conn));
00409   tor_assert(conn->marked_for_close);
00410   assert_connection_ok(conn, time(NULL));
00411   smartlist_add(closeable_connection_lst, conn);
00412 }
00413 
00415 int
00416 connection_is_on_closeable_list(connection_t *conn)
00417 {
00418   return smartlist_isin(closeable_connection_lst, conn);
00419 }
00420 
00422 int
00423 connection_in_array(connection_t *conn)
00424 {
00425   return smartlist_isin(connection_array, conn);
00426 }
00427 
00432 smartlist_t *
00433 get_connection_array(void)
00434 {
00435   if (!connection_array)
00436     connection_array = smartlist_new();
00437   return connection_array;
00438 }
00439 
00442 uint64_t
00443 get_bytes_read(void)
00444 {
00445   return stats_n_bytes_read;
00446 }
00447 
00448 /* DOCDOC get_bytes_written */
00449 uint64_t
00450 get_bytes_written(void)
00451 {
00452   return stats_n_bytes_written;
00453 }
00454 
00458 void
00459 connection_watch_events(connection_t *conn, watchable_events_t events)
00460 {
00461   IF_HAS_BUFFEREVENT(conn, {
00462       short ev = ((short)events) & (EV_READ|EV_WRITE);
00463       short old_ev = bufferevent_get_enabled(conn->bufev);
00464       if ((ev & ~old_ev) != 0) {
00465         bufferevent_enable(conn->bufev, ev);
00466       }
00467       if ((old_ev & ~ev) != 0) {
00468         bufferevent_disable(conn->bufev, old_ev & ~ev);
00469       }
00470       return;
00471   });
00472   if (events & READ_EVENT)
00473     connection_start_reading(conn);
00474   else
00475     connection_stop_reading(conn);
00476 
00477   if (events & WRITE_EVENT)
00478     connection_start_writing(conn);
00479   else
00480     connection_stop_writing(conn);
00481 }
00482 
00484 int
00485 connection_is_reading(connection_t *conn)
00486 {
00487   tor_assert(conn);
00488 
00489   IF_HAS_BUFFEREVENT(conn,
00490     return (bufferevent_get_enabled(conn->bufev) & EV_READ) != 0;
00491   );
00492   return conn->reading_from_linked_conn ||
00493     (conn->read_event && event_pending(conn->read_event, EV_READ, NULL));
00494 }
00495 
00497 void
00498 connection_stop_reading(connection_t *conn)
00499 {
00500   tor_assert(conn);
00501 
00502   IF_HAS_BUFFEREVENT(conn, {
00503       bufferevent_disable(conn->bufev, EV_READ);
00504       return;
00505   });
00506 
00507   tor_assert(conn->read_event);
00508 
00509   if (conn->linked) {
00510     conn->reading_from_linked_conn = 0;
00511     connection_stop_reading_from_linked_conn(conn);
00512   } else {
00513     if (event_del(conn->read_event))
00514       log_warn(LD_NET, "Error from libevent setting read event state for %d "
00515                "to unwatched: %s",
00516                (int)conn->s,
00517                tor_socket_strerror(tor_socket_errno(conn->s)));
00518   }
00519 }
00520 
00522 void
00523 connection_start_reading(connection_t *conn)
00524 {
00525   tor_assert(conn);
00526 
00527   IF_HAS_BUFFEREVENT(conn, {
00528       bufferevent_enable(conn->bufev, EV_READ);
00529       return;
00530   });
00531 
00532   tor_assert(conn->read_event);
00533 
00534   if (conn->linked) {
00535     conn->reading_from_linked_conn = 1;
00536     if (connection_should_read_from_linked_conn(conn))
00537       connection_start_reading_from_linked_conn(conn);
00538   } else {
00539     if (event_add(conn->read_event, NULL))
00540       log_warn(LD_NET, "Error from libevent setting read event state for %d "
00541                "to watched: %s",
00542                (int)conn->s,
00543                tor_socket_strerror(tor_socket_errno(conn->s)));
00544   }
00545 }
00546 
00548 int
00549 connection_is_writing(connection_t *conn)
00550 {
00551   tor_assert(conn);
00552 
00553   IF_HAS_BUFFEREVENT(conn,
00554     return (bufferevent_get_enabled(conn->bufev) & EV_WRITE) != 0;
00555   );
00556 
00557   return conn->writing_to_linked_conn ||
00558     (conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL));
00559 }
00560 
00562 void
00563 connection_stop_writing(connection_t *conn)
00564 {
00565   tor_assert(conn);
00566 
00567   IF_HAS_BUFFEREVENT(conn, {
00568       bufferevent_disable(conn->bufev, EV_WRITE);
00569       return;
00570   });
00571 
00572   tor_assert(conn->write_event);
00573 
00574   if (conn->linked) {
00575     conn->writing_to_linked_conn = 0;
00576     if (conn->linked_conn)
00577       connection_stop_reading_from_linked_conn(conn->linked_conn);
00578   } else {
00579     if (event_del(conn->write_event))
00580       log_warn(LD_NET, "Error from libevent setting write event state for %d "
00581                "to unwatched: %s",
00582                (int)conn->s,
00583                tor_socket_strerror(tor_socket_errno(conn->s)));
00584   }
00585 }
00586 
00588 void
00589 connection_start_writing(connection_t *conn)
00590 {
00591   tor_assert(conn);
00592 
00593   IF_HAS_BUFFEREVENT(conn, {
00594       bufferevent_enable(conn->bufev, EV_WRITE);
00595       return;
00596   });
00597 
00598   tor_assert(conn->write_event);
00599 
00600   if (conn->linked) {
00601     conn->writing_to_linked_conn = 1;
00602     if (conn->linked_conn &&
00603         connection_should_read_from_linked_conn(conn->linked_conn))
00604       connection_start_reading_from_linked_conn(conn->linked_conn);
00605   } else {
00606     if (event_add(conn->write_event, NULL))
00607       log_warn(LD_NET, "Error from libevent setting write event state for %d "
00608                "to watched: %s",
00609                (int)conn->s,
00610                tor_socket_strerror(tor_socket_errno(conn->s)));
00611   }
00612 }
00613 
00619 static int
00620 connection_should_read_from_linked_conn(connection_t *conn)
00621 {
00622   if (conn->linked && conn->reading_from_linked_conn) {
00623     if (! conn->linked_conn ||
00624         (conn->linked_conn->writing_to_linked_conn &&
00625          buf_datalen(conn->linked_conn->outbuf)))
00626       return 1;
00627   }
00628   return 0;
00629 }
00630 
00634 static void
00635 connection_start_reading_from_linked_conn(connection_t *conn)
00636 {
00637   tor_assert(conn);
00638   tor_assert(conn->linked == 1);
00639 
00640   if (!conn->active_on_link) {
00641     conn->active_on_link = 1;
00642     smartlist_add(active_linked_connection_lst, conn);
00643     if (!called_loop_once) {
00644       /* This is the first event on the list; we won't be in LOOP_ONCE mode,
00645        * so we need to make sure that the event_base_loop() actually exits at
00646        * the end of its run through the current connections and lets us
00647        * activate read events for linked connections. */
00648       struct timeval tv = { 0, 0 };
00649       tor_event_base_loopexit(tor_libevent_get_base(), &tv);
00650     }
00651   } else {
00652     tor_assert(smartlist_isin(active_linked_connection_lst, conn));
00653   }
00654 }
00655 
00659 void
00660 connection_stop_reading_from_linked_conn(connection_t *conn)
00661 {
00662   tor_assert(conn);
00663   tor_assert(conn->linked == 1);
00664 
00665   if (conn->active_on_link) {
00666     conn->active_on_link = 0;
00667     /* FFFF We could keep an index here so we can smartlist_del
00668      * cleanly.  On the other hand, this doesn't show up on profiles,
00669      * so let's leave it alone for now. */
00670     smartlist_remove(active_linked_connection_lst, conn);
00671   } else {
00672     tor_assert(!smartlist_isin(active_linked_connection_lst, conn));
00673   }
00674 }
00675 
00677 static void
00678 close_closeable_connections(void)
00679 {
00680   int i;
00681   for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
00682     connection_t *conn = smartlist_get(closeable_connection_lst, i);
00683     if (conn->conn_array_index < 0) {
00684       connection_unlink(conn); /* blow it away right now */
00685     } else {
00686       if (!conn_close_if_marked(conn->conn_array_index))
00687         ++i;
00688     }
00689   }
00690 }
00691 
00694 static void
00695 conn_read_callback(evutil_socket_t fd, short event, void *_conn)
00696 {
00697   connection_t *conn = _conn;
00698   (void)fd;
00699   (void)event;
00700 
00701   log_debug(LD_NET,"socket %d wants to read.",(int)conn->s);
00702 
00703   /* assert_connection_ok(conn, time(NULL)); */
00704 
00705   if (connection_handle_read(conn) < 0) {
00706     if (!conn->marked_for_close) {
00707 #ifndef _WIN32
00708       log_warn(LD_BUG,"Unhandled error on read for %s connection "
00709                "(fd %d); removing",
00710                conn_type_to_string(conn->type), (int)conn->s);
00711       tor_fragile_assert();
00712 #endif
00713       if (CONN_IS_EDGE(conn))
00714         connection_edge_end_errno(TO_EDGE_CONN(conn));
00715       connection_mark_for_close(conn);
00716     }
00717   }
00718   assert_connection_ok(conn, time(NULL));
00719 
00720   if (smartlist_len(closeable_connection_lst))
00721     close_closeable_connections();
00722 }
00723 
00726 static void
00727 conn_write_callback(evutil_socket_t fd, short events, void *_conn)
00728 {
00729   connection_t *conn = _conn;
00730   (void)fd;
00731   (void)events;
00732 
00733   LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",
00734                      (int)conn->s));
00735 
00736   /* assert_connection_ok(conn, time(NULL)); */
00737 
00738   if (connection_handle_write(conn, 0) < 0) {
00739     if (!conn->marked_for_close) {
00740       /* this connection is broken. remove it. */
00741       log_fn(LOG_WARN,LD_BUG,
00742              "unhandled error on write for %s connection (fd %d); removing",
00743              conn_type_to_string(conn->type), (int)conn->s);
00744       tor_fragile_assert();
00745       if (CONN_IS_EDGE(conn)) {
00746         /* otherwise we cry wolf about duplicate close */
00747         edge_connection_t *edge_conn = TO_EDGE_CONN(conn);
00748         if (!edge_conn->end_reason)
00749           edge_conn->end_reason = END_STREAM_REASON_INTERNAL;
00750         edge_conn->edge_has_sent_end = 1;
00751       }
00752       connection_close_immediate(conn); /* So we don't try to flush. */
00753       connection_mark_for_close(conn);
00754     }
00755   }
00756   assert_connection_ok(conn, time(NULL));
00757 
00758   if (smartlist_len(closeable_connection_lst))
00759     close_closeable_connections();
00760 }
00761 
00770 static int
00771 conn_close_if_marked(int i)
00772 {
00773   connection_t *conn;
00774   int retval;
00775   time_t now;
00776 
00777   conn = smartlist_get(connection_array, i);
00778   if (!conn->marked_for_close)
00779     return 0; /* nothing to see here, move along */
00780   now = time(NULL);
00781   assert_connection_ok(conn, now);
00782   /* assert_all_pending_dns_resolves_ok(); */
00783 
00784 #ifdef USE_BUFFEREVENTS
00785   if (conn->bufev) {
00786     if (conn->hold_open_until_flushed &&
00787         evbuffer_get_length(bufferevent_get_output(conn->bufev))) {
00788       /* don't close yet. */
00789       return 0;
00790     }
00791     if (conn->linked_conn && ! conn->linked_conn->marked_for_close) {
00792       /* We need to do this explicitly so that the linked connection
00793        * notices that there was an EOF. */
00794       bufferevent_flush(conn->bufev, EV_WRITE, BEV_FINISHED);
00795     }
00796   }
00797 #endif
00798 
00799   log_debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
00800 
00801   /* If the connection we are about to close was trying to connect to
00802   a proxy server and failed, the client won't be able to use that
00803   proxy. We should warn the user about this. */
00804   if (conn->proxy_state == PROXY_INFANT)
00805     log_failed_proxy_connection(conn);
00806 
00807   IF_HAS_BUFFEREVENT(conn, goto unlink);
00808   if ((SOCKET_OK(conn->s) || conn->linked_conn) &&
00809       connection_wants_to_flush(conn)) {
00810     /* s == -1 means it's an incomplete edge connection, or that the socket
00811      * has already been closed as unflushable. */
00812     ssize_t sz = connection_bucket_write_limit(conn, now);
00813     if (!conn->hold_open_until_flushed)
00814       log_info(LD_NET,
00815                "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
00816                "to flush %d bytes. (Marked at %s:%d)",
00817                escaped_safe_str_client(conn->address),
00818                (int)conn->s, conn_type_to_string(conn->type), conn->state,
00819                (int)conn->outbuf_flushlen,
00820                 conn->marked_for_close_file, conn->marked_for_close);
00821     if (conn->linked_conn) {
00822       retval = move_buf_to_buf(conn->linked_conn->inbuf, conn->outbuf,
00823                                &conn->outbuf_flushlen);
00824       if (retval >= 0) {
00825         /* The linked conn will notice that it has data when it notices that
00826          * we're gone. */
00827         connection_start_reading_from_linked_conn(conn->linked_conn);
00828       }
00829       log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
00830                "%d left; flushlen %d; wants-to-flush==%d", retval,
00831                 (int)connection_get_outbuf_len(conn),
00832                 (int)conn->outbuf_flushlen,
00833                 connection_wants_to_flush(conn));
00834     } else if (connection_speaks_cells(conn)) {
00835       if (conn->state == OR_CONN_STATE_OPEN) {
00836         retval = flush_buf_tls(TO_OR_CONN(conn)->tls, conn->outbuf, sz,
00837                                &conn->outbuf_flushlen);
00838       } else
00839         retval = -1; /* never flush non-open broken tls connections */
00840     } else {
00841       retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
00842     }
00843     if (retval >= 0 && /* Technically, we could survive things like
00844                           TLS_WANT_WRITE here. But don't bother for now. */
00845         conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
00846       if (retval > 0) {
00847         LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
00848                            "Holding conn (fd %d) open for more flushing.",
00849                            (int)conn->s));
00850         conn->timestamp_lastwritten = now; /* reset so we can flush more */
00851       } else if (sz == 0) {
00852         /* Also, retval==0.  If we get here, we didn't want to write anything
00853          * (because of rate-limiting) and we didn't. */
00854 
00855         /* Connection must flush before closing, but it's being rate-limited.
00856          * Let's remove from Libevent, and mark it as blocked on bandwidth
00857          * so it will be re-added on next token bucket refill. Prevents
00858          * busy Libevent loops where we keep ending up here and returning
00859          * 0 until we are no longer blocked on bandwidth.
00860          */
00861         if (connection_is_writing(conn)) {
00862           conn->write_blocked_on_bw = 1;
00863           connection_stop_writing(conn);
00864         }
00865         if (connection_is_reading(conn)) {
00866           /* XXXX024 We should make this code unreachable; if a connection is
00867            * marked for close and flushing, there is no point in reading to it
00868            * at all. Further, checking at this point is a bit of a hack: it
00869            * would make much more sense to react in
00870            * connection_handle_read_impl, or to just stop reading in
00871            * mark_and_flush */
00872 #if 0
00873 #define MARKED_READING_RATE 180
00874           static ratelim_t marked_read_lim = RATELIM_INIT(MARKED_READING_RATE);
00875           char *m;
00876           if ((m = rate_limit_log(&marked_read_lim, now))) {
00877             log_warn(LD_BUG, "Marked connection (fd %d, type %s, state %s) "
00878                      "is still reading; that shouldn't happen.%s",
00879                      (int)conn->s, conn_type_to_string(conn->type),
00880                      conn_state_to_string(conn->type, conn->state), m);
00881             tor_free(m);
00882           }
00883 #endif
00884           conn->read_blocked_on_bw = 1;
00885           connection_stop_reading(conn);
00886         }
00887       }
00888       return 0;
00889     }
00890     if (connection_wants_to_flush(conn)) {
00891       int severity;
00892       if (conn->type == CONN_TYPE_EXIT ||
00893           (conn->type == CONN_TYPE_OR && server_mode(get_options())) ||
00894           (conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
00895         severity = LOG_INFO;
00896       else
00897         severity = LOG_NOTICE;
00898       /* XXXX Maybe allow this to happen a certain amount per hour; it usually
00899        * is meaningless. */
00900       log_fn(severity, LD_NET, "We stalled too much while trying to write %d "
00901              "bytes to address %s.  If this happens a lot, either "
00902              "something is wrong with your network connection, or "
00903              "something is wrong with theirs. "
00904              "(fd %d, type %s, state %d, marked at %s:%d).",
00905              (int)connection_get_outbuf_len(conn),
00906              escaped_safe_str_client(conn->address),
00907              (int)conn->s, conn_type_to_string(conn->type), conn->state,
00908              conn->marked_for_close_file,
00909              conn->marked_for_close);
00910     }
00911   }
00912 
00913 #ifdef USE_BUFFEREVENTS
00914  unlink:
00915 #endif
00916   connection_unlink(conn); /* unlink, remove, free */
00917   return 1;
00918 }
00919 
00926 void
00927 directory_all_unreachable(time_t now)
00928 {
00929   connection_t *conn;
00930   (void)now;
00931 
00932   stats_n_seconds_working=0; /* reset it */
00933 
00934   while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
00935                                               AP_CONN_STATE_CIRCUIT_WAIT))) {
00936     entry_connection_t *entry_conn = TO_ENTRY_CONN(conn);
00937     log_notice(LD_NET,
00938                "Is your network connection down? "
00939                "Failing connection to '%s:%d'.",
00940                safe_str_client(entry_conn->socks_request->address),
00941                entry_conn->socks_request->port);
00942     connection_mark_unattached_ap(entry_conn,
00943                                   END_STREAM_REASON_NET_UNREACHABLE);
00944   }
00945   control_event_general_status(LOG_ERR, "DIR_ALL_UNREACHABLE");
00946 }
00947 
00950 void
00951 directory_info_has_arrived(time_t now, int from_cache)
00952 {
00953   const or_options_t *options = get_options();
00954 
00955   if (!router_have_minimum_dir_info()) {
00956     int quiet = directory_too_idle_to_fetch_descriptors(options, now);
00957     log(quiet ? LOG_INFO : LOG_NOTICE, LD_DIR,
00958         "I learned some more directory information, but not enough to "
00959         "build a circuit: %s", get_dir_info_status_string());
00960     update_all_descriptor_downloads(now);
00961     return;
00962   } else {
00963     if (directory_fetches_from_authorities(options)) {
00964       update_all_descriptor_downloads(now);
00965     }
00966 
00967     /* if we have enough dir info, then update our guard status with
00968      * whatever we just learned. */
00969     entry_guards_compute_status(options, now);
00970     /* Don't even bother trying to get extrainfo until the rest of our
00971      * directory info is up-to-date */
00972     if (options->DownloadExtraInfo)
00973       update_extrainfo_downloads(now);
00974   }
00975 
00976   if (server_mode(options) && !net_is_disabled() && !from_cache &&
00977       (can_complete_circuit || !any_predicted_circuits(now)))
00978     consider_testing_reachability(1, 1);
00979 }
00980 
00988 #define IDLE_OR_CONN_TIMEOUT 180
00989 
00993 static void
00994 run_connection_housekeeping(int i, time_t now)
00995 {
00996   cell_t cell;
00997   connection_t *conn = smartlist_get(connection_array, i);
00998   const or_options_t *options = get_options();
00999   or_connection_t *or_conn;
01000   int past_keepalive =
01001     now >= conn->timestamp_lastwritten + options->KeepalivePeriod;
01002 
01003   if (conn->outbuf && !connection_get_outbuf_len(conn) &&
01004       conn->type == CONN_TYPE_OR)
01005     TO_OR_CONN(conn)->timestamp_lastempty = now;
01006 
01007   if (conn->marked_for_close) {
01008     /* nothing to do here */
01009     return;
01010   }
01011 
01012   /* Expire any directory connections that haven't been active (sent
01013    * if a server or received if a client) for 5 min */
01014   if (conn->type == CONN_TYPE_DIR &&
01015       ((DIR_CONN_IS_SERVER(conn) &&
01016         conn->timestamp_lastwritten + DIR_CONN_MAX_STALL < now) ||
01017        (!DIR_CONN_IS_SERVER(conn) &&
01018         conn->timestamp_lastread + DIR_CONN_MAX_STALL < now))) {
01019     log_info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
01020              (int)conn->s, conn->purpose);
01021     /* This check is temporary; it's to let us know whether we should consider
01022      * parsing partial serverdesc responses. */
01023     if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
01024         connection_get_inbuf_len(conn) >= 1024) {
01025       log_info(LD_DIR,"Trying to extract information from wedged server desc "
01026                "download.");
01027       connection_dir_reached_eof(TO_DIR_CONN(conn));
01028     } else {
01029       connection_mark_for_close(conn);
01030     }
01031     return;
01032   }
01033 
01034   if (!connection_speaks_cells(conn))
01035     return; /* we're all done here, the rest is just for OR conns */
01036 
01037   /* If we haven't written to an OR connection for a while, then either nuke
01038      the connection or send a keepalive, depending. */
01039 
01040   or_conn = TO_OR_CONN(conn);
01041 #ifdef USE_BUFFEREVENTS
01042   tor_assert(conn->bufev);
01043 #else
01044   tor_assert(conn->outbuf);
01045 #endif
01046 
01047   if (or_conn->is_bad_for_new_circs && !or_conn->n_circuits) {
01048     /* It's bad for new circuits, and has no unmarked circuits on it:
01049      * mark it now. */
01050     log_info(LD_OR,
01051              "Expiring non-used OR connection to fd %d (%s:%d) [Too old].",
01052              (int)conn->s, conn->address, conn->port);
01053     if (conn->state == OR_CONN_STATE_CONNECTING)
01054       connection_or_connect_failed(TO_OR_CONN(conn),
01055                                    END_OR_CONN_REASON_TIMEOUT,
01056                                    "Tor gave up on the connection");
01057     connection_mark_and_flush(conn);
01058   } else if (!connection_state_is_open(conn)) {
01059     if (past_keepalive) {
01060       /* We never managed to actually get this connection open and happy. */
01061       log_info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
01062                (int)conn->s,conn->address, conn->port);
01063       connection_mark_for_close(conn);
01064     }
01065   } else if (we_are_hibernating() && !or_conn->n_circuits &&
01066              !connection_get_outbuf_len(conn)) {
01067     /* We're hibernating, there's no circuits, and nothing to flush.*/
01068     log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
01069              "[Hibernating or exiting].",
01070              (int)conn->s,conn->address, conn->port);
01071     connection_mark_and_flush(conn);
01072   } else if (!or_conn->n_circuits &&
01073              now >= or_conn->timestamp_last_added_nonpadding +
01074                                          IDLE_OR_CONN_TIMEOUT) {
01075     log_info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
01076              "[idle %d].", (int)conn->s,conn->address, conn->port,
01077              (int)(now - or_conn->timestamp_last_added_nonpadding));
01078     connection_mark_for_close(conn);
01079   } else if (
01080       now >= or_conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
01081       now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
01082     log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
01083            "Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
01084            "flush; %d seconds since last write)",
01085            (int)conn->s, conn->address, conn->port,
01086            (int)connection_get_outbuf_len(conn),
01087            (int)(now-conn->timestamp_lastwritten));
01088     connection_mark_for_close(conn);
01089   } else if (past_keepalive && !connection_get_outbuf_len(conn)) {
01090     /* send a padding cell */
01091     log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
01092            conn->address, conn->port);
01093     memset(&cell,0,sizeof(cell_t));
01094     cell.command = CELL_PADDING;
01095     connection_or_write_cell_to_buf(&cell, or_conn);
01096   }
01097 }
01098 
01101 static void
01102 signewnym_impl(time_t now)
01103 {
01104   const or_options_t *options = get_options();
01105   if (!proxy_mode(options)) {
01106     log_info(LD_CONTROL, "Ignoring SIGNAL NEWNYM because client functionality "
01107              "is disabled.");
01108     return;
01109   }
01110 
01111   circuit_expire_all_dirty_circs();
01112   addressmap_clear_transient();
01113   rend_client_purge_state();
01114   time_of_last_signewnym = now;
01115   signewnym_is_pending = 0;
01116 
01117   ++newnym_epoch;
01118 
01119   control_event_signal(SIGNEWNYM);
01120 }
01121 
01123 unsigned
01124 get_signewnym_epoch(void)
01125 {
01126   return newnym_epoch;
01127 }
01128 
01132 static void
01133 run_scheduled_events(time_t now)
01134 {
01135   static time_t last_rotated_x509_certificate = 0;
01136   static time_t time_to_check_v3_certificate = 0;
01137   static time_t time_to_check_listeners = 0;
01138   static time_t time_to_check_descriptor = 0;
01139   static time_t time_to_check_ipaddress = 0;
01140   static time_t time_to_shrink_memory = 0;
01141   static time_t time_to_try_getting_descriptors = 0;
01142   static time_t time_to_reset_descriptor_failures = 0;
01143   static time_t time_to_add_entropy = 0;
01144   static time_t time_to_write_bridge_status_file = 0;
01145   static time_t time_to_downrate_stability = 0;
01146   static time_t time_to_save_stability = 0;
01147   static time_t time_to_clean_caches = 0;
01148   static time_t time_to_recheck_bandwidth = 0;
01149   static time_t time_to_check_for_expired_networkstatus = 0;
01150   static time_t time_to_write_stats_files = 0;
01151   static time_t time_to_write_bridge_stats = 0;
01152   static time_t time_to_check_port_forwarding = 0;
01153   static time_t time_to_launch_reachability_tests = 0;
01154   static int should_init_bridge_stats = 1;
01155   static time_t time_to_retry_dns_init = 0;
01156   static time_t time_to_next_heartbeat = 0;
01157   const or_options_t *options = get_options();
01158 
01159   int is_server = server_mode(options);
01160   int i;
01161   int have_dir_info;
01162 
01167   consider_hibernation(now);
01168 
01169 #if 0
01170   {
01171     static time_t nl_check_time = 0;
01172     if (nl_check_time <= now) {
01173       nodelist_assert_ok();
01174       nl_check_time = now + 30;
01175     }
01176   }
01177 #endif
01178 
01179   /* 0b. If we've deferred a signewnym, make sure it gets handled
01180    * eventually. */
01181   if (signewnym_is_pending &&
01182       time_of_last_signewnym + MAX_SIGNEWNYM_RATE <= now) {
01183     log(LOG_INFO, LD_CONTROL, "Honoring delayed NEWNYM request");
01184     signewnym_impl(now);
01185   }
01186 
01187   /* 0c. If we've deferred log messages for the controller, handle them now */
01188   flush_pending_log_callbacks();
01189 
01194   if (is_server &&
01195       get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
01196     log_info(LD_GENERAL,"Rotating onion key.");
01197     rotate_onion_key();
01198     cpuworkers_rotate();
01199     if (router_rebuild_descriptor(1)<0) {
01200       log_info(LD_CONFIG, "Couldn't rebuild router descriptor");
01201     }
01202     if (advertised_server_mode() & !options->DisableNetwork)
01203       router_upload_dir_desc_to_dirservers(0);
01204   }
01205 
01206   if (!options->DisableNetwork && time_to_try_getting_descriptors < now) {
01207     update_all_descriptor_downloads(now);
01208     update_extrainfo_downloads(now);
01209     if (router_have_minimum_dir_info())
01210       time_to_try_getting_descriptors = now + LAZY_DESCRIPTOR_RETRY_INTERVAL;
01211     else
01212       time_to_try_getting_descriptors = now + GREEDY_DESCRIPTOR_RETRY_INTERVAL;
01213   }
01214 
01215   if (time_to_reset_descriptor_failures < now) {
01216     router_reset_descriptor_download_failures();
01217     time_to_reset_descriptor_failures =
01218       now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
01219   }
01220 
01221   if (options->UseBridges)
01222     fetch_bridge_descriptors(options, now);
01223 
01226   if (!last_rotated_x509_certificate)
01227     last_rotated_x509_certificate = now;
01228   if (last_rotated_x509_certificate+MAX_SSL_KEY_LIFETIME_INTERNAL < now) {
01229     log_info(LD_GENERAL,"Rotating tls context.");
01230     if (router_initialize_tls_context() < 0) {
01231       log_warn(LD_BUG, "Error reinitializing TLS context");
01232       /* XXX is it a bug here, that we just keep going? -RD */
01233     }
01234     last_rotated_x509_certificate = now;
01235     /* We also make sure to rotate the TLS connections themselves if they've
01236      * been up for too long -- but that's done via is_bad_for_new_circs in
01237      * connection_run_housekeeping() above. */
01238   }
01239 
01240   if (time_to_add_entropy < now) {
01241     if (time_to_add_entropy) {
01242       /* We already seeded once, so don't die on failure. */
01243       crypto_seed_rng(0);
01244     }
01246 #define ENTROPY_INTERVAL (60*60)
01247     time_to_add_entropy = now + ENTROPY_INTERVAL;
01248   }
01249 
01252   if (accounting_is_enabled(options))
01253     accounting_run_housekeeping(now);
01254 
01255   if (time_to_launch_reachability_tests < now &&
01256       (authdir_mode_tests_reachability(options)) &&
01257        !net_is_disabled()) {
01258     time_to_launch_reachability_tests = now + REACHABILITY_TEST_INTERVAL;
01259     /* try to determine reachability of the other Tor relays */
01260     dirserv_test_reachability(now);
01261   }
01262 
01266   if (time_to_downrate_stability < now)
01267     time_to_downrate_stability = rep_hist_downrate_old_runs(now);
01268   if (authdir_mode_tests_reachability(options)) {
01269     if (time_to_save_stability < now) {
01270       if (time_to_save_stability && rep_hist_record_mtbf_data(now, 1)<0) {
01271         log_warn(LD_GENERAL, "Couldn't store mtbf data.");
01272       }
01273 #define SAVE_STABILITY_INTERVAL (30*60)
01274       time_to_save_stability = now + SAVE_STABILITY_INTERVAL;
01275     }
01276   }
01277 
01278   /* 1e. Periodically, if we're a v3 authority, we check whether our cert is
01279    * close to expiring and warn the admin if it is. */
01280   if (time_to_check_v3_certificate < now) {
01281     v3_authority_check_key_expiry();
01282 #define CHECK_V3_CERTIFICATE_INTERVAL (5*60)
01283     time_to_check_v3_certificate = now + CHECK_V3_CERTIFICATE_INTERVAL;
01284   }
01285 
01286   /* 1f. Check whether our networkstatus has expired.
01287    */
01288   if (time_to_check_for_expired_networkstatus < now) {
01289     networkstatus_t *ns = networkstatus_get_latest_consensus();
01290     /*XXXX RD: This value needs to be the same as REASONABLY_LIVE_TIME in
01291      * networkstatus_get_reasonably_live_consensus(), but that value is way
01292      * way too high.  Arma: is the bridge issue there resolved yet? -NM */
01293 #define NS_EXPIRY_SLOP (24*60*60)
01294     if (ns && ns->valid_until < now+NS_EXPIRY_SLOP &&
01295         router_have_minimum_dir_info()) {
01296       router_dir_info_changed();
01297     }
01298 #define CHECK_EXPIRED_NS_INTERVAL (2*60)
01299     time_to_check_for_expired_networkstatus = now + CHECK_EXPIRED_NS_INTERVAL;
01300   }
01301 
01302   /* 1g. Check whether we should write statistics to disk.
01303    */
01304   if (time_to_write_stats_files < now) {
01305 #define CHECK_WRITE_STATS_INTERVAL (60*60)
01306     time_t next_time_to_write_stats_files = (time_to_write_stats_files > 0 ?
01307            time_to_write_stats_files : now) + CHECK_WRITE_STATS_INTERVAL;
01308     if (options->CellStatistics) {
01309       time_t next_write =
01310           rep_hist_buffer_stats_write(time_to_write_stats_files);
01311       if (next_write && next_write < next_time_to_write_stats_files)
01312         next_time_to_write_stats_files = next_write;
01313     }
01314     if (options->DirReqStatistics) {
01315       time_t next_write = geoip_dirreq_stats_write(time_to_write_stats_files);
01316       if (next_write && next_write < next_time_to_write_stats_files)
01317         next_time_to_write_stats_files = next_write;
01318     }
01319     if (options->EntryStatistics) {
01320       time_t next_write = geoip_entry_stats_write(time_to_write_stats_files);
01321       if (next_write && next_write < next_time_to_write_stats_files)
01322         next_time_to_write_stats_files = next_write;
01323     }
01324     if (options->ExitPortStatistics) {
01325       time_t next_write = rep_hist_exit_stats_write(time_to_write_stats_files);
01326       if (next_write && next_write < next_time_to_write_stats_files)
01327         next_time_to_write_stats_files = next_write;
01328     }
01329     if (options->ConnDirectionStatistics) {
01330       time_t next_write = rep_hist_conn_stats_write(time_to_write_stats_files);
01331       if (next_write && next_write < next_time_to_write_stats_files)
01332         next_time_to_write_stats_files = next_write;
01333     }
01334     if (options->BridgeAuthoritativeDir) {
01335       time_t next_write = rep_hist_desc_stats_write(time_to_write_stats_files);
01336       if (next_write && next_write < next_time_to_write_stats_files)
01337         next_time_to_write_stats_files = next_write;
01338     }
01339     time_to_write_stats_files = next_time_to_write_stats_files;
01340   }
01341 
01342   /* 1h. Check whether we should write bridge statistics to disk.
01343    */
01344   if (should_record_bridge_info(options)) {
01345     if (time_to_write_bridge_stats < now) {
01346       if (should_init_bridge_stats) {
01347         /* (Re-)initialize bridge statistics. */
01348         geoip_bridge_stats_init(now);
01349         time_to_write_bridge_stats = now + WRITE_STATS_INTERVAL;
01350         should_init_bridge_stats = 0;
01351       } else {
01352         /* Possibly write bridge statistics to disk and ask when to write
01353          * them next time. */
01354         time_to_write_bridge_stats = geoip_bridge_stats_write(
01355                                            time_to_write_bridge_stats);
01356       }
01357     }
01358   } else if (!should_init_bridge_stats) {
01359     /* Bridge mode was turned off. Ensure that stats are re-initialized
01360      * next time bridge mode is turned on. */
01361     should_init_bridge_stats = 1;
01362   }
01363 
01364   /* Remove old information from rephist and the rend cache. */
01365   if (time_to_clean_caches < now) {
01366     rep_history_clean(now - options->RephistTrackTime);
01367     rend_cache_clean(now);
01368     rend_cache_clean_v2_descs_as_dir(now);
01369     microdesc_cache_rebuild(NULL, 0);
01370 #define CLEAN_CACHES_INTERVAL (30*60)
01371     time_to_clean_caches = now + CLEAN_CACHES_INTERVAL;
01372   }
01373 
01374 #define RETRY_DNS_INTERVAL (10*60)
01375   /* If we're a server and initializing dns failed, retry periodically. */
01376   if (time_to_retry_dns_init < now) {
01377     time_to_retry_dns_init = now + RETRY_DNS_INTERVAL;
01378     if (is_server && has_dns_init_failed())
01379       dns_init();
01380   }
01381 
01387 #define CHECK_DESCRIPTOR_INTERVAL (60)
01388 
01389 #define CHECK_IPADDRESS_INTERVAL (15*60)
01390 
01391   /* 2b. Once per minute, regenerate and upload the descriptor if the old
01392    * one is inaccurate. */
01393   if (time_to_check_descriptor < now && !options->DisableNetwork) {
01394     static int dirport_reachability_count = 0;
01395     time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
01396     check_descriptor_bandwidth_changed(now);
01397     if (time_to_check_ipaddress < now) {
01398       time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
01399       check_descriptor_ipaddress_changed(now);
01400     }
01401     mark_my_descriptor_dirty_if_too_old(now);
01402     consider_publishable_server(0);
01403     /* also, check religiously for reachability, if it's within the first
01404      * 20 minutes of our uptime. */
01405     if (is_server &&
01406         (can_complete_circuit || !any_predicted_circuits(now)) &&
01407         !we_are_hibernating()) {
01408       if (stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
01409         consider_testing_reachability(1, dirport_reachability_count==0);
01410         if (++dirport_reachability_count > 5)
01411           dirport_reachability_count = 0;
01412       } else if (time_to_recheck_bandwidth < now) {
01413         /* If we haven't checked for 12 hours and our bandwidth estimate is
01414          * low, do another bandwidth test. This is especially important for
01415          * bridges, since they might go long periods without much use. */
01416         const routerinfo_t *me = router_get_my_routerinfo();
01417         if (time_to_recheck_bandwidth && me &&
01418             me->bandwidthcapacity < me->bandwidthrate &&
01419             me->bandwidthcapacity < 51200) {
01420           reset_bandwidth_test();
01421         }
01422 #define BANDWIDTH_RECHECK_INTERVAL (12*60*60)
01423         time_to_recheck_bandwidth = now + BANDWIDTH_RECHECK_INTERVAL;
01424       }
01425     }
01426 
01427     /* If any networkstatus documents are no longer recent, we need to
01428      * update all the descriptors' running status. */
01429     /* purge obsolete entries */
01430     networkstatus_v2_list_clean(now);
01431     /* Remove dead routers. */
01432     routerlist_remove_old_routers();
01433 
01434     /* Also, once per minute, check whether we want to download any
01435      * networkstatus documents.
01436      */
01437     update_networkstatus_downloads(now);
01438   }
01439 
01441   if (authdir_mode_v3(options))
01442     dirvote_act(options, now);
01443 
01449   /* (If our circuit build timeout can ever become lower than a second (which
01450    * it can't, currently), we should do this more often.) */
01451   circuit_expire_building();
01452 
01458   connection_ap_expire_beginning();
01459 
01462   connection_expire_held_open();
01463 
01465   if (!net_is_disabled() && time_to_check_listeners < now) {
01466     retry_all_listeners(NULL, NULL, 0);
01467     time_to_check_listeners = now+60;
01468   }
01469 
01475   have_dir_info = router_have_minimum_dir_info();
01476   if (have_dir_info && !net_is_disabled())
01477     circuit_build_needed_circs(now);
01478 
01479   /* every 10 seconds, but not at the same second as other such events */
01480   if (now % 10 == 5)
01481     circuit_expire_old_circuits_serverside(now);
01482 
01484   connection_or_set_bad_connections(NULL, 0);
01485   for (i=0;i<smartlist_len(connection_array);i++) {
01486     run_connection_housekeeping(i, now);
01487   }
01488   if (time_to_shrink_memory < now) {
01489     SMARTLIST_FOREACH(connection_array, connection_t *, conn, {
01490         if (conn->outbuf)
01491           buf_shrink(conn->outbuf);
01492         if (conn->inbuf)
01493           buf_shrink(conn->inbuf);
01494       });
01495     clean_cell_pool();
01496     buf_shrink_freelists(0);
01499 #define MEM_SHRINK_INTERVAL (60)
01500     time_to_shrink_memory = now + MEM_SHRINK_INTERVAL;
01501   }
01502 
01504   circuit_close_all_marked();
01505 
01507   if (can_complete_circuit && !net_is_disabled()) {
01508     rend_consider_services_upload(now);
01509     rend_consider_descriptor_republication();
01510   }
01511 
01516   close_closeable_connections();
01517 
01520   or_state_save(now);
01521 
01524   if (!net_is_disabled() &&
01525       public_server_mode(options) && time_to_check_for_correct_dns < now) {
01526     if (!time_to_check_for_correct_dns) {
01527       time_to_check_for_correct_dns = now + 60 + crypto_rand_int(120);
01528     } else {
01529       dns_launch_correctness_checks();
01530       time_to_check_for_correct_dns = now + 12*3600 +
01531         crypto_rand_int(12*3600);
01532     }
01533   }
01534 
01536   if (options->BridgeAuthoritativeDir &&
01537       time_to_write_bridge_status_file < now) {
01538     networkstatus_dump_bridge_status_to_file(now);
01539 #define BRIDGE_STATUSFILE_INTERVAL (30*60)
01540     time_to_write_bridge_status_file = now+BRIDGE_STATUSFILE_INTERVAL;
01541   }
01542 
01544   if (!net_is_disabled() &&
01545       time_to_check_port_forwarding < now &&
01546       options->PortForwarding &&
01547       is_server) {
01548 #define PORT_FORWARDING_CHECK_INTERVAL 5
01549     /* XXXXX this should take a list of ports, not just two! */
01550     tor_check_port_forwarding(options->PortForwardingHelper,
01551                               get_primary_dir_port(),
01552                               get_primary_or_port(),
01553                               now);
01554     time_to_check_port_forwarding = now+PORT_FORWARDING_CHECK_INTERVAL;
01555   }
01556 
01558   if (!net_is_disabled() && pt_proxies_configuration_pending())
01559     pt_configure_remaining_proxies();
01560 
01562   if (options->HeartbeatPeriod &&
01563       time_to_next_heartbeat <= now) {
01564     log_heartbeat(now);
01565     time_to_next_heartbeat = now+options->HeartbeatPeriod;
01566   }
01567 }
01568 
01570 static periodic_timer_t *second_timer = NULL;
01572 static int n_libevent_errors = 0;
01573 
01575 static void
01576 second_elapsed_callback(periodic_timer_t *timer, void *arg)
01577 {
01578   /* XXXX This could be sensibly refactored into multiple callbacks, and we
01579    * could use Libevent's timers for this rather than checking the current
01580    * time against a bunch of timeouts every second. */
01581   static time_t current_second = 0;
01582   time_t now;
01583   size_t bytes_written;
01584   size_t bytes_read;
01585   int seconds_elapsed;
01586   const or_options_t *options = get_options();
01587   (void)timer;
01588   (void)arg;
01589 
01590   n_libevent_errors = 0;
01591 
01592   /* log_notice(LD_GENERAL, "Tick."); */
01593   now = time(NULL);
01594   update_approx_time(now);
01595 
01596   /* the second has rolled over. check more stuff. */
01597   seconds_elapsed = current_second ? (int)(now - current_second) : 0;
01598 #ifdef USE_BUFFEREVENTS
01599   {
01600     uint64_t cur_read,cur_written;
01601     connection_get_rate_limit_totals(&cur_read, &cur_written);
01602     bytes_written = (size_t)(cur_written - stats_prev_n_written);
01603     bytes_read = (size_t)(cur_read - stats_prev_n_read);
01604     stats_n_bytes_read += bytes_read;
01605     stats_n_bytes_written += bytes_written;
01606     if (accounting_is_enabled(options) && seconds_elapsed >= 0)
01607       accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
01608     stats_prev_n_written = cur_written;
01609     stats_prev_n_read = cur_read;
01610   }
01611 #else
01612   bytes_read = (size_t)(stats_n_bytes_read - stats_prev_n_read);
01613   bytes_written = (size_t)(stats_n_bytes_written - stats_prev_n_written);
01614   stats_prev_n_read = stats_n_bytes_read;
01615   stats_prev_n_written = stats_n_bytes_written;
01616 #endif
01617 
01618   control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
01619   control_event_stream_bandwidth_used();
01620 
01621   if (server_mode(options) &&
01622       !net_is_disabled() &&
01623       seconds_elapsed > 0 &&
01624       can_complete_circuit &&
01625       stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
01626       (stats_n_seconds_working+seconds_elapsed) /
01627         TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
01628     /* every 20 minutes, check and complain if necessary */
01629     const routerinfo_t *me = router_get_my_routerinfo();
01630     if (me && !check_whether_orport_reachable()) {
01631       log_warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
01632                "its ORPort is reachable. Please check your firewalls, ports, "
01633                "address, /etc/hosts file, etc.",
01634                me->address, me->or_port);
01635       control_event_server_status(LOG_WARN,
01636                                   "REACHABILITY_FAILED ORADDRESS=%s:%d",
01637                                   me->address, me->or_port);
01638     }
01639 
01640     if (me && !check_whether_dirport_reachable()) {
01641       log_warn(LD_CONFIG,
01642                "Your server (%s:%d) has not managed to confirm that its "
01643                "DirPort is reachable. Please check your firewalls, ports, "
01644                "address, /etc/hosts file, etc.",
01645                me->address, me->dir_port);
01646       control_event_server_status(LOG_WARN,
01647                                   "REACHABILITY_FAILED DIRADDRESS=%s:%d",
01648                                   me->address, me->dir_port);
01649     }
01650   }
01651 
01654 #define NUM_JUMPED_SECONDS_BEFORE_WARN 100
01655   if (seconds_elapsed < -NUM_JUMPED_SECONDS_BEFORE_WARN ||
01656       seconds_elapsed >= NUM_JUMPED_SECONDS_BEFORE_WARN) {
01657     circuit_note_clock_jumped(seconds_elapsed);
01658     /* XXX if the time jumps *back* many months, do our events in
01659      * run_scheduled_events() recover? I don't think they do. -RD */
01660   } else if (seconds_elapsed > 0)
01661     stats_n_seconds_working += seconds_elapsed;
01662 
01663   run_scheduled_events(now);
01664 
01665   current_second = now; /* remember which second it is, for next time */
01666 }
01667 
01668 #ifndef USE_BUFFEREVENTS
01669 
01670 static periodic_timer_t *refill_timer = NULL;
01671 
01674 static void
01675 refill_callback(periodic_timer_t *timer, void *arg)
01676 {
01677   static struct timeval current_millisecond;
01678   struct timeval now;
01679 
01680   size_t bytes_written;
01681   size_t bytes_read;
01682   int milliseconds_elapsed = 0;
01683   int seconds_rolled_over = 0;
01684 
01685   const or_options_t *options = get_options();
01686 
01687   (void)timer;
01688   (void)arg;
01689 
01690   tor_gettimeofday(&now);
01691 
01692   /* If this is our first time, no time has passed. */
01693   if (current_millisecond.tv_sec) {
01694     long mdiff = tv_mdiff(&current_millisecond, &now);
01695     if (mdiff > INT_MAX)
01696       mdiff = INT_MAX;
01697     milliseconds_elapsed = (int)mdiff;
01698     seconds_rolled_over = (int)(now.tv_sec - current_millisecond.tv_sec);
01699   }
01700 
01701   bytes_written = stats_prev_global_write_bucket - global_write_bucket;
01702   bytes_read = stats_prev_global_read_bucket - global_read_bucket;
01703 
01704   stats_n_bytes_read += bytes_read;
01705   stats_n_bytes_written += bytes_written;
01706   if (accounting_is_enabled(options) && milliseconds_elapsed >= 0)
01707     accounting_add_bytes(bytes_read, bytes_written, seconds_rolled_over);
01708 
01709   if (milliseconds_elapsed > 0)
01710     connection_bucket_refill(milliseconds_elapsed, now.tv_sec);
01711 
01712   stats_prev_global_read_bucket = global_read_bucket;
01713   stats_prev_global_write_bucket = global_write_bucket;
01714 
01715   current_millisecond = now; /* remember what time it is, for next time */
01716 }
01717 #endif
01718 
01719 #ifndef _WIN32
01720 
01723 static int
01724 got_libevent_error(void)
01725 {
01726   if (++n_libevent_errors > 8) {
01727     log_err(LD_NET, "Too many libevent errors in one second; dying");
01728     return -1;
01729   }
01730   return 0;
01731 }
01732 #endif
01733 
01734 #define UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST (6*60*60)
01735 
01739 void
01740 ip_address_changed(int at_interface)
01741 {
01742   int server = server_mode(get_options());
01743 
01744   if (at_interface) {
01745     if (! server) {
01746       /* Okay, change our keys. */
01747       if (init_keys()<0)
01748         log_warn(LD_GENERAL, "Unable to rotate keys after IP change!");
01749     }
01750   } else {
01751     if (server) {
01752       if (stats_n_seconds_working > UPTIME_CUTOFF_FOR_NEW_BANDWIDTH_TEST)
01753         reset_bandwidth_test();
01754       stats_n_seconds_working = 0;
01755       router_reset_reachability();
01756       mark_my_descriptor_dirty("IP address changed");
01757     }
01758   }
01759 
01760   dns_servers_relaunch_checks();
01761 }
01762 
01765 void
01766 dns_servers_relaunch_checks(void)
01767 {
01768   if (server_mode(get_options())) {
01769     dns_reset_correctness_checks();
01770     time_to_check_for_correct_dns = 0;
01771   }
01772 }
01773 
01776 static int
01777 do_hup(void)
01778 {
01779   const or_options_t *options = get_options();
01780 
01781 #ifdef USE_DMALLOC
01782   dmalloc_log_stats();
01783   dmalloc_log_changed(0, 1, 0, 0);
01784 #endif
01785 
01786   log_notice(LD_GENERAL,"Received reload signal (hup). Reloading config and "
01787              "resetting internal state.");
01788   if (accounting_is_enabled(options))
01789     accounting_record_bandwidth_usage(time(NULL), get_or_state());
01790 
01791   router_reset_warnings();
01792   routerlist_reset_warnings();
01793   /* first, reload config variables, in case they've changed */
01794   if (options->ReloadTorrcOnSIGHUP) {
01795     /* no need to provide argc/v, they've been cached in init_from_config */
01796     if (options_init_from_torrc(0, NULL) < 0) {
01797       log_err(LD_CONFIG,"Reading config failed--see warnings above. "
01798               "For usage, try -h.");
01799       return -1;
01800     }
01801     options = get_options(); /* they have changed now */
01802   } else {
01803     char *msg = NULL;
01804     log_notice(LD_GENERAL, "Not reloading config file: the controller told "
01805                "us not to.");
01806     /* Make stuff get rescanned, reloaded, etc. */
01807     if (set_options((or_options_t*)options, &msg) < 0) {
01808       if (!msg)
01809         msg = tor_strdup("Unknown error");
01810       log_warn(LD_GENERAL, "Unable to re-set previous options: %s", msg);
01811       tor_free(msg);
01812     }
01813   }
01814   if (authdir_mode_handles_descs(options, -1)) {
01815     /* reload the approved-routers file */
01816     if (dirserv_load_fingerprint_file() < 0) {
01817       /* warnings are logged from dirserv_load_fingerprint_file() directly */
01818       log_info(LD_GENERAL, "Error reloading fingerprints. "
01819                "Continuing with old list.");
01820     }
01821   }
01822 
01823   /* Rotate away from the old dirty circuits. This has to be done
01824    * after we've read the new options, but before we start using
01825    * circuits for directory fetches. */
01826   circuit_expire_all_dirty_circs();
01827 
01828   /* retry appropriate downloads */
01829   router_reset_status_download_failures();
01830   router_reset_descriptor_download_failures();
01831   if (!options->DisableNetwork)
01832     update_networkstatus_downloads(time(NULL));
01833 
01834   /* We'll retry routerstatus downloads in about 10 seconds; no need to
01835    * force a retry there. */
01836 
01837   if (server_mode(options)) {
01838     /* Restart cpuworker and dnsworker processes, so they get up-to-date
01839      * configuration options. */
01840     cpuworkers_rotate();
01841     dns_reset();
01842   }
01843   return 0;
01844 }
01845 
01847 /* static */ int
01848 do_main_loop(void)
01849 {
01850   int loop_result;
01851   time_t now;
01852 
01853   /* initialize dns resolve map, spawn workers if needed */
01854   if (dns_init() < 0) {
01855     if (get_options()->ServerDNSAllowBrokenConfig)
01856       log_warn(LD_GENERAL, "Couldn't set up any working nameservers. "
01857                "Network not up yet?  Will try again soon.");
01858     else {
01859       log_err(LD_GENERAL,"Error initializing dns subsystem; exiting.  To "
01860               "retry instead, set the ServerDNSAllowBrokenResolvConf option.");
01861     }
01862   }
01863 
01864   handle_signals(1);
01865 
01866   /* load the private keys, if we're supposed to have them, and set up the
01867    * TLS context. */
01868   if (! client_identity_key_is_set()) {
01869     if (init_keys() < 0) {
01870       log_err(LD_BUG,"Error initializing keys; exiting");
01871       return -1;
01872     }
01873   }
01874 
01875   /* Set up the packed_cell_t memory pool. */
01876   init_cell_pool();
01877 
01878   /* Set up our buckets */
01879   connection_bucket_init();
01880 #ifndef USE_BUFFEREVENTS
01881   stats_prev_global_read_bucket = global_read_bucket;
01882   stats_prev_global_write_bucket = global_write_bucket;
01883 #endif
01884 
01885   /* initialize the bootstrap status events to know we're starting up */
01886   control_event_bootstrap(BOOTSTRAP_STATUS_STARTING, 0);
01887 
01888   if (trusted_dirs_reload_certs()) {
01889     log_warn(LD_DIR,
01890              "Couldn't load all cached v3 certificates. Starting anyway.");
01891   }
01892   if (router_reload_v2_networkstatus()) {
01893     return -1;
01894   }
01895   if (router_reload_consensus_networkstatus()) {
01896     return -1;
01897   }
01898   /* load the routers file, or assign the defaults. */
01899   if (router_reload_router_list()) {
01900     return -1;
01901   }
01902   /* load the networkstatuses. (This launches a download for new routers as
01903    * appropriate.)
01904    */
01905   now = time(NULL);
01906   directory_info_has_arrived(now, 1);
01907 
01908   if (server_mode(get_options())) {
01909     /* launch cpuworkers. Need to do this *after* we've read the onion key. */
01910     cpu_init();
01911   }
01912 
01913   /* set up once-a-second callback. */
01914   if (! second_timer) {
01915     struct timeval one_second;
01916     one_second.tv_sec = 1;
01917     one_second.tv_usec = 0;
01918 
01919     second_timer = periodic_timer_new(tor_libevent_get_base(),
01920                                       &one_second,
01921                                       second_elapsed_callback,
01922                                       NULL);
01923     tor_assert(second_timer);
01924   }
01925 
01926 #ifndef USE_BUFFEREVENTS
01927   if (!refill_timer) {
01928     struct timeval refill_interval;
01929     int msecs = get_options()->TokenBucketRefillInterval;
01930 
01931     refill_interval.tv_sec =  msecs/1000;
01932     refill_interval.tv_usec = (msecs%1000)*1000;
01933 
01934     refill_timer = periodic_timer_new(tor_libevent_get_base(),
01935                                       &refill_interval,
01936                                       refill_callback,
01937                                       NULL);
01938     tor_assert(refill_timer);
01939   }
01940 #endif
01941 
01942   for (;;) {
01943     if (nt_service_is_stopping())
01944       return 0;
01945 
01946 #ifndef _WIN32
01947     /* Make it easier to tell whether libevent failure is our fault or not. */
01948     errno = 0;
01949 #endif
01950     /* All active linked conns should get their read events activated. */
01951     SMARTLIST_FOREACH(active_linked_connection_lst, connection_t *, conn,
01952                       event_active(conn->read_event, EV_READ, 1));
01953     called_loop_once = smartlist_len(active_linked_connection_lst) ? 1 : 0;
01954 
01955     update_approx_time(time(NULL));
01956 
01957     /* poll until we have an event, or the second ends, or until we have
01958      * some active linked connections to trigger events for. */
01959     loop_result = event_base_loop(tor_libevent_get_base(),
01960                                   called_loop_once ? EVLOOP_ONCE : 0);
01961 
01962     /* let catch() handle things like ^c, and otherwise don't worry about it */
01963     if (loop_result < 0) {
01964       int e = tor_socket_errno(-1);
01965       /* let the program survive things like ^z */
01966       if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
01967         log_err(LD_NET,"libevent call with %s failed: %s [%d]",
01968                 tor_libevent_get_method(), tor_socket_strerror(e), e);
01969         return -1;
01970 #ifndef _WIN32
01971       } else if (e == EINVAL) {
01972         log_warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
01973         if (got_libevent_error())
01974           return -1;
01975 #endif
01976       } else {
01977         if (ERRNO_IS_EINPROGRESS(e))
01978           log_warn(LD_BUG,
01979                    "libevent call returned EINPROGRESS? Please report.");
01980         log_debug(LD_NET,"libevent call interrupted.");
01981         /* You can't trust the results of this poll(). Go back to the
01982          * top of the big for loop. */
01983         continue;
01984       }
01985     }
01986   }
01987 }
01988 
01989 #ifndef _WIN32 /* Only called when we're willing to use signals */
01990 
01992 static void
01993 signal_callback(int fd, short events, void *arg)
01994 {
01995   uintptr_t sig = (uintptr_t)arg;
01996   (void)fd;
01997   (void)events;
01998 
01999   process_signal(sig);
02000 }
02001 #endif
02002 
02004 void
02005 process_signal(uintptr_t sig)
02006 {
02007   switch (sig)
02008     {
02009     case SIGTERM:
02010       log_notice(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
02011       tor_cleanup();
02012       exit(0);
02013       break;
02014     case SIGINT:
02015       if (!server_mode(get_options())) { /* do it now */
02016         log_notice(LD_GENERAL,"Interrupt: exiting cleanly.");
02017         tor_cleanup();
02018         exit(0);
02019       }
02020       hibernate_begin_shutdown();
02021       break;
02022 #ifdef SIGPIPE
02023     case SIGPIPE:
02024       log_debug(LD_GENERAL,"Caught SIGPIPE. Ignoring.");
02025       break;
02026 #endif
02027     case SIGUSR1:
02028       /* prefer to log it at INFO, but make sure we always see it */
02029       dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
02030       control_event_signal(sig);
02031       break;
02032     case SIGUSR2:
02033       switch_logs_debug();
02034       log_debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
02035                 "Send HUP to change back.");
02036       control_event_signal(sig);
02037       break;
02038     case SIGHUP:
02039       if (do_hup() < 0) {
02040         log_warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
02041         tor_cleanup();
02042         exit(1);
02043       }
02044       control_event_signal(sig);
02045       break;
02046 #ifdef SIGCHLD
02047     case SIGCHLD:
02048       while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
02049                                                 zombies */
02050       break;
02051 #endif
02052     case SIGNEWNYM: {
02053       time_t now = time(NULL);
02054       if (time_of_last_signewnym + MAX_SIGNEWNYM_RATE > now) {
02055         signewnym_is_pending = 1;
02056         log(LOG_NOTICE, LD_CONTROL,
02057             "Rate limiting NEWNYM request: delaying by %d second(s)",
02058             (int)(MAX_SIGNEWNYM_RATE+time_of_last_signewnym-now));
02059       } else {
02060         signewnym_impl(now);
02061       }
02062       break;
02063     }
02064     case SIGCLEARDNSCACHE:
02065       addressmap_clear_transient();
02066       control_event_signal(sig);
02067       break;
02068   }
02069 }
02070 
02072 long
02073 get_uptime(void)
02074 {
02075   return stats_n_seconds_working;
02076 }
02077 
02078 extern uint64_t rephist_total_alloc;
02079 extern uint32_t rephist_total_num;
02080 
02084 static void
02085 dumpmemusage(int severity)
02086 {
02087   connection_dump_buffer_mem_stats(severity);
02088   log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
02089       U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
02090   dump_routerlist_mem_usage(severity);
02091   dump_cell_pool_usage(severity);
02092   dump_dns_mem_usage(severity);
02093   buf_dump_freelist_sizes(severity);
02094   tor_log_mallinfo(severity);
02095 }
02096 
02099 static void
02100 dumpstats(int severity)
02101 {
02102   time_t now = time(NULL);
02103   time_t elapsed;
02104   size_t rbuf_cap, wbuf_cap, rbuf_len, wbuf_len;
02105 
02106   log(severity, LD_GENERAL, "Dumping stats:");
02107 
02108   SMARTLIST_FOREACH(connection_array, connection_t *, conn,
02109   {
02110     int i = conn_sl_idx;
02111     log(severity, LD_GENERAL,
02112         "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
02113         i, (int)conn->s, conn->type, conn_type_to_string(conn->type),
02114         conn->state, conn_state_to_string(conn->type, conn->state),
02115         (int)(now - conn->timestamp_created));
02116     if (!connection_is_listener(conn)) {
02117       log(severity,LD_GENERAL,
02118           "Conn %d is to %s:%d.", i,
02119           safe_str_client(conn->address),
02120           conn->port);
02121       log(severity,LD_GENERAL,
02122           "Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
02123           i,
02124           (int)connection_get_inbuf_len(conn),
02125           (int)buf_allocation(conn->inbuf),
02126           (int)(now - conn->timestamp_lastread));
02127       log(severity,LD_GENERAL,
02128           "Conn %d: %d bytes waiting on outbuf "
02129           "(len %d, last written %d secs ago)",i,
02130           (int)connection_get_outbuf_len(conn),
02131           (int)buf_allocation(conn->outbuf),
02132           (int)(now - conn->timestamp_lastwritten));
02133       if (conn->type == CONN_TYPE_OR) {
02134         or_connection_t *or_conn = TO_OR_CONN(conn);
02135         if (or_conn->tls) {
02136           tor_tls_get_buffer_sizes(or_conn->tls, &rbuf_cap, &rbuf_len,
02137                                    &wbuf_cap, &wbuf_len);
02138           log(severity, LD_GENERAL,
02139               "Conn %d: %d/%d bytes used on OpenSSL read buffer; "
02140               "%d/%d bytes used on write buffer.",
02141               i, (int)rbuf_len, (int)rbuf_cap, (int)wbuf_len, (int)wbuf_cap);
02142         }
02143       }
02144     }
02145     circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
02146                                            * using this conn */
02147   });
02148   log(severity, LD_NET,
02149       "Cells processed: "U64_FORMAT" padding\n"
02150       "                 "U64_FORMAT" create\n"
02151       "                 "U64_FORMAT" created\n"
02152       "                 "U64_FORMAT" relay\n"
02153       "                        ("U64_FORMAT" relayed)\n"
02154       "                        ("U64_FORMAT" delivered)\n"
02155       "                 "U64_FORMAT" destroy",
02156       U64_PRINTF_ARG(stats_n_padding_cells_processed),
02157       U64_PRINTF_ARG(stats_n_create_cells_processed),
02158       U64_PRINTF_ARG(stats_n_created_cells_processed),
02159       U64_PRINTF_ARG(stats_n_relay_cells_processed),
02160       U64_PRINTF_ARG(stats_n_relay_cells_relayed),
02161       U64_PRINTF_ARG(stats_n_relay_cells_delivered),
02162       U64_PRINTF_ARG(stats_n_destroy_cells_processed));
02163   if (stats_n_data_cells_packaged)
02164     log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
02165         100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
02166              U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
02167   if (stats_n_data_cells_received)
02168     log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
02169         100*(U64_TO_DBL(stats_n_data_bytes_received) /
02170              U64_TO_DBL(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
02171 
02172   if (now - time_of_process_start >= 0)
02173     elapsed = now - time_of_process_start;
02174   else
02175     elapsed = 0;
02176 
02177   if (elapsed) {
02178     log(severity, LD_NET,
02179         "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
02180         U64_PRINTF_ARG(stats_n_bytes_read),
02181         (int)elapsed,
02182         (int) (stats_n_bytes_read/elapsed));
02183     log(severity, LD_NET,
02184         "Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
02185         U64_PRINTF_ARG(stats_n_bytes_written),
02186         (int)elapsed,
02187         (int) (stats_n_bytes_written/elapsed));
02188   }
02189 
02190   log(severity, LD_NET, "--------------- Dumping memory information:");
02191   dumpmemusage(severity);
02192 
02193   rep_hist_dump_stats(now,severity);
02194   rend_service_dump_stats(severity);
02195   dump_pk_ops(severity);
02196   dump_distinct_digest_count(severity);
02197 }
02198 
02201 static void
02202 exit_function(void)
02203 {
02204   /* NOTE: If we ever daemonize, this gets called immediately.  That's
02205    * okay for now, because we only use this on Windows.  */
02206 #ifdef _WIN32
02207   WSACleanup();
02208 #endif
02209 }
02210 
02212 void
02213 handle_signals(int is_parent)
02214 {
02215 #ifndef _WIN32 /* do signal stuff only on Unix */
02216   int i;
02217   static const int signals[] = {
02218     SIGINT,  /* do a controlled slow shutdown */
02219     SIGTERM, /* to terminate now */
02220     SIGPIPE, /* otherwise SIGPIPE kills us */
02221     SIGUSR1, /* dump stats */
02222     SIGUSR2, /* go to loglevel debug */
02223     SIGHUP,  /* to reload config, retry conns, etc */
02224 #ifdef SIGXFSZ
02225     SIGXFSZ, /* handle file-too-big resource exhaustion */
02226 #endif
02227     SIGCHLD, /* handle dns/cpu workers that exit */
02228     -1 };
02229   static struct event *signal_events[16]; /* bigger than it has to be. */
02230   if (is_parent) {
02231     for (i = 0; signals[i] >= 0; ++i) {
02232       signal_events[i] = tor_evsignal_new(
02233                        tor_libevent_get_base(), signals[i], signal_callback,
02234                        (void*)(uintptr_t)signals[i]);
02235       if (event_add(signal_events[i], NULL))
02236         log_warn(LD_BUG, "Error from libevent when adding event for signal %d",
02237                  signals[i]);
02238     }
02239   } else {
02240     struct sigaction action;
02241     action.sa_flags = 0;
02242     sigemptyset(&action.sa_mask);
02243     action.sa_handler = SIG_IGN;
02244     sigaction(SIGINT,  &action, NULL);
02245     sigaction(SIGTERM, &action, NULL);
02246     sigaction(SIGPIPE, &action, NULL);
02247     sigaction(SIGUSR1, &action, NULL);
02248     sigaction(SIGUSR2, &action, NULL);
02249     sigaction(SIGHUP,  &action, NULL);
02250 #ifdef SIGXFSZ
02251     sigaction(SIGXFSZ, &action, NULL);
02252 #endif
02253   }
02254 #else /* MS windows */
02255   (void)is_parent;
02256 #endif /* signal stuff */
02257 }
02258 
02261 /* static */ int
02262 tor_init(int argc, char *argv[])
02263 {
02264   char buf[256];
02265   int i, quiet = 0;
02266   time_of_process_start = time(NULL);
02267   if (!connection_array)
02268     connection_array = smartlist_new();
02269   if (!closeable_connection_lst)
02270     closeable_connection_lst = smartlist_new();
02271   if (!active_linked_connection_lst)
02272     active_linked_connection_lst = smartlist_new();
02273   /* Have the log set up with our application name. */
02274   tor_snprintf(buf, sizeof(buf), "Tor %s", get_version());
02275   log_set_application_name(buf);
02276   /* Initialize the history structures. */
02277   rep_hist_init();
02278   /* Initialize the service cache. */
02279   rend_cache_init();
02280   addressmap_init(); /* Init the client dns cache. Do it always, since it's
02281                       * cheap. */
02282 
02283   /* We search for the "quiet" option first, since it decides whether we
02284    * will log anything at all to the command line. */
02285   for (i=1;i<argc;++i) {
02286     if (!strcmp(argv[i], "--hush"))
02287       quiet = 1;
02288     if (!strcmp(argv[i], "--quiet"))
02289       quiet = 2;
02290   }
02291  /* give it somewhere to log to initially */
02292   switch (quiet) {
02293     case 2:
02294       /* no initial logging */
02295       break;
02296     case 1:
02297       add_temp_log(LOG_WARN);
02298       break;
02299     default:
02300       add_temp_log(LOG_NOTICE);
02301   }
02302   quiet_level = quiet;
02303 
02304   {
02305     const char *version = get_version();
02306 #ifdef USE_BUFFEREVENTS
02307     log_notice(LD_GENERAL, "Tor v%s (with bufferevents) running on %s.",
02308                 version, get_uname());
02309 #else
02310     log_notice(LD_GENERAL, "Tor v%s running on %s.", version, get_uname());
02311 #endif
02312 
02313     log_notice(LD_GENERAL, "Tor can't help you if you use it wrong! "
02314                "Learn how to be safe at "
02315                "https://www.torproject.org/download/download#warning");
02316 
02317     if (strstr(version, "alpha") || strstr(version, "beta"))
02318       log_notice(LD_GENERAL, "This version is not a stable Tor release. "
02319                  "Expect more bugs than usual.");
02320   }
02321 
02322 #ifdef NON_ANONYMOUS_MODE_ENABLED
02323   log(LOG_WARN, LD_GENERAL, "This copy of Tor was compiled to run in a "
02324       "non-anonymous mode. It will provide NO ANONYMITY.");
02325 #endif
02326 
02327   if (network_init()<0) {
02328     log_err(LD_BUG,"Error initializing network; exiting.");
02329     return -1;
02330   }
02331   atexit(exit_function);
02332 
02333   if (options_init_from_torrc(argc,argv) < 0) {
02334     log_err(LD_CONFIG,"Reading config failed--see warnings above.");
02335     return -1;
02336   }
02337 
02338 #ifndef _WIN32
02339   if (geteuid()==0)
02340     log_warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
02341              "and you probably shouldn't.");
02342 #endif
02343 
02344   if (crypto_global_init(get_options()->HardwareAccel,
02345                          get_options()->AccelName,
02346                          get_options()->AccelDir)) {
02347     log_err(LD_BUG, "Unable to initialize OpenSSL. Exiting.");
02348     return -1;
02349   }
02350 
02351   return 0;
02352 }
02353 
02357 static tor_lockfile_t *lockfile = NULL;
02358 
02364 int
02365 try_locking(const or_options_t *options, int err_if_locked)
02366 {
02367   if (lockfile)
02368     return 0;
02369   else {
02370     char *fname = options_get_datadir_fname2_suffix(options, "lock",NULL,NULL);
02371     int already_locked = 0;
02372     tor_lockfile_t *lf = tor_lockfile_lock(fname, 0, &already_locked);
02373     tor_free(fname);
02374     if (!lf) {
02375       if (err_if_locked && already_locked) {
02376         int r;
02377         log_warn(LD_GENERAL, "It looks like another Tor process is running "
02378                  "with the same data directory.  Waiting 5 seconds to see "
02379                  "if it goes away.");
02380 #ifndef _WIN32
02381         sleep(5);
02382 #else
02383         Sleep(5000);
02384 #endif
02385         r = try_locking(options, 0);
02386         if (r<0) {
02387           log_err(LD_GENERAL, "No, it's still there.  Exiting.");
02388           exit(0);
02389         }
02390         return r;
02391       }
02392       return -1;
02393     }
02394     lockfile = lf;
02395     return 0;
02396   }
02397 }
02398 
02400 int
02401 have_lockfile(void)
02402 {
02403   return lockfile != NULL;
02404 }
02405 
02407 void
02408 release_lockfile(void)
02409 {
02410   if (lockfile) {
02411     tor_lockfile_unlock(lockfile);
02412     lockfile = NULL;
02413   }
02414 }
02415 
02424 void
02425 tor_free_all(int postfork)
02426 {
02427   if (!postfork) {
02428     evdns_shutdown(1);
02429   }
02430   geoip_free_all();
02431   dirvote_free_all();
02432   routerlist_free_all();
02433   networkstatus_free_all();
02434   addressmap_free_all();
02435   dirserv_free_all();
02436   rend_service_free_all();
02437   rend_cache_free_all();
02438   rend_service_authorization_free_all();
02439   rep_hist_free_all();
02440   dns_free_all();
02441   clear_pending_onions();
02442   circuit_free_all();
02443   entry_guards_free_all();
02444   pt_free_all();
02445   connection_free_all();
02446   buf_shrink_freelists(1);
02447   memarea_clear_freelist();
02448   nodelist_free_all();
02449   microdesc_free_all();
02450   if (!postfork) {
02451     config_free_all();
02452     router_free_all();
02453     policies_free_all();
02454   }
02455   free_cell_pool();
02456   if (!postfork) {
02457     tor_tls_free_all();
02458   }
02459   /* stuff in main.c */
02460 
02461   smartlist_free(connection_array);
02462   smartlist_free(closeable_connection_lst);
02463   smartlist_free(active_linked_connection_lst);
02464   periodic_timer_free(second_timer);
02465   if (!postfork) {
02466     release_lockfile();
02467   }
02468   /* Stuff in util.c and address.c*/
02469   if (!postfork) {
02470     escaped(NULL);
02471     esc_router_info(NULL);
02472     logs_free_all(); /* free log strings. do this last so logs keep working. */
02473   }
02474 }
02475 
02477 void
02478 tor_cleanup(void)
02479 {
02480   const or_options_t *options = get_options();
02481   if (options->command == CMD_RUN_TOR) {
02482     time_t now = time(NULL);
02483     /* Remove our pid file. We don't care if there was an error when we
02484      * unlink, nothing we could do about it anyways. */
02485     if (options->PidFile)
02486       unlink(options->PidFile);
02487     if (options->ControlPortWriteToFile)
02488       unlink(options->ControlPortWriteToFile);
02489     if (accounting_is_enabled(options))
02490       accounting_record_bandwidth_usage(now, get_or_state());
02491     or_state_mark_dirty(get_or_state(), 0); /* force an immediate save. */
02492     or_state_save(now);
02493     if (authdir_mode_tests_reachability(options))
02494       rep_hist_record_mtbf_data(now, 0);
02495   }
02496 #ifdef USE_DMALLOC
02497   dmalloc_log_stats();
02498 #endif
02499   tor_free_all(0); /* We could move tor_free_all back into the ifdef below
02500                       later, if it makes shutdown unacceptably slow.  But for
02501                       now, leave it here: it's helped us catch bugs in the
02502                       past. */
02503   crypto_global_cleanup();
02504 #ifdef USE_DMALLOC
02505   dmalloc_log_unfreed();
02506   dmalloc_shutdown();
02507 #endif
02508 }
02509 
02511 /* static */ int
02512 do_list_fingerprint(void)
02513 {
02514   char buf[FINGERPRINT_LEN+1];
02515   crypto_pk_t *k;
02516   const char *nickname = get_options()->Nickname;
02517   if (!server_mode(get_options())) {
02518     log_err(LD_GENERAL,
02519             "Clients don't have long-term identity keys. Exiting.\n");
02520     return -1;
02521   }
02522   tor_assert(nickname);
02523   if (init_keys() < 0) {
02524     log_err(LD_BUG,"Error initializing keys; can't display fingerprint");
02525     return -1;
02526   }
02527   if (!(k = get_server_identity_key())) {
02528     log_err(LD_GENERAL,"Error: missing identity key.");
02529     return -1;
02530   }
02531   if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
02532     log_err(LD_BUG, "Error computing fingerprint");
02533     return -1;
02534   }
02535   printf("%s %s\n", nickname, buf);
02536   return 0;
02537 }
02538 
02541 /* static */ void
02542 do_hash_password(void)
02543 {
02544 
02545   char output[256];
02546   char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
02547 
02548   crypto_rand(key, S2K_SPECIFIER_LEN-1);
02549   key[S2K_SPECIFIER_LEN-1] = (uint8_t)96; /* Hash 64 K of data. */
02550   secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
02551                 get_options()->command_arg, strlen(get_options()->command_arg),
02552                 key);
02553   base16_encode(output, sizeof(output), key, sizeof(key));
02554   printf("16:%s\n",output);
02555 }
02556 
02557 #if defined (WINCE)
02558 int
02559 find_flashcard_path(PWCHAR path, size_t size)
02560 {
02561   WIN32_FIND_DATA d = {0};
02562   HANDLE h = NULL;
02563 
02564   if (!path)
02565     return -1;
02566 
02567   h = FindFirstFlashCard(&d);
02568   if (h == INVALID_HANDLE_VALUE)
02569     return -1;
02570 
02571   if (wcslen(d.cFileName) == 0) {
02572     FindClose(h);
02573     return -1;
02574   }
02575 
02576   wcsncpy(path,d.cFileName,size);
02577   FindClose(h);
02578   return 0;
02579 }
02580 #endif
02581 
02583 /* This function is distinct from main() only so we can link main.c into
02584  * the unittest binary without conflicting with the unittests' main. */
02585 int
02586 tor_main(int argc, char *argv[])
02587 {
02588   int result = 0;
02589 #if defined (WINCE)
02590   WCHAR path [MAX_PATH] = {0};
02591   WCHAR fullpath [MAX_PATH] = {0};
02592   PWCHAR p = NULL;
02593   FILE* redir = NULL;
02594   FILE* redirdbg = NULL;
02595 
02596   // this is to facilitate debugging by opening
02597   // a file on a folder shared by the wm emulator.
02598   // if no flashcard (real or emulated) is present,
02599   // log files will be written in the root folder
02600   if (find_flashcard_path(path,MAX_PATH) == -1) {
02601     redir = _wfreopen( L"\\stdout.log", L"w", stdout );
02602     redirdbg = _wfreopen( L"\\stderr.log", L"w", stderr );
02603   } else {
02604     swprintf(fullpath,L"\\%s\\tor",path);
02605     CreateDirectory(fullpath,NULL);
02606 
02607     swprintf(fullpath,L"\\%s\\tor\\stdout.log",path);
02608     redir = _wfreopen( fullpath, L"w", stdout );
02609 
02610     swprintf(fullpath,L"\\%s\\tor\\stderr.log",path);
02611     redirdbg = _wfreopen( fullpath, L"w", stderr );
02612   }
02613 #endif
02614 
02615 #ifdef _WIN32
02616   /* Call SetProcessDEPPolicy to permanently enable DEP.
02617      The function will not resolve on earlier versions of Windows,
02618      and failure is not dangerous. */
02619   HMODULE hMod = GetModuleHandleA("Kernel32.dll");
02620   if (hMod) {
02621     typedef BOOL (WINAPI *PSETDEP)(DWORD);
02622     PSETDEP setdeppolicy = (PSETDEP)GetProcAddress(hMod,
02623                            "SetProcessDEPPolicy");
02624     if (setdeppolicy) setdeppolicy(1); /* PROCESS_DEP_ENABLE */
02625   }
02626 #endif
02627 
02628   update_approx_time(time(NULL));
02629   tor_threads_init();
02630   init_logging();
02631 #ifdef USE_DMALLOC
02632   {
02633     /* Instruct OpenSSL to use our internal wrappers for malloc,
02634        realloc and free. */
02635     int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc, _tor_free);
02636     tor_assert(r);
02637   }
02638 #endif
02639 #ifdef NT_SERVICE
02640   {
02641      int done = 0;
02642      result = nt_service_parse_options(argc, argv, &done);
02643      if (done) return result;
02644   }
02645 #endif
02646   if (tor_init(argc, argv)<0)
02647     return -1;
02648   switch (get_options()->command) {
02649   case CMD_RUN_TOR:
02650 #ifdef NT_SERVICE
02651     nt_service_set_state(SERVICE_RUNNING);
02652 #endif
02653     result = do_main_loop();
02654     break;
02655   case CMD_LIST_FINGERPRINT:
02656     result = do_list_fingerprint();
02657     break;
02658   case CMD_HASH_PASSWORD:
02659     do_hash_password();
02660     result = 0;
02661     break;
02662   case CMD_VERIFY_CONFIG:
02663     printf("Configuration was valid\n");
02664     result = 0;
02665     break;
02666   case CMD_RUN_UNITTESTS: /* only set by test.c */
02667   default:
02668     log_warn(LD_BUG,"Illegal command number %d: internal error.",
02669              get_options()->command);
02670     result = -1;
02671   }
02672   tor_cleanup();
02673   return result;
02674 }
02675