1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177 |
- #include "or.h"
- static void dumpstats(int severity);
- int global_read_bucket;
- int global_write_bucket;
- static int stats_prev_global_read_bucket;
- static int stats_prev_global_write_bucket;
- static uint64_t stats_n_bytes_read = 0;
- static uint64_t stats_n_bytes_written = 0;
- long stats_n_seconds_uptime = 0;
- static time_t time_to_fetch_directory = 0;
- static connection_t *connection_array[MAXCONNECTIONS] =
- { NULL };
- static struct pollfd poll_array[MAXCONNECTIONS];
- static int nfds=0;
- #ifndef MS_WINDOWS
- static int please_dumpstats=0;
- static int please_reset=0;
- static int please_reap_children=0;
- static int please_sigpipe=0;
- static int please_shutdown=0;
- #endif
- int has_fetched_directory=0;
- int has_completed_circuit=0;
- #ifdef MS_WINDOWS_SERVICE
- SERVICE_STATUS service_status;
- SERVICE_STATUS_HANDLE hStatus;
- #endif
- int connection_add(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->s >= 0);
- if(nfds >= get_options()->MaxConn-1) {
- log_fn(LOG_WARN,"failing because nfds is too high.");
- return -1;
- }
- tor_assert(conn->poll_index == -1);
- conn->poll_index = nfds;
- connection_array[nfds] = conn;
- poll_array[nfds].fd = conn->s;
-
- poll_array[nfds].events = 0;
- poll_array[nfds].revents = 0;
- nfds++;
- log_fn(LOG_INFO,"new conn type %s, socket %d, nfds %d.",
- CONN_TYPE_TO_STRING(conn->type), conn->s, nfds);
- return 0;
- }
- int connection_remove(connection_t *conn) {
- int current_index;
- tor_assert(conn);
- tor_assert(nfds>0);
- log_fn(LOG_INFO,"removing socket %d (type %s), nfds now %d",
- conn->s, CONN_TYPE_TO_STRING(conn->type), nfds-1);
- tor_assert(conn->poll_index >= 0);
- current_index = conn->poll_index;
- if(current_index == nfds-1) {
- nfds--;
- return 0;
- }
-
- nfds--;
- poll_array[current_index].fd = poll_array[nfds].fd;
- poll_array[current_index].events = poll_array[nfds].events;
- poll_array[current_index].revents = poll_array[nfds].revents;
- connection_array[current_index] = connection_array[nfds];
- connection_array[current_index]->poll_index = current_index;
- return 0;
- }
- int connection_in_array(connection_t *conn) {
- int i;
- for (i=0; i<nfds; ++i) {
- if (conn==connection_array[i])
- return 1;
- }
- return 0;
- }
- void get_connection_array(connection_t ***array, int *n) {
- *array = connection_array;
- *n = nfds;
- }
- void connection_watch_events(connection_t *conn, short events) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- tor_assert(conn->poll_index < nfds);
- poll_array[conn->poll_index].events = events;
- }
- int connection_is_reading(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- return poll_array[conn->poll_index].events & POLLIN;
- }
- void connection_stop_reading(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- tor_assert(conn->poll_index < nfds);
- log(LOG_DEBUG,"connection_stop_reading() called.");
- poll_array[conn->poll_index].events &= ~POLLIN;
- }
- void connection_start_reading(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- tor_assert(conn->poll_index < nfds);
- poll_array[conn->poll_index].events |= POLLIN;
- }
- int connection_is_writing(connection_t *conn) {
- return poll_array[conn->poll_index].events & POLLOUT;
- }
- void connection_stop_writing(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- tor_assert(conn->poll_index < nfds);
- poll_array[conn->poll_index].events &= ~POLLOUT;
- }
- void connection_start_writing(connection_t *conn) {
- tor_assert(conn);
- tor_assert(conn->poll_index >= 0);
- tor_assert(conn->poll_index < nfds);
- poll_array[conn->poll_index].events |= POLLOUT;
- }
- static void conn_read(int i) {
- connection_t *conn = connection_array[i];
- if (conn->marked_for_close)
- return;
-
- if(!(poll_array[i].revents & (POLLIN|POLLHUP|POLLERR)))
- if(!connection_is_reading(conn) ||
- !connection_has_pending_tls_data(conn))
- return;
- log_fn(LOG_DEBUG,"socket %d wants to read.",conn->s);
- assert_connection_ok(conn, time(NULL));
- assert_all_pending_dns_resolves_ok();
- if(
-
- #ifdef MS_WINDOWS
- (poll_array[i].revents & POLLERR) ||
- #endif
- connection_handle_read(conn) < 0) {
- if (!conn->marked_for_close) {
-
- log_fn(LOG_WARN,"Unhandled error on read for %s connection (fd %d); removing",
- CONN_TYPE_TO_STRING(conn->type), conn->s);
- connection_mark_for_close(conn);
- }
- }
- assert_connection_ok(conn, time(NULL));
- assert_all_pending_dns_resolves_ok();
- }
- static void conn_write(int i) {
- connection_t *conn;
- if(!(poll_array[i].revents & POLLOUT))
- return;
- conn = connection_array[i];
- log_fn(LOG_DEBUG,"socket %d wants to write.",conn->s);
- if (conn->marked_for_close)
- return;
- assert_connection_ok(conn, time(NULL));
- assert_all_pending_dns_resolves_ok();
- if (connection_handle_write(conn) < 0) {
- if (!conn->marked_for_close) {
-
- log_fn(LOG_WARN,"Unhandled error on write for %s connection (fd %d); removing",
- CONN_TYPE_TO_STRING(conn->type), conn->s);
- conn->has_sent_end = 1;
-
- connection_mark_for_close(conn);
- }
- }
- assert_connection_ok(conn, time(NULL));
- assert_all_pending_dns_resolves_ok();
- }
- static void conn_close_if_marked(int i) {
- connection_t *conn;
- int retval;
- conn = connection_array[i];
- assert_connection_ok(conn, time(NULL));
- assert_all_pending_dns_resolves_ok();
- if(!conn->marked_for_close)
- return;
- log_fn(LOG_INFO,"Cleaning up connection (fd %d).",conn->s);
- if(conn->s >= 0 && connection_wants_to_flush(conn)) {
-
- if(!conn->hold_open_until_flushed)
- log_fn(LOG_INFO,
- "Conn (addr %s, fd %d, type %s, state %d) marked, but wants to flush %d bytes. "
- "(Marked at %s:%d)",
- conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
- (int)conn->outbuf_flushlen, conn->marked_for_close_file, conn->marked_for_close);
- if(connection_speaks_cells(conn)) {
- if(conn->state == OR_CONN_STATE_OPEN) {
- retval = flush_buf_tls(conn->tls, conn->outbuf, &conn->outbuf_flushlen);
- } else
- retval = -1;
- } else {
- retval = flush_buf(conn->s, conn->outbuf, &conn->outbuf_flushlen);
- }
- if(retval >= 0 &&
- conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
- log_fn(LOG_INFO,"Holding conn (fd %d) open for more flushing.",conn->s);
-
- return;
- }
- if(connection_wants_to_flush(conn)) {
- log_fn(LOG_WARN,"Conn (addr %s, fd %d, type %s, state %d) still wants to flush. Losing %d bytes! (Marked at %s:%d)",
- conn->address, conn->s, CONN_TYPE_TO_STRING(conn->type), conn->state,
- (int)buf_datalen(conn->outbuf), conn->marked_for_close_file,
- conn->marked_for_close);
- }
- }
-
- circuit_about_to_close_connection(conn);
- connection_about_to_close_connection(conn);
- connection_remove(conn);
- if(conn->type == CONN_TYPE_EXIT) {
- assert_connection_edge_not_dns_pending(conn);
- }
- connection_free(conn);
- if(i<nfds) {
- conn_close_if_marked(i);
- }
- }
- void directory_has_arrived(time_t now) {
- or_options_t *options = get_options();
- log_fn(LOG_INFO, "A directory has arrived.");
- has_fetched_directory=1;
-
- if (!time_to_fetch_directory)
- time_to_fetch_directory = now + options->DirFetchPostPeriod;
- if (server_mode(options) &&
- !we_are_hibernating()) {
- router_retry_connections();
- }
- }
- static void run_connection_housekeeping(int i, time_t now) {
- cell_t cell;
- connection_t *conn = connection_array[i];
- or_options_t *options = get_options();
-
- if(conn->type == CONN_TYPE_DIR &&
- !conn->marked_for_close &&
- conn->timestamp_lastwritten + 5*60 < now) {
- log_fn(LOG_INFO,"Expiring wedged directory conn (fd %d, purpose %d)", conn->s, conn->purpose);
- connection_mark_for_close(conn);
- return;
- }
-
- if(connection_speaks_cells(conn) &&
- now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
- routerinfo_t *router = router_get_by_digest(conn->identity_digest);
- if((!connection_state_is_open(conn)) ||
- (we_are_hibernating() && !circuit_get_by_conn(conn)) ||
- (!clique_mode(options) && !circuit_get_by_conn(conn) &&
- (!router || !server_mode(options) || !router_is_clique_mode(router)))) {
-
- log_fn(LOG_INFO,"Expiring connection to %d (%s:%d).",
- i,conn->address, conn->port);
-
- connection_mark_for_close(conn);
- conn->hold_open_until_flushed = 1;
- } else {
-
- log_fn(LOG_DEBUG,"Sending keepalive to (%s:%d)",
- conn->address, conn->port);
- memset(&cell,0,sizeof(cell_t));
- cell.command = CELL_PADDING;
- connection_or_write_cell_to_buf(&cell, conn);
- }
- }
- }
- #define MIN_BW_TO_PUBLISH_DESC 5000
- #define MIN_UPTIME_TO_PUBLISH_DESC (30*60)
- static int decide_if_publishable_server(time_t now) {
- int bw;
- or_options_t *options = get_options();
- bw = rep_hist_bandwidth_assess();
- router_set_bandwidth_capacity(bw);
- if(options->ClientOnly)
- return 0;
- if(!options->ORPort)
- return 0;
-
- return server_mode(options);
-
- if(0) {
- return 0;
- }
- if(bw < MIN_BW_TO_PUBLISH_DESC)
- return 0;
- if(options->AuthoritativeDir)
- return 1;
- if(stats_n_seconds_uptime < MIN_UPTIME_TO_PUBLISH_DESC)
- return 0;
- return 1;
- }
- int authdir_mode(or_options_t *options) {
- return options->AuthoritativeDir != 0;
- }
- int clique_mode(or_options_t *options) {
- return authdir_mode(options);
- }
- int server_mode(or_options_t *options) {
- return (options->ORPort != 0 || options->ORBindAddress);
- }
- static int server_is_advertised=0;
- int advertised_server_mode(void) {
- return server_is_advertised;
- }
- int proxy_mode(or_options_t *options) {
- return (options->SocksPort != 0 || options->SocksBindAddress);
- }
- static void run_scheduled_events(time_t now) {
- static time_t last_uploaded_services = 0;
- static time_t last_rotated_certificate = 0;
- static time_t time_to_check_listeners = 0;
- or_options_t *options = get_options();
- int i;
-
- consider_hibernation(now);
-
- if (server_mode(options) &&
- get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
- log_fn(LOG_INFO,"Rotating onion key.");
- rotate_onion_key();
- cpuworkers_rotate();
- if (router_rebuild_descriptor()<0) {
- log_fn(LOG_WARN, "Couldn't rebuild router descriptor");
- }
- if(advertised_server_mode())
- router_upload_dir_desc_to_dirservers();
- }
-
- if (!last_rotated_certificate)
- last_rotated_certificate = now;
- if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
- log_fn(LOG_INFO,"Rotating tls context.");
- if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
- MAX_SSL_KEY_LIFETIME) < 0) {
- log_fn(LOG_WARN, "Error reinitializing TLS context");
- }
- last_rotated_certificate = now;
-
- }
-
- if (options->AccountingMaxKB)
- accounting_run_housekeeping(now);
-
- if(time_to_fetch_directory < now) {
- if(decide_if_publishable_server(now)) {
- server_is_advertised = 1;
- router_rebuild_descriptor();
- router_upload_dir_desc_to_dirservers();
- } else {
- server_is_advertised = 0;
- }
-
- routerlist_remove_old_routers(ROUTER_MAX_AGE);
- if(authdir_mode(options)) {
-
- dirserv_remove_old_servers(ROUTER_MAX_AGE);
- }
- if(server_mode(options) && !we_are_hibernating()) {
-
- router_retry_connections();
- }
- directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
- if(!we_are_hibernating()) {
-
- rend_services_upload(1);
- last_uploaded_services = now;
- }
- rend_cache_clean();
- time_to_fetch_directory = now + options->DirFetchPostPeriod;
- }
-
- circuit_expire_building(now);
-
- connection_ap_expire_beginning();
-
- connection_expire_held_open();
-
- if (!we_are_hibernating() && time_to_check_listeners < now) {
- retry_all_listeners(0);
- time_to_check_listeners = now+60;
- }
-
- if(has_fetched_directory && !we_are_hibernating())
- circuit_build_needed_circs(now);
-
- for(i=0;i<nfds;i++) {
- run_connection_housekeeping(i, now);
- }
-
- circuit_close_all_marked();
-
- if (last_uploaded_services < now-5) {
- rend_services_upload(0);
- last_uploaded_services = now;
- }
-
- for(i=0;i<nfds;i++)
- conn_close_if_marked(i);
- }
- static int prepare_for_poll(void) {
- static long current_second = 0;
- connection_t *conn;
- struct timeval now;
- int i;
- tor_gettimeofday(&now);
- if(now.tv_sec > current_second) {
-
- size_t bytes_written;
- size_t bytes_read;
- int seconds_elapsed;
- bytes_written = stats_prev_global_write_bucket - global_write_bucket;
- bytes_read = stats_prev_global_read_bucket - global_read_bucket;
- seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
- stats_n_bytes_read += bytes_read;
- stats_n_bytes_written += bytes_written;
- if (get_options()->AccountingMaxKB)
- accounting_add_bytes(bytes_read, bytes_written, seconds_elapsed);
- control_event_bandwidth_used((uint32_t)bytes_read,(uint32_t)bytes_written);
- connection_bucket_refill(&now);
- stats_prev_global_read_bucket = global_read_bucket;
- stats_prev_global_write_bucket = global_write_bucket;
- stats_n_seconds_uptime += seconds_elapsed;
- assert_all_pending_dns_resolves_ok();
- run_scheduled_events(now.tv_sec);
- assert_all_pending_dns_resolves_ok();
- current_second = now.tv_sec;
- }
- for(i=0;i<nfds;i++) {
- conn = connection_array[i];
- if(connection_has_pending_tls_data(conn) &&
- connection_is_reading(conn)) {
- log_fn(LOG_DEBUG,"sock %d has pending bytes.",conn->s);
- return 0;
- }
- }
- return (1000 - (now.tv_usec / 1000));
- }
- static int do_hup(void) {
- char keydir[512];
- or_options_t *options = get_options();
- log_fn(LOG_NOTICE,"Received sighup. Reloading config.");
- has_completed_circuit=0;
-
-
- if (init_from_config(0, NULL) < 0) {
- log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
- return -1;
- }
- options = get_options();
- if(authdir_mode(options)) {
-
- tor_snprintf(keydir,sizeof(keydir),"%s/approved-routers", options->DataDirectory);
- log_fn(LOG_INFO,"Reloading approved fingerprints from %s...",keydir);
- if(dirserv_parse_fingerprint_file(keydir) < 0) {
- log_fn(LOG_WARN, "Error reloading fingerprints. Continuing with old list.");
- }
- }
-
- directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 0);
- if(server_mode(options)) {
-
- cpuworkers_rotate();
- dnsworkers_rotate();
-
- router_rebuild_descriptor();
- tor_snprintf(keydir,sizeof(keydir),"%s/router.desc", options->DataDirectory);
- log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
- if (write_str_to_file(keydir, router_get_my_descriptor(), 0)) {
- return -1;
- }
- }
- return 0;
- }
- static int do_main_loop(void) {
- int i;
- int timeout;
- int poll_result;
-
- rep_hist_init();
-
- rend_cache_init();
-
- if (init_keys() < 0) {
- log_fn(LOG_ERR,"Error initializing keys; exiting");
- return -1;
- }
-
- connection_bucket_init();
- stats_prev_global_read_bucket = global_read_bucket;
- stats_prev_global_write_bucket = global_write_bucket;
-
- if (get_options()->AccountingMaxKB)
- configure_accounting(time(NULL));
-
- if(router_reload_router_list()) {
- return -1;
- }
- if(authdir_mode(get_options())) {
-
- router_retry_connections();
- }
- if(server_mode(get_options())) {
-
- cpu_init();
- }
- for(;;) {
- #ifdef MS_WINDOWS_SERVICE
- if (service_status.dwCurrentState != SERVICE_RUNNING) {
- return 0;
- }
- #endif
- #ifndef MS_WINDOWS
- if(please_shutdown) {
- if(!server_mode(get_options())) {
- log(LOG_NOTICE,"Interrupt: exiting cleanly.");
- tor_cleanup();
- exit(0);
- }
- hibernate_begin_shutdown();
- please_shutdown = 0;
- }
- if(please_sigpipe) {
- log(LOG_NOTICE,"Caught sigpipe. Ignoring.");
- please_sigpipe = 0;
- }
- if(please_dumpstats) {
-
- dumpstats(get_min_log_level()>LOG_INFO ? get_min_log_level() : LOG_INFO);
- please_dumpstats = 0;
- }
- if(please_reset) {
- if (do_hup() < 0) {
- log_fn(LOG_WARN,"Restart failed (config error?). Exiting.");
- tor_cleanup();
- exit(1);
- }
- please_reset = 0;
- }
- if(please_reap_children) {
- while(waitpid(-1,NULL,WNOHANG) > 0) ;
- please_reap_children = 0;
- }
- #endif
- timeout = prepare_for_poll();
-
- poll_result = tor_poll(poll_array, nfds, timeout);
-
- if(poll_result < 0) {
-
- if(tor_socket_errno(-1) != EINTR) {
- log_fn(LOG_ERR,"poll failed: %s [%d]",
- tor_socket_strerror(tor_socket_errno(-1)),
- tor_socket_errno(-1));
- return -1;
- } else {
- log_fn(LOG_DEBUG,"poll interrupted.");
- }
- }
-
- for(i=0;i<nfds;i++)
- conn_read(i);
-
- for(i=0;i<nfds;i++)
- conn_write(i);
-
- for(i=0;i<nfds;i++)
- conn_close_if_marked(i);
-
- }
- }
- static void catch(int the_signal) {
- #ifndef MS_WINDOWS
- switch(the_signal) {
- case SIGTERM:
- log(LOG_ERR,"Catching signal %d, exiting cleanly.", the_signal);
- tor_cleanup();
- exit(0);
- case SIGINT:
- please_shutdown = 1;
- break;
- case SIGPIPE:
-
- please_sigpipe = 1;
- break;
- case SIGHUP:
- please_reset = 1;
- break;
- case SIGUSR1:
- please_dumpstats = 1;
- break;
- case SIGCHLD:
- please_reap_children = 1;
- break;
- default:
- log(LOG_WARN,"Caught signal %d that we can't handle??", the_signal);
- tor_cleanup();
- exit(1);
- }
- #endif
- }
- static void dumpstats(int severity) {
- int i;
- connection_t *conn;
- time_t now = time(NULL);
- log(severity, "Dumping stats:");
- for(i=0;i<nfds;i++) {
- conn = connection_array[i];
- log(severity, "Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
- i, conn->s, conn->type, CONN_TYPE_TO_STRING(conn->type),
- conn->state, conn_state_to_string[conn->type][conn->state], (int)(now - conn->timestamp_created));
- if(!connection_is_listener(conn)) {
- log(severity,"Conn %d is to '%s:%d'.",i,conn->address, conn->port);
- log(severity,"Conn %d: %d bytes waiting on inbuf (last read %d secs ago)",i,
- (int)buf_datalen(conn->inbuf),
- (int)(now - conn->timestamp_lastread));
- log(severity,"Conn %d: %d bytes waiting on outbuf (last written %d secs ago)",i,
- (int)buf_datalen(conn->outbuf), (int)(now - conn->timestamp_lastwritten));
- }
- circuit_dump_by_conn(conn, severity);
- }
- log(severity,
- "Cells processed: %10lu padding\n"
- " %10lu create\n"
- " %10lu created\n"
- " %10lu relay\n"
- " (%10lu relayed)\n"
- " (%10lu delivered)\n"
- " %10lu destroy",
- stats_n_padding_cells_processed,
- stats_n_create_cells_processed,
- stats_n_created_cells_processed,
- stats_n_relay_cells_processed,
- stats_n_relay_cells_relayed,
- stats_n_relay_cells_delivered,
- stats_n_destroy_cells_processed);
- if (stats_n_data_cells_packaged)
- log(severity,"Average packaged cell fullness: %2.3f%%",
- 100*(((double)stats_n_data_bytes_packaged) /
- (stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
- if (stats_n_data_cells_received)
- log(severity,"Average delivered cell fullness: %2.3f%%",
- 100*(((double)stats_n_data_bytes_received) /
- (stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
- if (stats_n_seconds_uptime)
- log(severity,
- "Average bandwidth used: "U64_FORMAT"/%ld = %d bytes/sec",
- U64_PRINTF_ARG(stats_n_bytes_read),
- stats_n_seconds_uptime,
- (int) (stats_n_bytes_read/stats_n_seconds_uptime));
- rep_hist_dump_stats(now,severity);
- rend_service_dump_stats(severity);
- }
- static int network_init(void)
- {
- #ifdef MS_WINDOWS
-
- WSADATA WSAData;
- int r;
- r = WSAStartup(0x101,&WSAData);
- if (r) {
- log_fn(LOG_WARN,"Error initializing windows network layer: code was %d",r);
- return -1;
- }
-
- #endif
- return 0;
- }
- static void exit_function(void)
- {
- #ifdef MS_WINDOWS
- WSACleanup();
- #endif
- }
- void handle_signals(int is_parent)
- {
- #ifndef MS_WINDOWS
- struct sigaction action;
- action.sa_flags = 0;
- sigemptyset(&action.sa_mask);
- action.sa_handler = is_parent ? catch : SIG_IGN;
- sigaction(SIGINT, &action, NULL);
- sigaction(SIGTERM, &action, NULL);
- sigaction(SIGPIPE, &action, NULL);
- sigaction(SIGUSR1, &action, NULL);
- sigaction(SIGHUP, &action, NULL);
- if(is_parent)
- sigaction(SIGCHLD, &action, NULL);
- #endif
- }
- static int tor_init(int argc, char *argv[]) {
-
- add_temp_log();
- log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
- if (network_init()<0) {
- log_fn(LOG_ERR,"Error initializing network; exiting.");
- return -1;
- }
- atexit(exit_function);
- if (init_from_config(argc,argv) < 0) {
- log_fn(LOG_ERR,"Reading config failed--see warnings above. For usage, try -h.");
- return -1;
- }
- #ifndef MS_WINDOWS
- if(geteuid()==0)
- log_fn(LOG_WARN,"You are running Tor as root. You don't need to, and you probably shouldn't.");
- #endif
- if(server_mode(get_options())) {
- dns_init();
- }
- client_dns_init();
- handle_signals(1);
- crypto_global_init();
- crypto_seed_rng();
- return 0;
- }
- void tor_cleanup(void) {
- or_options_t *options = get_options();
-
- if(options->PidFile && options->command == CMD_RUN_TOR)
- unlink(options->PidFile);
- crypto_global_cleanup();
- }
- static void do_list_fingerprint(void)
- {
- char buf[FINGERPRINT_LEN+1];
- crypto_pk_env_t *k;
- const char *nickname = get_options()->Nickname;
- if(!server_mode(get_options())) {
- printf("Clients don't have long-term identity keys. Exiting.");
- return;
- }
- tor_assert(nickname);
- if (init_keys() < 0) {
- log_fn(LOG_ERR,"Error initializing keys; exiting");
- return;
- }
- if (!(k = get_identity_key())) {
- log_fn(LOG_ERR,"Error: missing identity key.");
- return;
- }
- if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
- log_fn(LOG_ERR, "Error computing fingerprint");
- return;
- }
- printf("%s %s\n", nickname, buf);
- }
- static void do_hash_password(void)
- {
- char output[256];
- char key[S2K_SPECIFIER_LEN+DIGEST_LEN];
- crypto_rand(key, S2K_SPECIFIER_LEN-1);
- key[S2K_SPECIFIER_LEN-1] = (uint8_t)96;
- secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
- get_options()->command_arg, strlen(get_options()->command_arg),
- key);
- if (base64_encode(output, sizeof(output), key, sizeof(key))<0) {
- log_fn(LOG_ERR, "Unable to compute base64");
- } else {
- printf("%s",output);
- }
- }
- #ifdef MS_WINDOWS_SERVICE
- void nt_service_control(DWORD request)
- {
- switch (request) {
- case SERVICE_CONTROL_STOP:
- case SERVICE_CONTROL_SHUTDOWN:
- log(LOG_ERR, "Got stop/shutdown request; shutting down cleanly.");
- service_status.dwWin32ExitCode = 0;
- service_status.dwCurrentState = SERVICE_STOPPED;
- return;
- }
- SetServiceStatus(hStatus, &service_status);
- }
- void nt_service_body(int argc, char **argv)
- {
- int err;
- FILE *f;
- f = fopen("d:\\foo.txt", "w");
- fprintf(f, "POINT 1\n");
- fclose(f);
- service_status.dwServiceType = SERVICE_WIN32;
- service_status.dwCurrentState = SERVICE_START_PENDING;
- service_status.dwControlsAccepted =
- SERVICE_ACCEPT_STOP |
- SERVICE_ACCEPT_SHUTDOWN;
- service_status.dwWin32ExitCode = 0;
- service_status.dwServiceSpecificExitCode = 0;
- service_status.dwCheckPoint = 0;
- service_status.dwWaitHint = 0;
- hStatus = RegisterServiceCtrlHandler("Tor", (LPHANDLER_FUNCTION) nt_service_control);
- if (hStatus == 0) {
-
- return;
- }
- err = tor_init(argc, argv);
- if (err) {
-
- service_status.dwCurrentState = SERVICE_STOPPED;
- service_status.dwWin32ExitCode = -1;
- SetServiceStatus(hStatus, &service_status);
- return;
- }
- service_status.dwCurrentState = SERVICE_RUNNING;
- SetServiceStatus(hStatus, &service_status);
- do_main_loop();
- tor_cleanup();
- return;
- }
- void nt_service_main(void)
- {
- SERVICE_TABLE_ENTRY table[2];
- table[0].lpServiceName = "Tor";
- table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
- table[1].lpServiceName = NULL;
- table[1].lpServiceProc = NULL;
- if (!StartServiceCtrlDispatcher(table))
- printf("Error was %d\n",GetLastError());
- }
- #endif
- int tor_main(int argc, char *argv[]) {
- #ifdef MS_WINDOWS_SERVICE
- nt_service_main();
- return 0;
- #else
- if (tor_init(argc, argv)<0)
- return -1;
- switch (get_options()->command) {
- case CMD_RUN_TOR:
- do_main_loop();
- break;
- case CMD_LIST_FINGERPRINT:
- do_list_fingerprint();
- break;
- case CMD_HASH_PASSWORD:
- do_hash_password();
- break;
- default:
- log_fn(LOG_ERR, "Illegal command number %d: internal error.",
- get_options()->command);
- }
- tor_cleanup();
- return -1;
- #endif
- }
|