2004-11-07 02:33:06 +01:00
|
|
|
/* Copyright 2001 Matej Pfajfar.
|
|
|
|
* Copyright 2001-2004 Roger Dingledine.
|
2005-04-01 22:15:56 +02:00
|
|
|
* Copyright 2004-2005 Roger Dingledine, Nick Mathewson. */
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-27 05:52:34 +01:00
|
|
|
const char main_c_id[] =
|
|
|
|
"$Id$";
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file main.c
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Toplevel module. Handles signals, multiplexes between
|
|
|
|
* connections, implements main loop, and drives scheduled events.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
#include "or.h"
|
2005-02-03 08:25:10 +01:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
#include <dmalloc.h>
|
|
|
|
#endif
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-02-29 02:31:33 +01:00
|
|
|
/********* PROTOTYPES **********/
|
2003-07-05 07:46:06 +02:00
|
|
|
|
2005-06-08 19:24:58 +02:00
|
|
|
static void dumpmemusage(int severity);
|
2003-10-15 20:28:32 +02:00
|
|
|
static void dumpstats(int severity); /* log stats */
|
2005-01-12 07:42:32 +01:00
|
|
|
static void conn_read_callback(int fd, short event, void *_conn);
|
|
|
|
static void conn_write_callback(int fd, short event, void *_conn);
|
|
|
|
static void signal_callback(int fd, short events, void *arg);
|
|
|
|
static void second_elapsed_callback(int fd, short event, void *args);
|
|
|
|
static int conn_close_if_marked(int i);
|
2003-07-05 07:46:06 +02:00
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
/********* START VARIABLES **********/
|
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
int global_read_bucket; /**< Max number of bytes I can read this second. */
|
2004-09-08 08:52:33 +02:00
|
|
|
int global_write_bucket; /**< Max number of bytes I can write this second. */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** What was the read bucket before the last call to prepare_for_pool?
|
2004-05-05 23:32:43 +02:00
|
|
|
* (used to determine how many bytes we've read). */
|
2003-10-02 22:00:38 +02:00
|
|
|
static int stats_prev_global_read_bucket;
|
2004-09-08 08:52:33 +02:00
|
|
|
/** What was the write bucket before the last call to prepare_for_pool?
|
|
|
|
* (used to determine how many bytes we've written). */
|
|
|
|
static int stats_prev_global_write_bucket;
|
|
|
|
/** How many bytes have we read/written since we started the process? */
|
2003-10-02 22:00:38 +02:00
|
|
|
static uint64_t stats_n_bytes_read = 0;
|
2004-09-08 08:52:33 +02:00
|
|
|
static uint64_t stats_n_bytes_written = 0;
|
2005-01-10 06:10:22 +01:00
|
|
|
/** What time did this process start up? */
|
|
|
|
long time_of_process_start = 0;
|
2004-05-09 18:47:25 +02:00
|
|
|
/** How many seconds have we been running? */
|
2005-01-10 06:10:22 +01:00
|
|
|
long stats_n_seconds_working = 0;
|
2004-10-28 20:37:52 +02:00
|
|
|
/** When do we next download a directory? */
|
|
|
|
static time_t time_to_fetch_directory = 0;
|
2004-11-15 10:05:54 +01:00
|
|
|
/** When do we next download a running-routers summary? */
|
|
|
|
static time_t time_to_fetch_running_routers = 0;
|
2004-10-28 20:37:52 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Array of all open connections; each element corresponds to the element of
|
2004-05-05 23:32:43 +02:00
|
|
|
* poll_array in the same position. The first nfds elements are valid. */
|
2005-01-28 09:53:47 +01:00
|
|
|
static connection_t *connection_array[MAXCONNECTIONS+1] =
|
2002-06-27 00:45:49 +02:00
|
|
|
{ NULL };
|
2005-01-12 07:42:32 +01:00
|
|
|
static smartlist_t *closeable_connection_lst = NULL;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-10 12:27:54 +02:00
|
|
|
static int nfds=0; /**< Number of connections currently active. */
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** We set this to 1 when we've opened a circuit, so we can print a log
|
2004-02-29 02:31:33 +01:00
|
|
|
* entry to inform the user that Tor is working. */
|
2004-05-05 23:32:43 +02:00
|
|
|
int has_completed_circuit=0;
|
2004-02-29 02:31:33 +01:00
|
|
|
|
2005-03-19 02:04:05 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
#define MS_WINDOWS_SERVICE
|
|
|
|
#endif
|
|
|
|
|
2004-08-04 06:58:30 +02:00
|
|
|
#ifdef MS_WINDOWS_SERVICE
|
2004-11-22 23:10:37 +01:00
|
|
|
#include <tchar.h>
|
2005-02-09 03:26:24 +01:00
|
|
|
#define GENSRV_SERVICENAME TEXT("tor")
|
|
|
|
#define GENSRV_DISPLAYNAME TEXT("Tor Win32 Service")
|
2005-12-14 21:40:40 +01:00
|
|
|
#define GENSRV_DESCRIPTION \
|
|
|
|
TEXT("Provides an anonymous Internet communication system")
|
2005-07-09 18:37:12 +02:00
|
|
|
|
|
|
|
// Cheating: using the pre-defined error codes, tricks Windows into displaying
|
|
|
|
// a semi-related human-readable error message if startup fails as
|
|
|
|
// opposed to simply scaring people with Error: 0xffffffff
|
|
|
|
#define NT_SERVICE_ERROR_NO_TORRC ERROR_FILE_NOT_FOUND
|
|
|
|
#define NT_SERVICE_ERROR_TORINIT_FAILED ERROR_EXCEPTION_IN_SERVICE
|
|
|
|
|
2004-06-12 23:43:02 +02:00
|
|
|
SERVICE_STATUS service_status;
|
|
|
|
SERVICE_STATUS_HANDLE hStatus;
|
2004-11-22 23:10:37 +01:00
|
|
|
static char **backup_argv;
|
|
|
|
static int backup_argc;
|
2005-01-27 22:45:32 +01:00
|
|
|
static int nt_service_is_stopped(void);
|
2005-07-09 18:37:12 +02:00
|
|
|
static char* nt_strerror(uint32_t errnum);
|
2005-01-27 22:45:32 +01:00
|
|
|
#else
|
|
|
|
#define nt_service_is_stopped() (0)
|
2004-06-12 23:43:02 +02:00
|
|
|
#endif
|
|
|
|
|
2005-08-22 23:53:12 +02:00
|
|
|
#define FORCE_REGENERATE_DESCRIPTOR_INTERVAL 18*60*60 /* 18 hours */
|
2005-03-26 06:54:50 +01:00
|
|
|
#define CHECK_DESCRIPTOR_INTERVAL 60 /* one minute */
|
2005-11-18 22:57:49 +01:00
|
|
|
#define CHECK_IPADDRESS_INTERVAL (15*60) /* 15 minutes */
|
2005-05-14 03:11:45 +02:00
|
|
|
#define BUF_SHRINK_INTERVAL 60 /* one minute */
|
2005-09-23 20:05:14 +02:00
|
|
|
#define DESCRIPTOR_RETRY_INTERVAL 10
|
2005-09-22 08:34:29 +02:00
|
|
|
#define DESCRIPTOR_FAILURE_RESET_INTERVAL 60*60
|
2005-10-07 00:22:22 +02:00
|
|
|
#define ENTROPY_INTERVAL 60*60
|
2004-11-13 17:53:48 +01:00
|
|
|
|
2002-06-27 00:45:49 +02:00
|
|
|
/********* END VARIABLES ************/
|
|
|
|
|
2002-07-05 08:27:23 +02:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* This section contains accessors and other methods on the connection_array
|
|
|
|
* and poll_array variables (which are global within this file and unavailable
|
|
|
|
* outside it).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Add <b>conn</b> to the array of connections that we can poll on. The
|
2004-05-05 23:32:43 +02:00
|
|
|
* connection's socket must be set; the connection starts out
|
|
|
|
* non-reading and non-writing.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_add(connection_t *conn)
|
|
|
|
{
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn);
|
2004-05-05 03:26:57 +02:00
|
|
|
tor_assert(conn->s >= 0);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2005-02-23 00:52:44 +01:00
|
|
|
if (nfds >= get_options()->_ConnLimit-1) {
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET,"Failing because we have %d connections already. Please "
|
|
|
|
"raise your ulimit -n.", nfds);
|
2002-06-27 00:45:49 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2003-12-05 10:51:49 +01:00
|
|
|
|
2004-05-06 13:08:04 +02:00
|
|
|
tor_assert(conn->poll_index == -1); /* can only connection_add once */
|
2002-06-27 00:45:49 +02:00
|
|
|
conn->poll_index = nfds;
|
|
|
|
connection_array[nfds] = conn;
|
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
conn->read_event = tor_malloc_zero(sizeof(struct event));
|
|
|
|
conn->write_event = tor_malloc_zero(sizeof(struct event));
|
|
|
|
event_set(conn->read_event, conn->s, EV_READ|EV_PERSIST,
|
|
|
|
conn_read_callback, conn);
|
|
|
|
event_set(conn->write_event, conn->s, EV_WRITE|EV_PERSIST,
|
|
|
|
conn_write_callback, conn);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
nfds++;
|
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_NET,"new conn type %s, socket %d, nfds %d.",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn_type_to_string(conn->type), conn->s, nfds);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Remove the connection from the global list, and remove the
|
2003-10-09 20:45:14 +02:00
|
|
|
* corresponding poll entry. Calling this function will shift the last
|
|
|
|
* connection (if any) into the position occupied by conn.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_remove(connection_t *conn)
|
|
|
|
{
|
2002-06-27 00:45:49 +02:00
|
|
|
int current_index;
|
|
|
|
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(conn);
|
|
|
|
tor_assert(nfds>0);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_NET,"removing socket %d (type %s), nfds now %d",
|
|
|
|
conn->s, conn_type_to_string(conn->type), nfds-1);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2004-05-06 13:08:04 +02:00
|
|
|
tor_assert(conn->poll_index >= 0);
|
2002-06-27 00:45:49 +02:00
|
|
|
current_index = conn->poll_index;
|
2004-11-28 10:05:49 +01:00
|
|
|
if (current_index == nfds-1) { /* this is the end */
|
2002-06-27 00:45:49 +02:00
|
|
|
nfds--;
|
|
|
|
return 0;
|
2003-12-05 10:51:49 +01:00
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
connection_unregister(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
|
2004-03-18 20:22:56 +01:00
|
|
|
/* replace this one with the one at the end */
|
2002-06-27 00:45:49 +02:00
|
|
|
nfds--;
|
|
|
|
connection_array[current_index] = connection_array[nfds];
|
|
|
|
connection_array[current_index]->poll_index = current_index;
|
|
|
|
|
2003-12-05 10:51:49 +01:00
|
|
|
return 0;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2005-01-30 22:47:47 +01:00
|
|
|
/** If it's an edge conn, remove it from the list
|
|
|
|
* of conn's on this circuit. If it's not on an edge,
|
|
|
|
* flush and send destroys for all circuits on this conn.
|
|
|
|
*
|
|
|
|
* If <b>remove</b> is non-zero, then remove it from the
|
|
|
|
* connection_array and closeable_connection_lst.
|
|
|
|
*
|
|
|
|
* Then free it.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
connection_unlink(connection_t *conn, int remove)
|
|
|
|
{
|
2005-01-30 22:47:47 +01:00
|
|
|
circuit_about_to_close_connection(conn);
|
|
|
|
connection_about_to_close_connection(conn);
|
|
|
|
if (remove) {
|
|
|
|
connection_remove(conn);
|
|
|
|
}
|
2005-01-31 01:26:09 +01:00
|
|
|
smartlist_remove(closeable_connection_lst, conn);
|
2005-01-30 22:47:47 +01:00
|
|
|
if (conn->type == CONN_TYPE_EXIT) {
|
|
|
|
assert_connection_edge_not_dns_pending(conn);
|
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
if (conn->type == CONN_TYPE_OR &&
|
|
|
|
!tor_digest_is_zero(conn->identity_digest)) {
|
2005-11-30 05:28:41 +01:00
|
|
|
connection_or_remove_from_identity_map(conn);
|
|
|
|
}
|
2005-01-30 22:47:47 +01:00
|
|
|
connection_free(conn);
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Schedule <b>conn</b> to be closed. **/
|
2005-01-12 07:42:32 +01:00
|
|
|
void
|
|
|
|
add_connection_to_closeable_list(connection_t *conn)
|
|
|
|
{
|
|
|
|
tor_assert(!smartlist_isin(closeable_connection_lst, conn));
|
|
|
|
tor_assert(conn->marked_for_close);
|
2005-02-02 01:19:09 +01:00
|
|
|
assert_connection_ok(conn, time(NULL));
|
2005-01-12 07:42:32 +01:00
|
|
|
smartlist_add(closeable_connection_lst, conn);
|
|
|
|
}
|
|
|
|
|
2005-01-31 01:33:32 +01:00
|
|
|
/** Return 1 if conn is on the closeable list, else return 0. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_is_on_closeable_list(connection_t *conn)
|
|
|
|
{
|
2005-01-31 01:33:32 +01:00
|
|
|
return smartlist_isin(closeable_connection_lst, conn);
|
|
|
|
}
|
|
|
|
|
2004-06-02 00:09:58 +02:00
|
|
|
/** Return true iff conn is in the current poll array. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_in_array(connection_t *conn)
|
|
|
|
{
|
2004-06-02 00:09:58 +02:00
|
|
|
int i;
|
|
|
|
for (i=0; i<nfds; ++i) {
|
|
|
|
if (conn==connection_array[i])
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Set <b>*array</b> to an array of all connections, and <b>*n</b>
|
|
|
|
* to the length of the array. <b>*array</b> and <b>*n</b> must not
|
|
|
|
* be modified.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
get_connection_array(connection_t ***array, int *n)
|
|
|
|
{
|
2003-09-30 21:06:22 +02:00
|
|
|
*array = connection_array;
|
|
|
|
*n = nfds;
|
2003-06-25 09:19:30 +02:00
|
|
|
}
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
/** Set the event mask on <b>conn</b> to <b>events</b>. (The event
|
2005-06-11 20:52:12 +02:00
|
|
|
* mask is a bitmask whose bits are EV_READ and EV_WRITE.)
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_watch_events(connection_t *conn, short events)
|
|
|
|
{
|
2005-02-25 06:42:01 +01:00
|
|
|
int r;
|
|
|
|
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn->read_event);
|
|
|
|
tor_assert(conn->write_event);
|
2002-06-27 00:45:49 +02:00
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
if (events & EV_READ) {
|
2005-02-25 06:42:01 +01:00
|
|
|
r = event_add(conn->read_event, NULL);
|
2005-01-12 07:42:32 +01:00
|
|
|
} else {
|
2005-02-25 06:42:01 +01:00
|
|
|
r = event_del(conn->read_event);
|
2005-01-12 07:42:32 +01:00
|
|
|
}
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
if (r<0)
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_NET,
|
2005-12-14 21:40:40 +01:00
|
|
|
"Error from libevent setting read event state for %d to "
|
|
|
|
"%swatched: %s",
|
2005-11-26 10:37:00 +01:00
|
|
|
conn->s, (events & EV_READ)?"":"un",
|
|
|
|
tor_socket_strerror(tor_socket_errno(conn->s)));
|
2005-02-25 06:42:01 +01:00
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
if (events & EV_WRITE) {
|
2005-02-25 06:42:01 +01:00
|
|
|
r = event_add(conn->write_event, NULL);
|
2005-01-12 07:42:32 +01:00
|
|
|
} else {
|
2005-02-25 06:42:01 +01:00
|
|
|
r = event_del(conn->write_event);
|
2005-01-12 07:42:32 +01:00
|
|
|
}
|
2005-02-25 06:42:01 +01:00
|
|
|
|
|
|
|
if (r<0)
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_NET,
|
2005-12-14 21:40:40 +01:00
|
|
|
"Error from libevent setting read event state for %d to "
|
|
|
|
"%swatched: %s",
|
2005-11-26 10:37:00 +01:00
|
|
|
conn->s, (events & EV_WRITE)?"":"un",
|
|
|
|
tor_socket_strerror(tor_socket_errno(conn->s)));
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Return true iff <b>conn</b> is listening for read events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_is_reading(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
return conn->read_event && event_pending(conn->read_event, EV_READ, NULL);
|
2003-09-07 12:24:40 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Tell the main loop to stop notifying <b>conn</b> of any read events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_stop_reading(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn->read_event);
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_NET,"connection_stop_reading() called.");
|
2005-02-25 06:42:01 +01:00
|
|
|
if (event_del(conn->read_event))
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET, "Error from libevent setting read event state for %d "
|
|
|
|
"to unwatched.",
|
2005-10-25 10:20:10 +02:00
|
|
|
conn->s);
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Tell the main loop to start notifying <b>conn</b> of any read events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_start_reading(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn->read_event);
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
if (event_add(conn->read_event, NULL))
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET, "Error from libevent setting read event state for %d "
|
|
|
|
"to watched.",
|
2005-10-25 10:20:10 +02:00
|
|
|
conn->s);
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Return true iff <b>conn</b> is listening for write events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
connection_is_writing(connection_t *conn)
|
|
|
|
{
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn);
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
return conn->write_event && event_pending(conn->write_event, EV_WRITE, NULL);
|
2004-02-27 05:42:14 +01:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Tell the main loop to stop notifying <b>conn</b> of any write events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_stop_writing(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn->write_event);
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
if (event_del(conn->write_event))
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET, "Error from libevent setting write event state for %d "
|
|
|
|
"to unwatched.",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s);
|
2005-02-25 06:42:01 +01:00
|
|
|
|
2002-07-18 08:37:58 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Tell the main loop to start notifying <b>conn</b> of any write events. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
connection_start_writing(connection_t *conn)
|
|
|
|
{
|
2004-10-17 00:14:52 +02:00
|
|
|
tor_assert(conn);
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_assert(conn->write_event);
|
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
if (event_add(conn->write_event, NULL))
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_NET, "Error from libevent setting write event state for %d "
|
|
|
|
"to watched.",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s);
|
2002-07-18 08:37:58 +02:00
|
|
|
}
|
|
|
|
|
2005-04-08 08:16:09 +02:00
|
|
|
/** Close all connections that have been scheduled to get closed */
|
2005-01-12 07:42:32 +01:00
|
|
|
static void
|
|
|
|
close_closeable_connections(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < smartlist_len(closeable_connection_lst); ) {
|
|
|
|
connection_t *conn = smartlist_get(closeable_connection_lst, i);
|
2005-01-30 22:47:47 +01:00
|
|
|
if (conn->poll_index < 0) {
|
|
|
|
connection_unlink(conn, 0); /* blow it away right now */
|
|
|
|
} else {
|
|
|
|
if (!conn_close_if_marked(conn->poll_index))
|
|
|
|
++i;
|
|
|
|
}
|
2005-01-12 07:42:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
|
|
|
|
* some data to read. */
|
2005-01-12 07:42:32 +01:00
|
|
|
static void
|
|
|
|
conn_read_callback(int fd, short event, void *_conn)
|
|
|
|
{
|
|
|
|
connection_t *conn = _conn;
|
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_NET,"socket %d wants to read.",conn->s);
|
2005-01-12 07:42:32 +01:00
|
|
|
|
|
|
|
assert_connection_ok(conn, time(NULL));
|
|
|
|
|
|
|
|
if (connection_handle_read(conn) < 0) {
|
|
|
|
if (!conn->marked_for_close) {
|
|
|
|
#ifndef MS_WINDOWS
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_BUG,"Bug: unhandled error on read for %s connection "
|
|
|
|
"(fd %d); removing",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn_type_to_string(conn->type), conn->s);
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2005-01-12 07:42:32 +01:00
|
|
|
#endif
|
2005-03-02 04:13:05 +01:00
|
|
|
if (CONN_IS_EDGE(conn))
|
|
|
|
connection_edge_end_errno(conn, conn->cpath_layer);
|
2005-01-12 07:42:32 +01:00
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_connection_ok(conn, time(NULL));
|
|
|
|
|
|
|
|
if (smartlist_len(closeable_connection_lst))
|
|
|
|
close_closeable_connections();
|
|
|
|
}
|
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Libevent callback: this gets invoked when (connection_t*)<b>conn</b> has
|
|
|
|
* some data to write. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
conn_write_callback(int fd, short events, void *_conn)
|
2005-01-12 07:42:32 +01:00
|
|
|
{
|
|
|
|
connection_t *conn = _conn;
|
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
LOG_FN_CONN(conn, (LOG_DEBUG, LD_NET, "socket %d wants to write.",conn->s));
|
2005-01-12 07:42:32 +01:00
|
|
|
|
|
|
|
assert_connection_ok(conn, time(NULL));
|
|
|
|
|
|
|
|
if (connection_handle_write(conn) < 0) {
|
|
|
|
if (!conn->marked_for_close) {
|
|
|
|
/* this connection is broken. remove it. */
|
2005-12-14 21:40:40 +01:00
|
|
|
log_fn(LOG_WARN,LD_BUG,
|
|
|
|
"Bug: unhandled error on write for %s connection (fd %d); removing",
|
|
|
|
conn_type_to_string(conn->type), conn->s);
|
2005-04-26 20:52:16 +02:00
|
|
|
tor_fragile_assert();
|
2005-01-12 07:42:32 +01:00
|
|
|
conn->has_sent_end = 1; /* otherwise we cry wolf about duplicate close */
|
|
|
|
/* XXX do we need a close-immediate here, so we don't try to flush? */
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_connection_ok(conn, time(NULL));
|
|
|
|
|
|
|
|
if (smartlist_len(closeable_connection_lst))
|
|
|
|
close_closeable_connections();
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** If the connection at connection_array[i] is marked for close, then:
|
2004-05-05 23:32:43 +02:00
|
|
|
* - If it has data that it wants to flush, try to flush it.
|
|
|
|
* - If it _still_ has data to flush, and conn->hold_open_until_flushed is
|
|
|
|
* true, then leave the connection open and return.
|
|
|
|
* - Otherwise, remove the connection from connection_array and from
|
|
|
|
* all other lists, close it, and free it.
|
2005-01-12 07:42:32 +01:00
|
|
|
* Returns 1 if the connection was closed, 0 otherwise.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
conn_close_if_marked(int i)
|
|
|
|
{
|
2002-06-27 00:45:49 +02:00
|
|
|
connection_t *conn;
|
2004-03-03 09:46:18 +01:00
|
|
|
int retval;
|
2002-06-27 00:45:49 +02:00
|
|
|
|
|
|
|
conn = connection_array[i];
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn->marked_for_close)
|
2005-01-12 07:42:32 +01:00
|
|
|
return 0; /* nothing to see here, move along */
|
2004-12-25 07:43:40 +01:00
|
|
|
assert_connection_ok(conn, time(NULL));
|
|
|
|
assert_all_pending_dns_resolves_ok();
|
2004-03-03 09:46:18 +01:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_NET,"Cleaning up connection (fd %d).",conn->s);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->s >= 0 && connection_wants_to_flush(conn)) {
|
2005-10-29 20:19:37 +02:00
|
|
|
/* s == -1 means it's an incomplete edge connection, or that the socket
|
2004-03-03 09:46:18 +01:00
|
|
|
* has already been closed as unflushable. */
|
2005-10-29 20:19:37 +02:00
|
|
|
int sz = connection_bucket_write_limit(conn);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!conn->hold_open_until_flushed)
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_NET,
|
2005-12-10 10:36:26 +01:00
|
|
|
"Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
|
|
|
|
"to flush %d bytes. (Marked at %s:%d)",
|
2005-12-14 21:40:40 +01:00
|
|
|
conn->address, conn->s, conn_type_to_string(conn->type),
|
|
|
|
conn->state,
|
2005-12-10 10:36:26 +01:00
|
|
|
(int)conn->outbuf_flushlen,
|
|
|
|
conn->marked_for_close_file, conn->marked_for_close);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (connection_speaks_cells(conn)) {
|
|
|
|
if (conn->state == OR_CONN_STATE_OPEN) {
|
2005-12-14 21:40:40 +01:00
|
|
|
retval = flush_buf_tls(conn->tls, conn->outbuf, sz,
|
|
|
|
&conn->outbuf_flushlen);
|
2004-03-03 09:46:18 +01:00
|
|
|
} else
|
2004-03-06 02:43:37 +01:00
|
|
|
retval = -1; /* never flush non-open broken tls connections */
|
2004-03-03 09:46:18 +01:00
|
|
|
} else {
|
2005-10-29 20:19:37 +02:00
|
|
|
retval = flush_buf(conn->s, conn->outbuf, sz, &conn->outbuf_flushlen);
|
2004-03-03 09:46:18 +01:00
|
|
|
}
|
2005-10-29 20:19:37 +02:00
|
|
|
if (retval >= 0 && /* Technically, we could survive things like
|
|
|
|
TLS_WANT_WRITE here. But don't bother for now. */
|
|
|
|
conn->hold_open_until_flushed && connection_wants_to_flush(conn)) {
|
2005-12-14 21:40:40 +01:00
|
|
|
LOG_FN_CONN(conn, (LOG_INFO,LD_NET,
|
|
|
|
"Holding conn (fd %d) open for more flushing.",
|
|
|
|
conn->s));
|
2004-03-03 09:46:18 +01:00
|
|
|
/* XXX should we reset timestamp_lastwritten here? */
|
2005-01-12 07:42:32 +01:00
|
|
|
return 0;
|
Integrated onion proxy into or/
The 'or' process can now be told (by the global_role variable) what
roles this server should play -- connect to all ORs, listen for ORs,
listen for OPs, listen for APs, or any combination.
* everything in /src/op/ is now obsolete.
* connection_ap.c now handles all interactions with application proxies
* "port" is now or_port, op_port, ap_port. But routers are still always
referenced (say, in conn_get_by_addr_port()) by addr / or_port. We
should make routers.c actually read these new ports (currently I've
kludged it so op_port = or_port+10, ap_port=or_port+20)
* circuits currently know if they're at the beginning of the path because
circ->cpath is set. They use this instead for crypts (both ways),
if it's set.
* I still obey the "send a 0 back to the AP when you're ready" protocol,
but I think we should phase it out. I can simply not read from the AP
socket until I'm ready.
I need to do a lot of cleanup work here, but the code appears to work, so
now's a good time for a checkin.
svn:r22
2002-07-02 11:36:58 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (connection_wants_to_flush(conn)) {
|
2005-10-17 02:35:53 +02:00
|
|
|
int severity;
|
|
|
|
if (conn->type == CONN_TYPE_EXIT ||
|
|
|
|
(conn->type == CONN_TYPE_DIR && conn->purpose == DIR_PURPOSE_SERVER))
|
|
|
|
severity = LOG_INFO;
|
|
|
|
else
|
|
|
|
severity = LOG_NOTICE;
|
2005-12-09 06:37:26 +01:00
|
|
|
log_fn(severity, LD_NET, "Something wrong with your network connection? "
|
2006-01-11 05:04:42 +01:00
|
|
|
"We tried to write %d bytes to addr %s (fd %d, type %s, state %d)"
|
|
|
|
" but timed out. (Marked at %s:%d)",
|
2006-01-07 02:43:09 +01:00
|
|
|
(int)buf_datalen(conn->outbuf),
|
2005-05-03 12:04:08 +02:00
|
|
|
safe_str(conn->address), conn->s, conn_type_to_string(conn->type),
|
|
|
|
conn->state,
|
2006-01-07 02:43:09 +01:00
|
|
|
conn->marked_for_close_file,
|
2004-03-12 09:16:48 +01:00
|
|
|
conn->marked_for_close);
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
2005-01-30 22:47:47 +01:00
|
|
|
connection_unlink(conn, 1); /* unlink, remove, free */
|
2005-01-12 07:42:32 +01:00
|
|
|
return 1;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
|
2005-01-07 16:57:57 +01:00
|
|
|
/** We've just tried every dirserver we know about, and none of
|
|
|
|
* them were reachable. Assume the network is down. Change state
|
|
|
|
* so next time an application connection arrives we'll delay it
|
|
|
|
* and try another directory fetch. Kill off all the circuit_wait
|
|
|
|
* streams that are waiting now, since they will all timeout anyway.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
directory_all_unreachable(time_t now)
|
|
|
|
{
|
2005-01-07 16:57:57 +01:00
|
|
|
connection_t *conn;
|
|
|
|
|
2005-01-10 06:10:22 +01:00
|
|
|
stats_n_seconds_working=0; /* reset it */
|
2005-01-07 16:57:57 +01:00
|
|
|
|
|
|
|
while ((conn = connection_get_by_type_state(CONN_TYPE_AP,
|
|
|
|
AP_CONN_STATE_CIRCUIT_WAIT))) {
|
2005-12-15 23:10:03 +01:00
|
|
|
notice(LD_NET,
|
|
|
|
"Is your network connection down? Failing connection to '%s:%d'.",
|
2005-05-03 12:04:08 +02:00
|
|
|
safe_str(conn->socks_request->address), conn->socks_request->port);
|
2005-04-03 00:11:24 +02:00
|
|
|
connection_mark_unattached_ap(conn, END_STREAM_REASON_NET_UNREACHABLE);
|
2005-01-07 16:57:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/**
|
2006-01-10 08:21:01 +01:00
|
|
|
* Return the interval to wait between directory downloads, in seconds.
|
2005-06-11 20:52:12 +02:00
|
|
|
*/
|
2005-03-22 19:56:11 +01:00
|
|
|
static INLINE int
|
2005-03-17 13:38:37 +01:00
|
|
|
get_dir_fetch_period(or_options_t *options)
|
|
|
|
{
|
|
|
|
if (options->DirFetchPeriod)
|
|
|
|
/* Value from config file. */
|
|
|
|
return options->DirFetchPeriod;
|
|
|
|
else if (options->DirPort)
|
|
|
|
/* Default for directory server */
|
|
|
|
return 20*60;
|
|
|
|
else
|
|
|
|
/* Default for average user. */
|
|
|
|
return 40*60;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/**
|
|
|
|
* Return the interval to wait betweeen router status downloads, in seconds.
|
|
|
|
*/
|
2005-03-22 19:56:11 +01:00
|
|
|
static INLINE int
|
2005-03-17 13:38:37 +01:00
|
|
|
get_status_fetch_period(or_options_t *options)
|
|
|
|
{
|
|
|
|
if (options->StatusFetchPeriod)
|
|
|
|
/* Value from config file. */
|
|
|
|
return options->StatusFetchPeriod;
|
|
|
|
else if (options->DirPort)
|
|
|
|
/* Default for directory server */
|
|
|
|
return 15*60;
|
|
|
|
else
|
|
|
|
/* Default for average user. */
|
|
|
|
return 30*60;
|
|
|
|
}
|
|
|
|
|
2005-09-30 01:26:42 +02:00
|
|
|
/** This function is called whenever we successfully pull down some new
|
|
|
|
* network statuses or server descriptors. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-09-15 07:19:38 +02:00
|
|
|
directory_info_has_arrived(time_t now, int from_cache)
|
2005-06-11 20:52:12 +02:00
|
|
|
{
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-04-01 00:02:13 +02:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
if (!router_have_minimum_dir_info()) {
|
2005-12-14 21:40:40 +01:00
|
|
|
log(LOG_NOTICE, LD_DIR,
|
|
|
|
"I learned some more directory information, but not enough to "
|
|
|
|
"build a circuit.");
|
2005-09-15 07:19:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2005-08-24 04:31:02 +02:00
|
|
|
|
2004-11-06 06:18:11 +01:00
|
|
|
if (server_mode(options) &&
|
2004-10-31 21:28:41 +01:00
|
|
|
!we_are_hibernating()) { /* connect to the appropriate routers */
|
2005-08-24 04:31:02 +02:00
|
|
|
if (!authdir_mode(options))
|
2006-01-10 08:21:01 +01:00
|
|
|
router_retry_connections(0, 1);
|
2005-09-15 07:19:38 +02:00
|
|
|
if (!from_cache)
|
2005-02-27 10:47:01 +01:00
|
|
|
consider_testing_reachability();
|
2004-04-09 21:30:38 +02:00
|
|
|
}
|
2004-04-01 00:02:13 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Perform regular maintenance tasks for a single connection. This
|
2005-02-27 10:47:01 +01:00
|
|
|
* function gets run once per second per connection by run_scheduled_events.
|
2003-10-09 20:45:14 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
run_connection_housekeeping(int i, time_t now)
|
|
|
|
{
|
2003-10-09 20:45:14 +02:00
|
|
|
cell_t cell;
|
|
|
|
connection_t *conn = connection_array[i];
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2003-12-05 10:51:49 +01:00
|
|
|
|
2005-03-22 21:25:51 +01:00
|
|
|
if (conn->outbuf && !buf_datalen(conn->outbuf))
|
|
|
|
conn->timestamp_lastempty = now;
|
|
|
|
|
2006-01-07 04:56:56 +01:00
|
|
|
if (conn->marked_for_close) {
|
|
|
|
/* nothing to do here */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-05 23:32:43 +02:00
|
|
|
/* Expire any directory connections that haven't sent anything for 5 min */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (conn->type == CONN_TYPE_DIR &&
|
2004-11-28 12:39:53 +01:00
|
|
|
conn->timestamp_lastwritten + 5*60 < now) {
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_DIR,"Expiring wedged directory conn (fd %d, purpose %d)",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s, conn->purpose);
|
2005-10-05 00:34:09 +02:00
|
|
|
/* This check is temporary; it's to let us know whether we should consider
|
|
|
|
* parsing partial serverdesc responses. */
|
2005-10-05 02:22:56 +02:00
|
|
|
if (conn->purpose == DIR_PURPOSE_FETCH_SERVERDESC &&
|
2005-10-14 04:26:13 +02:00
|
|
|
buf_datalen(conn->inbuf)>=1024) {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_DIR,"Trying to extract information from wedged server desc "
|
|
|
|
"download.");
|
2005-10-14 04:26:13 +02:00
|
|
|
connection_dir_reached_eof(conn);
|
2005-11-04 06:00:12 +01:00
|
|
|
} else {
|
|
|
|
connection_mark_for_close(conn);
|
2005-10-05 00:34:09 +02:00
|
|
|
}
|
2004-04-18 08:35:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-11-19 07:57:44 +01:00
|
|
|
if (!connection_speaks_cells(conn))
|
|
|
|
return; /* we're all done here, the rest is just for OR conns */
|
|
|
|
|
|
|
|
#define TIME_BEFORE_OR_CONN_IS_OBSOLETE (60*60*24*7) /* a week */
|
2006-01-07 04:56:56 +01:00
|
|
|
if (!conn->is_obsolete) {
|
|
|
|
if (conn->timestamp_created + TIME_BEFORE_OR_CONN_IS_OBSOLETE < now) {
|
|
|
|
info(LD_OR, "Marking OR conn to %s:%d obsolete (fd %d, %d secs old).",
|
|
|
|
conn->address, conn->port, conn->s,
|
|
|
|
(int)(now - conn->timestamp_created));
|
|
|
|
conn->is_obsolete = 1;
|
2006-01-11 23:34:07 +01:00
|
|
|
} else {
|
|
|
|
connection_t *best =
|
|
|
|
connection_or_get_by_identity_digest(conn->identity_digest);
|
|
|
|
if (best && best != conn) {
|
|
|
|
info(LD_OR,
|
|
|
|
"Marking duplicate conn to %s:%d obsolete (fd %d, %d secs old).",
|
|
|
|
conn->address, conn->port, conn->s,
|
|
|
|
(int)(now - conn->timestamp_created));
|
|
|
|
conn->is_obsolete = 1;
|
|
|
|
}
|
2006-01-07 04:56:56 +01:00
|
|
|
}
|
2005-11-19 07:57:44 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 02:43:57 +01:00
|
|
|
if (conn->is_obsolete && conn->n_circuits == 0) {
|
2005-11-19 07:57:44 +01:00
|
|
|
/* no unmarked circs -- mark it now */
|
|
|
|
info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) [Obsolete].",
|
|
|
|
conn->s,conn->address, conn->port);
|
|
|
|
connection_mark_for_close(conn);
|
|
|
|
conn->hold_open_until_flushed = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-05-05 23:32:43 +02:00
|
|
|
/* If we haven't written to an OR connection for a while, then either nuke
|
|
|
|
the connection or send a keepalive, depending. */
|
2005-11-19 07:57:44 +01:00
|
|
|
if (now >= conn->timestamp_lastwritten + options->KeepalivePeriod) {
|
2004-07-20 21:28:02 +02:00
|
|
|
routerinfo_t *router = router_get_by_digest(conn->identity_digest);
|
2005-03-22 07:21:35 +01:00
|
|
|
if (!connection_state_is_open(conn)) {
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_OR,"Expiring non-open OR connection to fd %d (%s:%d).",
|
|
|
|
conn->s,conn->address, conn->port);
|
2004-05-12 23:12:33 +02:00
|
|
|
connection_mark_for_close(conn);
|
2004-03-08 02:19:57 +01:00
|
|
|
conn->hold_open_until_flushed = 1;
|
2005-11-26 02:43:57 +01:00
|
|
|
} else if (we_are_hibernating() && conn->n_circuits == 0 &&
|
2005-03-22 07:21:35 +01:00
|
|
|
!buf_datalen(conn->outbuf)) {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
|
|
|
|
"[Hibernating or exiting].",
|
2005-04-25 19:24:16 +02:00
|
|
|
conn->s,conn->address, conn->port);
|
2005-03-22 07:21:35 +01:00
|
|
|
connection_mark_for_close(conn);
|
|
|
|
conn->hold_open_until_flushed = 1;
|
2005-11-26 02:43:57 +01:00
|
|
|
} else if (!clique_mode(options) && conn->n_circuits &&
|
2005-11-19 07:57:44 +01:00
|
|
|
(!router || !server_mode(options) ||
|
|
|
|
!router_is_clique_mode(router))) {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_OR,"Expiring non-used OR connection to fd %d (%s:%d) "
|
|
|
|
"[Not in clique mode].",
|
2005-12-10 10:36:26 +01:00
|
|
|
conn->s,conn->address, conn->port);
|
2005-03-22 07:21:35 +01:00
|
|
|
connection_mark_for_close(conn);
|
|
|
|
conn->hold_open_until_flushed = 1;
|
2005-03-22 21:25:51 +01:00
|
|
|
} else if (
|
|
|
|
now >= conn->timestamp_lastempty + options->KeepalivePeriod*10 &&
|
|
|
|
now >= conn->timestamp_lastwritten + options->KeepalivePeriod*10) {
|
2005-12-14 21:40:40 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN,LD_PROTOCOL,
|
|
|
|
"Expiring stuck OR connection to fd %d (%s:%d). (%d bytes to "
|
|
|
|
"flush; %d seconds since last write)",
|
2005-04-25 19:24:16 +02:00
|
|
|
conn->s, conn->address, conn->port,
|
2005-03-23 00:27:29 +01:00
|
|
|
(int)buf_datalen(conn->outbuf),
|
2005-03-22 21:25:51 +01:00
|
|
|
(int)(now-conn->timestamp_lastwritten));
|
2005-03-22 07:21:35 +01:00
|
|
|
connection_mark_for_close(conn);
|
2005-05-24 00:20:54 +02:00
|
|
|
} else if (!buf_datalen(conn->outbuf)) {
|
2004-07-18 23:47:04 +02:00
|
|
|
/* either in clique mode, or we've got a circuit. send a padding cell. */
|
2005-10-25 10:20:10 +02:00
|
|
|
log_fn(LOG_DEBUG,LD_OR,"Sending keepalive to (%s:%d)",
|
2003-10-09 20:45:14 +02:00
|
|
|
conn->address, conn->port);
|
|
|
|
memset(&cell,0,sizeof(cell_t));
|
|
|
|
cell.command = CELL_PADDING;
|
|
|
|
connection_or_write_cell_to_buf(&cell, conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Perform regular maintenance tasks. This function gets run once per
|
2003-10-09 20:45:14 +02:00
|
|
|
* second by prepare_for_poll.
|
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
run_scheduled_events(time_t now)
|
|
|
|
{
|
2004-04-25 00:17:50 +02:00
|
|
|
static time_t last_rotated_certificate = 0;
|
2004-10-24 03:22:40 +02:00
|
|
|
static time_t time_to_check_listeners = 0;
|
2004-11-13 17:53:48 +01:00
|
|
|
static time_t time_to_check_descriptor = 0;
|
2005-10-13 00:41:16 +02:00
|
|
|
static time_t time_to_check_ipaddress = 0;
|
2005-05-03 01:17:08 +02:00
|
|
|
static time_t time_to_shrink_buffers = 0;
|
2005-09-22 08:34:29 +02:00
|
|
|
static time_t time_to_try_getting_descriptors = 0;
|
|
|
|
static time_t time_to_reset_descriptor_failures = 0;
|
2005-10-07 00:22:22 +02:00
|
|
|
static time_t time_to_add_entropy = 0;
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2003-10-09 20:45:14 +02:00
|
|
|
int i;
|
2005-12-15 21:44:15 +01:00
|
|
|
int have_dir_info;
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
|
2004-07-21 01:31:00 +02:00
|
|
|
/** 0. See if we've been asked to shut down and our timeout has
|
2004-10-31 21:28:41 +01:00
|
|
|
* expired; or if our bandwidth limits are exhausted and we
|
|
|
|
* should hibernate; or if it's time to wake up from hibernation.
|
2004-07-21 01:31:00 +02:00
|
|
|
*/
|
2004-10-31 21:28:41 +01:00
|
|
|
consider_hibernation(now);
|
2004-07-21 01:31:00 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** 1a. Every MIN_ONION_KEY_LIFETIME seconds, rotate the onion keys,
|
2004-04-25 00:17:50 +02:00
|
|
|
* shut down and restart all cpuworkers, and update the directory if
|
|
|
|
* necessary.
|
|
|
|
*/
|
2004-11-06 06:18:11 +01:00
|
|
|
if (server_mode(options) &&
|
|
|
|
get_onion_key_set_at()+MIN_ONION_KEY_LIFETIME < now) {
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_GENERAL,"Rotating onion key.");
|
2004-04-25 00:17:50 +02:00
|
|
|
rotate_onion_key();
|
|
|
|
cpuworkers_rotate();
|
2004-11-13 17:53:48 +01:00
|
|
|
if (router_rebuild_descriptor(1)<0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG, "Couldn't rebuild router descriptor");
|
2004-04-25 00:17:50 +02:00
|
|
|
}
|
2004-11-28 10:05:49 +01:00
|
|
|
if (advertised_server_mode())
|
2004-11-13 17:53:48 +01:00
|
|
|
router_upload_dir_desc_to_dirservers(0);
|
2004-04-25 00:17:50 +02:00
|
|
|
}
|
|
|
|
|
2005-09-22 08:34:29 +02:00
|
|
|
if (time_to_try_getting_descriptors < now) {
|
|
|
|
update_router_descriptor_downloads(now);
|
|
|
|
time_to_try_getting_descriptors = now + DESCRIPTOR_RETRY_INTERVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (time_to_reset_descriptor_failures < now) {
|
|
|
|
router_reset_descriptor_download_failures();
|
2005-12-14 21:40:40 +01:00
|
|
|
time_to_reset_descriptor_failures =
|
|
|
|
now + DESCRIPTOR_FAILURE_RESET_INTERVAL;
|
2005-09-22 08:34:29 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** 1b. Every MAX_SSL_KEY_LIFETIME seconds, we change our TLS context. */
|
2004-04-25 00:17:50 +02:00
|
|
|
if (!last_rotated_certificate)
|
|
|
|
last_rotated_certificate = now;
|
2004-10-17 23:51:20 +02:00
|
|
|
if (last_rotated_certificate+MAX_SSL_KEY_LIFETIME < now) {
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_GENERAL,"Rotating tls context.");
|
2004-11-06 07:56:28 +01:00
|
|
|
if (tor_tls_context_new(get_identity_key(), 1, options->Nickname,
|
2004-04-25 00:17:50 +02:00
|
|
|
MAX_SSL_KEY_LIFETIME) < 0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG, "Error reinitializing TLS context");
|
2004-12-13 01:44:39 +01:00
|
|
|
/* XXX is it a bug here, that we just keep going? */
|
2004-04-25 00:17:50 +02:00
|
|
|
}
|
|
|
|
last_rotated_certificate = now;
|
|
|
|
/* XXXX We should rotate TLS connections as well; this code doesn't change
|
2004-11-30 09:39:14 +01:00
|
|
|
* them at all. */
|
2004-04-25 00:17:50 +02:00
|
|
|
}
|
|
|
|
|
2005-10-07 00:22:22 +02:00
|
|
|
if (time_to_add_entropy == 0)
|
|
|
|
time_to_add_entropy = now + ENTROPY_INTERVAL;
|
|
|
|
if (time_to_add_entropy < now) {
|
|
|
|
/* We already seeded once, so don't die on failure. */
|
|
|
|
crypto_seed_rng();
|
|
|
|
time_to_add_entropy = now + ENTROPY_INTERVAL;
|
|
|
|
}
|
|
|
|
|
2004-11-03 00:47:32 +01:00
|
|
|
/** 1c. If we have to change the accounting interval or record
|
|
|
|
* bandwidth used in this accounting interval, do so. */
|
2004-11-15 05:01:31 +01:00
|
|
|
if (accounting_is_enabled(options))
|
2004-11-06 07:56:28 +01:00
|
|
|
accounting_run_housekeeping(now);
|
2004-11-03 00:47:32 +01:00
|
|
|
|
2006-01-10 08:21:01 +01:00
|
|
|
if (now % 10 == 0 && authdir_mode(options) && !we_are_hibernating()) {
|
|
|
|
/* try to determine reachability */
|
|
|
|
router_retry_connections(1, 0);
|
|
|
|
}
|
|
|
|
|
2004-11-15 10:05:54 +01:00
|
|
|
/** 2. Periodically, we consider getting a new directory, getting a
|
|
|
|
* new running-routers list, and/or force-uploading our descriptor
|
|
|
|
* (if we've passed our internal checks). */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (time_to_fetch_directory < now) {
|
2004-08-07 00:15:25 +02:00
|
|
|
/* purge obsolete entries */
|
2005-11-01 18:34:17 +01:00
|
|
|
routerlist_remove_old_routers();
|
2005-09-15 07:41:30 +02:00
|
|
|
networkstatus_list_clean(now);
|
2004-07-13 09:42:20 +02:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
/* Only caches actually need to fetch directories now. */
|
|
|
|
if (options->DirPort && !options->V1AuthoritativeDir) {
|
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_DIR, NULL, 1);
|
2004-11-15 10:05:54 +01:00
|
|
|
}
|
2004-11-21 06:14:46 +01:00
|
|
|
|
2005-09-15 07:19:38 +02:00
|
|
|
time_to_fetch_directory = now + get_dir_fetch_period(options);
|
|
|
|
|
2005-08-22 23:53:12 +02:00
|
|
|
/* Also, take this chance to remove old information from rephist
|
|
|
|
* and the rend cache. */
|
2005-07-25 12:29:21 +02:00
|
|
|
rep_history_clean(now - options->RephistTrackTime);
|
2005-08-22 23:53:12 +02:00
|
|
|
rend_cache_clean();
|
2005-12-24 00:56:42 +01:00
|
|
|
/* And while we are at it, save the state with bandwidth history
|
|
|
|
* and more. */
|
|
|
|
or_state_save();
|
2004-11-14 22:46:40 +01:00
|
|
|
}
|
|
|
|
|
2005-09-14 23:09:25 +02:00
|
|
|
/* Caches need to fetch running_routers; directory clients don't. */
|
|
|
|
if (options->DirPort && time_to_fetch_running_routers < now) {
|
2005-09-07 18:42:53 +02:00
|
|
|
if (!authdir_mode(options) || !options->V1AuthoritativeDir) {
|
2005-01-03 21:51:24 +01:00
|
|
|
directory_get_from_dirserver(DIR_PURPOSE_FETCH_RUNNING_LIST, NULL, 1);
|
2004-11-14 22:46:40 +01:00
|
|
|
}
|
2005-03-17 13:38:37 +01:00
|
|
|
time_to_fetch_running_routers = now + get_status_fetch_period(options);
|
2004-11-14 22:46:40 +01:00
|
|
|
}
|
|
|
|
|
2004-11-14 05:55:02 +01:00
|
|
|
/* 2b. Once per minute, regenerate and upload the descriptor if the old
|
|
|
|
* one is inaccurate. */
|
2004-11-13 17:53:48 +01:00
|
|
|
if (time_to_check_descriptor < now) {
|
|
|
|
time_to_check_descriptor = now + CHECK_DESCRIPTOR_INTERVAL;
|
2005-08-22 05:10:53 +02:00
|
|
|
check_descriptor_bandwidth_changed(now);
|
2005-10-13 00:41:16 +02:00
|
|
|
if (time_to_check_ipaddress < now) {
|
|
|
|
time_to_check_ipaddress = now + CHECK_IPADDRESS_INTERVAL;
|
|
|
|
check_descriptor_ipaddress_changed(now);
|
|
|
|
}
|
2005-08-22 05:10:53 +02:00
|
|
|
mark_my_descriptor_dirty_if_older_than(
|
2005-12-14 21:40:40 +01:00
|
|
|
now - FORCE_REGENERATE_DESCRIPTOR_INTERVAL);
|
2005-02-27 10:47:01 +01:00
|
|
|
consider_publishable_server(now, 0);
|
2005-03-26 06:54:50 +01:00
|
|
|
/* also, check religiously for reachability, if it's within the first
|
|
|
|
* 20 minutes of our uptime. */
|
2005-03-27 13:52:15 +02:00
|
|
|
if (server_mode(options) &&
|
2005-03-29 05:48:31 +02:00
|
|
|
stats_n_seconds_working < TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT &&
|
|
|
|
!we_are_hibernating())
|
2005-03-26 06:54:50 +01:00
|
|
|
consider_testing_reachability();
|
2005-09-08 08:22:44 +02:00
|
|
|
|
2005-09-18 04:24:42 +02:00
|
|
|
/* If any networkstatus documents are no longer recent, we need to
|
|
|
|
* update all the descriptors' running status. */
|
|
|
|
networkstatus_list_update_recent(now);
|
|
|
|
routers_update_all_from_networkstatus();
|
|
|
|
|
2005-09-14 23:09:25 +02:00
|
|
|
/* Also, once per minute, check whether we want to download any
|
|
|
|
* networkstatus documents.
|
|
|
|
*/
|
2005-10-05 07:03:52 +02:00
|
|
|
update_networkstatus_downloads(now);
|
2004-11-13 17:53:48 +01:00
|
|
|
}
|
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 3a. Every second, we examine pending circuits and prune the
|
2004-04-09 22:08:13 +02:00
|
|
|
* ones which have been pending for more than a few seconds.
|
2004-11-15 10:05:54 +01:00
|
|
|
* We do this before step 4, so it can try building more if
|
2003-11-18 08:48:00 +01:00
|
|
|
* it's not comfortable with the number of available circuits.
|
|
|
|
*/
|
2004-04-13 07:20:52 +02:00
|
|
|
circuit_expire_building(now);
|
2003-11-18 08:48:00 +01:00
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 3b. Also look at pending streams and prune the ones that 'began'
|
2004-01-20 10:21:46 +01:00
|
|
|
* a long time ago but haven't gotten a 'connected' yet.
|
2004-11-15 10:05:54 +01:00
|
|
|
* Do this before step 4, so we can put them back into pending
|
2004-01-20 10:21:46 +01:00
|
|
|
* state to be picked up by the new circuit.
|
|
|
|
*/
|
|
|
|
connection_ap_expire_beginning();
|
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 3c. And expire connections that we've held open for too long.
|
2004-03-03 06:08:01 +01:00
|
|
|
*/
|
|
|
|
connection_expire_held_open();
|
|
|
|
|
2004-10-31 21:28:41 +01:00
|
|
|
/** 3d. And every 60 seconds, we relaunch listeners if any died. */
|
|
|
|
if (!we_are_hibernating() && time_to_check_listeners < now) {
|
2005-09-14 04:36:29 +02:00
|
|
|
/* 0 means "only launch the ones that died." */
|
|
|
|
retry_all_listeners(0, NULL, NULL);
|
2004-10-24 03:22:40 +02:00
|
|
|
time_to_check_listeners = now+60;
|
|
|
|
}
|
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 4. Every second, we try a new circuit if there are no valid
|
2003-11-16 22:49:52 +01:00
|
|
|
* circuits. Every NewCircuitPeriod seconds, we expire circuits
|
Introduce a notion of 'internal' circs, which are chosen without regard
to the exit policy of the last hop. Intro and rendezvous circs must
be internal circs, to avoid leaking information. Resolve and connect
streams can use internal circs if they want.
New circuit pooling algorithm: make sure to have enough circs around
to satisfy any predicted ports, and also make sure to have 2 internal
circs around if we've required internal circs lately (with high uptime
if we've seen that lately).
Split NewCircuitPeriod config option into NewCircuitPeriod (30 secs),
which describes how often we retry making new circuits if current ones
are dirty, and MaxCircuitDirtiness (10 mins), which describes how long
we're willing to make use of an already-dirty circuit.
Once rendezvous circuits are established, keep using the same circuit as
long as you attach a new stream to it at least every 10 minutes. (So web
browsing doesn't require you to build new rend circs every 30 seconds.)
Cannibalize GENERAL circs to be C_REND, C_INTRO, S_INTRO, and S_REND
circ as necessary, if there are any completed ones lying around when
we try to launch one.
Re-instate the ifdef's to use version-0 style introduce cells, since
there was yet another bug in handling version-1 style. We'll try switching
over again after 0.0.9 is obsolete.
Bugfix: when choosing an exit node for a new non-internal circ, don't take
into account whether it'll be useful for any pending x.onion addresses --
it won't.
Bugfix: we weren't actually publishing the hidden service descriptor when
it became dirty. So we only published it every 20 minutes or so, which
means when you first start your Tor, the hidden service will seem broken.
svn:r3360
2005-01-17 19:13:09 +01:00
|
|
|
* that became dirty more than MaxCircuitDirtiness seconds ago,
|
2003-11-16 22:49:52 +01:00
|
|
|
* and we make a new circ if there are no clean circuits.
|
2003-10-09 20:45:14 +02:00
|
|
|
*/
|
2005-12-15 21:44:15 +01:00
|
|
|
have_dir_info = router_have_minimum_dir_info();
|
|
|
|
if (have_dir_info && !we_are_hibernating())
|
2004-04-13 00:47:12 +02:00
|
|
|
circuit_build_needed_circs(now);
|
2003-11-16 22:49:52 +01:00
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 5. We do housekeeping for each connection... */
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<nfds;i++) {
|
2003-10-09 20:45:14 +02:00
|
|
|
run_connection_housekeeping(i, now);
|
|
|
|
}
|
2005-05-03 01:17:08 +02:00
|
|
|
if (time_to_shrink_buffers < now) {
|
|
|
|
for (i=0;i<nfds;i++) {
|
|
|
|
connection_t *conn = connection_array[i];
|
|
|
|
if (conn->outbuf)
|
|
|
|
buf_shrink(conn->outbuf);
|
|
|
|
if (conn->inbuf)
|
|
|
|
buf_shrink(conn->inbuf);
|
|
|
|
}
|
|
|
|
time_to_shrink_buffers = now + BUF_SHRINK_INTERVAL;
|
|
|
|
}
|
2003-10-02 22:00:38 +02:00
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 6. And remove any marked circuits... */
|
2004-03-02 18:48:17 +01:00
|
|
|
circuit_close_all_marked();
|
|
|
|
|
2004-11-15 10:05:54 +01:00
|
|
|
/** 7. And upload service descriptors if necessary. */
|
2005-12-15 21:44:15 +01:00
|
|
|
if (have_dir_info && !we_are_hibernating())
|
2004-11-15 10:05:54 +01:00
|
|
|
rend_consider_services_upload(now);
|
2004-04-13 19:16:47 +02:00
|
|
|
|
2004-07-13 09:42:20 +02:00
|
|
|
/** 8. and blow away any connections that need to die. have to do this now,
|
2004-05-05 02:59:42 +02:00
|
|
|
* because if we marked a conn for close and left its socket -1, then
|
|
|
|
* we'll pass it to poll/select and bad things will happen.
|
2003-10-09 20:45:14 +02:00
|
|
|
*/
|
2005-01-12 07:42:32 +01:00
|
|
|
close_closeable_connections();
|
2003-10-09 20:45:14 +02:00
|
|
|
}
|
2002-09-26 14:09:10 +02:00
|
|
|
|
2005-05-24 19:13:15 +02:00
|
|
|
static struct event *timeout_event = NULL;
|
2005-06-13 02:35:19 +02:00
|
|
|
static int n_libevent_errors = 0;
|
2005-05-24 19:13:15 +02:00
|
|
|
|
2005-03-17 13:38:37 +01:00
|
|
|
/** Libevent callback: invoked once every second. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
second_elapsed_callback(int fd, short event, void *args)
|
2005-01-12 07:42:32 +01:00
|
|
|
{
|
|
|
|
static struct timeval one_second;
|
|
|
|
static long current_second = 0;
|
2003-10-09 20:45:14 +02:00
|
|
|
struct timeval now;
|
2005-01-12 07:42:32 +01:00
|
|
|
size_t bytes_written;
|
|
|
|
size_t bytes_read;
|
|
|
|
int seconds_elapsed;
|
2005-03-26 02:43:39 +01:00
|
|
|
or_options_t *options = get_options();
|
2005-01-12 07:42:32 +01:00
|
|
|
if (!timeout_event) {
|
|
|
|
timeout_event = tor_malloc_zero(sizeof(struct event));
|
|
|
|
evtimer_set(timeout_event, second_elapsed_callback, NULL);
|
|
|
|
one_second.tv_sec = 1;
|
|
|
|
one_second.tv_usec = 0;
|
|
|
|
}
|
2003-04-16 08:18:31 +02:00
|
|
|
|
2005-06-13 02:35:19 +02:00
|
|
|
n_libevent_errors = 0;
|
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
/* log_fn(LOG_NOTICE, "Tick."); */
|
2003-10-09 20:45:14 +02:00
|
|
|
tor_gettimeofday(&now);
|
2003-07-05 09:10:34 +02:00
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
/* the second has rolled over. check more stuff. */
|
|
|
|
bytes_written = stats_prev_global_write_bucket - global_write_bucket;
|
|
|
|
bytes_read = stats_prev_global_read_bucket - global_read_bucket;
|
2005-01-30 01:07:31 +01:00
|
|
|
/* XXX below we get suspicious if time jumps forward more than 10
|
|
|
|
* seconds, but we never notice if it jumps *back* more than 10 seconds.
|
|
|
|
* This could be useful for detecting that we just NTP'ed to three
|
|
|
|
* weeks ago and it will be 3 weeks and 15 minutes until any of our
|
|
|
|
* events trigger.
|
|
|
|
*/
|
2005-01-12 07:42:32 +01:00
|
|
|
seconds_elapsed = current_second ? (now.tv_sec - current_second) : 0;
|
|
|
|
stats_n_bytes_read += bytes_read;
|
|
|
|
stats_n_bytes_written += bytes_written;
|
2005-03-26 02:43:39 +01:00
|
|
|
if (accounting_is_enabled(options))
|
2005-01-12 07:42:32 +01:00
|
|
|
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;
|
|
|
|
|
2005-03-26 02:43:39 +01:00
|
|
|
if (server_mode(options) &&
|
2005-03-29 05:48:31 +02:00
|
|
|
!we_are_hibernating() &&
|
2005-03-27 08:50:36 +02:00
|
|
|
stats_n_seconds_working / TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT !=
|
|
|
|
(stats_n_seconds_working+seconds_elapsed) /
|
|
|
|
TIMEOUT_UNTIL_UNREACHABILITY_COMPLAINT) {
|
|
|
|
/* every 20 minutes, check and complain if necessary */
|
2005-03-26 02:43:39 +01:00
|
|
|
routerinfo_t *me = router_get_my_routerinfo();
|
2005-08-23 00:59:54 +02:00
|
|
|
if (me && !check_whether_orport_reachable())
|
2005-12-09 06:37:26 +01:00
|
|
|
warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that "
|
|
|
|
"its ORPort is reachable. Please check your firewalls, ports, "
|
|
|
|
"address, /etc/hosts file, etc.",
|
2005-12-10 10:36:26 +01:00
|
|
|
me->address, me->or_port);
|
2005-08-23 00:59:54 +02:00
|
|
|
if (me && !check_whether_dirport_reachable())
|
2005-12-09 06:37:26 +01:00
|
|
|
warn(LD_CONFIG,"Your server (%s:%d) has not managed to confirm that its "
|
|
|
|
"DirPort is reachable. Please check your firewalls, ports, "
|
|
|
|
"address, /etc/hosts file, etc.",
|
2005-12-10 10:36:26 +01:00
|
|
|
me->address, me->dir_port);
|
2005-03-26 02:43:39 +01:00
|
|
|
}
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
/* if more than 100s have elapsed, probably the clock jumped: doesn't
|
|
|
|
* count. */
|
2005-03-20 00:58:42 +01:00
|
|
|
if (seconds_elapsed < 100)
|
2005-01-12 07:42:32 +01:00
|
|
|
stats_n_seconds_working += seconds_elapsed;
|
2005-03-20 00:58:42 +01:00
|
|
|
else
|
|
|
|
circuit_note_clock_jumped(seconds_elapsed);
|
2005-01-12 07:42:32 +01:00
|
|
|
|
|
|
|
run_scheduled_events(now.tv_sec);
|
2002-10-02 01:37:31 +02:00
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
current_second = now.tv_sec; /* remember which second it is, for next time */
|
|
|
|
|
2005-07-18 21:16:17 +02:00
|
|
|
#if 0
|
2005-07-25 12:29:21 +02:00
|
|
|
if (current_second % 300 == 0) {
|
2005-07-25 12:36:01 +02:00
|
|
|
rep_history_clean(current_second - options->RephistTrackTime);
|
2005-12-14 21:40:40 +01:00
|
|
|
dumpmemusage(get_min_log_level()<LOG_INFO ?
|
|
|
|
get_min_log_level() : LOG_INFO);
|
2005-07-25 12:29:21 +02:00
|
|
|
}
|
2005-07-18 21:16:17 +02:00
|
|
|
#endif
|
2005-06-08 19:24:58 +02:00
|
|
|
|
2005-02-25 06:42:01 +01:00
|
|
|
if (evtimer_add(timeout_event, &one_second))
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_NET,
|
|
|
|
"Error from libevent when setting one-second timeout event");
|
2003-03-06 05:52:02 +01:00
|
|
|
}
|
|
|
|
|
2005-06-13 02:35:19 +02:00
|
|
|
/** Called when a possibly ignorable libevent error occurs; ensures that we
|
|
|
|
* don't get into an infinite loop by ignoring too many errors from
|
|
|
|
* libevent. */
|
2005-06-20 11:38:29 +02:00
|
|
|
static int
|
2005-06-13 02:35:19 +02:00
|
|
|
got_libevent_error(void)
|
|
|
|
{
|
|
|
|
if (++n_libevent_errors > 8) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_NET, "Too many libevent errors in one second; dying");
|
2005-06-13 02:35:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we get a SIGHUP: reload configuration files and keys,
|
2004-05-05 23:32:43 +02:00
|
|
|
* retry all connections, re-upload all descriptors, and so on. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
do_hup(void)
|
|
|
|
{
|
2004-02-26 23:30:44 +01:00
|
|
|
char keydir[512];
|
2004-11-09 08:05:53 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-02-26 23:30:44 +01:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
notice(LD_GENERAL,"Received sighup. Reloading config.");
|
2004-02-29 04:52:38 +01:00
|
|
|
has_completed_circuit=0;
|
2004-11-15 05:48:53 +01:00
|
|
|
if (accounting_is_enabled(options))
|
|
|
|
accounting_record_bandwidth_usage(time(NULL));
|
2004-11-14 23:21:23 +01:00
|
|
|
|
2005-10-06 01:20:45 +02:00
|
|
|
router_reset_warnings();
|
2005-10-05 04:20:46 +02:00
|
|
|
routerlist_reset_warnings();
|
2005-03-11 22:39:39 +01:00
|
|
|
addressmap_clear_transient();
|
2004-02-26 23:30:44 +01:00
|
|
|
/* first, reload config variables, in case they've changed */
|
|
|
|
/* no need to provide argc/v, they've been cached inside init_from_config */
|
2005-07-23 03:58:05 +02:00
|
|
|
if (options_init_from_torrc(0, NULL) < 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
err(LD_CONFIG,"Reading config failed--see warnings above. "
|
|
|
|
"For usage, try -h.");
|
2004-10-23 19:06:25 +02:00
|
|
|
return -1;
|
2004-02-26 23:30:44 +01:00
|
|
|
}
|
2004-12-07 10:11:38 +01:00
|
|
|
options = get_options(); /* they have changed now */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (authdir_mode(options)) {
|
2004-02-26 23:30:44 +01:00
|
|
|
/* reload the approved-routers file */
|
2005-12-14 21:40:40 +01:00
|
|
|
tor_snprintf(keydir, sizeof(keydir),
|
|
|
|
"%s/approved-routers", options->DataDirectory);
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_GENERAL,"Reloading approved fingerprints from \"%s\"...",keydir);
|
2004-11-28 10:05:49 +01:00
|
|
|
if (dirserv_parse_fingerprint_file(keydir) < 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
info(LD_GENERAL, "Error reloading fingerprints. "
|
|
|
|
"Continuing with old list.");
|
2004-02-26 23:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
2005-10-06 00:36:33 +02:00
|
|
|
|
|
|
|
/* Rotate away from the old dirty circuits. This has to be done
|
|
|
|
* after we've read the new options, but before we start using
|
|
|
|
* circuits for directory fetches. */
|
|
|
|
circuit_expire_all_dirty_circs();
|
|
|
|
|
2005-10-05 07:03:52 +02:00
|
|
|
/* retry appropriate downloads */
|
|
|
|
router_reset_status_download_failures();
|
|
|
|
router_reset_descriptor_download_failures();
|
|
|
|
update_networkstatus_downloads(time(NULL));
|
|
|
|
|
|
|
|
/* We'll retry routerstatus downloads in about 10 seconds; no need to
|
|
|
|
* force a retry there. */
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (server_mode(options)) {
|
2005-03-23 21:28:40 +01:00
|
|
|
const char *descriptor;
|
2004-06-06 05:38:31 +02:00
|
|
|
/* Restart cpuworker and dnsworker processes, so they get up-to-date
|
|
|
|
* configuration options. */
|
|
|
|
cpuworkers_rotate();
|
2004-08-09 09:41:32 +02:00
|
|
|
dnsworkers_rotate();
|
2005-08-15 12:27:37 +02:00
|
|
|
/* Write out a fresh descriptor, but leave old one on failure. */
|
2004-11-13 17:53:48 +01:00
|
|
|
router_rebuild_descriptor(1);
|
2005-03-23 21:28:40 +01:00
|
|
|
descriptor = router_get_my_descriptor();
|
2005-10-16 21:47:01 +02:00
|
|
|
if (descriptor) {
|
|
|
|
tor_snprintf(keydir,sizeof(keydir),"%s/router.desc",
|
|
|
|
options->DataDirectory);
|
2005-10-25 10:20:10 +02:00
|
|
|
info(LD_OR,"Saving descriptor to \"%s\"...",keydir);
|
2005-10-16 21:47:01 +02:00
|
|
|
if (write_str_to_file(keydir, descriptor, 0)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-02-26 23:30:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Tor main loop. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
do_main_loop(void)
|
|
|
|
{
|
2005-01-12 07:42:32 +01:00
|
|
|
int loop_result;
|
2003-12-05 10:51:49 +01:00
|
|
|
|
2005-12-03 03:21:31 +01:00
|
|
|
dns_init(); /* initialize dns resolve map, spawn workers if needed */
|
2005-06-19 10:35:46 +02:00
|
|
|
|
|
|
|
handle_signals(1);
|
|
|
|
|
2004-07-21 20:53:56 +02:00
|
|
|
/* load the private keys, if we're supposed to have them, and set up the
|
|
|
|
* TLS context. */
|
2004-11-21 05:19:04 +01:00
|
|
|
if (! identity_key_is_set()) {
|
|
|
|
if (init_keys() < 0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_GENERAL,"Error initializing keys; exiting");
|
2004-11-21 05:19:04 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2004-07-21 20:53:56 +02:00
|
|
|
}
|
|
|
|
|
2004-11-09 05:28:18 +01:00
|
|
|
/* Set up our buckets */
|
|
|
|
connection_bucket_init();
|
|
|
|
stats_prev_global_read_bucket = global_read_bucket;
|
|
|
|
stats_prev_global_write_bucket = global_write_bucket;
|
|
|
|
|
2004-09-03 00:08:36 +02:00
|
|
|
/* load the routers file, or assign the defaults. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (router_reload_router_list()) {
|
2004-09-08 08:52:33 +02:00
|
|
|
return -1;
|
2004-04-07 23:44:46 +02:00
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
/* load the networkstatuses. (This launches a download for new routers as
|
|
|
|
* appropriate.)
|
|
|
|
*/
|
2005-09-07 18:42:53 +02:00
|
|
|
if (router_reload_networkstatus()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-15 07:19:38 +02:00
|
|
|
directory_info_has_arrived(time(NULL),1);
|
2004-04-07 23:44:46 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (authdir_mode(get_options())) {
|
2004-07-20 12:17:43 +02:00
|
|
|
/* the directory is already here, run startup things */
|
2006-01-10 08:21:01 +01:00
|
|
|
router_retry_connections(1, 1);
|
2004-04-01 00:02:13 +02:00
|
|
|
}
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
if (server_mode(get_options())) {
|
2004-07-20 12:17:43 +02:00
|
|
|
/* launch cpuworkers. Need to do this *after* we've read the onion key. */
|
|
|
|
cpu_init();
|
2003-09-08 08:26:38 +02:00
|
|
|
}
|
2003-09-07 12:24:40 +02:00
|
|
|
|
2005-01-12 07:42:32 +01:00
|
|
|
/* set up once-a-second callback. */
|
|
|
|
second_elapsed_callback(0,0,NULL);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (;;) {
|
2005-01-27 22:45:32 +01:00
|
|
|
if (nt_service_is_stopped())
|
2004-06-12 23:43:02 +02:00
|
|
|
return 0;
|
2005-04-18 00:38:39 +02:00
|
|
|
|
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
/* Make it easier to tell whether libevent failure is our fault or not. */
|
|
|
|
errno = 0;
|
|
|
|
#endif
|
2003-09-05 08:04:03 +02:00
|
|
|
/* poll until we have an event, or the second ends */
|
2005-01-12 07:42:32 +01:00
|
|
|
loop_result = event_dispatch();
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
|
2003-10-15 20:28:32 +02:00
|
|
|
/* let catch() handle things like ^c, and otherwise don't worry about it */
|
2005-01-12 07:42:32 +01:00
|
|
|
if (loop_result < 0) {
|
2005-06-07 19:32:33 +02:00
|
|
|
int e = tor_socket_errno(-1);
|
2004-05-01 22:46:28 +02:00
|
|
|
/* let the program survive things like ^z */
|
2005-06-06 22:06:10 +02:00
|
|
|
if (e != EINTR && !ERRNO_IS_EINPROGRESS(e)) {
|
2005-04-01 04:37:40 +02:00
|
|
|
#ifdef HAVE_EVENT_GET_METHOD
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_NET,"libevent poll with %s failed: %s [%d]",
|
2005-12-10 10:36:26 +01:00
|
|
|
event_get_method(), tor_socket_strerror(e), e);
|
2005-04-01 04:37:40 +02:00
|
|
|
#else
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_NET,"libevent poll failed: %s [%d]",
|
2005-12-10 10:36:26 +01:00
|
|
|
tor_socket_strerror(e), e);
|
2005-04-01 04:37:40 +02:00
|
|
|
#endif
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
return -1;
|
2005-06-13 02:35:19 +02:00
|
|
|
#ifndef MS_WINDOWS
|
|
|
|
} else if (e == EINVAL) {
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_NET, "EINVAL from libevent: should you upgrade libevent?");
|
2005-06-13 02:35:19 +02:00
|
|
|
if (got_libevent_error())
|
|
|
|
return -1;
|
|
|
|
#endif
|
2003-10-15 20:28:32 +02:00
|
|
|
} else {
|
2005-06-06 22:06:10 +02:00
|
|
|
if (ERRNO_IS_EINPROGRESS(e))
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG,"libevent poll returned EINPROGRESS? Please report.");
|
|
|
|
debug(LD_NET,"event poll interrupted.");
|
2004-11-17 21:26:39 +01:00
|
|
|
/* You can't trust the results of this poll(). Go back to the
|
|
|
|
* top of the big for loop. */
|
|
|
|
continue;
|
2003-10-15 20:28:32 +02:00
|
|
|
}
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
}
|
2002-06-27 00:45:49 +02:00
|
|
|
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
/* refilling buckets and sending cells happens at the beginning of the
|
|
|
|
* next iteration of the loop, inside prepare_for_poll()
|
2005-01-12 07:42:32 +01:00
|
|
|
* XXXX No longer so.
|
Implemented link padding and receiver token buckets
Each socket reads at most 'bandwidth' bytes per second sustained, but
can handle bursts of up to 10*bandwidth bytes.
Cells are now sent out at evenly-spaced intervals, with padding sent
out otherwise. Set Linkpadding=0 in the rc file to send cells as soon
as they're available (and to never send padding cells).
Added license/copyrights statements at the top of most files.
router->min and router->max have been merged into a single 'bandwidth'
value. We should make the routerinfo_t reflect this (want to do that,
Mat?)
As the bandwidth increases, and we want to stop sleeping more and more
frequently to send a single cell, cpu usage goes up. At 128kB/s we're
pretty much calling poll with a timeout of 1ms or even 0ms. The current
code takes a timeout of 0-9ms and makes it 10ms. prepare_for_poll()
handles everything that should have happened in the past, so as long as
our buffers don't get too full in that 10ms, we're ok.
Speaking of too full, if you run three servers at 100kB/s with -l debug,
it spends too much time printing debugging messages to be able to keep
up with the cells. The outbuf ultimately fills up and it kills that
connection. If you run with -l err, it works fine up through 500kB/s and
probably beyond. Down the road we'll want to teach it to recognize when
an outbuf is getting full, and back off.
svn:r50
2002-07-16 03:12:15 +02:00
|
|
|
*/
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-05 07:40:47 +01:00
|
|
|
/** Used to implement the SIGNAL control command: if we accept
|
|
|
|
* <b>the_signal</b> as a remote pseudo-signal, then act on it and
|
|
|
|
* return 0. Else return -1. */
|
|
|
|
/* We don't re-use catch() here because:
|
|
|
|
* 1. We handle a different set of signals than those allowed in catch.
|
|
|
|
* 2. Platforms without signal() are unlikely to define SIGfoo.
|
|
|
|
* 3. The control spec is defined to use fixed numeric signal values
|
|
|
|
* which just happen to match the unix values.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
control_signal_act(int the_signal)
|
|
|
|
{
|
2005-02-05 22:42:46 +01:00
|
|
|
switch (the_signal)
|
2005-01-05 07:40:47 +01:00
|
|
|
{
|
|
|
|
case 1:
|
2005-01-20 21:16:48 +01:00
|
|
|
signal_callback(0,0,(void*)(uintptr_t)SIGHUP);
|
2005-01-05 07:40:47 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
2005-01-20 21:16:48 +01:00
|
|
|
signal_callback(0,0,(void*)(uintptr_t)SIGINT);
|
2005-01-05 07:40:47 +01:00
|
|
|
break;
|
|
|
|
case 10:
|
2005-01-20 21:16:48 +01:00
|
|
|
signal_callback(0,0,(void*)(uintptr_t)SIGUSR1);
|
2005-01-05 07:40:47 +01:00
|
|
|
break;
|
|
|
|
case 12:
|
2005-01-20 21:16:48 +01:00
|
|
|
signal_callback(0,0,(void*)(uintptr_t)SIGUSR2);
|
2005-01-05 07:40:47 +01:00
|
|
|
break;
|
|
|
|
case 15:
|
2005-01-20 21:16:48 +01:00
|
|
|
signal_callback(0,0,(void*)(uintptr_t)SIGTERM);
|
2005-01-05 07:40:47 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Libevent callback: invoked when we get a signal.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
signal_callback(int fd, short events, void *arg)
|
2005-01-12 07:42:32 +01:00
|
|
|
{
|
2005-01-20 21:16:48 +01:00
|
|
|
uintptr_t sig = (uintptr_t)arg;
|
2005-01-12 07:42:32 +01:00
|
|
|
switch (sig)
|
|
|
|
{
|
2002-09-28 03:40:11 +02:00
|
|
|
case SIGTERM:
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_GENERAL,"Catching signal TERM, exiting cleanly.");
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_cleanup();
|
|
|
|
exit(0);
|
2004-12-01 03:54:13 +01:00
|
|
|
break;
|
2004-07-21 01:31:00 +02:00
|
|
|
case SIGINT:
|
2005-01-12 07:42:32 +01:00
|
|
|
if (!server_mode(get_options())) { /* do it now */
|
2005-10-25 10:20:10 +02:00
|
|
|
notice(LD_GENERAL,"Interrupt: exiting cleanly.");
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_cleanup();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
hibernate_begin_shutdown();
|
2004-07-21 01:31:00 +02:00
|
|
|
break;
|
2005-01-12 07:42:32 +01:00
|
|
|
#ifdef SIGPIPE
|
2004-03-11 21:15:53 +01:00
|
|
|
case SIGPIPE:
|
2005-10-25 10:20:10 +02:00
|
|
|
debug(LD_GENERAL,"Caught sigpipe. Ignoring.");
|
2002-09-28 03:40:11 +02:00
|
|
|
break;
|
2005-01-12 07:42:32 +01:00
|
|
|
#endif
|
2002-09-28 03:40:11 +02:00
|
|
|
case SIGUSR1:
|
2005-01-12 07:42:32 +01:00
|
|
|
/* prefer to log it at INFO, but make sure we always see it */
|
|
|
|
dumpstats(get_min_log_level()<LOG_INFO ? get_min_log_level() : LOG_INFO);
|
2002-09-28 03:40:11 +02:00
|
|
|
break;
|
2004-11-20 08:33:55 +01:00
|
|
|
case SIGUSR2:
|
2005-01-12 07:42:32 +01:00
|
|
|
switch_logs_debug();
|
2005-12-14 21:40:40 +01:00
|
|
|
debug(LD_GENERAL,"Caught USR2, going to loglevel debug. "
|
|
|
|
"Send HUP to change back.");
|
2004-11-20 08:33:55 +01:00
|
|
|
break;
|
2005-01-12 07:42:32 +01:00
|
|
|
case SIGHUP:
|
|
|
|
if (do_hup() < 0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_CONFIG,"Restart failed (config error?). Exiting.");
|
2005-01-12 07:42:32 +01:00
|
|
|
tor_cleanup();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#ifdef SIGCHLD
|
2003-08-12 08:41:53 +02:00
|
|
|
case SIGCHLD:
|
2005-12-14 21:40:40 +01:00
|
|
|
while (waitpid(-1,NULL,WNOHANG) > 0) ; /* keep reaping until no more
|
|
|
|
zombies */
|
2003-09-13 23:53:38 +02:00
|
|
|
break;
|
2004-11-12 05:59:37 +01:00
|
|
|
#endif
|
2005-02-22 03:57:43 +01:00
|
|
|
}
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/**
|
|
|
|
* Write current memory uusage information to the log.
|
|
|
|
*/
|
2005-06-08 19:24:58 +02:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
dumpmemusage(int severity)
|
|
|
|
{
|
2005-06-08 19:24:58 +02:00
|
|
|
extern uint64_t buf_total_used;
|
|
|
|
extern uint64_t buf_total_alloc;
|
|
|
|
extern uint64_t rephist_total_alloc;
|
2005-07-18 08:09:04 +02:00
|
|
|
extern uint32_t rephist_total_num;
|
2005-06-08 19:24:58 +02:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
log(severity, LD_GENERAL,
|
|
|
|
"In buffers: "U64_FORMAT" used/"U64_FORMAT" allocated (%d conns).",
|
2005-06-08 19:24:58 +02:00
|
|
|
U64_PRINTF_ARG(buf_total_used), U64_PRINTF_ARG(buf_total_alloc),
|
|
|
|
nfds);
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_GENERAL, "In rephist: "U64_FORMAT" used by %d Tors.",
|
2005-07-18 08:09:04 +02:00
|
|
|
U64_PRINTF_ARG(rephist_total_alloc), rephist_total_num);
|
2005-11-23 07:00:58 +01:00
|
|
|
dump_routerlist_mem_usage(severity);
|
2005-06-08 19:24:58 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Write all statistics to the log, with log level 'severity'. Called
|
2004-05-05 23:35:12 +02:00
|
|
|
* in response to a SIGUSR1. */
|
2005-03-14 04:18:35 +01:00
|
|
|
static void
|
2005-06-11 20:52:12 +02:00
|
|
|
dumpstats(int severity)
|
|
|
|
{
|
2002-09-22 00:41:48 +02:00
|
|
|
int i;
|
|
|
|
connection_t *conn;
|
2003-10-04 05:29:09 +02:00
|
|
|
time_t now = time(NULL);
|
2005-01-10 06:10:22 +01:00
|
|
|
time_t elapsed;
|
2002-09-22 00:41:48 +02:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_GENERAL, "Dumping stats:");
|
2002-09-22 00:41:48 +02:00
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
for (i=0;i<nfds;i++) {
|
2002-09-22 00:41:48 +02:00
|
|
|
conn = connection_array[i];
|
2005-12-14 21:40:40 +01:00
|
|
|
log(severity, LD_GENERAL,
|
|
|
|
"Conn %d (socket %d) type %d (%s), state %d (%s), created %d secs ago",
|
2005-12-10 10:36:26 +01:00
|
|
|
i, conn->s, conn->type, conn_type_to_string(conn->type),
|
2005-12-14 21:40:40 +01:00
|
|
|
conn->state, conn_state_to_string(conn->type, conn->state),
|
|
|
|
(int)(now - conn->timestamp_created));
|
2004-11-28 10:05:49 +01:00
|
|
|
if (!connection_is_listener(conn)) {
|
2005-12-14 21:40:40 +01:00
|
|
|
log(severity,LD_GENERAL,
|
|
|
|
"Conn %d is to '%s:%d'.",i,safe_str(conn->address), conn->port);
|
|
|
|
log(severity,LD_GENERAL,
|
|
|
|
"Conn %d: %d bytes waiting on inbuf (len %d, last read %d secs ago)",
|
|
|
|
i,
|
2005-12-10 10:36:26 +01:00
|
|
|
(int)buf_datalen(conn->inbuf),
|
|
|
|
(int)buf_capacity(conn->inbuf),
|
|
|
|
(int)(now - conn->timestamp_lastread));
|
2005-12-14 21:40:40 +01:00
|
|
|
log(severity,LD_GENERAL,
|
|
|
|
"Conn %d: %d bytes waiting on outbuf "
|
|
|
|
"(len %d, last written %d secs ago)",i,
|
2005-12-10 10:36:26 +01:00
|
|
|
(int)buf_datalen(conn->outbuf),
|
|
|
|
(int)buf_capacity(conn->outbuf),
|
|
|
|
(int)(now - conn->timestamp_lastwritten));
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
2005-12-14 21:40:40 +01:00
|
|
|
circuit_dump_by_conn(conn, severity); /* dump info about all the circuits
|
|
|
|
* using this conn */
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_NET,
|
2005-12-31 09:09:26 +01:00
|
|
|
"Cells processed: "U64_FORMAT" padding\n"
|
|
|
|
" "U64_FORMAT" create\n"
|
|
|
|
" "U64_FORMAT" created\n"
|
|
|
|
" "U64_FORMAT" relay\n"
|
|
|
|
" ("U64_FORMAT" relayed)\n"
|
|
|
|
" ("U64_FORMAT" delivered)\n"
|
|
|
|
" "U64_FORMAT" destroy",
|
|
|
|
U64_PRINTF_ARG(stats_n_padding_cells_processed),
|
|
|
|
U64_PRINTF_ARG(stats_n_create_cells_processed),
|
|
|
|
U64_PRINTF_ARG(stats_n_created_cells_processed),
|
|
|
|
U64_PRINTF_ARG(stats_n_relay_cells_processed),
|
|
|
|
U64_PRINTF_ARG(stats_n_relay_cells_relayed),
|
|
|
|
U64_PRINTF_ARG(stats_n_relay_cells_delivered),
|
|
|
|
U64_PRINTF_ARG(stats_n_destroy_cells_processed));
|
2003-10-02 22:00:38 +02:00
|
|
|
if (stats_n_data_cells_packaged)
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity,LD_NET,"Average packaged cell fullness: %2.3f%%",
|
2005-12-10 10:36:26 +01:00
|
|
|
100*(((double)stats_n_data_bytes_packaged) /
|
|
|
|
(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
|
2003-10-15 21:25:28 +02:00
|
|
|
if (stats_n_data_cells_received)
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity,LD_NET,"Average delivered cell fullness: %2.3f%%",
|
2005-12-10 10:36:26 +01:00
|
|
|
100*(((double)stats_n_data_bytes_received) /
|
|
|
|
(stats_n_data_cells_received*RELAY_PAYLOAD_SIZE)) );
|
2003-12-05 10:51:49 +01:00
|
|
|
|
2005-01-10 06:10:22 +01:00
|
|
|
if (now - time_of_process_start >= 0)
|
|
|
|
elapsed = now - time_of_process_start;
|
|
|
|
else
|
|
|
|
elapsed = 0;
|
|
|
|
|
|
|
|
if (elapsed) {
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_NET,
|
2005-01-10 06:10:22 +01:00
|
|
|
"Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec reading",
|
2004-11-03 00:47:32 +01:00
|
|
|
U64_PRINTF_ARG(stats_n_bytes_read),
|
2005-01-10 06:10:22 +01:00
|
|
|
(int)elapsed,
|
|
|
|
(int) (stats_n_bytes_read/elapsed));
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_NET,
|
2005-01-10 06:10:22 +01:00
|
|
|
"Average bandwidth: "U64_FORMAT"/%d = %d bytes/sec writing",
|
2004-12-07 17:50:47 +01:00
|
|
|
U64_PRINTF_ARG(stats_n_bytes_written),
|
2005-01-10 06:10:22 +01:00
|
|
|
(int)elapsed,
|
|
|
|
(int) (stats_n_bytes_written/elapsed));
|
2004-12-07 17:50:47 +01:00
|
|
|
}
|
2004-11-03 17:38:04 +01:00
|
|
|
|
2005-10-25 10:20:10 +02:00
|
|
|
log(severity, LD_NET, "--------------- Dumping memory information:");
|
2005-06-08 19:24:58 +02:00
|
|
|
dumpmemusage(severity);
|
2005-06-06 19:03:21 +02:00
|
|
|
|
2004-03-20 05:59:29 +01:00
|
|
|
rep_hist_dump_stats(now,severity);
|
2004-04-09 22:02:16 +02:00
|
|
|
rend_service_dump_stats(severity);
|
2002-09-22 00:41:48 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called by exit() as we shut down the process.
|
2004-05-05 23:35:12 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
exit_function(void)
|
2004-03-10 07:26:38 +01:00
|
|
|
{
|
2004-11-12 17:39:03 +01:00
|
|
|
/* NOTE: If we ever daemonize, this gets called immediately. That's
|
|
|
|
* okay for now, because we only use this on Windows. */
|
2004-03-10 07:26:38 +01:00
|
|
|
#ifdef MS_WINDOWS
|
|
|
|
WSACleanup();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-08-08 09:25:45 +02:00
|
|
|
/** Set up the signal handlers for either parent or child. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
handle_signals(int is_parent)
|
2004-08-08 09:25:45 +02:00
|
|
|
{
|
|
|
|
#ifndef MS_WINDOWS /* do signal stuff only on unix */
|
2005-01-12 07:42:32 +01:00
|
|
|
int i;
|
|
|
|
static int signals[] = {
|
2005-01-12 13:18:46 +01:00
|
|
|
SIGINT, /* do a controlled slow shutdown */
|
|
|
|
SIGTERM, /* to terminate now */
|
|
|
|
SIGPIPE, /* otherwise sigpipe kills us */
|
|
|
|
SIGUSR1, /* dump stats */
|
|
|
|
SIGUSR2, /* go to loglevel debug */
|
|
|
|
SIGHUP, /* to reload config, retry conns, etc */
|
2004-11-12 05:59:37 +01:00
|
|
|
#ifdef SIGXFSZ
|
2005-01-12 13:18:46 +01:00
|
|
|
SIGXFSZ, /* handle file-too-big resource exhaustion */
|
2005-01-12 07:42:32 +01:00
|
|
|
#endif
|
2005-01-12 13:22:25 +01:00
|
|
|
SIGCHLD, /* handle dns/cpu workers that exit */
|
2005-01-12 07:42:32 +01:00
|
|
|
-1 };
|
|
|
|
static struct event signal_events[16]; /* bigger than it has to be. */
|
|
|
|
if (is_parent) {
|
|
|
|
for (i = 0; signals[i] >= 0; ++i) {
|
|
|
|
signal_set(&signal_events[i], signals[i], signal_callback,
|
2005-01-20 21:21:34 +01:00
|
|
|
(void*)(uintptr_t)signals[i]);
|
2005-02-25 06:42:01 +01:00
|
|
|
if (signal_add(&signal_events[i], NULL))
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG, "Error from libevent when adding event for signal %d",
|
2005-12-10 10:36:26 +01:00
|
|
|
signals[i]);
|
2005-01-12 07:42:32 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct sigaction action;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigemptyset(&action.sa_mask);
|
|
|
|
action.sa_handler = SIG_IGN;
|
2005-01-12 13:18:46 +01:00
|
|
|
sigaction(SIGINT, &action, NULL);
|
|
|
|
sigaction(SIGTERM, &action, NULL);
|
|
|
|
sigaction(SIGPIPE, &action, NULL);
|
|
|
|
sigaction(SIGUSR1, &action, NULL);
|
|
|
|
sigaction(SIGUSR2, &action, NULL);
|
|
|
|
sigaction(SIGHUP, &action, NULL);
|
2005-01-12 07:42:32 +01:00
|
|
|
#ifdef SIGXFSZ
|
2005-01-12 13:18:46 +01:00
|
|
|
sigaction(SIGXFSZ, &action, NULL);
|
2004-11-12 05:59:37 +01:00
|
|
|
#endif
|
2005-01-12 07:42:32 +01:00
|
|
|
}
|
2005-02-22 03:57:43 +01:00
|
|
|
#endif /* signal stuff */
|
2004-08-08 09:25:45 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Main entry point for the Tor command-line client.
|
2004-05-05 23:35:12 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
static int
|
|
|
|
tor_init(int argc, char *argv[])
|
|
|
|
{
|
2005-01-10 06:10:22 +01:00
|
|
|
time_of_process_start = time(NULL);
|
2005-09-30 23:22:25 +02:00
|
|
|
if (!closeable_connection_lst)
|
|
|
|
closeable_connection_lst = smartlist_create();
|
2004-11-23 10:00:55 +01:00
|
|
|
/* Initialize the history structures. */
|
|
|
|
rep_hist_init();
|
|
|
|
/* Initialize the service cache. */
|
|
|
|
rend_cache_init();
|
2005-12-14 21:40:40 +01:00
|
|
|
addressmap_init(); /* Init the client dns cache. Do it always, since it's
|
|
|
|
* cheap. */
|
2004-11-23 10:00:55 +01:00
|
|
|
|
2003-10-18 02:13:08 +02:00
|
|
|
/* give it somewhere to log to initially */
|
2004-05-20 21:47:28 +02:00
|
|
|
add_temp_log();
|
2005-01-27 22:45:32 +01:00
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
log(LOG_NOTICE, LD_GENERAL, "Tor v%s. This is experimental software. "
|
|
|
|
"Do not rely on it for strong anonymity.",VERSION);
|
2003-10-18 02:13:08 +02:00
|
|
|
|
2004-03-10 07:26:38 +01:00
|
|
|
if (network_init()<0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_NET,"Error initializing network; exiting.");
|
2004-06-12 21:45:46 +02:00
|
|
|
return -1;
|
2004-03-10 07:26:38 +01:00
|
|
|
}
|
|
|
|
atexit(exit_function);
|
|
|
|
|
2005-07-23 03:58:05 +02:00
|
|
|
if (options_init_from_torrc(argc,argv) < 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
err(LD_CONFIG,"Reading config failed--see warnings above. "
|
|
|
|
"For usage, try -h.");
|
2004-01-19 07:12:32 +01:00
|
|
|
return -1;
|
2004-11-09 05:28:18 +01:00
|
|
|
}
|
2004-01-19 07:12:32 +01:00
|
|
|
|
2003-12-14 05:39:23 +01:00
|
|
|
#ifndef MS_WINDOWS
|
2004-11-28 10:05:49 +01:00
|
|
|
if (geteuid()==0)
|
2005-12-14 21:40:40 +01:00
|
|
|
warn(LD_GENERAL,"You are running Tor as root. You don't need to, "
|
|
|
|
"and you probably shouldn't.");
|
2003-12-14 05:39:23 +01:00
|
|
|
#endif
|
2004-03-21 00:27:22 +01:00
|
|
|
|
2005-06-20 20:56:35 +02:00
|
|
|
crypto_global_init(get_options()->HardwareAccel);
|
2005-06-06 22:02:33 +02:00
|
|
|
if (crypto_seed_rng()) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_BUG, "Unable to seed random number generator. Exiting.");
|
2005-06-06 22:02:33 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2005-07-28 21:01:48 +02:00
|
|
|
|
2004-06-12 21:45:46 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-03 08:23:48 +01:00
|
|
|
/** Free all memory that we might have allocated somewhere.
|
|
|
|
* Helps us find the real leaks with dmalloc and the like.
|
|
|
|
*
|
|
|
|
* Also valgrind should then report 0 reachable in its
|
|
|
|
* leak report */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
tor_free_all(int postfork)
|
2005-02-11 00:18:39 +01:00
|
|
|
{
|
2005-09-15 01:42:06 +02:00
|
|
|
routerlist_free_all();
|
2005-02-22 01:53:08 +01:00
|
|
|
addressmap_free_all();
|
2005-05-16 12:32:10 +02:00
|
|
|
set_exit_redirects(NULL); /* free the registered exit redirects */
|
2005-02-11 00:18:39 +01:00
|
|
|
free_socks_policy();
|
|
|
|
free_dir_policy();
|
|
|
|
dirserv_free_all();
|
|
|
|
rend_service_free_all();
|
2005-02-28 23:38:00 +01:00
|
|
|
rend_cache_free_all();
|
2005-02-11 00:18:39 +01:00
|
|
|
rep_hist_free_all();
|
2005-02-11 02:26:47 +01:00
|
|
|
dns_free_all();
|
|
|
|
clear_pending_onions();
|
|
|
|
circuit_free_all();
|
2006-01-10 23:42:44 +01:00
|
|
|
entry_guards_free_all();
|
2005-02-11 02:26:47 +01:00
|
|
|
connection_free_all();
|
2005-05-03 05:51:20 +02:00
|
|
|
if (!postfork) {
|
|
|
|
config_free_all();
|
2005-10-06 01:20:45 +02:00
|
|
|
router_free_all();
|
2005-05-03 05:51:20 +02:00
|
|
|
}
|
2005-02-11 02:41:19 +01:00
|
|
|
tor_tls_free_all();
|
2005-02-11 02:26:47 +01:00
|
|
|
/* stuff in main.c */
|
|
|
|
smartlist_free(closeable_connection_lst);
|
2005-05-24 19:13:15 +02:00
|
|
|
tor_free(timeout_event);
|
2005-02-28 02:59:18 +01:00
|
|
|
|
2005-05-03 05:51:20 +02:00
|
|
|
if (!postfork) {
|
|
|
|
close_logs(); /* free log strings. do this last so logs keep working. */
|
|
|
|
}
|
2005-02-03 08:23:48 +01:00
|
|
|
}
|
|
|
|
|
2004-07-23 00:15:36 +02:00
|
|
|
/** Do whatever cleanup is necessary before shutting Tor down. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
2005-09-30 03:09:52 +02:00
|
|
|
tor_cleanup(void)
|
|
|
|
{
|
2004-11-06 06:18:11 +01:00
|
|
|
or_options_t *options = get_options();
|
2004-07-23 00:15:36 +02:00
|
|
|
/* Remove our pid file. We don't care if there was an error when we
|
|
|
|
* unlink, nothing we could do about it anyways. */
|
2004-11-28 10:05:49 +01:00
|
|
|
if (options->PidFile && options->command == CMD_RUN_TOR)
|
2004-11-06 06:18:11 +01:00
|
|
|
unlink(options->PidFile);
|
2004-11-15 05:48:53 +01:00
|
|
|
if (accounting_is_enabled(options))
|
|
|
|
accounting_record_bandwidth_usage(time(NULL));
|
2005-12-24 00:56:42 +01:00
|
|
|
or_state_save();
|
2005-05-03 05:51:20 +02:00
|
|
|
tor_free_all(0); /* move tor_free_all back into the ifdef below later. XXX*/
|
2005-02-28 03:52:51 +01:00
|
|
|
crypto_global_cleanup();
|
2005-02-03 08:25:10 +01:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
dmalloc_log_unfreed();
|
|
|
|
dmalloc_shutdown();
|
|
|
|
#endif
|
2004-06-12 21:45:46 +02:00
|
|
|
}
|
|
|
|
|
2005-01-12 13:18:46 +01:00
|
|
|
/** Read/create keys as needed, and echo our fingerprint to stdout. */
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
do_list_fingerprint(void)
|
2004-10-30 21:18:37 +02:00
|
|
|
{
|
|
|
|
char buf[FINGERPRINT_LEN+1];
|
|
|
|
crypto_pk_env_t *k;
|
2004-11-06 10:22:08 +01:00
|
|
|
const char *nickname = get_options()->Nickname;
|
2004-11-21 00:16:03 +01:00
|
|
|
if (!server_mode(get_options())) {
|
2004-11-20 09:00:57 +01:00
|
|
|
printf("Clients don't have long-term identity keys. Exiting.\n");
|
2004-11-11 14:24:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tor_assert(nickname);
|
2004-10-30 21:18:37 +02:00
|
|
|
if (init_keys() < 0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_BUG,"Error initializing keys; exiting");
|
2004-10-30 21:18:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(k = get_identity_key())) {
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_GENERAL,"Error: missing identity key.");
|
2004-10-30 21:18:37 +02:00
|
|
|
return;
|
2004-11-03 00:47:32 +01:00
|
|
|
}
|
2004-10-30 21:18:37 +02:00
|
|
|
if (crypto_pk_get_fingerprint(k, buf, 1)<0) {
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG, "Error computing fingerprint");
|
2004-10-30 21:18:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2004-11-11 14:24:10 +01:00
|
|
|
printf("%s %s\n", nickname, buf);
|
2004-10-30 21:18:37 +02:00
|
|
|
}
|
|
|
|
|
2004-11-09 19:22:17 +01:00
|
|
|
/** Entry point for password hashing: take the desired password from
|
|
|
|
* the command line, and print its salted hash to stdout. **/
|
2005-06-11 20:52:12 +02:00
|
|
|
static void
|
|
|
|
do_hash_password(void)
|
2004-11-03 20:49:03 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
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; /* Hash 64 K of data. */
|
|
|
|
secret_to_key(key+S2K_SPECIFIER_LEN, DIGEST_LEN,
|
2004-11-06 06:18:11 +01:00
|
|
|
get_options()->command_arg, strlen(get_options()->command_arg),
|
2004-11-03 20:49:03 +01:00
|
|
|
key);
|
2005-05-23 04:31:53 +02:00
|
|
|
base16_encode(output, sizeof(output), key, sizeof(key));
|
|
|
|
printf("16:%s\n",output);
|
2004-11-03 20:49:03 +01:00
|
|
|
}
|
|
|
|
|
2004-08-04 06:58:30 +02:00
|
|
|
#ifdef MS_WINDOWS_SERVICE
|
2005-07-09 18:37:12 +02:00
|
|
|
/** Checks if torrc is present in the same directory
|
|
|
|
* as the service executable.
|
|
|
|
* Return 1 if it is, 0 if it is not present. */
|
2005-09-30 03:09:52 +02:00
|
|
|
static int
|
|
|
|
nt_torrc_is_present()
|
2005-07-09 18:37:12 +02:00
|
|
|
{
|
|
|
|
HANDLE hFile;
|
|
|
|
TCHAR szPath[_MAX_PATH];
|
|
|
|
TCHAR szDrive[_MAX_DRIVE];
|
|
|
|
TCHAR szDir[_MAX_DIR];
|
|
|
|
char torrc[] = "torrc";
|
|
|
|
char *path_to_torrc;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
/* Get the service executable path */
|
|
|
|
if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
|
|
|
|
return 0;
|
|
|
|
_tsplitpath(szPath, szDrive, szDir, NULL, NULL);
|
|
|
|
|
|
|
|
/* Build the path to the torrc file */
|
|
|
|
len = _MAX_PATH + _MAX_DRIVE + _MAX_DIR + strlen(torrc) + 1;
|
|
|
|
path_to_torrc = tor_malloc(len);
|
|
|
|
if (tor_snprintf(path_to_torrc, len, "%s%s%s", szDrive, szDir, torrc)<0) {
|
|
|
|
printf("Failed: tor_snprinf()\n");
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(path_to_torrc);
|
2005-07-09 18:37:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* See if torrc is present */
|
|
|
|
hFile = CreateFile(TEXT(path_to_torrc),
|
|
|
|
GENERIC_READ, FILE_SHARE_READ, NULL,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
tor_free(path_to_torrc);
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-27 22:45:32 +01:00
|
|
|
/** If we're compile to run as an NT service, and the service has been
|
2005-02-05 22:40:25 +01:00
|
|
|
* shut down, then change our current status and return 1. Else
|
2005-01-27 22:45:32 +01:00
|
|
|
* return 0.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
nt_service_is_stopped(void)
|
|
|
|
{
|
|
|
|
if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
|
|
|
|
service_status.dwWin32ExitCode = 0;
|
|
|
|
service_status.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(hStatus, &service_status);
|
|
|
|
return 1;
|
2005-07-15 21:09:36 +02:00
|
|
|
} else if (service_status.dwCurrentState == SERVICE_STOPPED) {
|
|
|
|
return 1;
|
2005-01-27 22:45:32 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
void
|
|
|
|
nt_service_control(DWORD request)
|
2004-06-12 23:43:02 +02:00
|
|
|
{
|
2005-07-15 21:09:36 +02:00
|
|
|
static struct timeval exit_now;
|
|
|
|
exit_now.tv_sec = 0;
|
|
|
|
exit_now.tv_usec = 0;
|
|
|
|
|
2004-06-12 23:43:02 +02:00
|
|
|
switch (request) {
|
|
|
|
case SERVICE_CONTROL_STOP:
|
|
|
|
case SERVICE_CONTROL_SHUTDOWN:
|
2005-10-25 10:20:10 +02:00
|
|
|
err(LD_GENERAL, "Got stop/shutdown request; shutting down cleanly.");
|
2004-11-22 23:10:37 +01:00
|
|
|
service_status.dwCurrentState = SERVICE_STOP_PENDING;
|
2005-07-15 21:09:36 +02:00
|
|
|
event_loopexit(&exit_now);
|
2004-06-12 23:43:02 +02:00
|
|
|
return;
|
|
|
|
}
|
2004-07-23 00:15:36 +02:00
|
|
|
SetServiceStatus(hStatus, &service_status);
|
2004-06-12 23:43:02 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
void
|
|
|
|
nt_service_body(int argc, char **argv)
|
2004-06-12 23:43:02 +02:00
|
|
|
{
|
2005-11-15 04:05:23 +01:00
|
|
|
int r;
|
2004-11-22 23:10:37 +01:00
|
|
|
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
2004-06-12 23:43:02 +02:00
|
|
|
service_status.dwCurrentState = SERVICE_START_PENDING;
|
2004-07-23 01:21:12 +02:00
|
|
|
service_status.dwControlsAccepted =
|
2004-11-28 12:39:53 +01:00
|
|
|
SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
|
2004-06-12 23:43:02 +02:00
|
|
|
service_status.dwWin32ExitCode = 0;
|
|
|
|
service_status.dwServiceSpecificExitCode = 0;
|
|
|
|
service_status.dwCheckPoint = 0;
|
2004-11-22 23:10:37 +01:00
|
|
|
service_status.dwWaitHint = 1000;
|
2005-12-14 21:40:40 +01:00
|
|
|
hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME,
|
|
|
|
(LPHANDLER_FUNCTION) nt_service_control);
|
2005-01-27 22:45:32 +01:00
|
|
|
|
2004-06-12 23:43:02 +02:00
|
|
|
if (hStatus == 0) {
|
2004-11-28 12:39:53 +01:00
|
|
|
// failed;
|
|
|
|
return;
|
2004-06-12 23:43:02 +02:00
|
|
|
}
|
2005-01-27 22:45:32 +01:00
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
// check for torrc
|
|
|
|
if (nt_torrc_is_present()) {
|
2005-12-14 21:40:40 +01:00
|
|
|
r = tor_init(backup_argc, backup_argv);
|
|
|
|
// refactor this part out of tor_main and do_main_loop
|
2005-11-15 04:05:23 +01:00
|
|
|
if (r) {
|
|
|
|
r = NT_SERVICE_ERROR_TORINIT_FAILED;
|
2005-07-09 18:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2005-12-14 21:40:40 +01:00
|
|
|
err(LD_CONFIG, "torrc is not in the current working directory. "
|
|
|
|
"The Tor service will not start.");
|
2005-11-15 04:05:23 +01:00
|
|
|
r = NT_SERVICE_ERROR_NO_TORRC;
|
2005-07-09 18:37:12 +02:00
|
|
|
}
|
|
|
|
|
2005-11-15 04:05:23 +01:00
|
|
|
if (r) {
|
2004-11-28 12:39:53 +01:00
|
|
|
// failed.
|
|
|
|
service_status.dwCurrentState = SERVICE_STOPPED;
|
2005-11-15 04:05:23 +01:00
|
|
|
service_status.dwWin32ExitCode = r;
|
|
|
|
service_status.dwServiceSpecificExitCode = r;
|
2004-06-12 23:43:02 +02:00
|
|
|
SetServiceStatus(hStatus, &service_status);
|
2004-11-28 12:39:53 +01:00
|
|
|
return;
|
2004-06-12 23:43:02 +02:00
|
|
|
}
|
|
|
|
service_status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
SetServiceStatus(hStatus, &service_status);
|
|
|
|
do_main_loop();
|
|
|
|
tor_cleanup();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
void
|
|
|
|
nt_service_main(void)
|
2004-06-12 23:43:02 +02:00
|
|
|
{
|
|
|
|
SERVICE_TABLE_ENTRY table[2];
|
2004-11-22 23:10:37 +01:00
|
|
|
DWORD result = 0;
|
2005-07-09 18:37:12 +02:00
|
|
|
char *errmsg;
|
2004-11-22 23:10:37 +01:00
|
|
|
table[0].lpServiceName = GENSRV_SERVICENAME;
|
2004-06-12 23:43:02 +02:00
|
|
|
table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
|
|
|
|
table[1].lpServiceName = NULL;
|
|
|
|
table[1].lpServiceProc = NULL;
|
2005-01-27 22:45:32 +01:00
|
|
|
|
2004-11-22 23:10:37 +01:00
|
|
|
if (!StartServiceCtrlDispatcher(table)) {
|
|
|
|
result = GetLastError();
|
2005-07-09 18:37:12 +02:00
|
|
|
errmsg = nt_strerror(result);
|
|
|
|
printf("Service error %d : %s\n", result, errmsg);
|
|
|
|
LocalFree(errmsg);
|
2004-11-22 23:10:37 +01:00
|
|
|
if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
|
|
|
|
if (tor_init(backup_argc, backup_argv) < 0)
|
|
|
|
return;
|
|
|
|
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;
|
2005-05-07 06:49:09 +02:00
|
|
|
case CMD_VERIFY_CONFIG:
|
|
|
|
printf("Configuration was valid\n");
|
|
|
|
break;
|
2004-11-22 23:10:37 +01:00
|
|
|
default:
|
2005-12-14 21:40:40 +01:00
|
|
|
err(LD_CONFIG, "Illegal command number %d: internal error.",
|
|
|
|
get_options()->command);
|
2004-11-22 23:10:37 +01:00
|
|
|
}
|
|
|
|
tor_cleanup();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
SC_HANDLE
|
|
|
|
nt_service_open_scm(void)
|
|
|
|
{
|
|
|
|
SC_HANDLE hSCManager;
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE))
|
|
|
|
== NULL) {
|
2005-07-09 18:37:12 +02:00
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("OpenSCManager() failed : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
return hSCManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
SC_HANDLE
|
|
|
|
nt_service_open(SC_HANDLE hSCManager)
|
|
|
|
{
|
|
|
|
SC_HANDLE hService;
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
2005-12-14 21:40:40 +01:00
|
|
|
if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME,
|
|
|
|
SERVICE_ALL_ACCESS)) == NULL) {
|
2005-07-09 18:37:12 +02:00
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("OpenService() failed : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
return hService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_start(SC_HANDLE hService)
|
|
|
|
{
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
QueryServiceStatus(hService, &service_status);
|
|
|
|
if (service_status.dwCurrentState == SERVICE_RUNNING) {
|
|
|
|
printf("Service is already running\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StartService(hService, 0, NULL)) {
|
|
|
|
/* Loop until the service has finished attempting to start */
|
|
|
|
while (QueryServiceStatus(hService, &service_status)) {
|
|
|
|
if (service_status.dwCurrentState == SERVICE_START_PENDING)
|
|
|
|
Sleep(500);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if it started successfully or not */
|
|
|
|
if (service_status.dwCurrentState == SERVICE_RUNNING) {
|
|
|
|
printf("Service started successfully\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errmsg = nt_strerror(service_status.dwWin32ExitCode);
|
|
|
|
printf("Service failed to start : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("StartService() failed : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_stop(SC_HANDLE hService)
|
|
|
|
{
|
|
|
|
char *errmsg = NULL;
|
|
|
|
|
|
|
|
QueryServiceStatus(hService, &service_status);
|
|
|
|
if (service_status.dwCurrentState == SERVICE_STOPPED) {
|
|
|
|
printf("Service is already stopped\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ControlService(hService, SERVICE_CONTROL_STOP, &service_status)) {
|
|
|
|
while (QueryServiceStatus(hService, &service_status)) {
|
|
|
|
if (service_status.dwCurrentState == SERVICE_STOP_PENDING)
|
|
|
|
Sleep(500);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (service_status.dwCurrentState == SERVICE_STOPPED) {
|
|
|
|
printf("Service stopped successfully\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("Service failed to stop : %s\n");
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("ControlService() failed : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_install(void)
|
2004-11-22 23:10:37 +01:00
|
|
|
{
|
|
|
|
/* XXXX Problems with NT services:
|
|
|
|
* 1. The configuration file needs to be in the same directory as the .exe
|
2005-01-27 22:45:32 +01:00
|
|
|
*
|
2004-11-22 23:10:37 +01:00
|
|
|
* 2. The exe and the configuration file can't be on any directory path
|
|
|
|
* that contains a space.
|
2005-01-27 22:45:32 +01:00
|
|
|
* mje - you can quote the string (i.e., "c:\program files")
|
|
|
|
*
|
2004-11-22 23:10:37 +01:00
|
|
|
* 3. Ideally, there should be one EXE that can either run as a
|
|
|
|
* separate process (as now) or that can install and run itself
|
|
|
|
* as an NT service. I have no idea how hard this is.
|
2005-01-27 22:45:32 +01:00
|
|
|
* mje - should be done. It can install and run itself as a service
|
2004-11-22 23:10:37 +01:00
|
|
|
*
|
2004-12-01 04:48:14 +01:00
|
|
|
* Notes about developing NT services:
|
2004-11-22 23:10:37 +01:00
|
|
|
*
|
2004-12-01 04:48:14 +01:00
|
|
|
* 1. Don't count on your CWD. If an absolute path is not given, the
|
2004-11-22 23:10:37 +01:00
|
|
|
* fopen() function goes wrong.
|
|
|
|
* 2. The parameters given to the nt_service_body() function differ
|
|
|
|
* from those given to main() function.
|
|
|
|
*/
|
|
|
|
|
|
|
|
SC_HANDLE hSCManager = NULL;
|
|
|
|
SC_HANDLE hService = NULL;
|
2005-02-05 22:40:25 +01:00
|
|
|
SERVICE_DESCRIPTION sdBuff;
|
2004-11-22 23:10:37 +01:00
|
|
|
TCHAR szPath[_MAX_PATH];
|
|
|
|
TCHAR szDrive[_MAX_DRIVE];
|
|
|
|
TCHAR szDir[_MAX_DIR];
|
|
|
|
char cmd1[] = " -f ";
|
|
|
|
char cmd2[] = "\\torrc";
|
|
|
|
char *command;
|
2005-07-09 18:37:12 +02:00
|
|
|
char *errmsg;
|
2004-11-22 23:10:37 +01:00
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
if (0 == GetModuleFileName(NULL, szPath, MAX_PATH))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
_tsplitpath(szPath, szDrive, szDir, NULL, NULL);
|
2005-01-27 22:45:32 +01:00
|
|
|
|
|
|
|
/* Account for the extra quotes */
|
|
|
|
//len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
|
2005-02-05 22:40:25 +01:00
|
|
|
len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
|
2004-11-22 23:10:37 +01:00
|
|
|
command = tor_malloc(len);
|
|
|
|
|
2005-01-27 22:45:32 +01:00
|
|
|
/* Create a quoted command line, like "c:\with spaces\tor.exe" -f
|
|
|
|
* "c:\with spaces\tor.exe"
|
|
|
|
*/
|
2005-02-05 22:40:25 +01:00
|
|
|
if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
|
2005-01-27 22:45:32 +01:00
|
|
|
szPath, szDrive, szDir)<0) {
|
|
|
|
printf("Failed: tor_snprinf()\n");
|
2005-08-15 12:27:37 +02:00
|
|
|
tor_free(command);
|
2005-01-27 22:45:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2004-11-22 23:10:37 +01:00
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
if ((hSCManager = nt_service_open_scm()) == NULL) {
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(command);
|
2004-11-22 23:10:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-27 22:45:32 +01:00
|
|
|
/* 1/26/2005 mje
|
|
|
|
* - changed the service start type to auto
|
|
|
|
* - and changed the lpPassword param to "" instead of NULL as per an
|
|
|
|
* MSDN article.
|
|
|
|
*/
|
2005-12-14 21:40:40 +01:00
|
|
|
if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME,
|
|
|
|
GENSRV_DISPLAYNAME,
|
2004-11-28 12:39:53 +01:00
|
|
|
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
|
2005-12-14 21:40:40 +01:00
|
|
|
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
|
|
|
|
command,
|
2005-01-27 22:45:32 +01:00
|
|
|
NULL, NULL, NULL, NULL, "")) == NULL) {
|
2005-07-09 18:37:12 +02:00
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("CreateService() failed : %s\n", errmsg);
|
2004-11-28 12:39:53 +01:00
|
|
|
CloseServiceHandle(hSCManager);
|
2005-07-09 18:37:12 +02:00
|
|
|
LocalFree(errmsg);
|
2005-09-30 22:47:58 +02:00
|
|
|
tor_free(command);
|
2004-11-28 12:39:53 +01:00
|
|
|
return 0;
|
2004-11-22 23:10:37 +01:00
|
|
|
}
|
|
|
|
|
2005-02-05 22:40:25 +01:00
|
|
|
/* Set the service's description */
|
|
|
|
sdBuff.lpDescription = GENSRV_DESCRIPTION;
|
|
|
|
ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
|
2005-07-09 18:37:12 +02:00
|
|
|
printf("Service installed successfully\n");
|
2005-02-05 22:40:25 +01:00
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
/* Start the service initially */
|
|
|
|
nt_service_start(hService);
|
2005-01-27 22:45:32 +01:00
|
|
|
|
2004-11-22 23:10:37 +01:00
|
|
|
CloseServiceHandle(hService);
|
|
|
|
CloseServiceHandle(hSCManager);
|
2005-01-27 22:45:32 +01:00
|
|
|
tor_free(command);
|
2004-11-22 23:10:37 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_remove(void)
|
2004-11-22 23:10:37 +01:00
|
|
|
{
|
|
|
|
SC_HANDLE hSCManager = NULL;
|
|
|
|
SC_HANDLE hService = NULL;
|
|
|
|
BOOL result = FALSE;
|
2005-07-09 18:37:12 +02:00
|
|
|
char *errmsg;
|
2004-11-22 23:10:37 +01:00
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
if ((hSCManager = nt_service_open_scm()) == NULL) {
|
2004-11-22 23:10:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
if ((hService = nt_service_open(hSCManager)) == NULL) {
|
2004-11-22 23:10:37 +01:00
|
|
|
CloseServiceHandle(hSCManager);
|
2005-02-05 22:40:25 +01:00
|
|
|
return 0;
|
2004-11-22 23:10:37 +01:00
|
|
|
}
|
|
|
|
|
2005-07-09 18:37:12 +02:00
|
|
|
if (nt_service_stop(hService)) {
|
|
|
|
if (DeleteService(hService)) {
|
2005-02-05 22:40:25 +01:00
|
|
|
printf("Removed service successfully\n");
|
2005-07-09 18:37:12 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
errmsg = nt_strerror(GetLastError());
|
|
|
|
printf("DeleteService() failed : %s\n", errmsg);
|
|
|
|
LocalFree(errmsg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("Service could not be removed\n");
|
2004-11-22 23:10:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CloseServiceHandle(hService);
|
|
|
|
CloseServiceHandle(hSCManager);
|
|
|
|
|
|
|
|
return 0;
|
2004-06-12 23:43:02 +02:00
|
|
|
}
|
2005-07-09 18:37:12 +02:00
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_cmd_start(void)
|
|
|
|
{
|
|
|
|
SC_HANDLE hSCManager;
|
|
|
|
SC_HANDLE hService;
|
|
|
|
int start;
|
|
|
|
|
|
|
|
if ((hSCManager = nt_service_open_scm()) == NULL)
|
|
|
|
return -1;
|
|
|
|
if ((hService = nt_service_open(hSCManager)) == NULL) {
|
|
|
|
CloseHandle(hSCManager);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
start = nt_service_start(hService);
|
|
|
|
CloseHandle(hService);
|
|
|
|
CloseHandle(hSCManager);
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
nt_service_cmd_stop(void)
|
|
|
|
{
|
|
|
|
SC_HANDLE hSCManager;
|
|
|
|
SC_HANDLE hService;
|
|
|
|
int stop;
|
|
|
|
|
|
|
|
if ((hSCManager = nt_service_open_scm()) == NULL)
|
|
|
|
return -1;
|
|
|
|
if ((hService = nt_service_open(hSCManager)) == NULL) {
|
|
|
|
CloseHandle(hSCManager);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop = nt_service_stop(hService);
|
|
|
|
CloseHandle(hService);
|
|
|
|
CloseHandle(hSCManager);
|
|
|
|
|
|
|
|
return stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Given a Win32 error code, this attempts to make Windows
|
|
|
|
* return a human-readable error message. The char* returned
|
|
|
|
* is allocated by Windows, but should be freed with LocalFree()
|
|
|
|
* when finished with it. */
|
|
|
|
static char*
|
|
|
|
nt_strerror(uint32_t errnum)
|
|
|
|
{
|
|
|
|
char *msgbuf;
|
|
|
|
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
|
|
NULL, errnum, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
(LPSTR)&msgbuf, 0, NULL);
|
|
|
|
return msgbuf;
|
|
|
|
}
|
2004-06-12 23:43:02 +02:00
|
|
|
#endif
|
|
|
|
|
2005-10-03 22:20:38 +02:00
|
|
|
#ifdef USE_DMALLOC
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
static void
|
|
|
|
_tor_dmalloc_free(void *p)
|
|
|
|
{
|
|
|
|
tor_free(p);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** DOCDOC */
|
|
|
|
int
|
|
|
|
tor_main(int argc, char *argv[])
|
|
|
|
{
|
2005-10-03 22:20:38 +02:00
|
|
|
#ifdef USE_DMALLOC
|
2005-12-14 21:40:40 +01:00
|
|
|
int r = CRYPTO_set_mem_ex_functions(_tor_malloc, _tor_realloc,
|
|
|
|
_tor_dmalloc_free);
|
2005-10-25 10:20:10 +02:00
|
|
|
notice(LD_CONFIG, "Set up damalloc; returned %d", r);
|
2005-10-03 22:20:38 +02:00
|
|
|
#endif
|
2004-06-12 23:43:02 +02:00
|
|
|
#ifdef MS_WINDOWS_SERVICE
|
2004-11-22 23:10:37 +01:00
|
|
|
backup_argv = argv;
|
|
|
|
backup_argc = argc;
|
2005-12-14 21:40:40 +01:00
|
|
|
if ((argc >= 3) &&
|
|
|
|
(!strcmp(argv[1], "-service") || !strcmp(argv[1], "--service"))) {
|
2005-07-09 18:37:12 +02:00
|
|
|
if (!strcmp(argv[2], "install"))
|
|
|
|
return nt_service_install();
|
|
|
|
if (!strcmp(argv[2], "remove"))
|
|
|
|
return nt_service_remove();
|
|
|
|
if (!strcmp(argv[2], "start"))
|
|
|
|
return nt_service_cmd_start();
|
|
|
|
if (!strcmp(argv[2], "stop"))
|
|
|
|
return nt_service_cmd_stop();
|
|
|
|
printf("Unrecognized service command '%s'\n", argv[2]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// These are left so as not to confuse people who are used to these options
|
2005-07-09 05:28:58 +02:00
|
|
|
if (argc >= 2) {
|
|
|
|
if (!strcmp(argv[1], "-install") || !strcmp(argv[1], "--install"))
|
|
|
|
return nt_service_install();
|
|
|
|
if (!strcmp(argv[1], "-remove") || !strcmp(argv[1], "--remove"))
|
|
|
|
return nt_service_remove();
|
|
|
|
if (!strcmp(argv[1], "-nt-service") || !strcmp(argv[1], "--nt-service")) {
|
|
|
|
nt_service_main();
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-05 22:40:25 +01:00
|
|
|
}
|
2005-02-23 06:34:25 +01:00
|
|
|
#endif
|
2004-06-12 21:45:46 +02:00
|
|
|
if (tor_init(argc, argv)<0)
|
|
|
|
return -1;
|
2004-11-06 06:18:11 +01:00
|
|
|
switch (get_options()->command) {
|
2004-10-30 21:18:37 +02:00
|
|
|
case CMD_RUN_TOR:
|
2005-03-14 04:18:35 +01:00
|
|
|
#ifdef MS_WINDOWS_SERVICE
|
2005-02-23 06:34:25 +01:00
|
|
|
service_status.dwCurrentState = SERVICE_RUNNING;
|
|
|
|
#endif
|
2004-10-30 21:18:37 +02:00
|
|
|
do_main_loop();
|
|
|
|
break;
|
|
|
|
case CMD_LIST_FINGERPRINT:
|
|
|
|
do_list_fingerprint();
|
|
|
|
break;
|
2004-11-03 20:49:03 +01:00
|
|
|
case CMD_HASH_PASSWORD:
|
|
|
|
do_hash_password();
|
|
|
|
break;
|
2005-05-07 06:49:09 +02:00
|
|
|
case CMD_VERIFY_CONFIG:
|
|
|
|
printf("Configuration was valid\n");
|
|
|
|
break;
|
2004-10-30 21:18:37 +02:00
|
|
|
default:
|
2005-10-25 10:20:10 +02:00
|
|
|
warn(LD_BUG,"Illegal command number %d: internal error.",
|
|
|
|
get_options()->command);
|
2004-10-30 21:18:37 +02:00
|
|
|
}
|
2004-06-12 21:45:46 +02:00
|
|
|
tor_cleanup();
|
2003-09-29 09:50:08 +02:00
|
|
|
return -1;
|
2002-06-27 00:45:49 +02:00
|
|
|
}
|
2005-10-05 04:06:36 +02:00
|
|
|
|